Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add middleware executor for chaosblade-exec-middleware module #776

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _test
releases
tmp
.idea/
.vscode/

# Architecture specific extensions/prefixes
trace.out
Expand Down
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ BUILD_TARGET_CACHE=$(BUILD_TARGET)/cache
BLADE_EXEC_OS_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-os.git
BLADE_EXEC_OS_BRANCH=master


# chaosblade-exec-middleware
BLADE_EXEC_MIDDLEWARE_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-middleware.git
BLADE_EXEC_MIDDLEWARE_BRANCH=main

# chaosblade-exec-cloud
BLADE_EXEC_CLOUD_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cloud.git
BLADE_EXEC_CLOUD_BRANCH=main


# chaosblade-exec-cri
BLADE_EXEC_CRI_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cri.git
BLADE_EXEC_CRI_BRANCH=main
Expand Down Expand Up @@ -89,8 +95,8 @@ help:
@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)

##@ Build
build: pre_build cli nsexec os cloud cri cplus java kubernetes package check_yaml ## Build all scenarios
#build: pre_build cli nsexec os cloud cri cplus java kubernetes upx package check_yaml ## Build all scenarios
build: pre_build cli nsexec os cloud middleware cri cplus java kubernetes package check_yaml ## Build all scenarios
#build: pre_build cli nsexec os cloud middleware cri cplus java kubernetes upx package check_yaml ## Build all scenarios

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

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

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

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

##@ Build sub

Expand Down Expand Up @@ -137,6 +143,19 @@ endif
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-os/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-os/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)

middleware: ## Build middleware experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-middleware, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware))
git clone -b $(BLADE_EXEC_MIDDLEWARE_BRANCH) $(BLADE_EXEC_MIDDLEWARE_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware
else
ifdef ALERTMSG
$(error $(ALERTMSG))
endif
git -C $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware pull origin $(BLADE_EXEC_MIDDLEWARE_BRANCH)
endif
make -C $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-middleware/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)

cloud: ## Build cloud experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-cloud, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud))
git clone -b $(BLADE_EXEC_CLOUD_BRANCH) $(BLADE_EXEC_CLOUD_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud
Expand All @@ -150,6 +169,7 @@ endif
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud/$(BUILD_TARGET_BIN)/* $(BUILD_TARGET_BIN)
cp $(BUILD_TARGET_CACHE)/chaosblade-exec-cloud/$(BUILD_TARGET_YAML)/* $(BUILD_TARGET_YAML)


kubernetes: ## Build kubernetes experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-operator, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-operator))
git clone -b $(BLADE_OPERATOR_BRANCH) $(BLADE_OPERATOR_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-operator
Expand Down
20 changes: 20 additions & 0 deletions cli/cmd/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/chaosblade-io/chaosblade-exec-cri/exec"
"github.com/chaosblade-io/chaosblade-operator/exec/model"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade/exec/middleware"
"github.com/chaosblade-io/chaosblade/exec/cloud"
"path"

Expand Down Expand Up @@ -118,6 +119,8 @@ func (ec *baseExpCommandService) GetExecutor(target, actionTarget, action string
func (ec *baseExpCommandService) registerSubCommands() {
// register os type command
ec.registerOsExpCommands()
// register middleware command
ec.registerMiddlewareExpCommands()
// register cloud type command
ec.registerCloudExpCommands()
// register jvm framework commands
Expand Down Expand Up @@ -148,6 +151,22 @@ func (ec *baseExpCommandService) registerOsExpCommands() []*modelCommand {
return osCommands
}


// registerMiddlewareExpCommands
func (ec *baseExpCommandService) registerMiddlewareExpCommands() []*modelCommand {
file := path.Join(util.GetYamlHome(), fmt.Sprintf("chaosblade-middleware-spec-%s.yaml", version.Ver))
models, err := specutil.ParseSpecsToModel(file, middleware.NewExecutor())
if err != nil {
return nil
}
middlewareCommands := make([]*modelCommand, 0)
for idx := range models.Models {
model := &models.Models[idx]
command := ec.registerExpCommand(model, "")
middlewareCommands = append(middlewareCommands, command)
}
return middlewareCommands
}
// registerCloudExpCommands
func (ec *baseExpCommandService) registerCloudExpCommands() []*modelCommand {
file := path.Join(util.GetYamlHome(), fmt.Sprintf("chaosblade-cloud-spec-%s.yaml", version.Ver))
Expand All @@ -162,6 +181,7 @@ func (ec *baseExpCommandService) registerCloudExpCommands() []*modelCommand {
cloudCommands = append(cloudCommands, command)
}
return cloudCommands

}

// registerJvmExpCommands
Expand Down
90 changes: 90 additions & 0 deletions exec/middleware/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 middleware

import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-exec-os/exec"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
os_exec "os/exec"
"path"
"syscall"
)

type Executor struct {
}

func NewExecutor() spec.Executor {
return &Executor{}
}

func (*Executor) Name() string {
return "middleware"
}

func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {

if model.ActionFlags[exec.ChannelFlag.Name] == "ssh" {
sshExecutor := &exec.SSHExecutor{}
return sshExecutor.Exec(uid, ctx, model)
}

var mode string
var argsArray []string

_, isDestroy := spec.IsDestroy(ctx)
if isDestroy {
mode = spec.Destroy
} else {
mode = spec.Create
}

argsArray = append(argsArray, mode, model.Target, model.ActionName, fmt.Sprintf("--uid=%s", uid))
for k, v := range model.ActionFlags {
if v == "" || k == "timeout" {
continue
}
argsArray = append(argsArray, fmt.Sprintf("--%s=%s", k, v))
}

chaosOsBin := path.Join(util.GetProgramPath(), "bin", spec.ChaosMiddlewareBin)
command := os_exec.CommandContext(ctx, chaosOsBin, argsArray...)
log.Debugf(ctx, "run command, %s %v", chaosOsBin, argsArray)

if model.ActionProcessHang && !isDestroy {
if err := command.Start(); err != nil {
sprintf := fmt.Sprintf("create experiment command start failed, %v", err)
return spec.ReturnFail(spec.OsCmdExecFailed, sprintf)
}
command.SysProcAttr = &syscall.SysProcAttr{}
return spec.ReturnSuccess(command.Process.Pid)
} else {
output, err := command.CombinedOutput()
outMsg := string(output)
log.Debugf(ctx, "Command Result, output: %v, err: %v", outMsg, err)
if err != nil {
return spec.ReturnFail(spec.OsCmdExecFailed, fmt.Sprintf("command exec failed, %s", err.Error()))
}
return spec.Decode(outMsg, nil)
}
}

func (*Executor) SetChannel(channel spec.Channel) {
}