Преглед на файлове

构建部署 app 时使用动态的 DockerAuth

reghao преди 1 година
родител
ревизия
ccf5169417

+ 26 - 1
common/src/main/java/cn/reghao/bnt/common/agent/app/iface/impl/DockerApp.java

@@ -3,6 +3,7 @@ package cn.reghao.bnt.common.agent.app.iface.impl;
 import cn.reghao.bnt.common.docker.Docker;
 import cn.reghao.bnt.common.docker.DockerImpl;
 import cn.reghao.bnt.common.docker.po.Config;
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import cn.reghao.bnt.common.docker.po.HostConfig;
 import cn.reghao.bnt.common.machine.Machine;
 import cn.reghao.bnt.common.msg.constant.NodeStatus;
@@ -13,12 +14,19 @@ import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import com.github.dockerjava.api.command.InspectContainerResponse;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * @author reghao
  * @date 2023-03-06 15:26:22
  */
 public class DockerApp {
-    private final Docker docker = new DockerImpl();
+    private Map<String, Docker> map = new HashMap<>();
+
+    public DockerApp() {
+        this.map.put("noAuth", new DockerImpl());
+    }
 
     public EvtAppStatResult deploy(EvtAppDeploy deployParam) throws Exception {
         String appId = deployParam.getAppId();
@@ -33,27 +41,44 @@ public class DockerApp {
             }
         }
 
+        Docker docker;
+        DockerAuth dockerAuth = deployParam.getDockerAuth();
+        if (dockerAuth != null) {
+            String registryUrl = dockerAuth.getRegistryUrl();
+            docker = map.get(registryUrl);
+            if (docker == null) {
+                // TODO DockerAuth 修改后 map 中的 Docker 实例无效
+                docker = new DockerImpl(dockerAuth);
+                map.put(registryUrl, docker);
+            }
+        } else {
+            docker = map.get("noAuth");
+        }
         docker.pull(packagePath);
         InspectContainerResponse containerInfo = docker.createAndRun(appId, containerConfig);
         return getStat(appId, containerInfo);
     }
 
     public EvtAppStatResult start(String appId) throws Exception {
+        Docker docker = map.get("noAuth");
         InspectContainerResponse containerInfo = docker.start(appId);
         return getStat(appId, containerInfo);
     }
 
     public EvtAppStatResult stop(String appId) throws Exception {
+        Docker docker = map.get("noAuth");
         InspectContainerResponse containerInfo = docker.stop(appId);
         return getStat(appId, containerInfo);
     }
 
     public EvtAppStatResult restart(String appId) throws Exception {
+        Docker docker = map.get("noAuth");
         InspectContainerResponse containerInfo = docker.restart(appId);
         return getStat(appId, containerInfo);
     }
 
     public EvtAppStatResult stat(String appId) throws Exception {
+        Docker docker = map.get("noAuth");
         InspectContainerResponse containerInfo = docker.inspectContainer(appId);
         return getStat(appId, containerInfo);
     }

+ 9 - 4
common/src/main/java/cn/reghao/bnt/common/docker/DockerImpl.java

@@ -2,6 +2,7 @@ package cn.reghao.bnt.common.docker;
 
 import cn.reghao.bnt.common.docker.po.Config;
 import cn.reghao.bnt.common.docker.po.ContainerInfo;
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import cn.reghao.bnt.common.docker.po.ImageInfo;
 import cn.reghao.jutil.jdk.exception.ExceptionUtil;
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
@@ -46,15 +47,15 @@ public class DockerImpl implements Docker {
         init(config);
     }
 
