[ Agent ] [ Executor ] 优化Executor部分的代码 - 完成
This commit is contained in:
@@ -54,7 +54,7 @@ func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
|
|||||||
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
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 = collectOutput(stdout, resultSlice)
|
||||||
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
|
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
|
||||||
resultSlice = collectOutput(stderr, resultSlice)
|
resultSlice = collectOutput(stderr, resultSlice)
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ public class FuncServiceImpl implements FuncService {
|
|||||||
funcArgs,
|
funcArgs,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
true,
|
true,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ public class XrayCallAgent {
|
|||||||
updateCommandType,
|
updateCommandType,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
null,
|
||||||
updateXrayCommandList,
|
updateXrayCommandList,
|
||||||
false,
|
false,
|
||||||
false
|
false
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,11 +4,10 @@ import io.swagger.annotations.Api;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
import io.wdd.common.response.R;
|
import io.wdd.common.response.R;
|
||||||
|
import io.wdd.rpc.beans.request.ExecutionEntity;
|
||||||
import io.wdd.rpc.execute.service.ExecutionService;
|
import io.wdd.rpc.execute.service.ExecutionService;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -100,20 +99,23 @@ public class ExecutionController {
|
|||||||
|
|
||||||
@PostMapping("/command/sync/one")
|
@PostMapping("/command/sync/one")
|
||||||
@ApiOperation("[命令] [同步] - 单机-等待命令结果")
|
@ApiOperation("[命令] [同步] - 单机-等待命令结果")
|
||||||
public R<List<String>> SyncPatchCommandToAgent(
|
public R<List<String>> SyncCommandToAgent(
|
||||||
@RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName,
|
@RequestParam(value = "topicName") @ApiParam(name = "topicName", value = "目标主机名称") String topicName,
|
||||||
@RequestParam(value = "commandList", required = false)
|
@RequestParam(value = "commandList", required = false)
|
||||||
@ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
|
@ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
|
||||||
@RequestParam(value = "completeCommandList", required = false)
|
@RequestParam(value = "completeCommandList", required = false)
|
||||||
@ApiParam(name = "completeCommandList", value = "完整命令行,优先,可为空") @Nullable List<List<String>> completeCommandList,
|
@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
|
ArrayList<String> resultLog = executionService
|
||||||
.SendCommandToAgent(
|
.SendCommandToAgent(
|
||||||
topicName,
|
topicName,
|
||||||
type,
|
executionType,
|
||||||
null,
|
null,
|
||||||
|
pipeLineCommand,
|
||||||
commandList,
|
commandList,
|
||||||
completeCommandList,
|
completeCommandList,
|
||||||
true,
|
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")
|
@PostMapping("/command/sync/batch")
|
||||||
@ApiOperation("[命令] [同步] - 批量-等待命令结果")
|
@ApiOperation("[命令] [同步] - 批量-等待命令结果")
|
||||||
public R<List<ArrayList<String>>> SyncPatchCommandToAgentBatch(
|
public R<List<ArrayList<String>>> SyncPatchCommandToAgentBatch(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -19,7 +20,8 @@ public class ExecutionMessage {
|
|||||||
* 是否需要返回 命令行的处理调用结果
|
* 是否需要返回 命令行的处理调用结果
|
||||||
* 通过 MQ返回
|
* 通过 MQ返回
|
||||||
*/
|
*/
|
||||||
@JsonProperty(defaultValue = "false")
|
@JsonProperty(defaultValue = "true")
|
||||||
|
@Nullable
|
||||||
boolean needResultReplay;
|
boolean needResultReplay;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,28 +29,40 @@ public class ExecutionMessage {
|
|||||||
* 是否是长时间持续执行任务
|
* 是否是长时间持续执行任务
|
||||||
*/
|
*/
|
||||||
@JsonProperty(defaultValue = "false")
|
@JsonProperty(defaultValue = "false")
|
||||||
|
@Nullable
|
||||||
boolean durationTask;
|
boolean durationTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于区分 ExecutionMessage的类型
|
* 用于区分 ExecutionMessage的类型
|
||||||
* BASE APP
|
* BASE APP
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private String executionType;
|
private String executionType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行功能脚本时需要的参数
|
* 执行功能脚本时需要的参数
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private List<String> funcContent;
|
private List<String> funcContent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 只有一行的命令行
|
* 只有一行的命令行
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private List<String> singleLineCommand;
|
private List<String> singleLineCommand;
|
||||||
/**
|
/**
|
||||||
* add in 2023-1-17
|
* add in 2023-1-17
|
||||||
* 页面定时脚本任务 需要传递完整的命令列表
|
* 页面定时脚本任务 需要传递完整的命令列表
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
private List<List<String>> multiLineCommand;
|
private List<List<String>> multiLineCommand;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add in 2023年8月14日
|
||||||
|
* 管道命令
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private List<List<String>> pipeLineCommand;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public interface ExecutionService {
|
|||||||
String agentTopicName,
|
String agentTopicName,
|
||||||
String executionType,
|
String executionType,
|
||||||
List<String> funcContent,
|
List<String> funcContent,
|
||||||
|
List<List<String>> pipeLineCommand,
|
||||||
List<String> commandList,
|
List<String> commandList,
|
||||||
List<List<String>> commandListComplete,
|
List<List<String>> commandListComplete,
|
||||||
boolean needResultReplay,
|
boolean needResultReplay,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class ExecutionServiceImpl implements ExecutionService {
|
|||||||
OMessageToAgentSender oMessageToAgentSender;
|
OMessageToAgentSender oMessageToAgentSender;
|
||||||
|
|
||||||
@Override
|
@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;
|
ArrayList<String> commandResultLog = null;
|
||||||
|
|
||||||
@@ -49,6 +49,7 @@ public class ExecutionServiceImpl implements ExecutionService {
|
|||||||
ExecutionMessage executionMessage = this
|
ExecutionMessage executionMessage = this
|
||||||
.generateExecutionMessage(
|
.generateExecutionMessage(
|
||||||
executionType,
|
executionType,
|
||||||
|
pipeLineCommand,
|
||||||
commandList,
|
commandList,
|
||||||
commandListComplete,
|
commandListComplete,
|
||||||
needResultReplay,
|
needResultReplay,
|
||||||
@@ -128,13 +129,14 @@ public class ExecutionServiceImpl implements ExecutionService {
|
|||||||
.build();
|
.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
|
return ExecutionMessage
|
||||||
.builder()
|
.builder()
|
||||||
.executionType(executionType)
|
.executionType(executionType)
|
||||||
.singleLineCommand(commandList)
|
.singleLineCommand(commandList)
|
||||||
.multiLineCommand(commandListComplete)
|
.multiLineCommand(commandListComplete)
|
||||||
|
.pipeLineCommand(pipeLineCommand)
|
||||||
.needResultReplay(needResultReplay)
|
.needResultReplay(needResultReplay)
|
||||||
.durationTask(durationTask)
|
.durationTask(durationTask)
|
||||||
.build();
|
.build();
|
||||||
@@ -167,6 +169,7 @@ public class ExecutionServiceImpl implements ExecutionService {
|
|||||||
agentTopicName,
|
agentTopicName,
|
||||||
executionType,
|
executionType,
|
||||||
funcContent,
|
funcContent,
|
||||||
|
null,
|
||||||
commandList,
|
commandList,
|
||||||
commandListComplete,
|
commandListComplete,
|
||||||
needResultReplay,
|
needResultReplay,
|
||||||
|
|||||||
29
server/src/test/java/io/wdd/server/ExecutionEntityJson.json
Normal file
29
server/src/test/java/io/wdd/server/ExecutionEntityJson.json
Normal 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"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user