[Agent] [Executor] 重写BaseFunction

This commit is contained in:
zeaslity
2023-10-16 15:34:06 +08:00
parent 020fc76e05
commit db58a7a6f6
3 changed files with 192 additions and 6 deletions

View File

@@ -276,10 +276,35 @@ func (op *AgentOsOperator) disableSwap() [][]string {
func (op *AgentOsOperator) disableSwapExec() (bool, []string) { func (op *AgentOsOperator) disableSwapExec() (bool, []string) {
resultOK := true
var resultLog []string var resultLog []string
return resultOK, resultLog // 备份文件存在pass
if !BasicFileExists("/etc/fstab_back_wdd") {
AllCommandExecutor([]string{
"cp",
"-f",
"/etc/fstab",
"/etc/fstab_back_wdd",
})
}
// 执行关闭操作
_, lre := AllCompleteExecutor(
[][]string{
{
"swapoff",
"-a",
},
{
"sed",
"-i",
"/swap/d",
"/etc/fstab",
},
})
resultLog = append(resultLog, lre...)
return true, resultLog
} }
func (op *AgentOsOperator) removeDocker() [][]string { func (op *AgentOsOperator) removeDocker() [][]string {
@@ -311,7 +336,55 @@ func (op *AgentOsOperator) removeDocker() [][]string {
} }
func (op *AgentOsOperator) removeDockerExec() (bool, []string) { func (op *AgentOsOperator) removeDockerExec() (bool, []string) {
return false, nil var resultLog []string
dockerServiceStopCommand := [][]string{
{
"systemctl",
"stop",
"docker",
},
{
"systemctl",
"disable",
"docker",
},
}
ok, l := AllCompleteExecutor(dockerServiceStopCommand)
if !ok {
resultLog = append(resultLog, l...)
}
dockerStaff := []string{
"docker-ce",
"docker.io",
"docker-ce-cli",
"docker",
"docker-common",
"docker-latest",
"docker-latest-logrotate",
"docker-logrotate",
"docker-selinux",
"docker-engine-selinux",
"docker-engine",
"kubelet",
"kubeadm",
"kubectl",
"docker-client",
"docker-client-latest",
}
for _, staff := range dockerStaff {
removeCommand := append(op.RemoveCommandPrefix, staff)
ok, result := AllCommandExecutor(removeCommand)
if !ok {
resultLog = append(resultLog, result...)
}
}
return true, resultLog
} }
func (op *AgentOsOperator) installDocker(args []string) [][]string { func (op *AgentOsOperator) installDocker(args []string) [][]string {
@@ -428,7 +501,92 @@ func (op *AgentOsOperator) installDocker(args []string) [][]string {
} }
func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) { func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
return false, nil var resultLog []string
resultOk = true
if op.IsOsTypeUbuntu {
installDependencyCommand := append(op.InstallCommandPrefix, []string{
"apt-transport-https",
"ca-certificates",
"curl",
"gnupg-agent",
"software-properties-common",
}...)
ok, l := AllCommandExecutor(installDependencyCommand)
if !ok {
//
return false, l
}
// add dependency
if op.IsAgentInnerWall {
// inner gfw
installDockerFunc = append(installDockerFunc, [][]string{
installFirstLine,
{
"curl",
"-o",
"/usr/share/keyrings/docker-utsc.gpg",
"https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg",
},
{
"apt-key",
"add",
"/usr/share/keyrings/docker-utsc.gpg",
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu " + op.AgentOSReleaseCode + " stable",
},
}...)
} else {
// outside world
installDockerFunc = append(installDockerFunc, [][]string{
installFirstLine,
{
"curl",
"-o",
"/usr/share/keyrings/docker.gpg",
"https://download.docker.com/linux/ubuntu/gpg ",
},
{
"apt-key",
"add",
"/usr/share/keyrings/docker.gpg",
},
{
"add-apt-repository",
"deb [arch=" + op.AgentArch + "] https://download.docker.com/linux/ubuntu " + op.AgentOSReleaseCode + " stable",
},
}...)
}
// look for specific docker-version to install
installDockerFunc = append(installDockerFunc, []string{"apt-get", "update"})
var specificDockerVersion string
// hard code here 5:20.10.10~3-0~ubuntu-focal
if strings.HasPrefix(args[0], "19") {
specificDockerVersion = "5:19.03.15~3-0~ubuntu-" + op.AgentOSReleaseCode
} else {
specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode
}
installDockerFunc = append(installDockerFunc,
append(
op.InstallCommandPrefix,
"docker-ce="+specificDockerVersion,
"docker-ce-cli="+specificDockerVersion,
"containerd.io",
"docker-compose-plugin",
),
)
}
return resultOk, resultLog
} }
func (op *AgentOsOperator) removeDockerCompose() [][]string { func (op *AgentOsOperator) removeDockerCompose() [][]string {

View File

@@ -30,10 +30,15 @@ func BasicCommandExistsBatch(commandNameList []string) (bool, string) {
// BasicReplace 基础替换命令 // BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool { func BasicReplace(filename string, origin string, replace string) bool {
// 暂不添加
//if !BasicFileExistAndNotNull(filename) {
// log.DebugF("文件替换")
//}
cmd := exec.Command("sed", "-i", "s/"+origin+"/"+replace+"/g", filename) cmd := exec.Command("sed", "-i", "s/"+origin+"/"+replace+"/g", filename)
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.DebugF("文件 %s [%s] => [%s] 错误!", filename, origin, replace) log.DebugF("替换文件 %s ,从 [%s] => [%s] 错误!", filename, origin, replace)
return false return false
} else { } else {
return true return true

View File

@@ -5,7 +5,8 @@ import (
"testing" "testing"
) )
var emptyFilePath = "/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script" var emptyFilePath = "/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/123"
var noExistFilePath = "/home/wdd/IdeaProjects/ProjectOctopus/agent-go/executor/script/456"
func TestBasicFileExistAndNotNull(t *testing.T) { func TestBasicFileExistAndNotNull(t *testing.T) {
@@ -14,3 +15,25 @@ func TestBasicFileExistAndNotNull(t *testing.T) {
assert.Equal(t, resultOK, false, "判定为空文件返回false") assert.Equal(t, resultOK, false, "判定为空文件返回false")
} }
func TestBasicReplaceFileNotExists(t *testing.T) {
replace := BasicReplace(noExistFilePath, "123", "123")
t.Logf("replace no exists file result are => %#v", replace)
}
func TestBasicFileExists(t *testing.T) {
exists := BasicFileExists(emptyFilePath)
assert.Equal(t, exists, true, "文件存在但是为空应该返回true")
}
func TestBasicFileExistFalse(t *testing.T) {
exists := BasicFileExists(noExistFilePath)
assert.Equal(t, exists, false, "文件不存在应该返回false")
}