Search in sources :

Example 1 with BuildDirs

use of io.fabric8.maven.docker.assembly.BuildDirs 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 2 with BuildDirs

use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method createDockerTarArchive.

/**
 * Create an docker tar archive from the given configuration which can be send to the Docker host for
 * creating the image.
 *
 * @param imageName Name of the image to create (used for creating build directories)
 * @param params Mojos parameters (used for finding the directories)
 * @param buildConfig configuration for how to build the image
 * @param log Logger used to display warning if permissions are to be normalized
 * @param finalCustomizer finalCustomizer to be applied to the tar archive
 * @return file holding the path to the created assembly tar file
 * @throws MojoExecutionException
 */
public File createDockerTarArchive(String imageName, final MojoParameters params, final BuildImageConfiguration buildConfig, Logger log, ArchiverCustomizer finalCustomizer) throws MojoExecutionException {
    final BuildDirs buildDirs = createBuildDirs(imageName, params);
    final List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAssemblyConfigurations();
    final List<ArchiverCustomizer> archiveCustomizers = new ArrayList<>();
    // Build up assembly. In dockerfile mode this must be added explicitly in the Dockerfile with an ADD
    if (hasAssemblyConfiguration(assemblyConfigurations)) {
        createAssemblyArchives(assemblyConfigurations, params, buildDirs);
    }
    try {
        if (buildConfig.isDockerFileMode()) {
            // Use specified docker directory which must include a Dockerfile.
            final File dockerFile = buildConfig.getAbsoluteDockerFilePath(params);
            if (!dockerFile.exists()) {
                throw new MojoExecutionException("Configured Dockerfile \"" + buildConfig.getDockerFile() + "\" (resolved to \"" + dockerFile + "\") doesn't exist");
            }
            FixedStringSearchInterpolator interpolator = DockerFileUtil.createInterpolator(params, buildConfig.getFilter());
            verifyGivenDockerfile(dockerFile, buildConfig, interpolator, log);
            interpolateDockerfile(dockerFile, buildDirs, interpolator);
            // User dedicated Dockerfile from extra directory
            archiveCustomizers.add(new ArchiverCustomizer() {

                @Override
                public TarArchiver customize(TarArchiver archiver) throws IOException {
                    DefaultFileSet fileSet = DefaultFileSet.fileSet(buildConfig.getAbsoluteContextDirPath(params));
                    addDockerIncludesExcludesIfPresent(fileSet, params);
                    // Exclude non-interpolated dockerfile from source tree
                    // Interpolated Dockerfile is already added as it was created into the output directory when
                    // using dir dir mode
                    excludeDockerfile(fileSet, dockerFile);
                    // directly to docker.tar (as the output builddir is not picked up in archive mode)
                    if (isArchive(assemblyConfigurations)) {
                        String name = dockerFile.getName();
                        archiver.addFile(new File(buildDirs.getOutputDirectory(), name), name);
                    }
                    archiver.addFileSet(fileSet);
                    return archiver;
                }
            });
        } else {
            // Create custom docker file in output dir
            DockerFileBuilder builder = createDockerFileBuilder(buildConfig, assemblyConfigurations);
            builder.write(buildDirs.getOutputDirectory());
            // Add own Dockerfile
            final File dockerFile = new File(buildDirs.getOutputDirectory(), DOCKERFILE_NAME);
            archiveCustomizers.add(new ArchiverCustomizer() {

                @Override
                public TarArchiver customize(TarArchiver archiver) throws IOException {
                    archiver.addFile(dockerFile, DOCKERFILE_NAME);
                    return archiver;
                }
            });
        }
        // If required make all files in the assembly executable
        if (assemblyConfigurations.stream().anyMatch(assemblyConfig -> assemblyConfig.getPermissions() == AssemblyConfiguration.PermissionMode.exec || assemblyConfig.getPermissions() == AssemblyConfiguration.PermissionMode.auto && EnvUtil.isWindows())) {
            archiveCustomizers.add(new AllFilesExecCustomizer(log));
        }
        if (finalCustomizer != null) {
            archiveCustomizers.add(finalCustomizer);
        }
        return createBuildTarBall(buildDirs, archiveCustomizers, assemblyConfigurations, buildConfig.getCompression());
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Cannot create %s in %s", DOCKERFILE_NAME, buildDirs.getOutputDirectory()), e);
    }
}
Also used : FixedStringSearchInterpolator(org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator) DefaultFileSet(org.codehaus.plexus.archiver.util.DefaultFileSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) File(java.io.File)

Example 3 with BuildDirs

use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.

the class DockerAssemblyConfigurationSourceTest method testOutputDirHasImage.

