[agent-go] [Bastion] - basic accomplished !
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"wdd.io/agent-common/logger"
|
||||
@@ -233,15 +234,67 @@ func buildOctopusTCPConnect(agentConfig *viper.Viper) *rabbitmq.RabbitTCPConnect
|
||||
|
||||
func refreshAgentInfoByStatusInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
|
||||
|
||||
// host info
|
||||
agentServerInfo.Platform = agentInfo.HostInfo.Platform
|
||||
agentServerInfo.PlatformFamily = agentInfo.HostInfo.PlatformFamily
|
||||
agentServerInfo.PlatformVersion = agentInfo.HostInfo.PlatformVersion
|
||||
agentServerInfo.KernelVersion = agentInfo.HostInfo.KernelVersion
|
||||
agentServerInfo.KernelArch = agentInfo.HostInfo.KernelArch
|
||||
|
||||
// network part
|
||||
refreshAgentNetworkInfo(agentInfo, agentServerInfo)
|
||||
|
||||
log.DebugF("[refreshAgentInfoByStatusInfo] - ok !")
|
||||
}
|
||||
|
||||
func refreshAgentNetworkInfo(agentInfo *a_status.AgentInfo, agentServerInfo *a_agent.AgentServerInfo) {
|
||||
|
||||
// 测试网卡名称
|
||||
//testCases := []string{"ens33", "eno1", "enp0s3", "enp1s2", "eth0", "enp2s5", "enx1234567890ab", "ens1234567890ab", "enp1234567890ab", "enp1234567890ab", "enp1", "lo","","docker0", "virbr0", "veth0",}
|
||||
//for _, tc := range testCases {
|
||||
// fmt.Printf("Network interface '%s' is %s\n", tc, fmt.Sprintf("%v", isNetworkInterface(tc)))
|
||||
//}
|
||||
|
||||
// inner ip v4 v6
|
||||
for _, networkInfo := range agentInfo.NetworkInfo {
|
||||
if isNetworkInterface(networkInfo.Name) {
|
||||
log.InfoF("refreshAgentNetworkInfo - network interface is %v", networkInfo)
|
||||
if networkInfo.InternalIPv4 == nil || len(networkInfo.InternalIPv4) == 0 {
|
||||
continue
|
||||
}
|
||||
// 通配到对应的网卡
|
||||
s := networkInfo.InternalIPv4[0]
|
||||
if strings.Contains(s, "/") {
|
||||
s = strings.Split(s, "/")[0]
|
||||
}
|
||||
agentServerInfo.ServerIPInV4 = s
|
||||
|
||||
if networkInfo.InternalIPv6 == nil || len(networkInfo.InternalIPv6) == 0 {
|
||||
continue
|
||||
}
|
||||
s2 := networkInfo.InternalIPv6[0]
|
||||
if strings.Contains(s2, "/") {
|
||||
s2 = strings.Split(s2, "/")[0]
|
||||
}
|
||||
agentServerInfo.ServerIPInV6 = s2
|
||||
|
||||
if agentServerInfo.ServerIPInV4 != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// isNetworkInterface 检查网卡名称是否符合给定的模式(ens, eno或enp开头,或者像enp1s2这样的格式)。
|
||||
func isNetworkInterface(networkInterface string) bool {
|
||||
// 正则表达式匹配 ens 或 eno 开头的字符串
|
||||
// 或者以 enp 开头,后面跟着至少一个数字,一个连字符,再是一个数字,最后可以有一个可选的连字符和数字组合(如enp0s3或enp1s2)
|
||||
pattern := `^(ens|eno|eth|enp)[0-9]+$|enp+[0-9]s+[0-9]$`
|
||||
re := regexp.MustCompile(pattern)
|
||||
return re.MatchString(networkInterface)
|
||||
}
|
||||
|
||||
// handleInitMsgFromServer 处理从Server接收的 注册信息
|
||||
func handleInitMsgFromServer(initFromServerMsg *rabbitmq.OctopusMessage, initToServerQueue *rabbitmq.RabbitQueue, agentServerInfo *a_agent.AgentServerInfo) bool {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user