Search in sources :

Example 1 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class LogOutputSpecFactory method extractLogConfiguration.

private LogConfiguration extractLogConfiguration(ImageConfiguration imageConfiguration) {
    RunImageConfiguration runConfig = imageConfiguration.getRunConfiguration();
    LogConfiguration logConfig = null;
    if (runConfig != null) {
        logConfig = runConfig.getLogConfiguration();
    }
    if (logConfig == null) {
        logConfig = LogConfiguration.DEFAULT;
    }
    return logConfig;
}
Also used : LogConfiguration(io.fabric8.maven.docker.config.LogConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration)

Example 2 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

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
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs) throws DockerAccessException, MojoExecutionException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildImageConfiguration 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));
        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).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    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 : BuildOptions(io.fabric8.maven.docker.access.BuildOptions) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) CleanupMode(io.fabric8.maven.docker.config.CleanupMode) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 3 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class RegistryService method pushImages.

/**
 * Push a set of images to a registry
 *
 * @param imageConfigs images to push (but only if they have a build configuration)
 * @param retries how often to retry
 * @param registryConfig a global registry configuration
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
public void pushImages(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig) throws DockerAccessException, MojoExecutionException {
    for (ImageConfiguration imageConfig : imageConfigs) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        String name = imageConfig.getName();
        if (buildConfig != null) {
            String configuredRegistry = EnvUtil.fistRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
            AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
            long start = System.currentTimeMillis();
            docker.pushImage(name, authConfig, configuredRegistry, retries);
            log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
            for (String tag : imageConfig.getBuildConfiguration().getTags()) {
                if (tag != null) {
                    docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
                }
            }
        }
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 4 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class RunService method execInContainer.

/**
 * Create and start a Exec container with the given image configuration.
 * @param containerId container id to run exec command against
 * @param command command to execute
 * @param imageConfiguration configuration of the container's image
 * @return the exec container id
 *
 * @throws DockerAccessException if access to the docker backend fails
 */
public String execInContainer(String containerId, String command, ImageConfiguration imageConfiguration) throws DockerAccessException, ExecException {
    Arguments arguments = new Arguments();
    arguments.setExec(Arrays.asList(EnvUtil.splitOnSpaceWithEscape(command)));
    String execContainerId = docker.createExecContainer(containerId, arguments);
    docker.startExecContainer(execContainerId, logConfig.createSpec(containerId, imageConfiguration));
    ExecDetails execContainer = docker.getExecContainer(execContainerId);
    Integer exitCode = execContainer.getExitCode();
    if (exitCode != null && exitCode != 0) {
        ContainerDetails container = docker.getContainer(containerId);
        throw new ExecException(execContainer, container);
    }
    return execContainerId;
}
Also used : ExecDetails(io.fabric8.maven.docker.model.ExecDetails) ExecException(io.fabric8.maven.docker.access.ExecException) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails)

Example 5 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class WatchService method defaultContainerRestartTask.

private Task<ImageWatcher> defaultContainerRestartTask() {
    return new Task<ImageWatcher>() {

        @Override
        public void execute(ImageWatcher watcher) throws Exception {
            // Stop old one
            ImageConfiguration imageConfig = watcher.getImageConfiguration();
            PortMapping mappedPorts = runService.createPortMapping(imageConfig.getRunConfiguration(), watcher.getWatchContext().getMojoParameters().getProject().getProperties());
            String id = watcher.getContainerId();
            String optionalPreStop = getPreStopCommand(imageConfig);
            if (optionalPreStop != null) {
                runService.execInContainer(id, optionalPreStop, watcher.getImageConfiguration());
            }
            runService.stopPreviouslyStartedContainer(id, false, false);
            // Start new one
            watcher.setContainerId(runService.createAndStartContainer(imageConfig, mappedPorts, watcher.getWatchContext().getPomLabel(), watcher.getWatchContext().getMojoParameters().getProject().getProperties(), watcher.getWatchContext().getMojoParameters().getProject().getBasedir()));
        }
    };
}
Also used : Task(io.fabric8.maven.docker.util.Task) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) WatchImageConfiguration(io.fabric8.maven.docker.config.WatchImageConfiguration) PortMapping(io.fabric8.maven.docker.access.PortMapping)

Aggregations

BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)50 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)38 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)24 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)14 Test (org.junit.Test)13 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)10 File (java.io.File)9 IOException (java.io.IOException)9 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)8 ArrayList (java.util.ArrayList)8 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 MojoFailureException (org.apache.maven.plugin.MojoFailureException)7 Before (org.junit.Before)7 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)6 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)6 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)5 Expectations (mockit.Expectations)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 ExecException (io.fabric8.maven.docker.access.ExecException)4 WatchImageConfiguration (io.fabric8.maven.docker.config.WatchImageConfiguration)4