-
Notifications
You must be signed in to change notification settings - Fork 958
/
Copy pathcheck_os.go
425 lines (378 loc) · 12.3 KB
/
check_os.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
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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 cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var allCmd []string
var BladeBinPath string
var AllCheckExecCmds []CheckExecCmd
const (
BladeBin = "blade"
OperatorCommand = "operator"
)
type CheckOsCommand struct {
command *cobra.Command
*baseExpCommandService
sw *sync.WaitGroup
}
type ExecResult struct {
cmd string
result string
info string
}
type CheckExecCmd struct {
ExpName string
ActionName string
Scope string
ExecResult []*ExecResult
}
func (doc *CheckOsCommand) CobraCmd() *cobra.Command {
return doc.command
}
func (doc *CheckOsCommand) Name() string {
return ""
}
func (doc *CheckOsCommand) Init() {
doc.command = &cobra.Command{
Use: "os",
Short: "Check the environment of os for chaosblade",
Long: "Check the environment of os for chaosblade",
RunE: func(cmd *cobra.Command, args []string) error {
return doc.checkOsAll()
},
Example: doc.detectExample(),
}
BladeBinPath = path.Join(util.GetProgramPath(), BladeBin)
doc.baseExpCommandService = newBaseExpCheckCommandService(doc)
}
func (doc *CheckOsCommand) detectExample() string {
return "check os"
}
// check all os action
func (doc *CheckOsCommand) checkOsAll() error {
// 1. build all cmd
err := doc.buildAllOsCmd()
if err != nil {
fmt.Printf("[failed] check os failed! err: %s \n", err.Error())
}
// 2. one by one exec cmd
for _, allCheckExecCmd := range AllCheckExecCmds {
switch allCheckExecCmd.Scope {
case OperatorCommand:
doc.execOperatorCmd(&allCheckExecCmd)
default:
doc.execBladeCmd(&allCheckExecCmd, true)
}
}
// 3. output the result
var output [][]string
for _, checkExecCmd := range AllCheckExecCmds {
for _, execResult := range checkExecCmd.ExecResult {
oneOutput := []string{checkExecCmd.ExpName, checkExecCmd.ActionName, execResult.result, execResult.info}
output = append(output, oneOutput)
}
}
doc.outPutTheResult(output)
return nil
}
func (doc *CheckOsCommand) outPutTheResult(output [][]string) {
fmt.Printf("------------summary----------")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"experiment", "command", "result", "info"})
table.SetRowLine(true)
table.AppendBulk(output)
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.Render()
}
func (doc *CheckOsCommand) execBladeCmd(checkExecCmd *CheckExecCmd, osAll bool) *spec.Response {
ch := channel.NewLocalChannel()
var response *spec.Response
for _, execResult := range checkExecCmd.ExecResult {
//1.1 create os cmd
response = ch.Run(context.Background(), BladeBinPath, execResult.cmd)
var res spec.Response
if !response.Success {
execResult.result = "failed"
execResult.info = fmt.Sprintf("%s, exec failed! create err: %s", execResult.cmd, response.Err)
response.Err = fmt.Sprintf("[failed] %s, exec failed! create err: %s", execResult.cmd, response.Err)
if osAll {
fmt.Printf("[failed] %s, exec failed! create err: %s \n", execResult.cmd, response.Err)
}
continue
}
err := json.Unmarshal([]byte(response.Result.(string)), &res)
if err != nil {
execResult.result = "failed"
execResult.info = fmt.Sprintf("%s, exec failed! create err: %s", execResult.cmd, response.Result)
response.Err = fmt.Sprintf("[failed] %s, exec failed! create err: %s", execResult.cmd, response.Result)
if osAll {
fmt.Printf("[failed] %s, exec failed! create err: %s \n", execResult.cmd, response.Result)
}
continue
}
// 1.2 destroy os cmd
response = ch.Run(context.Background(), BladeBinPath, fmt.Sprintf("destroy %s", res.Result.(string)))
if !response.Success {
execResult.result = "failed"
execResult.info = fmt.Sprintf("%s, exec failed! destroy err: %s", execResult.cmd, response.Err)
response.Err = fmt.Sprintf("[failed] %s, exec failed! destroy err: %s", execResult.cmd, response.Err)
if osAll {
fmt.Printf("[failed] %s, exec failed! destroy err: %s \n", execResult.cmd, response.Err)
}
continue
}
execResult.result = "success"
execResult.info = fmt.Sprintf("%s, exec success!", execResult.cmd)
response.Result = fmt.Sprintf("[success] %s, success!", execResult.cmd)
if osAll {
fmt.Printf("[success] %s, success! \n", execResult.cmd)
}
}
return response
}
func (doc *CheckOsCommand) execOperatorCmd(checkExecCmd *CheckExecCmd) {
ch := channel.NewLocalChannel()
if len(checkExecCmd.ExecResult) == 0 {
return
}
for _, execResult := range checkExecCmd.ExecResult {
cmdArr := strings.Split(execResult.cmd, "|")
operatorCmd := strings.TrimSpace(cmdArr[0])
if len(cmdArr) != 2 {
fmt.Printf("[failed] %s, failed! error: yaml faile is wrong \n", execResult.cmd)
execResult.info = fmt.Sprintf("yaml faile is wrong")
execResult.result = "failed"
continue
}
if !ch.IsCommandAvailable(context.TODO(), operatorCmd) {
fmt.Printf("[failed] %s, failed! error: `%s` command not install \n", cmdArr[1], operatorCmd)
execResult.info = fmt.Sprintf("`%s` command not install", operatorCmd)
execResult.result = "failed"
continue
}
fmt.Printf("[success] %s, success! `%s` command exists \n", cmdArr[1], operatorCmd)
execResult.info = fmt.Sprintf("`%s` command exists", operatorCmd)
execResult.result = "success"
}
}
// build all os cmd
func (doc *CheckOsCommand) buildAllOsCmd() error {
models := AllCheckModels.Models
for _, model := range models {
expName := model.ExpName
scope := model.ExpScope
for _, action := range model.Actions() {
actionName := action.Name()
// merge matchers and flags
flags := doc.mergeMatchesAndFlags(action.Matchers(), action.Flags())
var execResult []*ExecResult
var err error
switch scope {
case OperatorCommand:
execResult = doc.buildOperatorCmd(action.Programs(), expName, actionName)
default:
execResult, err = doc.buildBladeCmd(action.Programs(), flags, expName, actionName)
if err != nil {
return err
}
}
AllCheckExecCmds = append(AllCheckExecCmds, CheckExecCmd{
ExpName: expName,
ActionName: actionName,
Scope: scope,
ExecResult: execResult,
})
}
}
return nil
}
func (doc *CheckOsCommand) buildOperatorCmd(programs []string, expName, actionName string) []*ExecResult {
var execResult []*ExecResult
for _, program := range programs {
execResult = append(execResult, &ExecResult{
cmd: fmt.Sprintf("%s|%s %s", program, expName, actionName),
})
}
return execResult
}
func (doc *CheckOsCommand) buildBladeCmd(programs []string, flags []spec.ExpFlagSpec, expName, actionName string) ([]*ExecResult, error) {
var execResults []*ExecResult
for _, program := range programs {
bladeCmd := fmt.Sprintf("%s %s %s", program, expName, actionName)
// build cmd by flags and matchers
execResult, err := doc.buildCmdByMatchersAndFlags(flags, expName, actionName, bladeCmd)
if err != nil {
return nil, err
}
execResults = append(execResults, execResult...)
}
return execResults, nil
}
// merge matchers and flags
func (doc *CheckOsCommand) mergeMatchesAndFlags(matches, flags []spec.ExpFlagSpec) []spec.ExpFlagSpec {
mergeResult := make([]spec.ExpFlagSpec, 0)
for _, flag := range flags {
mergeResult = append(mergeResult, flag)
}
for _, matcher := range matches {
mergeResult = append(mergeResult, matcher)
}
return mergeResult
}
func (doc *CheckOsCommand) buildCmdByMatchersAndFlags(flags []spec.ExpFlagSpec, expName, actionName, cmd string) ([]*ExecResult, error) {
var execResult []*ExecResult
if len(flags) == 0 {
execResult = append(execResult, &ExecResult{
cmd: cmd,
})
return execResult, nil
}
// build base cmd by required flag
for _, flag := range flags {
if !flag.FlagRequired() {
continue
}
if flag.FlagDefault() == "" {
return nil, errors.New("build all commadn by yaml file failed! less required parameter, model: " + expName +
" action: " + actionName + " parameter: " + flag.FlagName())
}
cmd += fmt.Sprintf(" --%s %s", flag.FlagName(), flag.FlagDefault())
execResult = append(execResult, &ExecResult{cmd: cmd})
}
// add other flag
baseCmd := cmd
for _, flag := range flags {
if flag.FlagRequired() {
continue
}
if flag.FlagDefault() == "" {
continue
}
cmd += fmt.Sprintf(" --%s %s", flag.FlagName(), flag.FlagDefault())
execResult = append(execResult, &ExecResult{cmd: cmd})
cmd = baseCmd
}
return execResult, nil
}
// bind flags
func (doc *CheckOsCommand) bindFlagsFunction() func(commandFlags map[string]func() string, cmd *cobra.Command, specFlags []spec.ExpFlagSpec) {
return func(commandFlags map[string]func() string, cmd *cobra.Command, specFlags []spec.ExpFlagSpec) {
//set action flags
for _, flag := range specFlags {
flagName := flag.FlagName()
flagDesc := flag.FlagDesc()
if flag.FlagNoArgs() {
var key bool
cmd.PersistentFlags().BoolVar(&key, flagName, false, flagDesc)
commandFlags[flagName] = func() string {
return strconv.FormatBool(key)
}
} else {
var key string
cmd.PersistentFlags().StringVar(&key, flagName, "", flagDesc)
commandFlags[flagName] = func() string {
return key
}
}
}
}
}
// RunE
func (doc *CheckOsCommand) actionRunEFunc(target, scope string, actionCommand *actionCommand, actionCommandSpec spec.ExpActionCommandSpec) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// 1. build expModel
expModel := createExpModel(target, scope, actionCommandSpec.Name(), cmd)
var response spec.Response
// 2. build cmd
programs := actionCommandSpec.Programs()
// 2.1 merge matchers and flags
cmdStr := ""
flags := doc.mergeMatchesAndFlags(actionCommandSpec.Matchers(), actionCommandSpec.Flags())
// 2.2 build cmd by flags
for _, flag := range flags {
// runEFun donot use default
value, ok := expModel.ActionFlags[flag.FlagName()]
if flag.FlagRequired() {
if !ok || value == "" {
response.Code = spec.ParameterLess.Code
response.Success = false
response.Err = fmt.Sprintf("[failed] check failed! err: less required parameter!")
cmd.Println(response.Print())
return nil
}
}
if value == "" {
continue
}
cmdStr += fmt.Sprintf(" --%s %s", flag.FlagName(), value)
}
// 3. exec cmd
switch scope {
case OperatorCommand:
failedCmd := ""
successCmd := ""
checkStr := fmt.Sprintf("%s %s", target, expModel.ActionName)
for _, program := range programs {
if channel.NewLocalChannel().IsCommandAvailable(context.TODO(), program) {
if successCmd == "" {
successCmd = program
} else {
successCmd += "|" + program
}
} else {
if failedCmd == "" {
failedCmd = program
} else {
failedCmd += "|" + program
}
}
}
if failedCmd != "" {
response.Code = spec.CommandIllegal.Code
response.Success = false
response.Err = fmt.Sprintf("[failed] %s, failed! `%s` command not install", checkStr, failedCmd)
} else {
response.Code = spec.OK.Code
response.Success = true
response.Result = fmt.Sprintf("[success] %s, success! `%s` command exists", checkStr, successCmd)
}
default:
cmdStr = fmt.Sprintf("%s %s %s %s", programs[0], expModel.Target, expModel.ActionName, cmdStr)
checkExecCmd := CheckExecCmd{ExpName: target, ActionName: actionCommandSpec.Name(), Scope: scope, ExecResult: []*ExecResult{&ExecResult{cmd: cmdStr}}}
response = *doc.execBladeCmd(&checkExecCmd, false)
}
return &response
}
}
func (doc *CheckOsCommand) actionPostRunEFunc(actionCommand *actionCommand) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return nil
}
}