All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.github.dockerjava.core.DockerClientImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.github.dockerjava.core;

import com.fitbur.github.dockerjava.api.DockerClient;
import com.fitbur.github.dockerjava.api.com.fitburmand.AttachContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.AuthCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.BuildImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CommitCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ContainerDiffCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CopyFileFromContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CreateContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CreateImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.DockerCmdExecFactory;
import com.fitbur.github.dockerjava.api.com.fitburmand.EventsCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ExecCreateCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ExecStartCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InfoCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectExecCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.KillContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ListContainersCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ListImagesCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.LogContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PauseContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PingCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PullImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PushImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RemoveContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RemoveImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RestartContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.SaveImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.SearchImagesCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StartContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StatsCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StopContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.TagImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.TopContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.UnpauseContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.VersionCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.WaitContainerCmd;
import com.fitbur.github.dockerjava.api.model.AuthConfig;
import com.fitbur.github.dockerjava.api.model.Identifier;
import com.fitbur.github.dockerjava.core.com.fitburmand.AttachContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.AuthCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.BuildImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.CommitCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.ContainerDiffCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.CopyFileFromContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.CreateContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.CreateImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.EventsCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.ExecCreateCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.ExecStartCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.InfoCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.InspectContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.InspectExecCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.InspectImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.KillContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.ListContainersCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.ListImagesCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.LogContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.PauseContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.PingCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.PullImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.PushImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.RemoveContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.RemoveImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.RestartContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.SaveImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.SearchImagesCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.StartContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.StatsCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.StopContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.TagImageCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.TopContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.UnpauseContainerCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.VersionCmdImpl;
import com.fitbur.github.dockerjava.core.com.fitburmand.WaitContainerCmdImpl;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static com.fitbur.google.com.fitburmon.base.Preconditions.checkNotNull;

/**
 * @author Konstantin Pelykh ([email protected])
 *
 * @see "https://github.com.fitbur/docker/docker/blob/master/api/client/com.fitburmands.go"
 */
public class DockerClientImpl implements Closeable, DockerClient {

    private final DockerClientConfig dockerClientConfig;

    private DockerCmdExecFactory dockerCmdExecFactory;

    private DockerClientImpl() {
        this(DockerClientConfig.createDefaultConfigBuilder().build());
    }

    private DockerClientImpl(String serverUrl) {
        this(configWithServerUrl(serverUrl));
    }

    private DockerClientImpl(DockerClientConfig dockerClientConfig) {
        checkNotNull(dockerClientConfig, "config was not specified");
        this.dockerClientConfig = dockerClientConfig;
    }

    private static DockerClientConfig configWithServerUrl(String serverUrl) {
        return DockerClientConfig.createDefaultConfigBuilder().withUri(serverUrl).build();
    }

    public static DockerClientImpl getInstance() {
        return new DockerClientImpl();
    }

    public static DockerClientImpl getInstance(DockerClientConfig dockerClientConfig) {
        return new DockerClientImpl(dockerClientConfig);
    }

    public static DockerClientImpl getInstance(String serverUrl) {
        return new DockerClientImpl(serverUrl);
    }

    public DockerClientImpl withDockerCmdExecFactory(DockerCmdExecFactory dockerCmdExecFactory) {
        checkNotNull(dockerCmdExecFactory, "dockerCmdExecFactory was not specified");
        this.dockerCmdExecFactory = dockerCmdExecFactory;
        this.dockerCmdExecFactory.init(dockerClientConfig);
        return this;
    }

    private DockerCmdExecFactory getDockerCmdExecFactory() {
        checkNotNull(dockerCmdExecFactory, "dockerCmdExecFactory was not specified");
        return dockerCmdExecFactory;
    }

    @Override
    public AuthConfig authConfig() {
        checkNotNull(dockerClientConfig.getUsername(), "Configured username is null.");
        checkNotNull(dockerClientConfig.getServerAddress(), "Configured serverAddress is null.");

        AuthConfig authConfig = new AuthConfig();
        authConfig.setUsername(dockerClientConfig.getUsername());
        authConfig.setPassword(dockerClientConfig.getPassword());
        authConfig.setEmail(dockerClientConfig.getEmail());
        authConfig.setServerAddress(dockerClientConfig.getServerAddress());

        return authConfig;
    }

