forked from rancher/cli
/
env.go
367 lines (318 loc) · 7.77 KB
/
env.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/rancher/go-rancher/v2"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
func EnvCommand() cli.Command {
envLsFlags := []cli.Flag{
listAllFlag(),
cli.BoolFlag{
Name: "quiet,q",
Usage: "Only display IDs",
},
cli.StringFlag{
Name: "format",
Usage: "'json' or Custom format: {{.Id}} {{.Name}}",
},
}
return cli.Command{
Name: "environment",
ShortName: "env",
Usage: "Interact with environments",
Action: defaultAction(envLs),
Flags: envLsFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "ls",
Usage: "List environments",
Description: "\nWith an account API key, all environments in Rancher will be listed. If you are using an environment API key, it will only list the environment of the API key. \n\nExample:\n\t$ rancher env ls\n",
ArgsUsage: "None",
Action: envLs,
Flags: envLsFlags,
},
cli.Command{
Name: "create",
Usage: "Create an environment",
Description: `
By default, an environment with cattle orchestration framework will be created. This command only works with Account API keys.
Example:
$ rancher env create newEnv
To add an orchestration framework do TODO
$ rancher env create -t kubernetes newK8sEnv
$ rancher env create -t mesos newMesosEnv
$ rancher env create -t swarm newSwarmEnv
`,
ArgsUsage: "[NEWENVNAME...]",
Action: envCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "template,t",
Usage: "Environment template to create from",
Value: "Cattle",
},
},
},
cli.Command{
Name: "templates",
ShortName: "template",
Usage: "Interact with environment templates",
Action: defaultAction(envTemplateLs),
Flags: envLsFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "export",
Usage: "Export an environment template to STDOUT",
ArgsUsage: "[TEMPLATEID TEMPLATENAME...]",
Action: envTemplateExport,
Flags: []cli.Flag{},
},
cli.Command{
Name: "import",
Usage: "Import an environment template to from file",
ArgsUsage: "[FILE FILE...]",
Action: envTemplateImport,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "public",
Usage: "Make template public",
},
},
},
},
},
cli.Command{
Name: "rm",
Usage: "Remove environment(s)",
Description: "\nExample:\n\t$ rancher env rm 1a5\n\t$ rancher env rm newEnv\n",
ArgsUsage: "[ENVID ENVNAME...]",
Action: envRm,
Flags: []cli.Flag{},
},
cli.Command{
Name: "deactivate",
Usage: "Deactivate environment(s)",
Description: `
Deactivate an environment by ID or name
Example:
$ rancher env deactivate 1a5
$ rancher env deactivate Default
`,
ArgsUsage: "[ID NAME...]",
Action: envDeactivate,
Flags: []cli.Flag{},
},
cli.Command{
Name: "activate",
Usage: "Activate environment(s)",
Description: `
Activate an environment by ID or name
Example:
$ rancher env activate 1a5
$ rancher env activate Default
`,
ArgsUsage: "[ID NAME...]",
Action: envActivate,
Flags: []cli.Flag{},
},
},
}
}
type EnvData struct {
ID string
Environment *client.Project
}
type TemplateData struct {
ID string
ProjectTemplate *client.ProjectTemplate
}
func NewEnvData(project client.Project) *EnvData {
return &EnvData{
ID: project.Id,
Environment: &project,
}
}
func envRm(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
return forEachResourceWithClient(c, ctx, []string{"project"}, func(c *client.RancherClient, resource *client.Resource) (string, error) {
return resource.Id, c.Delete(resource)
})
}
func envCreate(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
name := RandomName()
if ctx.NArg() > 0 {
name = ctx.Args()[0]
}
data := map[string]interface{}{
"name": name,
}
template := ctx.String("template")
if template != "" {
template, err := Lookup(c, template, "projectTemplate")
if err != nil {
return err
}
data["projectTemplateId"] = template.Id
}
var newEnv client.Project
if err := c.Create("project", data, &newEnv); err != nil {
return err
}
fmt.Println(newEnv.Id)
return nil
}
func envLs(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"ID", "ID"},
{"NAME", "Environment.Name"},
{"ORCHESTRATION", "Environment.Orchestration"},
{"STATE", "Environment.State"},
{"CREATED", "Environment.Created"},
}, ctx)
defer writer.Close()
collection, err := c.Project.List(defaultListOpts(ctx))
if err != nil {
return err
}
for _, item := range collection.Data {
writer.Write(NewEnvData(item))
}
return writer.Err()
}
func envDeactivate(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
return forEachResourceWithClient(c, ctx, []string{"project"}, func(c *client.RancherClient, resource *client.Resource) (string, error) {
action, err := pickAction(resource, "deactivate")
if err != nil {
return "", err
}
return resource.Id, c.Action(resource.Type, action, resource, nil, resource)
})
}
func envActivate(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
return forEachResourceWithClient(c, ctx, []string{"project"}, func(c *client.RancherClient, resource *client.Resource) (string, error) {
action, err := pickAction(resource, "activate")
if err != nil {
return "", err
}
return resource.Id, c.Action(resource.Type, action, resource, nil, resource)
})
}
func envTemplateLs(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"ID", "ID"},
{"NAME", "ProjectTemplate.Name"},
{"DESC", "ProjectTemplate.Description"},
}, ctx)
defer writer.Close()
collection, err := c.ProjectTemplate.List(defaultListOpts(ctx))
if err != nil {
return err
}
for _, item := range collection.Data {
writer.Write(TemplateData{
ID: item.Id,
ProjectTemplate: &item,
})
}
return writer.Err()
}
func envTemplateImport(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
w, err := NewWaiter(ctx)
if err != nil {
return err
}
for _, file := range ctx.Args() {
template := client.ProjectTemplate{
IsPublic: ctx.Bool("public"),
}
content, err := ioutil.ReadFile(file)
if err != nil {
return err
}
if err := yaml.Unmarshal(content, &template); err != nil {
return err
}
created, err := c.ProjectTemplate.Create(&template)
if err != nil {
return err
}
w.Add(created.Id)
}
return w.Wait()
}
func envTemplateExport(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}
for _, name := range ctx.Args() {
r, err := Lookup(c, name, "projectTemplate")
if err != nil {
return err
}
template, err := c.ProjectTemplate.ById(r.Id)
if err != nil {
return err
}
stacks := []map[string]interface{}{}
for _, s := range template.Stacks {
data := map[string]interface{}{
"name": s.Name,
}
if s.TemplateId != "" {
data["template_id"] = s.TemplateId
}
if s.TemplateVersionId != "" {
data["template_version_id"] = s.TemplateVersionId
}
if len(s.Answers) > 0 {
data["answers"] = s.Answers
}
stacks = append(stacks, data)
}
data := map[string]interface{}{
"name": template.Name,
"description": template.Description,
"stacks": stacks,
}
content, err := yaml.Marshal(&data)
if err != nil {
return err
}
_, err = os.Stdout.Write(content)
if err != nil {
return err
}
}
return nil
}