[ Agent ] [ Executor ] 优化Executor部分的代码 - 完成

This commit is contained in:
zeaslity
2023-08-14 11:35:28 +08:00
parent 129886bd57
commit 497677d727
9 changed files with 102 additions and 11 deletions

View File

@@ -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)

View File

@@ -55,6 +55,7 @@ public class FuncServiceImpl implements FuncService {
funcArgs,
null,
null,
null,
true,
false
);

View File

@@ -136,6 +136,7 @@ public class XrayCallAgent {
updateCommandType,
null,
null,
null,
updateXrayCommandList,
false,
false

View File

@@ -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;
}

View File

@@ -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<List<String>> SyncPatchCommandToAgent(
public R<List<String>> SyncCommandToAgent(
@RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName,
@RequestParam(value = "commandList", required = false)
@ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
@RequestParam(value = "completeCommandList", required = false)
@ApiParam(name = "completeCommandList", value = "完整命令行,优先,可为空") @Nullable List<List<String>> completeCommandList,
@RequestParam(value = "type", required = false) @ApiParam(name = "type", value = "执行命令类型") @Nullable String type
@RequestParam(value = "pipeLineCommand", required = false)
@ApiParam(name = "pipeLineCommand", value = "完整命令行,优先,可为空") @Nullable List<List<String>> pipeLineCommand,
@RequestParam(value = "executionType", required = false) @ApiParam(name = "executionType", value = "执行命令类型") @Nullable String executionType
) {
ArrayList<String> 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<List<String>> SyncCommandToAgentBody(
@RequestBody @Validated ExecutionEntity executionEntity
) {
ArrayList<String> 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<List<ArrayList<String>>> SyncPatchCommandToAgentBatch(

View File

@@ -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<String> funcContent;
/**
* 只有一行的命令行
*/
@Nullable
private List<String> singleLineCommand;
/**
* add in 2023-1-17
* 页面定时脚本任务 需要传递完整的命令列表
*/
@Nullable
private List<List<String>> multiLineCommand;
/**
* add in 2023年8月14日
* 管道命令
*/
@Nullable
private List<List<String>> pipeLineCommand;
}

View File

@@ -13,6 +13,7 @@ public interface ExecutionService {
String agentTopicName,
String executionType,
List<String> funcContent,
List<List<String>> pipeLineCommand,
List<String> commandList,
List<List<String>> commandListComplete,
boolean needResultReplay,

View File

@@ -32,7 +32,7 @@ public class ExecutionServiceImpl implements ExecutionService {
OMessageToAgentSender oMessageToAgentSender;
@Override
public ArrayList<String> SendCommandToAgent(String agentTopicName, String executionType, List<String> funcContent, List<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, boolean durationTask) {
public ArrayList<String> SendCommandToAgent(String agentTopicName, String executionType, List<String> funcContent, List<List<String>> pipeLineCommand, List<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, boolean durationTask) {
ArrayList<String> 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<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, boolean durationTask) {
private ExecutionMessage generateExecutionMessage(String executionType, List<List<String>> pipeLineCommand, List<String> commandList, List<List<String>> 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,

View File

@@ -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"
]
]
}