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
/
main.go
347 lines (297 loc) · 7.23 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"errors"
"fmt"
"os"
pathutil "path"
"strconv"
"strings"
"github.com/drone-plugins/drone-s3-cache/storage/s3"
"github.com/drone/drone-cache-lib/storage"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
version = "unknown"
)
func main() {
app := cli.NewApp()
app.Name = "cache plugin"
app.Usage = "cache plugin"
app.Action = run
app.Version = version
app.Flags = []cli.Flag{
// Cache information
cli.StringFlag{
Name: "filename",
Usage: "filename for the cache",
EnvVar: "PLUGIN_FILENAME",
},
cli.StringFlag{
Name: "root",
Usage: "root",
EnvVar: "PLUGIN_ROOT",
},
cli.StringFlag{
Name: "path",
Usage: "path",
EnvVar: "PLUGIN_PATH",
},
cli.StringFlag{
Name: "fallback_path",
Usage: "fallback_path",
EnvVar: "PLUGIN_FALLBACK_PATH",
},
cli.StringSliceFlag{
Name: "mount",
Usage: "cache directories",
EnvVar: "PLUGIN_MOUNT",
},
cli.BoolFlag{
Name: "rebuild",
Usage: "rebuild the cache directories",
EnvVar: "PLUGIN_REBUILD",
},
cli.BoolFlag{
Name: "restore",
Usage: "restore the cache directories",
EnvVar: "PLUGIN_RESTORE",
},
cli.BoolFlag{
Name: "flush",
Usage: "flush the cache",
EnvVar: "PLUGIN_FLUSH",
},
cli.StringFlag{
Name: "flush_age",
Usage: "flush cache files older than # days",
EnvVar: "PLUGIN_FLUSH_AGE",
Value: "30",
},
cli.StringFlag{
Name: "flush_path",
Usage: "path to search for flushable cache files",
EnvVar: "PLUGIN_FLUSH_PATH",
},
cli.StringFlag{
Name: "workdir",
Usage: "path where the cache will be extracted to",
EnvVar: "PLUGIN_WORKDIR",
},
cli.BoolFlag{
Name: "debug",
Usage: "debug plugin output",
EnvVar: "PLUGIN_DEBUG",
},
// Build information (for setting defaults)
cli.StringFlag{
Name: "repo.owner",
Usage: "repository owner",
EnvVar: "DRONE_REPO_OWNER",
},
cli.StringFlag{
Name: "repo.name",
Usage: "repository name",
EnvVar: "DRONE_REPO_NAME",
},
cli.StringFlag{
Name: "repo.branch",
Value: "master",
Usage: "repository default branch",
EnvVar: "DRONE_REPO_BRANCH",
},
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
// S3 information
cli.StringFlag{
Name: "server",
Usage: "s3 server",
EnvVar: "PLUGIN_SERVER,PLUGIN_ENDPOINT,CACHE_S3_ENDPOINT,CACHE_S3_SERVER,S3_ENDPOINT",
},
cli.StringFlag{
Name: "accelerated-endpoint",
Usage: "s3 accelerated endpoint",
EnvVar: "PLUGIN_ACCELERATED_ENDPOINT,CACHE_S3_ACCELERATED_ENDPOINT",
},
cli.StringFlag{
Name: "access-key",
Usage: "s3 access key",
EnvVar: "PLUGIN_ACCESS_KEY,CACHE_S3_ACCESS_KEY,AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: "secret-key",
Usage: "s3 secret key",
EnvVar: "PLUGIN_SECRET_KEY,CACHE_S3_SECRET_KEY,AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: "session-token",
Usage: "s3 session token",
EnvVar: "PLUGIN_SESSION_TOKEN,CACHE_S3_SESSION_TOKEN,AWS_SESSION_TOKEN",
},
cli.StringFlag{
Name: "region",
Usage: "s3 region",
EnvVar: "PLUGIN_REGION,CACHE_S3_REGION",
},
cli.StringFlag{
Name: "ca_cert",
Usage: "ca cert to connect to s3 server",
EnvVar: "PLUGIN_CA_CERT,CACHE_S3_CA_CERT",
},
cli.StringFlag{
Name: "ca_cert_path",
Usage: "ca cert to connect to s3 server",
EnvVar: "PLUGIN_CA_CERT_PATH,CACHE_S3_CA_CERT_PATH",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
// Set the logging level
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
// Determine the mode for the plugin
rebuild := c.Bool("rebuild")
restore := c.Bool("restore")
flush := c.Bool("flush")
if isMultipleModes(rebuild, restore, flush) {
return errors.New("Must use a single mode: rebuild, restore or flush")
} else if !rebuild && !restore && !flush {
return errors.New("No action specified")
}
var mode string
var mount []string
if rebuild {
// Look for the mount points to rebuild
mount = c.StringSlice("mount")
if len(mount) == 0 {
return errors.New("No mounts specified")
}
mode = RebuildMode
} else if flush {
mode = FlushMode
} else {
mode = RestoreMode
}
// Get the working directory
workdir := c.String("workdir")
// Get the root path prefix to place the cache files
root := c.GlobalString("root")
// Get the path to place the cache files
path := c.GlobalString("path")
// Defaults to <owner>/<repo>/<branch>/
if len(path) == 0 {
log.Info("No path specified. Creating default")
path = fmt.Sprintf(
"%s/%s/%s",
c.String("repo.owner"),
c.String("repo.name"),
c.String("commit.branch"),
)
path = prefixRoot(root, path)
}
// Get the fallback path to retrieve the cache files
fallbackPath := c.GlobalString("fallback_path")
// Defaults to <owner>/<repo>/master/
if len(fallbackPath) == 0 {
log.Info("No fallback_path specified. Creating default")
fallbackPath = fmt.Sprintf(
"%s/%s/%s",
c.String("repo.owner"),
c.String("repo.name"),
c.String("repo.branch"),
)
fallbackPath = prefixRoot(root, fallbackPath)
}
// Get the flush path to flush the cache files from
flushPath := c.GlobalString("flush_path")
// Defaults to <owner>/<repo>/
if len(flushPath) == 0 {
log.Info("No flush_path specified. Creating default")
flushPath = fmt.Sprintf(
"%s/%s",
c.String("repo.owner"),
c.String("repo.name"),
)
flushPath = prefixRoot(root, flushPath)
}
// Get the filename
filename := c.GlobalString("filename")
if len(filename) == 0 {
log.Info("No filename specified. Creating default")
filename = "archive.tar"
}
s, err := s3Storage(c)
if err != nil {
return err
}
flushAge, err := strconv.Atoi(c.String("flush_age"))
if err != nil {
return err
}
p := &Plugin{
Filename: filename,
Path: path,
FallbackPath: fallbackPath,
FlushPath: flushPath,
Mode: mode,
FlushAge: flushAge,
Mount: mount,
Storage: s,
Cacert: c.String("ca_cert"),
CacertPath: c.String("ca_cert_path"),
Workdir: workdir,
}
return p.Exec()
}
func s3Storage(c *cli.Context) (storage.Storage, error) {
// Get the endpoint
server := c.String("server")
var endpoint string
var useSSL bool
if len(server) > 0 {
useSSL = strings.HasPrefix(server, "https://")
if !useSSL {
if !strings.HasPrefix(server, "http://") {
return nil, fmt.Errorf("Invalid server %s. Needs to be a HTTP URI", server)
}
endpoint = server[7:]
} else {
endpoint = server[8:]
}
} else {
endpoint = "s3.amazonaws.com"
useSSL = true
}
return s3.New(&s3.Options{
Endpoint: endpoint,
AcceleratedEndpoint: c.String("accelerated-endpoint"),
Access: c.String("access-key"),
Secret: c.String("secret-key"),
Token: c.String("session-token"),
Region: c.String("region"),
UseSSL: useSSL,
})
}
func isMultipleModes(bools ...bool) bool {
var b bool
for _, v := range bools {
if b && b == v {
return true
}
if v {
b = true
}
}
return false
}
func prefixRoot(root, path string) string {
return pathutil.Clean(fmt.Sprintf("/%s/%s", root, path))
}