-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.go
More file actions
419 lines (342 loc) · 11 KB
/
client.go
File metadata and controls
419 lines (342 loc) · 11 KB
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
410
411
412
413
414
415
416
417
418
419
package client
import (
"errors"
"fmt"
"os"
"strconv"
"github.com/deis/deis/deisctl/backend"
"github.com/deis/deis/deisctl/backend/fleet"
"github.com/deis/deis/deisctl/cmd"
"github.com/deis/deis/deisctl/config"
"github.com/deis/deis/deisctl/config/etcd"
"github.com/deis/deis/deisctl/units"
docopt "github.com/docopt/docopt-go"
)
// DeisCtlClient manages Deis components, configuration, and related tasks.
type DeisCtlClient interface {
Config(argv []string) error
Install(argv []string) error
Journal(argv []string) error
List(argv []string) error
RefreshUnits(argv []string) error
Restart(argv []string) error
Scale(argv []string) error
SSH(argv []string) error
Start(argv []string) error
Status(argv []string) error
Stop(argv []string) error
Uninstall(argv []string) error
UpgradePrep(argv []string) error
UpgradeTakeover(argv []string) error
RollingRestart(argv []string) error
}
// Client uses a backend to implement the DeisCtlClient interface.
type Client struct {
Backend backend.Backend
configBackend config.Backend
}
// NewClient returns a Client using the requested backend.
// The only backend currently supported is "fleet".
func NewClient(requestedBackend string) (*Client, error) {
var backend backend.Backend
cb, err := etcd.NewConfigBackend()
if err != nil {
return nil, err
}
if requestedBackend == "" {
requestedBackend = "fleet"
}
switch requestedBackend {
case "fleet":
b, err := fleet.NewClient(cb)
if err != nil {
return nil, err
}
backend = b
default:
return nil, errors.New("invalid backend")
}
return &Client{Backend: backend, configBackend: cb}, nil
}
// UpgradePrep prepares a running cluster to be upgraded
func (c *Client) UpgradePrep(argv []string) error {
usage := `Prepare platform for graceful upgrade.
Usage:
deisctl upgrade-prep [options]
`
if _, err := docopt.Parse(usage, argv, true, "", false); err != nil {
return err
}
return cmd.UpgradePrep(c.Backend)
}
// UpgradeTakeover gracefully restarts a cluster prepared with upgrade-prep
func (c *Client) UpgradeTakeover(argv []string) error {
usage := `Complete the upgrade of a prepped cluster.
Usage:
deisctl upgrade-takeover [options]
`
if _, err := docopt.Parse(usage, argv, true, "", false); err != nil {
return err
}
return cmd.UpgradeTakeover(c.Backend, c.configBackend)
}
// RollingRestart attempts a rolling restart of an instance unit
func (c *Client) RollingRestart(argv []string) error {
usage := `Perform a rolling restart of an instance unit.
Usage:
deisctl rolling-restart <target>
`
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.RollingRestart(args["<target>"].(string), c.Backend)
}
// Config gets or sets a configuration value from the cluster.
//
// A configuration value is stored and retrieved from a key/value store (in this case, etcd)
// at /deis/<component>/<config>. Configuration values are typically used for component-level
// configuration, such as enabling TLS for the routers.
func (c *Client) Config(argv []string) error {
usage := `Gets or sets a configuration value from the cluster.
A configuration value is stored and retrieved from a key/value store
(in this case, etcd) at /deis/<component>/<config>. Configuration
values are typically used for component-level configuration, such as
enabling TLS for the routers.
Note: "deisctl config platform set sshPrivateKey=" expects a path
to a private key.
Usage:
deisctl config <target> get [<key>...]
deisctl config <target> set <key=val>...
deisctl config <target> rm [<key>...]
Examples:
deisctl config platform set domain=mydomain.com
deisctl config platform set sshPrivateKey=$HOME/.ssh/deis
deisctl config controller get webEnabled
deisctl config controller rm webEnabled
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
var action string
var key []string
switch {
case args["set"] == true:
action = "set"
key = args["<key=val>"].([]string)
case args["rm"] == true:
action = "rm"
key = args["<key>"].([]string)
default:
action = "get"
key = args["<key>"].([]string)
}
return cmd.Config(args["<target>"].(string), action, key, c.configBackend)
}
// Install loads the definitions of components from local unit files.
// After Install, the components will be available to Start.
func (c *Client) Install(argv []string) error {
usage := fmt.Sprintf(`Loads the definitions of components from local unit files.
After install, the components will be available to start.
"deisctl install" looks for unit files in these directories, in this order:
- the $DEISCTL_UNITS environment variable, if set
- $HOME/.deis/units
- /var/lib/deis/units
Usage:
deisctl install [<target>...] [options]
Options:
--router-mesh-size=<num> Number of routers to be loaded when installing the platform [default: %d].
`, cmd.DefaultRouterMeshSize)
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
meshSizeArg, _ := args["--router-mesh-size"].(string)
parsedValue, err := strconv.ParseUint(meshSizeArg, 0, 8)
if err != nil || parsedValue < 1 {
fmt.Print("Error: argument --router-mesh-size: invalid value, make sure the value is an integer between 1 and 255.\n")
return err
}
cmd.RouterMeshSize = uint8(parsedValue)
return cmd.Install(args["<target>"].([]string), c.Backend, c.configBackend, cmd.CheckRequiredKeys)
}
// Journal prints log output for the specified components.
func (c *Client) Journal(argv []string) error {
usage := `Prints log output for the specified components.
Usage:
deisctl journal [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Journal(args["<target>"].([]string), c.Backend)
}
// List prints a summary of installed components.
func (c *Client) List(argv []string) error {
usage := `Prints a list of installed units.
Usage:
deisctl list [options]
`
// parse command-line arguments
if _, err := docopt.Parse(usage, argv, true, "", false); err != nil {
return err
}
return cmd.ListUnits(c.Backend)
}
// RefreshUnits overwrites local unit files with those requested.
func (c *Client) RefreshUnits(argv []string) error {
usage := `Overwrites local unit files with those requested.
Downloading from the Deis project GitHub URL by tag or SHA is the only mechanism
currently supported.
"deisctl install" looks for unit files in these directories, in this order:
- the $DEISCTL_UNITS environment variable, if set
- $HOME/.deis/units
- /var/lib/deis/units
Usage:
deisctl refresh-units [-p <target>] [-t <tag>]
Options:
-p --path=<target> where to save unit files [default: $HOME/.deis/units]
-t --tag=<tag> git tag, branch, or SHA to use when downloading unit files
[default: master]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(2)
}
return cmd.RefreshUnits(args["--path"].(string), args["--tag"].(string), units.URL)
}
// Restart stops and then starts components.
func (c *Client) Restart(argv []string) error {
usage := `Stops and then starts the specified components.
Usage:
deisctl restart [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Restart(args["<target>"].([]string), c.Backend)
}
// Scale grows or shrinks the number of running components.
func (c *Client) Scale(argv []string) error {
usage := `Grows or shrinks the number of running components.
Currently "router", "registry" and "store-gateway" are the only types that can be scaled.
Usage:
deisctl scale [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Scale(args["<target>"].([]string), c.Backend)
}
// SSH opens an interactive shell with a machine in the cluster.
func (c *Client) SSH(argv []string) error {
usage := `Open an interactive shell on a machine in the cluster given a unit or machine id.
If an optional <command> is provided, that command is run remotely, and the results returned.
Usage:
deisctl ssh <target> [<command>...]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", true)
if err != nil {
return err
}
target := args["<target>"].(string)
// handle help explicitly since docopt parsing is relaxed
if target == "--help" {
fmt.Println(usage)
os.Exit(0)
}
var vargs []string
if v, ok := args["<command>"]; ok {
vargs = v.([]string)
}
return cmd.SSH(target, vargs, c.Backend)
}
func (c *Client) Dock(argv []string) error {
usage := `Connect to the named docker container and run commands on it.
This is equivalent to running 'docker exec -it <target> <command>'.
Usage:
deisctl dock <target> [<command>...]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", true)
if err != nil {
return err
}
target := args["<target>"].(string)
// handle help explicitly since docopt parsing is relaxed
if target == "--help" {
fmt.Println(usage)
os.Exit(0)
}
var vargs []string
if v, ok := args["<command>"]; ok {
vargs = v.([]string)
}
return cmd.Dock(target, vargs, c.Backend)
}
// Start activates the specified components.
func (c *Client) Start(argv []string) error {
usage := `Activates the specified components.
Usage:
deisctl start [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Start(args["<target>"].([]string), c.Backend)
}
// Status prints the current status of components.
func (c *Client) Status(argv []string) error {
usage := `Prints the current status of components.
Usage:
deisctl status [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Status(args["<target>"].([]string), c.Backend)
}
// Stop deactivates the specified components.
func (c *Client) Stop(argv []string) error {
usage := `Deactivates the specified components.
Usage:
deisctl stop [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Stop(args["<target>"].([]string), c.Backend)
}
// Uninstall unloads the definitions of the specified components.
// After Uninstall, the components will be unavailable until Install is called.
func (c *Client) Uninstall(argv []string) error {
usage := `Unloads the definitions of the specified components.
After uninstall, the components will be unavailable until install is called.
Usage:
deisctl uninstall [<target>...] [options]
`
// parse command-line arguments
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
return err
}
return cmd.Uninstall(args["<target>"].([]string), c.Backend)
}