diff --git a/agent-go/executor/BasicFunction.go b/agent-go/executor/BasicFunction.go index 3363144..22d9a94 100644 --- a/agent-go/executor/BasicFunction.go +++ b/agent-go/executor/BasicFunction.go @@ -93,18 +93,33 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) { serviceName = serviceName + ".service" } + systemdFilePath := []string{ + "/lib/systemd/system/", + "/etc/systemd/system", + } + // 检查是否存在 - execute := PureResultSingleExecute( - []string{ - "systemctl", - "status", - "-q", - serviceName, - }) - if !execute { + var existService bool + + for _, s := range systemdFilePath { + resultOk, _ := PipelineCommandExecutor( + [][]string{ + {"ls", + s, + }, + { + "grep", + serviceName, + }, + }) + if resultOk { + existService = true + } + } + if !existService { return true, []string{ serviceName, - "该服务器不存在!", + "该服务不存在!", } } @@ -115,6 +130,10 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) { "stop", serviceName, }, + { + "sleep", + "1", + }, { "systemctl", "disable", @@ -127,6 +146,65 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) { return resultOK, resultLog } +func BasicSystemdUp(serviceName string) (ok bool, resultLog []string) { + + if !strings.HasSuffix(serviceName, ".service") { + serviceName = serviceName + ".service" + } + + systemdFilePath := []string{ + "/lib/systemd/system/", + "/etc/systemd/system", + } + + // 检查是否存在 + var existService bool + + for _, s := range systemdFilePath { + resultOk, _ := PipelineCommandExecutor( + [][]string{ + {"ls", + s, + }, + { + "grep", + serviceName, + }, + }) + if resultOk { + existService = true + } + } + if !existService { + return true, []string{ + serviceName, + "该服务不存在!", + } + } + + // 关闭 + var shutDownCommand = [][]string{ + { + "systemctl", + "start", + serviceName, + }, + { + "sleep", + "1", + }, + { + "systemctl", + "enable", + serviceName, + }, + } + + resultOK := PureResultSingleExecuteBatch(shutDownCommand) + + return resultOK, resultLog +} + func BasicTransPipelineCommand(pipelineString string) (pipelineCommand [][]string) { var pipelineCommandC [][]string diff --git a/agent-go/executor/BasicFunction_test.go b/agent-go/executor/BasicFunction_test.go index 3e6a77a..8ffa992 100644 --- a/agent-go/executor/BasicFunction_test.go +++ b/agent-go/executor/BasicFunction_test.go @@ -47,3 +47,13 @@ func TestBasicSystemdShutdown(t *testing.T) { t.Logf("result ok is %v resultLog is %v", ok, resultLog) } + +func TestBasicSystemdUp(t *testing.T) { + + //ok, resultLog := BasicSystemdUp("docker.service") + //ok, resultLog := BasicSystemdUp("docker") + ok, resultLog := BasicSystemdUp("fwd") + + t.Logf("result ok is %v resultLog is %v", ok, resultLog) + +} diff --git a/agent-go/executor/FunctionalExecutor_test.go b/agent-go/executor/FunctionalExecutor_test.go index b331f90..69652f2 100644 --- a/agent-go/executor/FunctionalExecutor_test.go +++ b/agent-go/executor/FunctionalExecutor_test.go @@ -26,6 +26,14 @@ var shutdownFirewalld = []string{ "systemctl", "stop", "firewalld", "&&", "systemctl", "disable", "firewalld", } +var wgetCommand = []string{ + "wget", + "--timeout=10", + "https://oss-s1.107421.xyz/octopus_ssh_banner", + "-O", + "/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123", +} + var pipelineCommandFalse = []string{ "ls", "/etc", @@ -46,8 +54,9 @@ var pipelineCommand = [][]string{ } var pipelineCommandMore = "apt-cache madison docker-ce | awk {print$3} | cut -d: -f2" +var pipelineCommandSecond = "systemctl status -q docker.service | grep found." var pipelineCommandThird = "ls /etc/ | grep passwd | head -2" -var pipelineCommandSecond = "systemctl status docker.service | grep active | wc -w" +var pipelineCommandFourth = "echo dsadsad | tee /home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123" var ifconfigCommand = []string{"ifconfig"} @@ -58,7 +67,7 @@ func TestReadTimeOutput(t *testing.T) { } func TestAllCommandExecutor(t *testing.T) { - ok, result := AllCommandExecutor(pipelineCommandFalse) + ok, result := AllCommandExecutor(wgetCommand) t.Logf("执行结果为 => %#v", ok) t.Logf("执行日志为 => %#v", result) @@ -108,7 +117,7 @@ func TestPipelineCommandExecutorSingle(t *testing.T) { func TestPipelineCommandExecutor(t *testing.T) { //PipelineCommandExecutor(pipelineCommand) - pipelineStringToCommand := BasicTransPipelineCommand(pipelineCommandMore) + pipelineStringToCommand := BasicTransPipelineCommand(pipelineCommandSecond) t.Logf("pipelineCommmand are => %#v", pipelineStringToCommand) diff --git a/agent-go/executor/script/123 b/agent-go/executor/script/123 index 61e9bd8..2b06cfa 100644 --- a/agent-go/executor/script/123 +++ b/agent-go/executor/script/123 @@ -1,3 +1,25 @@ -passwd -passwd1 \ No newline at end of file + ______ __ ______ __ + / \ | \ / \ | \ +| $$$$$$\ _______ _| $$_ ______ ______ __ __ _______ | $$$$$$\ ______ ______ _______ _| $$_ +| $$ | $$/ | $$ \ / \ / \| \ | \/ \ | $$__| $$/ \ / \| | $$ \ +| $$ | $| $$$$$$$\$$$$$$ | $$$$$$| $$$$$$| $$ | $| $$$$$$$ | $$ $| $$$$$$| $$$$$$| $$$$$$$\$$$$$$ +| $$ | $| $$ | $$ __| $$ | $| $$ | $| $$ | $$\$$ \ | $$$$$$$| $$ | $| $$ $| $$ | $$| $$ __ +| $$__/ $| $$_____ | $$| | $$__/ $| $$__/ $| $$__/ $$_\$$$$$$\ | $$ | $| $$__| $| $$$$$$$| $$ | $$| $$| \ + \$$ $$\$$ \ \$$ $$\$$ $| $$ $$\$$ $| $$ | $$ | $$\$$ $$\$$ | $$ | $$ \$$ $$ + \$$$$$$ \$$$$$$$ \$$$$ \$$$$$$| $$$$$$$ \$$$$$$ \$$$$$$$ \$$ \$$_\$$$$$$$ \$$$$$$$\$$ \$$ \$$$$ + | $$ | \__| $$ + | $$ \$$ $$ + \$$ \$$$$$$ + __ __ + | \ | \ + ____| $$ ______ ____| $$ ______ __ __ __ ______ _______ ______ + ______ ______ ______ / $$| \ / $$| \ | \ | \ | \| \| \ / \ +| | | \ | $$$$$$$ \$$$$$$| $$$$$$$ \$$$$$$\ | $$ | $$ | $$ \$$$$$$| $$$$$$$| $$$$$$\ + \$$$$$$\$$$$$$\$$$$$$ | $$ | $$/ $| $$ | $$/ $$ | $$ | $$ | $$/ $| $$ | $| $$ | $$ + | $$__| $| $$$$$$| $$__| $| $$$$$$$ | $$_/ $$_/ $| $$$$$$| $$ | $| $$__| $$ + \$$ $$\$$ $$\$$ $$\$$ $$ \$$ $$ $$\$$ $| $$ | $$\$$ $$ + \$$$$$$$ \$$$$$$$ \$$$$$$$ \$$$$$$$ \$$$$$\$$$$ \$$$$$$$\$$ \$$_\$$$$$$$ + | \__| $$ + \$$ $$ + \$$$$$$