package cmd import ( "agent-wdd/config" "agent-wdd/log" "agent-wdd/op" "agent-wdd/utils" "fmt" "github.com/spf13/cobra" ) var ( ubuntuCommonTools = []string{ "iputils-ping", "net-tools", "dnsutils", "lsof", "curl", "wget", "mtr-tiny", "vim", "htop", "lrzsz", } centosCommonTools = []string{ "deltarpm", "net-tools", "iputils", "bind-utils", "lsof", "curl", "wget", "vim", "mtr", "htop", } dockerLocalInstallPath = "/root/wdd/docker-20.10.15.tgz" // 本地安装docker的文件路径 ) // 添加base子命令 func addBaseSubcommands(cmd *cobra.Command) { // 1.1 docker dockerCmd := &cobra.Command{ Use: "docker", Short: "Docker相关操作", } addDockerSubcommands(dockerCmd) // 1.2 dockercompose dockerComposeCmd := &cobra.Command{ Use: "dockercompose", Short: "Docker Compose相关操作", } addDockerComposeSubcommands(dockerComposeCmd) // 其他base子命令... // 通用工具安装 commonToolsInstall := &cobra.Command{ Use: "tools", Short: "通用工具安装 利用本机的yum,apt等从网络安装常用的软件", Run: func(cmd *cobra.Command, args []string) { log.Info("Common tool installation!") // Whether It can connect to internet if config.CanConnectInternet() <= 1 { log.Error("服务器无法连接互联网,无法执行tools") return } // package install // only support ubuntu(debian) centos(debian openEuler) packOperator := op.AgentPackOperator packOperator.PackageInit() os := config.ConfigCache.Agent.OS if os.IsUbuntuType { packOperator.Install(ubuntuCommonTools) } else { packOperator.Install(centosCommonTools) } }, } cmd.AddCommand( dockerCmd, dockerComposeCmd, commonToolsInstall, // 其他命令... ) } // 添加docker子命令 func addDockerSubcommands(cmd *cobra.Command) { onlineCmd := &cobra.Command{ Use: "online [version]", Short: "网络安装Docker", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { // 检查参数 if len(args) > 0 { fmt.Printf("Installing Docker version: %s\n", args[0]) } // 没有传递参数,使用默认参数 version := "26.0.7" log.Info("Installing Docker version: %s", version) // 安装docker packOperator := op.AgentPackOperator packOperator.PackageInit() // ubuntu 和 centos的安装命令是不是一样的 packOperator.Install([]string{"docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", "docker-compose-plugin"}) }, } removeCmd := &cobra.Command{ Use: "remove", Short: "卸载Docker", Run: func(cmd *cobra.Command, args []string) { fmt.Println("Removing Docker...") }, } localCmd := &cobra.Command{ Use: "local [path]", Short: "本地安装Docker", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { exist := utils.FileExistAndNotNull(dockerLocalInstallPath) if !exist { log.Error("Docker local install file not found: %s", dockerLocalInstallPath) return } // 解压文件 utils.Unzip(dockerLocalInstallPath, "/root/wdd") }, } cmd.AddCommand(onlineCmd, removeCmd, localCmd) } func addDockerComposeSubcommands(cmd *cobra.Command) { }