Prechádzať zdrojové kódy

1.CompilerConfig 中添加一个 compilerBinds 字段, 如果是 docker 镜像编译器, 则该字段用于设置 host 和 container 间的映射
2.这是因为编译阶段通常会伴随依赖缓存, 这些缓存不应放在容器中, 而应该放在 host 的磁盘上, 以便于重复使用

reghao 1 rok pred
rodič
commit
5d119ff00e

+ 6 - 4
mgr/src/main/java/cn/reghao/devops/mgr/mgr/build/chain/BuildTools.java

@@ -16,6 +16,7 @@ import cn.reghao.devops.mgr.mgr.build.tool.packer.DockerPack;
 import cn.reghao.devops.mgr.mgr.build.tool.packer.ZipPack;
 import cn.reghao.devops.mgr.mgr.build.tool.repo.CodeUpdater;
 import cn.reghao.devops.mgr.mgr.build.tool.repo.GitImpl;
+import cn.reghao.devops.mgr.mgr.builds.model.po.CompilerBind;
 
 import java.util.HashMap;
 import java.util.List;
@@ -95,14 +96,15 @@ public class BuildTools {
                     break;
                 case docker:
                     String compilerImage = compilerDto.getCompilerImage();
+                    List<CompilerBind> compilerBinds = compilerDto.getCompilerBinds();
                     String compileCmd = compilerDto.getCompileCmd();
                     Config config = new Config(compilerImage);
                     config.setVolumes(new Volumes());
-                    config.setCmd(List.of("sh", "-c", "cd /app && " + compileCmd));
+                    config.setCmd(List.of("sh", "-c", compileCmd));
+                    compilerBinds.forEach(bind -> {
+                        config.getVolumes().getMap().put(bind.getHostPath(), bind.getContainerPath());
+                    });
 
-                    String nodeModulesDir = "/home/reghao/Downloads/0/node_modules";
-                    String containerPath = "/app/node_modules";
-                    config.getVolumes().getMap().put(nodeModulesDir, containerPath);
                     codeCompiler = new DockerCompiler(config);
                     break;
                 case none:

+ 3 - 0
mgr/src/main/java/cn/reghao/devops/mgr/mgr/build/model/CompilerDto.java

@@ -1,9 +1,11 @@
 package cn.reghao.devops.mgr.mgr.build.model;
 
+import cn.reghao.devops.mgr.mgr.builds.model.po.CompilerBind;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * @author reghao
@@ -19,4 +21,5 @@ public class CompilerDto implements Serializable {
     private String homePath;
     private String compileCmd;
     private String compilerImage;
+    private List<CompilerBind> compilerBinds;
 }

+ 18 - 0
mgr/src/main/java/cn/reghao/devops/mgr/mgr/builds/controller/page/CompilerPageController.java

@@ -1,6 +1,7 @@
 package cn.reghao.devops.mgr.mgr.builds.controller.page;
 
 import cn.reghao.devops.mgr.mgr.builds.db.repository.CompilerConfigRepository;
+import cn.reghao.devops.mgr.mgr.builds.model.po.CompilerBind;
 import cn.reghao.devops.mgr.mgr.builds.model.po.CompilerConfig;
 import cn.reghao.devops.common.util.KeyValue;
 import cn.reghao.devops.mgr.mgr.build.model.constant.CompileType;
@@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -93,4 +95,20 @@ public class CompilerPageController {
         model.addAttribute("content", result);
         return "/devops/build/compiler/version";
     }
+
+    @ApiOperation(value = "docker 编译器映射目录页面")
+    @PreAuthorize("hasRole('ROLE_ADMIN')")
+    @GetMapping("/bind/{id}")
+    public String compilerBindPage(@PathVariable("id") int id, Model model) {
+        CompilerConfig compilerConfig = compilerConfigRepository.findById(id).orElse(null);
+        if (compilerConfig != null && compilerConfig.getType().equals(CompileType.docker.getName())) {
+            List<CompilerBind> list = compilerConfig.getCompilerBinds();
+            model.addAttribute("list", list);
+            return "/devops/build/compiler/bind";
+        } else {
+            String content = "not docker type";
+            model.addAttribute("content", content);
+            return "/devops/build/compiler/version";
+        }
+    }
 }

+ 22 - 0
mgr/src/main/java/cn/reghao/devops/mgr/mgr/builds/model/po/CompilerBind.java

