Search in sources :

Example 16 with QueryService

use of io.fabric8.maven.docker.service.QueryService 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 17 with QueryService

use of io.fabric8.maven.docker.service.QueryService in project docker-maven-plugin by fabric8io.

the class StopMojo method getNetworksToRemove.

private Set<Network> getNetworksToRemove(QueryService queryService, GavLabel gavLabel) throws IOException {
    if (!autoCreateCustomNetworks) {
        return Collections.emptySet();
    }
    Set<Network> customNetworks = new HashSet<>();
    Set<Network> networks = queryService.getNetworks();
    for (ImageConfiguration image : getResolvedImages()) {
        final NetworkConfig config = image.getRunConfiguration().getNetworkingConfig();
        if (!config.isCustomNetwork() || config.getName() == null) {
            continue;
        }
        final Network network = getNetworkByName(networks, config.getCustomNetwork());
        if (network == null) {
            continue;
        }
        customNetworks.add(network);
        Collection<Container> existingContainers = ContainerNamingUtil.getContainersToStop(image, containerNamePattern, getBuildTimestamp(), queryService.getContainersForImage(image.getName(), !keepContainer));
        for (Container container : existingContainers) {
            if (!shouldStopContainer(container, gavLabel)) {
                // it's sill in use don't collect it
                customNetworks.remove(network);
            }
        }
    }
    return customNetworks;
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Network(io.fabric8.maven.docker.model.Network) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) HashSet(java.util.HashSet)

Example 18 with QueryService

use of io.fabric8.maven.docker.service.QueryService in project docker-maven-plugin by fabric8io.

the class RunServiceTest method testWithMultipleStopExceptions.

@Test
public void testWithMultipleStopExceptions() throws Exception {
    GavLabel testLabel = new GavLabel("Im:A:Test");
    String firstName = "first-container:latest";
    ImageConfiguration first = new ImageConfiguration();
    first.setName(firstName);
    String secondName = "second-container:latest";
    ImageConfiguration second = new ImageConfiguration();
    second.setName(secondName);
    tracker.registerContainer(firstName, first, testLabel);
    tracker.registerContainer(secondName, second, testLabel);
    LogOutputSpecFactory logOutputSpecFactory = new LogOutputSpecFactory(true, true, null);
    new Expectations() {

        {
            docker.stopContainer(firstName, 0);
            result = new DockerAccessException("TEST one");
            docker.stopContainer(secondName, 0);
            result = new DockerAccessException("TEST two");
        }
    };
    runService = new RunService(docker, queryService, tracker, logOutputSpecFactory, log);
    Exception thrownException = assertThrows(DockerAccessException.class, () -> runService.stopStartedContainers(false, true, true, testLabel));
    assertEquals("(TEST two,TEST one)", thrownException.getLocalizedMessage());
}
Also used : GavLabel(io.fabric8.maven.docker.util.GavLabel) Expectations(mockit.Expectations) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) IOException(java.io.IOException) ExecException(io.fabric8.maven.docker.access.ExecException) PortBindingException(io.fabric8.maven.docker.model.PortBindingException) Test(org.junit.Test)

Example 19 with QueryService

use of io.fabric8.maven.docker.service.QueryService 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

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)11 Container (io.fabric8.maven.docker.model.Container)9 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)6 QueryService (io.fabric8.maven.docker.service.QueryService)5 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)4 Network (io.fabric8.maven.docker.model.Network)4 RunService (io.fabric8.maven.docker.service.RunService)4 GavLabel (io.fabric8.maven.docker.util.GavLabel)4 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)3 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Expectations (mockit.Expectations)3 Test (org.junit.Test)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 CopyConfiguration (io.fabric8.maven.docker.config.CopyConfiguration)2 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)2 RegistryService (io.fabric8.maven.docker.service.RegistryService)2 StartOrderResolver (io.fabric8.maven.docker.util.StartOrderResolver)2