[agent-go] 初步完成Executor部分的代码- 1

This commit is contained in:
zeaslity
2023-03-24 15:46:00 +08:00
parent 1af25d3992
commit 7e2450d30a
12 changed files with 60 additions and 41 deletions

View File

@@ -0,0 +1,26 @@
package config
type AgentServerInfo struct {
ServerName string `json:"serverName" yaml:"serverName"`
ServerIPPbV4 string `json:"serverIpPbV4" yaml:"serverIpPbV4"`
ServerIPInV4 string `json:"serverIpInV4" yaml:"serverIpInV4"`
ServerIPPbV6 string `json:"serverIpPbV6" yaml:"serverIpPbV6"`
ServerIPInV6 string `json:"serverIpInV6" yaml:"serverIpInV6"`
Location string `json:"location" yaml:"location"`
Provider string `json:"provider" yaml:"provider"`
ManagePort string `json:"managePort" yaml:"managePort"`
CPUCore string `json:"cpuCore" yaml:"cpuCore"`
CPUBrand string `json:"cpuBrand" yaml:"cpuBrand"`
OSInfo string `json:"osInfo" yaml:"osInfo"`
OSKernelInfo string `json:"osKernelInfo" yaml:"osKernelInfo"`
TCPControl string `json:"tcpControl" yaml:"tcpControl"`
Virtualization string `json:"virtualization" yaml:"virtualization"`
IoSpeed string `json:"ioSpeed" yaml:"ioSpeed"`
MemoryTotal string `json:"memoryTotal" yaml:"memoryTotal"`
DiskTotal string `json:"diskTotal" yaml:"diskTotal"`
DiskUsage string `json:"diskUsage" yaml:"diskUsage"`
Comment string `json:"comment" yaml:"comment"`
MachineID string `json:"machineId" yaml:"machineId"`
AgentVersion string `json:"agentVersion" yaml:"agentVersion"`
AgentTopicName string `json:"agentTopicName" yaml:"agentTopicName"`
}

View File

@@ -1,165 +0,0 @@
package config
import (
"agent-go/g"
"bytes"
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/spf13/viper"
"go.uber.org/zap"
"strconv"
"strings"
)
var log = g.G.LOG
var group = ""
func InitNacos(configFileName string) {
v := parseAgentConfigFile(configFileName, nil)
group = v.GetString("spring.cloud.nacos.config.group")
configClient := startNacosConnection(v)
allNacosConfig := getAllNacosConfig(v, group, configClient)
for _, nacosConfigContent := range allNacosConfig {
log.Debug(fmt.Sprintf("nacos config conetent is %s", nacosConfigContent))
//parseNacosConfigContend(nacosConfigContent, v)
}
log.Info(fmt.Sprintf("%s config read result are %v", configFileName, v.AllSettings()))
}
func parseAgentConfigFile(configFileName string, v *viper.Viper) *viper.Viper {
// 使用Viper框架读取
if v == nil {
v = viper.New()
}
// 设置配置文件路径和名称
v.SetConfigName(configFileName)
v.AddConfigPath(".")
v.SetConfigType("yaml")
// 读取默认的总配置文件
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
}
return v
}
func parseNacosConfigContend(configContent string, v *viper.Viper) *viper.Viper {
v.SetConfigType("yaml")
err := v.ReadConfig(bytes.NewBuffer([]byte(configContent)))
if err != nil {
log.Error("nacos config contend read error !", zap.Error(err))
}
return v
}
func startNacosConnection(v *viper.Viper) config_client.IConfigClient {
clientConfig := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: v.GetUint64("spring.cloud.nacos.config.timeout"),
NotLoadCacheAtStart: true,
AppendToStdout: true,
//UpdateCacheWhenEmpty: true,
//LogDir: "/tmp/nacos/log",
//CacheDir: "/tmp/nacos/cache",
Username: "nacos",
Password: "Superwmm.23",
}
serverAddr := v.GetString("spring.cloud.nacos.config.server-addr")
split := strings.Split(serverAddr, ":")
if len(split) != 2 {
log.Error("nacos server addr error!")
}
port, _ := strconv.ParseUint(split[1], 10, 64)
serverConfigs := []constant.ServerConfig{
{
IpAddr: split[0],
Port: port,
GrpcPort: port + 1000,
},
}
// Another way of create config client for dynamic configuration (recommend)
configClient, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfigs,
},
)
if err != nil {
panic(err)
}
return configClient
}
func getAllNacosConfig(v *viper.Viper, group string, configClient config_client.IConfigClient) []string {
result := make([]string, 0)
// main nacos configs
mainNacosConfigFileName := v.GetString("spring.application.name") + "-" + v.GetString("spring.profiles.active") + "." + v.GetString("spring.cloud.nacos.config.file-extension")
log.Debug(fmt.Sprintf("main nacos config file name is %s", mainNacosConfigFileName))
configContent := getConfig(mainNacosConfigFileName, group, configClient)
result = append(result, configContent)
// additional nacos config
additionalNacosConfig := v.Get("spring.cloud.nacos.config.extension-configs")
// 增加断言判定map的类型
m, ok := additionalNacosConfig.([]interface{})
if !ok {
fmt.Println("additionalNacosConfig is not a slice")
return nil
}
for _, addConfigMap := range m {
real, _ := addConfigMap.(map[string]interface{})
for additionalNacosConfigFileName, additionalNacosConfigGroup := range real {
s := additionalNacosConfigGroup.(string)
configContent := getConfig(additionalNacosConfigFileName, s, configClient)
result = append(result, configContent)
}
}
return result
}
// getConfig 从Nacos中获取相应的
func getConfig(dataId string, group string, configClient config_client.IConfigClient) string {
log.Debug(fmt.Sprintf("nacos config get method dataID is %s, group is %s", dataId, group))
content := ""
configClient.GetConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
Content: content,
Type: "yaml",
})
log.Debug(fmt.Sprintf("dataId %s , group %s, nacos config content is %s", dataId, group, content))
return content
}

View File

@@ -0,0 +1,41 @@
package config
import (
"agent-go/utils"
"time"
)
type OctopusMessage struct {
UUID string `json:"uuid"`
InitTime time.Time `json:"init_time" format:"2023-03-21 16:38:30"`
Type string `json:"type"`
Content interface{} `json:"content"`
Result interface{} `json:"result"`
ACTime time.Time `json:"ac_time" format:"2023-03-21 16:38:30"`
}
type ExecutionMessage struct {
NeedResultReplay bool `json:"needResultReplay"`
DurationTask bool `json:"durationTask,default:false"`
Type string `json:"type"`
SingleLineCommand []string `json:"singleLineCommand"`
MultiLineCommand [][]string `json:"multiLineCommand"`
PipeLineCommand [][]string `json:"pipeLineCommand"`
ResultKey string `json:"resultKey"`
}
// BuildOctopusMsg 生成OctopusMessage
func (m *OctopusMessage) BuildOctopusMsg(omType string, content interface{}) *OctopusMessage {
// 当前时间
curTimeString := utils.CurTimeString()
return &OctopusMessage{
UUID: curTimeString,
InitTime: time.Now(),
Type: omType,
Content: content,
Result: nil,
ACTime: time.Time{},
}
}