[ server ] [ agent ]- 收集Agent的版本信息

This commit is contained in:
zeaslity
2023-02-07 16:26:22 +08:00
parent f4e636a368
commit 602e3c3034
20 changed files with 334 additions and 84 deletions

View File

@@ -0,0 +1,43 @@
package io.wdd.agent.agent;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
@Slf4j
public class AgentOperationInfoService {
@Resource
AgentServerInfo agentServerInfo;
@Resource
OMessageToServerSender oMessageToServerSender;
public void AgentOpInfo(OctopusMessage order){
}
public void AgentOpVersion(OctopusMessage octopusMessage){
// 收集版本信息
String agentVersion = agentServerInfo.getAgentVersion();
// 构造结果OM
octopusMessage.setAc_time(TimeUtils.currentTime());
octopusMessage.setResult(agentVersion);
// 发送相应的OM至 OctopusToServer 中
oMessageToServerSender.send(octopusMessage);
}
}

View File

@@ -110,6 +110,9 @@ public class AgentServerInfo {
@Value("${machineId}")
private String machineId;
@Value("${agentVersion}")
private String agentVersion;
/*
* get from octopus server at the end of initialization
*

View File

@@ -1,7 +1,12 @@
package io.wdd.agent.config.message.handler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.wdd.agent.agent.AgentOperationInfoService;
import io.wdd.agent.agent.AgentRebootUpdateService;
import io.wdd.common.beans.agent.AgentOperationMessage;
import io.wdd.common.beans.agent.AgentOperationType;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import lombok.extern.slf4j.Slf4j;
@@ -17,28 +22,48 @@ public class OMHandlerAgent extends AbstractOctopusMessageHandler {
@Resource
AgentRebootUpdateService agentRebootUpdateService;
@Resource
AgentOperationInfoService agentOperationInfoService;
@Resource
ObjectMapper objectMapper;
@Override
public boolean handle(OctopusMessage octopusMessage) {
if (!octopusMessage.getType().equals(OctopusMessageType.AGENT)) {
if (!octopusMessage
.getType()
.equals(OctopusMessageType.AGENT)) {
return next.handle(octopusMessage);
}
AgentOperationMessage operationMessage = (AgentOperationMessage) octopusMessage.getContent();
String operationName = operationMessage.getOperationName();
if (operationName.startsWith("reb")) {
// reboot
agentRebootUpdateService.exAgentReboot(operationMessage);
} else if (operationName.startsWith("upd")) {
// update
agentRebootUpdateService.exAgentUpdate(operationMessage);
} else {
// operation unknown
log.error("Command Agent Operation Unknown! " );
AgentOperationMessage operationMessage;
try {
operationMessage = objectMapper.readValue(
(String) octopusMessage.getContent(),
new TypeReference<AgentOperationMessage>() {
}
);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
AgentOperationType opType = operationMessage.getOpType();
if (opType.equals(AgentOperationType.REBOOT)) {
// reboot
agentRebootUpdateService.exAgentReboot(operationMessage);
} else if (opType.equals(AgentOperationType.UPDATE)) {
// update
agentRebootUpdateService.exAgentUpdate(operationMessage);
} else if (opType.equals(AgentOperationType.VERSION)) {
agentOperationInfoService.AgentOpInfo(octopusMessage);
} else if (opType.equals(AgentOperationType.INFO)) {
agentOperationInfoService.AgentOpVersion(octopusMessage);
} else {
// operation unknown
log.error("Command Agent Operation Unknown! ");
}
return true;
}

View File

@@ -2,7 +2,7 @@ package io.wdd.agent.config.message.handler;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.initialization.message.GenOctopusRabbitMQConnection;
import io.wdd.agent.message.ToServerMessage;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessageType;
import lombok.extern.slf4j.Slf4j;
@@ -26,7 +26,7 @@ public class OMHandlerInit extends AbstractOctopusMessageHandler {
GenOctopusRabbitMQConnection genOctopusRabbitMQConnection;
@Resource
ToServerMessage toServerMessage;
OMessageToServerSender oMessageToServerSender;
@Resource
AgentServerInfo agentServerInfo;
@@ -52,7 +52,7 @@ public class OMHandlerInit extends AbstractOctopusMessageHandler {
octopusMessage.setResult(success);
// log.info(success);
toServerMessage.send(octopusMessage);
oMessageToServerSender.send(octopusMessage);
return true;
}

View File

@@ -52,58 +52,6 @@ public class CommandExecutor {
}
/*public int executeScript(String streamKey, List<String> commandList) {
ProcessBuilder processBuilder = new ProcessBuilder(commandList);
processBuilder.redirectErrorStream(true);
// processBuilder.inheritIO();
processBuilder.directory(new File(System.getProperty("user.home")));
int processResult = 233;
try {
Process process = processBuilder.start();
// start a backend thread to daemon the process
// wait for processMaxWaitSeconds and kill the process if it's still alived
DaemonCommandProcess.submit(
StopStuckCommandProcess(
process,
processMaxWaitSeconds
));
// todo this will stuck the process and rabbitmq message will reentry the queue
// get the command result must also be a timeout smaller than the process
boolean waitFor = process.waitFor(
50,
TimeUnit.SECONDS
);
// get the process result
if (ObjectUtils.isNotEmpty(waitFor) && ObjectUtils.isNotEmpty(process)) {
processResult = process.exitValue();
}
log.debug(
"current shell command {} result is {}",
processBuilder.command(),
processResult
);
} catch (IOException | InterruptedException e) {
log.error(
"Shell command error ! {} + {}",
e.getCause(),
e.getMessage()
);
}
return processResult;
}*/
public int execute(String streamKey, List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder(command);

View File

@@ -3,7 +3,7 @@ package io.wdd.agent.initialization.bootup;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import io.wdd.agent.config.beans.init.AgentServerInfo;
import io.wdd.agent.message.ToServerMessage;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.agent.message.handler.OctopusMessageHandler;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.handler.MyRuntimeException;
@@ -27,7 +27,7 @@ import java.util.concurrent.TimeUnit;
public class OctopusAgentInitService {
@Resource
ToServerMessage toServerMessage;
OMessageToServerSender oMessageToServerSender;
@Autowired
OctopusMessageHandler octopusMessageHandler;
@@ -43,7 +43,7 @@ public class OctopusAgentInitService {
public void SendInfoToServer(AgentServerInfo agentServerInfo) {
toServerMessage.sendInitInfo(agentServerInfo, defaultInitRegisterTimeOut);
oMessageToServerSender.sendInitInfo(agentServerInfo, defaultInitRegisterTimeOut);
}

View File

@@ -20,7 +20,7 @@ import java.time.LocalDateTime;
@Service
@Slf4j(topic = "To Octopus Server Message")
public class ToServerMessage {
public class OMessageToServerSender {
@Resource
InitRabbitMQConnector initRabbitMqConnector;