Skip to content

Commit 03c6b8d

Browse files
authored
Merge pull request #776 from fjz99/feat_middleware
feat: add middleware executor for chaosblade-exec-middleware module
2 parents d7cae27 + b927d91 commit 03c6b8d

File tree

4 files changed

+136
-5
lines changed

4 files changed

+136
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _test
1010
releases
1111
tmp
1212
.idea/
13+
.vscode/
1314

1415
# Architecture specific extensions/prefixes
1516
trace.out

Makefile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ BUILD_TARGET_CACHE=$(BUILD_TARGET)/cache
4848
BLADE_EXEC_OS_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-os.git
4949
BLADE_EXEC_OS_BRANCH=master
5050

51+
52+
# chaosblade-exec-middleware
53+
BLADE_EXEC_MIDDLEWARE_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-middleware.git
54+
BLADE_EXEC_MIDDLEWARE_BRANCH=main
55+
5156
# chaosblade-exec-cloud
5257
BLADE_EXEC_CLOUD_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cloud.git
5358
BLADE_EXEC_CLOUD_BRANCH=main
5459

60+
5561
# chaosblade-exec-cri
5662
BLADE_EXEC_CRI_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cri.git
5763
BLADE_EXEC_CRI_BRANCH=main
@@ -89,8 +95,8 @@ help:
8995
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>...\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
9096

9197
##@ Build
92-
build: pre_build cli nsexec os cloud cri cplus java kubernetes package check_yaml ## Build all scenarios
93-
#build: pre_build cli nsexec os cloud cri cplus java kubernetes upx package check_yaml ## Build all scenarios
98+
build: pre_build cli nsexec os cloud middleware cri cplus java kubernetes package check_yaml ## Build all scenarios
99+
#build: pre_build cli nsexec os cloud middleware cri cplus java kubernetes upx package check_yaml ## Build all scenarios
94100

95101
# for example: make build_with cli
96102
build_with: pre_build ## Select scenario build, for example `make build_with cli os cloud docker cri kubernetes java cplus`
@@ -102,12 +108,12 @@ build_with_linux_arm: pre_build build_linux_arm_with_arg ## Select scenario buil
102108

103109
# build chaosblade linux version by docker image
104110
build_linux: ## Build linux version of all scenarios by docker image
105-
make build_with_linux ARGS="cli os cloud cri nsexec kubernetes java cplus check_yaml" upx package
111+
make build_with_linux ARGS="cli os cloud middleware cri nsexec kubernetes java cplus check_yaml" upx package
106112

107113
build_linux_arm: ## Build linux arm version of all scenarios by docker image
108-
make build_with_linux_arm ARGS="cli os cloud cri nsexec kubernetes java cplus check_yaml" upx package
114+
make build_with_linux_arm ARGS="cli os cloud middleware cri nsexec kubernetes java cplus check_yaml" upx package
109115

110-
build_darwin: pre_build cli os cloud cri cplus java kubernetes upx package check_yaml ## Build all scenarios darwin version
116+
build_darwin: pre_build cli os cloud middleware cri cplus java kubernetes upx package check_yaml ## Build all scenarios darwin version
111117

112118
##@ Build sub
113119

@@ -137,6 +143,19 @@ endif
137143
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-os/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
138144
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-os/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)
139145

146+
middleware: ## Build middleware experimental scenarios.
147+
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-middleware, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware))
148+
git clone -b $(BLADE_EXEC_MIDDLEWARE_BRANCH) $(BLADE_EXEC_MIDDLEWARE_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware
149+
else
150+
ifdef ALERTMSG
151+
$(error $(ALERTMSG))
152+
endif
153+
git -C $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware pull origin $(BLADE_EXEC_MIDDLEWARE_BRANCH)
154+
endif
155+
make -C $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware
156+
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
157+
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)
158+
140159
cloud: ## Build cloud experimental scenarios.
141160
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-cloud, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud))
142161
git clone -b $(BLADE_EXEC_CLOUD_BRANCH) $(BLADE_EXEC_CLOUD_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud
@@ -150,6 +169,7 @@ endif
150169
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
151170
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)
152171

