[Agent] [Executor] 优化Functional Executor

This commit is contained in:
zeaslity
2023-10-12 11:42:58 +08:00
parent 62712dfbce
commit 0a294fa81b
7 changed files with 136 additions and 71 deletions

View File

@@ -163,7 +163,7 @@ func (op *AgentOsOperator) Command(baseFuncName string, funcArgs ...string) []st
// exec the command here // exec the command here
for _, singleLineCommand := range multiLineCommand { for _, singleLineCommand := range multiLineCommand {
singleCommandResult, _ := AllOutputCommandExecutor(singleLineCommand) singleCommandResult, _ := FormatAllCommandExecutor(singleLineCommand)
result = append(result, "") result = append(result, "")
result = append(result, singleCommandResult...) result = append(result, singleCommandResult...)
@@ -203,6 +203,8 @@ func (op *AgentOsOperator) shutdownFirewall() [][]string {
func (op *AgentOsOperator) shutdownFirewallExec() (bool, []string) { func (op *AgentOsOperator) shutdownFirewallExec() (bool, []string) {
// quick sort
return false, nil return false, nil
} }

View File

@@ -48,7 +48,7 @@ func TestBaseFunc(t *testing.T) {
//agentOP.Command("disableSwap") //agentOP.Command("disableSwap")
//agentOP.Command("enableSwap") //agentOP.Command("enableSwap")
//agentOP.Command("removeDocker") //agentOP.Command("removeDocker")
agentOP.Command("installDocker", "20") //agentOP.Command("installDocker", "20")
//agentOP.Command("removeDockerCompose") //agentOP.Command("removeDockerCompose")
//agentOP.Command("installDockerCompose") //agentOP.Command("installDockerCompose")
//agentOP.Command("installHelm") //agentOP.Command("installHelm")
@@ -56,5 +56,6 @@ func TestBaseFunc(t *testing.T) {
//agentOP.Command("chronyToPublicNTP") //agentOP.Command("chronyToPublicNTP")
//agentOP.Command("chronyToMaster", "192.168.0.8") //agentOP.Command("chronyToMaster", "192.168.0.8")
//agentOP.Command("installZSH") //agentOP.Command("installZSH")
agentOP.Command("ok")
} }

View File

@@ -57,7 +57,7 @@ func Execute(em *ExecutionMessage) ([]string, error) {
realCommand = em.MultiLineCommand realCommand = em.MultiLineCommand
} else { } else {
// 单行命令 // 单行命令
resultLog, err = AllOutputCommandExecutor(em.SingleLineCommand) resultLog, err = FormatAllCommandExecutor(em.SingleLineCommand)
realCommand = [][]string{em.SingleLineCommand} realCommand = [][]string{em.SingleLineCommand}
} }
} }
@@ -117,7 +117,7 @@ func MultiLineCommandExecutor(multiLineCommandExecutor [][]string) ([]string, er
var res []string var res []string
for _, singleLineCommand := range multiLineCommandExecutor { for _, singleLineCommand := range multiLineCommandExecutor {
singleLogs, err := AllOutputCommandExecutor(singleLineCommand) singleLogs, err := FormatAllCommandExecutor(singleLineCommand)
res = append(res, singleLogs...) res = append(res, singleLogs...)
if err != nil { if err != nil {
log.Error(fmt.Sprintf("Execution error ! command is %v, error is %v", singleLineCommand, err)) log.Error(fmt.Sprintf("Execution error ! command is %v, error is %v", singleLineCommand, err))

View File

@@ -7,6 +7,7 @@ import (
"os/exec" "os/exec"
) )
// ReadTimeCommandExecutor 执行命令,并且实时返回结果
func ReadTimeCommandExecutor(singleLineCommand []string) { func ReadTimeCommandExecutor(singleLineCommand []string) {
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
@@ -31,8 +32,45 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
} }
// AllOutputCommandExecutor 收集全部执行结果、错误并且返回 // AllCommandExecutor 正确或者错误的信息全部收集,共同返回
func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) { func AllCommandExecutor(singleLineCommand []string) (bool, []string) {
// result
var resultSlice []string
resultOk := true
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.DebugF("command %v stdout error => %v", singleLineCommand, err)
resultOk = false
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.DebugF("command %v stderr error => %v", singleLineCommand, err)
resultOk = false
}
if err := cmd.Start(); err != nil {
log.DebugF("command %v runtime error => %v", singleLineCommand, err)
resultOk = false
}
// 收集错误或者
resultSlice = collectOutput(stdout, resultSlice)
resultSlice = collectOutput(stderr, resultSlice)
if err := cmd.Wait(); err != nil {
log.DebugF("command %v result error => %v", singleLineCommand, err)
resultOk = false
}
log.DebugF("all out command executor result are => %v", resultSlice)
return resultOk, resultSlice
}
// FormatAllCommandExecutor 收集全部执行结果、错误并且返回
func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
// result // result
var resultSlice []string var resultSlice []string
@@ -54,7 +92,7 @@ func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
log.ErrorF("command %v runtime error => %v", singleLineCommand, err) log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
} }
resultSlice = append(resultSlice, fmt.Sprintf(" ========= 命令 ====> %v", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓") resultSlice = append(resultSlice, fmt.Sprintf(" 开始执行命令 ====> %s", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓")
resultSlice = collectOutput(stdout, resultSlice) resultSlice = collectOutput(stdout, resultSlice)
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓") resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
resultSlice = collectOutput(stderr, resultSlice) resultSlice = collectOutput(stderr, resultSlice)
@@ -95,7 +133,7 @@ func PureResultSingleExecute(singleCommand []string) bool {
cmd := exec.Command(singleCommand[0], singleCommand[1:]...) cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.DebugF("指令 %s 执行 错误", singleCommand) log.ErrorF("指令 %s 执行 错误, 错误内容为 %s", singleCommand, err.Error())
return false return false
} else { } else {
log.DebugF("指令 %s 执行 成功", singleCommand) log.DebugF("指令 %s 执行 成功", singleCommand)

View File

@@ -2,12 +2,36 @@ package executor
import "testing" import "testing"
func TestReadTimeOutput(t *testing.T) { var closeSELinux = []string{
strings := []string{ "sed",
"/bin/bash", "-i",
"/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh", "s/SELINUX=enforcing/SELINUX=disabled/g",
} "/etc/selinux/config",
}
AllOutputCommandExecutor(strings) var callShellScript = []string{
"/bin/bash",
"/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh",
}
var ifconfigCommand = []string{"ifconfig"}
func TestReadTimeOutput(t *testing.T) {
ReadTimeCommandExecutor(ifconfigCommand)
}
func TestAllCommandExecutor(t *testing.T) {
ok, result := AllCommandExecutor(ifconfigCommand)
t.Logf("执行结果为 => %#v", ok)
t.Logf("执行日志为 => %#v", result)
}
func TestPureResultSingleExecute(t *testing.T) {
PureResultSingleExecute(closeSELinux)
} }

View File

@@ -1,53 +1,53 @@
package executor package executor
import ( //import (
"fmt" // "fmt"
"os/exec" // "os/exec"
"strings" // "strings"
"syscall" // "syscall"
"unsafe" // "unsafe"
) //)
//
var ( //var (
user32DLL = syscall.NewLazyDLL("user32.dll") // user32DLL = syscall.NewLazyDLL("user32.dll")
//
messageBox = user32DLL.NewProc("MessageBoxW") // messageBox = user32DLL.NewProc("MessageBoxW")
) //)
//
func FindPublicIpAddress() { //func FindPublicIpAddress() {
//
cmd := exec.Command("curl", "ifconfig.io") // cmd := exec.Command("curl", "ifconfig.io")
output, err := cmd.Output() // output, err := cmd.Output()
if err != nil { // if err != nil {
fmt.Println("无法获取公网IP地址", err) // fmt.Println("无法获取公网IP地址", err)
return // return
} // }
//
ip := strings.TrimSpace(string(output)) // ip := strings.TrimSpace(string(output))
fmt.Println("公网IP地址:", ip) // fmt.Println("公网IP地址:", ip)
//
fmt.Println("") // fmt.Println("")
} //}
//
func CallAgent() { //func CallAgent() {
var ( // var (
hWnd uintptr // hWnd uintptr
text = "Hello, World!" // text = "Hello, World!"
caption = "Golang Win32API" // caption = "Golang Win32API"
flags = uint(0x00000001) // MB_OK // flags = uint(0x00000001) // MB_OK
) // )
//
ret, _, err := messageBox.Call( // ret, _, err := messageBox.Call(
hWnd, // hWnd,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), // uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), // uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
uintptr(flags), // uintptr(flags),
) // )
if ret == 0 { // if ret == 0 {
fmt.Println("MessageBox failed:", err) // fmt.Println("MessageBox failed:", err)
return // return
} // }
//
fmt.Println("MessageBox returned:", ret) // fmt.Println("MessageBox returned:", ret)
//
} //}

View File

@@ -1,8 +1,8 @@
package executor package executor
import "testing" //import "testing"
//
func TestCallAgent(t *testing.T) { //func TestCallAgent(t *testing.T) {
//
FindPublicIpAddress() // FindPublicIpAddress()
} //}