[agent-go] - 启动时候名称的选择

This commit is contained in:
zeaslity
2024-04-29 15:08:12 +08:00
parent 060c61aefe
commit aba731e719
4 changed files with 85 additions and 6 deletions

View File

@@ -247,10 +247,6 @@ func buildOctopusTCPConnect(agentConfig *viper.Viper) *rabbitmq.RabbitTCPConnect
func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
// host info
agentServerInfo.ServerName = agentInfo.HostInfo.Hostname
agentServerInfo.MachineID = agentInfo.HostInfo.HostID
// cpu part
agentServerInfo.CPUCore = strconv.FormatInt(int64(agentInfo.CPUInfo.NumCores), 10)
if len(agentInfo.CPUInfo.CPUInfo) > 0 {
@@ -289,9 +285,88 @@ func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo
// network part
refreshAgentNetworkInfo(agentInfo, agentServerInfo)
// host info
agentServerInfo.ServerName = uniformAgentServerName(agentInfo, agentServerInfo)
agentServerInfo.MachineID = agentInfo.HostInfo.HostID
log.DebugF("[refreshAgentInfoByStatusInfo] - ok !")
}
func uniformAgentServerName(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) string {
hostname := agentInfo.HostInfo.Hostname
// Shanghai-amd64-01
if strings.Count(hostname, "-") == 2 {
split := strings.Split(hostname, "-")
if split[1] == getMachineType(agentInfo.HostInfo.KernelArch) {
// 第二位是 amd64 arm64
return hostname
}
}
// 不是标准的AgentName格式
city := agentServerInfo.City
city = strings.Title(city)
if strings.Contains(city, " ") {
city = strings.Join(strings.Split(city, " "), "")
}
arch := getMachineType(agentInfo.HostInfo.KernelArch)
var numS string
if agentServerInfo.ServerIPInV4 != "" {
split := strings.Split(agentServerInfo.ServerIPInV4, ".")
numS = fmt.Sprintf("%03d", split[3])
} else {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
// Generate a random number between 1 and 999
num := rand.Intn(999) + 1
// Format the number as a string with leading zeros
numS = fmt.Sprintf("%03d", num)
}
return city + "-" + arch + "-" + numS
}
func getMachineType(arch string) string {
switch {
case strings.HasSuffix(arch, "386") || arch == "i386":
return "ia32"
case strings.HasSuffix(arch, "amd64") || arch == "x86_64":
return "amd64"
case arch == "armv5tel":
return "arm32"
case arch == "armv6l":
return "arm32"
case arch == "armv7" || arch == "armv7l":
return "arm32"
case arch == "armv8" || arch == "aarch64":
return "arm64"
case arch == "mips":
return "mips32"
case arch == "mipsle":
return "mips32le"
case arch == "mips64":
return "mips64"
case arch == "mips64le":
return "mips64le"
case arch == "ppc64":
return "ppc64"
case arch == "ppc64le":
return "ppc64le"
case arch == "riscv64":
return "riscv64"
case arch == "s390x":
return "s390x"
default:
fmt.Println("error: The architecture is not supported.")
return ""
}
}
func refreshAgentNetworkInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
// 获取Agent的公网服务信息