Search in sources :

Example 11 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel in project docker-maven-plugin by fabric8io.

the class StartMojo method startImage.

private void startImage(final ImageConfiguration imageConfig, final ServiceHub hub, final ExecutorCompletionService<StartedContainer> startingContainers, final PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper) throws IOException {
    final RunService runService = hub.getRunService();
    final Properties projProperties = project.getProperties();
    final RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
    final PortMapping portMapping = runService.createPortMapping(runConfig, projProperties);
    final LogDispatcher dispatcher = getLogDispatcher(hub);
    StartContainerExecutor startExecutor = new StartContainerExecutor.Builder().exposeContainerProps(exposeContainerProps).dispatcher(dispatcher).follow(follow).log(log).portMapping(portMapping).gavLabel(getGavLabel()).projectProperties(project.getProperties()).basedir(project.getBasedir()).imageConfig(imageConfig).serviceHub(hub).logOutputSpecFactory(serviceHubFactory.getLogOutputSpecFactory()).showLogs(showLogs).containerNamePattern(containerNamePattern).buildTimestamp(getBuildTimestamp()).build();
    startingContainers.submit(() -> {
        String containerId = startExecutor.startContainer();
        // Update port-mapping writer
        portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile());
        return new StartedContainer(imageConfig, containerId);
    });
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) StartContainerExecutor(io.fabric8.maven.docker.service.helper.StartContainerExecutor)

Example 12 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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 GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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 14 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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 15 with GavLabel

use of io.fabric8.maven.docker.util.GavLabel 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

GavLabel (io.fabric8.maven.docker.util.GavLabel)7 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 Expectations (mockit.Expectations)5 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 RunService (io.fabric8.maven.docker.service.RunService)4 Test (org.junit.Test)4 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)3 Container (io.fabric8.maven.docker.model.Container)3 Network (io.fabric8.maven.docker.model.Network)3 PortBindingException (io.fabric8.maven.docker.model.PortBindingException)3 QueryService (io.fabric8.maven.docker.service.QueryService)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Properties (java.util.Properties)3 Gson (com.google.gson.Gson)2 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 CopyConfiguration (io.fabric8.maven.docker.config.CopyConfiguration)2 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)2 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)2