[agent-go] 初步完成Executor部分的代码- 1
This commit is contained in:
11
agent-go/IRabbitSendWriter.go
Normal file
11
agent-go/IRabbitSendWriter.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
/*type RabbitSendWriter interface {
|
||||||
|
|
||||||
|
Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte)
|
||||||
|
|
||||||
|
Read(conn *RabbitMQConn, connProp *ConnectProperty, autoAck bool) <-chan amqp.Delivery
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package register
|
package config
|
||||||
|
|
||||||
type AgentServerInfo struct {
|
type AgentServerInfo struct {
|
||||||
ServerName string `json:"serverName" yaml:"serverName"`
|
ServerName string `json:"serverName" yaml:"serverName"`
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package rabbitmq
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-go/g"
|
"agent-go/utils"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ type ExecutionMessage struct {
|
|||||||
func (m *OctopusMessage) BuildOctopusMsg(omType string, content interface{}) *OctopusMessage {
|
func (m *OctopusMessage) BuildOctopusMsg(omType string, content interface{}) *OctopusMessage {
|
||||||
|
|
||||||
// 当前时间
|
// 当前时间
|
||||||
curTimeString := g.CurTimeString()
|
curTimeString := utils.CurTimeString()
|
||||||
|
|
||||||
return &OctopusMessage{
|
return &OctopusMessage{
|
||||||
UUID: curTimeString,
|
UUID: curTimeString,
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package executor
|
package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"agent-go/config"
|
||||||
"agent-go/g"
|
"agent-go/g"
|
||||||
"agent-go/rabbitmq"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
var log = g.G.LOG
|
var log = g.G.LOG
|
||||||
|
|
||||||
func Execute(om *rabbitmq.OctopusMessage, em *rabbitmq.ExecutionMessage) ([]string, error) {
|
func Execute(om *config.OctopusMessage, em *config.ExecutionMessage) ([]string, error) {
|
||||||
|
|
||||||
var resultLog []string
|
var resultLog []string
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package g
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-go/g"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
||||||
@@ -14,7 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = g.G.LOG
|
var log = G.LOG
|
||||||
var group = ""
|
var group = ""
|
||||||
|
|
||||||
func InitNacos(configFileName string) {
|
func InitNacos(configFileName string) {
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
package g
|
package g
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-go/register"
|
"agent-go/config"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Global struct {
|
type Global struct {
|
||||||
LOG *Logger
|
LOG *Logger
|
||||||
NacosConfig *viper.Viper
|
NacosConfig *viper.Viper
|
||||||
AgentServerInfo *register.AgentServerInfo
|
AgentServerInfo *config.AgentServerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -17,6 +17,8 @@ const (
|
|||||||
ExecOmType = "EXECUTOR"
|
ExecOmType = "EXECUTOR"
|
||||||
StatusOmType = "STATUS"
|
StatusOmType = "STATUS"
|
||||||
InitOmType = "INIT"
|
InitOmType = "INIT"
|
||||||
|
|
||||||
|
// write about
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger, _ = NewLogger()
|
var logger, _ = NewLogger()
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"agent-go/g"
|
||||||
"agent-go/register"
|
"agent-go/register"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = g.G.LOG
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// 解析命令行参数
|
// 解析命令行参数
|
||||||
@@ -20,6 +23,6 @@ func main() {
|
|||||||
//config.InitNacos(filename)
|
//config.InitNacos(filename)
|
||||||
|
|
||||||
// 执行初始化之策工作
|
// 执行初始化之策工作
|
||||||
register.INIT()
|
g.G.AgentServerInfo = register.INIT()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,19 @@ import (
|
|||||||
"github.com/streadway/amqp"
|
"github.com/streadway/amqp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RabbitMQConn is a struct that holds the connection and channel objects
|
||||||
|
type RabbitMQConn struct {
|
||||||
|
Connection *amqp.Connection
|
||||||
|
Channel *amqp.Channel
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConnectProperty struct {
|
||||||
|
ExchangeName string
|
||||||
|
QueueName string
|
||||||
|
ExchangeType string
|
||||||
|
TopicKey string
|
||||||
|
}
|
||||||
|
|
||||||
// Send 向RabbitMQ中发送消息
|
// Send 向RabbitMQ中发送消息
|
||||||
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
|
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
|
||||||
// 往哪里发
|
// 往哪里发
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
package rabbitmq
|
package rabbitmq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"agent-go/config"
|
||||||
"agent-go/executor"
|
"agent-go/executor"
|
||||||
"agent-go/g"
|
"agent-go/g"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleOMsg(initOMsgFromServer *OctopusMessage) {
|
func HandleOMsg(initOMsgFromServer *config.OctopusMessage) {
|
||||||
|
|
||||||
agentTopicName := initOMsgFromServer.UUID
|
agentTopicName := initOMsgFromServer.UUID
|
||||||
OctopusExchange := g.G.NacosConfig.GetString("octopus.message.octopus_exchange")
|
OctopusExchange := g.G.NacosConfig.GetString("octopus.message.octopus_exchange")
|
||||||
@@ -43,7 +44,7 @@ func HandleOMsg(initOMsgFromServer *OctopusMessage) {
|
|||||||
// 死循环,处理Ocotpus Message
|
// 死循环,处理Ocotpus Message
|
||||||
for delivery := range deliveries {
|
for delivery := range deliveries {
|
||||||
|
|
||||||
var om *OctopusMessage
|
var om *config.OctopusMessage
|
||||||
err := json.Unmarshal(delivery.Body, &om)
|
err := json.Unmarshal(delivery.Body, &om)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Octopus Message Parse Error !")
|
log.Error("Octopus Message Parse Error !")
|
||||||
@@ -57,7 +58,7 @@ func HandleOMsg(initOMsgFromServer *OctopusMessage) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func doHandleOctopusMessage(octopusMessage *OctopusMessage) {
|
func doHandleOctopusMessage(octopusMessage *config.OctopusMessage) {
|
||||||
|
|
||||||
switch octopusMessage.Type {
|
switch octopusMessage.Type {
|
||||||
case g.InitOmType:
|
case g.InitOmType:
|
||||||
@@ -72,11 +73,11 @@ func doHandleOctopusMessage(octopusMessage *OctopusMessage) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func executorOMHandler(octopusMessage *OctopusMessage) {
|
func executorOMHandler(octopusMessage *config.OctopusMessage) {
|
||||||
|
|
||||||
executionMsgString := octopusMessage.Content.(string)
|
executionMsgString := octopusMessage.Content.(string)
|
||||||
|
|
||||||
var executionMessage *ExecutionMessage
|
var executionMessage *config.ExecutionMessage
|
||||||
err := json.Unmarshal([]byte(executionMsgString), &executionMessage)
|
err := json.Unmarshal([]byte(executionMsgString), &executionMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -87,10 +88,10 @@ func executorOMHandler(octopusMessage *OctopusMessage) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func statusOMHandler(octopusMessage *OctopusMessage) {
|
func statusOMHandler(octopusMessage *config.OctopusMessage) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func blackHoleOMHandler(octopusMessage *OctopusMessage) {
|
func blackHoleOMHandler(octopusMessage *config.OctopusMessage) {
|
||||||
log.Error(fmt.Sprintf("octopusMessage type wrong! msg is => %v", octopusMessage))
|
log.Error(fmt.Sprintf("octopusMessage type wrong! msg is => %v", octopusMessage))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,19 +10,6 @@ import (
|
|||||||
|
|
||||||
var log = g.G.LOG
|
var log = g.G.LOG
|
||||||
|
|
||||||
// RabbitMQConn is a struct that holds the connection and channel objects
|
|
||||||
type RabbitMQConn struct {
|
|
||||||
Connection *amqp.Connection
|
|
||||||
Channel *amqp.Channel
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConnectProperty struct {
|
|
||||||
ExchangeName string
|
|
||||||
QueueName string
|
|
||||||
ExchangeType string
|
|
||||||
TopicKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
// 定义全局唯一的 Singleton 实例
|
// 定义全局唯一的 Singleton 实例
|
||||||
var instance *amqp.Connection
|
var instance *amqp.Connection
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package register
|
package register
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"agent-go/config"
|
||||||
"agent-go/g"
|
"agent-go/g"
|
||||||
"agent-go/rabbitmq"
|
"agent-go/rabbitmq"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -9,14 +10,13 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = g.G.LOG
|
|
||||||
var omType = g.InitOmType
|
var omType = g.InitOmType
|
||||||
var agentServerInfo = g.G.AgentServerInfo
|
var log = g.G.LOG
|
||||||
|
|
||||||
func INIT() {
|
func INIT() *config.AgentServerInfo {
|
||||||
|
|
||||||
// 获取系统的环境变量
|
// 获取系统的环境变量
|
||||||
g.G.AgentServerInfo = parseAgentServerInfo()
|
agentServerInfo := parseAgentServerInfo()
|
||||||
|
|
||||||
nacosConfig := g.G.NacosConfig
|
nacosConfig := g.G.NacosConfig
|
||||||
|
|
||||||
@@ -37,6 +37,7 @@ func INIT() {
|
|||||||
// 建立RabbitMQ的连接
|
// 建立RabbitMQ的连接
|
||||||
// defer 关闭初始化连接
|
// defer 关闭初始化连接
|
||||||
initToServer, err := rabbitmq.NewRabbitMQConn(
|
initToServer, err := rabbitmq.NewRabbitMQConn(
|
||||||
|
|
||||||
initToServerProp,
|
initToServerProp,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,7 +56,7 @@ func INIT() {
|
|||||||
defer rabbitmq.CloseChannel(initFromServer)
|
defer rabbitmq.CloseChannel(initFromServer)
|
||||||
|
|
||||||
// 组装OctopusMessage
|
// 组装OctopusMessage
|
||||||
var octopusMsg *rabbitmq.OctopusMessage
|
var octopusMsg *config.OctopusMessage
|
||||||
octopusMsg = octopusMsg.BuildOctopusMsg(
|
octopusMsg = octopusMsg.BuildOctopusMsg(
|
||||||
omType,
|
omType,
|
||||||
agentServerInfo,
|
agentServerInfo,
|
||||||
@@ -75,6 +76,8 @@ func INIT() {
|
|||||||
// 建立运行时RabbitMQ连接
|
// 建立运行时RabbitMQ连接
|
||||||
handleInitMsgFromServer(initFromServer, initFromServerProp, initToServer, initToServerProp)
|
handleInitMsgFromServer(initFromServer, initFromServerProp, initToServer, initToServerProp)
|
||||||
|
|
||||||
|
return agentServerInfo
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleInitMsgFromServer 处理从Server接收的注册信息
|
// handleInitMsgFromServer 处理从Server接收的注册信息
|
||||||
@@ -85,7 +88,7 @@ func handleInitMsgFromServer(initFromServer *rabbitmq.RabbitMQConn, initFromServ
|
|||||||
// 同步很多抢占注册的情况
|
// 同步很多抢占注册的情况
|
||||||
for delivery := range deliveries {
|
for delivery := range deliveries {
|
||||||
|
|
||||||
var om *rabbitmq.OctopusMessage
|
var om *config.OctopusMessage
|
||||||
err := json.Unmarshal(delivery.Body, &om)
|
err := json.Unmarshal(delivery.Body, &om)
|
||||||
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 ",
|
||||||
@@ -120,11 +123,11 @@ func shutdownRegisterQueueConnection(initFromServer *rabbitmq.RabbitMQConn, init
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAgentServerInfo() *AgentServerInfo {
|
func parseAgentServerInfo() *config.AgentServerInfo {
|
||||||
|
|
||||||
// 约定文件地址为 /etc/environment.d/octopus-agent.conf
|
// 约定文件地址为 /etc/environment.d/octopus-agent.conf
|
||||||
// 目前使用
|
// 目前使用
|
||||||
var agentServerInfo *AgentServerInfo
|
var agentServerInfo *config.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")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to read YAML file: %v", err))
|
panic(fmt.Errorf("failed to read YAML file: %v", err))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package g
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
Reference in New Issue
Block a user