Search in sources :

Example 11 with ServiceHub

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());
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) PortMapping(io.fabric8.maven.docker.access.PortMapping) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 12 with ServiceHub

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);
    }
}
Also used : VolumeService(io.fabric8.maven.docker.service.VolumeService) VolumeConfiguration(io.fabric8.maven.docker.config.VolumeConfiguration)

Example 13 with ServiceHub

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 14 with ServiceHub

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);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) WatcherContext(io.fabric8.maven.watcher.api.WatcherContext) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) URL(java.net.URL) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 15 with ServiceHub

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);
    }
}
Also used : Task(io.fabric8.maven.docker.util.Task) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) BuildService(io.fabric8.maven.docker.service.BuildService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) WatchService(io.fabric8.maven.docker.service.WatchService)

Aggregations

MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)4 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)4 IOException (java.io.IOException)4 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 ExecException (io.fabric8.maven.docker.access.ExecException)2 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)2 VolumeConfiguration (io.fabric8.maven.docker.config.VolumeConfiguration)2 BuildService (io.fabric8.maven.docker.service.BuildService)2 QueryService (io.fabric8.maven.docker.service.QueryService)2 ServiceHub (io.fabric8.maven.docker.service.ServiceHub)2 VolumeService (io.fabric8.maven.docker.service.VolumeService)2 File (java.io.File)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 Fabric8ServiceHub (io.fabric8.maven.core.service.Fabric8ServiceHub)1 DockerAccess (io.fabric8.maven.docker.access.DockerAccess)1 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)1