From ebb0759737cdaab375def67dc9031bbe981ddcd9 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 24 Oct 2023 14:04:52 +0800 Subject: [PATCH] [ Agent ] [ Executor ] - fix bugs --- agent-go/executor/CommandExecutor.go | 9 +++++---- agent-go/executor/FunctionalExecutor.go | 7 ++++++- agent-go/shell/agent-bootup.sh | 7 +++++-- server-go/main.go | 6 +++--- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/agent-go/executor/CommandExecutor.go b/agent-go/executor/CommandExecutor.go index e7880a0..5366524 100644 --- a/agent-go/executor/CommandExecutor.go +++ b/agent-go/executor/CommandExecutor.go @@ -12,7 +12,7 @@ import ( type ExecutionMessage struct { NeedResultReplay bool `json:"needResultReplay"` DurationTask bool `json:"durationTask,default:false"` - Type string `json:"type"` + ExecutionType string `json:"executionType"` FuncContent []string `json:"funcContent"` SingleLineCommand []string `json:"singleLineCommand"` MultiLineCommand [][]string `json:"multiLineCommand"` @@ -31,7 +31,9 @@ func Execute(em *ExecutionMessage) ([]string, error) { var realCommand [][]string ok := true - if strings.HasPrefix(em.Type, "BASE") { + log.DebugF("em message is => %#v", em) + + if strings.HasPrefix(em.ExecutionType, "BASE") { // base function if len(em.FuncContent) > 1 { ok, resultLog = AgentOsOperatorCache.Exec(em.FuncContent[0], em.FuncContent[1:]...) @@ -42,7 +44,7 @@ func Execute(em *ExecutionMessage) ([]string, error) { return resultLog, nil } - } else if strings.HasPrefix(em.Type, "APP") { + } else if strings.HasPrefix(em.ExecutionType, "APP") { // app function if len(em.FuncContent) > 1 { ok, resultLog = AgentOsOperatorCache.Deploy(em.FuncContent[0], em.FuncContent[1:]...) @@ -57,7 +59,6 @@ func Execute(em *ExecutionMessage) ([]string, error) { } else { // shell command - if em.PipeLineCommand != nil && len(em.PipeLineCommand) != 0 { // 管道命令 resultLog, err = PipeLineCommandExecutor(em.PipeLineCommand) diff --git a/agent-go/executor/FunctionalExecutor.go b/agent-go/executor/FunctionalExecutor.go index 950eb97..580eb52 100644 --- a/agent-go/executor/FunctionalExecutor.go +++ b/agent-go/executor/FunctionalExecutor.go @@ -70,8 +70,13 @@ func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) { // result var resultSlice []string var resultError error + var cmd *exec.Cmd - cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) + if len(singleLineCommand) > 1 { + cmd = exec.Command(singleLineCommand[0], singleLineCommand[1:]...) + } else { + cmd = exec.Command(singleLineCommand[0]) + } stdout, err := cmd.StdoutPipe() if err != nil { log.ErrorF("command %v stdout error => %v", singleLineCommand, err) diff --git a/agent-go/shell/agent-bootup.sh b/agent-go/shell/agent-bootup.sh index 793f418..5ecbb34 100644 --- a/agent-go/shell/agent-bootup.sh +++ b/agent-go/shell/agent-bootup.sh @@ -277,9 +277,12 @@ CheckAndDownloadLatestVersion() { echo "" colorEcho $BLUE "start to download the agent config!" - agentConfigFileList=$(curl "$AgentConfigUrl/" | grep -v h1 | grep "a href=" | awk '{print$2}' | cut -d">" -f2 | cut -d"<" -f1 | cut -d"_" -f4-) + rm -rf index.html + agentConfigFileList=$(curl "$AgentConfigUrl/" | grep -v h1 | grep "a href=" | awk '{print$2}' | cut -d">" -f2 | cut -d"<" -f1 | cut -d"_" -f4- | tr "\n" " ") - for agentConfig in "${agentConfigFileList[@]}"; do + IFS=" " read -ra file_array <<<"$agentConfigFileList" + + for agentConfig in "${file_array[@]}"; do colorEcho $BLUE "agent config file is => $agentConfig" wget -q "$AgentConfigUrl/$agentConfig" echo "" diff --git a/server-go/main.go b/server-go/main.go index 94786fd..953f484 100644 --- a/server-go/main.go +++ b/server-go/main.go @@ -61,7 +61,7 @@ func main() { // 打印文件信息 fmt.Printf("File Name: %s\n", file.Filename) fmt.Printf("File Size: %d bytes\n", file.Size) - fmt.Printf("MIME Type: %s\n", file.Header.Get("Content-Type")) + fmt.Printf("MIME ExecutionType: %s\n", file.Header.Get("Content-ExecutionType")) // 保存文件到本地 err = c.SaveUploadedFile(file, "./"+file.Filename) @@ -87,7 +87,7 @@ func main() { // 打印文件信息 fmt.Printf("File Name: %s\n", file.Filename) fmt.Printf("File Size: %d bytes\n", file.Size) - fmt.Printf("MIME Type: %s\n", file.Header.Get("Content-Type")) + fmt.Printf("MIME ExecutionType: %s\n", file.Header.Get("Content-ExecutionType")) // 保存文件到本地 err = c.SaveUploadedFile(file, "./"+file.Filename) @@ -112,7 +112,7 @@ func main() { // 设置响应头,使浏览器可以下载文件 c.Header("Content-Disposition", "attachment; filename="+fileName) - c.Header("Content-Type", "application/octet-stream") + c.Header("Content-ExecutionType", "application/octet-stream") c.Header("Content-Transfer-Encoding", "binary") c.Header("Cache-Control", "no-cache")