[Excution] - base function accomplish - 4
This commit is contained in:
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
}...,
|
||||
)
|
||||
|
||||
@@ -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")
|
||||
|
||||
}
|
||||
|
||||
@@ -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,12 +22,22 @@ 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 strings.HasPrefix(em.Type, "BASE") {
|
||||
// base function
|
||||
resultLog = AgentOsOperatorCache.Exec(em.BaseFuncContent[0], em.BaseFuncContent[1:]...)
|
||||
err = nil
|
||||
|
||||
} else {
|
||||
// shell command
|
||||
|
||||
if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 {
|
||||
// 管道命令
|
||||
resultLog, err = PipeLineCommandExecutor(em.PipeLineCommand)
|
||||
@@ -40,6 +51,7 @@ func Execute(em *ExecutionMessage) ([]string, error) {
|
||||
resultLog, err = SingleLineCommandExecutor(em.SingleLineCommand)
|
||||
realCommand = [][]string{em.SingleLineCommand}
|
||||
}
|
||||
}
|
||||
|
||||
// 归一化错误和日志
|
||||
if err != nil {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user