diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index b7c39cc..9932edc 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -276,10 +276,35 @@ func (op *AgentOsOperator) disableSwap() [][]string { func (op *AgentOsOperator) disableSwapExec() (bool, []string) { - resultOK := true 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 { @@ -311,7 +336,55 @@ func (op *AgentOsOperator) removeDocker() [][]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 { @@ -428,7 +501,92 @@ func (op *AgentOsOperator) installDocker(args []string) [][]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 { diff --git a/agent-go/executor/BasicFunction.go b/agent-go/executor/BasicFunction.go index b7de4e3..9db730a 100644 --- a/agent-go/executor/BasicFunction.go +++ b/agent-go/executor/BasicFunction.go @@ -30,10 +30,15 @@ func BasicCommandExistsBatch(commandNameList []string) (bool, string) { // BasicReplace 基础替换命令 func BasicReplace(filename string, origin string, replace string) bool { + // 暂不添加 + //if !BasicFileExistAndNotNull(filename) { + // log.DebugF("文件替换") + //} + cmd := exec.Command("sed", "-i", "s/"+origin+"/"+replace+"/g", filename) err := cmd.Run() if err != nil { - log.DebugF("文件 %s [%s] => [%s] 错误!", filename, origin, replace) + log.DebugF("替换文件 %s ,从 [%s] => [%s] 错误!", filename, origin, replace) return false } else { return true diff --git a/agent-go/executor/BasicFunction_test.go b/agent-go/executor/BasicFunction_test.go index 9c4515f..de704a9 100644 --- a/agent-go/executor/BasicFunction_test.go +++ b/agent-go/executor/BasicFunction_test.go @@ -5,7 +5,8 @@ import ( "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) { @@ -14,3 +15,25 @@ func TestBasicFileExistAndNotNull(t *testing.T) { 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!") +}