[Excution] - base function accomplish - 4
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"agent-go/executor"
|
||||||
"agent-go/g"
|
"agent-go/g"
|
||||||
"agent-go/rabbitmq"
|
"agent-go/rabbitmq"
|
||||||
"agent-go/register"
|
"agent-go/register"
|
||||||
@@ -184,5 +185,27 @@ func parseAgentServerInfo() *register.AgentServerInfo {
|
|||||||
}
|
}
|
||||||
log.Info(fmt.Sprintf("agent server info is %v", string(jsonFormat)))
|
log.Info(fmt.Sprintf("agent server info is %v", string(jsonFormat)))
|
||||||
|
|
||||||
|
// build a operator cache
|
||||||
|
BuildAgentOsOperator(agentServerInfo)
|
||||||
|
|
||||||
return 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 (
|
import (
|
||||||
"agent-go/g"
|
"agent-go/g"
|
||||||
"agent-go/register"
|
"agent-go/register"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseFunc interface {
|
type BaseFunc interface {
|
||||||
Exec(baseFuncName string, funcArgs ...string) string
|
Exec(baseFuncName string, funcArgs ...string) []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentOsOperator struct {
|
type AgentOsOperator struct {
|
||||||
@@ -25,11 +26,11 @@ type AgentOsOperator struct {
|
|||||||
|
|
||||||
AgentOSReleaseCode string `json:"agent_os_release_code",comment:"主机操作系统的发行版代号, focal之类的"`
|
AgentOSReleaseCode string `json:"agent_os_release_code",comment:"主机操作系统的发行版代号, focal之类的"`
|
||||||
|
|
||||||
AgentServerInfo register.AgentServerInfo `json:"agent_server_info"`
|
AgentServerInfo *register.AgentServerInfo `json:"agent_server_info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec 执行基础功能函数
|
// Exec 执行基础功能函数
|
||||||
func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string {
|
func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []string {
|
||||||
|
|
||||||
var multiLineCommand [][]string
|
var multiLineCommand [][]string
|
||||||
|
|
||||||
@@ -90,13 +91,21 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) string
|
|||||||
|
|
||||||
log.DebugF("multiLineCommand are => %v", multiLineCommand)
|
log.DebugF("multiLineCommand are => %v", multiLineCommand)
|
||||||
|
|
||||||
|
var result []string
|
||||||
|
|
||||||
// exec the command here
|
// exec the command here
|
||||||
for _, singleLineCommand := range multiLineCommand {
|
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 {
|
func (op *AgentOsOperator) shutdownFirewall() [][]string {
|
||||||
@@ -628,6 +637,11 @@ func (op *AgentOsOperator) chronyToMaster(args []string) [][]string {
|
|||||||
func (op *AgentOsOperator) installZSH() [][]string {
|
func (op *AgentOsOperator) installZSH() [][]string {
|
||||||
|
|
||||||
installZSHFunc := [][]string{
|
installZSHFunc := [][]string{
|
||||||
|
{
|
||||||
|
"mkdir",
|
||||||
|
"-p",
|
||||||
|
"/root/wdd/",
|
||||||
|
},
|
||||||
append(
|
append(
|
||||||
op.InstallCommandPrefix,
|
op.InstallCommandPrefix,
|
||||||
"zsh",
|
"zsh",
|
||||||
@@ -643,7 +657,7 @@ func (op *AgentOsOperator) installZSH() [][]string {
|
|||||||
"wget",
|
"wget",
|
||||||
"https://cdn.jsdelivr.net/gh/robbyrussell/oh-my-zsh@master/tools/install.sh",
|
"https://cdn.jsdelivr.net/gh/robbyrussell/oh-my-zsh@master/tools/install.sh",
|
||||||
"-O",
|
"-O",
|
||||||
"zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
},
|
},
|
||||||
}...,
|
}...,
|
||||||
)
|
)
|
||||||
@@ -656,7 +670,7 @@ func (op *AgentOsOperator) installZSH() [][]string {
|
|||||||
"wget",
|
"wget",
|
||||||
"https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh",
|
"https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh",
|
||||||
"-O",
|
"-O",
|
||||||
"zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
},
|
},
|
||||||
}...,
|
}...,
|
||||||
)
|
)
|
||||||
@@ -669,12 +683,12 @@ func (op *AgentOsOperator) installZSH() [][]string {
|
|||||||
{
|
{
|
||||||
"chmod",
|
"chmod",
|
||||||
"+x",
|
"+x",
|
||||||
"zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"sh",
|
"sh",
|
||||||
"-c",
|
"-c",
|
||||||
"./zsh-install.sh",
|
"/root/wdd/zsh-install.sh",
|
||||||
},
|
},
|
||||||
}...,
|
}...,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var agentOP = AgentOsOperator{
|
var agentOP = &AgentOsOperator{
|
||||||
InstallCommandPrefix: []string{
|
InstallCommandPrefix: []string{
|
||||||
"apt-get", "install", "-y",
|
"apt-get", "install", "-y",
|
||||||
},
|
},
|
||||||
@@ -15,7 +15,7 @@ var agentOP = AgentOsOperator{
|
|||||||
IsAgentInnerWall: true,
|
IsAgentInnerWall: true,
|
||||||
AgentArch: "amd64",
|
AgentArch: "amd64",
|
||||||
AgentOSReleaseCode: "focal",
|
AgentOSReleaseCode: "focal",
|
||||||
AgentServerInfo: register.AgentServerInfo{
|
AgentServerInfo: ®ister.AgentServerInfo{
|
||||||
ServerName: "",
|
ServerName: "",
|
||||||
ServerIPPbV4: "",
|
ServerIPPbV4: "",
|
||||||
ServerIPInV4: "192.168.0.8",
|
ServerIPInV4: "192.168.0.8",
|
||||||
@@ -47,7 +47,7 @@ func TestBaseFunc(t *testing.T) {
|
|||||||
//agentOP.Exec("modifyHostname")
|
//agentOP.Exec("modifyHostname")
|
||||||
//agentOP.Exec("disableSwap")
|
//agentOP.Exec("disableSwap")
|
||||||
//agentOP.Exec("enableSwap")
|
//agentOP.Exec("enableSwap")
|
||||||
//agentOP.Exec("removeDocker")
|
agentOP.Exec("removeDocker")
|
||||||
//agentOP.Exec("installDocker", "20")
|
//agentOP.Exec("installDocker", "20")
|
||||||
//agentOP.Exec("removeDockerCompose")
|
//agentOP.Exec("removeDockerCompose")
|
||||||
//agentOP.Exec("installDockerCompose")
|
//agentOP.Exec("installDockerCompose")
|
||||||
@@ -55,6 +55,6 @@ func TestBaseFunc(t *testing.T) {
|
|||||||
//agentOP.Exec("installHarbor")
|
//agentOP.Exec("installHarbor")
|
||||||
//agentOP.Exec("chronyToPublicNTP")
|
//agentOP.Exec("chronyToPublicNTP")
|
||||||
//agentOP.Exec("chronyToMaster", "192.168.0.8")
|
//agentOP.Exec("chronyToMaster", "192.168.0.8")
|
||||||
agentOP.Exec("installZSH")
|
//agentOP.Exec("installZSH")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type ExecutionMessage struct {
|
|||||||
NeedResultReplay bool `json:"needResultReplay"`
|
NeedResultReplay bool `json:"needResultReplay"`
|
||||||
DurationTask bool `json:"durationTask,default:false"`
|
DurationTask bool `json:"durationTask,default:false"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
BaseFuncContent []string `json:"baseFuncContent"`
|
||||||
SingleLineCommand []string `json:"singleLineCommand"`
|
SingleLineCommand []string `json:"singleLineCommand"`
|
||||||
MultiLineCommand [][]string `json:"multiLineCommand"`
|
MultiLineCommand [][]string `json:"multiLineCommand"`
|
||||||
PipeLineCommand [][]string `json:"pipeLineCommand"`
|
PipeLineCommand [][]string `json:"pipeLineCommand"`
|
||||||
@@ -21,24 +22,35 @@ type ExecutionMessage struct {
|
|||||||
|
|
||||||
var log = logger2.Log
|
var log = logger2.Log
|
||||||
|
|
||||||
|
var AgentOsOperatorCache = &AgentOsOperator{}
|
||||||
|
|
||||||
func Execute(em *ExecutionMessage) ([]string, error) {
|
func Execute(em *ExecutionMessage) ([]string, error) {
|
||||||
|
|
||||||
var resultLog []string
|
var resultLog []string
|
||||||
var err error
|
var err error
|
||||||
var realCommand [][]string
|
var realCommand [][]string
|
||||||
|
|
||||||
if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 {
|
if strings.HasPrefix(em.Type, "BASE") {
|
||||||
// 管道命令
|
// base function
|
||||||
resultLog, err = PipeLineCommandExecutor(em.PipeLineCommand)
|
resultLog = AgentOsOperatorCache.Exec(em.BaseFuncContent[0], em.BaseFuncContent[1:]...)
|
||||||
realCommand = em.PipeLineCommand
|
err = nil
|
||||||
} else if em.MultiLineCommand != nil && len(em.MultiLineCommand) != 0 {
|
|
||||||
// 多行命令
|
|
||||||
resultLog, err = MultiLineCommandExecutor(em.MultiLineCommand)
|
|
||||||
realCommand = em.MultiLineCommand
|
|
||||||
} else {
|
} else {
|
||||||
// 单行命令
|
// shell command
|
||||||
resultLog, err = SingleLineCommandExecutor(em.SingleLineCommand)
|
|
||||||
realCommand = [][]string{em.SingleLineCommand}
|
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}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 归一化错误和日志
|
// 归一化错误和日志
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ package executor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadTimeCommandExecutor(singleLineCommand []string) {
|
func ReadTimeCommandExecutor(singleLineCommand []string) []string {
|
||||||
|
|
||||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
@@ -23,18 +22,28 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
|
|||||||
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go copyOutput(stdout)
|
var resultSlice []string
|
||||||
go copyOutput(stderr)
|
resultSlice = append(resultSlice, copyOutput(stdout, resultSlice)...)
|
||||||
|
resultSlice = append(resultSlice, copyOutput(stderr, resultSlice)...)
|
||||||
|
|
||||||
if err := cmd.Wait(); err != nil {
|
if err := cmd.Wait(); err != nil {
|
||||||
log.ErrorF("command %v result error => %v", singleLineCommand, err)
|
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)
|
scanner := bufio.NewScanner(r)
|
||||||
for scanner.Scan() {
|
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