|
|
@@ -3,31 +3,28 @@ package cn.reghao.autodop.dmaster.app.service.impl;
|
|
|
import cn.reghao.autodop.common.mqtt.AsyncMqttClient;
|
|
|
import cn.reghao.autodop.common.msg.rpc.constant.AppRpcClazz;
|
|
|
import cn.reghao.autodop.common.msg.rpc.dto.app.DeployParam;
|
|
|
+import cn.reghao.autodop.common.msg.rpc.dto.app.PackType;
|
|
|
import cn.reghao.autodop.common.util.thread.ThreadPoolWrapper;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.crud.AppBuildingCrud;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.crud.AppDeployingCrud;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.crud.log.BuildLogCrud;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.query.AppBuildingQuery;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.query.AppDeployingQuery;
|
|
|
import cn.reghao.autodop.dmaster.app.db.query.config.AppConfigQuery;
|
|
|
import cn.reghao.autodop.dmaster.app.db.query.log.BuildLogQuery;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.po.AppBuilding;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.po.AppDeploying;
|
|
|
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
|
|
|
import cn.reghao.autodop.dmaster.app.model.po.log.BuildLog;
|
|
|
import cn.reghao.autodop.dmaster.app.service.AppBuildSupplier;
|
|
|
import cn.reghao.autodop.dmaster.app.service.BuildDeployService;
|
|
|
import cn.reghao.autodop.dmaster.app.service.AppDeployer;
|
|
|
import cn.reghao.autodop.dmaster.app.service.AppBuilder;
|
|
|
-import cn.reghao.autodop.dmaster.notification.service.NotifyService;
|
|
|
-import cn.reghao.autodop.dmaster.spring.interceptor.AppIntegrateReinitInterceptor;
|
|
|
+import cn.reghao.autodop.dmaster.app.service.bd.AppBuildingService;
|
|
|
+import cn.reghao.autodop.dmaster.util.UploadDownload;
|
|
|
+import cn.reghao.autodop.dmaster.spring.HttpRequestUtil;
|
|
|
import cn.reghao.jdkutil.result.ResultStatus;
|
|
|
import cn.reghao.jdkutil.serializer.JsonConverter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
@@ -45,71 +42,55 @@ public class BuildDeployServiceImpl implements BuildDeployService {
|
|
|
private final Set<String> onBuilding = new ConcurrentSkipListSet<>();
|
|
|
private final ExecutorService threadPool;
|
|
|
private final AsyncMqttClient mqttClient;
|
|
|
+ private final AppBuildingService buildingService;
|
|
|
private final AppDeployer appDeployer;
|
|
|
+ private final UploadDownload uploadDownload;
|
|
|
private final AppConfigQuery appConfigQuery;
|
|
|
- private final AppBuildingQuery buildingQuery;
|
|
|
- private final AppBuildingCrud buildingCrud;
|
|
|
- private final AppDeployingQuery deployingQuery;
|
|
|
- private final AppDeployingCrud deployingCrud;
|
|
|
private final BuildLogQuery buildLogQuery;
|
|
|
- private final BuildLogCrud buildLogCrud;
|
|
|
|
|
|
- public BuildDeployServiceImpl(AsyncMqttClient mqttClient, AppDeployer appDeployer, AppConfigQuery appConfigQuery,
|
|
|
- AppBuildingQuery buildingQuery, AppBuildingCrud buildingCrud,
|
|
|
- AppDeployingQuery deployingQuery, AppDeployingCrud deployingCrud,
|
|
|
- BuildLogQuery buildLogQuery, BuildLogCrud buildLogCrud) {
|
|
|
+ public BuildDeployServiceImpl(AsyncMqttClient mqttClient, AppDeployer appDeployer,
|
|
|
+ AppBuildingService buildingService, UploadDownload uploadDownload,
|
|
|
+ AppConfigQuery appConfigQuery, BuildLogQuery buildLogQuery) {
|
|
|
this.threadPool = ThreadPoolWrapper.threadPool("build-deploy");
|
|
|
this.mqttClient = mqttClient;
|
|
|
+ this.buildingService = buildingService;
|
|
|
this.appDeployer = appDeployer;
|
|
|
+ this.uploadDownload = uploadDownload;
|
|
|
this.appConfigQuery = appConfigQuery;
|
|
|
- this.buildingQuery = buildingQuery;
|
|
|
- this.buildingCrud = buildingCrud;
|
|
|
- this.deployingQuery = deployingQuery;
|
|
|
- this.deployingCrud = deployingCrud;
|
|
|
this.buildLogQuery = buildLogQuery;
|
|
|
- this.buildLogCrud = buildLogCrud;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String buildAndDeploy(String appId) {
|
|
|
- build(appId);
|
|
|
-
|
|
|
+ public void buildAndDeploy(String appId) throws Exception {
|
|
|
if (!onBuilding.add(appId)) {
|
|
|
- return appId + " 正在构建中";
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
AppConfig app = appConfigQuery.findByAppId(appId);
|
|
|
if (app == null) {
|
|
|
onBuilding.remove(appId);
|
|
|
- //throw new Exception(appId + " 不存在");
|
|
|
+ throw new Exception(appId + " 不存在");
|
|
|
}
|
|
|
- AppBuilder appIntegrate = new AppBuilder(app);
|
|
|
|
|
|
+ AppBuilder appIntegrate = new AppBuilder(app);
|
|
|
AppBuildSupplier supplier = new AppBuildSupplier(appIntegrate);
|
|
|
- log.info("开始异步构建 {}", appId);
|
|
|
+ log.info("{} 开始异步构建", appId);
|
|
|
+ buildingService.refreshBeforeBuild(appId);
|
|
|
CompletableFuture.supplyAsync(supplier, threadPool)
|
|
|
.whenComplete((buildLog, throwable) -> {
|
|
|
+ if (buildLog != null) {
|
|
|
+ buildLog = buildingService.addBuildLog(buildLog);
|
|
|
+ buildingService.refreshAfterBuild(buildLog);
|
|
|
+ }
|
|
|
+
|
|
|
onBuilding.remove(appId);
|
|
|
if (throwable != null) {
|
|
|
- String msg = throwable.getMessage();
|
|
|
+ String errMsg = throwable.getMessage();
|
|
|
+ log.error("{} 构建错误: {}", appId, errMsg);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- buildLog = buildLogCrud.save(buildLog);
|
|
|
- AppBuilding appBuilding = buildingQuery.findByAppId(appId);
|
|
|
- if (appBuilding != null) {
|
|
|
- appBuilding.update(buildLog);
|
|
|
- buildingCrud.save(appBuilding);
|
|
|
- }
|
|
|
-
|
|
|
- List<AppDeploying> appDeployings = deployingQuery.findByAppId(appId);
|
|
|
- if (!appDeployings.isEmpty()) {
|
|
|
- for (AppDeploying appDeploying : appDeployings) {
|
|
|
- appDeploying.setBuildLogId(buildLog.getId());
|
|
|
- }
|
|
|
- deployingCrud.saveAll(appDeployings);
|
|
|
- }
|
|
|
log.info("{} 构建完成", appId);
|
|
|
+ buildingService.sendBuildNotify(buildLog);
|
|
|
})
|
|
|
.thenAccept(buildLog -> {
|
|
|
if (buildLog.getResult().getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
@@ -121,72 +102,53 @@ public class BuildDeployServiceImpl implements BuildDeployService {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- return appId + " 已开始构建";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String build(String appId) {
|
|
|
- if (!onBuilding.add(appId)) {
|
|
|
- return appId + " 正在构建中";
|
|
|
- }
|
|
|
-
|
|
|
- AppConfig app = appConfigQuery.findByAppId(appId);
|
|
|
- if (app == null) {
|
|
|
- onBuilding.remove(appId);
|
|
|
- //throw new Exception(appId + " 不存在");
|
|
|
- }
|
|
|
-
|
|
|
- AppBuilder appIntegrate = new AppBuilder(app);
|
|
|
- AppBuildSupplier supplier = new AppBuildSupplier(appIntegrate);
|
|
|
- log.info("开始异步构建 {}", appId);
|
|
|
- CompletableFuture.supplyAsync(supplier, threadPool)
|
|
|
- .whenComplete((buildLog, throwable) -> {
|
|
|
- onBuilding.remove(appId);
|
|
|
- if (throwable != null) {
|
|
|
- String msg = throwable.getMessage();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- buildLog = buildLogCrud.save(buildLog);
|
|
|
- AppBuilding appBuilding = buildingQuery.findByAppId(appId);
|
|
|
- if (appBuilding != null) {
|
|
|
- appBuilding.update(buildLog);
|
|
|
- buildingCrud.save(appBuilding);
|
|
|
- }
|
|
|
-
|
|
|
- List<AppDeploying> appDeployings = deployingQuery.findByAppId(appId);
|
|
|
- if (!appDeployings.isEmpty()) {
|
|
|
- for (AppDeploying appDeploying : appDeployings) {
|
|
|
- appDeploying.setBuildLogId(buildLog.getId());
|
|
|
- }
|
|
|
- deployingCrud.saveAll(appDeployings);
|
|
|
- }
|
|
|
- log.info("{} 构建完成", appId);
|
|
|
- })
|
|
|
- .thenAccept(buildLog -> {
|
|
|
- });
|
|
|
- return appId + " 已开始构建";
|
|
|
+ public void build(String appId) {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean deploy(String buildLogId) {
|
|
|
- return false;
|
|
|
+ public void deploy(String buildLogId) {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void downloadPackage(String buildLogId) {
|
|
|
+ public void downloadPackage(String buildLogId) throws Exception {
|
|
|
BuildLog buildLog = buildLogQuery.findById(buildLogId);
|
|
|
if (buildLog == null) {
|
|
|
- String msg = "构建不存在";
|
|
|
- return;
|
|
|
+ String errMsg = "构建不存在";
|
|
|
+ throw new Exception(errMsg);
|
|
|
}
|
|
|
|
|
|
- String packagePath = buildLog.getPackagePath();
|
|
|
+ String appId = buildLog.getAppId();
|
|
|
+ AppConfig appConfig = appConfigQuery.findByAppId(appId);
|
|
|
+ if (appConfig == null) {
|
|
|
+ String errMsg = "应用不存在";
|
|
|
+ throw new Exception(errMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String packType = appConfig.getPackerConfig().getType();
|
|
|
+ switch (PackType.valueOf(packType)) {
|
|
|
+ case docker:
|
|
|
+ String errMsg = "docker 打包类型的应用不能下载";
|
|
|
+ throw new Exception(errMsg);
|
|
|
+ case zip:
|
|
|
+ String packagePath = buildLog.getPackagePath();
|
|
|
+ String filename = packagePath.substring(packagePath.lastIndexOf(File.separator)+1);
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(packagePath);
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(fileInputStream);
|
|
|
+ uploadDownload.download(filename, bis, HttpRequestUtil.getResponse());
|
|
|
+ break;
|
|
|
+ case zipHttp:
|
|
|
+ throw new Exception(String.format("打包类型 %s 暂未实现", packType));
|
|
|
+ default:
|
|
|
+ throw new Exception(String.format("打包类型 %s 不存在", packType));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<String> currentlyBuilding() {
|
|
|
- return Collections.emptyList();
|
|
|
+ return List.copyOf(onBuilding);
|
|
|
}
|
|
|
|
|
|
@Override
|