Search in sources :

Example 11 with ExecException

use of io.fabric8.maven.docker.access.ExecException 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);
                if (!startParallel) {
                    waitForStartedContainer(containerStartupService, startedContainerAliases, imagesStarting);
                }
            }
            if (startParallel) {
                waitForStartedContainer(containerStartupService, startedContainerAliases, imagesStarting);
            }
        }
        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, getGavLabel());
        }
    }
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque) QueryService(io.fabric8.maven.docker.service.QueryService) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ExecutorService(java.util.concurrent.ExecutorService) PortMapping(io.fabric8.maven.docker.access.PortMapping) HashSet(java.util.HashSet)

Example 12 with ExecException

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

the class StopMojo method stopContainers.

private void stopContainers(QueryService queryService, RunService runService, GavLabel gavLabel) throws MojoExecutionException, IOException, ExecException {
    Collection<Network> networksToRemove = getNetworksToRemove(queryService, gavLabel);
    List<DockerAccessException> thrownExceptions = new ArrayList<>();
    for (ImageConfiguration image : getResolvedImages()) {
        Collection<Container> existingContainers = getContainersForImage(queryService, image);
        for (Container container : existingContainers) {
            if (shouldStopContainer(container, gavLabel)) {
                try {
                    runService.stopContainer(container.getId(), image, keepContainer, removeVolumes);
                } catch (DockerAccessException exc) {
                    thrownExceptions.add(exc);
                }
            }
        }
    }
    // If the mojo has a stopNamePattern, check to see if there are matching containers
    for (Container container : getContainersForMojo(queryService)) {
        if (shouldStopContainer(container, gavLabel)) {
            try {
                runService.stopContainer(container.getId(), new ImageConfiguration.Builder().name(container.getImage()).build(), keepContainer, removeVolumes);
            } catch (DockerAccessException exc) {
                thrownExceptions.add(exc);
            }
        }
    }
    try {
        runService.removeCustomNetworks(networksToRemove);
    } catch (DockerAccessException exc) {
        thrownExceptions.add(exc);
    }
    if (!thrownExceptions.isEmpty()) {
        DockerAccessException exception = new DockerAccessException("At least one exception thrown during container removal.");
        for (DockerAccessException dae : thrownExceptions) {
            exception.addSuppressed(dae);
        }
        throw exception;
    }
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Network(io.fabric8.maven.docker.model.Network) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ArrayList(java.util.ArrayList)

Example 13 with ExecException

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

the class StartContainerExecutorTest method testStartContainers.

@Test
public void testStartContainers(@Mocked ServiceHub hub, @Mocked DockerAccess dockerAccess, @Mocked ContainerTracker containerTracker, @Mocked Logger log) throws IOException, ExecException {
    // Given
    new Expectations() {

        {
            dockerAccess.createContainer((ContainerCreateConfig) any, anyString);
            result = "container-name";
            dockerAccess.getContainer(anyString);
            result = new ContainerDetails(JsonFactory.newJsonObject("{\"NetworkSettings\":{\"IPAddress\":\"192.168.1.2\"}}"));
            QueryService queryService = new QueryService(dockerAccess);
            hub.getQueryService();
            result = queryService;
            hub.getRunService();
            result = new RunService(dockerAccess, queryService, containerTracker, new LogOutputSpecFactory(true, true, null), log);
        }
    };
    Properties projectProps = new Properties();
    StartContainerExecutor startContainerExecutor = new StartContainerExecutor.Builder().serviceHub(hub).projectProperties(projectProps).portMapping(new PortMapping(Collections.emptyList(), projectProps)).gavLabel(new GavLabel("io.fabric8:test:0.1.0")).basedir(new File("/tmp/foo")).containerNamePattern("test-").buildTimestamp(new Date()).exposeContainerProps("docker.container").imageConfig(new ImageConfiguration.Builder().name("name").alias("alias").runConfig(new RunImageConfiguration.Builder().build()).build()).build();
    // When
    String containerId = startContainerExecutor.startContainer();
    // Then
    assertEquals("container-name", containerId);
    assertEquals("container-name", projectProps.getProperty("docker.container.alias.id"));
    assertEquals("192.168.1.2", projectProps.getProperty("docker.container.alias.ip"));
}
Also used : Expectations(mockit.Expectations) RunService(io.fabric8.maven.docker.service.RunService) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) Properties(java.util.Properties) Date(java.util.Date) GavLabel(io.fabric8.maven.docker.util.GavLabel) QueryService(io.fabric8.maven.docker.service.QueryService) PortMapping(io.fabric8.maven.docker.access.PortMapping) File(java.io.File) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails) Test(org.junit.Test)

Aggregations

ExecException (io.fabric8.maven.docker.access.ExecException)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)3 Network (io.fabric8.maven.docker.model.Network)3 QueryService (io.fabric8.maven.docker.service.QueryService)3 RunService (io.fabric8.maven.docker.service.RunService)3 File (java.io.File)3 IOException (java.io.IOException)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)2 Container (io.fabric8.maven.docker.model.Container)2 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)2 ContainerShutdownDescriptor (io.fabric8.maven.docker.service.ContainerTracker.ContainerShutdownDescriptor)2 GavLabel (io.fabric8.maven.docker.util.GavLabel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DockerAccess (io.fabric8.maven.docker.access.DockerAccess)1 AssemblyFiles (io.fabric8.maven.docker.assembly.AssemblyFiles)1