[server] - 优化大量代码

This commit is contained in:
zeaslity
2023-02-16 16:07:03 +08:00
parent 2f2b9da5b7
commit e630b291e3
9 changed files with 171 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
package io.wdd.agent.agent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
@@ -19,11 +21,22 @@ public class AgentOperationInfoService {
@Resource
OMessageToServerSender oMessageToServerSender;
@Resource
ObjectMapper objectMapper;
public void AgentOpInfo(OctopusMessage octopusMessage){
// 构造结果OM
octopusMessage.setAc_time(TimeUtils.currentTime());
octopusMessage.setResult(agentServerInfo);
// 需要此部分写为String, 否则无法解析
String agentServerInfoString;
try {
agentServerInfoString = objectMapper.writeValueAsString(objectMapper);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
octopusMessage.setResult(agentServerInfoString);
// 发送相应的OM至 OctopusToServer 中
oMessageToServerSender.send(octopusMessage);

View File

@@ -40,6 +40,25 @@ public class XrayConfigDistribute {
private static final String updateCommandType = "OctopusXray";
static {
ArrayList<String> zero = new ArrayList<>(
List.of(
"if",
"[",
"!",
"-f",
"/usr/local/etc/xray/config.json",
"]",
";",
"then",
"touch",
"/usr/local/etc/xray/config.json",
";",
"fi"
)
);
ArrayList<String> first = new ArrayList<>(
List.of(
"mv",
@@ -79,6 +98,7 @@ public class XrayConfigDistribute {
);
updateXrayCommandList.add(zero);
updateXrayCommandList.add(first);
updateXrayCommandList.add(second);
updateXrayCommandList.add(third);
@@ -146,19 +166,21 @@ public class XrayConfigDistribute {
}
// 修改命令中的时间
String s = updateXrayCommandList
.get(0)
.get(1)
.get(2);
updateXrayCommandList
.get(0)
.get(1)
.set(
2,
s.replace("TIME",
formatTimeString)
s.replace(
"TIME",
formatTimeString
)
);
// 修改命令中的下载url
updateXrayCommandList
.get(1)
.get(2)
.set(
1,
realUrl

View File

@@ -181,6 +181,8 @@ public class OctopusAgentServiceImpl implements OctopusAgentService {
// 是当前需要的消息信息
try {
// 解析当前信息
ServerInfoVO serverInfoVO = objectMapper.readValue(
(String) message.getResult(),
new TypeReference<ServerInfoVO>() {

View File

@@ -21,14 +21,14 @@ public class AgentController {
OctopusAgentService octopusAgentService;
@GetMapping("/version")
@ApiOperation("获取所有OctopusAgent的版本")
@ApiOperation("[版本]-所有OctopusAgent")
public R<Map<String, String>> getAllAgentVersion(){
return R.ok(octopusAgentService.getAllAgentVersion());
}
@GetMapping("/coreInfo")
@ApiOperation("获取所有OctopusAgent的核心信息")
@ApiOperation("[核心信息]-所有OctopusAgent")
public R<Map<String, ServerInfoVO>> getAllAgentCoreInfo(){
return R.ok(octopusAgentService.getAllAgentCoreInfo());

View File

@@ -6,7 +6,6 @@ import io.swagger.annotations.ApiParam;
import io.wdd.common.beans.response.R;
import io.wdd.rpc.execute.result.BuildStreamReader;
import io.wdd.rpc.execute.service.CoreExecutionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -17,6 +16,7 @@ import javax.annotation.Resource;
import java.util.List;
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_TOPIC_NAME_LIST;
@RestController
@RequestMapping("/octopus/server/executor")
@@ -25,12 +25,10 @@ public class ExecutionController {
@Resource
CoreExecutionService coreExecutionService;
@Resource
BuildStreamReader buildStreamReader;
@PostMapping("command")
@PostMapping("/command/one")
@ApiOperation("[命令]-手动发送命令")
public R<String> patchCommandToAgent(
@RequestParam(value = "topicName") String topicName,
@@ -38,24 +36,49 @@ public class ExecutionController {
@RequestParam(value = "type", required = false) @Nullable String type
) {
String streamKey = "";
if (StringUtils.isEmpty(type)) {
streamKey = coreExecutionService.SendCommandToAgent(
topicName,
commandList
);
} else {
streamKey = coreExecutionService.SendCommandToAgent(
String streamKey = coreExecutionService
.SendCommandToAgent(
topicName,
type,
commandList
);
}
return R.ok(streamKey);
}
@PostMapping("/command/batch")
@ApiOperation("[命令]- 批量发送命令")
public R<List<String>> patchCommandToAgentList(
@RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList,
@RequestParam(value = "commandList", required = false)
@ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
@RequestParam(value = "type", required = false) @Nullable String type
) {
return R.ok(coreExecutionService.SendCommandToAgent(
topicNameList,
type,
commandList
));
}
@PostMapping("/command/all")
@ApiOperation("[命令]- 发送命令至所有的主机")
public R<List<String>> patchCommandToAgentAll(
@RequestParam(value = "commandList", required = false)
@ApiParam(name = "commandList", value = "命令行") @Nullable List<String> commandList,
@RequestParam(value = "type", required = false) @Nullable String type
) {
return R.ok(coreExecutionService.SendCommandToAgent(
ALL_AGENT_TOPIC_NAME_LIST,
type,
commandList
));
}
@PostMapping("/agentStatusStream")
@ApiOperation("切换Console查看Agent状态日志")
@@ -74,46 +97,49 @@ public class ExecutionController {
// auth required
@PostMapping("/AgentUpdate")
@ApiOperation("控制Agent升级的接口")
public R<String> AgentUpdate(
@RequestParam(value = "agentTopicName") String agentTopicName
@PostMapping("/function/update")
@ApiOperation("升级")
public R<List<String>> AgentUpdate(
@RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) {
return R.ok(
coreExecutionService
.SendCommandToAgent(
agentTopicName,
topicNameList,
"AgentUpdate",
null
));
}
@PostMapping("/AgentReboot")
@ApiOperation("控制Agent重启的接口")
public R<String> AgentReboot(
@RequestParam(value = "agentTopicName") String agentTopicName
@PostMapping("/function/reboot")
@ApiOperation("重启")
public R<List<String>> AgentReboot(
@RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) {
return R.ok(
coreExecutionService
.SendCommandToAgent(
agentTopicName,
"AgentRestart",
topicNameList,
"AgentReboot",
null
));
}
@PostMapping("/AgentShutdown")
@ApiOperation("控制Agent关闭的接口")
public R<String> AgentShutdown(
@RequestParam(value = "agentTopicName") String agentTopicName
@PostMapping("/function/shutdown")
@ApiOperation("关闭")
public R<List<String>> AgentShutdown(
@RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) {
return R.ok(
coreExecutionService
.SendCommandToAgent(
agentTopicName,
topicNameList,
"AgentShutdown",
null
));

View File

@@ -1,17 +1,20 @@
package io.wdd.rpc.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.wdd.common.beans.response.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_STATUS_MAP;
import static io.wdd.rpc.init.ServerBootUpEnvironment.STATUS_AGENT_LIST_MAP;
import static io.wdd.rpc.init.ServerBootUpEnvironment.*;
@RestController
@@ -20,18 +23,49 @@ import static io.wdd.rpc.init.ServerBootUpEnvironment.STATUS_AGENT_LIST_MAP;
public class StatusController {
@ApiOperation("获取所有Agent的运行状态")
@GetMapping("/agentStatus")
@ApiOperation("[ Agent-状态 ] Map")
@GetMapping("/agent/status")
public R<Map> GetAllAgentHealthyStatus() {
return R.ok(ALL_AGENT_STATUS_MAP);
}
@ApiOperation("获取 状态-Agent Map")
@GetMapping("/statusAgent")
@ApiOperation("[ 状态-Agent ] Map")
@GetMapping("/status/agent")
public R<Map> GetHealthyStatusAgentList() {
return R.ok(STATUS_AGENT_LIST_MAP);
}
@ApiOperation("[ Agent ] - 所有的Agent主机")
@PostMapping("/agent/all")
public R<List<String>> GetAllAgentTopicNameList() {
return R.ok(ALL_AGENT_TOPIC_NAME_LIST);
}
@ApiOperation("[ Agent ] - 所有主机String")
@PostMapping("/agent/all/string")
public R<String> GetAllAgentString() {
String data = ALL_AGENT_TOPIC_NAME_LIST.toString();
data.replace("[","");
data.replace("]","");
return R.ok(
data
);
}
@ApiOperation("[ Agent ] - 健康的Agent主机")
@PostMapping("/agent/healthy")
public R<List<String>> GetAllHealthyAgent() {
return R.ok(ALL_HEALTHY_AGENT_TOPIC_NAME_LIST);
}
}

View File

@@ -62,4 +62,13 @@ public interface CoreExecutionService {
List<String> SendCommandToAgentComplete(List<String> agentTopicNameList, String type, List<List<String>> completeCommandList, HashMap<String, String> atnFutureKey);
/**
* 向所有的Agent发送命令
* @param type
* @param commandList
* @return
*/
@Deprecated
List<String> SendCommandToAgentAll(String type, List<String> commandList);
}

View File

@@ -10,6 +10,7 @@ import io.wdd.rpc.execute.config.ExecutionLog;
import io.wdd.rpc.execute.result.BuildStreamReader;
import io.wdd.rpc.message.sender.OMessageToAgentSender;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -20,6 +21,7 @@ import java.util.List;
import java.util.stream.Collectors;
import static io.wdd.rpc.execute.service.ExecutionResultDaemonHandler.WAIT_EXECUTION_RESULT_LIST;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_TOPIC_NAME_LIST;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_TOPIC_NAME_SET;
@Service
@@ -35,8 +37,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Resource
RedisTemplate redisTemplate;
@Resource
BuildStreamReader buildStreamReader;
private static final String MANUAL_COMMAND_TYPE = "manual-command";
@Override
public String SendCommandToAgent(String agentTopicName, String command) {
@@ -50,7 +51,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
public String SendCommandToAgent(String agentTopicName, List<String> commandList) {
return this.SendCommandToAgent(
agentTopicName,
"manual-command",
MANUAL_COMMAND_TYPE,
commandList
);
}
@@ -58,6 +59,11 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Override
public String SendCommandToAgent(String agentTopicName, String type, List<String> commandList) {
// 归一化type
if (StringUtils.isEmpty(type)) {
type = MANUAL_COMMAND_TYPE;
}
return SendCommandToAgent(
agentTopicName,
type,
@@ -234,6 +240,12 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
.collect(Collectors.toList());
}
@Override
public List<String> SendCommandToAgentAll(String type, List<String> commandList) {
return null;
}
@Deprecated
private OctopusMessage generateOctopusMessage(String agentTopicName, String resultKey, String type, List<String> commandList, List<List<String>> commandListComplete) {

View File

@@ -142,10 +142,11 @@ public class ServerInfoVO {
* octopus message unique key name
*/
private String topicName;
/**
*
*/
private String comment;
private String agentVersion;
}