|
|
@@ -5,7 +5,6 @@ import cn.reghao.autodop.common.amqp.MessageType;
|
|
|
import cn.reghao.autodop.common.amqp.RpcResult;
|
|
|
import cn.reghao.autodop.common.dagent.app.api.AppOps;
|
|
|
import cn.reghao.autodop.common.dagent.app.api.data.deploy.AppDeployArgs;
|
|
|
-import cn.reghao.autodop.common.amqp.RabbitProducer;
|
|
|
import cn.reghao.autodop.common.dockerc.pojo.Config;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.deploy.DeployConfig;
|
|
|
@@ -13,16 +12,13 @@ import cn.reghao.autodop.dmaster.app.entity.log.AppStatus;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.DeployResult;
|
|
|
-import cn.reghao.autodop.common.utils.ExceptionUtil;
|
|
|
+import cn.reghao.autodop.dmaster.app.service.RemoteCallService;
|
|
|
import cn.reghao.autodop.dmaster.app.service.log.BuildDeployLogConsumer;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.Callable;
|
|
|
-import java.util.concurrent.ExecutionException;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Future;
|
|
|
|
|
|
/**
|
|
|
* 应用部署
|
|
|
@@ -31,15 +27,13 @@ import java.util.concurrent.Future;
|
|
|
* @date 2020-03-13 10:26:22
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Service
|
|
|
public class AppDeployer {
|
|
|
- private final String exchange = "amq.direct";
|
|
|
- private RabbitProducer rabbitProducer;
|
|
|
- private ExecutorService threadPool;
|
|
|
- BuildDeployLogConsumer logConsumer;
|
|
|
+ private RemoteCallService callService;
|
|
|
+ private BuildDeployLogConsumer logConsumer;
|
|
|
|
|
|
- public AppDeployer(RabbitProducer rabbitProducer, ExecutorService threadPool, BuildDeployLogConsumer logConsumer) {
|
|
|
- this.rabbitProducer = rabbitProducer;
|
|
|
- this.threadPool = threadPool;
|
|
|
+ public AppDeployer(RemoteCallService callService, BuildDeployLogConsumer logConsumer) {
|
|
|
+ this.callService = callService;
|
|
|
this.logConsumer = logConsumer;
|
|
|
}
|
|
|
|
|
|
@@ -51,7 +45,7 @@ public class AppDeployer {
|
|
|
* @date 2020-03-13 下午1:00
|
|
|
*/
|
|
|
public DeployResult deploy(BuildLog buildLog, List<DeployConfig> deployConfigs) {
|
|
|
- Map<String, Future<RpcResult>> futureMap = new HashMap<>(deployConfigs.size());
|
|
|
+ List<MqMessage> mqMessages = new ArrayList<>();
|
|
|
for (DeployConfig deployConfig : deployConfigs) {
|
|
|
AppDeployArgs appDeployArgs = new AppDeployArgs();
|
|
|
appDeployArgs.setAppId(buildLog.getAppId());
|
|
|
@@ -63,10 +57,17 @@ public class AppDeployer {
|
|
|
String config = deployConfig.getDockerConfig();
|
|
|
appDeployArgs.setDockerConfig((Config) JsonConverter.jsonToObject(config, Config.class));
|
|
|
|
|
|
- String machineId = deployConfig.getMachineId();
|
|
|
- futureMap.put(machineId, threadPool.submit(new DeployTask(machineId, appDeployArgs)));
|
|
|
+ MqMessage mqMessage = new MqMessage();
|
|
|
+ mqMessage.setMachineId(deployConfig.getMachineId());
|
|
|
+ mqMessage.setSendTime(System.currentTimeMillis());
|
|
|
+ mqMessage.setRpc(true);
|
|
|
+ mqMessage.setType(MessageType.appType.name());
|
|
|
+ mqMessage.setOps(AppOps.appDeployOps.name());
|
|
|
+ mqMessage.setPayload(JsonConverter.objectToJson(appDeployArgs));
|
|
|
+ mqMessages.add(mqMessage);
|
|
|
}
|
|
|
|
|
|
+ Map<String, RpcResult> rpcResultMap = callService.call(mqMessages);
|
|
|
DeployResult deployResult = new DeployResult();
|
|
|
deployResult.setAppId(buildLog.getAppId());
|
|
|
deployResult.setEnv(buildLog.getEnv());
|
|
|
@@ -74,69 +75,24 @@ public class AppDeployer {
|
|
|
LocalDateTime deployTime = LocalDateTime.now();
|
|
|
deployResult.setDeployTime(deployTime);
|
|
|
List<DeployLog> deployLogs = new ArrayList<>();
|
|
|
- for (Map.Entry<String, Future<RpcResult>> entry : futureMap.entrySet()) {
|
|
|
- Future<RpcResult> future = entry.getValue();
|
|
|
- while (!future.isDone() && !future.isCancelled()) {
|
|
|
- // 休眠等待异步任务结束
|
|
|
- try {
|
|
|
- Thread.sleep(1_000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ rpcResultMap.forEach((machineId, rpcResult) -> {
|
|
|
DeployLog deployLog = new DeployLog();
|
|
|
deployLog.setAppId(buildLog.getAppId());
|
|
|
deployLog.setEnv(buildLog.getEnv());
|
|
|
deployLog.setCommitId(buildLog.getCommitId());
|
|
|
- deployLog.setMachineId(entry.getKey());
|
|
|
- try {
|
|
|
- RpcResult rpcResult = future.get();
|
|
|
- if (rpcResult != null) {
|
|
|
- deployLog.setDeployTime(deployTime);
|
|
|
- deployLog.setStatusCode(rpcResult.getStatusCode());
|
|
|
- if (rpcResult.getStatusCode() == 0) {
|
|
|
- AppStatus appStatus =
|
|
|
- (AppStatus) JsonConverter.jsonToObject(rpcResult.getResult(), AppStatus.class);
|
|
|
- logConsumer.addAppStatus(appStatus);
|
|
|
- } else {
|
|
|
- deployLog.setErrDetail(rpcResult.getResult());
|
|
|
- }
|
|
|
- } else {
|
|
|
- deployLog.setStatusCode(1);
|
|
|
- deployLog.setErrDetail("RPC 调用失败");
|
|
|
- }
|
|
|
- } catch (InterruptedException | ExecutionException e) {
|
|
|
- deployLog.setStatusCode(1);
|
|
|
- deployLog.setErrDetail(ExceptionUtil.errorMsg(e));
|
|
|
+ deployLog.setMachineId(machineId);
|
|
|
+ deployLog.setDeployTime(deployTime);
|
|
|
+ deployLog.setStatusCode(rpcResult.getStatusCode());
|
|
|
+ if (rpcResult.getStatusCode() == 0) {
|
|
|
+ AppStatus appStatus =
|
|
|
+ (AppStatus) JsonConverter.jsonToObject(rpcResult.getResult(), AppStatus.class);
|
|
|
+ logConsumer.addAppStatus(appStatus);
|
|
|
+ } else {
|
|
|
+ deployLog.setErrDetail(rpcResult.getResult());
|
|
|
}
|
|
|
- deployLogs.add(deployLog);
|
|
|
- }
|
|
|
+ });
|
|
|
+
|
|
|
deployResult.setDeployLogs(deployLogs);
|
|
|
return deployResult;
|
|
|
}
|
|
|
-
|
|
|
- class DeployTask implements Callable<RpcResult> {
|
|
|
- private String machineId;
|
|
|
- private AppDeployArgs appDeployArgs;
|
|
|
-
|
|
|
- public DeployTask(String machineId, AppDeployArgs appDeployArgs) {
|
|
|
- this.machineId = machineId;
|
|
|
- this.appDeployArgs = appDeployArgs;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RpcResult call() {
|
|
|
- MqMessage mqMessage = new MqMessage();
|
|
|
- mqMessage.setMachineId(machineId);
|
|
|
- mqMessage.setSendTime(System.currentTimeMillis());
|
|
|
- mqMessage.setRpc(true);
|
|
|
- mqMessage.setType(MessageType.appType.name());
|
|
|
- mqMessage.setOps(AppOps.appDeployOps.name());
|
|
|
- mqMessage.setPayload(JsonConverter.objectToJson(appDeployArgs));
|
|
|
-
|
|
|
- log.info("RPC 调用部署应用...");
|
|
|
- return rabbitProducer.callRemote(exchange, machineId, mqMessage);
|
|
|
- }
|
|
|
- }
|
|
|
}
|