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

@@ -50,15 +50,16 @@ type Agent struct {
}
type OS struct {
Hostname string `yaml:"hostname"`
OsName string `yaml:"os_name"`
OsFamily string `yaml:"os_family"`
OsVersion string `yaml:"os_version"`
OsType string `yaml:"os_type"`
Kernel string `yaml:"kernel"`
Arch string `yaml:"arch"`
IsUbuntuType bool `yaml:"is_ubuntu_type" comment:"是否是ubuntu类型的操作系统"`
PackInit bool `yaml:"pack_init" comment:"是否初始化ubuntu需要"`
Hostname string `mapstructure:"hostname" yaml:"hostname" comment:"主机名"`
OsName string `mapstructure:"os_name" yaml:"os_name" comment:"操作系统名称"`
OsFamily string `mapstructure:"os_family" yaml:"os_family" comment:"操作系统家族"`
OsVersion string `mapstructure:"os_version" yaml:"os_version" comment:"操作系统版本"`
OsType string `mapstructure:"os_type" yaml:"os_type" comment:"操作系统类型"`
Kernel string `mapstructure:"kernel" yaml:"kernel" comment:"内核版本"`
Arch string `mapstructure:"arch" yaml:"arch" comment:"架构"`
IsUbuntuType bool `mapstructure:"is_ubuntu_type" yaml:"is_ubuntu_type" comment:"是否是ubuntu类型的操作系统"`
PackInit bool `mapstructure:"pack_init" yaml:"pack_init" comment:"是否初始化ubuntu需要"`
OSReleaseCode string `mapstructure:"os_release_code" yaml:"os_release_code" comment:"主机操作系统的发行版代号, focal之类的"`
}
type Network struct {
@@ -68,11 +69,12 @@ type Network struct {
}
type PublicInfo struct {
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Country string `yaml:"country"`
City string `yaml:"city"`
ASN string `yaml:"asn"`
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Country string `yaml:"country"`
City string `yaml:"city"`
ASN string `yaml:"asn"`
Timezone string `yaml:"timezone"`
}
type Interface struct {
@@ -122,7 +124,6 @@ func InitConfig() {
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
log.Error("读取配置文件失败: %w", err)
panic(err)

View File

@@ -46,7 +46,7 @@ func GetInterfaces() []Interface {
// 获取所有网卡信息
netInterfaces, err := net.Interfaces()
log.Info("all network interfaces: %v", netInterfaces)
// log.Info("all network interfaces: %v", netInterfaces)
if err != nil {
log.Error("获取网卡信息失败: %v", err)
return interfaces
@@ -120,17 +120,35 @@ func judgeCanConnectInternet() int {
Timeout: 3 * time.Second,
}
_, err := client.Get("https://www.google.com")
if err == nil {
return 9
results := make(chan int, 2)
go func() {
_, err := client.Get("https://www.google.com")
if err == nil {
results <- 9
} else {
results <- 1
}
}()
go func() {
_, err := client.Get("https://www.baidu.com")
if err == nil {
results <- 7
} else {
results <- 1
}
}()
maxResult := 1
for i := 0; i < 2; i++ {
result := <-results
if result > maxResult {
maxResult = result
}
}
_, err = client.Get("https://www.baidu.com")
if err == nil {
return 7
}
return 1
return maxResult
}
// GetPublicInfo 获取服务器的公网信息
@@ -194,13 +212,14 @@ func (p PublicInfo) GetPublicInfo() PublicInfo {
}
// 打印解析结果
log.Info("IP信息:\n%+v\n", info)
// log.Info("IP信息:\n%+v\n", info)
// 保存信息
p.IPv4 = info.IP
p.ASN = info.Org
p.Country = info.Country
p.City = info.City
p.Timezone = info.Timezone
ConfigCache.Agent.Network.Public = p

View File

@@ -26,7 +26,8 @@ func (o *OS) Gather() {
o.Hostname = hostname
}
o.OsType = "linux" // 固定为linux
o.OsType = "linux" // 固定为linux
o.IsUbuntuType = true // 默认为ubuntu
// 解析系统信息
file, err := os.Open("/etc/os-release")
@@ -78,12 +79,30 @@ func (o *OS) Gather() {
}
// 检查包管理的方式
c := exec.Command("command", "-v", "apt")
_, err = c.Output()
if err == nil {
o.IsUbuntuType = true
if strings.Contains(o.OsFamily, "centos") || strings.Contains(o.OsFamily, "rhel") {
o.IsUbuntuType = false
}
// 获取系统架构
o.Arch = runtime.GOARCH
// 获取系统发行版代号
o.OSReleaseCode = judgeUbuntuReleaseFromOsVersion(o.OsVersion)
}
func judgeUbuntuReleaseFromOsVersion(osVersion string) string {
switch osVersion {
case "16.04":
return "xenial"
case "18.04":
return "bionic"
case "20.04":
return "focal"
case "22.04":
return "jammy"
default:
return "ubuntu-unknown"
}
}