/
cmd.go
251 lines (220 loc) · 8.91 KB
/
cmd.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
package crane
import (
"fmt"
"github.com/michaelsauter/crane/print"
"github.com/spf13/cobra"
"os"
"strings"
)
type Options struct {
verbose bool
recreate bool
nocache bool
notrunc bool
forceRm bool
cascadeDependencies string
cascadeAffected string
config string
target []string
}
var options = Options{
verbose: false,
recreate: false,
nocache: false,
notrunc: false,
forceRm: false,
cascadeDependencies: "",
cascadeAffected: "",
config: "",
}
func isVerbose() bool {
return options.verbose
}
// returns a function to be set as a cobra command run, wrapping a command meant to be run according to the config
func configCommand(wrapped func(config Config), forceOrder bool) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
for _, value := range []string{options.cascadeDependencies, options.cascadeAffected} {
if value != "none" && value != "all" && value != "link" && value != "volumesFrom" && value != "net" {
cmd.Printf("Error: invalid cascading value: %v", value)
cmd.Usage()
panic(StatusError{status: 64})
}
}
// Set target from args
options.target = args
config := NewConfig(options, forceOrder)
if containers := config.TargetedContainers(); len(containers) == 0 {
print.Errorf("ERROR: Command cannot be applied to any container.")
} else {
if isVerbose() {
print.Infof("Command will be applied to: %v\n\n", strings.Join(containers.names(), ", "))
}
wrapped(config)
}
}
}
func handleCmd() {
var cmdLift = &cobra.Command{
Use: "lift",
Short: "Build or pull images if they don't exist, then run or start the containers",
Long: `
lift will provision missing images and run all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().lift(options.recreate, options.nocache)
}, false),
}
var cmdProvision = &cobra.Command{
Use: "provision",
Short: "Build or pull images",
Long: `
provision will use specified Dockerfiles to build all targeted images.
If no Dockerfile is given, it will pull the image(s) from the given registry.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().provision(options.nocache)
}, true),
}
var cmdRun = &cobra.Command{
Use: "run",
Short: "Run the containers",
Long: `run will call docker run for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().run(options.recreate)
}, false),
}
var cmdRm = &cobra.Command{
Use: "rm",
Short: "Remove the containers",
Long: `rm will call docker rm for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().reversed().rm(options.forceRm)
}, true),
}
var cmdKill = &cobra.Command{
Use: "kill",
Short: "Kill the containers",
Long: `kill will call docker kill for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().reversed().kill()
}, true),
}
var cmdStart = &cobra.Command{
Use: "start",
Short: "Start the containers",
Long: `start will call docker start for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().start()
}, false),
}
var cmdStop = &cobra.Command{
Use: "stop",
Short: "Stop the containers",
Long: `stop will call docker stop for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().reversed().stop()
}, true),
}
var cmdPause = &cobra.Command{
Use: "pause",
Short: "Pause the containers",
Long: `pause will call docker pause for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().reversed().pause()
}, true),
}
var cmdUnpause = &cobra.Command{
Use: "unpause",
Short: "Unpause the containers",
Long: `unpause will call docker unpause for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().unpause()
}, false),
}
var cmdPush = &cobra.Command{
Use: "push",
Short: "Push the containers",
Long: `push will call docker push for all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().push()
}, true),
}
var cmdStatus = &cobra.Command{
Use: "status",
Short: "Displays status of containers",
Long: `Displays the current status of all targeted containers.`,
Run: configCommand(func(config Config) {
config.TargetedContainers().status(options.notrunc)
}, true),
}
var cmdGraph = &cobra.Command{
Use: "graph",
Short: "Dumps the dependency graph as a DOT file",
Long: `Generate a DOT file representing the dependency graph. Bold nodes represent the
containers declared in the config (as opposed to non-bold ones that are referenced
in the config, but not defined). Targeted containers are highlighted with color
borders. Solid edges represent links, dashed edges volumesFrom, and dotted edges
net=container relations.`,
Run: configCommand(func(config Config) {
config.DependencyGraph().DOT(os.Stdout, config.TargetedContainers())
}, true),
}
var cmdVersion = &cobra.Command{
Use: "version",
Short: "Display version",
Long: `Displays the version of Crane.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v0.11.0")
},
}
var craneCmd = &cobra.Command{
Use: "crane",
Short: "crane - Lift containers with ease",
Long: `
Crane is a little tool to orchestrate Docker containers.
It works by reading in JSON or YAML which describes how to obtain container images and how to run them.
See the corresponding docker commands for more information.`,
}
craneCmd.PersistentFlags().BoolVarP(&options.verbose, "verbose", "v", false, "Verbose output")
craneCmd.PersistentFlags().StringVarP(&options.config, "config", "c", "", "Config file to read from")
cascadingValuesSuffix := `
"all": follow any kind of dependency
"link": follow --link dependencies only
"volumesFrom": follow --volumesFrom dependencies only
"net": follow --net dependencies only
`
craneCmd.PersistentFlags().StringVarP(&options.cascadeDependencies, "cascade-dependencies", "d", "none", "Also apply the command for the containers that (any of) the explicitly targeted one(s) depend on"+cascadingValuesSuffix)
craneCmd.PersistentFlags().StringVarP(&options.cascadeAffected, "cascade-affected", "a", "none", "Also apply the command for the existing containers depending on (any of) the explicitly targeted one(s)"+cascadingValuesSuffix)
cmdLift.Flags().BoolVarP(&options.recreate, "recreate", "r", false, "Recreate containers (force-remove containers if they exist, force-provision images, run containers)")
cmdLift.Flags().BoolVarP(&options.nocache, "no-cache", "n", false, "Build the image without any cache")
cmdProvision.Flags().BoolVarP(&options.nocache, "no-cache", "n", false, "Build the image without any cache")
cmdRun.Flags().BoolVarP(&options.recreate, "recreate", "r", false, "Recreate containers (force-remove containers first)")
cmdRm.Flags().BoolVarP(&options.forceRm, "force", "f", false, "Kill containers if they are running first")
cmdStatus.Flags().BoolVarP(&options.notrunc, "no-trunc", "", false, "Don't truncate output")
// default usage template with target arguments & description
craneCmd.SetUsageTemplate(`{{ $cmd := . }}
Usage: {{if .Runnable}}
{{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}
{{ .CommandPath}} [command]{{end}} [target1 [target2 [...]]]
{{ if .HasSubCommands}}
Available Commands: {{range .Commands}}{{if .Runnable}}
{{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}
{{end}}
Explicit targeting:
By default, the command is applied to all containers declared in the
config, or to the containers defined in the group ` + "`" + `default` + "`" + ` if it is
defined. If one or several container or group reference(s) is/are
passed as argument(s), the command will only be applied to containers
matching these references. Note however that providing cascading flags
might extend the set of targeted containers.
{{ if .HasFlags}}Available Flags:
{{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
{{end}}
Use "{{.Root.Name}} help [command]" for more information about that command.
`)
craneCmd.AddCommand(cmdLift, cmdProvision, cmdRun, cmdRm, cmdKill, cmdStart, cmdStop, cmdPause, cmdUnpause, cmdPush, cmdStatus, cmdGraph, cmdVersion)
err := craneCmd.Execute()
if err != nil {
panic(StatusError{status: 64})
}
}