Search in sources :

Example 1 with DockerClient

use of io.fabric8.docker.client.DockerClient in project kubernetes by ballerinax.

the class DockerHandler method pushImage.

/**
 * Push docker image.
 *
 * @param dockerModel DockerModel
 * @throws InterruptedException When error with docker build process
 * @throws IOException          When error with docker build process
 */
public void pushImage(DockerModel dockerModel) throws InterruptedException, IOException, KubernetesPluginException {
    AuthConfig authConfig = new AuthConfigBuilder().withUsername(dockerModel.getUsername()).withPassword(dockerModel.getPassword()).build();
    Config config = new ConfigBuilder().withDockerUrl(dockerModel.getDockerHost()).addToAuthConfigs(RegistryUtils.extractRegistry(dockerModel.getName()), authConfig).build();
    DockerClient client = new DefaultDockerClient(config);
    final DockerError dockerError = new DockerError();
    OutputHandle handle = client.image().withName(dockerModel.getName()).push().usingListener(new EventListener() {

        @Override
        public void onSuccess(String message) {
            pushDone.countDown();
        }

        @Override
        public void onError(String message) {
            pushDone.countDown();
            dockerError.setErrorMsg("error pushing docker image: " + message);
        }

        @Override
        public void onError(Throwable t) {
            pushDone.countDown();
            dockerError.setErrorMsg("error pushing docker image: " + t.getMessage());
        }

        @Override
        public void onEvent(String event) {
            printDebug(event);
        }
    }).toRegistry();
    pushDone.await();
    handle.close();
    client.close();
    handleError(dockerError);
}
Also used : AuthConfigBuilder(io.fabric8.docker.api.model.AuthConfigBuilder) DefaultDockerClient(io.fabric8.docker.client.DefaultDockerClient) DockerClient(io.fabric8.docker.client.DockerClient) Config(io.fabric8.docker.client.Config) AuthConfig(io.fabric8.docker.api.model.AuthConfig) DefaultDockerClient(io.fabric8.docker.client.DefaultDockerClient) AuthConfigBuilder(io.fabric8.docker.api.model.AuthConfigBuilder) ConfigBuilder(io.fabric8.docker.client.ConfigBuilder) OutputHandle(io.fabric8.docker.dsl.OutputHandle) AuthConfig(io.fabric8.docker.api.model.AuthConfig) EventListener(io.fabric8.docker.dsl.EventListener)

Example 2 with DockerClient

use of io.fabric8.docker.client.DockerClient in project ballerina by ballerina-lang.

the class TestUtils method deleteDockerImage.

public static void deleteDockerImage(String imageName) {
    if (!imageName.contains(":")) {
        imageName += ":" + Constants.IMAGE_VERSION_LATEST;
    }
    DockerClient client = new io.fabric8.docker.client.DefaultDockerClient();
    List<ImageDelete> imageDeleteList = client.image().withName(imageName).delete().force().andPrune(false);
    for (ImageDelete imageDelete : imageDeleteList) {
        imageDelete.getDeleted();
        imageDelete.getUntagged();
    }
}
Also used : ImageDelete(io.fabric8.docker.api.model.ImageDelete) DockerClient(io.fabric8.docker.client.DockerClient)

Example 3 with DockerClient

use of io.fabric8.docker.client.DockerClient in project ballerina by ballerina-lang.

the class DefaultBallerinaDockerClient method buildImage.

/*
    Execute a Docker image build using Fabric8 DSL.
     */
private void buildImage(String dockerEnv, String imageName, Path tmpDir, String buildArgs) throws InterruptedException, IOException {
    DockerClient client = getDockerClient(dockerEnv);
    OutputHandle buildHandle = client.image().build().withRepositoryName(imageName).withNoCache().alwaysRemovingIntermediate().withBuildArgs(buildArgs).usingListener(new DockerBuilderEventListener()).fromFolder(tmpDir.toString());
    buildDone.await();
    buildHandle.close();
    client.close();
}
Also used : DockerClient(io.fabric8.docker.client.DockerClient) BallerinaDockerClient(org.ballerinalang.containers.docker.BallerinaDockerClient) OutputHandle(io.fabric8.docker.dsl.OutputHandle)

