Ver código fonte

完善构建包下载功能

reghao 4 anos atrás
pai
commit
0d61b7a7b8

+ 4 - 0
dagent/src/main/java/cn/reghao/autodop/dagent/mqttsub/DagentTopicListener.java

@@ -36,6 +36,10 @@ public class DagentTopicListener implements IMqttMessageListener {
             Message msg = JsonConverter.jsonToObject(message.toString(), Message.class);
             String msgId = msg.getMsgId();
             long sendTime = msg.getSendTime();
+            if (sendTime < startTime) {
+                log.info("忽略 autodop-dagent 启动前发布的消息");
+                return;
+            }
 
             RpcMsg rpcMsg = msg.getRpcMsg();
             String clazz = rpcMsg.getClazz();

+ 6 - 18
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/BuildDeployController.java

@@ -65,18 +65,12 @@ public class BuildDeployController {
         return WebBody.success("暂未实现");
     }
 
-    @Deprecated
-    @ApiOperation(value = "刷新应用构建, 部署和运行列表")
-    @PostMapping(value = "/refresh", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String refreshAppBuilding() {
-        return WebBody.success();
-    }
-
-    @Deprecated
-    @ApiOperation(value = "清除当前正在构建和部署的应用")
-    @PostMapping(value = "/clear", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String clearCurrentBuildingOrDeploy() {
-        return WebBody.success("暂未实现");
+    @ApiOperation(value = "构建包下载接口")
+    @GetMapping(value = "/dlpak/{buildLogId}", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String packageUrl(@PathVariable("buildLogId") String buildLogId) throws Exception {
+        String packageUrl = buildDeployService.packageUrl(buildLogId);
+        //return ResponseEntity.status(HttpStatus.FOUND).header("location", url).build();
+        return WebBody.successWithMsg(packageUrl);
     }
 
     @ApiOperation(value = "构建包下载接口")
@@ -84,10 +78,4 @@ public class BuildDeployController {
     public void dlPackage(@PathVariable("filename") String filename) throws Exception {
         buildDeployService.dlPackage(filename);
     }
-
-    @ApiOperation(value = "构建包下载接口")
-    @GetMapping("/dl1/{buildLogId}")
-    public void dlPackage1(@PathVariable("buildLogId") String buildLogId) throws Exception {
-        buildDeployService.downloadPackage(buildLogId);
-    }
 }

+ 0 - 9
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/AppDeployer.java

@@ -17,7 +17,6 @@ import org.eclipse.paho.client.mqttv3.MqttException;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
-import java.util.concurrent.ConcurrentSkipListSet;
 
 /**
  * 应用部署
@@ -28,7 +27,6 @@ import java.util.concurrent.ConcurrentSkipListSet;
 @Slf4j
 @Service
 public class AppDeployer {
-    private final Set<String> onDeploying = new ConcurrentSkipListSet<>();
     private final AsyncMqttClient mqttClient;
     private final AppDeployingService deployingService;
     private final AppDeployConfigQuery deployConfigQuery;
@@ -42,17 +40,11 @@ public class AppDeployer {
 
     public void deploy(BuildLog buildLog) throws MqttException {
         String appId = buildLog.getAppId();
-        if (!onDeploying.add(appId)) {
-            return;
-        }
-
         List<AppDeployConfig> deployConfigs = deployConfigQuery.findByAppId(appId);
         // TODO 使用线程池并发执行
         for (AppDeployConfig appDeployConfig : deployConfigs) {
             deploy(buildLog, appDeployConfig);
         }
-
-        onDeploying.remove(appId);
     }
 
     /**
@@ -93,6 +85,5 @@ public class AppDeployer {
         // TODO 对于需要返回值的 pub,需要做一个记录,pub 和 sub 一一对应
         String topic = MsgQueue.dagentTopic(machineId);
         mqttClient.pubWithResult(topic, 1, message);
-        onDeploying.remove(appId);
     }
 }

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildDeployService.java

@@ -8,6 +8,6 @@ public interface BuildDeployService {
     void buildAndDeploy(String appId, boolean isDeploy) throws Exception;
     default void build(String appId, String commitId) {}
     void deploy(String buildLogId, String machineId) throws Exception;
+    String packageUrl(String buildLogId) throws Exception;
     void dlPackage(String filename) throws Exception;
-    void downloadPackage(String buildLogId) throws Exception;
 }

+ 32 - 43
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/impl/BuildDeployServiceImpl.java

@@ -1,8 +1,6 @@
 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.ExceptionUtil;
 import cn.reghao.autodop.common.util.thread.ThreadPoolWrapper;
@@ -21,19 +19,16 @@ import cn.reghao.autodop.dmaster.app.service.bd.AppBuildingService;
 import cn.reghao.autodop.dmaster.util.UploadDownload;
 import cn.reghao.autodop.dmaster.spring.HttpUtil;
 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.io.File;
 import java.io.FileInputStream;
-import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentSkipListSet;
 import java.util.concurrent.ExecutorService;
-import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -43,6 +38,7 @@ import java.util.stream.Collectors;
 @Service
 public class BuildDeployServiceImpl implements BuildDeployService {
     private final Set<String> onBuilding = new ConcurrentSkipListSet<>();
+    private final Set<String> onDeploying = new ConcurrentSkipListSet<>();
     private final ExecutorService threadPool;
     private final AsyncMqttClient mqttClient;
     private final AppBuildingService buildingService;
@@ -106,11 +102,17 @@ public class BuildDeployServiceImpl implements BuildDeployService {
                 .thenAccept(buildLog -> {
                     if (isDeploy && buildLog.getResult().getCode() == ResultStatus.SUCCESS.getCode()) {
                         log.info("开始部署 {}", appId);
+                        if (!onDeploying.add(appId)) {
+                            return;
+                        }
+
                         try {
                             appDeployer.deploy(buildLog);
                         } catch (MqttException e) {
                             String errMsg = ExceptionUtil.errorMsg(e);
                             log.error("部署 {} 时抛出异常: {}", appId, errMsg);
+                        } finally {
+                            onDeploying.remove(appId);
                         }
                     }
                 });
@@ -124,31 +126,20 @@ public class BuildDeployServiceImpl implements BuildDeployService {
         }
 
         String appId = buildLog.getAppId();
+        if (!onDeploying.add(appId)) {
+            return;
+        }
+
         AppDeployConfig deployConfig = deployConfigQuery.findByAppIdAndMachineId(appId, machineId);
         if (deployConfig != null) {
             appDeployer.deploy(buildLog, deployConfig);
         }
-    }
 
-    @Override
-    public void dlPackage(String filename) throws Exception {
-        String[] arr = filename.split("_");
-        if (arr.length != 2) {
-            String errMsg = String.format("%s 不合法", filename);
-            throw new Exception(errMsg);
-        }
-
-        String appId = arr[0];
-        String commitId = arr[1];
-        String localPath = String.format("%s%s%s%s%s",
-                LocalBuildDir.packDir, File.separator, appId, File.separator, filename);
-        FileInputStream fileInputStream = new FileInputStream(localPath);
-        //BufferedInputStream bis = new BufferedInputStream(fileInputStream);
-        uploadDownload.download(filename, fileInputStream, HttpUtil.getResponse());
+        onDeploying.remove(appId);
     }
 
     @Override
-    public void downloadPackage(String buildLogId) throws Exception {
+    public String packageUrl(String buildLogId) throws Exception {
         BuildLog buildLog = buildLogQuery.findById(buildLogId);
         if (buildLog == null) {
             String errMsg = "构建不存在";
@@ -162,37 +153,35 @@ public class BuildDeployServiceImpl implements BuildDeployService {
             throw new Exception(errMsg);
         }
 
+        String packagePath = buildLog.getPackagePath();
         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);
-                dlPackage(filename);
-                break;
+                return packagePath;
             case zipHttp:
                 throw new Exception(String.format("打包类型 %s 暂未实现", packType));
+            case docker:
+                String errMsg = "docker 打包类型的应用不能下载";
+                throw new Exception(errMsg);
             default:
                 throw new Exception(String.format("打包类型 %s 不存在", packType));
         }
     }
 
-    public List<String> currentlyBuilding() {
-        return List.copyOf(onBuilding);
-    }
-
-    public List<String> currentlyDeploying() {
-        List<DeployParam> list = mqttClient.unreturnedRpc().values().stream()
-                .filter(rpcMsg -> {
-                    String clazz = rpcMsg.getClazz();
-                    String method = rpcMsg.getMethod();
-                    return clazz.equals(AppRpcClazz.class.getSimpleName()) && method.equals(AppRpcClazz.deploy.name());
-                })
-                .map(rpcMsg -> JsonConverter.jsonToObject(rpcMsg.getJsonParam(), DeployParam.class))
-                .collect(Collectors.toList());
+    @Override
+    public void dlPackage(String filename) throws Exception {
+        String[] arr = filename.split("_");
+        if (arr.length != 2) {
+            String errMsg = String.format("%s 不合法", filename);
+            throw new Exception(errMsg);
+        }
 
-        return list.stream().map(DeployParam::getAppId).collect(Collectors.toList());
+        String appId = arr[0];
+        String commitId = arr[1];
+        String localPath = String.format("%s%s%s%s%s",
+                LocalBuildDir.packDir, File.separator, appId, File.separator, filename);
+        FileInputStream fileInputStream = new FileInputStream(localPath);
+        //BufferedInputStream bis = new BufferedInputStream(fileInputStream);
+        uploadDownload.download(filename, fileInputStream, HttpUtil.getResponse());
     }
 }

+ 16 - 1
dmaster/src/main/resources/static/js/main.js

@@ -171,7 +171,7 @@ layui.use(['element', 'form', 'layer', 'upload'], function () {
         });*/
     });
 
-    /* get方式异步 */
+    /* get 方式异步 */
     $(document).on("click", ".ajax-get", function (e) {
         e.preventDefault();
         var msg = $(this).data("msg");
@@ -191,6 +191,21 @@ layui.use(['element', 'form', 'layer', 'upload'], function () {
         }
     });
 
+    /* ajax 重定向 */
+    $(document).on("click", ".ajax-redirect", function (e) {
+        e.preventDefault();
+        $.get(e.target.href, function (result) {
+            console.log(result)
+
+            var code = result.code
+            if (code === 0) {
+                window.location.href = result.msg
+            } else {
+                $.fn.Messager(result);
+            }
+        });
+    });
+
     /* ajax delete 操作 */
     $(document).on("click", ".ajax-delete", function (e) {
         e.preventDefault();

+ 2 - 2
dmaster/src/main/resources/templates/app/bd/index.html

@@ -75,8 +75,8 @@
                     <td>
                         <a class="ajax-post" th:href="@{'/api/app/bd/update/'+${item.appId}}">更新</a>
                         <a class="ajax-post" th:href="@{'/api/app/bd/build/'+${item.appId}}">构建</a>
-                        <!--<a class="ajax-get" th:href="@{'/api/app/bd/dl1/'+${item.buildLogId}}">下载</a>-->
-                        <a th:href="@{'/api/app/bd/dl1/'+${item.buildLogId}}">下载</a>
+                        <a class="ajax-redirect" th:href="@{'/api/app/bd/dlpak/'+${item.buildLogId}}">下载</a>
+                        <!--<a th:href="@{'/api/app/bd/dl1/'+${item.buildLogId}}">下载</a>-->
                         <a class="open-popup" data-title="应用部署状态" th:attr="data-url=@{'/app/bd/deploy/'+${item.appId}}"
                            data-size="1000,500" href="#">部署状态</a>
                     </td>