Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GO_FLAGS=-ldflags="-s -w"
UNAME := $(shell uname)

ifeq ($(BLADE_VERSION), )
BLADE_VERSION=1.0.0
BLADE_VERSION=1.1.0
endif

BUILD_TARGET=target
Expand Down
4 changes: 4 additions & 0 deletions exec/bin/burncpu/burncpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/process"
Expand Down Expand Up @@ -231,6 +232,9 @@ func runBurnCpu(ctx context.Context, cpuCount int, cpuPercent int, pidNeeded boo

// bindBurnCpu by taskset command
func bindBurnCpuByTaskset(ctx context.Context, core string, pid int) {
if !cl.IsCommandAvailable("taskset") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandTasksetNotFound].Err)
}
response := cl.Run(ctx, "taskset", fmt.Sprintf("-a -cp %s %d", core, pid))
if !response.Success {
stopBurnCpuFunc()
Expand Down
5 changes: 5 additions & 0 deletions exec/bin/burncpu/forlinux/burncpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/containerd/cgroups"
"github.com/opencontainers/runtime-spec/specs-go"

Expand Down Expand Up @@ -176,6 +177,10 @@ func runBurnCpu(ctx context.Context, cpuCount int, pidNeeded bool, processor str

// bindBurnCpu by taskset command
func bindBurnCpuByTaskset(ctx context.Context, core string, pid int) {
if !cl.IsCommandAvailable("taskset") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandTasksetNotFound].Err)
}

response := cl.Run(ctx, "taskset", fmt.Sprintf("-a -cp %s %d", core, pid))
if !response.Success {
stopBurnCpuFunc()
Expand Down
5 changes: 5 additions & 0 deletions exec/bin/burnio/burnio.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"

"github.com/chaosblade-io/chaosblade-exec-os/exec"
Expand Down Expand Up @@ -131,6 +132,10 @@ func stopBurnIO(directory string, read, write bool) {

// write burn
func burnWrite(directory, size string) {
if !cl.IsCommandAvailable("dd") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandDdNotFound].Err)
}

tmpFileForWrite := path.Join(directory, writeFile)
for {
args := fmt.Sprintf(`if=/dev/zero of=%s bs=%sM count=%d oflag=dsync`, tmpFileForWrite, size, count)
Expand Down
10 changes: 9 additions & 1 deletion exec/bin/burnmem/burnmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ var runBurnMemFunc = runBurnMem
func startBurnMem() {
ctx := context.Background()
if burnMemMode == "cache" {
if !cl.IsCommandAvailable("mount") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandMountNotFound].Err)
}

flPath := path.Join(util.GetProgramPath(), dirName)
if _, err := os.Stat(flPath); err != nil {
err = os.Mkdir(flPath, os.ModePerm)
Expand Down Expand Up @@ -213,6 +217,10 @@ func stopBurnMem() (success bool, errs string) {
if burnMemMode == "cache" {
dirPath := path.Join(util.GetProgramPath(), dirName)
if _, err := os.Stat(dirPath); err == nil {
if !cl.IsCommandAvailable("umount") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandUmountNotFound].Err)
}

response = cl.Run(ctx, "umount", dirPath)
if !response.Success {
if !strings.Contains(response.Err, "not mounted") {
Expand Down Expand Up @@ -244,7 +252,7 @@ func calculateMemSize(percent, reserve int) (int64, int64, error) {
total = int64(virtualMemory.Total)
available = int64(virtualMemory.Free)
if burnMemMode == "ram" && !includeBufferCache {
available = available + int64(virtualMemory.Buffers + virtualMemory.Cached)
available = available + int64(virtualMemory.Buffers+virtualMemory.Cached)
}
} else {
total = int64(memoryStat.Usage.Limit)
Expand Down
6 changes: 6 additions & 0 deletions exec/bin/changedns/changedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"

"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
)
Expand Down Expand Up @@ -79,6 +80,11 @@ func recoverDns(domain, ip string) {
bin.PrintOutputAndExit("nothing to do")
return
}

if !cl.IsCommandAvailable("cat") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandCatNotFound].Err)
}

