/
main.go
348 lines (303 loc) · 11.5 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
348
/*
*Copyright ClusterHQ Inc. See LICENSE file for details.
*
*/
package main
import (
"os"
"flag"
"strings"
"path/filepath"
"io/ioutil"
"github.com/ClusterHQ/fli-docker/utils"
"github.com/ClusterHQ/fli-docker/cli"
"github.com/ClusterHQ/fli-docker/logger"
)
func main() {
// should this be a struct?
var tokenfile string
var flockerhub string
var manifest string
var compose bool
var verbose bool
var project string
var push bool
var clean bool
// FlagSets for SubCommands
runSet := flag.NewFlagSet("fli-docker run", flag.ExitOnError)
snapSet := flag.NewFlagSet("fli-docker snapshot", flag.ExitOnError)
stopSet := flag.NewFlagSet("fli-docker stop", flag.ExitOnError)
destroySet := flag.NewFlagSet("fli-docker destroy", flag.ExitOnError)
// runSet
runSet.StringVar(&tokenfile, "t", "", "[OPTIONAL] Flocker Hub user token, optionally set it in the manifest YAML")
runSet.StringVar(&flockerhub, "e", "", "[OPTIONAL] Flocker Hub endpoint, optionally set it in the manifest YAML")
runSet.StringVar(&manifest, "f", "manifest.yml", "[OPTIONAL] Stateful application manifest file")
runSet.BoolVar(&compose, "c", false, "[OPTIONAL] if flag is present, fli-docker will start the compose services")
runSet.BoolVar(&verbose, "verbose", false, "[OPTIONAL] verbose logging")
runSet.StringVar(&project, "p", "fli-compose", "[OPTIONAL] project name for compose if using -c")
// snapSet
snapSet.StringVar(&tokenfile, "t", "", "[OPTIONAL] Flocker Hub user token, optionally set it in the manifest YAML")
snapSet.StringVar(&flockerhub, "e", "", "[OPTIONAL] Flocker Hub endpoint, optionally set it in the manifest YAML")
snapSet.BoolVar(&push, "push", false, "[OPTIONAL] if flag is present, fli-docker will push new snapshots back to FlockerHub")
snapSet.BoolVar(&verbose, "verbose", false, "[OPTIONAL] verbose logging")
// stopSet
stopSet.BoolVar(&verbose, "verbose", false, "[OPTIONAL] verbose logging")
stopSet.StringVar(&manifest, "f", "manifest.yml", "[OPTIONAL] Stateful application manifest file")
stopSet.StringVar(&project, "p", "fli-compose", "[OPTIONAL] project name for compose if using -c")
// destroySet
destroySet.BoolVar(&verbose, "verbose", false, "[OPTIONAL] verbose logging")
destroySet.StringVar(&manifest, "f", "manifest.yml", "[OPTIONAL] Stateful application manifest file")
destroySet.StringVar(&project, "p", "fli-compose", "[OPTIONAL] project name for compose if using -c")
destroySet.BoolVar(&clean, "clean", false, "[OPTIONAL] places docker-compose file back to original state")
// Initialize logger before `verbose` is captured for
// log messages before that conditional
logger.Init(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
if (len(os.Args) > 1) {
if (strings.Contains(os.Args[1], "help")) {os.Args[1] = "help"}
switch os.Args[1] {
case "version":
logger.Message.Println(utils.FliDockerVersion)
os.Exit(0)
case "run":
runSet.Parse(os.Args[2:])
if verbose {
logger.Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
}else{
logger.Init(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
}
case "snapshot":
snapSet.Parse(os.Args[2:])
if verbose {
logger.Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
}else{
logger.Init(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
}
case "destroy":
destroySet.Parse(os.Args[2:])
if verbose {
logger.Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
}else{
logger.Init(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
}
case "stop":
stopSet.Parse(os.Args[2:])
if verbose {
logger.Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)
}else{
logger.Init(os.Stdout, ioutil.Discard, ioutil.Discard, os.Stderr)
}
case "help":
snapSet.Parse(os.Args[2:])
logger.Message.Println(utils.FliDockerHelp)
os.Exit(0)
default:
logger.Message.Println("Unrecognized Command. Use fli-docker --help.")
os.Exit(0)
}
} else {
logger.Message.Println("Unrecognized Command. Use fli-docker --help.")
os.Exit(0)
}
var dockerCmd string
dockerCmd = "docker version"
var fliCmd1 string
var fliCmd2 string
fliCmd1 = "fli version"
fliCmd2 = utils.FliDockerCmd + "version"
// check if needed dependencies are available
isDockerAvail, err := utils.CheckForCmd(dockerCmd)
if (!isDockerAvail){
logger.Info.Println(err)
logger.Info.Println(utils.DockerHelpMessage)
logger.Error.Fatal("Could not find docker, please install docker. ")
}else{
logger.Info.Println("Docker Ready!")
}
isFliAvail1, err := utils.CheckForCmd(fliCmd1)
isFliAvail2, err := utils.CheckForCmd(fliCmd2)
var binary bool
var docker bool
var fliCmd string
binary = true
docker = false
if (!isFliAvail1 && !isFliAvail2){
logger.Info.Println(utils.FliHelpMessage)
logger.Error.Fatal("Fli not detected, please install / configure Fli.")
}else{
if (!isFliAvail1) {
binary = false
docker = true
fliCmd = utils.FliDockerCmd
}else{
fliCmd = utils.FliBinaryCmd
}
logger.Info.Println("using fli container: ", docker)
logger.Info.Println("using fli binary: ", binary)
}
if os.Args[1] == "run" {
logger.Info.Println("Running: `fli-docker run`")
if manifest == "manifest.yml" {
logger.Warning.Println("Using default 'manifest.yml`, otherwise specify differently with -f")
}
// verify that the manifest exists
isManifestAvail, err := utils.CheckForFile(manifest)
if (!isManifestAvail){
logger.Info.Println(err.Error())
logger.Message.Fatal("Missing manifest, either name it 'manifest.yml' or pass in file with '-f'.")
}
// get the yaml file passed in the args.
filename, _ := filepath.Abs(manifest)
// read the file.
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
logger.Error.Fatal(err.Error())
}
// pass the file to the ParseManifest
logger.Message.Println("Parsing the fli manifest...")
m := utils.ParseManifest(yamlFile)
if tokenfile == "" {
logger.Info.Println("token not specified with -t")
tf, err := cli.GetFlockerHubTokenFile(fliCmd)
if err != nil{
logger.Message.Fatal("Could not get tokenfile config")
}
logger.Info.Println("Existing tokenfile config: ", tf)
// Was is placed in the manifest?
logger.Info.Println("tokenfile " + m.Hub.AuthToken + " in manifest")
tokenfileFromManifest := m.Hub.AuthToken
if tokenfileFromManifest == "" {
if strings.Contains(tf, "Authentication Token File: -") {
logger.Message.Fatal("Must set tokenfile")
}else{
logger.Info.Println("Trying existing tokenfile config: ", tf)
}
}else{
cli.SetFlockerHubTokenFile(tokenfileFromManifest, fliCmd)
}
}else{
cli.SetFlockerHubTokenFile(tokenfile, fliCmd)
}
// was it passed with `-e`?
if flockerhub == "" {
logger.Info.Println("FlockerHub endpoint not specified with -e")
fh, err := cli.GetFlockerHubEndpoint(fliCmd)
if err != nil{
logger.Error.Fatal("Could not get FlockerHub config")
}
logger.Info.Println("Existing FlockerHub Endpoint config: ", fh)
// was it placed in manifest?
flockerhubFromManifest := m.Hub.Endpoint
logger.Info.Println("FlockerHub Endpoint " + m.Hub.Endpoint + " in manifest")
if flockerhubFromManifest == "" {
// Did the user have a pre-existing fli setup?
// Lets try and assume the volumes are there.
if strings.Contains(fh, "FlockerHub URL: -") {
logger.Message.Fatal("Must set FlockerHub Endpoint")
}else{
logger.Info.Println("Trying existing FlockerHub configuration: ", fh)
}
}else{
// set endpoint from manifest
cli.SetFlockerHubEndpoint(flockerhubFromManifest, fliCmd)
}
}else{
// set endpoint from fli-docker arg
cli.SetFlockerHubEndpoint(flockerhub, fliCmd)
}
// verify that the compose file exists.
isComposeFileAvail, err := utils.CheckForFile(m.DockerApp)
if (!isComposeFileAvail){
logger.Info.Println(err.Error())
logger.Message.Fatal("Docker Compose file doesnt exist.")
}
// try and pull snapshots
logger.Message.Println("Pulling FlockerHub volumes...")
cli.PullSnapshots(m.Volumes, fliCmd)
// create volumes from snapshots and map them to
// `newVolPaths = {compose_volume_name : "/chq/<vol_path>"...}`
newVolPaths, err := cli.CreateVolumesFromSnapshots(m.Volumes, fliCmd)
// create a copy of the compose file before we edit it.
// replace a fresh copy if we already copied before
utils.CheckForCopy(m.DockerApp)
// it will be `filename` + `-fli.copy`
// will only copy if copy doesnt exist already
utils.MakeCopy(m.DockerApp)
// replace volume_name with `volume_name`'s associated
// "/chq/<vol_path/" and modify the compose file
logger.Message.Println("Mapping new volumes in compose file...")
for _, newVol := range newVolPaths {
utils.MapVolumeToCompose(newVol.Name, newVol.VolumePath, m.DockerApp)
}
// this just parses the compose file, not needed,
// but in verbose we can be thorough as it also prints it.
utils.ParseCompose(m.DockerApp)
// `-c` means "run compose".
// if not, done, we only modified the compose file.
if compose {
logger.Info.Println("compose option set, running docker-compose")
utils.RunCompose(m.DockerApp, project)
}
} else if os.Args[1] == "snapshot" {
logger.Info.Println("Running: `fli-docker snapshot`")
// Does user want us to push snapshots back?
if push {
logger.Message.Println("Snapshotting and Pushing volumes to FlockerHub...")
cli.SnapshotAndPushWorkingVolumes(fliCmd)
}else{
logger.Message.Println("Snapshotting volumes...")
cli.SnapshotWorkingVolumes(fliCmd)
}
} else if os.Args[1] == "destroy" {
logger.Info.Println("Running: `fli-docker destroy`")
if manifest == "manifest.yml" {
logger.Warning.Println("Using default 'manifest.yml`, otherwise specify differently with -f")
}
// verify that the manifest exists
isManifestAvail, err := utils.CheckForFile(manifest)
if (!isManifestAvail){
logger.Info.Println(err.Error())
logger.Message.Fatal("Missing manifest, either name it 'manifest.yml' or pass in file with '-f'.")
}
// get the yaml file passed in the args.
filename, _ := filepath.Abs(manifest)
// read the file.
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
logger.Info.Println(err.Error())
logger.Message.Fatal("Docker Compose file doesnt exist.")
}
// pass the file to the ParseManifest
logger.Message.Println("Parsing the fli manifest...")
m := utils.ParseManifest(yamlFile)
logger.Info.Println("Destroying compose application")
utils.DestroyCompose(m.DockerApp, project)
if clean {
logger.Message.Println("Cleaning files...")
utils.CleanEnv(m.DockerApp)
}
} else if os.Args[1] == "stop" {
logger.Info.Println("Running: `fli-docker stop`")
if manifest == "manifest.yml" {
logger.Warning.Println("Using default 'manifest.yml`, otherwise specify differently with -f")
}
// verify that the manifest exists
isManifestAvail, err := utils.CheckForFile(manifest)
if (!isManifestAvail){
logger.Info.Println(err.Error())
logger.Message.Fatal("Missing manifest, either name it 'manifest.yml' or pass in file with '-f'.")
}
// get the yaml file passed in the args.
filename, _ := filepath.Abs(manifest)
// read the file.
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
logger.Info.Println(err.Error())
logger.Message.Fatal("Docker Compose file doesnt exist.")
}
// pass the file to the ParseManifest
logger.Message.Println("Parsing the fli manifest...")
m := utils.ParseManifest(yamlFile)
logger.Info.Println("Stopping compose application")
utils.StopCompose(m.DockerApp, project)
}
}