Example 4 with DockerClient

use of io.fabric8.docker.client.DockerClient in project kubernetes by ballerinax.

the class DockerHandler method buildImage.

/**
 * Create docker image.
 *
 * @param dockerModel dockerModel object
 * @param dockerDir   dockerfile directory
 * @throws InterruptedException When error with docker build process
 * @throws IOException          When error with docker build process
 */
public void buildImage(DockerModel dockerModel, String dockerDir) throws InterruptedException, IOException, KubernetesPluginException {
    Config dockerClientConfig = new ConfigBuilder().withDockerUrl(dockerModel.getDockerHost()).build();
    DockerClient client = new io.fabric8.docker.client.DefaultDockerClient(dockerClientConfig);
    final DockerError dockerError = new DockerError();
    OutputHandle buildHandle = client.image().build().withRepositoryName(dockerModel.getName()).withNoCache().alwaysRemovingIntermediate().usingListener(new EventListener() {

        @Override
        public void onSuccess(String message) {
            buildDone.countDown();
        }

        @Override
        public void onError(String message) {
            dockerError.setErrorMsg("error building docker image: " + message);
            buildDone.countDown();
        }

        @Override
        public void onError(Throwable t) {
            dockerError.setErrorMsg("error building docker image: " + t.getMessage());
            buildDone.countDown();
        }

        @Override
        public void onEvent(String event) {
            printDebug(event);
        }
    }).fromFolder(dockerDir);
    buildDone.await();
    buildHandle.close();
    client.close();
    handleError(dockerError);
}
Also used : DefaultDockerClient(io.fabric8.docker.client.DefaultDockerClient) DockerClient(io.fabric8.docker.client.DockerClient) Config(io.fabric8.docker.client.Config) AuthConfig(io.fabric8.docker.api.model.AuthConfig) DefaultDockerClient(io.fabric8.docker.client.DefaultDockerClient) AuthConfigBuilder(io.fabric8.docker.api.model.AuthConfigBuilder) ConfigBuilder(io.fabric8.docker.client.ConfigBuilder) OutputHandle(io.fabric8.docker.dsl.OutputHandle) EventListener(io.fabric8.docker.dsl.EventListener)

Example 5 with DockerClient

use of io.fabric8.docker.client.DockerClient in project ballerina by ballerina-lang.

the class DefaultBallerinaDockerClient method getImage.

/**
 * {@inheritDoc}
 */
@Override
public String getImage(String imageName, String dockerEnv) {
    DockerClient client = getDockerClient(dockerEnv);
    List<Image> images = client.image().list().filter(imageName).endImages();
    // No images found?
    if (images == null || images.size() < 1) {
        return null;
    }
    for (Image image : images) {
        // Invalid tags found?
        if (image.getRepoTags() == null) {
            continue;
        }
        String currentImageName = image.getRepoTags().get(0);
        if (currentImageName.equals(imageName + ":" + Constants.IMAGE_VERSION_LATEST) || currentImageName.equals(imageName)) {
            return currentImageName;
        }
    }
    return null;
}
Also used : DockerClient(io.fabric8.docker.client.DockerClient) BallerinaDockerClient(org.ballerinalang.containers.docker.BallerinaDockerClient) Image(io.fabric8.docker.api.model.Image)

Aggregations

DockerClient (io.fabric8.docker.client.DockerClient)6 Config (io.fabric8.docker.client.Config)3 ConfigBuilder (io.fabric8.docker.client.ConfigBuilder)3 OutputHandle (io.fabric8.docker.dsl.OutputHandle)3 BallerinaDockerClient (org.ballerinalang.containers.docker.BallerinaDockerClient)3 AuthConfig (io.fabric8.docker.api.model.AuthConfig)2 AuthConfigBuilder (io.fabric8.docker.api.model.AuthConfigBuilder)2 DefaultDockerClient (io.fabric8.docker.client.DefaultDockerClient)2 EventListener (io.fabric8.docker.dsl.EventListener)2 Image (io.fabric8.docker.api.model.Image)1 ImageDelete (io.fabric8.docker.api.model.ImageDelete)1