[ Cmii ] [ Octopus ] - reformat agent-go - 1

This commit is contained in:
zeaslity
2024-03-29 11:39:14 +08:00
parent aa4412f042
commit 1be48aaac2
52 changed files with 683 additions and 557 deletions

View File

@@ -3,11 +3,7 @@ package rabbitmq
import (
"encoding/json"
"fmt"
"strings"
"wdd.io/agent-common/utils"
"wdd.io/agent-go/executor"
"wdd.io/agent-go/g"
"wdd.io/agent-go/status"
)
type IOctopusMessage interface {
@@ -16,10 +12,6 @@ type IOctopusMessage interface {
OctopusMsgBuilder
}
type OctopusMsgHandler interface {
Handle(octopusMessage *OctopusMessage)
}
type OctopusMsgSender interface {
Send(rabbitQueue *RabbitQueue, msg []byte)
@@ -40,12 +32,6 @@ type OctopusMessage struct {
ResultCode string `json:"resultCode"`
}
func (om *OctopusMessage) Handle() {
// 实际执行 OM handle进程
log.Debug("接收到OctopusMessage,开始处理!")
doHandleOctopusMessage(om)
}
func (om *OctopusMessage) Send(rabbitQueue *RabbitQueue, msg []byte) {
rabbitQueue.Send(msg)
}
@@ -84,116 +70,3 @@ func (om *OctopusMessage) Build(omType string, content interface{}) *OctopusMess
ACTime: curTimeString,
}
}
func doHandleOctopusMessage(octopusMessage *OctopusMessage) {
switch octopusMessage.OctopusMessageType {
case g.InitOmType:
go func() {}()
case g.ExecOmType:
P.Submit(func() {
executorOMHandler(octopusMessage)
})
case g.StatusOmType:
P.Submit(func() {
statusOMHandler(octopusMessage)
})
case g.AgentOmType:
P.Submit(func() {
agentOMHandler(octopusMessage)
},
)
default:
P.Submit(func() {
blackHoleOMHandler(octopusMessage)
})
}
}
// agentOMHandler 处理Agent的核心操作指令
func agentOMHandler(octopusMessage *OctopusMessage) {
}
func executorOMHandler(octopusMessage *OctopusMessage) {
log.Debug("开始处理 Executor Octopus Message !")
// 转换类型
executionMsgString := octopusMessage.Content.(string)
// 解析 ExecutionMessage
var executionMessage *executor.ExecutionMessage
err := json.Unmarshal([]byte(executionMsgString), &executionMessage)
if err != nil {
log.Error(fmt.Sprintf("execution message convert to json is wrong! msg is => %s", executionMsgString))
return
}
// 执行命令
ok, resultLog := executor.Execute(executionMessage)
if ok {
octopusMessage.ResultCode = "200"
} else {
octopusMessage.ResultCode = "300"
}
// 返回结果
if executionMessage.NeedResultReplay {
// send back the result log
octopusMessage.Result = resultLog
}
// 返回时间
octopusMessage.ACTime = utils.ParseDateTimeTime()
// 返回结果
octopusMessage.SendToOctopusServer()
}
func statusOMHandler(octopusMessage *OctopusMessage) {
v, ok := (octopusMessage.Content).(string)
if !ok {
log.ErrorF("convert to string is wrong %s", v)
}
statusMsgString := octopusMessage.Content.(string)
var statusMessage *status.StatusMessage
err := json.Unmarshal([]byte(statusMsgString), &statusMessage)
if err != nil {
fmt.Println(err.Error())
log.Error(fmt.Sprintf("status message convert to json is wrong! msg is => %s", octopusMessage))
return
}
// OMessageStatusTypeEnum
var statusRes string
if strings.HasPrefix(statusMessage.StatusType, "PING") {
// ping info
statusRes = status.Ping()
} else if strings.HasPrefix(statusMessage.StatusType, "METRIC") {
// metric info
agentStatusString, _ := json.Marshal(status.ReportAgentMetric())
statusRes = string(agentStatusString)
} else if strings.HasPrefix(statusMessage.StatusType, "INFO") {
log.InfoF("[statusOMHandler] - call for agent info !")
} else {
log.WarnF("[statusOMHandler] - error octopus status message type of %s", statusMessage.StatusType)
}
// 返回消息
// 组装消息
octopusMessage.ACTime = utils.ParseDateTimeTime()
octopusMessage.Result = statusRes
// 发送回去
statusOctopusReplayMessage, _ := json.Marshal(octopusMessage)
OctopusToServerQueue.Send(statusOctopusReplayMessage)
// 输出日志
log.InfoF("接收到查询Agent状态的请求结果为 => %s", statusRes)
}
func blackHoleOMHandler(octopusMessage *OctopusMessage) {
log.Error(fmt.Sprintf("[BLACK HOLE] octopusMessage type wrong! msg is => %v", octopusMessage))
}