[ Agent ] [ Executor ] - 优化修改Pipeline部分的代码
This commit is contained in:
@@ -225,7 +225,14 @@ func (op *AgentOsOperator) modifyHostname(args []string) [][]string {
|
|||||||
|
|
||||||
func (op *AgentOsOperator) modifyHostnameExec(args []string) (bool, []string) {
|
func (op *AgentOsOperator) modifyHostnameExec(args []string) (bool, []string) {
|
||||||
|
|
||||||
return false, nil
|
ok, resultLog := AllCommandExecutor(
|
||||||
|
[]string{
|
||||||
|
"hostnamectl",
|
||||||
|
"set-hostname",
|
||||||
|
args[0],
|
||||||
|
})
|
||||||
|
|
||||||
|
return ok, resultLog
|
||||||
}
|
}
|
||||||
|
|
||||||
func (op *AgentOsOperator) enableSwap() [][]string {
|
func (op *AgentOsOperator) enableSwap() [][]string {
|
||||||
@@ -342,12 +349,17 @@ func (op *AgentOsOperator) removeDockerExec() (bool, []string) {
|
|||||||
{
|
{
|
||||||
"systemctl",
|
"systemctl",
|
||||||
"stop",
|
"stop",
|
||||||
"docker",
|
"docker.service",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"systemctl",
|
||||||
|
"stop",
|
||||||
|
"docker.socket",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"systemctl",
|
"systemctl",
|
||||||
"disable",
|
"disable",
|
||||||
"docker",
|
"docker.service",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,7 +520,7 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
"apt-transport-https",
|
"apt-transport-https",
|
||||||
"ca-certificates",
|
"ca-certificates",
|
||||||
"curl",
|
"curl",
|
||||||
"gnupg-agent",
|
"gnupg",
|
||||||
"software-properties-common",
|
"software-properties-common",
|
||||||
}...)
|
}...)
|
||||||
|
|
||||||
@@ -518,26 +530,39 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
return false, l
|
return false, l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dockerGPGFilePath := "/etc/apt/keyrings/docker.gpg"
|
||||||
|
dockerAPTFilePath := "/etc/apt/sources.list.d/docker.list"
|
||||||
|
dockerGPGSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg"
|
||||||
|
dockerAPTSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu"
|
||||||
|
|
||||||
executor, resultLog := AllCompleteExecutor([][]string{
|
executor, resultLog := AllCompleteExecutor([][]string{
|
||||||
{
|
{
|
||||||
"rm",
|
"rm",
|
||||||
"-rf",
|
"-rf",
|
||||||
"/etc/apt/keyrings/docker.gpg",
|
dockerGPGFilePath,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rm",
|
||||||
|
"-rf",
|
||||||
|
dockerAPTFilePath,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"mkdir",
|
"mkdir",
|
||||||
"-p",
|
"-p",
|
||||||
"/etc/apt/keyrings",
|
"/etc/apt/keyrings",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"install",
|
||||||
|
"-m",
|
||||||
|
"0755",
|
||||||
|
"-d",
|
||||||
|
"/etc/apt/keyrings",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if !executor {
|
if !executor {
|
||||||
return false, append(resultLog, "创建docker-key gpg文件失败!")
|
return false, append(resultLog, "创建docker-key gpg文件失败!")
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerGPGFilePath := "/etc/apt/keyrings/docker.gpg"
|
|
||||||
dockerGPGSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg"
|
|
||||||
dockerAPTSource := "https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu"
|
|
||||||
|
|
||||||
// 安装镜像的证书
|
// 安装镜像的证书
|
||||||
if !op.IsAgentInnerWall {
|
if !op.IsAgentInnerWall {
|
||||||
// outside world
|
// outside world
|
||||||
@@ -545,12 +570,18 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
dockerAPTSource = "https://download.docker.com/linux/ubuntu"
|
dockerAPTSource = "https://download.docker.com/linux/ubuntu"
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, l2 := AllCommandExecutor([]string{
|
ok, l2 := PipelineCommandExecutor([][]string{
|
||||||
|
{
|
||||||
"curl",
|
"curl",
|
||||||
"-fsSL",
|
"-fsSL",
|
||||||
dockerGPGSource,
|
dockerGPGSource,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"gpg",
|
||||||
|
"--dearmor",
|
||||||
"-o",
|
"-o",
|
||||||
dockerGPGFilePath,
|
dockerGPGFilePath,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, append(l2, "下载docker gpg文件失败", dockerGPGSource, dockerAPTSource)
|
return false, append(l2, "下载docker gpg文件失败", dockerGPGSource, dockerAPTSource)
|
||||||
@@ -560,31 +591,44 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resultOk, log2 := AllCompleteExecutor([][]string{
|
resultOk, log2 := AllCompleteExecutor([][]string{
|
||||||
{
|
|
||||||
"gpg",
|
|
||||||
"--dearmor",
|
|
||||||
"-o",
|
|
||||||
dockerGPGFilePath,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"chmod",
|
"chmod",
|
||||||
"a+r",
|
"a+r",
|
||||||
dockerGPGFilePath,
|
dockerGPGFilePath,
|
||||||
},
|
},
|
||||||
{
|
//{
|
||||||
"add-apt-repository",
|
// "add-apt-repository",
|
||||||
"deb [arch=" + op.AgentArch + "] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable",
|
// "deb [arch=" + op.AgentArch + " signed-by=" + dockerGPGFilePath + " ] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable",
|
||||||
},
|
//},
|
||||||
})
|
})
|
||||||
if !resultOk {
|
if !resultOk {
|
||||||
return false, append(log2, "添加APT源失败!")
|
return false, append(log2, "添加APT源失败!")
|
||||||
}
|
}
|
||||||
|
dockerAPTSourceCommand := "deb [arch=" + op.AgentArch + " signed-by=" + dockerGPGFilePath + "] " + dockerAPTSource + " " + op.AgentOSReleaseCode + " stable"
|
||||||
|
|
||||||
|
log.InfoF("dockerAPTSourceCommand is => %s ", dockerAPTSourceCommand)
|
||||||
|
|
||||||
|
ok, log3 := PipelineCommandExecutor([][]string{
|
||||||
|
{
|
||||||
|
"echo",
|
||||||
|
dockerAPTSourceCommand,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tee",
|
||||||
|
dockerAPTFilePath,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if !ok {
|
||||||
|
return false, append(log3, "添加APT源失败!")
|
||||||
|
}
|
||||||
|
|
||||||
// look for specific docker-version to install
|
// look for specific docker-version to install
|
||||||
AllCommandExecutor([]string{"apt-get", "update"})
|
ok, log4 := AllCommandExecutor([]string{"apt-get", "update"})
|
||||||
|
if !ok {
|
||||||
|
return false, append(log4, "apt-get update 失败!")
|
||||||
|
}
|
||||||
|
|
||||||
// ubuntu 内部
|
// ubuntu 内部
|
||||||
|
|
||||||
var specificDockerVersion string
|
var specificDockerVersion string
|
||||||
// hard code here 5:20.10.10~3-0~ubuntu-focal
|
// hard code here 5:20.10.10~3-0~ubuntu-focal
|
||||||
if strings.HasPrefix(args[0], "19") {
|
if strings.HasPrefix(args[0], "19") {
|
||||||
@@ -593,19 +637,53 @@ func (op *AgentOsOperator) installDockerExec(args []string) (bool, []string) {
|
|||||||
specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode
|
specificDockerVersion = "5:20.10.10~3-0~ubuntu-" + op.AgentOSReleaseCode
|
||||||
}
|
}
|
||||||
|
|
||||||
resultOk, log2 = AllCommandExecutor([]string{
|
log.InfoF("需要安装的docker版本为 => %s", specificDockerVersion)
|
||||||
|
|
||||||
|
dockerStaffList := []string{
|
||||||
"docker-ce=" + specificDockerVersion,
|
"docker-ce=" + specificDockerVersion,
|
||||||
"docker-ce-cli=" + specificDockerVersion,
|
"docker-ce-cli=" + specificDockerVersion,
|
||||||
"containerd.io",
|
"containerd.io",
|
||||||
"docker-compose-plugin",
|
"docker-compose-plugin",
|
||||||
})
|
}
|
||||||
|
|
||||||
|
for _, s := range dockerStaffList {
|
||||||
|
resultOk, log2 = AllCommandExecutor(
|
||||||
|
append(op.InstallCommandPrefix, s))
|
||||||
if !resultOk {
|
if !resultOk {
|
||||||
return false, log2
|
return false, append(log2, s, "安装失败!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 启动docker服务
|
||||||
|
completeExecutor, log5 := AllCompleteExecutor([][]string{
|
||||||
|
{
|
||||||
|
"systemctl",
|
||||||
|
"stop",
|
||||||
|
"docker.service",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"sleep",
|
||||||
|
"2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"systemctl",
|
||||||
|
"start",
|
||||||
|
"docker.service",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"systemctl",
|
||||||
|
"enable",
|
||||||
|
"docker.service",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if !completeExecutor {
|
||||||
|
return false, append(log5, "启动docker.service失败,请查明原因!", "journalctl -u docker -n 100 -f")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return true, []string{
|
||||||
|
"docker安装成功!",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (op *AgentOsOperator) removeDockerCompose() [][]string {
|
func (op *AgentOsOperator) removeDockerCompose() [][]string {
|
||||||
@@ -624,7 +702,9 @@ func (op *AgentOsOperator) removeDockerComposeExec() (bool, []string) {
|
|||||||
|
|
||||||
commandName := "docker-compose"
|
commandName := "docker-compose"
|
||||||
if !BasicCommandExists(commandName) {
|
if !BasicCommandExists(commandName) {
|
||||||
return true, nil
|
return true, []string{
|
||||||
|
commandName + " 卸载成功!",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
possibleDockerComposeFilePath := []string{
|
possibleDockerComposeFilePath := []string{
|
||||||
"/usr/local/bin/" + commandName,
|
"/usr/local/bin/" + commandName,
|
||||||
@@ -636,7 +716,9 @@ func (op *AgentOsOperator) removeDockerComposeExec() (bool, []string) {
|
|||||||
AllCommandExecutor(removeDockerCommand)
|
AllCommandExecutor(removeDockerCommand)
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return true, []string{
|
||||||
|
commandName + " 卸载成功!",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (op *AgentOsOperator) installDockerCompose() [][]string {
|
func (op *AgentOsOperator) installDockerCompose() [][]string {
|
||||||
@@ -652,6 +734,7 @@ func (op *AgentOsOperator) installDockerCompose() [][]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
|
func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
|
||||||
|
log.Info("准备安装docker-compose!")
|
||||||
|
|
||||||
// 安装特定版本的 docker-compose 2.18.0
|
// 安装特定版本的 docker-compose 2.18.0
|
||||||
if op.OssOfflinePrefix == "" {
|
if op.OssOfflinePrefix == "" {
|
||||||
@@ -659,9 +742,12 @@ func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DockerComposeFile := op.OssOfflinePrefix + "docker-compose-linux-x86_64-v2.18.0"
|
DockerComposeFile := op.OssOfflinePrefix + "docker-compose-linux-x86_64-v2.18.0"
|
||||||
|
log.InfoF("需要安装的docker版本为 => %s", DockerComposeFile)
|
||||||
|
|
||||||
ok, resultLog := AllCommandExecutor([]string{
|
ok, resultLog := AllCommandExecutor([]string{
|
||||||
"wget", DockerComposeFile,
|
"wget",
|
||||||
|
"--timeout=10",
|
||||||
|
DockerComposeFile,
|
||||||
"-O",
|
"-O",
|
||||||
"/usr/local/bin/docker-compose",
|
"/usr/local/bin/docker-compose",
|
||||||
})
|
})
|
||||||
@@ -669,6 +755,7 @@ func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
|
|||||||
return false, resultLog
|
return false, resultLog
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("docker-compose下载成功!")
|
||||||
if !BasicFileExistAndNotNull("/usr/local/bin/docker-compose") {
|
if !BasicFileExistAndNotNull("/usr/local/bin/docker-compose") {
|
||||||
return false, []string{"docker-compose 下载失败!"}
|
return false, []string{"docker-compose 下载失败!"}
|
||||||
}
|
}
|
||||||
@@ -681,8 +768,8 @@ func (op *AgentOsOperator) installDockerComposeExec() (bool, []string) {
|
|||||||
"ln", "-s", "/usr/local/bin/docker-compose", "/usr/bin/docker-compose",
|
"ln", "-s", "/usr/local/bin/docker-compose", "/usr/bin/docker-compose",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
log.Info("docker-compose安装成功!")
|
||||||
return true, nil
|
return true, []string{"docker-compose安装成功!"}
|
||||||
}
|
}
|
||||||
func (op *AgentOsOperator) installHelm() [][]string {
|
func (op *AgentOsOperator) installHelm() [][]string {
|
||||||
installHelmFunc := [][]string{
|
installHelmFunc := [][]string{
|
||||||
@@ -1148,9 +1235,10 @@ func (op *AgentOsOperator) installZSHExec() (bool, []string) {
|
|||||||
} else {
|
} else {
|
||||||
zshRemoteGitUrl = "https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh"
|
zshRemoteGitUrl = "https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh"
|
||||||
}
|
}
|
||||||
|
log.InfoF("开始下载zsh的安装脚本 => %s", zshRemoteGitUrl)
|
||||||
resultOk, l := AllCommandExecutor([]string{
|
resultOk, l := AllCommandExecutor([]string{
|
||||||
"wget",
|
"wget",
|
||||||
|
"--timeout=5",
|
||||||
zshRemoteGitUrl,
|
zshRemoteGitUrl,
|
||||||
"-O",
|
"-O",
|
||||||
"/root/wdd/zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
@@ -1178,6 +1266,7 @@ func (op *AgentOsOperator) installZSHExec() (bool, []string) {
|
|||||||
return false, log2
|
return false, log2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Info("zsh安装完成! 开始进行zsh的配置!")
|
||||||
modifyZSHOK, log3 := AllCompleteExecutor(
|
modifyZSHOK, log3 := AllCompleteExecutor(
|
||||||
[][]string{
|
[][]string{
|
||||||
{
|
{
|
||||||
@@ -1237,7 +1326,6 @@ func (op *AgentOsOperator) installZSHExec() (bool, []string) {
|
|||||||
"zsh",
|
"zsh",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if !modifyZSHOK {
|
if !modifyZSHOK {
|
||||||
log.Warn("ZSH 安装成功,但是配置修改失败!")
|
log.Warn("ZSH 安装成功,但是配置修改失败!")
|
||||||
return true, log3
|
return true, log3
|
||||||
|
|||||||
@@ -79,6 +79,9 @@ func BasicFileExistAndNotNull(filename string) bool {
|
|||||||
|
|
||||||
func BasicPrettyPrint(resultOk bool, resultLog []string) {
|
func BasicPrettyPrint(resultOk bool, resultLog []string) {
|
||||||
fmt.Printf("resultOK is => %#v\n", resultOk)
|
fmt.Printf("resultOK is => %#v\n", resultOk)
|
||||||
|
if resultLog == nil || len(resultLog) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, s := range resultLog {
|
for _, s := range resultLog {
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ var Log, _ = NewLogger()
|
|||||||
func NewLogger() (*Logger, error) {
|
func NewLogger() (*Logger, error) {
|
||||||
config := zap.Config{
|
config := zap.Config{
|
||||||
Encoding: "json",
|
Encoding: "json",
|
||||||
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
|
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
||||||
OutputPaths: []string{"stdout"}, // 输出到控制台
|
OutputPaths: []string{"stdout"}, // 输出到控制台
|
||||||
ErrorOutputPaths: []string{"stderr"},
|
ErrorOutputPaths: []string{"stderr"},
|
||||||
EncoderConfig: zapcore.EncoderConfig{
|
EncoderConfig: zapcore.EncoderConfig{
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func main() {
|
|||||||
|
|
||||||
var agentOP = &executor.AgentOsOperator{
|
var agentOP = &executor.AgentOsOperator{
|
||||||
InstallCommandPrefix: []string{
|
InstallCommandPrefix: []string{
|
||||||
"apt-get", "install", "-y",
|
"apt-get", "install", "--allow-downgrades", "-y",
|
||||||
},
|
},
|
||||||
RemoveCommandPrefix: []string{"apt", "remove", "-y"},
|
RemoveCommandPrefix: []string{"apt", "remove", "-y"},
|
||||||
CanAccessInternet: true,
|
CanAccessInternet: true,
|
||||||
@@ -48,12 +48,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall"))
|
//executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall"))
|
||||||
//executor.BasicPrettyPrint(agentOP.Exec("modifyHostname", "seoul-amd64-01"))
|
//executor.BasicPrettyPrint(agentOP.Exec("modifyHostname", "seoul-amd64-07"))
|
||||||
//executor.BasicPrettyPrint(agentOP.Exec("disableSwap"))
|
//executor.BasicPrettyPrint(agentOP.Exec("disableSwap"))
|
||||||
executor.BasicPrettyPrint(agentOP.Exec("removeDocker"))
|
//executor.BasicPrettyPrint(agentOP.Exec("removeDocker"))
|
||||||
executor.BasicPrettyPrint(agentOP.Exec("installDocker", "20"))
|
//executor.BasicPrettyPrint(agentOP.Exec("installDocker", "20"))
|
||||||
executor.BasicPrettyPrint(agentOP.Exec("removeDockerCompose"))
|
//executor.BasicPrettyPrint(agentOP.Exec("removeDockerCompose"))
|
||||||
executor.BasicPrettyPrint(agentOP.Exec("installDockerCompose"))
|
executor.BasicPrettyPrint(agentOP.Exec("installDockerCompose"))
|
||||||
//executor.BasicPrettyPrint(agentOP.Exec("installZSH"))
|
executor.BasicPrettyPrint(agentOP.Exec("installZSH"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user