|
|
@@ -1,279 +0,0 @@
|
|
|
-package cn.reghao.devops.common.docker;
|
|
|
-
|
|
|
-import cn.reghao.devops.common.docker.po.Config;
|
|
|
-import cn.reghao.jutil.jdk.exception.ExceptionUtil;
|
|
|
-import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
-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.*;
|
|
|
-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.time.Duration;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * 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(String.format("%s/.docker", System.getProperty("user.home")))
|
|
|
- //.withRegistryUsername("username")
|
|
|
- //.withRegistryPassword("password")
|
|
|
- //.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 Exception {
|
|
|
- File dockerfile = new File(compileHome + "/Dockerfile.tmp");
|
|
|
- try {
|
|
|
- textFile.write(dockerfile, dockerfileContent);
|
|
|
- String imageId = dockerClient.buildImageCmd()
|
|
|
- .withDockerfile(dockerfile)
|
|
|
- // repoTag 格式为 docker.reghao.cn/devops:12345678
|
|
|
- .withTags(Set.of(repoTag))
|
|
|
- .start()
|
|
|
- .awaitImageId();
|
|
|
- } catch (Exception e) {
|
|
|
- throw new Exception(ExceptionUtil.errorMsg(e));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void build(String repoTag, String compileHome) throws Exception {
|
|
|
- String dockerfilePath = compileHome + "/Dockerfile";
|
|
|
- dockerClient.buildImageCmd()
|
|
|
- .withDockerfile(new File(dockerfilePath))
|
|
|
- .withTags(Set.of(repoTag))
|
|
|
- .start()
|
|
|
- .awaitImageId();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void push(String image) throws Exception {
|
|
|
- try {
|
|
|
- dockerClient.pushImageCmd(image).exec(new PushImageResultCallback()).awaitCompletion();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new Exception(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 Exception {
|
|
|
- try {
|
|
|
- dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new Exception(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();*/
|
|
|
- }
|
|
|
-
|
|
|
- private String getContainerIdByName(String containerName) {
|
|
|
- List<Container> list = dockerClient.listContainersCmd()
|
|
|
- .withShowAll(true)
|
|
|
- .withNameFilter(List.of(containerName))
|
|
|
- .exec();
|
|
|
-
|
|
|
- for (Container container : list) {
|
|
|
- Set<String> set = new HashSet<>(Arrays.asList(container.getNames()));
|
|
|
- if (set.contains("/" + containerName)) {
|
|
|
- return container.getId();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void stopAndDelete(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- if (containerId != null) {
|
|
|
- InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- 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 InspectContainerResponse createAndRun(String containerName, Config containerConfig) throws Exception {
|
|
|
- stopAndDelete(containerName);
|
|
|
-
|
|
|
- String image = containerConfig.getImage();
|
|
|
- CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image)
|
|
|
- .withName(containerName);
|
|
|
-
|
|
|
- List<String> env = containerConfig.getEnv();
|
|
|
- if (env != null) {
|
|
|
- createContainerCmd.withEnv(env);
|
|
|
- }
|
|
|
-
|
|
|
- HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
- .withNetworkMode("host")
|
|
|
- .withRestartPolicy(RestartPolicy.unlessStoppedRestart());
|
|
|
- if (containerConfig.getVolumes() != null) {
|
|
|
- Map<String, String> map = containerConfig.getVolumes().getMap();
|
|
|
- List<Bind> list = new ArrayList<>();
|
|
|
- for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
- String key = entry.getKey();
|
|
|
- String value = entry.getValue();
|
|
|
- Volume volume2 = new Volume(value);
|
|
|
- Bind bind = new Bind(key, volume2);
|
|
|
- list.add(bind);
|
|
|
- }
|
|
|
- hostConfig.withBinds(list);
|
|
|
- }
|
|
|
-
|
|
|
- createContainerCmd.withHostConfig(hostConfig);
|
|
|
- CreateContainerResponse response = createContainerCmd.exec();
|
|
|
- String containerId = response.getId();
|
|
|
- dockerClient.startContainerCmd(containerId).exec();
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse start(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- dockerClient.startContainerCmd(containerId).exec();
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse stop(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- dockerClient.stopContainerCmd(containerId).exec();
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse restart(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- dockerClient.restartContainerCmd(containerId).exec();
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse inspectContainer(String containerName) {
|
|
|
- String containerId = getContainerIdByName(containerName);
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Image> images() {
|
|
|
- List<Image> images = dockerClient.listImagesCmd().exec();
|
|
|
-
|
|
|
- images.forEach(image -> {
|
|
|
- long created = image.getCreated();
|
|
|
- String[] repoTags = image.getRepoTags();
|
|
|
- if (repoTags.length == 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String[] repoTag = repoTags[0].split(":");
|
|
|
- String repo = repoTag[0];
|
|
|
- String tag = repoTag[1];
|
|
|
- System.out.printf("%s:%s -> %s\n", repo, tag, DateTimeConverter.format(created*1000));
|
|
|
- });
|
|
|
-
|
|
|
- return images;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Container> ps(boolean isAll) {
|
|
|
- return dockerClient.listContainersCmd()
|
|
|
- .withShowAll(isAll)
|
|
|
- .exec();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public InspectContainerResponse ps(String appId) {
|
|
|
- List<Container> list = dockerClient.listContainersCmd()
|
|
|
- .withNameFilter(List.of(appId))
|
|
|
- .exec();
|
|
|
-
|
|
|
- if (list.isEmpty()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Container container = list.get(0);
|
|
|
- String containerId = container.getId();
|
|
|
- return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
- }
|
|
|
-}
|