forked from rancher/cli
/
stack.go
206 lines (180 loc) · 4.44 KB
/
stack.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
package cmd
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/rancher/go-rancher/v2"
"github.com/rancher/rancher-compose-executor/lookup"
"github.com/urfave/cli"
)
func StackCommand() cli.Command {
stackLsFlags := []cli.Flag{
listSystemFlag(),
cli.BoolFlag{
Name: "quiet,q",
Usage: "Only display IDs",
},
cli.StringFlag{
Name: "format",
Usage: "'json' or Custom format: '{{.ID}} {{.Stack.Name}}'",
},
}
return cli.Command{
Name: "stacks",
ShortName: "stack",
Usage: "Operations on stacks",
Action: defaultAction(stackLs),
Flags: stackLsFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "ls",
Usage: "List stacks",
Description: "\nLists all stacks in the current $RANCHER_ENVIRONMENT. Use `--env <envID>` or `--env <envName>` to select a different environment.\n\nExample:\n\t$ rancher stacks ls\n\t$ rancher --env 1a5 stacks ls\n",
ArgsUsage: "None",
Action: stackLs,
Flags: stackLsFlags,
},
cli.Command{
Name: "create",
Usage: "Create a stacks",
Description: "\nCreate all stack in the current $RANCHER_ENVIRONMENT. Use `--env <envID>` or `--env <envName>` to select a different environment.\n\nExample:\n\t$ rancher stacks create\n\t$ rancher --env 1a5 stacks ls\n",
ArgsUsage: "None",
Action: stackCreate,
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "start",
Usage: "Start stack on create",
},
cli.BoolFlag{
Name: "system,s",
Usage: "Create a system stack",
},
cli.BoolFlag{
Name: "empty,e",
Usage: "Create an empty stack",
},
cli.BoolFlag{
Name: "quiet,q",
Usage: "Only display IDs",
},
cli.StringFlag{
Name: "docker-compose,f",
Usage: "Docker Compose file",
Value: "docker-compose.yml",
},
cli.StringFlag{
Name: "rancher-compose,r",
Usage: "Rancher Compose file",
Value: "rancher-compose.yml",
},
//cli.StringFlag{
// Name: "answers,a",
// Usage: "Answers files",
// Value: "answers",
//},
},
},
},
}
}
type StackData struct {
ID string
Catalog string
Stack client.Stack
State string
ServiceCount int
}
func stackLs(ctx *cli.Context) error {
c, err := GetClient(ctx)
if err != nil {
return err
}
collection, err := c.Stack.List(defaultListOpts(ctx))
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"ID", "ID"},
{"NAME", "Stack.Name"},
{"STATE", "State"},
{"CATALOG", "Catalog"},
{"SERVICES", "ServiceCount"},
{"SYSTEM", "Stack.System"},
{"DETAIL", "Stack.TransitioningMessage"},
}, ctx)
defer writer.Close()
for _, item := range collection.Data {
combined := item.HealthState
if item.State != "active" || combined == "" {
combined = item.State
}
writer.Write(&StackData{
ID: item.Id,
Stack: item,
State: combined,
Catalog: item.ExternalId,
ServiceCount: len(item.ServiceIds),
})
}
return writer.Err()
}
func getFile(name string) (string, error) {
if name == "" {
return "", nil
}
bytes, err := ioutil.ReadFile(name)
if os.IsNotExist(err) {
return "", nil
}
return string(bytes), err
}
func parseAnswers(ctx *cli.Context) (map[string]interface{}, error) {
file := ctx.String("answers")
return lookup.ParseEnvFile(file)
}
func stackCreate(ctx *cli.Context) error {
c, err := GetClient(ctx)
names := []string{RandomName()}
if len(ctx.Args()) > 0 {
names = ctx.Args()
}
w, err := NewWaiter(ctx)
if err != nil {
return err
}
var lastErr error
for _, name := range names {
stack := &client.Stack{
Name: name,
System: ctx.Bool("system"),
StartOnCreate: ctx.Bool("start"),
}
if !ctx.Bool("empty") {
var err error
stack.DockerCompose, err = getFile(ctx.String("docker-compose"))
if err != nil {
return err
}
if stack.DockerCompose == "" {
return errors.New("docker-compose.yml files is required")
}
stack.RancherCompose, err = getFile(ctx.String("rancher-compose"))
if err != nil {
return errors.Wrap(err, "reading "+ctx.String("rancher-compose"))
}
//stack.Answers, err = parseAnswers(ctx)
//if err != nil {
//return errors.Wrap(err, "reading answers")
//}
}
stack, err = c.Stack.Create(stack)
if err != nil {
lastErr = err
}
w.Add(stack.Id)
}
if lastErr != nil {
return lastErr
}
return w.Wait()
}