/
cli.go
525 lines (470 loc) · 14.8 KB
/
cli.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package crane
import (
"fmt"
"os"
"strings"
"github.com/alecthomas/kingpin"
)
var cfg Config
var allowed []string
var defaultFiles = []string{"docker-compose.yml", "docker-compose.override.yml", "crane.yml", "crane.override.yml"}
var (
app = kingpin.New("crane", "Lift containers with ease - https://www.craneup.tech").Interspersed(false).DefaultEnvars()
verboseFlag = app.Flag("verbose", "Enable verbose output.").Short('v').Bool()
dryRunFlag = app.Flag("dry-run", "Dry run (implicit verbose, no side effects).").Bool()
configFlag = app.Flag(
"config",
"Location of config file.",
).Short('c').Default(defaultFiles...).PlaceHolder("~/crane.yml").Strings()
prefixFlag = app.Flag(
"prefix",
"Container/Network/Volume prefix.",
).Short('p').String()
excludeFlag = app.Flag(
"exclude",
"Exclude group or container. Can be repeated.",
).Short('x').PlaceHolder("container|group").Strings()
onlyFlag = app.Flag(
"only",
"Include only group or container.",
).Short('o').PlaceHolder("container|group").String()
extendFlag = app.Flag(
"extend",
"Extend command from target to dependencies",
).Short('e').Bool()
tagFlag = app.Flag(
"tag",
"Override image tags.",
).String()
upCommand = app.Command(
"up",
"Build or pull images if they don't exist, then run or start the containers.",
)
upNoCacheFlag = upCommand.Flag(
"no-cache",
"Build the image without any cache.",
).Short('n').Bool()
upParallelFlag = upCommand.Flag(
"parallel",
"Define how many containers are provisioned in parallel.",
).Short('l').Default("1").Int()
upDetachFlag = upCommand.Flag(
"detach",
"Detach from container",
).Short('d').Bool()
upTargetArg = upCommand.Arg("target", "Target of command").String()
upCmdArg = upCommand.Arg("cmd", "Command for container").Strings()
liftCommand = app.Command(
"lift",
"Build or pull images if they don't exist, then run or start the containers.",
)
liftNoCacheFlag = liftCommand.Flag(
"no-cache",
"Build the image without any cache.",
).Short('n').Bool()
liftParallelFlag = liftCommand.Flag(
"parallel",
"Define how many containers are provisioned in parallel.",
).Short('l').Default("1").Int()
liftDetachFlag = liftCommand.Flag(
"detach",
"Detach from container",
).Short('d').Bool()
liftTargetArg = liftCommand.Arg("target", "Target of command").String()
liftCmdArg = liftCommand.Arg("cmd", "Command for container").Strings()
versionCommand = app.Command(
"version",
"Displays the current version and checks for update.",
)
versionNoCheckFlag = versionCommand.Flag(
"no-check",
"Don't check for updates.",
).Short('n').Bool()
statsCommand = app.Command(
"stats",
"Displays statistics about containers.",
)
statsNoStreamFlag = statsCommand.Flag(
"no-stream",
"Disable stats streaming (Docker >= 1.7).",
).Short('n').Bool()
statsTargetArg = statsCommand.Arg("target", "Target of command").String()
statusCommand = app.Command(
"status",
"Displays status of containers.",
)
noTruncFlag = statusCommand.Flag(
"no-trunc",
"Don't truncate output.",
).Bool()
statusTargetArg = statusCommand.Arg("target", "Target of command").String()
pushCommand = app.Command(
"push",
"Push the containers to the registry.",
)
pushTargetArg = pushCommand.Arg("target", "Target of command").String()
pauseCommand = app.Command(
"pause",
"Pause the containers.",
)
pauseTargetArg = pauseCommand.Arg("target", "Target of command").String()
unpauseCommand = app.Command(
"unpause",
"Unpause the containers.",
)
unpauseTargetArg = unpauseCommand.Arg("target", "Target of command").String()
startCommand = app.Command(
"start",
"Start the containers.",
)
startTargetArg = startCommand.Arg("target", "Target of command").String()
stopCommand = app.Command(
"stop",
"Stop the containers.",
)
stopTargetArg = stopCommand.Arg("target", "Target of command").String()
killCommand = app.Command(
"kill",
"Kill the containers.",
)
killTargetArg = killCommand.Arg("target", "Target of command").String()
execCommand = app.Command(
"exec",
"Execute command in the container(s).",
)
execPrivilegedFlag = execCommand.Flag(
"privileged",
"Give extended privileges to the process.",
).Bool()
execUserFlag = execCommand.Flag(
"user",
"Run the command as this user.",
).String()
execTargetArg = execCommand.Arg("target", "Target of command").String()
execCmdArg = execCommand.Arg("cmd", "Command for container").Strings()
rmCommand = app.Command(
"rm",
"Remove the containers.",
)
rmForceFlag = rmCommand.Flag(
"force",
"Kill containers if they are running first.",
).Short('f').Bool()
rmVolumesFlag = provisionCommand.Flag(
"volumes",
"Remove volumes as well.",
).Bool()
rmTargetArg = rmCommand.Arg("target", "Target of command").String()
runCommand = app.Command(
"run",
"Run the containers.",
)
runDetachFlag = runCommand.Flag(
"detach",
"Detach from container",
).Short('d').Bool()
runTargetArg = runCommand.Arg("target", "Target of command").String()
runCmdArg = runCommand.Arg("cmd", "Command for container").Strings()
createCommand = app.Command(
"create",
"Create the containers.",
)
createTargetArg = createCommand.Arg("target", "Target of command").String()
createCmdArg = createCommand.Arg("cmd", "Command for container").Strings()
provisionCommand = app.Command(
"provision",
"Build or pull images.",
)
provisionNoCacheFlag = provisionCommand.Flag(
"no-cache",
"Build the image without any cache.",
).Short('n').Bool()
provisionParallelFlag = provisionCommand.Flag(
"parallel",
"Define how many containers are provisioned in parallel.",
).Short('l').Default("1").Int()
provisionTargetArg = provisionCommand.Arg("target", "Target of command").String()
pullCommand = app.Command(
"pull",
"Pull images.",
)
pullTargetArg = pullCommand.Arg("target", "Target of command").String()
logsCommand = app.Command(
"logs",
"Display container logs.",
)
followFlag = logsCommand.Flag(
"follow",
"Follow log output.",
).Short('f').Bool()
tailFlag = logsCommand.Flag(
"tail",
"Output the specified number of lines at the end of logs.",
).String()
timestampsFlag = logsCommand.Flag(
"timestamps",
"Show timestamps.",
).Short('t').Bool()
colorizeFlag = logsCommand.Flag(
"colorize",
"Output the lines with one color per container.",
).Short('z').Bool()
sinceFlag = logsCommand.Flag(
"since",
"Show logs since timestamp (Docker >= 1.7).",
).String()
logsTargetArg = logsCommand.Arg("target", "Target of command").String()
generateCommand = app.Command(
"generate",
"Generate files by passing the config to a given template.",
)
templateFlag = generateCommand.Flag(
"template",
"Template to use.",
).Short('t').String()
outputFlag = generateCommand.Flag(
"output",
"The file(s) to write the output to.",
).Short('O').String()
generateTargetArg = generateCommand.Arg("target", "Target of command").String()
amCommand = app.Command(
"am",
"Sub-commands for accelerated mounts",
)
amResetCommand = amCommand.Command(
"reset",
"Resets accelerated mount",
)
amResetTargetArg = amResetCommand.Arg("target", "Target of command").String()
amLogsCommand = amCommand.Command(
"logs",
"Show logs of accelerated mount",
)
amFollowFlag = amLogsCommand.Flag(
"follow",
"Follow log output.",
).Short('f').Bool()
amLogsTargetArg = amLogsCommand.Arg("target", "Target of command").String()
)
func isVerbose() bool {
return *verboseFlag || *dryRunFlag
}
func isDryRun() bool {
return *dryRunFlag
}
func commandAction(targetArg string, wrapped func(unitOfWork *UnitOfWork), mightStartRelated bool) {
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
allowed = allowedContainers(*excludeFlag, *onlyFlag)
dependencyMap := cfg.DependencyMap()
target, err := NewTarget(dependencyMap, targetArg, *extendFlag)
if err != nil {
panic(StatusError{err, 78})
}
unitOfWork, err := NewUnitOfWork(dependencyMap, target.all())
if err != nil {
panic(StatusError{err, 78})
}
if isVerbose() {
printInfof("Command will be applied to: %s", strings.Join(unitOfWork.targeted, ", "))
if mightStartRelated {
associated := unitOfWork.Associated()
if len(associated) > 0 {
printInfof("\nIf needed, also starts containers: %s", strings.Join(associated, ", "))
}
requiredNetworks := unitOfWork.RequiredNetworks()
if len(requiredNetworks) > 0 {
printInfof("\nIf needed, also creates networks: %s", strings.Join(requiredNetworks, ", "))
}
requiredVolumes := unitOfWork.RequiredVolumes()
if len(requiredVolumes) > 0 {
printInfof("\nIf needed, also creates volumes: %s", strings.Join(requiredVolumes, ", "))
}
}
fmt.Printf("\n\n")
}
wrapped(unitOfWork)
}
func allowedContainers(excludedReference []string, onlyReference string) (containers []string) {
allContainers := []string{}
if len(onlyReference) == 0 {
for name := range cfg.ContainerMap() {
allContainers = append(allContainers, name)
}
} else {
allContainers = cfg.ContainersForReference(onlyReference)
}
excludedContainers := []string{}
for _, reference := range excludedReference {
excludedContainers = append(excludedContainers, cfg.ContainersForReference(reference)...)
}
for _, name := range allContainers {
if !includes(excludedContainers, name) {
containers = append(containers, name)
}
}
return
}
func runCli() {
command := kingpin.MustParse(app.Parse(os.Args[1:]))
err := readSettings()
if err != nil {
printNoticef(err.Error())
} else {
settings.CorrectVersion()
if command != versionCommand.FullCommand() {
if settings.ShouldCheckForUpdates() {
if err := checkForUpdates(false); err != nil {
printNoticef("Could not update settings file")
}
} else if settings.LatestVersion != Version {
printNoticef("Newer version %s is available!\n\n", settings.LatestVersion)
}
}
}
switch command {
case upCommand.FullCommand():
commandAction(*upTargetArg, func(uow *UnitOfWork) {
uow.Up(*upCmdArg, *upDetachFlag, *upNoCacheFlag, *upParallelFlag)
}, true)
case liftCommand.FullCommand():
commandAction(*liftTargetArg, func(uow *UnitOfWork) {
uow.Up(*liftCmdArg, *liftDetachFlag, *liftNoCacheFlag, *liftParallelFlag)
}, true)
case versionCommand.FullCommand():
printVersion()
if !*versionNoCheckFlag && settings.CheckForUpdates {
fmt.Println("")
checkForUpdates(true)
}
case statsCommand.FullCommand():
commandAction(*statsTargetArg, func(uow *UnitOfWork) {
uow.Stats(*statsNoStreamFlag)
}, false)
case statusCommand.FullCommand():
commandAction(*statusTargetArg, func(uow *UnitOfWork) {
uow.Status(*noTruncFlag)
}, false)
case pushCommand.FullCommand():
commandAction(*pushTargetArg, func(uow *UnitOfWork) {
uow.Push()
}, false)
case unpauseCommand.FullCommand():
commandAction(*unpauseTargetArg, func(uow *UnitOfWork) {
uow.Unpause()
}, false)
case pauseCommand.FullCommand():
commandAction(*pauseTargetArg, func(uow *UnitOfWork) {
uow.Pause()
}, false)
case startCommand.FullCommand():
commandAction(*startTargetArg, func(uow *UnitOfWork) {
uow.Start()
}, true)
case stopCommand.FullCommand():
commandAction(*stopTargetArg, func(uow *UnitOfWork) {
uow.Stop()
}, false)
case killCommand.FullCommand():
commandAction(*killTargetArg, func(uow *UnitOfWork) {
uow.Kill()
}, false)
case execCommand.FullCommand():
commandAction(*execTargetArg, func(uow *UnitOfWork) {
uow.Exec(*execCmdArg, *execPrivilegedFlag, *execUserFlag)
}, false)
case rmCommand.FullCommand():
commandAction(*rmTargetArg, func(uow *UnitOfWork) {
uow.Rm(*rmForceFlag, *rmVolumesFlag)
}, false)
case runCommand.FullCommand():
commandAction(*runTargetArg, func(uow *UnitOfWork) {
uow.Run(*runCmdArg, *runDetachFlag)
}, true)
case createCommand.FullCommand():
commandAction(*createTargetArg, func(uow *UnitOfWork) {
uow.Create(*createCmdArg)
}, true)
case provisionCommand.FullCommand():
commandAction(*provisionTargetArg, func(uow *UnitOfWork) {
uow.Provision(*provisionNoCacheFlag, *provisionParallelFlag)
}, false)
case pullCommand.FullCommand():
commandAction(*pullTargetArg, func(uow *UnitOfWork) {
uow.PullImage()
}, false)
case logsCommand.FullCommand():
commandAction(*logsTargetArg, func(uow *UnitOfWork) {
uow.Logs(*followFlag, *timestampsFlag, *tailFlag, *colorizeFlag, *sinceFlag)
}, false)
case generateCommand.FullCommand():
if len(*templateFlag) == 0 {
printErrorf("ERROR: No template specified. The flag `--template` is required.\n")
return
}
commandAction(*generateTargetArg, func(uow *UnitOfWork) {
uow.Generate(*templateFlag, *outputFlag)
}, false)
case amResetCommand.FullCommand():
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
resetTargets := []string{}
container := cfg.Container(*amResetTargetArg)
if container != nil {
for _, bm := range container.BindMounts(cfg.VolumeNames()) {
am := &acceleratedMount{RawVolume: bm, configPath: cfg.Path()}
resetTargets = append(resetTargets, am.Volume())
}
} else {
resetTargets = append(resetTargets, *amResetTargetArg)
}
for _, resetTarget := range resetTargets {
am := cfg.AcceleratedMount(resetTarget)
if accelerationEnabled() && am != nil {
printInfof("Resetting accelerated mount %s ...\n", resetTarget)
am.Reset()
} else {
amNames := cfg.AcceleratedMountNames()
if len(amNames) > 0 {
printErrorf("ERROR: No such accelerated mount. Configured mounts: %s\n", strings.Join(amNames, ", "))
} else {
printErrorf("ERROR: No accelerated mounts configured.\n")
}
}
}
case amLogsCommand.FullCommand():
cfg = NewConfig(*configFlag, *prefixFlag, *tagFlag)
var logsTarget string
configuredAcceleratedMounts := cfg.AcceleratedMountNames()
container := cfg.Container(*amLogsTargetArg)
if container != nil {
allBindMounts := container.BindMounts(cfg.VolumeNames())
acceleratedBindMounts := []string{}
for _, name := range allBindMounts {
if includes(configuredAcceleratedMounts, name) {
acceleratedBindMounts = append(acceleratedBindMounts, name)
}
}
am := &acceleratedMount{RawVolume: acceleratedBindMounts[0], configPath: cfg.Path()}
logsTarget = am.Volume()
if len(acceleratedBindMounts) > 1 {
printNoticef("WARNING: %s has more than one bind-mount configured. The first one will be selected. To select a different bind-mount, pass it directly.\n", *amLogsTargetArg)
}
} else {
logsTarget = *amLogsTargetArg
}
am := cfg.AcceleratedMount(logsTarget)
if accelerationEnabled() && am != nil {
kind := "Showing"
if *amFollowFlag {
kind = "Following"
}
printInfof("%s logs of accelerated mount %s ...\n", kind, *amLogsTargetArg)
am.Logs(*amFollowFlag)
} else {
if len(configuredAcceleratedMounts) > 0 {
printErrorf("ERROR: No such accelerated mount. Configured mounts: %s\n", strings.Join(configuredAcceleratedMounts, ", "))
} else {
printErrorf("ERROR: No accelerated mounts configured.\n")
}
}
}
}