-
Notifications
You must be signed in to change notification settings - Fork 291
/
check.go
406 lines (320 loc) · 10.1 KB
/
check.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
//
// (C) Copyright 2022-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"bytes"
"context"
"fmt"
"strconv"
"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/ui"
)
type checkCmdRoot struct {
Enable checkEnableCmd `command:"enable" description:"Enable system checker"`
Disable checkDisableCmd `command:"disable" description:"Disable system checker"`
Start checkStartCmd `command:"start" description:"Start a system check"`
Stop checkStopCmd `command:"stop" description:"Stop a system check"`
Query checkQueryCmd `command:"query" description:"Query a system check"`
SetPolicy checkSetPolicyCmd `command:"set-policy" description:"Set system checker policies"`
GetPolicy checkGetPolicyCmd `command:"get-policy" description:"Get system checker policies"`
Repair checkRepairCmd `command:"repair" description:"Repair a reported system check problem"`
}
type poolIDSet []PoolID
func (p poolIDSet) List() (ids []string) {
ids = make([]string, len(p))
for i, id := range p {
ids[i] = id.String()
}
return
}
type checkCmdBase struct {
cmdutil.JSONOutputCmd
cmdutil.LogCmd
cfgCmd
ctlInvokerCmd
}
func (c *checkCmdBase) Execute(_ []string) error {
return errors.New("not implemented")
}
type checkPoolCmdBase struct {
checkCmdBase
Args struct {
Pools poolIDSet `positional-arg-name:"[pool name or UUID [pool name or UUID]] "`
} `positional-args:"yes"`
}
type checkEnableCmd struct {
checkCmdBase
}
func (cmd *checkEnableCmd) Execute([]string) error {
req := new(control.SystemCheckEnableReq)
if err := control.SystemCheckEnable(context.Background(), cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("system checker enabled")
return nil
}
type checkDisableCmd struct {
checkCmdBase
}
func (cmd *checkDisableCmd) Execute([]string) error {
req := new(control.SystemCheckDisableReq)
if err := control.SystemCheckDisable(context.Background(), cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("system checker disabled")
return nil
}
type setRepPolFlag struct {
ui.SetPropertiesFlag
SetPolicies []*control.SystemCheckPolicy
}
func (f *setRepPolFlag) UnmarshalFlag(fv string) error {
var keys []string
for _, class := range control.CheckerPolicyClasses() {
keys = append(keys, class.String())
}
f.SettableKeys(keys...)
if err := f.SetPropertiesFlag.UnmarshalFlag(fv); err != nil {
return err
}
f.SetPolicies = make([]*control.SystemCheckPolicy, 0, len(f.ParsedProps))
for class, action := range f.ParsedProps {
policy, err := control.NewSystemCheckPolicy(class, action)
if err != nil {
return err
}
f.SetPolicies = append(f.SetPolicies, policy)
}
return nil
}
func (f *setRepPolFlag) Complete(match string) []flags.Completion {
actions := control.CheckerPolicyActions()
actKeys := make([]string, len(actions))
for i, act := range actions {
actKeys[i] = act.String()
}
comps := make(ui.CompletionMap)
for _, class := range control.CheckerPolicyClasses() {
comps[class.String()] = actKeys
}
f.SetCompletions(comps)
return f.SetPropertiesFlag.Complete(match)
}
type checkStartCmd struct {
checkPoolCmdBase
DryRun bool `short:"n" long:"dry-run" description:"Scan only; do not initiate repairs."`
Reset bool `short:"r" long:"reset" description:"Reset the system check state."`
Failout ui.EnabledFlag `short:"f" long:"failout" description:"Stop on failure." choice:"on" choice:"off"`
Auto ui.EnabledFlag `short:"a" long:"auto" description:"Attempt to automatically repair problems." choice:"on" choice:"off"`
FindOrphans bool `short:"O" long:"find-orphans" description:"Find orphaned pools."`
Policies setRepPolFlag `short:"p" long:"policies" description:"Set repair policies."`
}
func (cmd *checkStartCmd) Execute(_ []string) error {
ctx := context.Background()
req := new(control.SystemCheckStartReq)
req.Uuids = cmd.Args.Pools.List()
if cmd.DryRun {
req.Flags |= uint32(control.SystemCheckFlagDryRun)
}
if cmd.Reset {
req.Flags |= uint32(control.SystemCheckFlagReset)
}
if cmd.Failout.Set {
if cmd.Failout.Enabled {
req.Flags |= uint32(control.SystemCheckFlagFailout)
} else {
req.Flags |= uint32(control.SystemCheckFlagDisableFailout)
}
}
if cmd.Auto.Set {
if cmd.Auto.Enabled {
req.Flags |= uint32(control.SystemCheckFlagAuto)
} else {
req.Flags |= uint32(control.SystemCheckFlagDisableAuto)
}
}
if cmd.FindOrphans {
req.Flags |= uint32(control.SystemCheckFlagFindOrphans)
}
req.Policies = cmd.Policies.SetPolicies
if err := control.SystemCheckStart(ctx, cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("system checker started")
return nil
}
type checkStopCmd struct {
checkPoolCmdBase
}
func (cmd *checkStopCmd) Execute(_ []string) error {
ctx := context.Background()
req := new(control.SystemCheckStopReq)
req.Uuids = cmd.Args.Pools.List()
if err := control.SystemCheckStop(ctx, cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("system checker stopped")
return nil
}
type checkQueryCmd struct {
checkPoolCmdBase
Verbose bool `short:"v" long:"verbose" description:"Show more detailed information."`
}
func (cmd *checkQueryCmd) Execute(_ []string) error {
ctx := context.Background()
req := new(control.SystemCheckQueryReq)
req.Uuids = cmd.Args.Pools.List()
resp, err := control.SystemCheckQuery(ctx, cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, nil)
}
if err != nil {
return err
}
var buf bytes.Buffer
pretty.PrintCheckQueryResp(&buf, resp, cmd.Verbose)
cmd.Info(buf.String())
return nil
}
type checkSetPolicyCmd struct {
checkCmdBase
ResetToDefaults bool `short:"d" long:"reset-defaults" description:"Set all policies to their default action."`
AllInteractive bool `short:"a" long:"all-interactive" description:"Set all policies to interactive."`
Args struct {
Policies setRepPolFlag `description:"Repair policies (required unless --all-interactive is specified)"`
} `positional-args:"yes"`
}
func (cmd *checkSetPolicyCmd) Execute(_ []string) error {
ctx := context.Background()
req := &control.SystemCheckSetPolicyReq{
ResetToDefaults: cmd.ResetToDefaults,
AllInteractive: cmd.AllInteractive,
Policies: cmd.Args.Policies.SetPolicies,
}
if err := control.SystemCheckSetPolicy(ctx, cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("system checker policies updated")
return nil
}
type getRepPolFlag struct {
ui.GetPropertiesFlag
ReqClasses []control.SystemCheckFindingClass
}
func (f *getRepPolFlag) UnmarshalFlag(fv string) error {
var keys []string
for _, class := range control.CheckerPolicyClasses() {
keys = append(keys, class.String())
}
f.GettableKeys(keys...)
if err := f.GetPropertiesFlag.UnmarshalFlag(fv); err != nil {
return err
}
i := 0
f.ReqClasses = make([]control.SystemCheckFindingClass, len(f.ParsedProps))
for _, class := range f.ParsedProps.ToSlice() {
if err := f.ReqClasses[i].FromString(class); err != nil {
return err
}
i++
}
return nil
}
func (f *getRepPolFlag) Complete(match string) []flags.Completion {
comps := make(ui.CompletionMap)
for _, class := range control.CheckerPolicyClasses() {
comps[class.String()] = nil
}
f.SetCompletions(comps)
return f.GetPropertiesFlag.Complete(match)
}
type checkGetPolicyCmd struct {
checkCmdBase
LastUsed bool `short:"L" long:"last" description:"Fetch the last policy used by the checker."`
Args struct {
Classes getRepPolFlag `description:"Inconsistency class names"`
} `positional-args:"yes"`
}
func (cmd *checkGetPolicyCmd) Execute(_ []string) error {
ctx := context.Background()
req := new(control.SystemCheckGetPolicyReq)
req.LastUsed = cmd.LastUsed
req.SetClasses(cmd.Args.Classes.ReqClasses)
resp, err := control.SystemCheckGetPolicy(ctx, cmd.ctlInvoker, req)
if cmd.JSONOutputEnabled() {
return cmd.OutputJSON(resp, nil)
}
if err != nil {
return err
}
var buf bytes.Buffer
pretty.PrintCheckerPolicies(&buf, resp.CheckerFlags, resp.Policies...)
cmd.Info(buf.String())
return nil
}
type repairSeqNum uint64
func (r repairSeqNum) String() string {
return fmt.Sprintf("0x%x", uint64(r))
}
func (r *repairSeqNum) UnmarshalFlag(value string) error {
var val uint64
var err error
if strings.HasPrefix(value, "0x") {
cleaned := strings.Replace(value, "0x", "", -1)
val, err = strconv.ParseUint(cleaned, 16, 64)
} else {
val, err = strconv.ParseUint(value, 10, 64)
}
if err != nil {
return err
}
*r = repairSeqNum(val)
return nil
}
type checkRepairCmd struct {
checkCmdBase
ForAll bool `short:"f" long:"for-all" description:"Take the same action for all inconsistencies with the same class."`
Args struct {
SeqNum repairSeqNum `positional-arg-name:"[seq-num]" required:"1"`
SelectedAction int `positional-arg-name:"[action]" required:"1"`
} `positional-args:"yes"`
}
func (cmd *checkRepairCmd) Execute(_ []string) error {
ctx := context.Background()
qReq := new(control.SystemCheckQueryReq)
qReq.Seqs = []uint64{uint64(cmd.Args.SeqNum)}
qResp, err := control.SystemCheckQuery(ctx, cmd.ctlInvoker, qReq)
if err != nil {
return err
}
if len(qResp.Reports) == 0 {
return errors.Errorf("no report found for seq %s", cmd.Args.SeqNum)
}
report := qResp.Reports[0]
if !report.IsInteractive() {
return errors.Errorf("finding %s is already resolved: %s", cmd.Args.SeqNum, report.Resolution())
}
choices := report.RepairChoices()
if cmd.Args.SelectedAction < 0 || cmd.Args.SelectedAction >= len(choices) {
return errors.Errorf("invalid action %d for seq %s", cmd.Args.SelectedAction, cmd.Args.SeqNum)
}
req := new(control.SystemCheckRepairReq)
req.Seq = uint64(cmd.Args.SeqNum)
req.ForAll = cmd.ForAll
if err := req.SetAction(int32(choices[cmd.Args.SelectedAction].Action)); err != nil {
return err
}
if err := control.SystemCheckRepair(ctx, cmd.ctlInvoker, req); err != nil {
return err
}
cmd.Info("Repair request sent")
return nil
}