-
Notifications
You must be signed in to change notification settings - Fork 105
/
script_delay.go
137 lines (121 loc) · 4.02 KB
/
script_delay.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
/*
* 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 script
import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-exec-os/exec"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"strconv"
"github.com/chaosblade-io/chaosblade-exec-os/exec/category"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
)
type ScriptDelayActionCommand struct {
spec.BaseExpActionCommandSpec
}
func NewScriptDelayActionCommand() spec.ExpActionCommandSpec {
return &ScriptDelayActionCommand{
spec.BaseExpActionCommandSpec{
ActionMatchers: []spec.ExpFlagSpec{},
ActionFlags: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "time",
Desc: "sleep time, unit is millisecond",
Required: true,
},
},
ActionExecutor: &ScriptDelayExecutor{},
ActionExample: `
# Add commands to the script "start0() { sleep 10.000000 ...}"
blade create script delay --time 10000 --file test.sh --function-name start0`,
ActionCategories: []string{category.SystemScript},
},
}
}
func (*ScriptDelayActionCommand) Name() string {
return "delay"
}
func (*ScriptDelayActionCommand) Aliases() []string {
return []string{}
}
func (*ScriptDelayActionCommand) ShortDesc() string {
return "Script executed delay"
}
func (s *ScriptDelayActionCommand) LongDesc() string {
if s.ActionLongDesc != "" {
return s.ActionLongDesc
}
return "Sleep in script"
}
type ScriptDelayExecutor struct {
channel spec.Channel
}
func (*ScriptDelayExecutor) Name() string {
return "delay"
}
func (sde *ScriptDelayExecutor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
commands := []string{"cat", "rm", "sed", "awk", "rm"}
if response, ok := sde.channel.IsAllCommandsAvailable(ctx, commands); !ok {
return response
}
scriptFile := model.ActionFlags["file"]
if scriptFile == "" {
log.Errorf(ctx, "file is nil")
return spec.ResponseFailWithFlags(spec.ParameterLess, "file")
}
if !exec.CheckFilepathExists(ctx, sde.channel, scriptFile) {
log.Errorf(ctx, "`%s`, file is invalid. it not found", scriptFile)
return spec.ResponseFailWithFlags(spec.ParameterInvalid, "file", scriptFile, "it is not found")
}
if _, ok := spec.IsDestroy(ctx); ok {
return sde.stop(ctx, scriptFile)
}
functionName := model.ActionFlags["function-name"]
if functionName == "" {
log.Errorf(ctx, "function-name")
return spec.ResponseFailWithFlags(spec.ParameterLess, "function-name")
}
time := model.ActionFlags["time"]
if time == "" {
log.Errorf(ctx, "time")
return spec.ResponseFailWithFlags(spec.ParameterLess, "time")
}
t, err := strconv.Atoi(time)
if err != nil {
log.Errorf(ctx, "time %v it must be a positive integer", time)
return spec.ResponseFailWithFlags(spec.ParameterIllegal, "time", time, "ti must be a positive integer")
}
return sde.start(ctx, scriptFile, functionName, t)
}
func (sde *ScriptDelayExecutor) start(ctx context.Context, scriptFile, functionName string, timt int) *spec.Response {
timeInSecond := float32(timt) / 1000.0
// backup file
response := backScript(ctx, sde.channel, scriptFile)
if !response.Success {
return response
}
response = insertContentToScriptBy(ctx, sde.channel, functionName, fmt.Sprintf("sleep %f", timeInSecond), scriptFile)
if !response.Success {
sde.stop(ctx, scriptFile)
}
return response
}
func (sde *ScriptDelayExecutor) stop(ctx context.Context, scriptFile string) *spec.Response {
return recoverScript(ctx, sde.channel, scriptFile)
}
func (sde *ScriptDelayExecutor) SetChannel(channel spec.Channel) {
sde.channel = channel
}