-
Notifications
You must be signed in to change notification settings - Fork 291
/
system.go
651 lines (535 loc) · 17.1 KB
/
system.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//
// (C) Copyright 2019-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"fmt"
"io"
"strings"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/cmd/dmg/pretty"
"github.com/daos-stack/daos/src/control/common/cmdutil"
"github.com/daos-stack/daos/src/control/lib/control"
"github.com/daos-stack/daos/src/control/lib/daos"
"github.com/daos-stack/daos/src/control/lib/ranklist"
"github.com/daos-stack/daos/src/control/lib/txtfmt"
"github.com/daos-stack/daos/src/control/lib/ui"
)
// SystemCmd is the struct representing the top-level system subcommand.
type SystemCmd struct {
LeaderQuery leaderQueryCmd `command:"leader-query" description:"Query for current Management Service leader"`
Query systemQueryCmd `command:"query" description:"Query DAOS system status"`
Stop systemStopCmd `command:"stop" description:"Perform controlled shutdown of DAOS system"`
Start systemStartCmd `command:"start" description:"Perform start of stopped DAOS system"`
Exclude systemExcludeCmd `command:"exclude" description:"Exclude ranks from DAOS system"`
ClearExclude systemClearExcludeCmd `command:"clear-exclude" description:"Clear excluded state for ranks"`
Erase systemEraseCmd `command:"erase" description:"Erase system metadata prior to reformat"`
ListPools PoolListCmd `command:"list-pools" description:"List all pools in the DAOS system"`
Cleanup systemCleanupCmd `command:"cleanup" description:"Clean up all resources associated with the specified machine"`
SetAttr systemSetAttrCmd `command:"set-attr" description:"Set system attributes"`
GetAttr systemGetAttrCmd `command:"get-attr" description:"Get system attributes"`
DelAttr systemDelAttrCmd `command:"del-attr" description:"Delete system attributes"`
SetProp systemSetPropCmd `command:"set-prop" description:"Set system properties"`
GetProp systemGetPropCmd `command:"get-prop" description:"Get system properties"`
}
type leaderQueryCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
DownReplicas bool `short:"N" long:"down-replicas" description:"Show Down Replicas only"`
}
func (cmd *leaderQueryCmd) Execute(_ []string) (errOut error) {
defer func() {
errOut = errors.Wrap(errOut, "leader query failed")
}()
if cmd.config == nil {
return errors.New("no configuration loaded")
}
ctx := cmd.MustLogCtx()
req := new(control.LeaderQueryReq)
resp, err := control.LeaderQuery(ctx, cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, err)
}
if cmd.DownReplicas {
cmd.Infof("Unresponsive Replicas: %s\n", strings.Join(resp.DownReplicas, ", "))
} else {
cmd.Infof("Current Leader: %s\n Replica Set: %s\n Unresponsive Replicas: %s\n", resp.Leader,
strings.Join(resp.Replicas, ", "), strings.Join(resp.DownReplicas, ", "))
}
return nil
}
// rankListCmd enables rank or host list to be supplied with command to filter
// which ranks are operated upon.
type rankListCmd struct {
Ranks ui.RankSetFlag `long:"ranks" short:"r" description:"Comma separated ranges or individual system ranks to operate on"`
Hosts ui.HostSetFlag `long:"rank-hosts" description:"Hostlist representing hosts whose managed ranks are to be operated on"`
}
// validateHostsRanks validates rank and host lists have correct format.
//
// Populate request with valid list strings.
func (cmd *rankListCmd) validateHostsRanks() error {
if cmd.Hosts.Count() > 0 && cmd.Ranks.Count() > 0 {
return errors.New("--ranks and --rank-hosts options cannot be set together")
}
return nil
}
// systemQueryCmd is the struct representing the command to query system status.
type systemQueryCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
rankListCmd
Verbose bool `long:"verbose" short:"v" description:"Display more member details"`
NotOK bool `long:"not-ok" description:"Display components in need of administrative investigation"`
WantedStates ui.MemberStateSetFlag `long:"with-states" description:"Only show engines in one of a set of comma-separated states"`
}
// Execute is run when systemQueryCmd activates.
func (cmd *systemQueryCmd) Execute(_ []string) (errOut error) {
defer func() {
errOut = errors.Wrap(errOut, "system query failed")
}()
if cmd.NotOK && !cmd.WantedStates.Empty() {
return errors.New("--not-ok and --with-states options cannot be set together")
}
if err := cmd.validateHostsRanks(); err != nil {
return err
}
req := new(control.SystemQueryReq)
req.Hosts.Replace(&cmd.Hosts.HostSet)
req.Ranks.Replace(&cmd.Ranks.RankSet)
req.NotOK = cmd.NotOK
req.WantedStates = cmd.WantedStates.States
resp, err := control.SystemQuery(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, resp.Errors())
}
var out, outErr strings.Builder
if err := pretty.PrintSystemQueryResponse(&out, &outErr, resp,
pretty.PrintWithVerboseOutput(cmd.Verbose)); err != nil {
return err
}
cmd.Info(out.String())
if outErr.String() != "" {
cmd.Error(outErr.String())
}
return resp.Errors()
}
type systemEraseCmd struct {
baseCmd
ctlInvokerCmd
}
func (cmd *systemEraseCmd) Execute(_ []string) error {
resp, err := control.SystemErase(cmd.MustLogCtx(), cmd.ctlInvoker, new(control.SystemEraseReq))
if err != nil {
return err
}
return resp.Errors()
}
// systemStopCmd is the struct representing the command to shutdown DAOS system.
type systemStopCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
rankListCmd
Force bool `long:"force" description:"Force stop DAOS system members"`
}
// Execute is run when systemStopCmd activates.
//
// Always request both prep and kill stages when calling control API.
func (cmd *systemStopCmd) Execute(_ []string) (errOut error) {
defer func() {
errOut = errors.Wrap(errOut, "system stop failed")
}()
if err := cmd.validateHostsRanks(); err != nil {
return err
}
req := &control.SystemStopReq{Force: cmd.Force}
req.Hosts.Replace(&cmd.Hosts.HostSet)
req.Ranks.Replace(&cmd.Ranks.RankSet)
resp, err := control.SystemStop(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, resp.Errors())
}
var out, outErr strings.Builder
if err := pretty.PrintSystemStopResponse(&out, &outErr, resp); err != nil {
return err
}
cmd.Info(out.String())
if outErr.String() != "" {
cmd.Error(outErr.String())
}
return resp.Errors()
}
type baseExcludeCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
rankListCmd
}
func (cmd *baseExcludeCmd) execute(clear bool) error {
if err := cmd.validateHostsRanks(); err != nil {
return err
}
if cmd.Ranks.Count() == 0 && cmd.Hosts.Count() == 0 {
return errors.New("no ranks or hosts specified")
}
req := &control.SystemExcludeReq{Clear: clear}
req.Hosts.Replace(&cmd.Hosts.HostSet)
req.Ranks.Replace(&cmd.Ranks.RankSet)
resp, err := control.SystemExclude(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, resp.Errors())
}
updated := ranklist.NewRankSet()
for _, result := range resp.Results {
updated.Add(result.Rank)
}
if resp.Errors() != nil {
cmd.Errorf("Errors: %s", resp.Errors())
}
cmd.Infof("updated ranks: %s", updated)
return nil
}
type systemExcludeCmd struct {
baseExcludeCmd
}
func (cmd *systemExcludeCmd) Execute(_ []string) error {
return cmd.execute(false)
}
type systemClearExcludeCmd struct {
baseExcludeCmd
}
func (cmd *systemClearExcludeCmd) Execute(_ []string) error {
return cmd.execute(true)
}
// systemStartCmd is the struct representing the command to start system.
type systemStartCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
rankListCmd
}
// Execute is run when systemStartCmd activates.
func (cmd *systemStartCmd) Execute(_ []string) (errOut error) {
defer func() {
errOut = errors.Wrap(errOut, "system start failed")
}()
if err := cmd.validateHostsRanks(); err != nil {
return err
}
req := new(control.SystemStartReq)
req.Hosts.Replace(&cmd.Hosts.HostSet)
req.Ranks.Replace(&cmd.Ranks.RankSet)
resp, err := control.SystemStart(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, resp.Errors())
}
var out, outErr strings.Builder
if err := pretty.PrintSystemStartResponse(&out, &outErr, resp); err != nil {
return err
}
cmd.Info(out.String())
if outErr.String() != "" {
cmd.Error(outErr.String())
}
return resp.Errors()
}
type systemCleanupCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Machine string `positional-arg-name:"<Machine to cleanup>"`
} `positional-args:"yes"`
Verbose bool `long:"verbose" short:"v" description:"Output additional cleanup information"`
}
func (cmd *systemCleanupCmd) Execute(_ []string) (errOut error) {
defer func() {
errOut = errors.Wrap(errOut, "system cleanup failed")
}()
ctx := cmd.MustLogCtx()
req := new(control.SystemCleanupReq)
req.SetSystem(cmd.config.SystemName)
req.Machine = cmd.Args.Machine
resp, err := control.SystemCleanup(ctx, cmd.ctlInvoker, req)
if err != nil {
return err // control api returned an error, disregard response
}
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, err)
}
var out, outErr strings.Builder
if err := pretty.PrintSystemCleanupResponse(&out, &outErr, resp, cmd.Verbose); err != nil {
return err
}
if outErr.String() != "" {
cmd.Error(outErr.String())
}
// Infof prints raw string and doesn't try to expand "%"
// preserving column formatting in txtfmt table
cmd.Infof("%s", out.String())
return resp.Errors()
}
// systemSetAttrCmd represents the command to set system attributes.
type systemSetAttrCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Attrs ui.SetPropertiesFlag `positional-arg-name:"system attributes to set (key:val[,key:val...])" required:"1"`
} `positional-args:"yes"`
}
// Execute is run when systemSetAttrCmd subcommand is activated.
func (cmd *systemSetAttrCmd) Execute(_ []string) error {
req := &control.SystemSetAttrReq{
Attributes: cmd.Args.Attrs.ParsedProps,
}
err := control.SystemSetAttr(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(nil, err)
}
if err != nil {
return errors.Wrap(err, "system set-attr failed")
}
cmd.Info("system set-attr succeeded")
return nil
}
// systemGetAttrCmd represents the command to get system attributes.
type systemGetAttrCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Attrs ui.GetPropertiesFlag `positional-arg-name:"system attributes to get (key[,key...])"`
} `positional-args:"yes"`
}
func prettyPrintAttrs(out io.Writer, attrs map[string]string) {
if len(attrs) == 0 {
fmt.Fprintln(out, "No system attributes found.")
return
}
nameTitle := "Name"
valueTitle := "Value"
table := []txtfmt.TableRow{}
for key, val := range attrs {
row := txtfmt.TableRow{}
row[nameTitle] = key
row[valueTitle] = val
table = append(table, row)
}
tf := txtfmt.NewTableFormatter(nameTitle, valueTitle)
tf.InitWriter(out)
tf.Format(table)
}
// Execute is run when systemGetAttrCmd subcommand is activated.
func (cmd *systemGetAttrCmd) Execute(_ []string) error {
req := &control.SystemGetAttrReq{
Keys: cmd.Args.Attrs.ParsedProps.ToSlice(),
}
resp, err := control.SystemGetAttr(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, err)
}
if err != nil {
return errors.Wrap(err, "system get-attr failed")
}
var bld strings.Builder
prettyPrintAttrs(&bld, resp.Attributes)
cmd.Infof("%s", bld.String())
return nil
}
// systemDelAttrCmd represents the command to delete system attributes.
type systemDelAttrCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Attrs ui.GetPropertiesFlag `positional-arg-name:"system attributes to delete (key[,key...])" required:"1"`
} `positional-args:"yes"`
}
// Execute is run when systemDelAttrCmd subcommand is activated.
func (cmd *systemDelAttrCmd) Execute(_ []string) error {
req := &control.SystemSetAttrReq{
Attributes: make(map[string]string),
}
for _, key := range cmd.Args.Attrs.ParsedProps.ToSlice() {
req.Attributes[key] = ""
}
err := control.SystemSetAttr(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(nil, err)
}
if err != nil {
return errors.Wrap(err, "system del-attr failed")
}
cmd.Info("system del-attr succeeded")
return nil
}
type systemSetPropsFlag struct {
ui.SetPropertiesFlag
SystemProps daos.SystemPropertyMap
ParsedProps map[daos.SystemPropertyKey]daos.SystemPropertyValue
}
func (f *systemSetPropsFlag) UnmarshalFlag(fv string) error {
err := f.SetPropertiesFlag.UnmarshalFlag(fv)
if err != nil {
return err
}
if f.SystemProps == nil {
f.SystemProps = daos.SystemProperties()
}
f.ParsedProps = make(map[daos.SystemPropertyKey]daos.SystemPropertyValue)
for k, v := range f.SetPropertiesFlag.ParsedProps {
if prop, ok := f.SystemProps.Get(k); ok {
if err := prop.Value.Handler(v); err != nil {
return errors.Wrapf(err, "invalid system property value for %s", k)
}
f.ParsedProps[prop.Key] = prop.Value
} else {
return errors.Errorf("invalid system property key: %s", k)
}
}
return nil
}
func (f *systemSetPropsFlag) Complete(match string) []flags.Completion {
if f.SystemProps == nil {
f.SystemProps = daos.SystemProperties()
}
comps := make(ui.CompletionMap)
for _, prop := range f.SystemProps {
comps[prop.Key.String()] = prop.Value.Choices()
}
f.SetCompletions(comps)
return f.SetPropertiesFlag.Complete(match)
}
// systemSetPropCmd represents the command to set system properties.
type systemSetPropCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Props systemSetPropsFlag `positional-arg-name:"system properties to set (key:val[,key:val...])" required:"1"`
} `positional-args:"yes"`
}
// Execute is run when systemSetPropCmd subcommand is activated.
func (cmd *systemSetPropCmd) Execute(_ []string) error {
req := &control.SystemSetPropReq{
Properties: cmd.Args.Props.ParsedProps,
}
err := control.SystemSetProp(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(nil, err)
}
if err != nil {
return errors.Wrap(err, "system set-prop failed")
}
cmd.Info("system set-prop succeeded")
return nil
}
type systemGetPropsFlag struct {
ui.GetPropertiesFlag
SystemProps daos.SystemPropertyMap
ParsedProps []daos.SystemPropertyKey
}
func (f *systemGetPropsFlag) UnmarshalFlag(fv string) error {
err := f.GetPropertiesFlag.UnmarshalFlag(fv)
if err != nil {
return err
}
if f.SystemProps == nil {
f.SystemProps = daos.SystemProperties()
}
for _, k := range f.GetPropertiesFlag.ParsedProps.ToSlice() {
if prop, ok := f.SystemProps.Get(k); ok {
f.ParsedProps = append(f.ParsedProps, prop.Key)
} else {
return errors.Errorf("invalid system property key: %s", k)
}
}
return nil
}
func (f *systemGetPropsFlag) Complete(match string) []flags.Completion {
if f.SystemProps == nil {
f.SystemProps = daos.SystemProperties()
}
comps := make(ui.CompletionMap)
for _, prop := range f.SystemProps {
comps[prop.Key.String()] = prop.Value.Choices()
}
f.SetCompletions(comps)
return f.GetPropertiesFlag.Complete(match)
}
// systemGetPropCmd represents the command to get system properties.
type systemGetPropCmd struct {
baseCmd
cfgCmd
ctlInvokerCmd
cmdutil.JSONOutputCmd
Args struct {
Props systemGetPropsFlag `positional-arg-name:"system properties to get (key[,key...])"`
} `positional-args:"yes"`
}
func prettyPrintSysProps(out io.Writer, props []*daos.SystemProperty) {
if len(props) == 0 {
fmt.Fprintln(out, "No system properties found.")
return
}
nameTitle := "Name"
valueTitle := "Value"
table := []txtfmt.TableRow{}
for _, prop := range props {
row := txtfmt.TableRow{}
row[nameTitle] = fmt.Sprintf("%s (%s)", prop.Description, prop.Key)
row[valueTitle] = prop.Value.String()
table = append(table, row)
}
tf := txtfmt.NewTableFormatter(nameTitle, valueTitle)
tf.InitWriter(out)
tf.Format(table)
}
// Execute is run when systemGetPropCmd subcommand is activated.
func (cmd *systemGetPropCmd) Execute(_ []string) error {
req := &control.SystemGetPropReq{
Keys: cmd.Args.Props.ParsedProps,
}
resp, err := control.SystemGetProp(cmd.MustLogCtx(), cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp.Properties, err)
}
if err != nil {
return errors.Wrap(err, "system get-attr failed")
}
var bld strings.Builder
prettyPrintSysProps(&bld, resp.Properties)
cmd.Infof("%s", bld.String())
return nil
}