|
|
@@ -1,30 +1,36 @@
|
|
|
package cn.reghao.autodop.dmaster.app.service.deploy;
|
|
|
|
|
|
+import cn.reghao.autodop.common.amqp.MQMessage;
|
|
|
+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.dmaster.app.entity.build.AppPack;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.deploy.AppDeploy;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
+import cn.reghao.autodop.common.dagent.machine.api.data.MachineRegistry;
|
|
|
+import cn.reghao.autodop.common.utils.data.serializer.JsonConverter;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.deploy.DeployConfig;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
|
|
|
import cn.reghao.autodop.dmaster.common.exception.ExceptionUtil;
|
|
|
import cn.reghao.autodop.dmaster.common.thread.ThreadPoolWrapper;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-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;
|
|
|
|
|
|
/**
|
|
|
+ * 应用部署
|
|
|
+ *
|
|
|
* @author reghao
|
|
|
* @date 2020-03-13 10:26:22
|
|
|
*/
|
|
|
@Component
|
|
|
public class AppDeployer {
|
|
|
+ private final String routeKey = "dagent";
|
|
|
private RabbitProducer sender;
|
|
|
- private ExecutorService threadPool = ThreadPoolWrapper.threadPool("deploy");
|
|
|
+ private ExecutorService threadPool = ThreadPoolWrapper.threadPool("app-deploy");
|
|
|
|
|
|
public AppDeployer(RabbitProducer sender) {
|
|
|
this.sender = sender;
|
|
|
@@ -37,29 +43,24 @@ public class AppDeployer {
|
|
|
* @return
|
|
|
* @date 2020-03-13 下午1:00
|
|
|
*/
|
|
|
- public List<DeployLog> deploy(BuildLog buildLog, AppDeploy appDeploy, Set<String> hosts) {
|
|
|
- //AppPack appPack = buildLog.getAppPack();
|
|
|
-
|
|
|
+ public List<DeployLog> deploy(String appId, String packagePath, DeployConfig deployConfig) {
|
|
|
AppDeployArgs appDeployArgs = new AppDeployArgs();
|
|
|
- /*appDeployArgs.setPackerType(appPack.getPackerType());
|
|
|
- appDeployArgs.setAppId(buildLog.getAppId());
|
|
|
- appDeployArgs.setAppPath(buildLog.getAppPath());
|
|
|
- appDeployArgs.setRunningDir(appDeploy.getRunningDir());*/
|
|
|
-
|
|
|
- if (hosts == null) {
|
|
|
- //hosts = appDeploy.getHosts();
|
|
|
- }
|
|
|
+ appDeployArgs.setAppId(appId);
|
|
|
+ appDeployArgs.setPackagePath(packagePath);
|
|
|
+ appDeployArgs.setPackerType(deployConfig.getPackerType());
|
|
|
+ appDeployArgs.setRunningHome(deployConfig.getRunningHome());
|
|
|
+ appDeployArgs.setRunningScript(deployConfig.getRunningScript());
|
|
|
|
|
|
- // TODO 处理 hosts 为空的情况,这会导致 deployLogs 为空
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
Map<String, Future<RPCResult>> futureMap = new HashMap<>();
|
|
|
- for (String host : hosts) {
|
|
|
- futureMap.put(host, threadPool.submit(new DeployTask(sender, appDeployArgs)));
|
|
|
+ for (MachineRegistry machine : deployConfig.getMachineRegistry()) {
|
|
|
+ futureMap.put(machine.getMachineId(),
|
|
|
+ threadPool.submit(new DeployTask(machine.getMachineId(), appDeployArgs)));
|
|
|
}
|
|
|
|
|
|
List<DeployLog> deployLogs = new ArrayList<>();
|
|
|
for (Map.Entry<String, Future<RPCResult>> entry : futureMap.entrySet()) {
|
|
|
- String host = entry.getKey();
|
|
|
+ String machineId = entry.getKey();
|
|
|
Future<RPCResult> future = entry.getValue();
|
|
|
while (!future.isDone() && !future.isCancelled()) {
|
|
|
// 休眠等待异步任务结束
|
|
|
@@ -71,10 +72,6 @@ public class AppDeployer {
|
|
|
}
|
|
|
|
|
|
DeployLog deployLog = new DeployLog();
|
|
|
- deployLog.setBuildLog(buildLog);
|
|
|
- deployLog.setHost(host);
|
|
|
- deployLog.setDeployTime(LocalDateTime.now());
|
|
|
- deployLog.setDeployTotalTime(System.currentTimeMillis()-startTime);
|
|
|
try {
|
|
|
RPCResult rpcResult = future.get();
|
|
|
if (rpcResult != null) {
|
|
|
@@ -82,7 +79,7 @@ public class AppDeployer {
|
|
|
deployLog.setMsg(rpcResult.getResult());
|
|
|
} else {
|
|
|
deployLog.setStatusCode(1);
|
|
|
- deployLog.setMsg("RPC 调用失败");
|
|
|
+ deployLog.setMsg(machineId + " RPC 调用失败");
|
|
|
}
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
deployLog.setStatusCode(1);
|
|
|
@@ -90,6 +87,30 @@ public class AppDeployer {
|
|
|
}
|
|
|
deployLogs.add(deployLog);
|
|
|
}
|
|
|
+
|
|
|
+ long costTime = System.currentTimeMillis() - startTime;
|
|
|
return deployLogs;
|
|
|
}
|
|
|
+
|
|
|
+ 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));
|
|
|
+ return sender.callRemote(routeKey, mqMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|