This commit is contained in:
zeaslity
2025-02-26 17:49:03 +08:00
parent c751c21871
commit e8f0e0d4a9
3 changed files with 58 additions and 4 deletions

View File

@@ -20,15 +20,18 @@ var (
"deltarpm", "net-tools", "iputils", "bind-utils", "lsof", "curl", "wget", "vim", "mtr", "htop", "deltarpm", "net-tools", "iputils", "bind-utils", "lsof", "curl", "wget", "vim", "mtr", "htop",
} }
dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径 dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-amd64" // 本地安装docker compose的文件路径
) )
func init() { func init() {
switch runtime.GOARCH { switch runtime.GOARCH {
case "amd64": case "amd64":
dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径 dockerLocalInstallPath = "/root/wdd/docker-amd64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-amd64" // 本地安装docker compose的文件路径
case "arm64": case "arm64":
dockerLocalInstallPath = "/root/wdd/docker-arm64-20.10.15.tgz" // 本地安装docker的文件路径 dockerLocalInstallPath = "/root/wdd/docker-arm64-20.10.15.tgz" // 本地安装docker的文件路径
dockerComposeLocalInstallPath = "/root/wdd/docker-compose-v2.18.0-linux-arm64" // 本地安装docker compose的文件路径
} }
} }
@@ -293,6 +296,13 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
Short: "卸载Docker Compose", Short: "卸载Docker Compose",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
log.Info("Removing Docker Compose...") log.Info("Removing Docker Compose...")
if utils.RemoveFile("/usr/local/bin/docker-compose") {
log.Info("Docker Compose removed successfully!")
} else {
log.Error("Failed to remove Docker Compose!")
}
}, },
} }
@@ -302,6 +312,29 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
log.Info("Installing Docker Compose from local file...") log.Info("Installing Docker Compose from local file...")
exist := utils.FileExistAndNotNull(dockerLocalInstallPath)
if !exist {
log.Error("Docker local install file not found: %s", dockerLocalInstallPath)
return
}
// move file to /usr/local/bin
err := utils.MoveFolerToAnother(dockerComposeLocalInstallPath, "/usr/local/bin")
if err != nil {
log.Error("Failed to move Docker Compose binaries: %s", err.Error())
return
}
// set permission
chmodCmd := []string{"chmod", "+x", "/usr/local/bin/docker-compose"}
ok, resultLog := op.SingleLineCommandExecutor(chmodCmd)
if !ok {
log.Error("Failed to set permissions for Docker Compose binaries: %s", resultLog)
return
}
log.Info("Docker Compose installed successfully from local file!")
}, },
} }
@@ -310,6 +343,12 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
Short: "查看Docker Compose版本", Short: "查看Docker Compose版本",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
log.Info("Docker Compose version...") log.Info("Docker Compose version...")
ok, resultLog := op.SingleLineCommandExecutor([]string{"docker-compose", "--version"})
if !ok {
log.Error("Failed to get Docker Compose version: %s", resultLog)
return
}
log.Info("Docker Compose version: %s", resultLog)
}, },
} }

View File

@@ -1,4 +1,4 @@
package op package utils
import ( import (
"fmt" "fmt"

View File

@@ -236,6 +236,21 @@ func listAllFileInFolderWithFullPath(folderName string, fullPath bool) ([]string
return files, nil return files, nil
} }
// RemoveFile 删除文件如果文件不存在则返回true
func RemoveFile(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
log.Error("文件不存在: %s", filePath)
return true
}
err := os.Remove(filePath)
if err != nil {
log.Error("Failed to remove file: %s", err.Error())
return false
}
return true
}
func RemoveFolderComplete(folderName string) bool { func RemoveFolderComplete(folderName string) bool {
err := filepath.Walk(folderName, err := filepath.Walk(folderName,