use of io.fabric8.maven.docker.service.RunService in project docker-maven-plugin by fabric8io.
the class StopMojo method executeInternal.
@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, IOException, ExecException {
QueryService queryService = hub.getQueryService();
RunService runService = hub.getRunService();
GavLabel gavLabel = getGavLabel();
if (!keepRunning) {
if (invokedTogetherWithDockerStart()) {
runService.stopStartedContainers(keepContainer, removeVolumes, autoCreateCustomNetworks, gavLabel);
} else {
stopContainers(queryService, runService, gavLabel);
}
}
// Switch off all logging
LogDispatcher dispatcher = getLogDispatcher(hub);
dispatcher.untrackAllContainerLogs();
}
use of io.fabric8.maven.docker.service.RunService in project docker-maven-plugin by fabric8io.
the class StopMojo method stopContainers.
private void stopContainers(QueryService queryService, RunService runService, PomLabel pomLabel) throws DockerAccessException, ExecException {
Collection<Network> networksToRemove = getNetworksToRemove(queryService, pomLabel);
for (ImageConfiguration image : getResolvedImages()) {
for (Container container : getContainersToStop(queryService, image)) {
if (shouldStopContainer(container, pomLabel, image)) {
runService.stopContainer(container.getId(), image, keepContainer, removeVolumes);
}
}
}
runService.removeCustomNetworks(networksToRemove);
}
use of io.fabric8.maven.docker.service.RunService 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.service.RunService 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());
}
}
}
use of io.fabric8.maven.docker.service.RunService 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;
}
}
Aggregations