[ server] [ agent ] - 完成核心信息,关机的功能

This commit is contained in:
zeaslity
2023-02-09 10:51:01 +08:00
parent 8231dd21e4
commit 47d13ca4d7
8 changed files with 247 additions and 6 deletions

View File

@@ -21,6 +21,13 @@ public class AgentOperationInfoService {
public void AgentOpInfo(OctopusMessage octopusMessage){
// 构造结果OM
octopusMessage.setAc_time(TimeUtils.currentTime());
octopusMessage.setResult(agentServerInfo);
// 发送相应的OM至 OctopusToServer 中
oMessageToServerSender.send(octopusMessage);
}
public void AgentOpVersion(OctopusMessage octopusMessage){

View File

@@ -1,12 +1,26 @@
package io.wdd.agent.agent;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.agent.executor.CommandExecutor;
import io.wdd.agent.executor.FunctionExecutor;
import io.wdd.agent.message.OMessageToServerSender;
import io.wdd.common.beans.agent.AgentOperationMessage;
import io.wdd.common.beans.rabbitmq.OctopusMessage;
import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import static io.wdd.common.beans.status.OctopusStatusMessage.ALL_AGENT_STATUS_REDIS_KEY;
@Service
@Slf4j
@@ -15,6 +29,15 @@ public class AgentRebootUpdateService {
@Resource
FunctionExecutor functionExecutor;
@Resource
CommandExecutor commandExecutor;
@Resource
OMessageToServerSender toServerSender;
@Resource
RedisTemplate redisTemplate;
public void exAgentReboot(AgentOperationMessage operationMessage) {
}
@@ -22,4 +45,91 @@ public class AgentRebootUpdateService {
public void exAgentUpdate(AgentOperationMessage operationMessage) {
}
public void exAgentShutdown(OctopusMessage octopusMessage, AgentOperationMessage operationMessage) {
LocalDateTime now = TimeUtils.currentTime();
LocalDateTime operationTime = operationMessage.getOperationTime();
Duration duration = Duration.between(
now,
operationTime
);
long seconds = duration.toSeconds();
// 发送消息至RabbitMQ中
octopusMessage.setAc_time(now);
octopusMessage.setResult(
String.format(
"OctopusAgent [ %s ] 将会在 [ %s ] 秒之后关机",
octopusMessage.getUuid(),
seconds
)
);
toServerSender.send(
octopusMessage
);
ExecutorService pool = AgentCommonThreadPool.pool;
// 休眠,阻塞当前线程
if (seconds > 0) {
pool.submit(
() -> {
// keep this to local
OctopusMessage message = octopusMessage;
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 执行实际的关机操作
doShutdownAgent(message);
}
);
} else {
// 不需要等待,直接关机
doShutdownAgent(octopusMessage);
}
}
private void doShutdownAgent(OctopusMessage octopusMessage) {
ArrayList<String> shutdownCommand = new ArrayList<>(
List.of(
"systemctl",
"poweroff"
)
);
//发送消息至Redis中, 修改Agent的状态
redisTemplate
.opsForHash()
.put(
ALL_AGENT_STATUS_REDIS_KEY,
octopusMessage.getUuid(),
"0"
);
// 执行关机操作
log.error(
"开始关闭OctopusAgent! 时间为 [ {} ]",
TimeUtils.currentTimeString()
);
String streamKey = octopusMessage.getUuid() + "-Status";
// 最终执行关机操作
commandExecutor.execute(
streamKey,
shutdownCommand
);
}
}

View File

@@ -66,6 +66,10 @@ public class OMHandlerAgent extends AbstractOctopusMessageHandler {
// 收集Agent的核心Info信息
agentOperationInfoService.AgentOpInfo(octopusMessage);
} else if (opType.equals(AgentOperationType.SHUTDOWN)) {
// 关闭Agent的接口
agentRebootUpdateService.exAgentShutdown(octopusMessage, operationMessage);
} else {
// operation unknown
log.error("Command Agent Operation Unknown! ");

View File

@@ -29,9 +29,6 @@ public class OMHandlerStatus extends AbstractOctopusMessageHandler {
@Resource
AgentStatusCollector agentStatusCollector;
@Override
public boolean handle(OctopusMessage octopusMessage) {

View File

@@ -75,7 +75,7 @@ public class CommandExecutor {
}
public int processExecute(String streamKey, ProcessBuilder processBuilder) {
private int processExecute(String streamKey, ProcessBuilder processBuilder) {
processBuilder.redirectErrorStream(true);
// processBuilder.inheritIO();