Search in sources :

Example 16 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project docker-maven-plugin by fabric8io.

the class RunService method createVolumesAsPerVolumeBinds.

/**
 * Creates a Volume if a volume is referred to during startup in bind mount mapping and
 * a VolumeConfiguration exists
 *
 * @param hub Service hub
 * @param binds volume binds present in ImageConfiguration
 * @param volumes VolumeConfigs present
 * @return List of volumes created
 * @throws DockerAccessException
 */
public List<String> createVolumesAsPerVolumeBinds(ServiceHub hub, List<String> binds, List<VolumeConfiguration> volumes) throws DockerAccessException {
    Map<String, Integer> indexMap = new HashMap<>();
    List<String> volumesCreated = new ArrayList<>();
    for (int index = 0; index < volumes.size(); index++) {
        indexMap.put(volumes.get(index).getName(), index);
    }
    for (String bind : binds) {
        if (bind.contains(":")) {
            String name = bind.substring(0, bind.indexOf(':'));
            Integer volumeConfigIndex = indexMap.get(name);
            if (volumeConfigIndex != null) {
                VolumeConfiguration volumeConfig = volumes.get(volumeConfigIndex);
                hub.getVolumeService().createVolume(volumeConfig);
                volumesCreated.add(volumeConfig.getName());
            }
        }
    }
    return volumesCreated;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RunVolumeConfiguration(io.fabric8.maven.docker.config.RunVolumeConfiguration) VolumeConfiguration(io.fabric8.maven.docker.config.VolumeConfiguration)

Example 17 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub 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);
    });
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) StartContainerExecutor(io.fabric8.maven.docker.service.helper.StartContainerExecutor)

Example 18 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);
                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());
        }
    }
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque) QueryService(io.fabric8.maven.docker.service.QueryService) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ExecutorService(java.util.concurrent.ExecutorService) PortMapping(io.fabric8.maven.docker.access.PortMapping) HashSet(java.util.HashSet)

Example 19 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project docker-maven-plugin by fabric8io.

the class TagMojo method executeInternal.

@Override
public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
    if (skipTag) {
        return;
    }
    List<ImageConfiguration> imageConfigs = getResolvedImages();
    for (ImageConfiguration imageConfig : imageConfigs) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        hub.getBuildService().tagImage(imageConfig.getName(), tagName, repo, buildConfig.cleanupMode());
    }
}
Also used : BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 20 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project docker-maven-plugin by fabric8io.

the class PushMojo method executeJibPush.

private void executeJibPush(ServiceHub hub) throws MojoExecutionException {
    log.info("Pushing Container image with [[B]]JIB(Java Image Builder)[[B]] mode");
    JibBuildService jibBuildService = new JibBuildService(hub, new MojoParameters(session, project, null, null, null, settings, sourceDirectory, outputDirectory, null), log);
    jibBuildService.push(getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag);
}
Also used : JibBuildService(io.fabric8.maven.docker.service.JibBuildService) MojoParameters(io.fabric8.maven.docker.util.MojoParameters)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)9 File (java.io.File)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 VolumeConfiguration (io.fabric8.maven.docker.config.VolumeConfiguration)5 QueryService (io.fabric8.maven.docker.service.QueryService)5 RunService (io.fabric8.maven.docker.service.RunService)5 IOException (java.io.IOException)5 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)4 Test (org.junit.Test)4 PortMapping (io.fabric8.maven.docker.access.PortMapping)3 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)3 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)3 BuildService (io.fabric8.maven.docker.service.BuildService)3 JibBuildService (io.fabric8.maven.docker.service.JibBuildService)3 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 Fabric8ServiceHub (io.fabric8.maven.core.service.Fabric8ServiceHub)2 DockerAccess (io.fabric8.maven.docker.access.DockerAccess)2