[ Agent ] [ Executor ] 初步完成部署的函数功能
This commit is contained in:
108
agent-go/executor/FunctionalExecutor.go
Normal file
108
agent-go/executor/FunctionalExecutor.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func ReadTimeCommandExecutor(singleLineCommand []string) {
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.ErrorF("command %v stdout error => %v", singleLineCommand, err)
|
||||
}
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.ErrorF("command %v stderr error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
go realTimeOutput(stdout)
|
||||
go realTimeOutput(stderr)
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.ErrorF("command %v result error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// AllOutputCommandExecutor 收集全部执行结果、错误并且返回
|
||||
func AllOutputCommandExecutor(singleLineCommand []string) []string {
|
||||
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.ErrorF("command %v stdout error => %v", singleLineCommand, err)
|
||||
}
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.ErrorF("command %v stderr error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
var resultSlice []string
|
||||
resultSlice = append(resultSlice, collectOutput(stdout, resultSlice)...)
|
||||
resultSlice = append(resultSlice, collectOutput(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 realTimeOutput(r io.Reader) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
fmt.Println(scanner.Text())
|
||||
}
|
||||
}
|
||||
|
||||
func collectOutput(r io.Reader, resultSlice []string) []string {
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
resultLine := scanner.Text()
|
||||
|
||||
resultSlice = append(resultSlice, resultLine)
|
||||
// debug usage
|
||||
//fmt.Println(resultLine)
|
||||
}
|
||||
|
||||
return resultSlice
|
||||
}
|
||||
|
||||
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
|
||||
func PureResultSingleExecute(singleCommand []string) bool {
|
||||
|
||||
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.DebugF("指令 %s 执行 错误", singleCommand)
|
||||
return false
|
||||
} else {
|
||||
log.DebugF("指令 %s 执行 成功", singleCommand)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// PureResultSingleExecuteBatch 批量 执行单行命令,忽略输出,只对执行成功与否负责
|
||||
func PureResultSingleExecuteBatch(singleCommandList [][]string) bool {
|
||||
|
||||
for _, singleCommand := range singleCommandList {
|
||||
if !PureResultSingleExecute(singleCommand) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user