@@ -0,0 +1,22 @@
+package cn.reghao.devops.mgr.mgr.builds.model.po;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+import javax.persistence.Embeddable;
+
+/**
+ * @author reghao
+ * @date 2024-08-20 17:29:17
+ */
+@AllArgsConstructor
+@NoArgsConstructor
+@Setter
+@Getter
+@Embeddable
+public class CompilerBind {
+    private String hostPath;
+    private String containerPath;
+}

+ 9 - 4
mgr/src/main/java/cn/reghao/devops/mgr/mgr/builds/model/po/CompilerConfig.java

@@ -6,14 +6,15 @@ import cn.reghao.devops.mgr.mgr.builds.model.provider.CompilerConfigGroupSequenc
 import cn.reghao.devops.mgr.admin.util.validator.ValidEnum;
 import cn.reghao.devops.mgr.admin.util.BaseEntity;
 import lombok.*;
+import org.hibernate.annotations.LazyCollection;
+import org.hibernate.annotations.LazyCollectionOption;
 import org.hibernate.validator.constraints.Length;
 import org.hibernate.validator.group.GroupSequenceProvider;
 
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.Table;
+import javax.persistence.*;
 import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.Pattern;
+import java.util.List;
 
 /**
  * 编译方式配置
@@ -43,6 +44,10 @@ public class CompilerConfig extends BaseEntity {
     private String versionCmd;
     @NotBlank(groups = { DockerCompiler.class}, message = "编译器版本命令不能为空")
     private String compilerImage;
+    @ElementCollection(targetClass = CompilerBind.class)
+    @LazyCollection(LazyCollectionOption.FALSE)
+    @CollectionTable(name = "devops_compiler_config_binds")
+    private List<CompilerBind> compilerBinds;
 
     public interface NoneCompiler {
     }
@@ -57,6 +62,6 @@ public class CompilerConfig extends BaseEntity {
     }
 
     public CompilerDto getCompilerDto() {
-        return new CompilerDto(this.type, this.name, this.homePath, this.compileCmd, this.compilerImage);
+        return new CompilerDto(this.type, this.name, this.homePath, this.compileCmd, this.compilerImage, this.compilerBinds);
     }
 }

+ 32 - 0
mgr/src/main/resources/templates/devops/build/compiler/bind.html

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})">
+    <style>
+        a{
+            color: #009688;
+        }
+        a:hover{
+            color: #004a43;
+            text-decoration: underline;
+        }
+    </style>
+</head>
+<body>
+<div class="timo-detail-page">
+    <table class="layui-table">
+        <thead>
+        <tr>
+            <th>host 路径</th>
+            <th>container 路径</th>
+        </tr>
+        </thead>
+        <tbody>
+        <tr th:each="item:${list}">
+            <td>[[${item.hostPath}]]</td>
+            <td>[[${item.containerPath}]]</td>
+        </tr>
+        </tbody>
+    </table>
+</div>
+</body>
+</html>

+ 6 - 1
mgr/src/main/resources/templates/devops/build/compiler/index.html

@@ -27,6 +27,7 @@
                     <th class="sortable" data-field="name">编译名字</th>
                     <th class="sortable" data-field="homePath">编译器主目录</th>
                     <th class="sortable" data-field="compilerImage">编译器镜像</th>
+                    <th class="sortable" data-field="binds">镜像映射</th>
                     <th class="sortable" data-field="cmd">命令</th>
                     <th class="sortable" data-field="version">版本</th>
                     <th>操作</th>
@@ -37,7 +38,11 @@
                     <td th:text="${item.type}">编译类型</td>
                     <td th:text="${item.name}">编译名字</td>
                     <td th:text="${item.homePath}">编译器主目录</td>
-                    <td th:text="${item.compilerImage}">编译器主目录</td>
+                    <td th:text="${item.compilerImage}">编译器镜像</td>
+                    <td>
+                        <a class="open-popup" data-title="镜像映射" th:attr="data-url=@{'/build/compiler/bind/'+${item.id}}"
+                           data-size="720,480" href="#">查看</a>
+                    </td>
                     <td>
                         <a class="open-popup" data-title="命令" th:attr="data-url=@{'/build/compiler/detail/'+${item.id}}"
                            data-size="720,480" href="#">查看</a>