From 497677d727945f9ee0606d03aa1b5e8c5d02b092 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Mon, 14 Aug 2023 11:35:28 +0800 Subject: [PATCH] =?UTF-8?q?[=20Agent=20]=20[=20Executor=20]=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96Executor=E9=83=A8=E5=88=86=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=20-=20=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent-go/executor/FunctionalExecutor.go | 2 +- .../func/script/service/FuncServiceImpl.java | 1 + .../wdd/func/xray/service/XrayCallAgent.java | 1 + .../rpc/beans/request/ExecutionEntity.java | 19 ++++++++++ .../rpc/controller/ExecutionController.java | 37 +++++++++++++++---- .../io/wdd/rpc/execute/ExecutionMessage.java | 16 +++++++- .../rpc/execute/service/ExecutionService.java | 1 + .../execute/service/ExecutionServiceImpl.java | 7 +++- .../io/wdd/server/ExecutionEntityJson.json | 29 +++++++++++++++ 9 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 server/src/main/java/io/wdd/rpc/beans/request/ExecutionEntity.java create mode 100644 server/src/test/java/io/wdd/server/ExecutionEntityJson.json diff --git a/agent-go/executor/FunctionalExecutor.go b/agent-go/executor/FunctionalExecutor.go index e040a7d..4004ee8 100644 --- a/agent-go/executor/FunctionalExecutor.go +++ b/agent-go/executor/FunctionalExecutor.go @@ -54,7 +54,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(" ========= 命令为 ====> %v", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓") resultSlice = collectOutput(stdout, resultSlice) resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓") resultSlice = collectOutput(stderr, resultSlice) diff --git a/server/src/main/java/io/wdd/func/script/service/FuncServiceImpl.java b/server/src/main/java/io/wdd/func/script/service/FuncServiceImpl.java index 5e57cd6..41297b9 100644 --- a/server/src/main/java/io/wdd/func/script/service/FuncServiceImpl.java +++ b/server/src/main/java/io/wdd/func/script/service/FuncServiceImpl.java @@ -55,6 +55,7 @@ public class FuncServiceImpl implements FuncService { funcArgs, null, null, + null, true, false ); diff --git a/server/src/main/java/io/wdd/func/xray/service/XrayCallAgent.java b/server/src/main/java/io/wdd/func/xray/service/XrayCallAgent.java index e4aaea8..e2c40fb 100644 --- a/server/src/main/java/io/wdd/func/xray/service/XrayCallAgent.java +++ b/server/src/main/java/io/wdd/func/xray/service/XrayCallAgent.java @@ -136,6 +136,7 @@ public class XrayCallAgent { updateCommandType, null, null, + null, updateXrayCommandList, false, false diff --git a/server/src/main/java/io/wdd/rpc/beans/request/ExecutionEntity.java b/server/src/main/java/io/wdd/rpc/beans/request/ExecutionEntity.java new file mode 100644 index 0000000..13d4f9f --- /dev/null +++ b/server/src/main/java/io/wdd/rpc/beans/request/ExecutionEntity.java @@ -0,0 +1,19 @@ +package io.wdd.rpc.beans.request; + +import io.wdd.rpc.execute.ExecutionMessage; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.annotation.Nullable; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ExecutionEntity extends ExecutionMessage { + + @Nullable + String agentTopicName; + + +} diff --git a/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java b/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java index d3959fe..789c583 100644 --- a/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java +++ b/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java @@ -4,11 +4,10 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import io.wdd.common.response.R; +import io.wdd.rpc.beans.request.ExecutionEntity; import io.wdd.rpc.execute.service.ExecutionService; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; import javax.annotation.Nullable; import javax.annotation.Resource; @@ -100,20 +99,23 @@ public class ExecutionController { @PostMapping("/command/sync/one") @ApiOperation("[命令] [同步] - 单机-等待命令结果") - public R> SyncPatchCommandToAgent( + public R> SyncCommandToAgent( @RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName, @RequestParam(value = "commandList", required = false) @ApiParam(name = "commandList", value = "命令行") @Nullable List commandList, @RequestParam(value = "completeCommandList", required = false) @ApiParam(name = "completeCommandList", value = "完整命令行,优先,可为空") @Nullable List> completeCommandList, - @RequestParam(value = "type", required = false) @ApiParam(name = "type", value = "执行命令类型") @Nullable String type + @RequestParam(value = "pipeLineCommand", required = false) + @ApiParam(name = "pipeLineCommand", value = "完整命令行,优先,可为空") @Nullable List> pipeLineCommand, + @RequestParam(value = "executionType", required = false) @ApiParam(name = "executionType", value = "执行命令类型") @Nullable String executionType ) { ArrayList resultLog = executionService .SendCommandToAgent( topicName, - type, + executionType, null, + pipeLineCommand, commandList, completeCommandList, true, @@ -124,6 +126,27 @@ public class ExecutionController { } + @PostMapping("/command/sync/one/body") + @ApiOperation("[命令] [同步] - 单机-等待命令结果") + public R> SyncCommandToAgentBody( + @RequestBody @Validated ExecutionEntity executionEntity + ) { + + ArrayList resultLog = executionService + .SendCommandToAgent( + executionEntity.getAgentTopicName(), + executionEntity.getExecutionType(), + executionEntity.getFuncContent(), + executionEntity.getPipeLineCommand(), + executionEntity.getSingleLineCommand(), + executionEntity.getMultiLineCommand(), + executionEntity.isNeedResultReplay(), + executionEntity.isDurationTask() + ); + + return R.ok(resultLog); + } + @PostMapping("/command/sync/batch") @ApiOperation("[命令] [同步] - 批量-等待命令结果") public R>> SyncPatchCommandToAgentBatch( diff --git a/server/src/main/java/io/wdd/rpc/execute/ExecutionMessage.java b/server/src/main/java/io/wdd/rpc/execute/ExecutionMessage.java index 4a2caf9..03f8a3f 100644 --- a/server/src/main/java/io/wdd/rpc/execute/ExecutionMessage.java +++ b/server/src/main/java/io/wdd/rpc/execute/ExecutionMessage.java @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; +import javax.annotation.Nullable; import java.util.List; @Data @@ -19,7 +20,8 @@ public class ExecutionMessage { * 是否需要返回 命令行的处理调用结果 * 通过 MQ返回 */ - @JsonProperty(defaultValue = "false") + @JsonProperty(defaultValue = "true") + @Nullable boolean needResultReplay; /** @@ -27,28 +29,40 @@ public class ExecutionMessage { * 是否是长时间持续执行任务 */ @JsonProperty(defaultValue = "false") + @Nullable boolean durationTask; /** * 用于区分 ExecutionMessage的类型 * BASE APP */ + @Nullable private String executionType; /** * 执行功能脚本时需要的参数 */ + @Nullable private List funcContent; /** * 只有一行的命令行 */ + @Nullable private List singleLineCommand; /** * add in 2023-1-17 * 页面定时脚本任务 需要传递完整的命令列表 */ + @Nullable private List> multiLineCommand; + /** + * add in 2023年8月14日 + * 管道命令 + */ + @Nullable + private List> pipeLineCommand; + } diff --git a/server/src/main/java/io/wdd/rpc/execute/service/ExecutionService.java b/server/src/main/java/io/wdd/rpc/execute/service/ExecutionService.java index 885c2b5..d54827b 100644 --- a/server/src/main/java/io/wdd/rpc/execute/service/ExecutionService.java +++ b/server/src/main/java/io/wdd/rpc/execute/service/ExecutionService.java @@ -13,6 +13,7 @@ public interface ExecutionService { String agentTopicName, String executionType, List funcContent, + List> pipeLineCommand, List commandList, List> commandListComplete, boolean needResultReplay, diff --git a/server/src/main/java/io/wdd/rpc/execute/service/ExecutionServiceImpl.java b/server/src/main/java/io/wdd/rpc/execute/service/ExecutionServiceImpl.java index 09567cb..6b084fd 100644 --- a/server/src/main/java/io/wdd/rpc/execute/service/ExecutionServiceImpl.java +++ b/server/src/main/java/io/wdd/rpc/execute/service/ExecutionServiceImpl.java @@ -32,7 +32,7 @@ public class ExecutionServiceImpl implements ExecutionService { OMessageToAgentSender oMessageToAgentSender; @Override - public ArrayList SendCommandToAgent(String agentTopicName, String executionType, List funcContent, List commandList, List> commandListComplete, boolean needResultReplay, boolean durationTask) { + public ArrayList SendCommandToAgent(String agentTopicName, String executionType, List funcContent, List> pipeLineCommand, List commandList, List> commandListComplete, boolean needResultReplay, boolean durationTask) { ArrayList commandResultLog = null; @@ -49,6 +49,7 @@ public class ExecutionServiceImpl implements ExecutionService { ExecutionMessage executionMessage = this .generateExecutionMessage( executionType, + pipeLineCommand, commandList, commandListComplete, needResultReplay, @@ -128,13 +129,14 @@ public class ExecutionServiceImpl implements ExecutionService { .build(); } - private ExecutionMessage generateExecutionMessage(String executionType, List commandList, List> commandListComplete, boolean needResultReplay, boolean durationTask) { + private ExecutionMessage generateExecutionMessage(String executionType, List> pipeLineCommand, List commandList, List> commandListComplete, boolean needResultReplay, boolean durationTask) { return ExecutionMessage .builder() .executionType(executionType) .singleLineCommand(commandList) .multiLineCommand(commandListComplete) + .pipeLineCommand(pipeLineCommand) .needResultReplay(needResultReplay) .durationTask(durationTask) .build(); @@ -167,6 +169,7 @@ public class ExecutionServiceImpl implements ExecutionService { agentTopicName, executionType, funcContent, + null, commandList, commandListComplete, needResultReplay, diff --git a/server/src/test/java/io/wdd/server/ExecutionEntityJson.json b/server/src/test/java/io/wdd/server/ExecutionEntityJson.json new file mode 100644 index 0000000..dcc1d8f --- /dev/null +++ b/server/src/test/java/io/wdd/server/ExecutionEntityJson.json @@ -0,0 +1,29 @@ +{ + "agentTopicName": "Chengdu-amd64-99-98066f", + "needResultReplay": true, + "durationTask": false, + "executionType": "manual", + "funcContent": null, + "singleLineCommand": null, + "multiLineCommand": [ + [ + "apt-get", + "update" + ], + [ + "systemctl", + "status", + "nginx" + ] + ], + "pipeLineCommand": [ + [ + "cat", + "/etc/passwd" + ], + [ + "grep", + "root" + ] + ] +} \ No newline at end of file