[Excution] - base function start - 3

This commit is contained in:
IceDerce
2023-06-25 11:26:33 +08:00
parent 6ce6e5f0d3
commit fb38ab8f15
7 changed files with 136 additions and 48 deletions

View File

@@ -1,9 +1,9 @@
package register package main
import ( import (
"agent-go/g" "agent-go/g"
logger2 "agent-go/logger"
"agent-go/rabbitmq" "agent-go/rabbitmq"
"agent-go/register"
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -12,12 +12,11 @@ import (
) )
var omType = g.InitOmType var omType = g.InitOmType
var log = logger2.Log
var P = g.G.P var P = g.G.P
var AgentServerInfoCache = &AgentServerInfo{} var AgentServerInfoCache = &register.AgentServerInfo{}
func INIT() *AgentServerInfo { func INIT() *register.AgentServerInfo {
// 获取系统的环境变量 // 获取系统的环境变量
agentServerInfo := parseAgentServerInfo() agentServerInfo := parseAgentServerInfo()
@@ -48,7 +47,7 @@ func INIT() *AgentServerInfo {
initToServerQueue.Connect() initToServerQueue.Connect()
// 组装OctopusMessage // 组装OctopusMessage
var octopusMsg *rabbitmq.OctopusMessage var octopusMsg *OctopusMessage
octopusMsg = octopusMsg.Build( octopusMsg = octopusMsg.Build(
omType, omType,
agentServerInfo, agentServerInfo,
@@ -92,7 +91,7 @@ func INIT() *AgentServerInfo {
} }
// handleInitMsgFromServer 处理从Server接收的 注册信息 // 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) 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))) 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) err := json.Unmarshal(delivery.Body, &initOctopusMsg)
if err != nil { if err != nil {
log.Error(fmt.Sprintf("parse init message from server wroong, message is => %s ", log.Error(fmt.Sprintf("parse init message from server wroong, message is => %s ",
string(delivery.Body))) string(delivery.Body)))
} }
var serverInfo AgentServerInfo var serverInfo register.AgentServerInfo
s, _ := initOctopusMsg.Content.(string) s, _ := initOctopusMsg.Content.(string)
cc := json.Unmarshal([]byte(s), &serverInfo) cc := json.Unmarshal([]byte(s), &serverInfo)
@@ -161,11 +160,11 @@ func shutdownRegisterQueueConnection(initFromServerQueue *rabbitmq.RabbitQueue,
log.InfoF("Pretend to Shutdown register queue connection !") log.InfoF("Pretend to Shutdown register queue connection !")
} }
func parseAgentServerInfo() *AgentServerInfo { func parseAgentServerInfo() *register.AgentServerInfo {
// 约定文件地址为 /etc/environment.d/octopus-agent.conf // 约定文件地址为 /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("C:\\Users\\wdd\\IdeaProjects\\ProjectOctopus\\agent-go\\server-env.yaml")
yamlFile, err := ioutil.ReadFile("server-env.yaml") yamlFile, err := ioutil.ReadFile("server-env.yaml")

View File

@@ -1,8 +1,9 @@
package rabbitmq package main
import ( import (
"agent-go/executor" "agent-go/executor"
"agent-go/g" "agent-go/g"
"agent-go/rabbitmq"
"agent-go/status" "agent-go/status"
"agent-go/utils" "agent-go/utils"
"encoding/json" "encoding/json"
@@ -10,8 +11,6 @@ import (
"strings" "strings"
) )
var P = g.G.P
type IOctopusMessage interface { type IOctopusMessage interface {
OctopusMsgHandler OctopusMsgHandler
OctopusMsgSender OctopusMsgSender
@@ -23,7 +22,7 @@ type OctopusMsgHandler interface {
} }
type OctopusMsgSender interface { type OctopusMsgSender interface {
Send(rabbitQueue *RabbitQueue, msg []byte) Send(rabbitQueue *rabbitmq.RabbitQueue, msg []byte)
} }
type OctopusMsgBuilder interface { type OctopusMsgBuilder interface {
@@ -44,7 +43,7 @@ func (om *OctopusMessage) Handle() {
doHandleOctopusMessage(om) doHandleOctopusMessage(om)
} }
func (om *OctopusMessage) Send(rabbitQueue *RabbitQueue, msg []byte) { func (om *OctopusMessage) Send(rabbitQueue *rabbitmq.RabbitQueue, msg []byte) {
rabbitQueue.Send(msg) rabbitQueue.Send(msg)
} }
@@ -148,7 +147,7 @@ func statusOMHandler(octopusMessage *OctopusMessage) {
octopusMessage.Result = statusRes octopusMessage.Result = statusRes
// 发送回去 // 发送回去
statusOctopusReplayMessage, _ := json.Marshal(octopusMessage) statusOctopusReplayMessage, _ := json.Marshal(octopusMessage)
OctopusToServerQueue.Send(statusOctopusReplayMessage) rabbitmq.OctopusToServerQueue.Send(statusOctopusReplayMessage)
// 输出日志 // 输出日志
log.InfoF("接收到查询Agent状态的请求结果为 => %s", statusRes) log.InfoF("接收到查询Agent状态的请求结果为 => %s", statusRes)

View File

@@ -1,6 +1,7 @@
package executor package executor
import ( import (
"agent-go/g"
"agent-go/register" "agent-go/register"
"strings" "strings"
) )
@@ -52,6 +53,9 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string
case "removeDocker": case "removeDocker":
multiLineCommand = op.removeDocker() multiLineCommand = op.removeDocker()
break break
case "removeDockerCompose":
multiLineCommand = op.removeDockerCompose()
break
case "installDockerCompose": case "installDockerCompose":
multiLineCommand = op.installDockerCompose() multiLineCommand = op.installDockerCompose()
break break
@@ -329,11 +333,52 @@ func (op *AgentOsOperator) installDockerCompose() [][]string {
} }
func (op *AgentOsOperator) installHelm() [][]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{ installHelmFunc = [][]string{
{ {
"curl", "curl",
"-o", "-o",
@@ -357,7 +402,7 @@ func (op *AgentOsOperator) installHelm() [][]string {
} }
} else { } else {
log.ErrorF("Operation OS is CentOS, Helm not installed!") log.ErrorF("Operation OS is CentOS, Helm not installed!")
} }*/
return installHelmFunc return installHelmFunc
} }
@@ -374,7 +419,7 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string {
}, },
{ {
"wget", "wget",
"https://oss2.demo.uavcmlc.com:18000/wangdada/daemon-config.json", g.BaseFuncOssUrlPrefix + "daemon-config.json",
"-O", "-O",
"/etc/docker/daemon.json", "/etc/docker/daemon.json",
}, },
@@ -397,50 +442,62 @@ func (op *AgentOsOperator) modifyDockerConfig(args []string) [][]string {
func (op *AgentOsOperator) installHarbor(args []string) [][]string { func (op *AgentOsOperator) installHarbor(args []string) [][]string {
installHarborFunc := [][]string{ installHarborFunc := [][]string{
{ //{
"wget", // "mkdir",
"--no-check-certificate", // "-p",
"https://oss2.demo.uavcmlc.com:18000/wangdada/harbor-offline-installer-v2.1.0.tgz", // "/root/wdd/",
"-O", //},
"harbor-offline-installer-v2.1.0.tgz", //{
}, // "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", "tar",
"-zvxf", "-zvxf",
"harbor-offline-installer-v2.1.0.tgz", "/root/wdd/harbor-offline-installer-v2.1.0.tgz",
"-C",
"/root/wdd/",
}, },
{ {
"rm", "rm",
"-rf", "-rf",
"./harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
{ {
"wget", "wget",
"--no-check-certificate", "--no-check-certificate",
"https://oss2.demo.uavcmlc.com:18000/wangdada/harbor.yml", g.BaseFuncOssUrlPrefix + "harbor-config-template.yml",
"-O", "-O",
"./harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
{ {
"sed", "sed",
"-i", "-i",
"s/$HarborHostName/" + op.AgentServerInfo.ServerIPInV4 + "/g", "s/$HarborHostName/" + op.AgentServerInfo.ServerIPInV4 + "/g",
"./harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
{ {
"sed", "sed",
"-i", "-i",
"s/$HarborHostPort/8033/g", "s/$HarborHostPort/8033/g",
"./harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
{ {
"sed", "sed",
"-i", "-i",
"s/$HarborHostPort/V2ryStr@ngPss/g", "s/$HarborHostPort/V2ryStr@ngPss/g",
"./harbor/harbor.yml", "/root/wdd/harbor/harbor.yml",
}, },
{ {
"./harbor/install.sh", "/root/wdd/harbor/install.sh",
"--with-chartmuseum", "--with-chartmuseum",
}, },
} }
@@ -488,7 +545,7 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string {
{ {
"sed", "sed",
"-i", "-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, chronyFile,
}, },
{ {
@@ -505,6 +562,8 @@ func (op *AgentOsOperator) chronyToPublicNTP() [][]string {
"-n", "-n",
"sources", "sources",
"-v", "-v",
},
{
"chronyc", "chronyc",
"tracking", "tracking",
}, },

View File

@@ -1,6 +1,9 @@
package executor package executor
import "testing" import (
"agent-go/register"
"testing"
)
var agentOP = AgentOsOperator{ var agentOP = AgentOsOperator{
InstallCommandPrefix: []string{ InstallCommandPrefix: []string{
@@ -12,6 +15,30 @@ var agentOP = AgentOsOperator{
IsAgentInnerWall: true, IsAgentInnerWall: true,
AgentArch: "amd64", AgentArch: "amd64",
AgentOSReleaseCode: "focal", 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) { func TestBaseFunc(t *testing.T) {
@@ -22,11 +49,12 @@ func TestBaseFunc(t *testing.T) {
//agentOP.Exec("enableSwap") //agentOP.Exec("enableSwap")
//agentOP.Exec("removeDocker") //agentOP.Exec("removeDocker")
//agentOP.Exec("installDocker", "20") //agentOP.Exec("installDocker", "20")
agentOP.Exec("installDockerCompose") //agentOP.Exec("removeDockerCompose")
agentOP.Exec("installHelm") //agentOP.Exec("installDockerCompose")
agentOP.Exec("installHarbor") //agentOP.Exec("installHelm")
agentOP.Exec("chronyToPublicNTP") //agentOP.Exec("installHarbor")
agentOP.Exec("chronyToMaster", "192.168.0.8") //agentOP.Exec("chronyToPublicNTP")
//agentOP.Exec("chronyToMaster", "192.168.0.8")
agentOP.Exec("installZSH") agentOP.Exec("installZSH")
} }

View File

@@ -19,6 +19,8 @@ const (
StatusOmType = "STATUS" StatusOmType = "STATUS"
InitOmType = "INIT" InitOmType = "INIT"
AgentOmType = "AGENT" 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)) var pool, _ = ants.NewPool(100, ants.WithNonblocking(false), ants.WithLogger(logger2.Log), ants.WithMaxBlockingTasks(30), ants.WithDisablePurge(true))

View File

@@ -24,6 +24,6 @@ func main() {
g.G.AgentConfig = register.ParseConfiguration(filename) g.G.AgentConfig = register.ParseConfiguration(filename)
// 执行初始化之策工作 // 执行初始化之策工作
register.AgentServerInfoCache = register.INIT() AgentServerInfoCache = INIT()
} }

View File

@@ -1,6 +1,7 @@
package rabbitmq package rabbitmq
import ( import (
"agent-go"
"agent-go/g" "agent-go/g"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -50,12 +51,12 @@ func BuildOMsgRuntimeConnectorQueue(agentTopicName string) {
deliveries := octopusMsgQueue.Read(true) deliveries := octopusMsgQueue.Read(true)
forever := make(chan bool) forever := make(chan bool)
P.Submit( main.P.Submit(
func() { func() {
// 死循环处理Octopus Message // 死循环处理Octopus Message
for delivery := range deliveries { for delivery := range deliveries {
var om *OctopusMessage var om *main.OctopusMessage
err := json.Unmarshal(delivery.Body, &om) err := json.Unmarshal(delivery.Body, &om)
if err != nil { if err != nil {
log.Error(fmt.Sprintf("octopus message convert to json is wrong! msg is => %s", delivery.Body)) 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() om.Handle()
}) })
} }