Skip to content

Commit

Permalink
feature: Separate cli and exec-os
Browse files Browse the repository at this point in the history
Signed-off-by: tiny.x <[email protected]>
  • Loading branch information
tiny-x committed May 31, 2021
1 parent 2c8992f commit da6b8cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build clean

export BLADE_VERSION=1.1.0
export BLADE_VERSION=1.2.0

ALLOWGITVERSION=1.8.5
GITVERSION:=$(shell git --version | grep ^git | sed 's/^.* //g')
Expand Down
46 changes: 28 additions & 18 deletions exec/os/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,53 @@ package os
import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-exec-os/exec"
"github.com/chaosblade-io/chaosblade-exec-os/exec/model"
"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"path"
)

type Executor struct {
executors map[string]spec.Executor
sshExecutor spec.Executor
}

func NewExecutor() spec.Executor {
return &Executor{
executors: model.GetAllOsExecutors(),
sshExecutor: model.GetSHHExecutor(),
}
return &Executor{}
}

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

var c = channel.NewLocalChannel()

const (
OS_BIN = "chaos_os"
CREATE = "create"
DESTROY = "destroy"
)

func (e *Executor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
if model.ActionFlags[exec.ChannelFlag.Name] == e.sshExecutor.Name() {
return e.sshExecutor.Exec(uid, ctx, model)
var args string
var flags string
for k, v := range model.ActionFlags {
if v == "" {
continue
}
flags = fmt.Sprintf("%s %s=%s", flags, k, v)
}

if _, ok := spec.IsDestroy(ctx); ok {
args = fmt.Sprintf("%s %s %s%s uid=%s", DESTROY, model.Target, model.ActionName, flags, uid)
} else {
args = fmt.Sprintf("%s %s %s%s uid=%s", CREATE, model.Target, model.ActionName, flags, uid)
}

key := model.Target + model.ActionName
executor := e.executors[key]
if executor == nil {
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf(spec.ResponseErr[spec.OsExecutorNotFound].ErrInfo, key))
return spec.ResponseFailWaitResult(spec.OsExecutorNotFound, fmt.Sprintf(spec.ResponseErr[spec.OsExecutorNotFound].Err, uid),
fmt.Sprintf(spec.ResponseErr[spec.OsExecutorNotFound].ErrInfo, key))
response := c.Run(ctx, path.Join(util.GetBinPath(), OS_BIN), args)
if response.Success {
return spec.Decode(response.Result.(string), response)
} else {
return response
}
executor.SetChannel(channel.NewLocalChannel())
return executor.Exec(uid, ctx, model)
}

func (*Executor) SetChannel(channel spec.Channel) {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/chaosblade-io/chaosblade
go 1.13

require (
github.com/chaosblade-io/chaosblade-exec-docker v1.0.1-0.20210409014035-355048b67b24
github.com/chaosblade-io/chaosblade-exec-os v1.0.1-0.20210408072857-7eed94250576
github.com/chaosblade-io/chaosblade-operator v1.0.1-0.20210409014453-713dbeea24fe
github.com/chaosblade-io/chaosblade-spec-go v1.1.0-dev
github.com/chaosblade-io/chaosblade-exec-docker v1.2.0
github.com/chaosblade-io/chaosblade-exec-os v1.2.0
github.com/chaosblade-io/chaosblade-operator v1.2.0
github.com/chaosblade-io/chaosblade-spec-go v1.2.0
github.com/mattn/go-sqlite3 v1.10.1-0.20190217174029-ad30583d8387
github.com/olekukonko/tablewriter v0.0.5-0.20201029120751-42e21c7531a3
github.com/prometheus/common v0.9.1
Expand Down

0 comments on commit da6b8cb

Please sign in to comment.