This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
forked from drone-plugins/drone-s3-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
137 lines (111 loc) · 2.85 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"io/ioutil"
pathutil "path"
"time"
"os"
"github.com/drone/drone-cache-lib/archive/util"
"github.com/drone/drone-cache-lib/cache"
"github.com/drone/drone-cache-lib/storage"
log "github.com/sirupsen/logrus"
)
// Plugin structure
type Plugin struct {
Filename string
Path string
FallbackPath string
FlushPath string
Mode string
FlushAge int
Mount []string
Cacert string
CacertPath string
Workdir string
Storage storage.Storage
}
const (
// RestoreMode for resotre mode string
RestoreMode = "restore"
// RebuildMode for rebuild mode string
RebuildMode = "rebuild"
// FlushMode for flush mode string
FlushMode = "flush"
)
// Exec runs the plugin
func (p *Plugin) Exec() error {
var err error
at, err := util.FromFilename(p.Filename)
if err != nil {
return err
}
c := cache.New(p.Storage, at)
path := pathutil.Join(p.Path, p.Filename)
fallbackPath := pathutil.Join(p.FallbackPath, p.Filename)
if p.Workdir != "" {
os.MkdirAll(p.Workdir, os.ModePerm) // ensure p.Workdir
err = os.Chdir(p.Workdir)
if err == nil {
log.Infof("Sucessfully changed workdir to %s", p.Workdir)
} else {
log.Errorf("Unable to change workdir to %s", p.Workdir)
return err
}
}
if p.Cacert != "" {
certPath := "/etc/ssl/certs/ca-certificates.crt"
log.Infof("Installing new ca certificate at %s", certPath)
err := installCaCert(certPath, p.Cacert)
if err == nil {
log.Info("Successfully installed new certificate")
}
}
if p.CacertPath != "" {
certPath := "/etc/ssl/certs/ca-certificates.crt"
log.Infof("Installing new ca certificate at %s", certPath)
err := installCaCertFromPath(certPath, p.CacertPath)
if err == nil {
log.Info("Successfully installed new certificate")
}
}
if p.Mode == RebuildMode {
log.Infof("Rebuilding cache at %s", path)
err = c.Rebuild(p.Mount, path)
if err == nil {
log.Infof("Cache rebuilt")
}
}
if p.Mode == RestoreMode {
log.Infof("Restoring cache at %s", path)
err = c.Restore(path, fallbackPath)
if err == nil {
log.Info("Cache restored")
}
}
if p.Mode == FlushMode {
log.Infof("Flushing cache items older than %d days at %s", p.FlushAge, path)
f := cache.NewFlusher(p.Storage, genIsExpired(p.FlushAge))
err = f.Flush(p.FlushPath)
if err == nil {
log.Info("Cache flushed")
}
}
return err
}
func genIsExpired(age int) cache.DirtyFunc {
return func(file storage.FileEntry) bool {
// Check if older than "age" days
return file.LastModified.Before(time.Now().AddDate(0, 0, age*-1))
}
}
func installCaCert(path, cacert string) error {
err := ioutil.WriteFile(path, []byte(cacert), 0644)
return err
}
func installCaCertFromPath(path, cacertPath string) error {
cacert, err := ioutil.ReadFile(cacertPath)
if err != nil {
return err
}
err = ioutil.WriteFile(path, []byte(cacert), 0644)
return err
}