-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathroots.go
337 lines (259 loc) · 7.33 KB
/
roots.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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"os/user"
"path"
"runtime"
"strings"
cli "github.com/jawher/mow.cli"
"github.com/seantis/roots/pkg/image"
_ "github.com/seantis/roots/pkg/provider" // to register providers
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
app := cli.App("roots", "Download and extract containers")
ctx := newInterruptableContext()
// disable datetime output
log.SetFlags(0)
app.Command("version", "Show version", func(cmd *cli.Cmd) {
cmd.Action = func() {
fmt.Printf("roots %s, commit %s, built at %s\n", version, commit, date)
}
})
app.Command("digest", "Show the latest digest", func(cmd *cli.Cmd) {
cmd.Spec = "CONTAINER [--auth] [--arch] [--os]"
var (
url = newURLArg(cmd)
auth = newAuthOpt(cmd)
arch = newArchOpt(cmd)
ops = newOSOpt(cmd)
)
cmd.Action = func() {
digest, err := newRemote(ctx, url, auth, arch, ops).Digest()
if err != nil {
log.Fatal(err)
}
fmt.Println(digest)
}
})
app.Command("purge", "Purge unused files from the cache", func(cmd *cli.Cmd) {
cmd.Spec = "[--cache]"
var (
cache = newCacheOpt(cmd)
)
cmd.Action = func() {
// setup the cache
if *cache == "" {
*cache = os.Getenv("ROOTS_CACHE")
}
if *cache == "" {
*cache = defaultCache()
}
entries, err := os.ReadDir(*cache)
if err != nil {
log.Fatalf("error accessing %s: %v", *cache, err)
}
if len(entries) == 0 {
log.Fatalf("not a cache directory: %s", *cache)
}
valid := false
for _, info := range entries {
if info.Name() == "layers" {
valid = true
break
}
}
if !valid {
log.Fatalf("not a cache directory: %s", *cache)
}
store, err := image.NewStore(*cache)
if err != nil {
log.Fatalf("could not create store at %s: %v", *cache, err)
}
if err := store.Purge(); err != nil {
log.Fatalf("error during purge of %s: %v", *cache, err)
}
}
})
app.Command("pull", "Download and extract", func(cmd *cli.Cmd) {
cmd.Spec = "CONTAINER DEST [--auth] [--arch] [--os] [--cache] [--force]"
var (
url = newURLArg(cmd)
dest = newDestArg(cmd)
auth = newAuthOpt(cmd)
arch = newArchOpt(cmd)
ops = newOSOpt(cmd)
cache = newCacheOpt(cmd)
force = newForceOpt(cmd)
)
cmd.Action = func() {
// setup the cache
if *cache == "" {
*cache = os.Getenv("ROOTS_CACHE")
}
if strings.ToLower(*cache) == "no" {
temp, err := os.MkdirTemp("", "store")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(temp)
*cache = temp
}
if *cache == "" {
*cache = defaultCache()
}
if err := os.MkdirAll(*cache, 0755); err != nil {
log.Fatalf("could not create cache at %s: %v", *cache, err)
}
store, err := image.NewStore(*cache)
if err != nil {
log.Fatalf("could not create store at %s: %v", *cache, err)
}
// create the destination
if *force {
// let's not be responsible for wiping out an actual root fs
if strings.Count(*dest, "/") <= 2 {
log.Fatalf("not enough path separators to force-remove: %s", *dest)
}
if err := os.RemoveAll(*dest); err != nil {
log.Fatalf("could note force-remove %s: %v", *dest, err)
}
}
if err := os.MkdirAll(*dest, 0755); err != nil {
log.Fatalf("could not create destination at %s: %v", *dest, err)
}
// pull & extract the image
remote := newRemote(ctx, url, auth, arch, ops)
if err := store.Extract(ctx, remote, *dest); err != nil {
log.Fatalf("error during pull: %v", err)
}
}
})
err := app.Run(os.Args)
if err != nil {
log.Fatalf("error running command: %v", err)
}
}
func defaultCache() string {
usr, err := user.Current()
if err != nil {
log.Fatalf("error looking up current user: %v", err)
}
if usr.Uid == "0" || usr.HomeDir == "" {
return "/var/cache/roots"
}
return path.Join(usr.HomeDir, ".cache", "seantis", "roots")
}
func newInterruptableContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
signal.Stop(c)
cancel()
}()
return ctx
}
func newRemote(ctx context.Context, urlstring, auth, arch, ops *string) *image.Remote {
if *auth == "" {
*auth = os.Getenv("ROOTS_AUTH")
}
if *arch == "" {
*arch = os.Getenv("ROOTS_ARCH")
}
if *ops == "" {
*ops = os.Getenv("ROOTS_OS")
}
url, err := image.Parse(*urlstring)
if err != nil {
log.Fatalf("failed to parse image url %s: %v", *urlstring, err)
}
remote, err := image.NewRemote(ctx, *url, *auth)
if err != nil {
log.Fatalf("failed to connect to %s: %v", *urlstring, err)
}
if len(*arch) > 0 || len(*ops) > 0 {
if len(*arch) == 0 {
*arch = runtime.GOARCH
}
if len(*ops) == 0 {
*ops = "linux"
}
remote.WithPlatform(&image.Platform{
Architecture: *arch,
OS: *ops,
})
}
return remote
}
func newURLArg(cmd *cli.Cmd) *string {
return cmd.StringArg("CONTAINER", "",
`The url of the container, example values:
- ubuntu:latest
- gcr.io/google-containers/etcd:3.3.10
`)
}
func newDestArg(cmd *cli.Cmd) *string {
return cmd.StringArg("DEST", "", "The destination folder")
}
func newAuthOpt(cmd *cli.Cmd) *string {
return cmd.StringOpt("auth", "",
`Authentication for the following providers:
* Google Container Registry:
Path to service worker json file, with the following scope:
<https://www.googleapis.com/auth/devstorage.read_only>
This value can also be set through the env var ROOTS_AUTH,
though the flag takes precedence.
`)
}
func newArchOpt(cmd *cli.Cmd) *string {
return cmd.StringOpt("arch", "",
`Force the given architecture, example values:
* amd64
* arm
See https://github.com/golang/go/blob/master/src/go/build/syslist.go
Requires multi-arch support by the container.
This value can also be set through the env var ROOTS_ARCH,
though the flag takes precedence.
`)
}
func newOSOpt(cmd *cli.Cmd) *string {
return cmd.StringOpt("os", "",
`Force the given OS, example values:
* linux
* windows
See https://github.com/golang/go/blob/master/src/go/build/syslist.go
Requires multi-arch support by the container.
This value can also be set through the env var ROOTS_OS,
though the flag takes precedence.
`)
}
func newCacheOpt(cmd *cli.Cmd) *string {
return cmd.StringOpt("cache", "",
`Sets the cache folder that should be used. Defaults:
* For non-root users:
~/.cache/seantis/roots
* For root users:
/var/cache/roots
If the special value 'no' is given, a temporary folder will
be used during the lifetime of the process.
This value can also be set through the env var ROOTS_CACHE,
though the flag takes precedence.
`)
}
func newForceOpt(cmd *cli.Cmd) *bool {
return cmd.BoolOpt("force", false, `Remove the destination before pulling
Note that this only works if there are at least two path
separatores in the destination. So you can force remove
/var/roots/ubuntu, but not / or /var/lib.
`)
}