Enhance Docker Installation and Management Commands

- Improved Docker installation process for Ubuntu systems
- Added support for dynamic Docker version detection
- Enhanced Docker local and online installation commands
- Implemented more robust Docker removal functionality
- Updated Docker installation to use system-specific package sources
- Added better error handling and logging for Docker operations
- Refined Docker service startup and configuration checks
This commit is contained in:
zeaslity
2025-02-28 17:45:12 +08:00
parent bffb643a56
commit 5c39bd7594
16 changed files with 586 additions and 86 deletions

View File

@@ -1,10 +1,13 @@
package utils
import (
"agent-wdd/log"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
@@ -18,7 +21,19 @@ func DownloadFile(url string, path string) (bool, string) {
return DownloadFileWithClient(client, url, path)
}
// DownloadFileWithClient 使用http客户端下载文件
func DownloadFileWithClient(client *http.Client, url string, path string) (bool, string) {
// path如果是一个目录则需要获取文件名
// 获取url使用 / 分割最后的一部分
// 如果path是目录则需要获取文件名
if IsDirOrFile(path) {
fileName := strings.Split(url, "/")[len(strings.Split(url, "/"))-1]
path = filepath.Join(path, fileName)
log.Info("path是目录自动获取文件名为 => : %s", path)
}
return downloadWithProgress(client, url, path)
}
@@ -27,14 +42,14 @@ func downloadWithProgress(client *http.Client, url, dest string) (bool, string)
// 创建目标文件
file, err := os.Create(dest)
if err != nil {
return false, fmt.Sprintf("创建文件失败: %w", err)
return false, fmt.Sprintf("创建文件失败: %s", err.Error())
}
defer file.Close()
// 发起请求
resp, err := client.Get(url)
if err != nil {
return false, fmt.Sprintf("HTTP请求失败: %w", err)
return false, fmt.Sprintf("HTTP请求失败: %s", err.Error())
}
defer resp.Body.Close()
@@ -46,6 +61,13 @@ func downloadWithProgress(client *http.Client, url, dest string) (bool, string)
size := resp.ContentLength
var downloaded int64
// 不支持下载超过10GB的文件
if size > 10*1024*1024*1024 || size < 0 {
log.Error("文件大小超过10GB或者文件大小未知不支持高级下载方式! 尝试使用wget下载")
return downloadFileByWget(url, dest)
}
// 打印下载信息
fmt.Printf("开始下载: %s 大小: %s\n", url, HumanSizeInt(size))
@@ -60,13 +82,32 @@ func downloadWithProgress(client *http.Client, url, dest string) (bool, string)
// 执行拷贝
if _, err := io.Copy(file, progressReader); err != nil {
return false, fmt.Sprintf("文件拷贝失败: %w", err)
return false, fmt.Sprintf("文件拷贝失败: %s", err.Error())
}
fmt.Print("\n") // 保持最后进度显示的完整性
return true, fmt.Sprintf("文件下载成功: %s", dest)
}
// 使用wget下载文件
func downloadFileByWget(url, dest string) (bool, string) {
log.Info("使用wget下载文件: %s", fmt.Sprintf("wget %s -qO %s", url, dest))
cmd := exec.Command("wget", url, "-qO", dest)
_, err := cmd.CombinedOutput()
if err != nil {
return false, fmt.Sprintf("wget下载失败: %s", err.Error())
}
// 检查文件是否存在且不为空
fileInfo, err := os.Stat(dest)
if err != nil || fileInfo.Size() == 0 {
return false, fmt.Sprintf("wget下载失败 文件不存在或为空: %s", dest)
}
return true, fmt.Sprintf("wget下载成功: %s", dest)
}
// 进度跟踪Reader
type progressReader struct {
io.Reader