Search in sources :

Example 1 with ServiceHub

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

the class BuildMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
    if (skipBuild) {
        return;
    }
    List<ImageConfiguration> resolvedImages = getResolvedImages();
    if (resolvedImages.isEmpty()) {
        // No Configuration found, so build one up dynamically.
        if (imageName == null) {
            /*
                 * Image name defaults to:
                 *     `${project.groupId}/${project.artifactId}:${project.version}`
                 */
            imageName = project.getGroupId() + "/" + project.getArtifactId() + ":" + project.getVersion();
        }
        ImageConfiguration aDefaultImageConfig = ImageConfiguration.getDefaultImageConfiguration(imageName, project.getBasedir().getAbsolutePath());
        processImageConfig(hub, aDefaultImageConfig);
        return;
    } else {
        // Iterate over all the ImageConfigurations and process one by one
        for (ImageConfiguration imageConfig : resolvedImages) {
            processImageConfig(hub, imageConfig);
        }
    }
}
Also used : BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration)

Example 2 with ServiceHub

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

the class WatchMojo method customizeConfig.

@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
    try {
        Fabric8ServiceHub serviceHub = getFabric8ServiceHub();
        GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:watch").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).artifactResolver(serviceHub.getArtifactResolverService()).build();
        return GeneratorManager.generate(configs, ctx, false);
    } catch (MojoExecutionException e) {
        throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Fabric8ServiceHub(io.fabric8.maven.core.service.Fabric8ServiceHub) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext)

Example 3 with ServiceHub

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

the class JibBuildService method build.

public void build(String jibImageFormat, ImageConfiguration imageConfig, RegistryService.RegistryConfig registryConfig) throws MojoExecutionException {
    try {
        log.info("[[B]]JIB[[B]] image build started");
        if (imageConfig.getBuildConfiguration().isDockerFileMode()) {
            throw new MojoExecutionException("Dockerfile mode is not supported with JIB build strategy");
        }
        prependRegistry(imageConfig, mojoParameters.getProject().getProperties().getProperty(PUSH_REGISTRY));
        BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), mojoParameters);
        final Credential pullRegistryCredential = getRegistryCredentials(registryConfig, false, imageConfig, log);
        final JibContainerBuilder containerBuilder = containerFromImageConfiguration(jibImageFormat, imageConfig, pullRegistryCredential);
        File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log);
        for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
            // TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
            // the call to DockerAssemblyManager.getInstance().createDockerTarArchive should not be necessary,
            // files should be added using the AssemblyFileEntry list. DockerAssemblyManager, should provide
            // a common way to achieve this so that both the tar builder and any other builder could get a hold of
            // archive customizers, file entries, etc.
            AssemblyFiles assemblyFiles = serviceHub.getDockerAssemblyManager().getAssemblyFiles(imageConfig.getName(), assemblyConfiguration, mojoParameters, log);
            final Map<File, AssemblyFiles.Entry> files = assemblyFiles.getUpdatedEntriesAndRefresh().stream().collect(Collectors.toMap(AssemblyFiles.Entry::getDestFile, Function.identity(), (oldV, newV) -> newV));
            JibServiceUtil.copyToContainer(containerBuilder, buildDirs.getOutputDirectory(), buildDirs.getOutputDirectory().getAbsolutePath(), files);
        }
        JibServiceUtil.buildContainer(containerBuilder, TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), log);
        log.info(" %s successfully built", dockerTarArchive.getAbsolutePath());
    } catch (Exception ex) {
        throw new MojoExecutionException("Error when building JIB image", ex);
    }
}
Also used : Arrays(java.util.Arrays) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) ImageName(io.fabric8.maven.docker.util.ImageName) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Logger(io.fabric8.maven.docker.util.Logger) Function(java.util.function.Function) Credential(com.google.cloud.tools.jib.api.Credential) JibServiceUtil(io.fabric8.maven.docker.util.JibServiceUtil) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) Map(java.util.Map) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) AuthConfigFactory(io.fabric8.maven.docker.util.AuthConfigFactory) Collection(java.util.Collection) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) TarImage(com.google.cloud.tools.jib.api.TarImage) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ArchiveCompression(io.fabric8.maven.docker.config.ArchiveCompression) JibServiceUtil.getBaseImage(io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage) EnvUtil(io.fabric8.maven.docker.util.EnvUtil) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) Credential(com.google.cloud.tools.jib.api.Credential) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 4 with ServiceHub

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

the class SaveMojo method executeInternal.

@Override
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {
    List<ImageConfiguration> images = getResolvedImages();
    if (skipSaveFor(images)) {
        return;
    }
    ImageConfiguration image = getImageToSave(images);
    String imageName = image.getName();
    String fileName = getFileName(imageName);
    ensureSaveDir(fileName);
    log.info("Saving image %s to %s", imageName, fileName);
    if (!serviceHub.getQueryService().hasImage(imageName)) {
        throw new MojoExecutionException("No image " + imageName + " exists");
    }
    long time = System.currentTimeMillis();
    ArchiveCompression compression = ArchiveCompression.fromFileName(fileName);
    serviceHub.getDockerAccess().saveImage(imageName, fileName, compression);
    log.info("%s: Saved image to %s in %s", imageName, fileName, EnvUtil.formatDurationTill(time));
    String classifier = getClassifier(image);
    if (classifier != null) {
        projectHelper.attachArtifact(project, compression.getFileSuffix(), classifier, new File(fileName));
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ArchiveCompression(io.fabric8.maven.docker.config.ArchiveCompression) File(java.io.File)

Example 5 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub 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)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)11 File (java.io.File)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)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 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)4 Test (org.junit.Test)4 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)3 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)3 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)3 JibBuildService (io.fabric8.maven.docker.service.JibBuildService)3 GavLabel (io.fabric8.maven.docker.util.GavLabel)3 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)3 IOException (java.io.IOException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2