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);
});
}
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;
}
}
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;
}
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());
}
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"));
}
Aggregations