forked from mantika/whaleprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp.go
186 lines (165 loc) · 4.25 KB
/
wp.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/docker/docker/api/client/bundlefile"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Usage = "Manage DAB files as docker swarm service blueprints"
app.Commands = []cli.Command{
{
Name: "plan",
Usage: "Plan DAB whaleprint",
ArgsUsage: `[STACK] [STACK...]
Prints an execultion plan to review before applying changes.
Whaleprint will look for .dab files or use the stack name to load the DAB file.
`,
Action: plan,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "DAB file to use",
},
cli.BoolFlag{
Name: "detail",
Usage: "Show all properties instead of changes only",
},
cli.StringSliceFlag{
Name: "target",
Usage: "Process specified services only (default [])",
},
},
},
{
Name: "apply",
Usage: "Apply DAB whaleprint",
ArgsUsage: `[STACK] [STACK...]
Applies the execution plan returned by the "whaleprint plan" command
Whaleprint will look for .dab files or use the stack name to load the DAB file.
`,
Action: apply,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "DAB file to use",
},
cli.StringSliceFlag{
Name: "target",
Usage: "Process specified services only (default [])",
},
},
},
{
Name: "destroy",
Usage: "Destroy a DAB stack",
ArgsUsage: `[STACK] [STACK...]
Destroys the stack present in the DAB file
Whaleprint will look for .dab files use the stack name to load the DAB file.
`,
Action: destroy,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "DAB file to use",
},
cli.BoolFlag{
Name: "force",
Usage: "Ignore destroy DAB file to useconfirmation",
},
cli.StringSliceFlag{
Name: "target",
Usage: "Process specified services only (default [])",
},
},
},
{
Name: "output",
Usage: "Show import output information stacks",
ArgsUsage: `[STACK] [STACK...]
Show important information for the specified stacks.
`,
Action: output,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "DAB file to use",
},
},
},
}
app.Run(os.Args)
}
func getStacksFromCWD() []string {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal("Error fetching files from current dir", err)
}
dabs := []string{}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".dab") {
dabs = append(dabs, strings.TrimSuffix(file.Name(), ".dab"))
}
}
if len(dabs) == 0 {
log.Fatal("No DABs found in current directory")
}
return dabs
}
func getStacks(c *cli.Context) ([]Stack, error) {
type stackDefinition struct {
name string
file string
}
defs := []stackDefinition{}
stackNames := c.Args()
dabFile := c.String("file")
if dabFile != "" {
if len(stackNames) > 1 {
return nil, cli.NewExitError("You can only specify one stack name when using -f", 1)
} else if len(stackNames) == 1 {
defs = append(defs, stackDefinition{name: stackNames[0], file: dabFile})
} else {
stackName := strings.TrimSuffix(filepath.Base(dabFile), filepath.Ext(dabFile))
defs = append(defs, stackDefinition{name: stackName, file: dabFile})
}
} else if len(stackNames) == 0 {
stackNames = getStacksFromCWD()
for _, name := range stackNames {
dabFile := fmt.Sprintf("%s.dab", name)
defs = append(defs, stackDefinition{name: name, file: dabFile})
}
} else if len(stackNames) > 0 {
for _, name := range stackNames {
dabFile := fmt.Sprintf("%s.dab", name)
defs = append(defs, stackDefinition{name: name, file: dabFile})
}
}
stacks := make([]Stack, len(defs))
for i, def := range defs {
var dabReader io.Reader
if u, e := url.Parse(def.file); e == nil && u.IsAbs() {
// DAB file seems to be remote, try to download it first
return nil, cli.NewExitError("Not implemented", 2)
} else {
if file, err := os.Open(def.file); err != nil {
return nil, cli.NewExitError(err.Error(), 3)
} else {
dabReader = file
}
}
bundle, bundleErr := bundlefile.LoadFile(dabReader)
if bundleErr != nil {
return nil, cli.NewExitError(bundleErr.Error(), 3)
}
stacks[i] = Stack{Name: def.name, Bundle: bundle}
}
return stacks, nil
}