Search in sources :

Example 26 with DockerAccessException

use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.

the class LogsMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
    QueryService queryService = hub.getQueryService();
    LogDispatcher logDispatcher = getLogDispatcher(hub);
    for (ImageConfiguration image : getResolvedImages()) {
        String imageName = image.getName();
        if (logAll) {
            for (Container container : queryService.getContainersForImage(imageName)) {
                doLogging(logDispatcher, image, container.getId());
            }
        } else {
            Container container = queryService.getLatestContainerForImage(imageName);
            if (container != null) {
                doLogging(logDispatcher, image, container.getId());
            }
        }
    }
    if (follow) {
        // Block forever ....
        waitForEver();
    }
}
Also used : Container(io.fabric8.maven.docker.model.Container) QueryService(io.fabric8.maven.docker.service.QueryService) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher)

Example 27 with DockerAccessException

use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.

the class SourceMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
    MojoParameters params = createMojoParameters();
    List<ImageConfiguration> imageConfigs = new ArrayList<>();
    for (ImageConfiguration imageConfig : getResolvedImages()) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        if (buildConfig != null) {
            if (buildConfig.skip()) {
                log.info("%s: Skipped creating source", imageConfig.getDescription());
            } else {
                imageConfigs.add(imageConfig);
            }
        }
    }
    if (sourceMode == BuildImageSelectMode.first && imageConfigs.size() > 0) {
        ImageConfiguration imageConfig = imageConfigs.get(0);
        File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
        projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(null), dockerTar);
    } else {
        for (ImageConfiguration imageConfig : imageConfigs) {
            File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
            String alias = imageConfig.getAlias();
            if (alias == null) {
                throw new IllegalArgumentException("Image " + imageConfig.getDescription() + " must have an 'alias' configured to be " + "used as a classifier for attaching a docker build tar as source to the maven build");
            }
            projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(alias), dockerTar);
        }
    }
}
Also used : MojoParameters(io.fabric8.maven.docker.util.MojoParameters) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ArrayList(java.util.ArrayList) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 28 with DockerAccessException

use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.

the class StartMojo method exposeContainerProps.

// Expose ports as project properties
private void exposeContainerProps(QueryService queryService, StartedContainer startedContainer) throws DockerAccessException {
    String propKey = getExposedPropertyKeyPart(startedContainer.imageConfig);
    if (StringUtils.isNotEmpty(exposeContainerProps) && StringUtils.isNotEmpty(propKey)) {
        Container container = queryService.getMandatoryContainer(startedContainer.containerId);
        Properties props = project.getProperties();
        String prefix = addDot(exposeContainerProps) + addDot(propKey);
        props.put(prefix + "id", startedContainer.containerId);
        String ip = container.getIPAddress();
        if (StringUtils.isNotEmpty(ip)) {
            props.put(prefix + "ip", ip);
        }
        Map<String, String> nets = container.getCustomNetworkIpAddresses();
        if (nets != null) {
            for (Map.Entry<String, String> entry : nets.entrySet()) {
                props.put(prefix + addDot("net") + addDot(entry.getKey()) + "ip", entry.getValue());
            }
        }
    }
}
Also used : Container(io.fabric8.maven.docker.model.Container)

Example 29 with DockerAccessException

use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.

the class StartMojo method executeInternal.

/**
 * {@inheritDoc}
 */
