Search in sources :

Example 1 with RegistryConfig

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

the class RegistryService method pushImages.

/**
 * Push a set of images to a registry
 *
 * @param imageConfigs images to push (but only if they have a build configuration)
 * @param retries how often to retry
 * @param registryConfig a global registry configuration
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
public void pushImages(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig) throws DockerAccessException, MojoExecutionException {
    for (ImageConfiguration imageConfig : imageConfigs) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        String name = imageConfig.getName();
        if (buildConfig != null) {
            String configuredRegistry = EnvUtil.fistRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
            AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
            long start = System.currentTimeMillis();
            docker.pushImage(name, authConfig, configuredRegistry, retries);
            log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
            for (String tag : imageConfig.getBuildConfiguration().getTags()) {
                if (tag != null) {
                    docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
                }
            }
        }
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 2 with RegistryConfig

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

the class RegistryService method pullImageWithPolicy.

/**
 * Check an image, and, if <code>autoPull</code> is set to true, fetch it. Otherwise if the image
 * is not existent, throw an error
 * @param registryConfig registry configuration
 *
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
public void pullImageWithPolicy(String image, ImagePullManager pullManager, RegistryConfig registryConfig, boolean hasImage) throws DockerAccessException, MojoExecutionException {
    // Already pulled, so we don't need to take care
    if (pullManager.hasAlreadyPulled(image)) {
        return;
    }
    // Check if a pull is required
    if (!imageRequiresPull(hasImage, pullManager.getImagePullPolicy(), image)) {
        return;
    }
    ImageName imageName = new ImageName(image);
    long time = System.currentTimeMillis();
    String actualRegistry = EnvUtil.fistRegistryOf(imageName.getRegistry(), registryConfig.getRegistry());
    docker.pullImage(imageName.getFullName(), createAuthConfig(false, null, actualRegistry, registryConfig), actualRegistry);
    log.info("Pulled %s in %s", imageName.getFullName(), EnvUtil.formatDurationTill(time));
    pullManager.pulled(image);
    if (actualRegistry != null && !imageName.hasRegistry()) {
        // If coming from a registry which was not contained in the original name, add a tag from the
        // full name with the registry to the short name with no-registry.
        docker.tag(imageName.getFullName(actualRegistry), image, false);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName)

Example 3 with RegistryConfig

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

the class AbstractDockerMojo method pullImage.

protected void pullImage(RegistryService registryService, ImageConfiguration imageConfig, String pullRegistry) throws MojoExecutionException, DockerAccessException {
    String imageName = imageConfig.getName();
    RunImageConfiguration runConfiguration = imageConfig.getRunConfiguration();
    ImagePullManager pullManager = getImagePullManager(determinePullPolicy(runConfiguration), autoPull);
    RegistryConfig registryConfig = getRegistryConfig(pullRegistry);
    registryService.pullImageWithPolicy(imageName, pullManager, registryConfig, imageConfig.getBuildConfiguration());
}
Also used : RegistryConfig(io.fabric8.maven.docker.service.RegistryService.RegistryConfig) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ImagePullManager(io.fabric8.maven.docker.service.ImagePullManager)

Example 4 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig 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 5 with RegistryConfig

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

the class JibBuildService method push.

public void push(Collection<ImageConfiguration> imageConfigs, int retries, RegistryService.RegistryConfig registryConfig, boolean skipTag) throws MojoExecutionException {
    try {
        for (ImageConfiguration imageConfiguration : imageConfigs) {
            prependRegistry(imageConfiguration, registryConfig.getRegistry());
            log.info("This push refers to: %s", imageConfiguration.getName());
            JibServiceUtil.jibPush(imageConfiguration, getRegistryCredentials(registryConfig, true, imageConfiguration, log), getBuildTarArchive(imageConfiguration, mojoParameters), log);
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Error when push JIB image", ex);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)7 ImageName (io.fabric8.maven.docker.util.ImageName)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)5 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 Credential (com.google.cloud.tools.jib.api.Credential)4 Test (org.junit.Test)3 RegistryConfig (io.fabric8.maven.docker.service.RegistryService.RegistryConfig)2 AuthConfigFactory (io.fabric8.maven.docker.util.AuthConfigFactory)2 JibServiceUtil.containerFromImageConfiguration (io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration)2 File (java.io.File)2 Verifications (mockit.Verifications)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 TarImage (com.google.cloud.tools.jib.api.TarImage)1 CreateImageOptions (io.fabric8.maven.docker.access.CreateImageOptions)1 AssemblyFiles (io.fabric8.maven.docker.assembly.AssemblyFiles)1 BuildDirs (io.fabric8.maven.docker.assembly.BuildDirs)1 ArchiveCompression (io.fabric8.maven.docker.config.ArchiveCompression)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)1