From 97187363ccccd8d5e0f598f0c2420d6b52d254d1 Mon Sep 17 00:00:00 2001 From: IceDerce Date: Mon, 26 Jun 2023 13:50:14 +0800 Subject: [PATCH] [Excution] - base function accomplish - 4 --- agent-go/AgentInitialization.go | 23 +++++++++++++ agent-go/executor/BaseFunction.go | 32 ++++++++++++----- agent-go/executor/BaseFunction_test.go | 8 ++--- agent-go/executor/CommandExecutor.go | 34 +++++++++++++------ agent-go/executor/RealTimeExecutor.go | 21 ++++++++---- ...ion.go => NacosInitalizationDeprecated.go} | 0 6 files changed, 88 insertions(+), 30 deletions(-) rename agent-go/register/{NacosInitalization.go => NacosInitalizationDeprecated.go} (100%) diff --git a/agent-go/AgentInitialization.go b/agent-go/AgentInitialization.go index b92e3a0..065d57c 100644 --- a/agent-go/AgentInitialization.go +++ b/agent-go/AgentInitialization.go @@ -1,6 +1,7 @@ package main import ( + "agent-go/executor" "agent-go/g" "agent-go/rabbitmq" "agent-go/register" @@ -184,5 +185,27 @@ func parseAgentServerInfo() *register.AgentServerInfo { } log.Info(fmt.Sprintf("agent server info is %v", string(jsonFormat))) + // build a operator cache + BuildAgentOsOperator(agentServerInfo) + return agentServerInfo } + +func BuildAgentOsOperator(agentServerInfo *register.AgentServerInfo) { + + executor.AgentOsOperatorCache = &executor.AgentOsOperator{ + InstallCommandPrefix: nil, + RemoveCommandPrefix: nil, + CanAccessInternet: false, + IsOsTypeUbuntu: false, + IsAgentInnerWall: false, + AgentArch: "", + AgentOSReleaseCode: "", + AgentServerInfo: agentServerInfo, + } + + // debug + marshal, _ := json.Marshal(executor.AgentOsOperatorCache) + log.DebugF("cached agent operator is %s", marshal) + +} diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index d0cdf9a..bb6249c 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -3,11 +3,12 @@ package executor import ( "agent-go/g" "agent-go/register" + "fmt" "strings" ) type BaseFunc interface { - Exec(baseFuncName string, funcArgs ...string) string + Exec(baseFuncName string, funcArgs ...string) []string } type AgentOsOperator struct { @@ -25,11 +26,11 @@ type AgentOsOperator struct { AgentOSReleaseCode string `json:"agent_os_release_code",comment:"主机操作系统的发行版代号, focal之类的"` - AgentServerInfo register.AgentServerInfo `json:"agent_server_info"` + AgentServerInfo *register.AgentServerInfo `json:"agent_server_info"` } // Exec 执行基础功能函数 -func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string { +func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []string { var multiLineCommand [][]string @@ -90,13 +91,21 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string log.DebugF("multiLineCommand are => %v", multiLineCommand) + var result []string + // exec the command here for _, singleLineCommand := range multiLineCommand { - ReadTimeCommandExecutor(singleLineCommand) + result = append(result, ReadTimeCommandExecutor(singleLineCommand)...) + } + + // debug usage + //log.DebugF("exec result are => %v", result) + for _, logLine := range result { + fmt.Println(logLine) } // 归一化处理 - return strings.Join([]string{}, "") + return result } func (op *AgentOsOperator) shutdownFirewall() [][]string { @@ -628,6 +637,11 @@ func (op *AgentOsOperator) chronyToMaster(args []string) [][]string { func (op *AgentOsOperator) installZSH() [][]string { installZSHFunc := [][]string{ + { + "mkdir", + "-p", + "/root/wdd/", + }, append( op.InstallCommandPrefix, "zsh", @@ -643,7 +657,7 @@ func (op *AgentOsOperator) installZSH() [][]string { "wget", "https://cdn.jsdelivr.net/gh/robbyrussell/oh-my-zsh@master/tools/install.sh", "-O", - "zsh-install.sh", + "/root/wdd/zsh-install.sh", }, }..., ) @@ -656,7 +670,7 @@ func (op *AgentOsOperator) installZSH() [][]string { "wget", "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh", "-O", - "zsh-install.sh", + "/root/wdd/zsh-install.sh", }, }..., ) @@ -669,12 +683,12 @@ func (op *AgentOsOperator) installZSH() [][]string { { "chmod", "+x", - "zsh-install.sh", + "/root/wdd/zsh-install.sh", }, { "sh", "-c", - "./zsh-install.sh", + "/root/wdd/zsh-install.sh", }, }..., ) diff --git a/agent-go/executor/BaseFunction_test.go b/agent-go/executor/BaseFunction_test.go index 1002a11..63518d7 100644 --- a/agent-go/executor/BaseFunction_test.go +++ b/agent-go/executor/BaseFunction_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -var agentOP = AgentOsOperator{ +var agentOP = &AgentOsOperator{ InstallCommandPrefix: []string{ "apt-get", "install", "-y", }, @@ -15,7 +15,7 @@ var agentOP = AgentOsOperator{ IsAgentInnerWall: true, AgentArch: "amd64", AgentOSReleaseCode: "focal", - AgentServerInfo: register.AgentServerInfo{ + AgentServerInfo: ®ister.AgentServerInfo{ ServerName: "", ServerIPPbV4: "", ServerIPInV4: "192.168.0.8", @@ -47,7 +47,7 @@ func TestBaseFunc(t *testing.T) { //agentOP.Exec("modifyHostname") //agentOP.Exec("disableSwap") //agentOP.Exec("enableSwap") - //agentOP.Exec("removeDocker") + agentOP.Exec("removeDocker") //agentOP.Exec("installDocker", "20") //agentOP.Exec("removeDockerCompose") //agentOP.Exec("installDockerCompose") @@ -55,6 +55,6 @@ func TestBaseFunc(t *testing.T) { //agentOP.Exec("installHarbor") //agentOP.Exec("chronyToPublicNTP") //agentOP.Exec("chronyToMaster", "192.168.0.8") - agentOP.Exec("installZSH") + //agentOP.Exec("installZSH") } diff --git a/agent-go/executor/CommandExecutor.go b/agent-go/executor/CommandExecutor.go index 1a16a89..727198c 100644 --- a/agent-go/executor/CommandExecutor.go +++ b/agent-go/executor/CommandExecutor.go @@ -13,6 +13,7 @@ type ExecutionMessage struct { NeedResultReplay bool `json:"needResultReplay"` DurationTask bool `json:"durationTask,default:false"` Type string `json:"type"` + BaseFuncContent []string `json:"baseFuncContent"` SingleLineCommand []string `json:"singleLineCommand"` MultiLineCommand [][]string `json:"multiLineCommand"` PipeLineCommand [][]string `json:"pipeLineCommand"` @@ -21,24 +22,35 @@ type ExecutionMessage struct { var log = logger2.Log +var AgentOsOperatorCache = &AgentOsOperator{} + func Execute(em *ExecutionMessage) ([]string, error) { var resultLog []string var err error var realCommand [][]string - if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 { - // 管道命令 - resultLog, err = PipeLineCommandExecutor(em.PipeLineCommand) - realCommand = em.PipeLineCommand - } else if em.MultiLineCommand != nil && len(em.MultiLineCommand) != 0 { - // 多行命令 - resultLog, err = MultiLineCommandExecutor(em.MultiLineCommand) - realCommand = em.MultiLineCommand + if strings.HasPrefix(em.Type, "BASE") { + // base function + resultLog = AgentOsOperatorCache.Exec(em.BaseFuncContent[0], em.BaseFuncContent[1:]...) + err = nil + } else { - // 单行命令 - resultLog, err = SingleLineCommandExecutor(em.SingleLineCommand) - realCommand = [][]string{em.SingleLineCommand} + // shell command + + if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 { + // 管道命令 + resultLog, err = PipeLineCommandExecutor(em.PipeLineCommand) + realCommand = em.PipeLineCommand + } else if em.MultiLineCommand != nil && len(em.MultiLineCommand) != 0 { + // 多行命令 + resultLog, err = MultiLineCommandExecutor(em.MultiLineCommand) + realCommand = em.MultiLineCommand + } else { + // 单行命令 + resultLog, err = SingleLineCommandExecutor(em.SingleLineCommand) + realCommand = [][]string{em.SingleLineCommand} + } } // 归一化错误和日志 diff --git a/agent-go/executor/RealTimeExecutor.go b/agent-go/executor/RealTimeExecutor.go index b56c642..b6d6de1 100644 --- a/agent-go/executor/RealTimeExecutor.go +++ b/agent-go/executor/RealTimeExecutor.go @@ -2,12 +2,11 @@ package executor import ( "bufio" - "fmt" "io" "os/exec" ) -func ReadTimeCommandExecutor(singleLineCommand []string) { +func ReadTimeCommandExecutor(singleLineCommand []string) []string { cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) stdout, err := cmd.StdoutPipe() @@ -23,18 +22,28 @@ func ReadTimeCommandExecutor(singleLineCommand []string) { log.ErrorF("command %v runtime error => %v", singleLineCommand, err) } - go copyOutput(stdout) - go copyOutput(stderr) + var resultSlice []string + resultSlice = append(resultSlice, copyOutput(stdout, resultSlice)...) + resultSlice = append(resultSlice, copyOutput(stderr, resultSlice)...) if err := cmd.Wait(); err != nil { log.ErrorF("command %v result error => %v", singleLineCommand, err) } + //log.DebugF("real time exec result are %v", resultSlice) + + return resultSlice } -func copyOutput(r io.Reader) { +func copyOutput(r io.Reader, resultSlice []string) []string { scanner := bufio.NewScanner(r) for scanner.Scan() { - fmt.Println(scanner.Text()) + resultLine := scanner.Text() + + resultSlice = append(resultSlice, resultLine) + // debug usage + //fmt.Println(resultLine) } + + return resultSlice } diff --git a/agent-go/register/NacosInitalization.go b/agent-go/register/NacosInitalizationDeprecated.go similarity index 100% rename from agent-go/register/NacosInitalization.go rename to agent-go/register/NacosInitalizationDeprecated.go