Browse Source

下载私有仓库算法

martin 2 năm trước cách đây
mục cha
commit
eb86333f2b

+ 14 - 6
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/ProjectService.java

@@ -52,8 +52,6 @@ public class ProjectService {
     String algorithmPlatformTokenUri;
     @Value("${algorithm-platform.algorithm-addr-uri}")
     String algorithmPlatformAlgorithmAddrUri;
-
-
     @Value("${scheduler.minio-path.project-result}")
     String projectResultPathOfMinio;
     @Value("${scheduler.linux-path.temp}")
@@ -339,29 +337,39 @@ public class ProjectService {
         //4-1 根据算法 id 获取算法文件地址、是否已导入成镜像。
         AlgorithmPO algorithmPO = algorithmMapper.selectById(algorithmId);
         String dockerImage = dockerConfiguration.getRegistry() + "/";
-        String algorithmTarLinuxTempPath;
+        String algorithmDirectoryLinuxTempPath;
+        String algorithmTarLinuxTempPath = null;
         if (algorithmPO != null) { // 使用仿真平台自己的算法
             String algorithmCode = algorithmPO.getAlgorithmCode();
             String dockerImport = algorithmPO.getDockerImport();
-            dockerImage += "algorithm_" + algorithmCode + ":latest";
-            algorithmTarLinuxTempPath = linuxTempPath + "algorithm/" + algorithmCode + ".tar";
+            dockerImage += dockerConfiguration.getRegistry() + "/algorithm_" + algorithmCode + ":latest";
             if ("1".equals(dockerImport) && StringUtil.isNotEmpty(dockerImport)) { // 已经导入同时存在镜像名称
                 dockerImage = algorithmPO.getDockerImage();
                 return dockerImage;
             } else if (dockerImport == null || "0".equals(dockerImport)) {  // 未导入
                 String uploadMode = algorithmPO.getUploadMode();
                 if (DictConstants.ALGORITHM_UPLOAD_MODE_FILE.equals(uploadMode)) {
+                    algorithmTarLinuxTempPath = linuxTempPath + "algorithm-file/" + algorithmCode + "/" + algorithmCode + ".tar";
                     String minioPath = algorithmPO.getMinioPath();
                     MinioUtil.downloadToFile(minioClient, bucketName, minioPath, algorithmTarLinuxTempPath);    // 下载算法文件到本地
                 } else if (DictConstants.ALGORITHM_UPLOAD_MODE_GIT.equals(uploadMode)) {
+                    algorithmDirectoryLinuxTempPath = linuxTempPath + "algorithm-git/" + algorithmCode + "/";
                     String gitUrl = algorithmPO.getGitUrl();
                     String gitUserName = algorithmPO.getGitUserName();
                     String gitPassword = algorithmPO.getGitPassword();
-                    GitUtil.clone(gitUrl, gitUserName, gitPassword, algorithmTarLinuxTempPath); // 下载算法文件到本地
+                    GitUtil.clone(gitUrl, gitUserName, gitPassword, algorithmDirectoryLinuxTempPath); // 下载算法文件到目录
+                    for (String filename : Objects.requireNonNull(new File(algorithmDirectoryLinuxTempPath).list())) {
+                        if (filename.endsWith(".tar")) {
+                            algorithmTarLinuxTempPath = algorithmDirectoryLinuxTempPath + filename;
+                        }
+                    }
                 } else {
                     throw new RuntimeException("算法 " + algorithmId + " 的 upload_mode 字段数据有误!");
                 }
                 algorithmMapper.updateDockerImportAndDockerImageById("1", dockerImage, algorithmId);
+                if (algorithmTarLinuxTempPath == null) {
+                    throw new RuntimeException("ProjectService--handleAlgorithm 算法" + algorithmId + "下载失败。");
+                }
                 LinuxUtil.execute("docker import " + algorithmTarLinuxTempPath + " " + dockerImage);
                 LinuxUtil.execute("docker push " + dockerImage);
                 FileUtil.rm(algorithmTarLinuxTempPath);

+ 20 - 8
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/GitUtil.java

@@ -1,11 +1,11 @@
 package com.css.simulation.resource.scheduler.util;
 
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
 import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 
 import java.io.File;
-import java.util.Objects;
 
 /**
  * <!-- git 操作类库 -->
@@ -15,18 +15,30 @@ import java.util.Objects;
  * <version>${org.eclipse.jgit.version}</version>
  * </dependency>
  */
+@Slf4j
 public class GitUtil {
 
-    public static boolean clone(String url, String username, String password, String localDirectory) throws GitAPIException {
-        File localDirectoryObject = new File(localDirectory);
-        if (Objects.requireNonNull(localDirectoryObject.listFiles()).length > 0) {
-            return false;
+    /**
+     * @param url                 git 地址
+     * @param username            用户名
+     * @param password            密码
+     * @param localEmptyDirectory 空目录
+     */
+    @SneakyThrows
+    public static void clone(String url, String username, String password, String localEmptyDirectory) {
+        File localEmptyDirectoryFile = new File(localEmptyDirectory);
+        if (localEmptyDirectoryFile.exists()) {
+            File[] files = localEmptyDirectoryFile.listFiles();
+            if (files == null || files.length > 0) {
+                throw new RuntimeException("GitUtil--clone 目标下载目录不是空目录。");
+            }
+        } else {
+            localEmptyDirectoryFile.mkdirs();
         }
         Git git = Git.cloneRepository()
                 .setURI(url)
                 .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
-                .setDirectory(localDirectoryObject)
+                .setDirectory(localEmptyDirectoryFile)
                 .call();
-        return true;
     }
 }