diff --git a/agent-wdd/cmd/Base.go b/agent-wdd/cmd/Base.go index 5f284b7..c36acf1 100644 --- a/agent-wdd/cmd/Base.go +++ b/agent-wdd/cmd/Base.go @@ -220,6 +220,7 @@ func addBaseSubcommands(cmd *cobra.Command) { cmd.AddCommand( dockerCmd, dockerComposeCmd, + harborCmd, swapCmd, commonToolsInstall, selinuxCmd, @@ -246,10 +247,13 @@ func addHarborSubcommands(harborCmd *cobra.Command) { } // 解压harbor - utils.UnzipFile(harborLocalInstallPath, "/root/wdd/harbor") + utils.UnzipFile(harborLocalInstallPath, "/root/wdd/") // 获取本机的内网IPv4地址 configCache := config.ConfigCache + if len(configCache.Agent.Network.Interfaces) == 0 { + log.Error("没有获取到本机的ipv4地址,无法安装harbor! 请执行 info all !") + } ip := configCache.Agent.Network.Interfaces[0].IPv4 if ip == "" { @@ -266,11 +270,9 @@ func addHarborSubcommands(harborCmd *cobra.Command) { return } - // 进入harbor目录 - os.Chdir("/root/wdd/harbor") - // 运行install.sh - ok, log1 := op.HardCodeCommandExecutor("bash install.sh") + op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"}) + ok, log1 := op.SingleLineCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"}) if !ok { log.Error("安装harbor失败: %s", log1) return @@ -314,18 +316,16 @@ func addHarborSubcommands(harborCmd *cobra.Command) { return } - // 进入harbor目录 - os.Chdir("/root/wdd/harbor") - // 启动harbor - ok, log1 := op.HardCodeCommandExecutor("docker-compose up -d") + op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"}) + ok, log1 := op.SingleLineCommandExecutor([]string{"docker-compose", "up", "-d"}) if !ok { log.Error("启动harbor失败: %s", log1) return } - // 等待5秒 - time.Sleep(5 * time.Second) + // 等待10秒 + time.Sleep(10 * time.Second) // 检查harbor是否启动成功 if !checkHarborIsRunning() { @@ -357,7 +357,7 @@ func addHarborSubcommands(harborCmd *cobra.Command) { os.Chdir("/root/wdd/harbor") // 停止harbor - ok, log1 := op.HardCodeCommandExecutor("docker-compose down") + ok, log1 := op.SingleLineCommandExecutor([]string{"docker-compose", "down"}) if !ok { log.Error("停止harbor失败: %s", log1) return @@ -372,14 +372,30 @@ func addHarborSubcommands(harborCmd *cobra.Command) { harborCmd.AddCommand(harborInstallCmd, harborUninstallCmd, harborStartCmd, harborStopCmd) } -// checkHarborIsRunning 检查harbor是否运行 +// checkHarborIsRunning 检查harbor是否运行 如果存在Exit 或者 Restarting 则表示有错误! func checkHarborIsRunning() bool { - ok, log2 := op.HardCodeCommandExecutor("docker ps -a | grep goharbor/ | grep -E 'Exit|Restarting' | wc -l") - if !ok { - log.Error("查看harbor容器是否存在失败: %s", log2) - return false - } - if strings.TrimSpace(log2[0]) != "0" { + _, resultLog := op.PipeLineCommandExecutor([][]string{ + { + "docker", + "ps", + "-a", + }, + { + "grep", + "goharbor/", + }, + { + "grep", + "-E", + "'Exit|Restarting'", + }, + { + "wc", + "-l", + }, + }) + + if strings.TrimSpace(resultLog[0]) != "0" { log.Error("harbor启动失败! 请检查日志!") return false } @@ -605,7 +621,21 @@ func addDockerSubcommands(cmd *cobra.Command) { // apt-cache madison docker-ce | grep 20.10.20 | awk '{print$3}' // get by method - ok, log4 := op.HardCodeCommandExecutor("apt-cache madison docker-ce | grep 20.10.20 | awk '{print$3}'") + ok, log4 := op.PipeLineCommandExecutor([][]string{ + { + "apt-cache", + "madison", + "docker-ce", + }, + { + "grep", + "20.10.20", + }, + { + "awk", + "'{print$3}'", + }, + }) if ok && log4 != nil && len(log4) > 0 { specificDockerVersion = strings.TrimSpace(log4[0]) fmt.Println("get docker version from online => " + specificDockerVersion) @@ -706,14 +736,7 @@ func addDockerSubcommands(cmd *cobra.Command) { } // 解压文件 - utils.UnzipFile(dockerLocalInstallPath, "/root/wdd") - - // 安装docker - err := utils.MoveFolerToAnother("/root/wdd/docker", "/usr/bin") - if err != nil { - log.Error("Failed to move Docker binaries: %s", err.Error()) - return - } + utils.UnzipFile(dockerLocalInstallPath, "/root/wdd/") // 设置权限 dockerBinList := []string{ @@ -728,11 +751,22 @@ func addDockerSubcommands(cmd *cobra.Command) { "containerd-shim-runc-v2", } + // 删除旧的docker 二进制文件 for _, bin := range dockerBinList { - ok, resultLog := op.HardCodeCommandExecutor("chmod 777 /usr/bin/" + bin) + utils.RemoveFile("/usr/bin/" + bin) + } + + // 安装docker + err := utils.MoveFolerToAnother("/root/wdd/docker/", "/usr/bin") + if err != nil { + log.Error("Failed to move Docker binaries: %s", err.Error()) + return + } + + for _, bin := range dockerBinList { + ok, resultLog := op.SingleLineCommandExecutor([]string{"chmod", "777", "/usr/bin/" + bin}) if !ok { log.Error("Failed to set permissions for Docker binaries: %s", resultLog) - return } } @@ -910,7 +944,7 @@ func addDockerComposeSubcommands(cmd *cobra.Command) { op.SingleLineCommandExecutor(linkCmd) // 验证安装 - verifyCmd := []string{"docker-compose", "--version"} + verifyCmd := []string{"/usr/local/bin/docker-compose", "version"} ok, resultLog = op.SingleLineCommandExecutor(verifyCmd) if !ok { log.Error("Docker Compose 安装验证失败: %s", resultLog) @@ -950,7 +984,7 @@ func addDockerComposeSubcommands(cmd *cobra.Command) { } // move file to /usr/local/bin - err := utils.MoveFileToAnother(dockerComposeLocalInstallPath, "/usr/local/bin") + err := utils.MoveFileToAnother(dockerComposeLocalInstallPath, "/usr/local/bin/docker-compose") if err != nil { log.Error("Failed to move Docker Compose binaries: %s", err.Error()) return @@ -964,6 +998,18 @@ func addDockerComposeSubcommands(cmd *cobra.Command) { return } + // 创建软链接 + linkCmd := []string{"ln", "-sf", "/usr/local/bin/docker-compose", "/usr/bin/docker-compose"} + op.SingleLineCommandExecutor(linkCmd) + + // 验证安装 + verifyCmd := []string{"/usr/local/bin/docker-compose", "version"} + ok, resultLog = op.SingleLineCommandExecutor(verifyCmd) + if !ok { + log.Error("Docker Compose 安装验证失败: %s", resultLog) + return + } + log.Info("Docker Compose installed successfully from local file!") }, } diff --git a/agent-wdd/cmd/Zsh.go b/agent-wdd/cmd/Zsh.go index f43c9a6..2f1d2a7 100644 --- a/agent-wdd/cmd/Zsh.go +++ b/agent-wdd/cmd/Zsh.go @@ -76,13 +76,13 @@ func addZshSubcommands(cmd *cobra.Command) { log.Info("安装zsh完成, 开始安装插件!") pluginsHardCodeUrl := []string{ - "/usr/bin/git clone https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions", - "/usr/bin/git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting", + "https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions", + "https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting", } pluginsHardCodeOutsideUrl := []string{ - "/usr/bin/git clone https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions", - "/usr/bin/git clone https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting", + "https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions", + "https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting", } pluginsListUrl := []string{ @@ -104,13 +104,13 @@ func addZshSubcommands(cmd *cobra.Command) { if config.ConfigCache.Agent.Network.Internet == 7 { // 执行硬编码命令, 安装插件 for _, command := range pluginsHardCodeUrl { - op.HardCodeCommandExecutor(command) + op.SingleLineCommandExecutor([]string{"git", "clone", command}) } } else { // 执行硬编码命令, 安装插件 国外 for _, command := range pluginsHardCodeOutsideUrl { - op.HardCodeCommandExecutor(command) + op.SingleLineCommandExecutor([]string{"git", "clone", command}) } } // 下载插件 diff --git a/agent-wdd/cmd/beans/DockerDaemonConfig.go b/agent-wdd/cmd/beans/DockerDaemonConfig.go index a1ac546..ae778d9 100644 --- a/agent-wdd/cmd/beans/DockerDaemonConfig.go +++ b/agent-wdd/cmd/beans/DockerDaemonConfig.go @@ -43,7 +43,7 @@ LimitNOFILE=infinity TasksMax=infinity OOMScoreAdjust=-999 -[installPrefix] +[Install] WantedBy=multi-user.target ` @@ -57,7 +57,7 @@ SocketMode=0660 SocketUser=root SocketGroup=docker -[installPrefix] +[Install] WantedBy=sockets.target ` @@ -107,7 +107,7 @@ Delegate=yes KillMode=process OOMScoreAdjust=-500 -[installPrefix] +[Install] WantedBy=multi-user.target ` diff --git a/agent-wdd/test/one-click-build-run.ps1 b/agent-wdd/test/one-click-build-run.ps1 index fb001c6..c50972d 100644 --- a/agent-wdd/test/one-click-build-run.ps1 +++ b/agent-wdd/test/one-click-build-run.ps1 @@ -21,12 +21,12 @@ Set-Location "C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\tes # 删除上面存在的旧的内容 mc.exe rm oracle-seoul-2/seoul-2/agent-wdd_linux_amd64 -mc.exe rm oracle-seoul-2/seoul-2/test-shell.sh +# mc.exe rm oracle-seoul-2/seoul-2/test-shell.sh # 上传文件 mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\build\agent-wdd_linux_amd64 oracle-seoul-2/seoul-2/ -mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\test\test-shell.sh oracle-seoul-2/seoul-2/ +# mc.exe cp C:\Users\wddsh\Documents\IdeaProjects\ProjectOctopus\agent-wdd\test\test-shell.sh oracle-seoul-2/seoul-2/ diff --git a/agent-wdd/utils/ZipUtils.go b/agent-wdd/utils/ZipUtils.go index 4030ca4..f4331ea 100644 --- a/agent-wdd/utils/ZipUtils.go +++ b/agent-wdd/utils/ZipUtils.go @@ -10,8 +10,10 @@ import ( "path/filepath" ) -// UnzipFile 解压文件, 支持zip和tar.gz +// UnzipFile 解压文件, 支持zip tgz tar.gz tar func UnzipFile(zipFile string, targetFolder string) { + log.Info("解压文件: %s, 到: %s", zipFile, targetFolder) + // 检查文件扩展名以确定解压缩方法 fileExt := filepath.Ext(zipFile) switch fileExt {