|
|
@@ -1,61 +1,74 @@
|
|
|
package cn.reghao.autodop.dmaster.app.service.deploy;
|
|
|
|
|
|
-import cn.reghao.autodop.common.config.BuildDeployResult;
|
|
|
-import cn.reghao.autodop.common.grpc.client.GrpcClientProxy;
|
|
|
-import cn.reghao.autodop.common.grpc.facade.DeployService;
|
|
|
-import cn.reghao.autodop.common.config.DeployConfig;
|
|
|
-import cn.reghao.autodop.common.http.HttpApi;
|
|
|
+import cn.reghao.autodop.common.deploy.DeployResult;
|
|
|
+import cn.reghao.autodop.common.deploy.DeployConfig;
|
|
|
+import cn.reghao.autodop.common.notification.DingNotify;
|
|
|
+import cn.reghao.autodop.common.notification.Notify;
|
|
|
+import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.build.pack.AppPack;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.constant.NotifyType;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.deploy.AppDeploy;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.orchestration.Notification;
|
|
|
+import cn.reghao.autodop.dmaster.app.thread.ThreadPoolWrapper;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
|
|
/**
|
|
|
- * 部署应用到远程主机
|
|
|
- *
|
|
|
* @author reghao
|
|
|
- * @date 2019-11-11 17:33:56
|
|
|
+ * @date 2020-03-13 10:26:22
|
|
|
*/
|
|
|
-@Slf4j
|
|
|
public class Deployer {
|
|
|
- private static final int GRPC_PORT = 4001;
|
|
|
+ private static ExecutorService threadPool = ThreadPoolWrapper.threadPool("deploy");
|
|
|
|
|
|
/**
|
|
|
- * @return 返回 List 是因为可能是集群部署
|
|
|
- * @date 2020-03-12 下午3:18
|
|
|
+ * 部署应用
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-03-13 下午1:00
|
|
|
*/
|
|
|
- public static List<BuildDeployResult> deploy(AppOrchestration app, String appPath) {
|
|
|
- DeployConfig deployConfig = new DeployConfig();
|
|
|
- deployConfig.setAppId(app.getAppId());
|
|
|
- deployConfig.setAppPath(appPath);
|
|
|
- /*deployConfig.setPackerType(app.getBuild().getAppPacker().getPackerType());
|
|
|
- deployConfig.setRunningDir(app.getRunning().getRunningDir());*/
|
|
|
-
|
|
|
- List<BuildDeployResult> results = new ArrayList<>();
|
|
|
- /* String[] hosts = app.getDeploy().getHosts().split(",");
|
|
|
- // TODO 异步调用
|
|
|
- for (String host : hosts) {
|
|
|
- // TODO 公共网络下,gRPC 调用需要经过认证
|
|
|
- DeployService deployService = (DeployService)new GrpcClientProxy<DeployService>()
|
|
|
- .getProxy(host, GRPC_PORT, DeployService.class);
|
|
|
+ public static List<DeployResult> deploy(AppOrchestration app, BuildLog buildLog) throws Exception {
|
|
|
+ Notification notification = app.getNotification();
|
|
|
+ String notifyType = notification.getNotifyType();
|
|
|
+ String dest = notification.getDestination();
|
|
|
+ Notify notify = null;
|
|
|
+ if (notifyType.equals(NotifyType.ding.name())) {
|
|
|
+ notify = new DingNotify();
|
|
|
+ }
|
|
|
|
|
|
- Object object = deployService.deploy(deployConfig);
|
|
|
- JSONObject jsonObject = (JSONObject) object;
|
|
|
- BuildDeployResult result = jsonObject.toJavaObject(BuildDeployResult.class);
|
|
|
+ AppPack appPack = app.getAppBuild().getAppPack();
|
|
|
+ AppDeploy appDeploy = app.getAppDeploy();
|
|
|
+ String[] hosts = appDeploy.getHosts().split(",");
|
|
|
+ List<Future<DeployResult>> futures = new ArrayList<>();
|
|
|
+ for (String host : hosts) {
|
|
|
+ DeployConfig deployConfig = new DeployConfig();
|
|
|
+ deployConfig.setAppId(app.getAppId());
|
|
|
+ deployConfig.setAppPath(buildLog.getAppPath());
|
|
|
+ deployConfig.setPackerType(appPack.getPackerType());
|
|
|
+ deployConfig.setHost(host);
|
|
|
+ deployConfig.setRunningDir(appDeploy.getRunningDir());
|
|
|
+ futures.add(threadPool.submit(new AppDeployTask(deployConfig)));
|
|
|
+ }
|
|
|
|
|
|
- // TODO 检查应用是否成功运行,若启动失败,则回退到上一个版本。若上一个版本仍失败,则返回错误并发出紧急通知
|
|
|
- if ("运行".equals(result.getStage()) && result.getCode() == 1) {
|
|
|
- String healthCheck = app.getRunning().getHealthCheck();
|
|
|
- int port = app.getRunning().getPort();
|
|
|
- // 健康检查 URL
|
|
|
- String url = HttpApi.url(host, port, healthCheck);
|
|
|
+ Iterator<Future<DeployResult>> iterator = futures.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Future<DeployResult> future = iterator.next();
|
|
|
+ iterator.remove();
|
|
|
+ while (!future.isDone() && !future.isCancelled()) {
|
|
|
+ Thread.sleep(1_000);
|
|
|
}
|
|
|
|
|
|
- results.add(result);
|
|
|
- }*/
|
|
|
+ // TODO 检查应用是否成功运行,若启动失败,则回退到上一个版本。若上一个版本仍失败,则返回错误并发出紧急通知
|
|
|
+ DeployResult deployResult = future.get();
|
|
|
+ notify.send(dest, JsonUtil.objectToJson(deployResult));
|
|
|
+ }
|
|
|
|
|
|
- return results;
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|