    /**
     * * MISC API *
     */

    /**
     * Authenticate with the server, useful for checking authentication.
     */
    @Override
    public AuthCmd authCmd() {
        return new AuthCmdImpl(getDockerCmdExecFactory().createAuthCmdExec(), authConfig());
    }

    @Override
    public InfoCmd infoCmd() {
        return new InfoCmdImpl(getDockerCmdExecFactory().createInfoCmdExec());
    }

    @Override
    public PingCmd pingCmd() {
        return new PingCmdImpl(getDockerCmdExecFactory().createPingCmdExec());
    }

    @Override
    public VersionCmd versionCmd() {
        return new VersionCmdImpl(getDockerCmdExecFactory().createVersionCmdExec());
    }

    /**
     * * IMAGE API *
     */
    @Override
    public PullImageCmd pullImageCmd(String repository) {
        return new PullImageCmdImpl(getDockerCmdExecFactory().createPullImageCmdExec(),
                dockerClientConfig.effectiveAuthConfig(repository), repository);
    }

    @Override
    public PushImageCmd pushImageCmd(String name) {
        PushImageCmd cmd = new PushImageCmdImpl(getDockerCmdExecFactory().createPushImageCmdExec(), name);

        AuthConfig cfg = dockerClientConfig.effectiveAuthConfig(name);
        if (cfg != null)
            cmd.withAuthConfig(cfg);
        return cmd;
    }

    @Override
    public PushImageCmd pushImageCmd(Identifier identifier) {
        PushImageCmd cmd = pushImageCmd(identifier.repository.name);
        if (identifier.tag.isPresent())
            cmd.withTag(identifier.tag.get());

        AuthConfig cfg = dockerClientConfig.effectiveAuthConfig(identifier.repository.name);
        if (cfg != null)
            cmd.withAuthConfig(cfg);

        return cmd;
    }

    @Override
    public SaveImageCmd saveImageCmd(String name) {
        return new SaveImageCmdImpl(getDockerCmdExecFactory().createSaveImageCmdExec(), name);
    }

    @Override
    public CreateImageCmd createImageCmd(String repository, InputStream imageStream) {
        return new CreateImageCmdImpl(getDockerCmdExecFactory().createCreateImageCmdExec(), repository, imageStream);
    }

    @Override
    public SearchImagesCmd searchImagesCmd(String term) {
        return new SearchImagesCmdImpl(getDockerCmdExecFactory().createSearchImagesCmdExec(), term);
    }

    @Override
    public RemoveImageCmd removeImageCmd(String imageId) {
        return new RemoveImageCmdImpl(getDockerCmdExecFactory().createRemoveImageCmdExec(), imageId);
    }

    @Override
    public ListImagesCmd listImagesCmd() {
        return new ListImagesCmdImpl(getDockerCmdExecFactory().createListImagesCmdExec());
    }

    @Override
    public InspectImageCmd inspectImageCmd(String imageId) {
        return new InspectImageCmdImpl(getDockerCmdExecFactory().createInspectImageCmdExec(), imageId);
    }

    /**
     * * CONTAINER API *
     */

    @Override
    public ListContainersCmd listContainersCmd() {
        return new ListContainersCmdImpl(getDockerCmdExecFactory().createListContainersCmdExec());
    }

    @Override
    public CreateContainerCmd createContainerCmd(String image) {
        return new CreateContainerCmdImpl(getDockerCmdExecFactory().createCreateContainerCmdExec(), image);
    }

    @Override
    public StartContainerCmd startContainerCmd(String containerId) {
        return new StartContainerCmdImpl(getDockerCmdExecFactory().createStartContainerCmdExec(), containerId);
    }

    @Override
    public InspectContainerCmd inspectContainerCmd(String containerId) {
        return new InspectContainerCmdImpl(getDockerCmdExecFactory().createInspectContainerCmdExec(), containerId);
    }

    @Override
    public ExecCreateCmd execCreateCmd(String containerId) {
        return new ExecCreateCmdImpl(getDockerCmdExecFactory().createExecCmdExec(), containerId);
    }

    @Override
    public RemoveContainerCmd removeContainerCmd(String containerId) {
        return new RemoveContainerCmdImpl(getDockerCmdExecFactory().createRemoveContainerCmdExec(), containerId);
    }

