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

1.6.0 dev #703

Merged
merged 8 commits into from
Apr 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
35 changes: 13 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build clean

export BLADE_VERSION=1.5.0
export BLADE_VERSION=1.6.0

ALLOWGITVERSION=1.8.5
GITVERSION:=$(shell git --version | grep ^git | sed 's/^.* //g')
Expand Down Expand Up @@ -31,8 +31,9 @@ BUILD_TARGET_FOR_JAVA_CPLUS=build-target
BUILD_TARGET_DIR_NAME=chaosblade-$(BLADE_VERSION)
BUILD_TARGET_PKG_DIR=$(BUILD_TARGET)/chaosblade-$(BLADE_VERSION)
BUILD_TARGET_PKG_NAME=$(BUILD_TARGET)/chaosblade-$(BLADE_VERSION).tar.gz
BUILD_TARGET_BIN=$(BUILD_TARGET_PKG_DIR)/bin

BUILD_TARGET_LIB=$(BUILD_TARGET_PKG_DIR)/lib
BUILD_TARGET_BIN=$(BUILD_TARGET_PKG_DIR)/bin
BUILD_TARGET_YAML=$(BUILD_TARGET_PKG_DIR)/yaml
BUILD_TARGET_TAR_NAME=$(BUILD_TARGET_DIR_NAME).tar.gz
BUILD_TARGET_PKG_FILE_PATH=$(BUILD_TARGET)/$(BUILD_TARGET_TAR_NAME)
Expand All @@ -43,23 +44,23 @@ BUILD_TARGET_CACHE=$(BUILD_TARGET)/cache

# chaosblade-exec-os
BLADE_EXEC_OS_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-os.git
BLADE_EXEC_OS_BRANCH=v1.5.0
BLADE_EXEC_OS_BRANCH=1.6.0-dev

# chaosblade-exec-docker
BLADE_EXEC_DOCKER_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-docker.git
BLADE_EXEC_DOCKER_BRANCH=v1.5.0

# chaosblade-exec-cri
BLADE_EXEC_CRI_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cri.git
BLADE_EXEC_CRI_BRANCH=v1.5.0
BLADE_EXEC_CRI_BRANCH=1.6.0-dev

# chaosblade-exec-kubernetes
BLADE_OPERATOR_PROJECT=https://github.com/chaosblade-io/chaosblade-operator.git
BLADE_OPERATOR_BRANCH=v1.5.0
BLADE_OPERATOR_BRANCH=1.6.0-dev

# chaosblade-exec-jvm
BLADE_EXEC_JVM_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-jvm.git
BLADE_EXEC_JVM_BRANCH=v1.5.0
BLADE_EXEC_JVM_BRANCH=1.6.0-dev

# chaosblade-exec-cplus
BLADE_EXEC_CPLUS_PROJECT=https://github.com/chaosblade-io/chaosblade-exec-cplus.git
Expand Down Expand Up @@ -90,9 +91,9 @@ 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 os docker cri cplus java kubernetes upx package check_yaml ## Build all scenarios
build: pre_build cli nsexec os cri cplus java kubernetes upx package check_yaml ## Build all scenarios

# for example: make build_with cli os_darwin
# for example: make build_with cli
build_with: pre_build ## Select scenario build, for example `make build_with cli os docker cri kubernetes java cplus`

# for example: make build_with_linux cli os
Expand All @@ -107,7 +108,7 @@ build_linux: ## Build linux version of all scenarios by docker image
build_linux_arm: ## Build linux arm version of all scenarios by docker image
make build_with_linux_arm ARGS="cli os docker cri kubernetes java cplus check_yaml" upx package

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

##@ Build sub

Expand All @@ -121,6 +122,9 @@ pre_build: mkdir_build_target ## Mkdir build target
cli: ## Build blade cli
$(GO) build $(GO_FLAGS) -o $(BUILD_TARGET_PKG_DIR)/blade ./cli

nsexec: ## Build nsexec
/usr/local/musl/bin/musl-gcc -static nsexec.c -o $(BUILD_TARGET_PKG_DIR)/bin/nsexec

os: ## Build basic resource experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-os, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-os))
git clone -b $(BLADE_EXEC_OS_BRANCH) $(BLADE_EXEC_OS_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-os
Expand All @@ -134,19 +138,6 @@ 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)

os_darwin: ## Build basic resource experimental scenarios for darwin.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-os, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-os))
git clone -b $(BLADE_EXEC_OS_BRANCH) $(BLADE_EXEC_OS_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-os
else
ifdef ALERTMSG
$(error $(ALERTMSG))
endif
git -C $(BUILD_TARGET_CACHE)/chaosblade-exec-os pull origin $(BLADE_EXEC_OS_BRANCH)
endif
make build_darwin -C $(BUILD_TARGET_CACHE)/chaosblade-exec-os
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)

docker: ## Build docker experimental scenarios.
ifneq ($(BUILD_TARGET_CACHE)/chaosblade-exec-docker, $(wildcard $(BUILD_TARGET_CACHE)/chaosblade-exec-docker))
git clone -b $(BLADE_EXEC_DOCKER_BRANCH) $(BLADE_EXEC_DOCKER_PROJECT) $(BUILD_TARGET_CACHE)/chaosblade-exec-docker
Expand Down
15 changes: 7 additions & 8 deletions build/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/sirupsen/logrus"
)

var version = "0.7.1"
Expand Down Expand Up @@ -55,7 +54,7 @@ func main() {

file, err := os.OpenFile(chaosSpecFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
if err != nil {
logrus.Fatalf("open %s file err, %s", chaosSpecFile, err.Error())
log.Fatalf("open %s file err, %s", chaosSpecFile, err.Error())
}
defer file.Close()
util.MarshalModelSpec(models, file)
Expand All @@ -64,46 +63,46 @@ func main() {
func getOsModels(osSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(osSpecFile, nil)
if err != nil {
logrus.Fatalf("parse os spec failed, %s", err)
log.Fatalf("parse os spec failed, %s", err)
}
return models
}
func getJvmModels(jvmSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(jvmSpecFile, nil)
if err != nil {
logrus.Fatalf("parse java spec failed, %s", err)
log.Fatalf("parse java spec failed, %s", err)
}
return models
}

func getCplusModels(cplusSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(cplusSpecFile, nil)
if err != nil {
logrus.Fatalf("parse cplus spec failed, %s", err)
log.Fatalf("parse cplus spec failed, %s", err)
}
return models
}

func getDockerModels(dockerSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(dockerSpecFile, nil)
if err != nil {
logrus.Fatalf("parse docker spec failed, %s", err)
log.Fatalf("parse docker spec failed, %s", err)
}
return models
}

func getCriModels(criSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(criSpecFile, nil)
if err != nil {
logrus.Fatalf("parse cri spec failed, %s", err)
log.Fatalf("parse cri spec failed, %s", err)
}
return models
}

func getKubernetesModels(k8sSpecFile string) *spec.Models {
models, err := util.ParseSpecsToModel(k8sSpecFile, nil)
if err != nil {
logrus.Fatalf("parse kubernetes spec failed, %s", err)
log.Fatalf("parse kubernetes spec failed, %s", err)
}
return models
}
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/check_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (doc *CheckOsCommand) execOperatorCmd(checkExecCmd *CheckExecCmd) {
execResult.result = "failed"
continue
}
if !ch.IsCommandAvailable(operatorCmd) {
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"
Expand Down Expand Up @@ -384,7 +384,7 @@ func (doc *CheckOsCommand) actionRunEFunc(target, scope string, actionCommand *a
successCmd := ""
checkStr := fmt.Sprintf("%s %s", target, expModel.ActionName)
for _, program := range programs {
if channel.NewLocalChannel().IsCommandAvailable(program) {
if channel.NewLocalChannel().IsCommandAvailable(context.TODO(), program) {
if successCmd == "" {
successCmd = program
} else {
Expand Down
62 changes: 45 additions & 17 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/shirou/gopsutil/process"
"os/exec"
"path"
"strconv"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/pflag"

"github.com/chaosblade-io/chaosblade/data"
Expand Down Expand Up @@ -103,6 +104,7 @@ func (cc *CreateCommand) bindFlagsFunction() func(commandFlags map[string]func()
func (cc *CreateCommand) actionRunEFunc(target, scope string, actionCommand *actionCommand, actionCommandSpec spec.ExpActionCommandSpec) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
expModel := createExpModel(target, scope, actionCommandSpec.Name(), cmd)
expModel.ActionProcessHang = actionCommandSpec.ProcessHang()
// check timeout flag
tt := expModel.ActionFlags["timeout"]
if tt != "" {
Expand All @@ -118,12 +120,16 @@ func (cc *CreateCommand) actionRunEFunc(target, scope string, actionCommand *act
var model *data.ExperimentModel
var resp *spec.Response
var err error
ctx := context.Background()

if nohup {
uid := expModel.ActionFlags[UidFlag]
if uid == "" {
logrus.Infof("can not execute nohup, uid is null")
ctx := context.Background()
log.Infof(ctx, "can not execute nohup, uid is null")
return spec.ResponseFailWithFlags(spec.ParameterLess, UidFlag)
} else {
ctx = context.WithValue(context.Background(), spec.Uid, uid)
model, err = GetDS().QueryExperimentModelByUid(uid)
if err == nil {
delete(expModel.ActionFlags, NohupFlag)
Expand Down Expand Up @@ -161,24 +167,24 @@ func (cc *CreateCommand) actionRunEFunc(target, scope string, actionCommand *act
args = fmt.Sprintf("%s %s %s", path.Join(util.GetProgramPath(), "blade"), args, "> /dev/null 2>&1 &")
response := channel.NewLocalChannel().Run(context.Background(), "nohup", args)
if response.Success {
logrus.Infof("async create success, uid: %s", model.Uid)
log.Infof(ctx, "async create success, uid: %s", model.Uid)
cmd.Println(spec.ReturnSuccess(model.Uid).Print())
} else {
logrus.Warningf("async create fail, err: %s, uid: %s", response.Err, model.Uid)
log.Warnf(ctx, "async create fail, err: %s, uid: %s", response.Err, model.Uid)
cmd.Println(spec.ResponseFailWithFlags(spec.OsCmdExecFailed, "nohup", response.Err).Print())
}
return nil
} else {
// execute experiment
executor := actionCommandSpec.Executor()
executor.SetChannel(channel.NewLocalChannel())
response := executor.Exec(model.Uid, context.Background(), expModel)
response.Result = model.Uid
ctx := context.WithValue(context.Background(), spec.Uid, model.Uid)
response := executor.Exec(model.Uid, ctx, expModel)
if response.Code == spec.ReturnOKDirectly.Code {
// return directly
response.Code = spec.OK.Code
cmd.Println(response.Print())
endpointCallBack(endpoint, model.Uid, response)
endpointCallBack(ctx, endpoint, model.Uid, response)
}
// pass the uid, expModel to actionCommand
actionCommand.expModel = expModel
Expand All @@ -187,34 +193,56 @@ func (cc *CreateCommand) actionRunEFunc(target, scope string, actionCommand *act
if !response.Success {
// update status
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Error, response.Err))
endpointCallBack(endpoint, model.Uid, response)
endpointCallBack(ctx, endpoint, model.Uid, response)
return response
}
// update status
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Success, response.Err))

if expModel.ActionProcessHang && scope != "pod" && scope != "container" {
// todo -> need to find a better way to query the status
time.Sleep(time.Millisecond * 100)
log.Debugf(ctx, "result: %v", response.Result)
if response.Result == nil {
errMsg := fmt.Sprintf("chaos_os process not found, please check chaosblade log")
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Error, errMsg))
response.Err = errMsg
} else {
_, err := process.NewProcess(int32(response.Result.(int)))
if err != nil {
errMsg := fmt.Sprintf("chaos_os process not found, please check chaosblade log, err: %s", err.Error())
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Error, errMsg))
response.Err = errMsg
} else {
// update status
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Success, response.Err))
}
}
} else {
// update status
checkError(GetDS().UpdateExperimentModelByUid(model.Uid, Success, response.Err))
}
response.Result = model.Uid
cmd.Println(response.Print())
endpointCallBack(endpoint, model.Uid, response)
endpointCallBack(ctx, endpoint, model.Uid, response)
return nil
}
}
}

func endpointCallBack(endpoint, uid string, response *spec.Response) {
func endpointCallBack(ctx context.Context, endpoint, uid string, response *spec.Response) {
if endpoint != "" {
logrus.Infof("report response: %s to endpoint: %s", response.Print(), endpoint)
log.Infof(ctx, "report response: %s to endpoint: %s", response.Print(), endpoint)
experimentModel, _ := GetDS().QueryExperimentModelByUid(uid)
body, err := json.Marshal(experimentModel)
if err != nil {
logrus.Warningf("create post body %s failed, %v", response.Print(), err)
log.Warnf(ctx, "create post body %s failed, %v", response.Print(), err)
} else {
result, err, code := util.PostCurl(endpoint, body, "application/json")
if err != nil {
logrus.Warningf("report result %s failed, %v", response.Print(), err)
log.Warnf(ctx, "report result %s failed, %v", response.Print(), err)
} else if code != 200 {
logrus.Warningf("response code is %d, result %s", code, result)
log.Warnf(ctx, "response code is %d, result %s", code, result)
} else {
logrus.Infof("report result success, result %s", result)
log.Infof(ctx, "report result success, result %s", result)
}
}
}
Expand Down
Loading