use of io.fabric8.maven.docker.service.ServiceHub 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);
}
// Wait for the next container to finish startup
final Future<StartedContainer> startedContainerFuture = containerStartupService.take();
try {
final StartedContainer startedContainer = startedContainerFuture.get();
final ImageConfiguration imageConfig = startedContainer.imageConfig;
updateAliasesSet(startedContainerAliases, imageConfig.getAlias());
exposeContainerProps(hub.getQueryService(), startedContainer);
// All done with this image
imagesStarting.remove(imageConfig);
} catch (ExecutionException e) {
rethrowCause(e);
}
}
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, getPomLabel());
}
}
}
use of io.fabric8.maven.docker.service.ServiceHub in project docker-maven-plugin by fabric8io.
the class VolumeCreateMojo method executeInternal.
@Override
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {
VolumeService volService = serviceHub.getVolumeService();
for (VolumeConfiguration volume : getVolumes()) {
log.info("Creating volume '%s'", volume.getName());
volService.createVolume(volume);
}
}
use of io.fabric8.maven.docker.service.ServiceHub in project fabric8-maven-plugin by fabric8io.
the class BuildMojo method buildAndTag.
@Override
protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig) throws MojoExecutionException, DockerAccessException {
try {
// TODO need to refactor d-m-p to avoid this call
EnvUtil.storeTimestamp(this.getBuildTimestampFile(), this.getBuildTimestamp());
fabric8ServiceHub.getBuildService().build(imageConfig);
} catch (Exception ex) {
throw new MojoExecutionException("Failed to execute the build", ex);
}
}
use of io.fabric8.maven.docker.service.ServiceHub in project fabric8-maven-plugin by fabric8io.
the class WatchMojo method executeInternal.
@Override
protected synchronized void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
this.hub = hub;
URL masterUrl = kubernetes.getMasterUrl();
KubernetesResourceUtil.validateKubernetesMasterUrl(masterUrl);
File manifest;
boolean isOpenshift = KubernetesHelper.isOpenShift(kubernetes);
if (isOpenshift) {
manifest = openshiftManifest;
} else {
manifest = kubernetesManifest;
}
try {
Set<HasMetadata> resources = KubernetesResourceUtil.loadResources(manifest);
WatcherContext context = getWatcherContext();
WatcherManager.watch(getResolvedImages(), resources, context);
} catch (KubernetesClientException ex) {
KubernetesResourceUtil.handleKubernetesClientException(ex, this.log);
} catch (Exception ex) {
throw new MojoExecutionException("An error has occurred while while trying to watch the resources", ex);
}
}
use of io.fabric8.maven.docker.service.ServiceHub in project fabric8-maven-plugin by fabric8io.
the class DockerImageWatcher method watch.
@Override
public void watch(List<ImageConfiguration> configs, final Set<HasMetadata> resources, PlatformMode mode) {
BuildService.BuildContext buildContext = getContext().getBuildContext();
WatchService.WatchContext watchContext = getContext().getWatchContext();
// add a image customizer
watchContext = new WatchService.WatchContext.Builder(watchContext).imageCustomizer(new Task<ImageConfiguration>() {
@Override
public void execute(ImageConfiguration imageConfiguration) throws DockerAccessException, MojoExecutionException, MojoFailureException {
buildImage(imageConfiguration);
}
}).containerRestarter(new Task<WatchService.ImageWatcher>() {
@Override
public void execute(WatchService.ImageWatcher imageWatcher) throws DockerAccessException, MojoExecutionException, MojoFailureException {
restartContainer(imageWatcher, resources);
}
}).build();
ServiceHub hub = getContext().getServiceHub();
try {
hub.getWatchService().watch(watchContext, buildContext, configs);
} catch (Exception ex) {
throw new RuntimeException("Error while watching", ex);
}
}
Aggregations