新增Harbor管理功能和优化文件操作工具

- 在Base.go中添加Harbor安装、启动、停止和卸载子命令
- 实现Harbor本地安装流程,包括配置文件修改和容器检查
- 在Excutor.go中改进命令执行错误处理
- 在FileUtils.go中新增MoveFileToAnother方法,优化文件移动逻辑
- 修复DockerCompose本地安装命令的文件路径和移动方法
This commit is contained in:
zeaslity
2025-03-07 10:58:20 +08:00
parent 5c2325b7e4
commit 06b044dabc
4 changed files with 266 additions and 8 deletions

View File

@@ -7,8 +7,10 @@ import (
"agent-wdd/op"
"agent-wdd/utils"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/spf13/cobra"
)
@@ -21,8 +23,9 @@ var (
"deltarpm", "net-tools", "iputils", "bind-utils", "lsof", "curl", "wget", "vim", "mtr", "htop",
}
dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-amd64" // 本地安装docker compose的文件路径
dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-amd64" // 本地安装docker compose的文件路径
harborLocalInstallPath = "/root/wdd/harbor-offline-installer-v2.9.0.tgz" // 本地安装harbor的文件路径
)
func init() {
@@ -30,9 +33,11 @@ func init() {
case "amd64":
dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-amd64" // 本地安装docker compose的文件路径
harborLocalInstallPath = "/root/wdd/harbor-offline-installer-v2.9.0.tgz" // 本地安装harbor的文件路径
case "arm64":
dockerLocalInstallPath = "/root/wdd/docker-arm64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-arm64" // 本地安装docker compose的文件路径
harborLocalInstallPath = "/root/wdd/harbor-offline-installer-v2.9.0-arm64.tgz" // 本地安装harbor的文件路径
}
}
@@ -205,6 +210,13 @@ func addBaseSubcommands(cmd *cobra.Command) {
}
addSSHSubcommands(sshCmd)
// harbor 相关的函数
harborCmd := &cobra.Command{
Use: "harbor",
Short: "harbor相关的操作",
}
addHarborSubcommands(harborCmd)
cmd.AddCommand(
dockerCmd,
dockerComposeCmd,
@@ -218,6 +230,162 @@ func addBaseSubcommands(cmd *cobra.Command) {
)
}
// addHarborSubcommands 添加harbor子命令
func addHarborSubcommands(harborCmd *cobra.Command) {
harborInstallCmd := &cobra.Command{
Use: "install",
Short: "安装harbor 从本地安装 " + harborLocalInstallPath,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("")
log.Info("安装harbor")
// 检查是否存在harbor目录
if utils.FileExistAndNotNull("/root/wdd/harbor/harbor.yml") {
log.Info("harbor已存在无需安装!")
return
}
// 解压harbor
utils.UnzipFile(harborLocalInstallPath, "/root/wdd/harbor")
// 获取本机的内网IPv4地址
configCache := config.ConfigCache
ip := configCache.Agent.Network.Interfaces[0].IPv4
if ip == "" {
log.Error("没有获取到本机的ipv4地址无法安装harbor! 请执行 info all !")
return
}
// 修改harbor的配置
utils.AppendOverwriteContentToFile(beans.HarborYamlConfig, "/root/wdd/harbor/harbor.yml")
utils.FindAndReplaceContentInFile("HarborHostName", ip, "/root/wdd/harbor/harbor.yml")
if !utils.FileExistAndNotNull("/root/wdd/harbor/harbor.yml") {
log.Error("修改harbor的配置失败! 请检查文件是否存在!")
return
}
// 进入harbor目录
os.Chdir("/root/wdd/harbor")
// 运行install.sh
ok, log1 := op.HardCodeCommandExecutor("bash install.sh")
if !ok {
log.Error("安装harbor失败: %s", log1)
return
}
// wait to see the harbor is running
log.Info("等待10秒 harbor启动...")
time.Sleep(10 * time.Second)
// 执行docker ps -a 查看 harbor名称的容器是否存在 Exit 或者 Restarting 则表示有错误!
if !checkHarborIsRunning() {
log.Error("harbor启动失败! 请检查日志!")
return
}
log.Info("安装harbor成功!")
},
}
harborUninstallCmd := &cobra.Command{
Use: "uninstall",
Short: "卸载harbor",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("")
log.Info("暂未实现harbor卸载!")
},
}
harborStartCmd := &cobra.Command{
Use: "start",
Short: "启动harbor",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("")
log.Info("启动harbor")
// 检查harbor是否存在
if !utils.FileExistAndNotNull("/root/wdd/harbor/docker-compose.yml") {
log.Error("harbor不存在! 请先安装harbor!")
return
}
// 进入harbor目录
os.Chdir("/root/wdd/harbor")
// 启动harbor
ok, log1 := op.HardCodeCommandExecutor("docker-compose up -d")
if !ok {
log.Error("启动harbor失败: %s", log1)
return
}
// 等待5秒
time.Sleep(5 * time.Second)
// 检查harbor是否启动成功
if !checkHarborIsRunning() {
log.Error("harbor启动失败! 请检查日志!")
return
}
log.Info("启动harbor成功!")
fmt.Println("")
fmt.Println("")
},
}
// 停止harbor
harborStopCmd := &cobra.Command{
Use: "stop",
Short: "停止harbor",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("")
log.Info("停止harbor")
// 检查harbor是否存在
if !utils.FileExistAndNotNull("/root/wdd/harbor/docker-compose.yml") {
log.Error("harbor不存在! 请先安装harbor!")
return
}
// 进入harbor目录
os.Chdir("/root/wdd/harbor")
// 停止harbor
ok, log1 := op.HardCodeCommandExecutor("docker-compose down")
if !ok {
log.Error("停止harbor失败: %s", log1)
return
}
log.Info("停止harbor成功!")
fmt.Println("")
fmt.Println("")
},
}
harborCmd.AddCommand(harborInstallCmd, harborUninstallCmd, harborStartCmd, harborStopCmd)
}
// checkHarborIsRunning 检查harbor是否运行
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" {
log.Error("harbor启动失败! 请检查日志!")
return false
}
return true
}
func addSSHSubcommands(sshCmd *cobra.Command) {
keyCmd := &cobra.Command{
Use: "key",
@@ -770,20 +938,19 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
}
localCmd := &cobra.Command{
Use: "local [path]",
Use: "local",
Short: "本地安装DockerCompose 安装文件应该为 " + dockerComposeLocalInstallPath,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
log.Info("Installing Docker Compose from local file...")
exist := utils.FileExistAndNotNull(dockerLocalInstallPath)
exist := utils.FileExistAndNotNull(dockerComposeLocalInstallPath)
if !exist {
log.Error("Docker local install file not found: %s", dockerLocalInstallPath)
log.Error("Docker local install file not found: %s", dockerComposeLocalInstallPath)
return
}
// move file to /usr/local/bin
err := utils.MoveFolerToAnother(dockerComposeLocalInstallPath, "/usr/local/bin")
err := utils.MoveFileToAnother(dockerComposeLocalInstallPath, "/usr/local/bin")
if err != nil {
log.Error("Failed to move Docker Compose binaries: %s", err.Error())
return