Search in sources :

Example 61 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration 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 && imageConfig.getBuildConfiguration().getAssemblyConfiguration() != null) {
                if (watcher.isCopy()) {
                    String containerBaseDir = imageConfig.getBuildConfiguration().getAssemblyConfiguration().getTargetDir();
                    schedule(executor, createCopyWatchTask(watcher, context.getMojoParameters(), containerBaseDir), interval);
                    tasks.add("copying artifacts");
                }
                if (watcher.isBuild()) {
                    schedule(executor, createBuildWatchTask(watcher, 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) 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 62 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class BuildMojo method buildAndTag.

protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig) throws MojoExecutionException, DockerAccessException {
    EnvUtil.storeTimestamp(getBuildTimestampFile(), getBuildTimestamp());
    BuildService.BuildContext buildContext = getBuildContext();
    ImagePullManager pullManager = getImagePullManager(determinePullPolicy(imageConfig.getBuildConfiguration()), autoPull);
    BuildService buildService = hub.getBuildService();
    buildService.buildImage(imageConfig, pullManager, buildContext);
    if (!skipTag) {
        buildService.tagImage(imageConfig.getName(), imageConfig);
    }
}
Also used : BuildService(io.fabric8.maven.docker.service.BuildService) ImagePullManager(io.fabric8.maven.docker.service.ImagePullManager)

Example 63 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration 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)) {
                doLogging(logDispatcher, image, container.getId());
            }
        } else {
            Container container = queryService.getLatestContainerForImage(imageName);
            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)

Example 64 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class SourceMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
    MojoParameters params = createMojoParameters();
    List<ImageConfiguration> imageConfigs = new ArrayList<>();
    for (ImageConfiguration imageConfig : getResolvedImages()) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        if (buildConfig != null) {
            if (buildConfig.skip()) {
                log.info("%s: Skipped creating source", imageConfig.getDescription());
            } else {
                imageConfigs.add(imageConfig);
            }
        }
    }
    if (sourceMode == BuildImageSelectMode.first && imageConfigs.size() > 0) {
        ImageConfiguration imageConfig = imageConfigs.get(0);
        File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
        projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(null), dockerTar);
    } else {
        for (ImageConfiguration imageConfig : imageConfigs) {
            File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
            String alias = imageConfig.getAlias();
            if (alias == null) {
                throw new IllegalArgumentException("Image " + imageConfig.getDescription() + " must have an 'alias' configured to be " + "used as a classifier for attaching a docker build tar as source to the maven build");
            }
            projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(alias), dockerTar);
        }
    }
}
Also used : MojoParameters(io.fabric8.maven.docker.util.MojoParameters) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ArrayList(java.util.ArrayList) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 65 with ImageConfiguration

use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.

the class StartMojo method startImage.

private void startImage(final ImageConfiguration image, final ServiceHub hub, final ExecutorCompletionService<StartedContainer> startingContainers, final PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper) {
    final RunService runService = hub.getRunService();
    final Properties projProperties = project.getProperties();
    final RunImageConfiguration runConfig = image.getRunConfiguration();
    final PortMapping portMapping = runService.createPortMapping(runConfig, projProperties);
    final LogDispatcher dispatcher = getLogDispatcher(hub);
    startingContainers.submit(new Callable<StartedContainer>() {

        @Override
        public StartedContainer call() throws Exception {
            final String containerId = runService.createAndStartContainer(image, portMapping, getPomLabel(), projProperties, project.getBasedir());
            // Update port-mapping writer
            portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile());
            if (showLogs(image)) {
                dispatcher.trackContainerLog(containerId, serviceHubFactory.getLogOutputSpecFactory().createSpec(containerId, image));
            }
            // Wait if requested
            hub.getWaitService().wait(image, projProperties, containerId);
            WaitConfiguration waitConfig = runConfig.getWaitConfiguration();
            if (waitConfig != null && waitConfig.getExec() != null && waitConfig.getExec().getPostStart() != null) {
                try {
                    runService.execInContainer(containerId, waitConfig.getExec().getPostStart(), image);
                } catch (ExecException exp) {
                    if (waitConfig.getExec().isBreakOnError()) {
                        throw exp;
                    } else {
                        log.warn("Cannot run postStart: %s", exp.getMessage());
                    }
                }
            }
            return new StartedContainer(image, containerId);
        }
    });
}
Also used : LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher) ExecException(io.fabric8.maven.docker.access.ExecException) PortMapping(io.fabric8.maven.docker.access.PortMapping) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) IOException(java.io.IOException) ExecException(io.fabric8.maven.docker.access.ExecException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)50 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)38 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)24 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)14 Test (org.junit.Test)13 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)10 File (java.io.File)9 IOException (java.io.IOException)9 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)8 ArrayList (java.util.ArrayList)8 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 MojoFailureException (org.apache.maven.plugin.MojoFailureException)7 Before (org.junit.Before)7 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)6 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)6 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)5 Expectations (mockit.Expectations)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 ExecException (io.fabric8.maven.docker.access.ExecException)4 WatchImageConfiguration (io.fabric8.maven.docker.config.WatchImageConfiguration)4