@Test
public void testOutputDirHasImage() {
    String image = "image";
    MojoParameters params = buildParameters(".", "src/docker", "output/docker");
    DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(params, new BuildDirs(image, params), assemblyConfig);
    assertTrue(containsDir(image, source.getOutputDirectory()));
    assertTrue(containsDir(image, source.getWorkingDirectory()));
    assertTrue(containsDir(image, source.getTemporaryRootDirectory()));
}
Also used : MojoParameters(io.fabric8.maven.docker.util.MojoParameters) Test(org.junit.Test)

Example 4 with BuildDirs

use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method getAssemblyFiles.

/**
 * Extract all files with a tracking archiver. These can be used to track changes in the filesystem and triggering
 * a rebuild of the image if needed ('docker:watch')
 */
public AssemblyFiles getAssemblyFiles(String name, BuildImageConfiguration buildConfig, MojoParameters mojoParams, Logger log) throws InvalidAssemblerConfigurationException, ArchiveCreationException, AssemblyFormattingException, MojoExecutionException {
    BuildDirs buildDirs = createBuildDirs(name, mojoParams);
    AssemblyConfiguration assemblyConfig = buildConfig.getAssemblyConfiguration();
    String assemblyName = assemblyConfig.getName();
    DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(mojoParams, buildDirs, assemblyConfig);
    Assembly assembly = getAssemblyConfig(assemblyConfig, source);
    synchronized (trackArchiver) {
        MappingTrackArchiver ta = (MappingTrackArchiver) trackArchiver;
        ta.init(log, assemblyName);
        assembly.setId("tracker");
        assemblyArchiver.createArchive(assembly, assemblyName, "track", source, false);
        return ta.getAssemblyFiles(mojoParams.getSession());
    }
}
Also used : AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) Assembly(org.apache.maven.plugin.assembly.model.Assembly)

Example 5 with BuildDirs

use of io.fabric8.maven.docker.assembly.BuildDirs in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method createAssemblyArchive.

private void createAssemblyArchive(AssemblyConfiguration assemblyConfig, MojoParameters params, BuildDirs buildDirs) throws MojoExecutionException {
    DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(params, buildDirs, assemblyConfig);
    Assembly assembly = getAssemblyConfig(assemblyConfig, source);
    AssemblyMode buildMode = assemblyConfig.getMode();
    File originalArtifactFile = null;
    try {
        originalArtifactFile = ensureThatArtifactFileIsSet(params.getProject());
        assembly.setId("docker");
        assemblyArchiver.createArchive(assembly, assemblyConfig.getName(), buildMode.getExtension(), source, false, null);
    } catch (ArchiveCreationException | AssemblyFormattingException e) {
        String error = "Failed to create assembly for docker image " + " (with mode '" + buildMode + "'): " + e.getMessage() + ".";
        if (params.getProject().getArtifact().getFile() == null) {
            error += " If you include the build artifact please ensure that you have " + "built the artifact before with 'mvn package' (should be available in the target/ dir). " + "Please see the documentation (section \"Assembly\") for more information.";
        }
        throw new MojoExecutionException(error, e);
    } catch (InvalidAssemblerConfigurationException e) {
        throw new MojoExecutionException(assembly, "Assembly is incorrectly configured: " + assembly.getId(), "Assembly: " + assembly.getId() + " is not configured correctly: " + e.getMessage());
    } finally {
        setArtifactFile(params.getProject(), originalArtifactFile);
    }
}
Also used : AssemblyFormattingException(org.apache.maven.plugins.assembly.format.AssemblyFormattingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiveCreationException(org.apache.maven.plugins.assembly.archive.ArchiveCreationException) AssemblyMode(io.fabric8.maven.docker.config.AssemblyMode) InvalidAssemblerConfigurationException(org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException) File(java.io.File) Assembly(org.apache.maven.plugins.assembly.model.Assembly)

Aggregations

AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)3 File (java.io.File)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)2 Credential (com.google.cloud.tools.jib.api.Credential)1 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 TarImage (com.google.cloud.tools.jib.api.TarImage)1 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)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 AssemblyMode (io.fabric8.maven.docker.config.AssemblyMode)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1 AuthConfigFactory (io.fabric8.maven.docker.util.AuthConfigFactory)1 EnvUtil (io.fabric8.maven.docker.util.EnvUtil)1 ImageName (io.fabric8.maven.docker.util.ImageName)1 JibServiceUtil (io.fabric8.maven.docker.util.JibServiceUtil)1 JibServiceUtil.containerFromImageConfiguration (io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration)1 JibServiceUtil.getBaseImage (io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage)1 Logger (io.fabric8.maven.docker.util.Logger)1