-    public DockerImpl(String username, String password) {
+    public DockerImpl(DockerAuth dockerAuth) {
         DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                 .withDockerHost("unix://" + unixSock)
                 .withDockerTlsVerify(false)
                 //.withDockerCertPath(String.format("%s/.docker", System.getProperty("user.home")))
-                .withRegistryUsername("username")
-                .withRegistryPassword("password")
+                .withRegistryUrl(dockerAuth.getRegistryUrl())
+                .withRegistryUsername(dockerAuth.getUsername())
+                .withRegistryPassword(dockerAuth.getPassword())
                 //.withRegistryEmail(registryMail)
-                //.withRegistryUrl(registryUrl)
                 .build();
         init(config);
     }
@@ -70,6 +71,10 @@ public class DockerImpl implements Docker {
         this.dockerClient = DockerClientImpl.getInstance(config, httpClient);
     }
 
+    public void auth() {
+        dockerClient.authCmd().exec();
+    }
+
     @Override
     public void build(String repoTag, String compileHome, String dockerfileContent) throws Exception {
         File dockerfile = new File(compileHome + "/Dockerfile.tmp");

+ 2 - 0
common/src/main/java/cn/reghao/bnt/common/msg/event/EvtAppDeploy.java

@@ -1,5 +1,6 @@
 package cn.reghao.bnt.common.msg.event;
 
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import cn.reghao.jutil.jdk.event.message.Event;
 import lombok.Getter;
 import lombok.Setter;
@@ -16,4 +17,5 @@ public class EvtAppDeploy extends Event {
     private String packagePath;
     private String startScript;
     private String startHome;
+    private DockerAuth dockerAuth;
 }

+ 4 - 4
web/src/main/java/cn/reghao/bnt/web/devops/app/model/po/config/AppConfig.java

@@ -114,10 +114,10 @@ public class AppConfig extends BaseEntity implements Cloneable {
     }
 
     public AppDto getAppDto() {
-        BuildConfigDto buildConfigDto =
-                new BuildConfigDto(this.repoAuthConfig.getRepoAuth(), this.compilerConfig.getCompilerDto(), this.packerConfig.getPackerDto());;
+        BuildConfigDto buildConfigDto = new BuildConfigDto(this.repoAuthConfig.getRepoAuth(),
+                this.compilerConfig.getCompilerDto(), this.packerConfig.getPackerDto(), this.packerConfig.getDockerAuth());
 
-        return new AppDto(this.appId, this.env, this.appType, this.appRepo, this.repoBranch, this.projDirname, this.appRootPath,
-                this.dockerfile, buildConfigDto);
+        return new AppDto(this.appId, this.env, this.appType, this.appRepo, this.repoBranch, this.projDirname,
+                this.appRootPath, this.dockerfile, buildConfigDto);
     }
 }

+ 14 - 0
web/src/main/java/cn/reghao/bnt/web/devops/app/model/po/config/build/PackerConfig.java

@@ -1,5 +1,7 @@
 package cn.reghao.bnt.web.devops.app.model.po.config.build;
 
+import cn.reghao.bnt.common.docker.po.DockerAuth;
+import cn.reghao.bnt.web.devops.app.model.po.DockerRegistry;
 import cn.reghao.bnt.web.devops.build.model.PackerDto;
 import cn.reghao.bnt.common.msg.constant.PackType;
 import cn.reghao.bnt.web.util.validator.provider.PackerConfigGroupSequenceProvider;
@@ -11,6 +13,7 @@ import org.hibernate.validator.group.GroupSequenceProvider;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.Pattern;
@@ -41,6 +44,8 @@ public class PackerConfig extends BaseEntity {
     // 构建生成的可执行文件及配置文件, 脚本文件等所在的目录名, 目录位于 appRootPath 下
     @NotBlank(groups = { ZipPacker.class }, message = "bin 目录不能为空")
     private String binDirname;
+    @OneToOne
+    private DockerRegistry dockerRegistry;
 
     public interface DockerPacker {
     }
@@ -51,4 +56,13 @@ public class PackerConfig extends BaseEntity {
     public PackerDto getPackerDto() {
         return new PackerDto(this.type, this.name, this.targetPath, this.binDirname);
     }
+
+    public DockerAuth getDockerAuth() {
+        DockerRegistry registry = this.dockerRegistry;
+        if (registry != null) {
+            return new DockerAuth(registry.getRegistryUrl(), registry.getUsername(), registry.getPassword());
+        }
+
+        return null;
+    }
 }

+ 4 - 2
web/src/main/java/cn/reghao/bnt/web/devops/app/service/DockerRegistryService.java

@@ -1,5 +1,6 @@
 package cn.reghao.bnt.web.devops.app.service;
 
+import cn.reghao.bnt.common.docker.DockerImpl;
 import cn.reghao.bnt.common.docker.po.DockerAuth;
 import cn.reghao.bnt.web.devops.app.db.repository.DockerRegistryRepository;
 import cn.reghao.bnt.web.devops.app.model.po.DockerRegistry;
@@ -18,6 +19,9 @@ public class DockerRegistryService {
     }
 
     public void addOrModify(DockerAuth dockerAuth) {
+        DockerImpl docker = new DockerImpl(dockerAuth);
+        docker.auth();
+
         String registryUrl = dockerAuth.getRegistryUrl();
         DockerRegistry dockerRegistry = dockerAuthRepository.findByRegistryUrl(registryUrl);
         if (dockerRegistry == null) {
@@ -33,6 +37,4 @@ public class DockerRegistryService {
     public void delete(Integer id) {
         dockerAuthRepository.deleteById(id);
     }
-
-
 }

+ 10 - 0
web/src/main/java/cn/reghao/bnt/web/devops/app/service/bd/impl/DeployAppImpl.java

@@ -1,8 +1,11 @@
 package cn.reghao.bnt.web.devops.app.service.bd.impl;
 
+import cn.reghao.bnt.common.docker.po.DockerAuth;
+import cn.reghao.bnt.common.msg.constant.PackType;
 import cn.reghao.bnt.web.devops.app.db.query.AppDeployConfigQuery;
 import cn.reghao.bnt.web.devops.app.db.repository.log.BuildLogRepository;
 import cn.reghao.bnt.web.devops.app.model.po.AppBuilding;
+import cn.reghao.bnt.web.devops.app.model.po.DockerRegistry;
 import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
 import cn.reghao.bnt.web.devops.app.model.po.config.AppDeployConfig;
 import cn.reghao.bnt.web.devops.app.model.po.log.BuildLog;
@@ -84,6 +87,13 @@ public class DeployAppImpl implements DeployApp {
         deployParam.setPackType(packType);
         deployParam.setStartScript(startScript);
         deployParam.setStartHome(startHome);
+        if (packType.equals(PackType.docker.getName())) {
+            DockerRegistry registry = appDeployConfig.getAppConfig().getPackerConfig().getDockerRegistry();
+            if (registry != null) {
+                DockerAuth dockerAuth = new DockerAuth(registry.getRegistryUrl(), registry.getUsername(), registry.getPassword());
+                deployParam.setDockerAuth(dockerAuth);
+            }
+        }
 
         if (deployStat.isDeploying(appId, machineId)) {
             log.info("应用正在部署...");

+ 4 - 4
web/src/main/java/cn/reghao/bnt/web/devops/build/chain/BuildTools.java

@@ -1,5 +1,6 @@
 package cn.reghao.bnt.web.devops.build.chain;
 
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import cn.reghao.bnt.web.devops.build.model.AppDto;
 import cn.reghao.bnt.web.devops.build.model.CompilerDto;
 import cn.reghao.bnt.web.devops.build.model.PackerDto;
@@ -9,8 +10,6 @@ import cn.reghao.bnt.web.devops.build.model.constant.RepoType;
 import cn.reghao.bnt.web.devops.build.tool.compiler.EmptyCompiler;
 import cn.reghao.bnt.web.devops.build.tool.compiler.MavenCompiler;
 import cn.reghao.bnt.web.devops.build.tool.compiler.ShellCompiler;
-import cn.reghao.bnt.common.docker.Docker;
-import cn.reghao.bnt.common.docker.DockerImpl;
 import cn.reghao.bnt.common.msg.constant.PackType;
 import cn.reghao.bnt.web.devops.build.tool.compiler.CodeCompiler;
 import cn.reghao.bnt.web.devops.build.tool.packer.CodePacker;
@@ -67,6 +66,8 @@ public class BuildTools {
     public static void init(AppDto appDto) throws Exception {
         String appId = appDto.getAppId();
         RepoAuth repoAuth = appDto.getBuildConfigDto().getRepoAuth();
+        DockerAuth dockerAuth = appDto.getBuildConfigDto().getDockerAuth();
+
         String repoAuthName = repoAuth.getName();
         CodeUpdater codeUpdater = codeUpdaterMap.get(repoAuthName);
         if (codeUpdater == null) {
@@ -81,7 +82,6 @@ public class BuildTools {
         }
         buildConfig.computeIfAbsent(appId, k -> new HashMap<>()).put("codeUpdater", repoAuthName);
 
-        Docker docker = new DockerImpl();
         CompilerDto compilerDto = appDto.getBuildConfigDto().getCompilerDto();
         String compilerName = compilerDto.getName();
         CodeCompiler codeCompiler = codeCompilerMap.get(compilerName);
@@ -110,7 +110,7 @@ public class BuildTools {
         if (codePacker == null) {
             switch (PackType.valueOf(packerDto.getType())) {
                 case docker:
-                    codePacker = new DockerPack(packerDto.getTargetPath(), docker);
+                    codePacker = new DockerPack(packerDto.getTargetPath(), dockerAuth);
                     break;
                 default:
                     codePacker = new ZipPack(packerDto.getBinDirname());

+ 2 - 0
web/src/main/java/cn/reghao/bnt/web/devops/build/model/BuildConfigDto.java

@@ -1,5 +1,6 @@
 package cn.reghao.bnt.web.devops.build.model;
 
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 
@@ -13,4 +14,5 @@ public class BuildConfigDto {
     private RepoAuth repoAuth;
     private CompilerDto compilerDto;
     private PackerDto packerDto;
+    private DockerAuth dockerAuth;
 }

+ 4 - 2
web/src/main/java/cn/reghao/bnt/web/devops/build/tool/packer/DockerPack.java

@@ -1,6 +1,8 @@
 package cn.reghao.bnt.web.devops.build.tool.packer;
 
 import cn.reghao.bnt.common.docker.Docker;
+import cn.reghao.bnt.common.docker.DockerImpl;
+import cn.reghao.bnt.common.docker.po.DockerAuth;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.File;
@@ -16,9 +18,9 @@ public class DockerPack implements CodePacker {
     private final String targetPath;
     private final Docker docker;
 
-    public DockerPack(String targetPath, Docker docker) {
+    public DockerPack(String targetPath, DockerAuth dockerAuth) {
         this.targetPath = targetPath;
-        this.docker = docker;
+        this.docker = new DockerImpl(dockerAuth);
     }
 
     @Override