From fb38ab8f15fca26308a0ad3f4e0f19af17b9d9e2 Mon Sep 17 00:00:00 2001 From: IceDerce Date: Sun, 25 Jun 2023 11:26:33 +0800 Subject: [PATCH] [Excution] - base function start - 3 --- .../{register => }/AgentInitialization.go | 21 ++-- agent-go/{rabbitmq => }/OctopusMessage.go | 11 +- agent-go/executor/BaseFunction.go | 101 ++++++++++++++---- agent-go/executor/BaseFunction_test.go | 40 +++++-- agent-go/g/global.go | 2 + agent-go/main.go | 2 +- agent-go/rabbitmq/OMsgConnector.go | 7 +- 7 files changed, 136 insertions(+), 48 deletions(-) rename agent-go/{register => }/AgentInitialization.go (93%) rename agent-go/{rabbitmq => }/OctopusMessage.go (93%) diff --git a/agent-go/register/AgentInitialization.go b/agent-go/AgentInitialization.go similarity index 93% rename from agent-go/register/AgentInitialization.go rename to agent-go/AgentInitialization.go index f0a02a0..40c82e6 100644 --- a/agent-go/register/AgentInitialization.go +++ b/agent-go/AgentInitialization.go @@ -1,9 +1,9 @@ -package register +package main import ( "agent-go/g" - logger2 "agent-go/logger" "agent-go/rabbitmq" + "agent-go/register" "encoding/json" "fmt" "gopkg.in/yaml.v3" @@ -12,12 +12,11 @@ import ( ) var omType = g.InitOmType -var log = logger2.Log var P = g.G.P -var AgentServerInfoCache = &AgentServerInfo{} +var AgentServerInfoCache = ®ister.AgentServerInfo{} -func INIT() *AgentServerInfo { +func INIT() *register.AgentServerInfo { // 获取系统的环境变量 agentServerInfo := parseAgentServerInfo() @@ -48,7 +47,7 @@ func INIT() *AgentServerInfo { initToServerQueue.Connect() // 组装OctopusMessage - var octopusMsg *rabbitmq.OctopusMessage + var octopusMsg *OctopusMessage octopusMsg = octopusMsg.Build( omType, agentServerInfo, @@ -92,7 +91,7 @@ func INIT() *AgentServerInfo { } // handleInitMsgFromServer 处理从Server接收的 注册信息 -func handleInitMsgFromServer(initFromServerQueue *rabbitmq.RabbitQueue, initToServerQueue *rabbitmq.RabbitQueue, agentServerInfo *AgentServerInfo) { +func handleInitMsgFromServer(initFromServerQueue *rabbitmq.RabbitQueue, initToServerQueue *rabbitmq.RabbitQueue, agentServerInfo *register.AgentServerInfo) { initOctopusMessageDeliveries := initFromServerQueue.Read(false) @@ -104,14 +103,14 @@ func handleInitMsgFromServer(initFromServerQueue *rabbitmq.RabbitQueue, initToSe log.Debug(fmt.Sprintf("message received from server is %s", string(delivery.Body))) - var initOctopusMsg *rabbitmq.OctopusMessage + var initOctopusMsg *OctopusMessage err := json.Unmarshal(delivery.Body, &initOctopusMsg) if err != nil { log.Error(fmt.Sprintf("parse init message from server wroong, message is => %s ", string(delivery.Body))) } - var serverInfo AgentServerInfo + var serverInfo register.AgentServerInfo s, _ := initOctopusMsg.Content.(string) cc := json.Unmarshal([]byte(s), &serverInfo) @@ -161,11 +160,11 @@ func shutdownRegisterQueueConnection(initFromServerQueue *rabbitmq.RabbitQueue, log.InfoF("Pretend to Shutdown register queue connection !") } -func parseAgentServerInfo() *AgentServerInfo { +func parseAgentServerInfo() *register.AgentServerInfo { // 约定文件地址为 /etc/environment.d/octopus-agent.conf // 目前使用 - var agentServerInfo *AgentServerInfo + var agentServerInfo *register.AgentServerInfo //yamlFile, err := ioutil.ReadFile("C:\\Users\\wdd\\IdeaProjects\\ProjectOctopus\\agent-go\\server-env.yaml") yamlFile, err := ioutil.ReadFile("server-env.yaml") diff --git a/agent-go/rabbitmq/OctopusMessage.go b/agent-go/OctopusMessage.go similarity index 93% rename from agent-go/rabbitmq/OctopusMessage.go rename to agent-go/OctopusMessage.go index 69a0c1f..045fa60 100644 --- a/agent-go/rabbitmq/OctopusMessage.go +++ b/agent-go/OctopusMessage.go @@ -1,8 +1,9 @@ -package rabbitmq +package main import ( "agent-go/executor" "agent-go/g" + "agent-go/rabbitmq" "agent-go/status" "agent-go/utils" "encoding/json" @@ -10,8 +11,6 @@ import ( "strings" ) -var P = g.G.P - type IOctopusMessage interface { OctopusMsgHandler OctopusMsgSender @@ -23,7 +22,7 @@ type OctopusMsgHandler interface { } type OctopusMsgSender interface { - Send(rabbitQueue *RabbitQueue, msg []byte) + Send(rabbitQueue *rabbitmq.RabbitQueue, msg []byte) } type OctopusMsgBuilder interface { @@ -44,7 +43,7 @@ func (om *OctopusMessage) Handle() { doHandleOctopusMessage(om) } -func (om *OctopusMessage) Send(rabbitQueue *RabbitQueue, msg []byte) { +func (om *OctopusMessage) Send(rabbitQueue *rabbitmq.RabbitQueue, msg []byte) { rabbitQueue.Send(msg) } @@ -148,7 +147,7 @@ func statusOMHandler(octopusMessage *OctopusMessage) { octopusMessage.Result = statusRes // 发送回去 statusOctopusReplayMessage, _ := json.Marshal(octopusMessage) - OctopusToServerQueue.Send(statusOctopusReplayMessage) + rabbitmq.OctopusToServerQueue.Send(statusOctopusReplayMessage) // 输出日志 log.InfoF("接收到查询Agent状态的请求,结果为 => %s", statusRes) diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index a8d8250..d0cdf9a 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -1,6 +1,7 @@ package executor import ( + "agent-go/g" "agent-go/register" "strings" ) @@ -52,6 +53,9 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string case "removeDocker": multiLineCommand = op.removeDocker() break + case "removeDockerCompose": + multiLineCommand = op.removeDockerCompose() + break case "installDockerCompose": multiLineCommand = op.installDockerCompose() break @@ -329,11 +333,52 @@ func (op *AgentOsOperator) installDockerCompose() [][]string { } func (op *AgentOsOperator) installHelm() [][]string { - var installHelmFunc [][]string + installHelmFunc := [][]string{ + { + "mkdir", + "-p", + "/root/wdd/", + }, + { + "rm", + "-rf", + "/root/wdd/helm-v*", + }, + { + "rm", + "-rf", + "/root/wdd/linux-amd64", + }, + { + "wget", + "--no-check-certificate", + g.BaseFuncOssUrlPrefix + "helm-v3.12.1-linux-amd64.tar.gz", + "-O", + "/root/wdd/helm-v3.12.1-linux-amd64.tar.gz", + }, + { + "tar", + "-zvxf", + "/root/wdd/helm-v3.12.1-linux-amd64.tar.gz", + }, + { + "chmod", + "+x", + "/root/wdd/linux-amd64/helm", + }, + { + "mv", + "/root/wdd/linux-amd64/helm", + "/usr/local/bin/helm", + }, + { + "helm", + "version", + }, + } - if op.IsOsTypeUbuntu { + /*if op.IsOsTypeUbuntu { installHelmFunc = [][]string{ - { "curl", "-o", @@ -357,7 +402,7 @@ func (op *AgentOsOperator) installHelm() [][]string { } } else { log.ErrorF("Operation OS is CentOS, Helm not installed!") - } + }*/ return installHelmFunc } @@ -374,7 +419,7 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { }, { "wget", - "https://oss2.demo.uavcmlc.com:18000/wangdada/daemon-config.json", + g.BaseFuncOssUrlPrefix + "daemon-config.json", "-O", "/etc/docker/daemon.json", }, @@ -397,50 +442,62 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string { func (op *AgentOsOperator) installHarbor(args []string) [][]string { installHarborFunc := [][]string{ - { - "wget", - "--no-check-certificate", - "https://oss2.demo.uavcmlc.com:18000/wangdada/harbor-offline-installer-v2.1.0.tgz", - "-O", - "harbor-offline-installer-v2.1.0.tgz", - }, + //{ + // "mkdir", + // "-p", + // "/root/wdd/", + //}, + //{ + // "rm", + // "-rf", + // "/root/wdd/harbor-offline-installer-v2.1.0.tgz", + //}, + //{ + // "wget", + // "--no-check-certificate", + // g.BaseFuncOssUrlPrefix + "harbor-offline-installer-v2.1.0.tgz", + // "-O", + // "/root/wdd/harbor-offline-installer-v2.1.0.tgz", + //}, { "tar", "-zvxf", - "harbor-offline-installer-v2.1.0.tgz", + "/root/wdd/harbor-offline-installer-v2.1.0.tgz", + "-C", + "/root/wdd/", }, { "rm", "-rf", - "./harbor/harbor.yml", + "/root/wdd/harbor/harbor.yml", }, { "wget", "--no-check-certificate", - "https://oss2.demo.uavcmlc.com:18000/wangdada/harbor.yml", + g.BaseFuncOssUrlPrefix + "harbor-config-template.yml", "-O", - "./harbor/harbor.yml", + "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostName/" + op.AgentServerInfo.ServerIPInV4 + "/g", - "./harbor/harbor.yml", + "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostPort/8033/g", - "./harbor/harbor.yml", + "/root/wdd/harbor/harbor.yml", }, { "sed", "-i", "s/$HarborHostPort/V2ryStr@ngPss/g", - "./harbor/harbor.yml", + "/root/wdd/harbor/harbor.yml", }, { - "./harbor/install.sh", + "/root/wdd/harbor/install.sh", "--with-chartmuseum", }, } @@ -488,7 +545,7 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string { { "sed", "-i", - "s/server 0.centos.pool.ntp.org iburst/server ntp2.aliyun.com iburst/g", + "s/pool ntp.ubuntu.com iburst/server ntp2.aliyun.com iburst/g", chronyFile, }, { @@ -505,6 +562,8 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string { "-n", "sources", "-v", + }, + { "chronyc", "tracking", }, diff --git a/agent-go/executor/BaseFunction_test.go b/agent-go/executor/BaseFunction_test.go index e2dc4af..1002a11 100644 --- a/agent-go/executor/BaseFunction_test.go +++ b/agent-go/executor/BaseFunction_test.go @@ -1,6 +1,9 @@ package executor -import "testing" +import ( + "agent-go/register" + "testing" +) var agentOP = AgentOsOperator{ InstallCommandPrefix: []string{ @@ -12,6 +15,30 @@ var agentOP = AgentOsOperator{ IsAgentInnerWall: true, AgentArch: "amd64", AgentOSReleaseCode: "focal", + AgentServerInfo: register.AgentServerInfo{ + ServerName: "", + ServerIPPbV4: "", + ServerIPInV4: "192.168.0.8", + ServerIPPbV6: "", + ServerIPInV6: "", + Location: "", + Provider: "", + ManagePort: "", + CPUCore: "", + CPUBrand: "", + OSInfo: "", + OSKernelInfo: "", + TCPControl: "", + Virtualization: "", + IoSpeed: "", + MemoryTotal: "", + DiskTotal: "", + DiskUsage: "", + Comment: "", + MachineID: "", + AgentVersion: "", + TopicName: "", + }, } func TestBaseFunc(t *testing.T) { @@ -22,11 +49,12 @@ func TestBaseFunc(t *testing.T) { //agentOP.Exec("enableSwap") //agentOP.Exec("removeDocker") //agentOP.Exec("installDocker", "20") - agentOP.Exec("installDockerCompose") - agentOP.Exec("installHelm") - agentOP.Exec("installHarbor") - agentOP.Exec("chronyToPublicNTP") - agentOP.Exec("chronyToMaster", "192.168.0.8") + //agentOP.Exec("removeDockerCompose") + //agentOP.Exec("installDockerCompose") + //agentOP.Exec("installHelm") + //agentOP.Exec("installHarbor") + //agentOP.Exec("chronyToPublicNTP") + //agentOP.Exec("chronyToMaster", "192.168.0.8") agentOP.Exec("installZSH") } diff --git a/agent-go/g/global.go b/agent-go/g/global.go index 4c35d06..e070da7 100644 --- a/agent-go/g/global.go +++ b/agent-go/g/global.go @@ -19,6 +19,8 @@ const ( StatusOmType = "STATUS" InitOmType = "INIT" AgentOmType = "AGENT" + + BaseFuncOssUrlPrefix = "https://b2.107421.xyz/" ) var pool, _ = ants.NewPool(100, ants.WithNonblocking(false), ants.WithLogger(logger2.Log), ants.WithMaxBlockingTasks(30), ants.WithDisablePurge(true)) diff --git a/agent-go/main.go b/agent-go/main.go index 2784f0e..47f0ad9 100644 --- a/agent-go/main.go +++ b/agent-go/main.go @@ -24,6 +24,6 @@ func main() { g.G.AgentConfig = register.ParseConfiguration(filename) // 执行初始化之策工作 - register.AgentServerInfoCache = register.INIT() + AgentServerInfoCache = INIT() } diff --git a/agent-go/rabbitmq/OMsgConnector.go b/agent-go/rabbitmq/OMsgConnector.go index 4cd4c85..cd72e29 100644 --- a/agent-go/rabbitmq/OMsgConnector.go +++ b/agent-go/rabbitmq/OMsgConnector.go @@ -1,6 +1,7 @@ package rabbitmq import ( + "agent-go" "agent-go/g" "encoding/json" "fmt" @@ -50,12 +51,12 @@ func BuildOMsgRuntimeConnectorQueue(agentTopicName string) { deliveries := octopusMsgQueue.Read(true) forever := make(chan bool) - P.Submit( + main.P.Submit( func() { // 死循环,处理Octopus Message for delivery := range deliveries { - var om *OctopusMessage + var om *main.OctopusMessage err := json.Unmarshal(delivery.Body, &om) if err != nil { log.Error(fmt.Sprintf("octopus message convert to json is wrong! msg is => %s", delivery.Body)) @@ -64,7 +65,7 @@ func BuildOMsgRuntimeConnectorQueue(agentTopicName string) { } // 策略模式 处理消息 - P.Submit(func() { + main.P.Submit(func() { om.Handle() }) }