From 8e451f85fbe48219f8c3b438c90cb23730da940c Mon Sep 17 00:00:00 2001 From: "Jeschke, Peter Rene" Date: Tue, 13 Aug 2019 08:13:33 +0200 Subject: [PATCH] Add option to keep trying to remove containers, even if one fails Signed-off-by: Jeschke, Peter Rene --- .../io/fabric8/maven/docker/StopMojo.java | 45 ++++++++++++++----- .../maven/docker/service/RunService.java | 41 ++++++++++++++--- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/fabric8/maven/docker/StopMojo.java b/src/main/java/io/fabric8/maven/docker/StopMojo.java index 44fab53e5..4945e854e 100644 --- a/src/main/java/io/fabric8/maven/docker/StopMojo.java +++ b/src/main/java/io/fabric8/maven/docker/StopMojo.java @@ -1,15 +1,23 @@ package io.fabric8.maven.docker; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +import io.fabric8.maven.docker.access.DockerAccessException; import io.fabric8.maven.docker.access.ExecException; import io.fabric8.maven.docker.config.ImageConfiguration; import io.fabric8.maven.docker.config.NetworkConfig; @@ -23,10 +31,6 @@ import io.fabric8.maven.docker.util.GavLabel; import io.fabric8.maven.docker.util.NamePatternUtil; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; /** * Mojo for stopping containers. If called together with docker:start (i.e. @@ -92,7 +96,7 @@ private void stopContainers(QueryService queryService, RunService runService, Ga throws MojoExecutionException, IOException, ExecException { Collection networksToRemove = getNetworksToRemove(queryService, gavLabel); - + List thrownExceptions = new ArrayList<>(); for (ImageConfiguration image : getResolvedImages()) { Collection existingContainers @@ -100,21 +104,40 @@ private void stopContainers(QueryService queryService, RunService runService, Ga for (Container container : existingContainers) { if (shouldStopContainer(container, gavLabel)) { - runService.stopContainer(container.getId(), image, keepContainer, removeVolumes); + 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)) { - runService.stopContainer(container.getId(), - new ImageConfiguration.Builder().name(container.getImage()).build(), - keepContainer, removeVolumes); + try { + runService.stopContainer(container.getId(), + new ImageConfiguration.Builder().name(container.getImage()).build(), + keepContainer, removeVolumes); + } catch (DockerAccessException exc) { + thrownExceptions.add(exc); + } } } - runService.removeCustomNetworks(networksToRemove); + 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; + } } private Collection getContainersForPattern(QueryService queryService, Matcher imageNameMatcher, diff --git a/src/main/java/io/fabric8/maven/docker/service/RunService.java b/src/main/java/io/fabric8/maven/docker/service/RunService.java index f3009bd21..63b0017ea 100644 --- a/src/main/java/io/fabric8/maven/docker/service/RunService.java +++ b/src/main/java/io/fabric8/maven/docker/service/RunService.java @@ -1,5 +1,7 @@ package io.fabric8.maven.docker.service; +import static io.fabric8.maven.docker.util.VolumeBindingUtil.resolveRelativeVolumeBindings; + /* * * Copyright 2014 Roland Huss @@ -21,7 +23,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -60,8 +61,6 @@ import io.fabric8.maven.docker.wait.WaitTimeoutException; import io.fabric8.maven.docker.wait.WaitUtil; -import static io.fabric8.maven.docker.util.VolumeBindingUtil.resolveRelativeVolumeBindings; - /** * Service class for helping in running containers. @@ -206,12 +205,28 @@ public void stopStartedContainers(boolean keepContainer, boolean removeCustomNetworks, GavLabel gavLabel) throws DockerAccessException, ExecException { + List thrownExceptions = new ArrayList<>(); Set networksToRemove = new HashSet<>(); for (ContainerTracker.ContainerShutdownDescriptor descriptor : tracker.removeShutdownDescriptors(gavLabel)) { - collectCustomNetworks(networksToRemove, descriptor, removeCustomNetworks); - shutdown(descriptor, keepContainer, removeVolumes); + try { + collectCustomNetworks(networksToRemove, descriptor, removeCustomNetworks); + shutdown(descriptor, keepContainer, removeVolumes); + } catch (DockerAccessException exc) { + thrownExceptions.add(exc); + } + } + try { + removeCustomNetworks(networksToRemove); + } catch (DockerAccessException exc) { + thrownExceptions.add(exc); } - removeCustomNetworks(networksToRemove); + if (!thrownExceptions.isEmpty()) { + DockerAccessException exception = new DockerAccessException("At least one exception thrown during container removal."); + for (DockerAccessException dae : thrownExceptions) { + exception.addSuppressed(dae); + } + throw exception; + } } private void collectCustomNetworks(Set networksToRemove, ContainerTracker.ContainerShutdownDescriptor descriptor, boolean removeCustomNetworks) throws DockerAccessException { @@ -495,8 +510,20 @@ public void createCustomNetworkIfNotExistant(String customNetwork) throws Docker } public void removeCustomNetworks(Collection networks) throws DockerAccessException { + List thrownExceptions = new ArrayList<>(); for (Network network : networks) { - docker.removeNetwork(network.getId()); + try { + docker.removeNetwork(network.getId()); + } catch (DockerAccessException exc) { + thrownExceptions.add(exc); + } + } + if (!thrownExceptions.isEmpty()) { + DockerAccessException exception = new DockerAccessException("At least one exception was thrown while trying to remove custom networks."); + for (DockerAccessException dae : thrownExceptions) { + exception.addSuppressed(dae); + } + throw exception; } }