response = cl.Run(ctx, "cat", fmt.Sprintf(`%s | grep -v "%s" > %s && cat %s > %s`,
hosts, dnsPair, tmpHosts, tmpHosts, hosts))
if !response.Success {
Expand Down
20 changes: 14 additions & 6 deletions exec/bin/dropnetwork/dropnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func startDropNet(sourceIp, destinationIp, sourcePort, destinationPort, stringPa
}

func handleDropSpecifyPort(sourceIp string, destinationIp string, sourcePort string, destinationPort string, stringPattern string, networkTraffic string, ctx context.Context) {
if !cl.IsCommandAvailable("iptables") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandIptablesNotFound].Err)
}

var response *spec.Response
netFlows := []string{"INPUT", "OUTPUT"}
if networkTraffic == "in" {
Expand All @@ -88,7 +92,7 @@ func handleDropSpecifyPort(sourceIp string, destinationIp string, sourcePort str
udpArgs = fmt.Sprintf("%s -d %s", udpArgs, destinationIp)
}
if sourcePort != "" {
if strings.Contains(sourcePort, ","){
if strings.Contains(sourcePort, ",") {
tcpArgs = fmt.Sprintf("%s -m multiport --sports %s", tcpArgs, sourcePort)
udpArgs = fmt.Sprintf("%s -m multiport --sports %s", udpArgs, sourcePort)
} else {
Expand All @@ -97,10 +101,10 @@ func handleDropSpecifyPort(sourceIp string, destinationIp string, sourcePort str
}
}
if destinationPort != "" {
if strings.Contains(destinationPort, ","){
if strings.Contains(destinationPort, ",") {
tcpArgs = fmt.Sprintf("%s -m multiport --dports %s", tcpArgs, destinationPort)
udpArgs = fmt.Sprintf("%s -m multiport --dports %s", udpArgs, destinationPort)
}else{
} else {
tcpArgs = fmt.Sprintf("%s --dport %s", tcpArgs, destinationPort)
udpArgs = fmt.Sprintf("%s --dport %s", udpArgs, destinationPort)
}
Expand Down Expand Up @@ -128,6 +132,10 @@ func handleDropSpecifyPort(sourceIp string, destinationIp string, sourcePort str
}

func stopDropNet(sourceIp, destinationIp, sourcePort, destinationPort, stringPattern, networkTraffic string) {
if !cl.IsCommandAvailable("iptables") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandIptablesNotFound].Err)
}

ctx := context.Background()
var response *spec.Response
netFlows := []string{"INPUT", "OUTPUT"}
Expand All @@ -149,7 +157,7 @@ func stopDropNet(sourceIp, destinationIp, sourcePort, destinationPort, stringPat
udpArgs = fmt.Sprintf("%s -d %s", udpArgs, destinationIp)
}
if sourcePort != "" {
if strings.Contains(sourcePort, ","){
if strings.Contains(sourcePort, ",") {
tcpArgs = fmt.Sprintf("%s -m multiport --sports %s", tcpArgs, sourcePort)
udpArgs = fmt.Sprintf("%s -m multiport --sports %s", udpArgs, sourcePort)
} else {
Expand All @@ -158,10 +166,10 @@ func stopDropNet(sourceIp, destinationIp, sourcePort, destinationPort, stringPat
}
}
if destinationPort != "" {
if strings.Contains(destinationPort, ","){
if strings.Contains(destinationPort, ",") {
tcpArgs = fmt.Sprintf("%s -m multiport --dports %s", tcpArgs, destinationPort)
udpArgs = fmt.Sprintf("%s -m multiport --dports %s", udpArgs, destinationPort)
}else{
} else {
tcpArgs = fmt.Sprintf("%s --dport %s", tcpArgs, destinationPort)
udpArgs = fmt.Sprintf("%s --dport %s", udpArgs, destinationPort)
}
Expand Down
22 changes: 11 additions & 11 deletions exec/bin/dropnetwork/dropnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func Test_startDropNet_failed(t *testing.T) {
exitCode = code
}
tests := []struct {
sourceIp string
destinationIp string
sourcePort string
sourceIp string
destinationIp string
sourcePort string
destinationPort string
stringPattern string
networkTraffic string
stringPattern string
networkTraffic string
}{
{"", "", "", "", "", ""},
}
Expand All @@ -52,13 +52,13 @@ func Test_startDropNet_failed(t *testing.T) {

func Test_handleDropSpecifyPort(t *testing.T) {
type input struct {
sourceIp string
destinationIp string
sourcePort string
sourceIp string
destinationIp string
sourcePort string
destinationPort string
stringPattern string
networkTraffic string
response *spec.Response
stringPattern string
networkTraffic string
response *spec.Response
}
type expect struct {
exitCode int
Expand Down
3 changes: 3 additions & 0 deletions exec/bin/file/chmodfile/chmodfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func stopChmodFile(filepath string) {
}

func clearTempFile(filepath string, response *spec.Response, ctx context.Context) (*spec.Response, bool) {
if !cl.IsCommandAvailable("cat") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandCatNotFound].Err)
}

response = cl.Run(ctx, "cat", fmt.Sprintf(`"%s"| grep -v %s:`, tmpFileChmod, filepath))
if !response.Success {
Expand Down
5 changes: 5 additions & 0 deletions exec/bin/filldisk/filldisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ func fillDiskByFallocate(ctx context.Context, size string, dataFile string) *spe
}

func fillDiskByDD(ctx context.Context, dataFile string, directory string, size string) *spec.Response {
if !cl.IsCommandAvailable("dd") {
return spec.ResponseFailWaitResult(spec.CommandDdNotFound, spec.ResponseErr[spec.CommandDdNotFound].Err,
spec.ResponseErr[spec.CommandDdNotFound].ErrInfo)
}

// Because of filling disk slowly using dd, so execute dd with 1b size first to test the command.
response := cl.Run(ctx, "dd", fmt.Sprintf(`if=/dev/zero of=%s bs=1b count=1 iflag=fullblock`, dataFile))
if !response.Success {
Expand Down
14 changes: 7 additions & 7 deletions exec/bin/kernel/delay/delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (

var (
straceDelayStart, straceDelayStop, straceDelayNohup bool
debug bool
pidList string
time string
syscallName string
delayLoc string
first, end, step string
debug bool
pidList string
time string
syscallName string
delayLoc string
first, end, step string
)

var straceDelayBin = exec.StraceDelayBin
Expand Down Expand Up @@ -108,7 +108,7 @@ func startDelay() {
path.Join(util.GetProgramPath(), straceDelayBin), pidList, time, syscallName, delayLoc)

if first != "" {
args = fmt.Sprintf("%s --first %s", args, first)
args = fmt.Sprintf("%s --first %s", args, first)
}
if end != "" {
args = fmt.Sprintf("%s --end %s", args, end)
Expand Down
9 changes: 4 additions & 5 deletions exec/bin/kernel/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

var (
straceErrorStart, straceErrorStop, straceErrorNohup bool
pidList string
syscallName string
returnValue string
first, end, step string
pidList string
syscallName string
returnValue string
first, end, step string
)

var straceErrorBin = exec.StraceErrorBin
Expand Down Expand Up @@ -127,4 +127,3 @@ func errorNohup() {
return
}
}

2 changes: 1 addition & 1 deletion exec/bin/killprocess/killprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func killProcess(process, processCmd, localPorts, signal, excludeProcess string,
}
killProcessName = processCmd
} else if localPorts != "" {
ports, err := util.ParseIntegerListToStringSlice(localPorts)
ports, err := util.ParseIntegerListToStringSlice("local-port", localPorts)
if err != nil {
bin.PrintErrAndExit(err.Error())
}
Expand Down
11 changes: 7 additions & 4 deletions exec/bin/tcnetwork/tcnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func main() {
bin.PrintErrAndExit("less --interface flag")
}

if !cl.IsCommandAvailable("tc") {
bin.PrintErrAndExit(spec.ResponseErr[spec.CommandTcNotFound].Err)
}

if tcNetStart {
var classRule string
switch actionType {
Expand Down Expand Up @@ -381,11 +385,10 @@ func stopNet(netInterface string) {

// getPeerPorts returns all ports communicating with the port
func getPeerPorts(port string) ([]string, error) {
response := cl.Run(context.TODO(), "command", "-v ss")
if !response.Success {
return nil, fmt.Errorf("ss command not found")
if !cl.IsCommandAvailable("ss") {
return nil, fmt.Errorf(spec.ResponseErr[spec.CommandSsNotFound].Err)
}
response = cl.Run(context.TODO(), "ss", fmt.Sprintf("-n sport = %s or dport = %s", port, port))
response := cl.Run(context.TODO(), "ss", fmt.Sprintf("-n sport = %s or dport = %s", port, port))
if !response.Success {
return nil, fmt.Errorf(response.Err)
}
Expand Down
36 changes: 21 additions & 15 deletions exec/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ func (ce *cpuExecutor) Exec(uid string, ctx context.Context, model *spec.ExpMode
var err error
cpuPercent, err = strconv.Atoi(cpuPercentStr)
if err != nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
"--cpu-percent value must be a positive integer")
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: cpu-percent is illegal, it must be a prositive integer", cpuPercentStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "cpu-percent"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "cpu-percent"))
}
if cpuPercent > 100 || cpuPercent < 0 {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
"--cpu-percent value must be a prositive integer and not bigger than 100")
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: cpu-list is illegal, it must be a prositive integer and not bigger than 100", cpuPercentStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "cpu-percent"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "cpu-percent"))
}
} else {
cpuPercent = 100
Expand All @@ -178,13 +180,14 @@ func (ce *cpuExecutor) Exec(uid string, ctx context.Context, model *spec.ExpMode
cpuListStr := model.ActionFlags["cpu-list"]
if cpuListStr != "" {
if !channel.NewLocalChannel().IsCommandAvailable("taskset") {
return spec.ReturnFail(spec.Code[spec.EnvironmentError],
"taskset command not exist")
return spec.ResponseFailWaitResult(spec.CommandTasksetNotFound, spec.ResponseErr[spec.CommandTasksetNotFound].Err,
spec.ResponseErr[spec.CommandTasksetNotFound].ErrInfo)
}
cores, err := util.ParseIntegerListToStringSlice(cpuListStr)
cores, err := util.ParseIntegerListToStringSlice("cpu-list", cpuListStr)
if err != nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
fmt.Sprintf("parse %s flag err, %v", "cpu-list", err))
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: cpu-list is illegal", cpuListStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "cpu-list"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "cpu-list"))
}
cpuList = strings.Join(cores, ",")
} else {
Expand All @@ -194,8 +197,9 @@ func (ce *cpuExecutor) Exec(uid string, ctx context.Context, model *spec.ExpMode
if cpuCountStr != "" {
cpuCount, err = strconv.Atoi(cpuCountStr)
if err != nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
"--cpu-count value must be a positive integer")
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: cpu-count is illegal, cpu-count value must be a positive integer", cpuCountStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "cpu-count"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "cpu-count"))
}
}
if cpuCount <= 0 || int(cpuCount) > runtime.NumCPU() {
Expand All @@ -208,12 +212,14 @@ func (ce *cpuExecutor) Exec(uid string, ctx context.Context, model *spec.ExpMode
var err error
climbTime, err = strconv.Atoi(climbTimeStr)
if err != nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
"--climb-time value must be a positive integer")
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: climb-time is illegal, climb-time value must be a positive integer", climbTimeStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "climb-time"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "climb-time"))
}
if climbTime > 600 || climbTime < 0 {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
"--climb-time value must be a prositive integer and not bigger than 600")
util.Errorf(uid, util.GetRunFuncName(), fmt.Sprintf("`%s`: climb-time is illegal, climb-time value must be a prositive integer and not bigger than 600", climbTimeStr))
return spec.ResponseFailWaitResult(spec.ParameterIllegal, fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].Err, "climb-time"),
fmt.Sprintf(spec.ResponseErr[spec.ParameterIllegal].ErrInfo, "climb-time"))
}
}

Expand Down
Loading