@Override
public synchronized void executeInternal(final ServiceHub hub) throws DockerAccessException, ExecException, MojoExecutionException {
    if (skipRun) {
        return;
    }
    getPluginContext().put(CONTEXT_KEY_START_CALLED, true);
    this.follow = followLogs();
    QueryService queryService = hub.getQueryService();
    final RunService runService = hub.getRunService();
    PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper = new PortMapping.PropertyWriteHelper(portPropertyFile);
    boolean success = false;
    final ExecutorService executorService = getExecutorService();
    final ExecutorCompletionService<StartedContainer> containerStartupService = new ExecutorCompletionService<>(executorService);
    try {
        // All aliases which are provided in the image configuration:
        final Set<String> imageAliases = new HashSet<>();
        // Remember all aliases which has been started
        final Set<String> startedContainerAliases = new HashSet<>();
        // All images to to start
        Queue<ImageConfiguration> imagesWaitingToStart = prepareStart(hub, queryService, runService, imageAliases);
        // Queue of images to start as containers
        final Queue<ImageConfiguration> imagesStarting = new ArrayDeque<>();
        // of the containers so that partial or aborted starts will behave the same as fully-successful ones.
        if (follow) {
            runService.addShutdownHookForStoppingContainers(keepContainer, removeVolumes, autoCreateCustomNetworks);
        }
        // Loop until every image has been started and the start of all images has been completed
        while (!hasBeenAllImagesStarted(imagesWaitingToStart, imagesStarting)) {
            final List<ImageConfiguration> imagesReadyToStart = getImagesWhoseDependenciesHasStarted(imagesWaitingToStart, startedContainerAliases, imageAliases);
            for (final ImageConfiguration image : imagesReadyToStart) {
                startImage(image, hub, containerStartupService, portMappingPropertyWriteHelper);
                // Move from waiting to starting status
                imagesStarting.add(image);
                imagesWaitingToStart.remove(image);
            }
            // Wait for the next container to finish startup
            final Future<StartedContainer> startedContainerFuture = containerStartupService.take();
            try {
                final StartedContainer startedContainer = startedContainerFuture.get();
                final ImageConfiguration imageConfig = startedContainer.imageConfig;
                updateAliasesSet(startedContainerAliases, imageConfig.getAlias());
                exposeContainerProps(hub.getQueryService(), startedContainer);
                // All done with this image
                imagesStarting.remove(imageConfig);
            } catch (ExecutionException e) {
                rethrowCause(e);
            }
        }
        portMappingPropertyWriteHelper.write();
        if (follow) {
            wait();
        }
        success = true;
    } catch (InterruptedException e) {
        log.warn("Interrupted");
        Thread.currentThread().interrupt();
        throw new MojoExecutionException("interrupted", e);
    } catch (IOException e) {
        throw new MojoExecutionException("I/O Error", e);
    } finally {
        shutdownExecutorService(executorService);
        // Rollback if not all could be started
        if (!success) {
            log.error("Error occurred during container startup, shutting down...");
            runService.stopStartedContainers(keepContainer, removeVolumes, autoCreateCustomNetworks, getPomLabel());
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) PortMapping(io.fabric8.maven.docker.access.PortMapping) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 30 with DockerAccessException

use of io.fabric8.maven.docker.access.DockerAccessException in project docker-maven-plugin by fabric8io.

the class StopMojo method stopContainers.

private void stopContainers(QueryService queryService, RunService runService, PomLabel pomLabel) throws DockerAccessException, ExecException {
    Collection<Network> networksToRemove = getNetworksToRemove(queryService, pomLabel);
    for (ImageConfiguration image : getResolvedImages()) {
        for (Container container : getContainersToStop(queryService, image)) {
            if (shouldStopContainer(container, pomLabel, image)) {
                runService.stopContainer(container.getId(), image, keepContainer, removeVolumes);
            }
        }
    }
    runService.removeCustomNetworks(networksToRemove);
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) Network(io.fabric8.maven.docker.model.Network)

Aggregations

DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)11 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Container (io.fabric8.maven.docker.model.Container)6 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 Arguments (io.fabric8.maven.docker.config.Arguments)4 MojoFailureException (org.apache.maven.plugin.MojoFailureException)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)3 File (java.io.File)3 IOException (java.io.IOException)3 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)2 ContainerHostConfig (io.fabric8.maven.docker.access.ContainerHostConfig)2 ExecException (io.fabric8.maven.docker.access.ExecException)2 HttpBodyAndStatus (io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.HttpBodyAndStatus)2 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)2 VolumeConfiguration (io.fabric8.maven.docker.config.VolumeConfiguration)2 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)2 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)2 Network (io.fabric8.maven.docker.model.Network)2