[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
for _, singleLineCommand := range multiLineCommand {
singleCommandResult, _ := AllOutputCommandExecutor(singleLineCommand)
singleCommandResult, _ := FormatAllCommandExecutor(singleLineCommand)
result = append(result, "")
result = append(result, singleCommandResult...)
@@ -203,6 +203,8 @@ func (op *AgentOsOperator) shutdownFirewall() [][]string {
func (op *AgentOsOperator) shutdownFirewallExec() (bool, []string) {
// quick sort
return false, nil
}

View File

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

View File

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

View File

@@ -7,6 +7,7 @@ import (
"os/exec"
)
// ReadTimeCommandExecutor 执行命令,并且实时返回结果
func ReadTimeCommandExecutor(singleLineCommand []string) {
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe()
@@ -31,8 +32,45 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
}
// AllOutputCommandExecutor 收集全部执行结果、错误并且返回
func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
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
var resultSlice []string
@@ -54,7 +92,7 @@ func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
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 = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
resultSlice = collectOutput(stderr, resultSlice)
@@ -95,7 +133,7 @@ func PureResultSingleExecute(singleCommand []string) bool {
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
err := cmd.Run()
if err != nil {
log.DebugF("指令 %s 执行 错误", singleCommand)
log.ErrorF("指令 %s 执行 错误, 错误内容为 %s", singleCommand, err.Error())
return false
} else {
log.DebugF("指令 %s 执行 成功", singleCommand)

View File

@@ -2,12 +2,36 @@ package executor
import "testing"
func TestReadTimeOutput(t *testing.T) {
strings := []string{
var closeSELinux = []string{
"sed",
"-i",
"s/SELINUX=enforcing/SELINUX=disabled/g",
"/etc/selinux/config",
}
var callShellScript = []string{
"/bin/bash",
"/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh",
}
AllOutputCommandExecutor(strings)
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
import (
"fmt"
"os/exec"
"strings"
"syscall"
"unsafe"
)
var (
user32DLL = syscall.NewLazyDLL("user32.dll")
messageBox = user32DLL.NewProc("MessageBoxW")
)
func FindPublicIpAddress() {
cmd := exec.Command("curl", "ifconfig.io")
output, err := cmd.Output()
if err != nil {
fmt.Println("无法获取公网IP地址", err)
return
}
ip := strings.TrimSpace(string(output))
fmt.Println("公网IP地址:", ip)
fmt.Println("")
}
func CallAgent() {
var (
hWnd uintptr
text = "Hello, World!"
caption = "Golang Win32API"
flags = uint(0x00000001) // MB_OK
)
ret, _, err := messageBox.Call(
hWnd,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
uintptr(flags),
)
if ret == 0 {
fmt.Println("MessageBox failed:", err)
return
}
fmt.Println("MessageBox returned:", ret)
}
//import (
// "fmt"
// "os/exec"
// "strings"
// "syscall"
// "unsafe"
//)
//
//var (
// user32DLL = syscall.NewLazyDLL("user32.dll")
//
// messageBox = user32DLL.NewProc("MessageBoxW")
//)
//
//func FindPublicIpAddress() {
//
// cmd := exec.Command("curl", "ifconfig.io")
// output, err := cmd.Output()
// if err != nil {
// fmt.Println("无法获取公网IP地址", err)
// return
// }
//
// ip := strings.TrimSpace(string(output))
// fmt.Println("公网IP地址:", ip)
//
// fmt.Println("")
//}
//
//func CallAgent() {
// var (
// hWnd uintptr
// text = "Hello, World!"
// caption = "Golang Win32API"
// flags = uint(0x00000001) // MB_OK
// )
//
// ret, _, err := messageBox.Call(
// hWnd,
// uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
// uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
// uintptr(flags),
// )
// if ret == 0 {
// fmt.Println("MessageBox failed:", err)
// return
// }
//
// fmt.Println("MessageBox returned:", ret)
//
//}

View File

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