Skip to content

Commit

Permalink
feature:add exclude-process flag in killing process experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
lvchazaizai committed Jun 16, 2020
1 parent 62057ad commit 33c0a6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions exec/bin/killprocess/killprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
)

var killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal string
var killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal, killProcessExcludeProcess string
var killProcessCount int

func main() {
Expand All @@ -37,17 +37,19 @@ func main() {
flag.IntVar(&killProcessCount, "count", 0, "limit count")
flag.StringVar(&killProcessLocalPorts, "local-port", "", "local service ports")
flag.StringVar(&killProcessSignal, "signal", "9", "kill process signal")
flag.StringVar(&killProcessExcludeProcess, "exclude-process", "", "kill process exclude specific process")
bin.ParseFlagAndInitLog()

killProcess(killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal, killProcessCount)
killProcess(killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal, killProcessExcludeProcess, killProcessCount)
}

var cl = channel.NewLocalChannel()

func killProcess(process, processCmd, localPorts, signal string, count int) {
func killProcess(process, processCmd, localPorts, signal, excludeProcess string, count int) {
var pids []string
var err error
var ctx = context.WithValue(context.Background(), channel.ExcludeProcessKey, "blade")
var excludeProcessValue = fmt.Sprintf("blade,%s", excludeProcess)
var ctx = context.WithValue(context.Background(), channel.ExcludeProcessKey, excludeProcessValue)
if process != "" {
pids, err = cl.GetPidsByProcessName(process, ctx)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions exec/process_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func NewKillProcessActionCommandSpec() spec.ExpActionCommandSpec {
Name: "signal",
Desc: "Killing process signal, such as 9,15",
},
&spec.ExpFlag{
Name: "exclude-process",
Desc: "Exclude process",
},
},
ActionFlags: []spec.ExpFlagSpec{},
ActionExecutor: &KillProcessExecutor{},
Expand Down Expand Up @@ -103,6 +107,7 @@ func (kpe *KillProcessExecutor) Exec(uid string, ctx context.Context, model *spe
processCmd := model.ActionFlags["process-cmd"]
localPorts := model.ActionFlags["local-port"]
signal := model.ActionFlags["signal"]
excludeProcess := model.ActionFlags["exclude-process"]
if process == "" && processCmd == "" && localPorts == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "less process matcher")
}
Expand All @@ -124,6 +129,9 @@ func (kpe *KillProcessExecutor) Exec(uid string, ctx context.Context, model *spe
if signal != "" {
flags = fmt.Sprintf(`%s --signal %s`, flags, signal)
}
if excludeProcess != "" {
flags = fmt.Sprintf(`%s --exclude-process %s`, flags, excludeProcess)
}
return kpe.channel.Run(ctx, path.Join(kpe.channel.GetScriptPath(), killProcessBin), flags)
}

Expand Down

0 comments on commit 33c0a6b

Please sign in to comment.