/
cluster.go
409 lines (362 loc) · 9.54 KB
/
cluster.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2018-2019, CS Systemes d'Information, http://www.c-s.fr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package commands
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// ClusterListCommand handles 'perform list'
var ClusterListCommand = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Category: "Cluster",
Action: func(c *cli.Context) error {
cmdStr := RebrandCommand(fmt.Sprintf("safescale cluster list"))
log.Debugf("Calling '%s'", cmdStr)
return runCommand(cmdStr)
},
}
// ClusterInspectCommand handles 'perform <clustername inspect'
var ClusterInspectCommand = cli.Command{
Name: "inspect",
Aliases: []string{"show", "get"},
Category: "Cluster",
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
cmdStr := RebrandCommand(fmt.Sprintf("safescale cluster inspect %s", clusterName))
return runCommand(cmdStr)
},
}
// ClusterCreateCommand handles 'perform <clustername> create"
var ClusterCreateCommand = cli.Command{
Name: "create",
Aliases: []string{"new"},
Usage: "Creates a cluster",
ArgsUsage: "CLUSTERNAME",
Category: "Cluster",
// Help: &cli.HelpContent{
// Usage: `
// Usage: deploy [options] cluster <clustername> state`,
// Description: `
// Gets the state of the cluster <clustername>.`,
// },
Flags: []cli.Flag{
cli.StringFlag{
Name: "complexity, C",
Usage: "Defines the sizing of the cluster: Small, Normal, Large (default: Normal)",
},
cli.StringFlag{
Name: "flavor, F",
Usage: "Defines the type of the cluster; can be BOH, SWARM, OHPC, DCOS, K8S (default: K8S)",
},
cli.BoolFlag{
Name: "keep-on-failure, k",
Usage: "If used, the resources are not deleted on failure (default: not set)",
},
cli.StringFlag{
Name: "cidr, N",
Usage: "Defines the CIDR of the network to use with cluster (default: 192.168.0.0/16)",
},
cli.StringSliceFlag{
Name: "disable",
Usage: "Allows to disable addition of default features",
},
cli.StringFlag{
Name: "os",
Usage: "Defines the operating system to use",
},
cli.UintFlag{
Name: "cpu",
Usage: "Defines the number of cpu of masters and nodes in the cluster",
},
cli.Float64Flag{
Name: "ram",
Usage: "Defines the size of RAM of masters and nodes in the cluster (in GB)",
},
cli.UintFlag{
Name: "disk",
Usage: "Defines the size of system disk of masters and nodes (in GB)",
},
},
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
complexityStr := c.String("complexity")
if complexityStr == "" {
complexityStr = "Normal"
}
cidr := c.String("cidr")
keep := c.Bool("keep-on-failure")
cpu := c.Uint("cpu")
ram := c.Float64("ram")
disk := c.Uint("disk")
// Create cluster with deploy
cmdStr := fmt.Sprintf("safescale cluster create %s -F DCOS -C %s -N %s --cpu %d --ram %f --disk %d",
clusterName, complexityStr, cidr, cpu, ram, disk)
if keep {
cmdStr += " -k"
}
cmdStr = RebrandCommand(cmdStr)
err = runCommand(cmdStr)
if err != nil {
return err
}
// Installs feature Spark
cmdStr = fmt.Sprintf("safescale cluster add-feature %s sparkmaster", clusterName)
err = runCommand(cmdStr)
if err != nil {
return err
}
// Done
return nil
},
}
// ClusterDeleteCommand handles 'perform <clustername> delete'
var ClusterDeleteCommand = cli.Command{
Name: "delete",
Aliases: []string{"destroy", "remove", "rm"},
ArgsUsage: "CLUSTERNAME",
Category: "Cluster",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "assume-yes, yes, y",
},
cli.BoolFlag{
Name: "force, f",
},
},
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
yes := c.Bool("assume-yes")
cmdStr := fmt.Sprintf("safescale cluster rm %s", clusterName)
if yes {
cmdStr += " -y"
}
cmdStr = RebrandCommand(cmdStr)
return runCommand(cmdStr)
},
}
// ClusterStopCommand handles 'perform <clustername> stop'
var ClusterStopCommand = cli.Command{
Name: "stop",
Aliases: []string{"freeze"},
Category: "Cluster",
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
cmdStr := RebrandCommand(fmt.Sprintf("safescale cluster stop %s", clusterName))
return runCommand(cmdStr)
},
}
// ClusterStartCommand handles 'perform <clustername> start'
var ClusterStartCommand = cli.Command{
Name: "start",
Aliases: []string{"unfreeze", "boot"},
Category: "Cluster",
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
cmdStr := RebrandCommand(fmt.Sprintf("safescale cluster start %s", clusterName))
return runCommand(cmdStr)
},
}
// ClusterStateCommand handles 'perform state CLUSTERNAME'
var ClusterStateCommand = cli.Command{
Name: "state",
Category: "Cluster",
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
cmdStr := RebrandCommand(fmt.Sprintf("safescale cluster state %s", clusterName))
return runCommand(cmdStr)
},
}
// ClusterExpandCommand handles 'perform <clustername> expand'
var ClusterExpandCommand = cli.Command{
Name: "expand",
Category: "Cluster",
Flags: []cli.Flag{
cli.UintFlag{
Name: "count, n",
Usage: "Define the number of nodes wanted (default: 1)",
Value: 1,
},
cli.BoolFlag{
Name: "public, p",
Usage: "If used, the node(s) will have public IP address (default: no)",
},
cli.StringFlag{
Name: "os",
Usage: "Define the Operating System wanted",
},
cli.UintFlag{
Name: "cpu",
Usage: "Define the number of cpu for new node(s); default: number used at cluster creation",
Value: 0,
},
cli.Float64Flag{
Name: "ram",
Usage: "Define the size of RAM for new node(s) (in GB); default: size used at cluster creation",
Value: 0.0,
},
cli.UintFlag{
Name: "disk",
Usage: "Define the size of system disk for new node(s) (in GB); default: size used at cluster creation",
Value: 0,
},
cli.BoolFlag{
Name: "gpu",
Usage: "Ask for gpu capable host; default: no",
Hidden: true,
},
},
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
count := c.Uint("count")
if count == 0 {
count = 1
}
public := c.Bool("public")
gpu := c.Bool("gpu")
los := c.String("os")
cpu := c.Uint("cpu")
ram := c.Float64("ram")
disk := c.Uint("disk")
cmdStr := fmt.Sprintf("safescale cluster expand %s -n %d", clusterName, count)
if public {
cmdStr += " -p"
}
if gpu {
cmdStr += " --gpu"
}
if los != "" {
cmdStr += " --os " + los
}
if cpu > 0 {
cmdStr += fmt.Sprintf(" --cpu %d", cpu)
}
if ram > 0.0 {
cmdStr += fmt.Sprintf(" --ram %f ", ram)
}
if disk > 0 {
cmdStr += fmt.Sprintf(" --disk %d", disk)
}
cmdStr = RebrandCommand(cmdStr)
return runCommand(cmdStr)
},
}
// ClusterShrinkCommand handles 'perform <clustername> shrink'
var ClusterShrinkCommand = cli.Command{
Name: "shrink",
Usage: "shrink CLUSTERNAME",
Description: "Removes node(s) from the cluster (starting from the last added)",
Category: "Cluster",
Flags: []cli.Flag{
cli.UintFlag{
Name: "count, n",
Usage: "Define the number of nodes wanted (default: 1)",
Value: 1,
},
cli.BoolFlag{
Name: "public, p",
Usage: "If used, the node(s) will have public IP address (default: no)",
},
},
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
count := c.Uint("count")
if count == 0 {
count = 1
}
public := c.Bool("public")
cmdStr := fmt.Sprintf("safescale cluster shrink %s -n %d", clusterName, count)
if public {
cmdStr += " -p"
}
cmdStr = RebrandCommand(cmdStr)
return runCommand(cmdStr)
},
}
// ClusterCallCommand handles 'perform dcos CLUSTERNAME'
var ClusterCallCommand = cli.Command{
Name: "call",
Usage: "call [options] CLUSTERNAME COMMAND [PARAM ...]",
Description: `
Calls a COMMAND on the designated target of the cluster CLUSTERNAME.
By default, the target is any available master (--any-master).
Note:
Everything after COMMAND will be considered a parameter of the COMMAND.
`,
Category: "Cluster",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "any-master, m",
},
cli.BoolFlag{
Name: "all-masters, a",
},
cli.StringFlag{
Name: "master",
},
},
Action: func(c *cli.Context) error {
err := extractClusterArgument(c)
if err != nil {
return err
}
anyMaster := c.Bool("any-master")
allMasters := c.Bool("all-masters")
master := c.String("master")
target := ""
if allMasters {
target = "-a"
} else if master != "" {
target = "--master " + master
}
if anyMaster || target == "" {
target = "-m"
}
args := c.Args()
tail := args.Tail()
command := args.Get(1)
cmdStr := fmt.Sprintf("safescale cluster call %s %s %s %s", target, clusterName, command, strings.Join(tail[1:], " "))
cmdStr = RebrandCommand(cmdStr)
log.Debugf("Calling '%s'", cmdStr)
return runCommand(cmdStr)
},
}