[Agent] [Executor] 新增Systemd的部分

This commit is contained in:
zeaslity
2023-10-19 15:48:04 +08:00
parent 328247eeae
commit d9c1c0fb91
4 changed files with 133 additions and 14 deletions

View File

@@ -93,18 +93,33 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) {
serviceName = serviceName + ".service" serviceName = serviceName + ".service"
} }
systemdFilePath := []string{
"/lib/systemd/system/",
"/etc/systemd/system",
}
// 检查是否存在 // 检查是否存在
execute := PureResultSingleExecute( var existService bool
[]string{
"systemctl", for _, s := range systemdFilePath {
"status", resultOk, _ := PipelineCommandExecutor(
"-q", [][]string{
serviceName, {"ls",
}) s,
if !execute { },
{
"grep",
serviceName,
},
})
if resultOk {
existService = true
}
}
if !existService {
return true, []string{ return true, []string{
serviceName, serviceName,
"该服务不存在!", "该服务不存在!",
} }
} }
@@ -115,6 +130,10 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) {
"stop", "stop",
serviceName, serviceName,
}, },
{
"sleep",
"1",
},
{ {
"systemctl", "systemctl",
"disable", "disable",
@@ -127,6 +146,65 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) {
return resultOK, resultLog 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) { func BasicTransPipelineCommand(pipelineString string) (pipelineCommand [][]string) {
var pipelineCommandC [][]string var pipelineCommandC [][]string

View File

@@ -47,3 +47,13 @@ func TestBasicSystemdShutdown(t *testing.T) {
t.Logf("result ok is %v resultLog is %v", ok, resultLog) 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)
}

View File

@@ -26,6 +26,14 @@ var shutdownFirewalld = []string{
"systemctl", "stop", "firewalld", "&&", "systemctl", "disable", "firewalld", "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{ var pipelineCommandFalse = []string{
"ls", "ls",
"/etc", "/etc",
@@ -46,8 +54,9 @@ var pipelineCommand = [][]string{
} }
var pipelineCommandMore = "apt-cache madison docker-ce | awk {print$3} | cut -d: -f2" 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 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"} var ifconfigCommand = []string{"ifconfig"}
@@ -58,7 +67,7 @@ func TestReadTimeOutput(t *testing.T) {
} }
func TestAllCommandExecutor(t *testing.T) { func TestAllCommandExecutor(t *testing.T) {
ok, result := AllCommandExecutor(pipelineCommandFalse) ok, result := AllCommandExecutor(wgetCommand)
t.Logf("执行结果为 => %#v", ok) t.Logf("执行结果为 => %#v", ok)
t.Logf("执行日志为 => %#v", result) t.Logf("执行日志为 => %#v", result)
@@ -108,7 +117,7 @@ func TestPipelineCommandExecutorSingle(t *testing.T) {
func TestPipelineCommandExecutor(t *testing.T) { func TestPipelineCommandExecutor(t *testing.T) {
//PipelineCommandExecutor(pipelineCommand) //PipelineCommandExecutor(pipelineCommand)
pipelineStringToCommand := BasicTransPipelineCommand(pipelineCommandMore) pipelineStringToCommand := BasicTransPipelineCommand(pipelineCommandSecond)
t.Logf("pipelineCommmand are => %#v", pipelineStringToCommand) t.Logf("pipelineCommmand are => %#v", pipelineStringToCommand)

View File

@@ -1,3 +1,25 @@
passwd
passwd1 ______ __ ______ __
/ \ | \ / \ | \
| $$$$$$\ _______ _| $$_ ______ ______ __ __ _______ | $$$$$$\ ______ ______ _______ _| $$_
| $$ | $$/ | $$ \ / \ / \| \ | \/ \ | $$__| $$/ \ / \| | $$ \
| $$ | $| $$$$$$$\$$$$$$ | $$$$$$| $$$$$$| $$ | $| $$$$$$$ | $$ $| $$$$$$| $$$$$$| $$$$$$$\$$$$$$
| $$ | $| $$ | $$ __| $$ | $| $$ | $| $$ | $$\$$ \ | $$$$$$$| $$ | $| $$ $| $$ | $$| $$ __
| $$__/ $| $$_____ | $$| | $$__/ $| $$__/ $| $$__/ $$_\$$$$$$\ | $$ | $| $$__| $| $$$$$$$| $$ | $$| $$| \
\$$ $$\$$ \ \$$ $$\$$ $| $$ $$\$$ $| $$ | $$ | $$\$$ $$\$$ | $$ | $$ \$$ $$
\$$$$$$ \$$$$$$$ \$$$$ \$$$$$$| $$$$$$$ \$$$$$$ \$$$$$$$ \$$ \$$_\$$$$$$$ \$$$$$$$\$$ \$$ \$$$$
| $$ | \__| $$
| $$ \$$ $$
\$$ \$$$$$$
__ __
| \ | \
____| $$ ______ ____| $$ ______ __ __ __ ______ _______ ______
______ ______ ______ / $$| \ / $$| \ | \ | \ | \| \| \ / \
| | | \ | $$$$$$$ \$$$$$$| $$$$$$$ \$$$$$$\ | $$ | $$ | $$ \$$$$$$| $$$$$$$| $$$$$$\
\$$$$$$\$$$$$$\$$$$$$ | $$ | $$/ $| $$ | $$/ $$ | $$ | $$ | $$/ $| $$ | $| $$ | $$
| $$__| $| $$$$$$| $$__| $| $$$$$$$ | $$_/ $$_/ $| $$$$$$| $$ | $| $$__| $$
\$$ $$\$$ $$\$$ $$\$$ $$ \$$ $$ $$\$$ $| $$ | $$\$$ $$
\$$$$$$$ \$$$$$$$ \$$$$$$$ \$$$$$$$ \$$$$$\$$$$ \$$$$$$$\$$ \$$_\$$$$$$$
| \__| $$
\$$ $$
\$$$$$$