[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; 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.config.beans.init.AgentServerInfo;
import io.wdd.agent.message.OMessageToServerSender; import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.rabbitmq.OctopusMessage; import io.wdd.common.beans.rabbitmq.OctopusMessage;
@@ -19,11 +21,22 @@ public class AgentOperationInfoService {
@Resource @Resource
OMessageToServerSender oMessageToServerSender; OMessageToServerSender oMessageToServerSender;
@Resource
ObjectMapper objectMapper;
public void AgentOpInfo(OctopusMessage octopusMessage){ public void AgentOpInfo(OctopusMessage octopusMessage){
// 构造结果OM // 构造结果OM
octopusMessage.setAc_time(TimeUtils.currentTime()); 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 中 // 发送相应的OM至 OctopusToServer 中
oMessageToServerSender.send(octopusMessage); oMessageToServerSender.send(octopusMessage);

View File

@@ -40,6 +40,25 @@ public class XrayConfigDistribute {
private static final String updateCommandType = "OctopusXray"; private static final String updateCommandType = "OctopusXray";
static { 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<>( ArrayList<String> first = new ArrayList<>(
List.of( List.of(
"mv", "mv",
@@ -79,6 +98,7 @@ public class XrayConfigDistribute {
); );
updateXrayCommandList.add(zero);
updateXrayCommandList.add(first); updateXrayCommandList.add(first);
updateXrayCommandList.add(second); updateXrayCommandList.add(second);
updateXrayCommandList.add(third); updateXrayCommandList.add(third);
@@ -146,19 +166,21 @@ public class XrayConfigDistribute {
} }
// 修改命令中的时间 // 修改命令中的时间
String s = updateXrayCommandList String s = updateXrayCommandList
.get(0) .get(1)
.get(2); .get(2);
updateXrayCommandList updateXrayCommandList
.get(0) .get(1)
.set( .set(
2, 2,
s.replace("TIME", s.replace(
formatTimeString) "TIME",
formatTimeString
)
); );
// 修改命令中的下载url // 修改命令中的下载url
updateXrayCommandList updateXrayCommandList
.get(1) .get(2)
.set( .set(
1, 1,
realUrl realUrl

View File

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

View File

@@ -21,14 +21,14 @@ public class AgentController {
OctopusAgentService octopusAgentService; OctopusAgentService octopusAgentService;
@GetMapping("/version") @GetMapping("/version")
@ApiOperation("获取所有OctopusAgent的版本") @ApiOperation("[版本]-所有OctopusAgent")
public R<Map<String, String>> getAllAgentVersion(){ public R<Map<String, String>> getAllAgentVersion(){
return R.ok(octopusAgentService.getAllAgentVersion()); return R.ok(octopusAgentService.getAllAgentVersion());
} }
@GetMapping("/coreInfo") @GetMapping("/coreInfo")
@ApiOperation("获取所有OctopusAgent的核心信息") @ApiOperation("[核心信息]-所有OctopusAgent")
public R<Map<String, ServerInfoVO>> getAllAgentCoreInfo(){ public R<Map<String, ServerInfoVO>> getAllAgentCoreInfo(){
return R.ok(octopusAgentService.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.common.beans.response.R;
import io.wdd.rpc.execute.result.BuildStreamReader; import io.wdd.rpc.execute.result.BuildStreamReader;
import io.wdd.rpc.execute.service.CoreExecutionService; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@@ -17,6 +16,7 @@ import javax.annotation.Resource;
import java.util.List; import java.util.List;
import static io.wdd.rpc.execute.result.RedisStreamReaderConfig.AGENT_STATUS_REDIS_STREAM_LISTENER_CONTAINER; 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 @RestController
@RequestMapping("/octopus/server/executor") @RequestMapping("/octopus/server/executor")
@@ -25,12 +25,10 @@ public class ExecutionController {
@Resource @Resource
CoreExecutionService coreExecutionService; CoreExecutionService coreExecutionService;
@Resource @Resource
BuildStreamReader buildStreamReader; BuildStreamReader buildStreamReader;
@PostMapping("/command/one")
@PostMapping("command")
@ApiOperation("[命令]-手动发送命令") @ApiOperation("[命令]-手动发送命令")
public R<String> patchCommandToAgent( public R<String> patchCommandToAgent(
@RequestParam(value = "topicName") String topicName, @RequestParam(value = "topicName") String topicName,
@@ -38,24 +36,49 @@ public class ExecutionController {
@RequestParam(value = "type", required = false) @Nullable String type @RequestParam(value = "type", required = false) @Nullable String type
) { ) {
String streamKey = ""; String streamKey = coreExecutionService
.SendCommandToAgent(
if (StringUtils.isEmpty(type)) {
streamKey = coreExecutionService.SendCommandToAgent(
topicName,
commandList
);
} else {
streamKey = coreExecutionService.SendCommandToAgent(
topicName, topicName,
type, type,
commandList commandList
); );
}
return R.ok(streamKey); 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") @PostMapping("/agentStatusStream")
@ApiOperation("切换Console查看Agent状态日志") @ApiOperation("切换Console查看Agent状态日志")
@@ -74,46 +97,49 @@ public class ExecutionController {
// auth required // auth required
@PostMapping("/AgentUpdate") @PostMapping("/function/update")
@ApiOperation("控制Agent升级的接口") @ApiOperation("升级")
public R<String> AgentUpdate( public R<List<String>> AgentUpdate(
@RequestParam(value = "agentTopicName") String agentTopicName @RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) { ) {
return R.ok( return R.ok(
coreExecutionService coreExecutionService
.SendCommandToAgent( .SendCommandToAgent(
agentTopicName, topicNameList,
"AgentUpdate", "AgentUpdate",
null null
)); ));
} }
@PostMapping("/AgentReboot") @PostMapping("/function/reboot")
@ApiOperation("控制Agent重启的接口") @ApiOperation("重启")
public R<String> AgentReboot( public R<List<String>> AgentReboot(
@RequestParam(value = "agentTopicName") String agentTopicName @RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) { ) {
return R.ok( return R.ok(
coreExecutionService coreExecutionService
.SendCommandToAgent( .SendCommandToAgent(
agentTopicName, topicNameList,
"AgentRestart", "AgentReboot",
null null
)); ));
} }
@PostMapping("/AgentShutdown") @PostMapping("/function/shutdown")
@ApiOperation("控制Agent关闭的接口") @ApiOperation("关闭")
public R<String> AgentShutdown( public R<List<String>> AgentShutdown(
@RequestParam(value = "agentTopicName") String agentTopicName @RequestParam(value = "topicNameList")
@ApiParam(name = "topicNameList", value = "目标机器列表") List<String> topicNameList
) { ) {
return R.ok( return R.ok(
coreExecutionService coreExecutionService
.SendCommandToAgent( .SendCommandToAgent(
agentTopicName, topicNameList,
"AgentShutdown", "AgentShutdown",
null null
)); ));

View File

@@ -1,17 +1,20 @@
package io.wdd.rpc.controller; package io.wdd.rpc.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.wdd.common.beans.response.R; import io.wdd.common.beans.response.R;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map; import java.util.Map;
import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_STATUS_MAP; import static io.wdd.rpc.init.ServerBootUpEnvironment.*;
import static io.wdd.rpc.init.ServerBootUpEnvironment.STATUS_AGENT_LIST_MAP;
@RestController @RestController
@@ -20,18 +23,49 @@ import static io.wdd.rpc.init.ServerBootUpEnvironment.STATUS_AGENT_LIST_MAP;
public class StatusController { public class StatusController {
@ApiOperation("获取所有Agent的运行状态") @ApiOperation("[ Agent-状态 ] Map")
@GetMapping("/agentStatus") @GetMapping("/agent/status")
public R<Map> GetAllAgentHealthyStatus() { public R<Map> GetAllAgentHealthyStatus() {
return R.ok(ALL_AGENT_STATUS_MAP); return R.ok(ALL_AGENT_STATUS_MAP);
} }
@ApiOperation("获取 状态-Agent Map") @ApiOperation("[ 状态-Agent ] Map")
@GetMapping("/statusAgent") @GetMapping("/status/agent")
public R<Map> GetHealthyStatusAgentList() { public R<Map> GetHealthyStatusAgentList() {
return R.ok(STATUS_AGENT_LIST_MAP); 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); 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.execute.result.BuildStreamReader;
import io.wdd.rpc.message.sender.OMessageToAgentSender; import io.wdd.rpc.message.sender.OMessageToAgentSender;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -20,6 +21,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.wdd.rpc.execute.service.ExecutionResultDaemonHandler.WAIT_EXECUTION_RESULT_LIST; 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; import static io.wdd.rpc.init.ServerBootUpEnvironment.ALL_AGENT_TOPIC_NAME_SET;
@Service @Service
@@ -35,8 +37,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Resource @Resource
RedisTemplate redisTemplate; RedisTemplate redisTemplate;
@Resource private static final String MANUAL_COMMAND_TYPE = "manual-command";
BuildStreamReader buildStreamReader;
@Override @Override
public String SendCommandToAgent(String agentTopicName, String command) { public String SendCommandToAgent(String agentTopicName, String command) {
@@ -50,7 +51,7 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
public String SendCommandToAgent(String agentTopicName, List<String> commandList) { public String SendCommandToAgent(String agentTopicName, List<String> commandList) {
return this.SendCommandToAgent( return this.SendCommandToAgent(
agentTopicName, agentTopicName,
"manual-command", MANUAL_COMMAND_TYPE,
commandList commandList
); );
} }
@@ -58,6 +59,11 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
@Override @Override
public String SendCommandToAgent(String agentTopicName, String type, List<String> commandList) { public String SendCommandToAgent(String agentTopicName, String type, List<String> commandList) {
// 归一化type
if (StringUtils.isEmpty(type)) {
type = MANUAL_COMMAND_TYPE;
}
return SendCommandToAgent( return SendCommandToAgent(
agentTopicName, agentTopicName,
type, type,
@@ -234,6 +240,12 @@ public class CoreExecutionServiceImpl implements CoreExecutionService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override
public List<String> SendCommandToAgentAll(String type, List<String> commandList) {
return null;
}
@Deprecated @Deprecated
private OctopusMessage generateOctopusMessage(String agentTopicName, String resultKey, String type, List<String> commandList, List<List<String>> commandListComplete) { 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 * octopus message unique key name
*/ */
private String topicName; private String topicName;
/** /**
* *
*/ */
private String comment; private String comment;
private String agentVersion;
} }