Ver código fonte

判断算法平台算法是否已经导入

martin 2 anos atrás
pai
commit
2d285d3041

+ 4 - 4
api-common/src/main/java/api/common/util/SshUtil.java

@@ -51,13 +51,13 @@ public class SshUtil {
      */
     public static ClientSession getSession(SshClient sshClient, String hostname, String username, String password) throws IOException {
         sshClient.start();
-        ClientSession session = sshClient.connect(username, hostname, port).verify(3600000).getSession();
-        session.addPasswordIdentity(password);
-        AuthFuture verify = session.auth().verify(3600000);
+        ClientSession clientSession = sshClient.connect(username, hostname, port).verify(3600000).getSession();
+        clientSession.addPasswordIdentity(password);
+        AuthFuture verify = clientSession.auth().verify(3600000);
         if (verify.isFailure()) {
             throw new RuntimeException("------- ssh 用户名密码验证失败!");
         }
-        return session;
+        return clientSession;
     }
 
     /**

+ 0 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ProjectConsumer.java

@@ -207,7 +207,6 @@ public class ProjectConsumer {
         // 设置任务数量之后将项目运行信息放入 redis
         stringRedisTemplate.opsForValue().set(projectRunningKey, JsonUtil.beanToJson(projectMessageDTO));
         Set<ScenePO> scenePOSet = new HashSet<>(scenePOList); // 如果不去重的话会出现多个场景重复关联多个指标
-
         // -------------------------------- 2 查询模型 --------------------------------
         //2-1 根据车辆配置id vehicleConfigId, 获取 模型信息和传感器信息
         VehiclePO vehiclePO = vehicleMapper.selectByVehicleConfigId(vehicleConfigId);   // 车辆

+ 23 - 16
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/ProjectService.java

@@ -359,30 +359,37 @@ public class ProjectService {
                     throw new RuntimeException("算法 " + algorithmId + " 的 upload_mode 字段数据有误!");
                 }
                 algorithmMapper.updateDockerImportAndDockerImageById("1", dockerImage, algorithmId);
+                LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
+                FileUtil.rm(algorithmTarLinuxTempPath);
                 log.info("ProjectService--handleAlgorithm 已删除算法临时文件:" + algorithmTarLinuxTempPath);
             } else {
                 throw new RuntimeException("算法 " + algorithmId + " 的 mysql 数据有误!");
             }
         } else { // 从索为算法平台下载算法
             log.info("ProjectService--handleAlgorithm 项目" + projectId + "需要从索为平台下载算法 " + algorithmId);
-            dockerImage = "algorithm_" + algorithmId + ":latest";
-            //查看算法是否已经导入
             algorithmTarLinuxTempPath = linuxTempPath + "algorithm/" + algorithmId + ".tar";
-            //1 获取 token
-            String tokenUrl = algorithmPlatformTokenUri + "?grant_type=client_credential&appid=" + algorithmPlatformAppid + "&secret=" + algorithmPlatformSecret;
-            String tokenJson = HttpUtil.get(closeableHttpClient, requestConfig, tokenUrl);
-            String token = new ObjectMapper().readTree(tokenJson).path("data").path("access_token").asText();
-            //2 获取 下载地址
-            String downloadUrl = algorithmPlatformAlgorithmAddrUri + "?access_token=" + token + "&id=" + algorithmId;
-            //3 下载算法包
-            String downloadUrlJson = HttpUtil.get(closeableHttpClient, requestConfig, downloadUrl);
-            String tempDownloadUrl = new ObjectMapper().readTree(downloadUrlJson).path("data").asText();
-            InputStream inputStream = HttpUtil.getInputStream(closeableHttpClient, requestConfig, tempDownloadUrl);
-            FileUtil.writeInputStreamToLocalFile(inputStream, algorithmTarLinuxTempPath);
+            String dockerImageWithoutVersion = "algorithm_" + algorithmId;
+            dockerImage = dockerImageWithoutVersion + ":latest";
+            //0 查看算法是否已经导入
+            if (!projectUtil.isImported(dockerImageWithoutVersion)) {
+                //1 获取 token
+                String tokenUrl = algorithmPlatformTokenUri + "?grant_type=client_credential&appid=" + algorithmPlatformAppid + "&secret=" + algorithmPlatformSecret;
+                String tokenJson = HttpUtil.get(closeableHttpClient, requestConfig, tokenUrl);
+                String token = new ObjectMapper().readTree(tokenJson).path("data").path("access_token").asText();
+                //2 获取 下载地址
+                String downloadUrl = algorithmPlatformAlgorithmAddrUri + "?access_token=" + token + "&id=" + algorithmId;
+                //3 下载算法包
+                String downloadUrlJson = HttpUtil.get(closeableHttpClient, requestConfig, downloadUrl);
+                String tempDownloadUrl = new ObjectMapper().readTree(downloadUrlJson).path("data").asText();
+                InputStream inputStream = HttpUtil.getInputStream(closeableHttpClient, requestConfig, tempDownloadUrl);
+                FileUtil.writeInputStreamToLocalFile(inputStream, algorithmTarLinuxTempPath);
+                // 本地执行 docker import 算法文件成镜像(可改成用 docker-java 操作仓库)
+                LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
+                FileUtil.rm(algorithmTarLinuxTempPath);
+                log.info("ProjectService--handleAlgorithm 已删除算法临时文件:" + algorithmTarLinuxTempPath);
+            }
         }
-        // 本地执行 docker load 算法文件成镜像(可改成用 docker-java 操作仓库)
-        LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
-        FileUtil.rm(algorithmTarLinuxTempPath);
+
 //        LinuxUtil.execute("docker tag " + algorithmTarLinuxTempPath + " " + dockerImage);   // 标记镜像名称
 //        LinuxUtil.execute("docker login " + algorithmTarLinuxTempPath + " " + dockerImage); // 登录 harbor
 //        LinuxUtil.execute("docker push " + algorithmTarLinuxTempPath + " " + dockerImage);  // 推送镜像到仓库

+ 12 - 4
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/ProjectUtil.java

@@ -2,10 +2,7 @@ package com.css.simulation.resource.scheduler.util;
 
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.dto.ProjectMessageDTO;
-import api.common.util.CollectionUtil;
-import api.common.util.FileUtil;
-import api.common.util.JsonUtil;
-import api.common.util.StringUtil;
+import api.common.util.*;
 import com.css.simulation.resource.scheduler.configuration.kubernetes.KubernetesConfiguration;
 import com.css.simulation.resource.scheduler.mapper.AutoSubProjectMapper;
 import com.css.simulation.resource.scheduler.mapper.ClusterMapper;
@@ -64,6 +61,17 @@ public class ProjectUtil {
     @Resource
     ApiClient apiClient;
 
+    /**
+     * 判断算法是否已经导入
+     *
+     * @return 算法是否已经导入
+     */
+    @SneakyThrows
+    public boolean isImported(String dockerImageWithoutVersion) {
+        String dockerImageListString = LinuxUtil.execute("docker images");
+        return dockerImageListString.contains(dockerImageWithoutVersion);
+    }
+
     public String getRandomPodName(String projectId) {
         return "project-" + projectId + "-" + StringUtil.getRandomEightBitUUID();
     }