diff --git a/agent/src/main/java/io/wdd/agent/agent/AgentOperationInfoService.java b/agent/src/main/java/io/wdd/agent/agent/AgentOperationInfoService.java index c20e6ec..554065c 100644 --- a/agent/src/main/java/io/wdd/agent/agent/AgentOperationInfoService.java +++ b/agent/src/main/java/io/wdd/agent/agent/AgentOperationInfoService.java @@ -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); diff --git a/server/src/main/java/io/wdd/func/xray/service/XrayConfigDistribute.java b/server/src/main/java/io/wdd/func/xray/service/XrayConfigDistribute.java index 73a419a..615eee0 100644 --- a/server/src/main/java/io/wdd/func/xray/service/XrayConfigDistribute.java +++ b/server/src/main/java/io/wdd/func/xray/service/XrayConfigDistribute.java @@ -40,6 +40,25 @@ public class XrayConfigDistribute { private static final String updateCommandType = "OctopusXray"; static { + + ArrayList zero = new ArrayList<>( + List.of( + "if", + "[", + "!", + "-f", + "/usr/local/etc/xray/config.json", + "]", + ";", + "then", + "touch", + "/usr/local/etc/xray/config.json", + ";", + "fi" + ) + ); + + ArrayList 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 diff --git a/server/src/main/java/io/wdd/rpc/agent/OctopusAgentServiceImpl.java b/server/src/main/java/io/wdd/rpc/agent/OctopusAgentServiceImpl.java index 3173ffa..280e247 100644 --- a/server/src/main/java/io/wdd/rpc/agent/OctopusAgentServiceImpl.java +++ b/server/src/main/java/io/wdd/rpc/agent/OctopusAgentServiceImpl.java @@ -181,6 +181,8 @@ public class OctopusAgentServiceImpl implements OctopusAgentService { // 是当前需要的消息信息 try { + + // 解析当前信息 ServerInfoVO serverInfoVO = objectMapper.readValue( (String) message.getResult(), new TypeReference() { diff --git a/server/src/main/java/io/wdd/rpc/controller/AgentController.java b/server/src/main/java/io/wdd/rpc/controller/AgentController.java index 7e6ece0..4e946b4 100644 --- a/server/src/main/java/io/wdd/rpc/controller/AgentController.java +++ b/server/src/main/java/io/wdd/rpc/controller/AgentController.java @@ -21,14 +21,14 @@ public class AgentController { OctopusAgentService octopusAgentService; @GetMapping("/version") - @ApiOperation("获取所有OctopusAgent的版本") + @ApiOperation("[版本]-所有OctopusAgent") public R> getAllAgentVersion(){ return R.ok(octopusAgentService.getAllAgentVersion()); } @GetMapping("/coreInfo") - @ApiOperation("获取所有OctopusAgent的核心信息") + @ApiOperation("[核心信息]-所有OctopusAgent") public R> getAllAgentCoreInfo(){ return R.ok(octopusAgentService.getAllAgentCoreInfo()); 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 0bbc8f5..505c056 100644 --- a/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java +++ b/server/src/main/java/io/wdd/rpc/controller/ExecutionController.java @@ -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 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( - topicName, - type, - commandList - ); - } + String streamKey = coreExecutionService + .SendCommandToAgent( + topicName, + type, + commandList + ); return R.ok(streamKey); } + @PostMapping("/command/batch") + @ApiOperation("[命令]- 批量发送命令") + public R> patchCommandToAgentList( + @RequestParam(value = "topicNameList") + @ApiParam(name = "topicNameList", value = "目标机器列表") List topicNameList, + @RequestParam(value = "commandList", required = false) + @ApiParam(name = "commandList", value = "命令行") @Nullable List commandList, + @RequestParam(value = "type", required = false) @Nullable String type + ) { + + return R.ok(coreExecutionService.SendCommandToAgent( + topicNameList, + type, + commandList + )); + } + + + @PostMapping("/command/all") + @ApiOperation("[命令]- 发送命令至所有的主机") + public R> patchCommandToAgentAll( + @RequestParam(value = "commandList", required = false) + @ApiParam(name = "commandList", value = "命令行") @Nullable List 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 AgentUpdate( - @RequestParam(value = "agentTopicName") String agentTopicName + @PostMapping("/function/update") + @ApiOperation("升级") + public R> AgentUpdate( + @RequestParam(value = "topicNameList") + @ApiParam(name = "topicNameList", value = "目标机器列表") List topicNameList ) { return R.ok( coreExecutionService .SendCommandToAgent( - agentTopicName, + topicNameList, "AgentUpdate", null )); } - @PostMapping("/AgentReboot") - @ApiOperation("控制Agent重启的接口") - public R AgentReboot( - @RequestParam(value = "agentTopicName") String agentTopicName + @PostMapping("/function/reboot") + @ApiOperation("重启") + public R> AgentReboot( + @RequestParam(value = "topicNameList") + @ApiParam(name = "topicNameList", value = "目标机器列表") List topicNameList ) { return R.ok( coreExecutionService .SendCommandToAgent( - agentTopicName, - "AgentRestart", + topicNameList, + "AgentReboot", null )); } - @PostMapping("/AgentShutdown") - @ApiOperation("控制Agent关闭的接口") - public R AgentShutdown( - @RequestParam(value = "agentTopicName") String agentTopicName + @PostMapping("/function/shutdown") + @ApiOperation("关闭") + public R> AgentShutdown( + @RequestParam(value = "topicNameList") + @ApiParam(name = "topicNameList", value = "目标机器列表") List topicNameList ) { return R.ok( coreExecutionService .SendCommandToAgent( - agentTopicName, + topicNameList, "AgentShutdown", null )); diff --git a/server/src/main/java/io/wdd/rpc/controller/StatusController.java b/server/src/main/java/io/wdd/rpc/controller/StatusController.java index a84bd9e..dfea0d4 100644 --- a/server/src/main/java/io/wdd/rpc/controller/StatusController.java +++ b/server/src/main/java/io/wdd/rpc/controller/StatusController.java @@ -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 GetAllAgentHealthyStatus() { return R.ok(ALL_AGENT_STATUS_MAP); } - @ApiOperation("获取 状态-Agent Map") - @GetMapping("/statusAgent") + @ApiOperation("[ 状态-Agent ] Map") + @GetMapping("/status/agent") public R GetHealthyStatusAgentList() { return R.ok(STATUS_AGENT_LIST_MAP); } + @ApiOperation("[ Agent ] - 所有的Agent主机") + @PostMapping("/agent/all") + public R> GetAllAgentTopicNameList() { + + return R.ok(ALL_AGENT_TOPIC_NAME_LIST); + } + + + @ApiOperation("[ Agent ] - 所有主机String") + @PostMapping("/agent/all/string") + public R 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> GetAllHealthyAgent() { + + return R.ok(ALL_HEALTHY_AGENT_TOPIC_NAME_LIST); + } + + } diff --git a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionService.java b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionService.java index 29d753d..58516c7 100644 --- a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionService.java +++ b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionService.java @@ -62,4 +62,13 @@ public interface CoreExecutionService { List SendCommandToAgentComplete(List agentTopicNameList, String type, List> completeCommandList, HashMap atnFutureKey); + /** + * 向所有的Agent发送命令 + * @param type + * @param commandList + * @return + + */ + @Deprecated + List SendCommandToAgentAll(String type, List commandList); } diff --git a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java index 46d7925..0d8b33b 100644 --- a/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java +++ b/server/src/main/java/io/wdd/rpc/execute/service/CoreExecutionServiceImpl.java @@ -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 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 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 SendCommandToAgentAll(String type, List commandList) { + + return null; + } + @Deprecated private OctopusMessage generateOctopusMessage(String agentTopicName, String resultKey, String type, List commandList, List> commandListComplete) { diff --git a/server/src/main/java/io/wdd/server/beans/vo/ServerInfoVO.java b/server/src/main/java/io/wdd/server/beans/vo/ServerInfoVO.java index 7d02920..b70fe8d 100644 --- a/server/src/main/java/io/wdd/server/beans/vo/ServerInfoVO.java +++ b/server/src/main/java/io/wdd/server/beans/vo/ServerInfoVO.java @@ -142,10 +142,11 @@ public class ServerInfoVO { * octopus message unique key name */ private String topicName; - /** * */ private String comment; + + private String agentVersion; }