Search in sources :

Example 11 with QueryService

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

the class ExitCodeCheckerTest method checkReturnsFalseIfContainerDoesNotExist.

@Test
public void checkReturnsFalseIfContainerDoesNotExist() throws Exception {
    new Expectations() {

        {
            Exception e = new DockerAccessException("Cannot find container %s", CONTAINER_ID);
            queryService.getMandatoryContainer(CONTAINER_ID);
            result = e;
        }
    };
    ExitCodeChecker checker = new ExitCodeChecker(0, queryService, CONTAINER_ID);
    assertThat(checker.check()).isFalse();
}
Also used : Expectations(mockit.Expectations) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) Test(org.junit.Test)

Example 12 with QueryService

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

the class WaitService method prepareWaitCheckers.

private List<WaitChecker> prepareWaitCheckers(ImageConfiguration imageConfig, Properties projectProperties, String containerId) throws IOException {
    WaitConfiguration wait = getWaitConfiguration(imageConfig);
    if (wait == null) {
        return Collections.emptyList();
    }
    List<WaitChecker> checkers = new ArrayList<>();
    if (wait.getUrl() != null) {
        checkers.add(getUrlWaitChecker(imageConfig.getDescription(), projectProperties, wait));
    }
    if (wait.getLog() != null) {
        log.debug("LogWaitChecker: Waiting on %s", wait.getLog());
        checkers.add(new LogWaitChecker(wait.getLog(), dockerAccess, containerId, log));
    }
    if (wait.getTcp() != null) {
        try {
            Container container = queryService.getMandatoryContainer(containerId);
            checkers.add(getTcpWaitChecker(container, imageConfig.getDescription(), projectProperties, wait.getTcp()));
        } catch (DockerAccessException e) {
            throw new IOException("Unable to access container " + containerId, e);
        }
    }
    if (Boolean.TRUE.equals(wait.getHealthy())) {
        checkers.add(new HealthCheckChecker(dockerAccess, containerId, imageConfig.getDescription(), log));
    }
    if (wait.getExit() != null) {
        checkers.add(new ExitCodeChecker(wait.getExit(), queryService, containerId));
    }
    return checkers;
}
Also used : WaitConfiguration(io.fabric8.maven.docker.config.WaitConfiguration) HealthCheckChecker(io.fabric8.maven.docker.wait.HealthCheckChecker) Container(io.fabric8.maven.docker.model.Container) LogWaitChecker(io.fabric8.maven.docker.wait.LogWaitChecker) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ArrayList(java.util.ArrayList) ExitCodeChecker(io.fabric8.maven.docker.wait.ExitCodeChecker) IOException(java.io.IOException) LogWaitChecker(io.fabric8.maven.docker.wait.LogWaitChecker) WaitChecker(io.fabric8.maven.docker.wait.WaitChecker)

Example 13 with QueryService

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

the class WatchService method watch.

public synchronized void watch(WatchContext context, BuildService.BuildContext buildContext, List<ImageConfiguration> images) throws DockerAccessException, MojoExecutionException {
    // Important to be be a single threaded scheduler since watch jobs must run serialized
    ScheduledExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadScheduledExecutor();
        for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, images)) {
            final ImageConfiguration imageConfig = (ImageConfiguration) resolvable;
            String imageId = queryService.getImageId(imageConfig.getName());
            String containerId = runService.lookupContainer(imageConfig.getName());
            ImageWatcher watcher = new ImageWatcher(imageConfig, context, imageId, containerId);
            long interval = watcher.getInterval();
            WatchMode watchMode = watcher.getWatchMode(imageConfig);
            log.info("Watching " + imageConfig.getName() + (watchMode != null ? " using " + watchMode.getDescription() : ""));
            ArrayList<String> tasks = new ArrayList<>();
            if (imageConfig.getBuildConfiguration() != null) {
                for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
                    if (watcher.isCopy()) {
                        String containerBaseDir = assemblyConfiguration.getTargetDir();
                        schedule(executor, createCopyWatchTask(watcher, assemblyConfiguration.getName(), context.getMojoParameters(), containerBaseDir), interval);
                        tasks.add("copying artifacts");
                    }
                    if (watcher.isBuild()) {
                        schedule(executor, createBuildWatchTask(watcher, assemblyConfiguration.getName(), context.getMojoParameters(), watchMode == WatchMode.both, buildContext), interval);
                        tasks.add("rebuilding");
                    }
                }
            }
            if (watcher.isRun() && watcher.getContainerId() != null) {
                schedule(executor, createRestartWatchTask(watcher), interval);
                tasks.add("restarting");
            }
            if (tasks.size() > 0) {
                log.info("%s: Watch for %s", imageConfig.getDescription(), StringUtils.join(tasks.toArray(), " and "));
            }
        }
        log.info("Waiting ...");
        if (!context.isKeepRunning()) {
            runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
        }
        wait();
    } catch (InterruptedException e) {
        log.warn("Interrupted");
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) StartOrderResolver(io.fabric8.maven.docker.util.StartOrderResolver) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) WatchImageConfiguration(io.fabric8.maven.docker.config.WatchImageConfiguration) WatchMode(io.fabric8.maven.docker.config.WatchMode) ArrayList(java.util.ArrayList)

Example 14 with QueryService

use of io.fabric8.maven.docker.service.QueryService 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 15 with QueryService

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

the class LogsMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
    QueryService queryService = hub.getQueryService();
    LogDispatcher logDispatcher = getLogDispatcher(hub);
    for (ImageConfiguration image : getResolvedImages()) {
        String imageName = image.getName();
        if (logAll) {
            for (Container container : queryService.getContainersForImage(imageName, false)) {
                doLogging(logDispatcher, image, container.getId());
            }
        } else {
            Container container = queryService.getLatestContainerForImage(imageName, false);
            if (container != null) {
                doLogging(logDispatcher, image, container.getId());
            }
        }
    }
    if (follow) {
        // Block forever ....
        waitForEver();
    }
}
Also used : Container(io.fabric8.maven.docker.model.Container) QueryService(io.fabric8.maven.docker.service.QueryService) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)11 Container (io.fabric8.maven.docker.model.Container)9 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)6 QueryService (io.fabric8.maven.docker.service.QueryService)5 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)4 Network (io.fabric8.maven.docker.model.Network)4 RunService (io.fabric8.maven.docker.service.RunService)4 GavLabel (io.fabric8.maven.docker.util.GavLabel)4 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)3 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Expectations (mockit.Expectations)3 Test (org.junit.Test)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 CopyConfiguration (io.fabric8.maven.docker.config.CopyConfiguration)2 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)2 RegistryService (io.fabric8.maven.docker.service.RegistryService)2 StartOrderResolver (io.fabric8.maven.docker.util.StartOrderResolver)2