172+
153173
kubernetes: ## Build kubernetes experimental scenarios.
154174
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-operator, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-operator))
155175
git clone -b $(BLADE_OPERATOR_BRANCH) $(BLADE_OPERATOR_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-operator

cli/cmd/exp.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/chaosblade-io/chaosblade-exec-cri/exec"
2323
"github.com/chaosblade-io/chaosblade-operator/exec/model"
2424
"github.com/chaosblade-io/chaosblade-spec-go/log"
25+
"github.com/chaosblade-io/chaosblade/exec/middleware"
2526
"github.com/chaosblade-io/chaosblade/exec/cloud"
2627
"path"
2728

@@ -118,6 +119,8 @@ func (ec *baseExpCommandService) GetExecutor(target, actionTarget, action string
118119
func (ec *baseExpCommandService) registerSubCommands() {
119120
// register os type command
120121
ec.registerOsExpCommands()
122+
// register middleware command
123+
ec.registerMiddlewareExpCommands()
121124
// register cloud type command
122125
ec.registerCloudExpCommands()
123126
// register jvm framework commands
@@ -148,6 +151,22 @@ func (ec *baseExpCommandService) registerOsExpCommands() []*modelCommand {
148151
return osCommands
149152
}
150153

154+
155+
// registerMiddlewareExpCommands
156+
func (ec *baseExpCommandService) registerMiddlewareExpCommands() []*modelCommand {
157+
file := path.Join(util.GetYamlHome(), fmt.Sprintf("chaosblade-middleware-spec-%s.yaml", version.Ver))
158+
models, err := specutil.ParseSpecsToModel(file, middleware.NewExecutor())
159+
if err != nil {
160+
return nil
161+
}
162+
middlewareCommands := make([]*modelCommand, 0)
163+
for idx := range models.Models {
164+
model := &models.Models[idx]
165+
command := ec.registerExpCommand(model, "")
166+
middlewareCommands = append(middlewareCommands, command)
167+
}
168+
return middlewareCommands
169+
}
151170
// registerCloudExpCommands
152171
func (ec *baseExpCommandService) registerCloudExpCommands() []*modelCommand {
153172
file := path.Join(util.GetYamlHome(), fmt.Sprintf("chaosblade-cloud-spec-%s.yaml", version.Ver))
@@ -162,6 +181,7 @@ func (ec *baseExpCommandService) registerCloudExpCommands() []*modelCommand {
162181
cloudCommands = append(cloudCommands, command)
163182
}
164183
return cloudCommands
184+
165185
}
166186

167187
// registerJvmExpCommands

exec/middleware/executor.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 1999-2020 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package middleware
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"github.com/chaosblade-io/chaosblade-exec-os/exec"
23+
"github.com/chaosblade-io/chaosblade-spec-go/log"
24+
"github.com/chaosblade-io/chaosblade-spec-go/spec"
25+
"github.com/chaosblade-io/chaosblade-spec-go/util"
26+
os_exec "os/exec"
27+
"path"
28+
"syscall"
29+
)
30+
31+
type Executor struct {
32+
}
33+
34+
func NewExecutor() spec.Executor {
35+
return &Executor{}
36+
}
37+
38+
func (*Executor) Name() string {
39+
return "middleware"
40+
}
41+
42+
func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
43+
44+
if model.ActionFlags[exec.ChannelFlag.Name] == "ssh" {
45+
sshExecutor := &exec.SSHExecutor{}
46+
return sshExecutor.Exec(uid, ctx, model)
47+
}
48+
49+
var mode string
50+
var argsArray []string
51+
52+
_, isDestroy := spec.IsDestroy(ctx)
53+
if isDestroy {
54+
mode = spec.Destroy
55+
} else {
56+
mode = spec.Create
57+
}
58+
59+
argsArray = append(argsArray, mode, model.Target, model.ActionName, fmt.Sprintf("--uid=%s", uid))
60+
for k, v := range model.ActionFlags {
61+
if v == "" || k == "timeout" {
62+
continue
63+
}
64+
argsArray = append(argsArray, fmt.Sprintf("--%s=%s", k, v))
65+
}
66+
67+
chaosOsBin := path.Join(util.GetProgramPath(), "bin", spec.ChaosMiddlewareBin)
68+
command := os_exec.CommandContext(ctx, chaosOsBin, argsArray...)
69+
log.Debugf(ctx, "run command, %s %v", chaosOsBin, argsArray)
70+
71+
if model.ActionProcessHang && !isDestroy {
72+
if err := command.Start(); err != nil {
73+
sprintf := fmt.Sprintf("create experiment command start failed, %v", err)
74+
return spec.ReturnFail(spec.OsCmdExecFailed, sprintf)
75+
}
76+
command.SysProcAttr = &syscall.SysProcAttr{}
77+
return spec.ReturnSuccess(command.Process.Pid)
78+
} else {
79+
output, err := command.CombinedOutput()
80+
outMsg := string(output)
81+
log.Debugf(ctx, "Command Result, output: %v, err: %v", outMsg, err)
82+
if err != nil {
83+
return spec.ReturnFail(spec.OsCmdExecFailed, fmt.Sprintf("command exec failed, %s", err.Error()))
84+
}
85+
return spec.Decode(outMsg, nil)
86+
}
87+
}
88+
89+
func (*Executor) SetChannel(channel spec.Channel) {
90+
}

0 commit comments

Comments
 (0)