|
|
@@ -1,213 +0,0 @@
|
|
|
-package cn.reghao.autodop.common.docker;
|
|
|
-
|
|
|
-import cn.reghao.autodop.common.docker.po.Config;
|
|
|
-import cn.reghao.autodop.common.util.ExceptionUtil;
|
|
|
-import cn.reghao.jutil.jdk.text.TextFile;
|
|
|
-import com.github.dockerjava.api.DockerClient;
|
|
|
-import com.github.dockerjava.api.command.*;
|
|
|
-import com.github.dockerjava.api.model.Container;
|
|
|
-import com.github.dockerjava.api.model.HostConfig;
|
|
|
-import com.github.dockerjava.api.model.RestartPolicy;
|
|
|
-import com.github.dockerjava.core.DefaultDockerClientConfig;
|
|
|
-import com.github.dockerjava.core.DockerClientConfig;
|
|
|
-import com.github.dockerjava.core.DockerClientImpl;
|
|
|
-import com.github.dockerjava.core.command.PushImageResultCallback;
|
|
|
-import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
|
|
-import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.time.Duration;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * Docker 客户端
|
|
|
- *
|
|
|
- * @author reghao
|
|
|
- * @date 2021-10-27 03:41:38
|
|
|
- */
|
|
|
-public class DockerImpl implements Docker {
|
|
|
- private final DockerClient dockerClient;
|
|
|
- private final TextFile textFile = new TextFile();
|
|
|
-
|
|
|
- public DockerImpl() {
|
|
|
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
- .withDockerHost("unix:///var/run/docker.sock")
|
|
|
- .withDockerTlsVerify(false)
|
|
|
- //.withDockerCertPath("/home/user/.docker")
|
|
|
- //.withRegistryUsername(registryUser)
|
|
|
- //.withRegistryPassword(registryPass)
|
|
|
- //.withRegistryEmail(registryMail)
|
|
|
- //.withRegistryUrl(registryUrl)
|
|
|
- .build();
|
|
|
-
|
|
|
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
- .dockerHost(config.getDockerHost())
|
|
|
- .sslConfig(config.getSSLConfig())
|
|
|
- .maxConnections(100)
|
|
|
- .connectionTimeout(Duration.ofSeconds(30))
|
|
|
- .responseTimeout(Duration.ofSeconds(45))
|
|
|
- .build();
|
|
|
-
|
|
|
- this.dockerClient = DockerClientImpl.getInstance(config, httpClient);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void build(String repoTag, String compileHome, String dockerfileContent) throws DockerException {
|
|
|
- File dockerfile = new File(compileHome + "/Dockerfile.tmp");
|
|
|
- String[] arr = repoTag.split(":");
|
|
|
- try {
|
|
|
- textFile.write(dockerfile, dockerfileContent);
|
|
|
- // TODO 需要判断构建成功与否,FROM 中的镜像不存在时并不会抛出异常
|
|
|
- dockerClient.buildImageCmd()
|
|
|
- .withDockerfile(dockerfile)
|
|
|
- // 格式为 docker.reghao.cn/autodop/dmaster:12345678
|
|
|
- .withTags(Set.of(repoTag))
|
|
|
- .exec(new BuildImageResultCallback()).awaitCompletion();
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void push(String image) throws DockerException {
|
|
|
- try {
|
|
|
- dockerClient.pushImageCmd(image).exec(new PushImageResultCallback()).awaitCompletion();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
- }
|
|
|
- /*dockerClient.pushImageCmd(image).exec(new ResultCallback<PushResponseItem>() {
|
|
|
- @Override
|
|
|
- public void onStart(Closeable closeable) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onNext(PushResponseItem object) {
|
|
|
- System.out.println(object.getStatus());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onError(Throwable throwable) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onComplete() {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void close() throws IOException {
|
|
|
-
|
|
|
- }
|
|
|
- }).onComplete();*/
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void pull(String image) throws DockerException {
|
|
|
- try {
|
|
|
- dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
- }
|
|
|
- /*dockerClient.pullImageCmd(image).exec(new ResultCallback<PullResponseItem>() {
|
|
|
- public void onStart(Closeable closeable) {
|
|
|
- System.out.println("开始下载!");
|
|
|
- }
|
|
|
-
|
|
|
- public void onNext(PullResponseItem object) {
|
|
|
- // 实时显示出下载信息
|
|
|
- System.out.println(object.getStatus());
|
|
|
- }
|
|
|
-
|
|
|
- public void onError(Throwable throwable) {
|
|
|
- throwable.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- public void onComplete() {
|
|
|
- System.out.println("下载完毕!");
|
|
|
- }
|
|
|
-
|
|
|
- public void close() throws IOException {
|
|
|
- System.out.println("下载完毕...");
|
|
|
- }
|
|
|
- }).onComplete();*/
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String getContainerIdByName(String containerName) {
|
|
|
- List<Container> list = dockerClient.listContainersCmd()
|
|
|
- .withShowAll(true)
|
|
|
- .withNameFilter(List.of(containerName))
|
|
|
- .exec();
|
|
|
-
|
|
|
- for (Container container : list) {
|
|
|
- if (Arrays.stream(container.getNames()).collect(Collectors.toSet()).contains("/"+containerName)) {
|
|
|
- return container.getId();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void stopAndDelete(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- if (containerId != null) {
|
|
|
- InspectContainerResponse containerInfo = inspectContainer(containerId);
|
|
|
- InspectContainerResponse.ContainerState state = containerInfo.getState();
|
|
|
- boolean isRunning = state.getRunning() != null ? state.getRunning() : false;
|
|
|
- if (isRunning) {
|
|
|
- dockerClient.stopContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
- dockerClient.removeContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String createAndRun(String containerName, Config config) throws DockerException {
|
|
|
- stopAndDelete(containerName);
|
|
|
-
|
|
|
- HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
- .withNetworkMode("host")
|
|
|
- .withRestartPolicy(RestartPolicy.onFailureRestart(3));
|
|
|
- String image = config.getImage();
|
|
|
- CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image)
|
|
|
- .withName(containerName)
|
|
|
- .withHostConfig(hostConfig);
|
|
|
-
|
|
|
- List<String> env = config.getEnv();
|
|
|
- if (env != null) {
|
|
|
- createContainerCmd.withEnv(env);
|
|
|
- }
|
|
|
-
|
|
|
- CreateContainerResponse response = createContainerCmd.exec();
|
|
|
- String containerId1 = response.getId();
|
|
|
- dockerClient.startContainerCmd(containerId1).exec();
|
|
|
- return containerId1;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse inspectContainer(String containerId) {
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void restart(String containerId) {
|
|
|
- dockerClient.restartContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void stop(String containerId) {
|
|
|
- dockerClient.stopContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void start(String containerId) {
|
|
|
- dockerClient.startContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-}
|