    @Override
    public WaitContainerCmd waitContainerCmd(String containerId) {
        return new WaitContainerCmdImpl(getDockerCmdExecFactory().createWaitContainerCmdExec(), containerId);
    }

    @Override
    public AttachContainerCmd attachContainerCmd(String containerId) {
        return new AttachContainerCmdImpl(getDockerCmdExecFactory().createAttachContainerCmdExec(), containerId);
    }

    @Override
    public ExecStartCmd execStartCmd(String containerId) {
        return new ExecStartCmdImpl(getDockerCmdExecFactory().createExecStartCmdExec(), containerId);
    }

    @Override
    public InspectExecCmd inspectExecCmd(String execId) {
        return new InspectExecCmdImpl(getDockerCmdExecFactory().createInspectExecCmdExec(), execId);
    }

    @Override
    public LogContainerCmd logContainerCmd(String containerId) {
        return new LogContainerCmdImpl(getDockerCmdExecFactory().createLogContainerCmdExec(), containerId);
    }

    @Override
    public CopyFileFromContainerCmd copyFileFromContainerCmd(String containerId, String resource) {
        return new CopyFileFromContainerCmdImpl(getDockerCmdExecFactory().createCopyFileFromContainerCmdExec(),
                containerId, resource);
    }

    @Override
    public ContainerDiffCmd containerDiffCmd(String containerId) {
        return new ContainerDiffCmdImpl(getDockerCmdExecFactory().createContainerDiffCmdExec(), containerId);
    }

    @Override
    public StopContainerCmd stopContainerCmd(String containerId) {
        return new StopContainerCmdImpl(getDockerCmdExecFactory().createStopContainerCmdExec(), containerId);
    }

    @Override
    public KillContainerCmd killContainerCmd(String containerId) {
        return new KillContainerCmdImpl(getDockerCmdExecFactory().createKillContainerCmdExec(), containerId);
    }

    @Override
    public RestartContainerCmd restartContainerCmd(String containerId) {
        return new RestartContainerCmdImpl(getDockerCmdExecFactory().createRestartContainerCmdExec(), containerId);
    }

    @Override
    public CommitCmd com.fitburmitCmd(String containerId) {
        return new CommitCmdImpl(getDockerCmdExecFactory().createCommitCmdExec(), containerId);
    }

    @Override
    public BuildImageCmd buildImageCmd() {
        return new BuildImageCmdImpl(getDockerCmdExecFactory().createBuildImageCmdExec());
    }

    @Override
    public BuildImageCmd buildImageCmd(File dockerFileOrFolder) {
        return new BuildImageCmdImpl(getDockerCmdExecFactory().createBuildImageCmdExec(), dockerFileOrFolder);
    }

    @Override
    public BuildImageCmd buildImageCmd(InputStream tarInputStream) {
        return new BuildImageCmdImpl(getDockerCmdExecFactory().createBuildImageCmdExec(), tarInputStream);
    }

    @Override
    public TopContainerCmd topContainerCmd(String containerId) {
        return new TopContainerCmdImpl(getDockerCmdExecFactory().createTopContainerCmdExec(), containerId);
    }

    @Override
    public TagImageCmd tagImageCmd(String imageId, String repository, String tag) {
        return new TagImageCmdImpl(getDockerCmdExecFactory().createTagImageCmdExec(), imageId, repository, tag);
    }

    @Override
    public PauseContainerCmd pauseContainerCmd(String containerId) {
        return new PauseContainerCmdImpl(getDockerCmdExecFactory().createPauseContainerCmdExec(), containerId);
    }

    @Override
    public UnpauseContainerCmd unpauseContainerCmd(String containerId) {
        return new UnpauseContainerCmdImpl(getDockerCmdExecFactory().createUnpauseContainerCmdExec(), containerId);
    }

    @Override
    public EventsCmd eventsCmd() {
        return new EventsCmdImpl(getDockerCmdExecFactory().createEventsCmdExec());
    }

    @Override
    public StatsCmd statsCmd() {
        return new StatsCmdImpl(getDockerCmdExecFactory().createStatsCmdExec());
    }

    @Override
    public void close() throws IOException {
        getDockerCmdExecFactory().close();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy