Search in sources :

Example 11 with DockerAccessException

use of org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException in project jkube by eclipse.

the class DockerAccessWithHcClient method saveImage.

@Override
public void saveImage(String image, String filename, ArchiveCompression compression) throws DockerAccessException {
    ImageName name = new ImageName(image);
    String url = urlBuilder.getImage(name);
    try {
        delegate.get(url, getImageResponseHandler(filename, compression), HTTP_OK);
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to save '%s' to '%s'", image, filename);
    }
}
Also used : ImageName(org.eclipse.jkube.kit.config.image.ImageName) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) IOException(java.io.IOException)

Example 12 with DockerAccessException

use of org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException in project jkube by eclipse.

the class DockerAccessWithHcClient method createVolume.

@Override
public String createVolume(VolumeCreateConfig containerConfig) throws DockerAccessException {
    String createJson = containerConfig.toJson();
    log.debug("Volume create config: %s", createJson);
    try {
        String url = urlBuilder.createVolume();
        String response = delegate.post(url, createJson, new ApacheHttpClientDelegate.BodyResponseHandler(), HTTP_CREATED);
        JsonObject json = JsonFactory.newJsonObject(response);
        logWarnings(json);
        return json.get("Name").getAsString();
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to create volume for [%s]", containerConfig.getName());
    }
}
Also used : DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Example 13 with DockerAccessException

use of org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException in project jkube by eclipse.

the class BuildJsonResponseHandler method process.

@Override
public void process(JsonObject json) throws DockerAccessException {
    if (json.has("error")) {
        String msg = json.get("error").getAsString();
        String detailMsg = "";
        if (json.has("errorDetail")) {
            JsonObject details = json.getAsJsonObject("errorDetail");
            detailMsg = details.get("message").getAsString();
        }
        throw new DockerAccessException("%s %s", json.get("error"), (msg.equals(detailMsg) || "".equals(detailMsg) ? "" : "(" + detailMsg + ")"));
    } else if (json.has("stream")) {
        String message = json.get("stream").getAsString();
        log.verbose("%s", message.trim());
    } else if (json.has("status")) {
        String status = json.get("status").getAsString().trim();
        String id = json.has("id") ? json.get("id").getAsString() : null;
        if (status.matches("^.*(Download|Pulling).*")) {
            log.info("  %s%s", id != null ? id + " " : "", status);
        }
    }
}
Also used : DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) JsonObject(com.google.gson.JsonObject)

Example 14 with DockerAccessException

use of org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException in project jkube by eclipse.

the class BuildService method buildImage.

/**
 * Build an image
 *
 * @param imageConfig the image configuration
 * @param params mojo params for the project
 * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
 * @param buildArgs maven build context
 * @throws DockerAccessException docker access exception
 * @throws IOException in case of any I/O exception
 */
protected void buildImage(ImageConfiguration imageConfig, JKubeConfiguration params, boolean noCache, Map<String, String> buildArgs) throws IOException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String oldImageId = null;
    CleanupMode cleanupMode = buildConfig.cleanupMode();
    if (cleanupMode.isRemove()) {
        oldImageId = queryService.getImageId(imageName);
    }
    long time = System.currentTimeMillis();
    if (buildConfig.getDockerArchive() != null) {
        docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params.getSourceDirectory(), params.getProject().getBaseDirectory() != null ? params.getProject().getBaseDirectory().toString() : null));
        log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
        return;
    }
    File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
    log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
    Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
    // auto is now supported by docker, consider switching?
    BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).cacheFrom(buildConfig.getCacheFrom()).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    if (newImageId == null) {
        throw new IllegalStateException("Failure in building image, unable to find image built with name " + imageName);
    }
    log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
    if (oldImageId != null && !oldImageId.equals(newImageId)) {
        try {
            docker.removeImage(oldImageId, true);
            log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
        } catch (DockerAccessException exp) {
            if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
                log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
            } else {
                throw exp;
            }
        }
    }
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) BuildOptions(org.eclipse.jkube.kit.build.service.docker.access.BuildOptions) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) CleanupMode(org.eclipse.jkube.kit.config.image.build.CleanupMode) File(java.io.File)

Example 15 with DockerAccessException

use of org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException in project jkube by eclipse.

the class DockerAccessWithHcClient method removeVolume.

@Override
public void removeVolume(String name) throws DockerAccessException {
    try {
        String url = urlBuilder.removeVolume(name);
        delegate.delete(url, HTTP_NO_CONTENT, HTTP_NOT_FOUND);
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to remove volume [%s]", name);
    }
}
Also used : DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) IOException(java.io.IOException)

Aggregations

DockerAccessException (org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException)28 IOException (java.io.IOException)21 JsonObject (com.google.gson.JsonObject)8 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 ImageName (org.eclipse.jkube.kit.config.image.ImageName)3 JsonArray (com.google.gson.JsonArray)2 File (java.io.File)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 Container (org.eclipse.jkube.kit.build.api.model.Container)2 Map (java.util.Map)1 ContainerDetails (org.eclipse.jkube.kit.build.api.model.ContainerDetails)1 ContainersListElement (org.eclipse.jkube.kit.build.api.model.ContainersListElement)1 Network (org.eclipse.jkube.kit.build.api.model.Network)1 NetworksListElement (org.eclipse.jkube.kit.build.api.model.NetworksListElement)1 BuildOptions (org.eclipse.jkube.kit.build.service.docker.access.BuildOptions)1 CreateImageOptions (org.eclipse.jkube.kit.build.service.docker.access.CreateImageOptions)1 ExitCodeChecker (org.eclipse.jkube.kit.build.service.docker.wait.ExitCodeChecker)1 HealthCheckChecker (org.eclipse.jkube.kit.build.service.docker.wait.HealthCheckChecker)1 LogWaitChecker (org.eclipse.jkube.kit.build.service.docker.wait.LogWaitChecker)1