Search in sources :

Example 1 with QueryService

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

the class StopMojo method getNetworksToRemove.

private Set<Network> getNetworksToRemove(QueryService queryService, PomLabel pomLabel) throws DockerAccessException {
    if (!autoCreateCustomNetworks) {
        return Collections.emptySet();
    }
    Set<Network> customNetworks = new HashSet<>();
    Set<Network> networks = queryService.getNetworks();
    for (ImageConfiguration image : getResolvedImages()) {
        final NetworkConfig config = image.getRunConfiguration().getNetworkingConfig();
        if (config.isCustomNetwork()) {
            Network network = getNetworkByName(networks, config.getCustomNetwork());
            if (network != null) {
                customNetworks.add(network);
                for (Container container : getContainersToStop(queryService, image)) {
                    if (!shouldStopContainer(container, pomLabel, image)) {
                        // it's sill in use don't collect it
                        customNetworks.remove(network);
                    }
                }
            }
        }
    }
    return customNetworks;
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) Network(io.fabric8.maven.docker.model.Network) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) HashSet(java.util.HashSet)

Example 2 with QueryService

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

the class StartMojo method prepareStart.

// Prepare start like creating custom networks, auto pull images, map aliases and return the list of images
// to start in the correct order
private Queue<ImageConfiguration> prepareStart(ServiceHub hub, QueryService queryService, RunService runService, Set<String> imageAliases) throws DockerAccessException, MojoExecutionException {
    final Queue<ImageConfiguration> imagesWaitingToStart = new ArrayDeque<>();
    for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, getResolvedImages())) {
        final ImageConfiguration imageConfig = (ImageConfiguration) resolvable;
        // Still to check: How to work with linking, volumes, etc ....
        // String imageName = new ImageName(imageConfig.getName()).getFullNameWithTag(registry);
        RegistryService registryService = hub.getRegistryService();
        pullImage(registryService, imageConfig, pullRegistry);
        RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
        NetworkConfig config = runConfig.getNetworkingConfig();
        List<String> bindMounts = extractBindMounts(runConfig.getVolumeConfiguration());
        List<VolumeConfiguration> volumes = getVolumes();
        if (!bindMounts.isEmpty() && volumes != null) {
            runService.createVolumesAsPerVolumeBinds(hub, bindMounts, volumes);
        }
        if (autoCreateCustomNetworks && config.isCustomNetwork()) {
            runService.createCustomNetworkIfNotExistant(config.getCustomNetwork());
        }
        imagesWaitingToStart.add(imageConfig);
        updateAliasesSet(imageAliases, imageConfig.getAlias());
    }
    return imagesWaitingToStart;
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) StartOrderResolver(io.fabric8.maven.docker.util.StartOrderResolver) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) RegistryService(io.fabric8.maven.docker.service.RegistryService) RunVolumeConfiguration(io.fabric8.maven.docker.config.RunVolumeConfiguration) VolumeConfiguration(io.fabric8.maven.docker.config.VolumeConfiguration) ArrayDeque(java.util.ArrayDeque) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration)

Example 3 with QueryService

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

the class CopyMojo method copyFromTemporaryContainers.

private void copyFromTemporaryContainers(DockerAccess dockerAccess, RunService runService, RegistryService registryService, ArchiveService archiveService, QueryService queryService, GavLabel gavLabel) throws IOException, MojoExecutionException {
    List<ImageConfiguration> imageConfigurations = getResolvedImages();
    for (ImageConfiguration imageConfiguration : imageConfigurations) {
        CopyConfiguration copyConfiguration = imageConfiguration.getCopyConfiguration();
        String imageName = imageConfiguration.getName();
        if (isEmpty(copyConfiguration)) {
            log.debug("Copy configuration is not defined for %s image, skipping coping", imageName);
            continue;
        }
        try (ContainerRemover containerRemover = new ContainerRemover(log, runService, removeVolumes)) {
            String containerId = createContainer(runService, registryService, imageConfiguration, gavLabel);
            containerRemover.setContainerId(containerId);
            log.debug("Created %s container from %s image", containerId, imageName);
            copy(dockerAccess, archiveService, containerId, imageName, copyConfiguration);
        }
    }
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) CopyConfiguration(io.fabric8.maven.docker.config.CopyConfiguration)

Example 4 with QueryService

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

the class CopyMojo method getContainersForImage.

private List<Container> getContainersForImage(QueryService queryService, ImageConfiguration imageConfiguration) throws IOException, MojoExecutionException {
    String imageName = imageConfiguration.getName();
    String copyNamePattern = imageConfiguration.getCopyNamePattern();
    Matcher containerNameMatcher = copyNamePattern == null ? null : getContainerNameMatcher(copyNamePattern, COPY_NAME_PATTERN_CONFIG);
    if (copyAll) {
        if (containerNameMatcher == null) {
            return queryService.getContainersForImage(imageName, true);
        }
        return getContainersForPattern(queryService, true, null, containerNameMatcher, COPY_NAME_PATTERN_CONFIG);
    }
    Container latestContainer;
    if (containerNameMatcher == null) {
        latestContainer = queryService.getLatestContainerForImage(imageName, true);
    } else {
        List<Container> matchingContainers = getContainersForPattern(queryService, true, null, containerNameMatcher, COPY_NAME_PATTERN_CONFIG);
        latestContainer = queryService.getLatestContainer(matchingContainers);
    }
    return latestContainer == null ? Collections.emptyList() : Collections.singletonList(latestContainer);
}
Also used : Container(io.fabric8.maven.docker.model.Container) Matcher(java.util.regex.Matcher)

Example 5 with QueryService

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

the class CopyMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws IOException, MojoExecutionException {
    DockerAccess dockerAccess = hub.getDockerAccess();
    RunService runService = hub.getRunService();
    ArchiveService archiveService = hub.getArchiveService();
    QueryService queryService = hub.getQueryService();
    GavLabel gavLabel = getGavLabel();
    if (createContainers) {
        RegistryService registryService = hub.getRegistryService();
        log.debug("Copy mojo is invoked standalone, copying from new temporary containers");
        copyFromTemporaryContainers(dockerAccess, runService, registryService, archiveService, queryService, gavLabel);
    } else if (invokedTogetherWithDockerStart()) {
        log.debug("Copy mojo is invoked together with start mojo, copying from containers created by start mojo");
        copyFromStartedContainers(dockerAccess, runService, archiveService, gavLabel);
    } else {
        log.debug("Copy mojo is invoked standalone, copying from existing containers");
        copyFromExistingContainers(dockerAccess, archiveService, queryService);
    }
}
Also used : DockerAccess(io.fabric8.maven.docker.access.DockerAccess) RunService(io.fabric8.maven.docker.service.RunService) GavLabel(io.fabric8.maven.docker.util.GavLabel) ArchiveService(io.fabric8.maven.docker.service.ArchiveService) QueryService(io.fabric8.maven.docker.service.QueryService) RegistryService(io.fabric8.maven.docker.service.RegistryService)

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