//package com.bocloud.ims.service.utils;
//
//import com.bocloud.common.model.BocloudResult;
//import com.bocloud.common.utils.ListTool;
//import com.github.dockerjava.api.DockerClient;
//import com.github.dockerjava.api.command.DockerCmdExecFactory;
//import com.github.dockerjava.api.command.TagImageCmd;
//import com.github.dockerjava.api.model.AuthConfig;
//import com.github.dockerjava.api.model.PullResponseItem;
//import com.github.dockerjava.api.model.PushResponseItem;
//import com.github.dockerjava.api.model.Version;
//import com.github.dockerjava.core.DefaultDockerClientConfig;
//import com.github.dockerjava.core.DockerClientBuilder;
//import com.github.dockerjava.core.DockerClientConfig;
//import com.github.dockerjava.core.command.PullImageResultCallback;
//import com.github.dockerjava.core.command.PushImageResultCallback;
//import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStream;
//import java.util.Arrays;
//import java.util.List;
//
//public class DockerUtils {
//
//	private static Logger logger = LoggerFactory.getLogger(DockerUtils.class);
//
//	private String dockerHost;
//	private DockerClient dockerClient;
//
//	public DockerUtils(String dockerHost) {
//		super();
//		this.dockerHost = dockerHost;
//		init();
//	}
//
//	public DockerUtils() {
//		super();
//	}
//
//	@SuppressWarnings("deprecation")
//	public BocloudResult pushImageCmd(String name, String version, AuthConfig authConfig) {
//		try {
//			PushImageResultCallback pushImageResultCallback = new PushImageResultCallback() {
//				@Override
//				public void onNext(PushResponseItem item) {
//					super.onNext(item);
//				}
//			};
//			dockerClient.pushImageCmd(name).withAuthConfig(authConfig).withName(name).withTag(version)
//					.exec(pushImageResultCallback).awaitSuccess();
//			logger.info("push success !! ");
//			return new BocloudResult(true, "push success !! ");
//		} catch (Exception e) {
//			logger.error("push image fail : " + e.getMessage());
//			return new BocloudResult(false, "push fail : " + e.getMessage());
//		}
//	}
//
//	public com.github.dockerjava.api.model.Image getImagesCmd(String imageName) {
//		com.github.dockerjava.api.model.Image oldImage = null;
//		try {
//			List<com.github.dockerjava.api.model.Image> exec = dockerClient.listImagesCmd().exec();
//			for (com.github.dockerjava.api.model.Image image : exec) {
//				String[] repoTags = image.getRepoTags();
//				if (null != repoTags && repoTags.length > 0) {
//					List<String> asList = Arrays.asList(repoTags);
//					if (!ListTool.isEmpty(asList) && asList.contains(imageName)) {
//						oldImage = image;
//						break;
//					}
//				}
//			}
//			logger.info("query success !!!!! ");
//		} catch (Exception e) {
//			logger.error("query image fail : " + e.getMessage());
//		}
//		return oldImage;
//	}
//
//	public Version versionCmd() {
//		Version version = null;
//		try {
//			version = dockerClient.versionCmd().exec();
//			logger.info("query version success !!!!! ");
//		} catch (Exception e) {
//			logger.error("query version fail : " + e.getMessage());
//		}
//		return version;
//	}
//
//	@SuppressWarnings("deprecation")
//	public BocloudResult pullImageCmd(String imageName) {
//		try {
//			PullImageResultCallback resultCallback = new PullImageResultCallback() {
//				@Override
//				public void onNext(PullResponseItem item) {
//					super.onNext(item);
//				}
//			};
//			dockerClient.pullImageCmd(imageName).exec(resultCallback).awaitSuccess();
//			logger.info("pull success !!!!! ");
//			return new BocloudResult(true, "remove success!");
//		} catch (Exception e) {
//			logger.error("pull image fail : " + e.getMessage());
//			return new BocloudResult(false, "pull image fail : " + e.getMessage());
//		}
//	}
//
//	public InputStream saveImageCmd(String imageName) {
//		try {
//			return dockerClient.saveImageCmd(imageName).exec();
//		} catch (Exception e) {
//			logger.error("save image fail : " + e.getMessage());
//			return null;
//		}
//	}
//
//	public BocloudResult removeImageCmd(String name) {
//		try {
//			dockerClient.removeImageCmd(name).exec();
//			logger.info("remove success !!!!! ");
//			return new BocloudResult(true, "remove success!");
//		} catch (Exception e) {
//			logger.error("remove image fail : " + e.getMessage());
//			return new BocloudResult(false, "remove image fail : " + e.getMessage());
//		}
//	}
//
//	public BocloudResult tagImageCmd(String imageId, String imageNameWithRepository, String tag) {
//		try {
//			TagImageCmd tagImageCmd = dockerClient.tagImageCmd(imageId, imageNameWithRepository, tag);
//			tagImageCmd.exec();
//			logger.info("tag success !!!!! ");
//			return new BocloudResult(true, "tag success!");
//		} catch (Exception e) {
//			logger.error("tag image fail : " + e.getMessage());
//			return new BocloudResult(false, "tag fail : " + e.getMessage());
//		}
//	}
//
//	public BocloudResult loadImageCmd(File file) {
//		try {
//			FileInputStream inputStream = new FileInputStream(file);
//			dockerClient.loadImageCmd(inputStream).exec();
//			logger.info("load success !!!!! ");
//			return new BocloudResult(true, "load image success!");
//		} catch (Exception e) {
//			logger.error("load image fail : " + e.getMessage());
//			return new BocloudResult(false, "load image fail : " + e.getMessage());
//		}
//	}
//
//	public void init() {
//		DockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
//				.withDockerHost(this.dockerHost).build();
//		@SuppressWarnings("resource")
//		DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory().withReadTimeout(60000)
//				.withConnectTimeout(1000).withMaxTotalConnections(100).withMaxPerRouteConnections(100);
//
//		dockerClient = DockerClientBuilder.getInstance(dockerClientConfig)
//				.withDockerCmdExecFactory(dockerCmdExecFactory).build();
//	}
//
//	public String getDockerHost() {
//		return dockerHost;
//	}
//
//	public void setDockerHost(String dockerHost) {
//		this.dockerHost = dockerHost;
//	}
//
//	public DockerClient getDockerClient() {
//		return dockerClient;
//	}
//
//	public void setDockerClient(DockerClient dockerClient) {
//		this.dockerClient = dockerClient;
//	}
//
//}