From f124972b844260a4b861e43d7aa37170a7db323d Mon Sep 17 00:00:00 2001 From: zeaslity Date: Sun, 28 Apr 2024 16:41:44 +0800 Subject: [PATCH] =?UTF-8?q?[agent-go]=20-=20=E5=8E=BB=E9=99=A4=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E7=8E=AF=E5=A2=83=E4=BF=A1=E6=81=AF=E7=9A=84=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E8=AE=BE=E7=BD=AE=EF=BC=8C=E4=BC=98=E5=8C=96=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent-common/utils/ReflectUtils.go | 21 +++++++ agent-go/a_agent/AgentServerInfo.go | 1 + agent-go/a_init/AgentInitialization.go | 60 ++++++++++++++++--- agent-go/main.go | 8 +-- agent-operator/deploy/c_app/DeployCmiiApp.go | 7 ++- agent-operator/deploy/z_dep/G.go | 14 ----- .../server/func/TestImageSyncScheduler.java | 2 +- 7 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 agent-common/utils/ReflectUtils.go diff --git a/agent-common/utils/ReflectUtils.go b/agent-common/utils/ReflectUtils.go new file mode 100644 index 0000000..fe637a4 --- /dev/null +++ b/agent-common/utils/ReflectUtils.go @@ -0,0 +1,21 @@ +package utils + +import "reflect" + +// CopySameFields 利用反射,将a中的所有同名字段的值 复制到b中的对应字段 +func CopySameFields(source, target interface{}) { + va := reflect.ValueOf(source).Elem() + vb := reflect.ValueOf(target).Elem() + + for i := 0; i < va.NumField(); i++ { + // 忽略source中 空值的部分 + if va.Field(i).IsZero() { + continue + } + + fieldName := va.Type().Field(i).Name + if vb.FieldByName(fieldName).IsValid() { + vb.FieldByName(fieldName).Set(va.Field(i)) + } + } +} diff --git a/agent-go/a_agent/AgentServerInfo.go b/agent-go/a_agent/AgentServerInfo.go index 3f64541..cf92421 100644 --- a/agent-go/a_agent/AgentServerInfo.go +++ b/agent-go/a_agent/AgentServerInfo.go @@ -28,6 +28,7 @@ type AgentServerInfo struct { KernelArch string `json:"kernelArch"` // native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error IoSpeed string `json:"ioSpeed" yaml:"ioSpeed"` MemoryTotal string `json:"memoryTotal" yaml:"memoryTotal"` + SwapTotal string `json:"swapTotal" yaml:"swapTotal"` DiskTotal string `json:"diskTotal" yaml:"diskTotal"` DiskUsage string `json:"diskUsage" yaml:"diskUsage"` Comment string `json:"comment" yaml:"comment"` diff --git a/agent-go/a_init/AgentInitialization.go b/agent-go/a_init/AgentInitialization.go index 286aba2..c28553f 100644 --- a/agent-go/a_init/AgentInitialization.go +++ b/agent-go/a_init/AgentInitialization.go @@ -5,13 +5,15 @@ import ( "fmt" "github.com/spf13/viper" "gopkg.in/yaml.v3" - "io/ioutil" "math/rand" + "os" "reflect" "regexp" + "strconv" "strings" "time" "wdd.io/agent-common/logger" + "wdd.io/agent-common/utils" "wdd.io/agent-go/a_agent" "wdd.io/agent-go/a_executor" "wdd.io/agent-go/a_status" @@ -19,25 +21,35 @@ import ( "wdd.io/agent-go/rabbitmq" ) +const AgentServerInfoLocalFilePath = "/usr/local/etc/octopus-agent/octopus-agent.conf" + var initOmType = g.InitOmType var P = g.G.P var log = logger.Log -func INIT(octopusAgentConfigFileName string, agentServerInfoConf string) chan bool { +func INIT(octopusAgentConfigFileName string) chan bool { - // 获取系统的环境变量 - agentServerInfo := parseAgentServerInfo(agentServerInfoConf) - // todo totally get from a_status module + // 初始化变量 + agentServerInfo := &a_agent.AgentServerInfo{} - // 初始化Nacos的连接配置 + // 初始化Agent的 RabbitMQ连接信息 agentConfig := parseOctopusAgentConf(octopusAgentConfigFileName) a_agent.AgentConfig = agentConfig - // re-get agentInfo from status module + // 使用a_status模块,自身获取到运行的环境信息 agentInfo := a_status.ReportAgentInfo() refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo) + if utils.FileExistAndNotNull(AgentServerInfoLocalFilePath) { + + // 获取系统的环境变量 + agentServerInfoFromLocalFile := parseAgentServerInfo(AgentServerInfoLocalFilePath) + + // 合并系统环境变量,手动输入的部分会覆盖自身获得内容 + utils.CopySameFields(agentServerInfoFromLocalFile, agentServerInfo) + } + // build operator cache buildAgentOsOperator(agentInfo, agentServerInfo) @@ -235,12 +247,44 @@ 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 { + marshal, _ := json.Marshal(agentInfo.CPUInfo.CPUInfo[0]) + agentServerInfo.CPUBrand = string(marshal) + } + + // os info + agentServerInfo.OSInfo = agentInfo.HostInfo.PlatformFamily + agentInfo.HostInfo.Platform + agentInfo.HostInfo.PlatformVersion + agentServerInfo.OSKernelInfo = agentInfo.HostInfo.KernelVersion + agentServerInfo.Virtualization = agentInfo.HostInfo.VirtualizationSystem + agentInfo.HostInfo.VirtualizationRole 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 + // memory part + agentServerInfo.MemoryTotal = strconv.FormatInt(int64(agentInfo.MemoryInfo.TotalMemory), 10) + agentServerInfo.SwapTotal = strconv.FormatInt(int64(agentInfo.MemoryInfo.SwapTotal), 10) + + // disk part + info := agentInfo.DiskInfo + diskTotal := uint64(0) + diskUsage := uint64(0) + + for _, diskInfo := range info { + if diskInfo.Total > diskTotal { + diskTotal = diskInfo.Total + diskUsage = diskInfo.Used + } + } + agentServerInfo.DiskTotal = strconv.FormatUint(diskTotal, 10) + agentServerInfo.DiskUsage = strconv.FormatUint(diskUsage, 10) + // network part refreshAgentNetworkInfo(agentInfo, agentServerInfo) @@ -357,7 +401,7 @@ func parseAgentServerInfo(agentServerInfoConf string) *a_agent.AgentServerInfo { // 约定文件地址为 /octopus-agent/octopus-agent.conf var agentServerInfo *a_agent.AgentServerInfo - yamlFile, err := ioutil.ReadFile(agentServerInfoConf) + yamlFile, err := os.ReadFile(agentServerInfoConf) if err != nil { panic(fmt.Errorf("failed to read YAML file: %v", err)) diff --git a/agent-go/main.go b/agent-go/main.go index 52b35a8..f7a5f60 100644 --- a/agent-go/main.go +++ b/agent-go/main.go @@ -13,11 +13,11 @@ func main() { // 解析命令行参数 var version string - var agentServerInfoConfFile string + //var agentServerInfoConfFile string var mode string flag.StringVar(&version, "version", "", "config file version") flag.StringVar(&mode, "mode", "agent", "agent run mode") - flag.StringVar(&agentServerInfoConfFile, "agentServerInfoConfFile", "", "agent server info conf file") + //flag.StringVar(&agentServerInfoConfFile, "agentServerInfoConfFile", "", "agent server info conf file") flag.Parse() if mode == "bastion" { @@ -30,10 +30,10 @@ func main() { // 读取对应版本的配置文件 octopusAgentConfigFileName := fmt.Sprintf("octopus-agent-%s.yaml", version) fmt.Println("config file name is => " + octopusAgentConfigFileName) - fmt.Println("agent server info file is => " + agentServerInfoConfFile) + //fmt.Println("agent server info file is => " + agentServerInfoConfFile) // 执行初始化之策工作 - businessForeverChan := a_init.INIT(octopusAgentConfigFileName, agentServerInfoConfFile) + businessForeverChan := a_init.INIT(octopusAgentConfigFileName) // 永远等待 runtime的队列消息 <-businessForeverChan diff --git a/agent-operator/deploy/c_app/DeployCmiiApp.go b/agent-operator/deploy/c_app/DeployCmiiApp.go index b2a68a0..4cda66a 100644 --- a/agent-operator/deploy/c_app/DeployCmiiApp.go +++ b/agent-operator/deploy/c_app/DeployCmiiApp.go @@ -4,6 +4,7 @@ import ( "github.com/go-playground/validator/v10" "os" "wdd.io/agent-common/logger" + "wdd.io/agent-common/utils" "wdd.io/agent-operator/deploy/z_dep" ) @@ -55,7 +56,7 @@ func init() { func (backend *CmiiBackendConfig) BackendDeploy(common *z_dep.CommonEnvironmentConfig) bool { // copy - z_dep.CopySameFields(common, backend) + utils.CopySameFields(common, backend) validate := validator.New() err := validate.Struct(backend) @@ -84,7 +85,7 @@ func (backend *CmiiBackendConfig) BackendDeploy(common *z_dep.CommonEnvironmentC func (frontend *CmiiFrontendConfig) FrontendDeploy(common *z_dep.CommonEnvironmentConfig) bool { // copy - z_dep.CopySameFields(common, frontend) + utils.CopySameFields(common, frontend) validate := validator.New() err := validate.Struct(frontend) @@ -106,7 +107,7 @@ func (frontend *CmiiFrontendConfig) FrontendDeploy(common *z_dep.CommonEnvironme func (frontend *CmiiFrontendConfig) ConfigMapDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool { // copy - z_dep.CopySameFields(commonEnv, frontend) + utils.CopySameFields(commonEnv, frontend) // manual validate if frontend.ShortName == "" || frontend.ClientId == "" { diff --git a/agent-operator/deploy/z_dep/G.go b/agent-operator/deploy/z_dep/G.go index d5cdf16..68f0af1 100644 --- a/agent-operator/deploy/z_dep/G.go +++ b/agent-operator/deploy/z_dep/G.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "github.com/go-playground/validator/v10" - "reflect" "runtime" "text/template" "wdd.io/agent-common/assert" @@ -104,16 +103,3 @@ func ParseEnvToApplyFile(environment any, applyTemplate string, applyFilePath st return true } - -// CopySameFields 利用反射,将a中的所有同名字段的值 复制到b中的对应字段 -func CopySameFields(a, b interface{}) { - va := reflect.ValueOf(a).Elem() - vb := reflect.ValueOf(b).Elem() - - for i := 0; i < va.NumField(); i++ { - fieldName := va.Type().Field(i).Name - if vb.FieldByName(fieldName).IsValid() { - vb.FieldByName(fieldName).Set(va.Field(i)) - } - } -} diff --git a/server/src/test/java/io/wdd/server/func/TestImageSyncScheduler.java b/server/src/test/java/io/wdd/server/func/TestImageSyncScheduler.java index 2c4e8a6..5c54eb7 100644 --- a/server/src/test/java/io/wdd/server/func/TestImageSyncScheduler.java +++ b/server/src/test/java/io/wdd/server/func/TestImageSyncScheduler.java @@ -44,7 +44,7 @@ public class TestImageSyncScheduler { ArrayList ImageFullNameList = new ArrayList<>(List.of( // "harbor.cdcyy.com.cn/cmii/cmii-live-operator:5.2.0", // "harbor.cdcyy.com.cn/cmii/cmii/srs:v5.0.195" - "harbor.cdcyy.com.cn/cmii/cmii-uav-developer:5.4.0-cqly-042801" + "harbor.cdcyy.com.cn/cmii/cmii-uav-industrial-portfolio:5.4.0-cqly-042802" )); Boolean downloadAndCompressOnly = false;