Search in sources :

Example 1 with AssemblyConfiguration

use of io.fabric8.maven.docker.config.AssemblyConfiguration 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, MojoParameters params, final BuildImageConfiguration buildConfig, Logger log, ArchiverCustomizer finalCustomizer) throws MojoExecutionException {
    final BuildDirs buildDirs = createBuildDirs(imageName, params);
    final AssemblyConfiguration assemblyConfig = buildConfig.getAssemblyConfiguration();
    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(assemblyConfig)) {
        createAssemblyArchive(assemblyConfig, 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(dockerFile.getParentFile());
                    addDockerIgnoreIfPresent(fileSet);
                    // 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(assemblyConfig)) {
                        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, assemblyConfig);
            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 (assemblyConfig != null) {
            AssemblyConfiguration.PermissionMode mode = assemblyConfig.getPermissions();
            if (mode == AssemblyConfiguration.PermissionMode.exec || mode == AssemblyConfiguration.PermissionMode.auto && EnvUtil.isWindows()) {
                archiveCustomizers.add(new AllFilesExecCustomizer(log));
            }
        }
        if (finalCustomizer != null) {
            archiveCustomizers.add(finalCustomizer);
        }
        return createBuildTarBall(buildDirs, archiveCustomizers, assemblyConfig, 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 2 with AssemblyConfiguration

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

the class DockerAssemblyManager method verifyGivenDockerfile.

// visible for testing
void verifyGivenDockerfile(File dockerFile, BuildImageConfiguration buildConfig, FixedStringSearchInterpolator interpolator, Logger log) throws IOException {
    AssemblyConfiguration assemblyConfig = buildConfig.getAssemblyConfiguration();
    if (assemblyConfig == null) {
        return;
    }
    String name = assemblyConfig.getName();
    for (String keyword : new String[] { "ADD", "COPY" }) {
        List<String[]> lines = DockerFileUtil.extractLines(dockerFile, keyword, interpolator);
        for (String[] line : lines) {
            if (!line[0].startsWith("#")) {
                // Skip command flags like --chown
                int i;
                for (i = 1; i < line.length; i++) {
                    String component = line[i];
                    if (!component.startsWith("--")) {
                        break;
                    }
                }
                // contains an ADD/COPY ... targetDir .... All good.
                if (i < line.length && line[i].contains(name)) {
                    return;
                }
            }
        }
    }
    log.warn("Dockerfile %s does not contain an ADD or COPY directive to include assembly created at %s. Ignoring assembly.", dockerFile.getPath(), name);
}
Also used : AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration)

Example 3 with AssemblyConfiguration

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

the class BuildService method extractBaseFromConfiguration.

private String extractBaseFromConfiguration(BuildImageConfiguration buildConfig) {
    String fromImage;
    fromImage = buildConfig.getFrom();
    if (fromImage == null) {
        AssemblyConfiguration assemblyConfig = buildConfig.getAssemblyConfiguration();
        if (assemblyConfig == null) {
            fromImage = DockerAssemblyManager.DEFAULT_DATA_BASE_IMAGE;
        }
    }
    return fromImage;
}
Also used : AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration)

Example 4 with AssemblyConfiguration

use of io.fabric8.maven.docker.config.AssemblyConfiguration 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);
    } 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.plugin.assembly.format.AssemblyFormattingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiveCreationException(org.apache.maven.plugin.assembly.archive.ArchiveCreationException) AssemblyMode(io.fabric8.maven.docker.config.AssemblyMode) InvalidAssemblerConfigurationException(org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException) File(java.io.File) Assembly(org.apache.maven.plugin.assembly.model.Assembly)

Example 5 with AssemblyConfiguration

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

Aggregations

AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)6 File (java.io.File)3 AssemblyMode (io.fabric8.maven.docker.config.AssemblyMode)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Assembly (org.apache.maven.plugin.assembly.model.Assembly)2 TarArchiver (org.codehaus.plexus.archiver.tar.TarArchiver)2 DefaultFileSet (org.codehaus.plexus.archiver.util.DefaultFileSet)2 Test (org.junit.Test)2 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InvalidAssemblerConfigurationException (org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException)1 ArchiveCreationException (org.apache.maven.plugin.assembly.archive.ArchiveCreationException)1 AssemblyFormattingException (org.apache.maven.plugin.assembly.format.AssemblyFormattingException)1 DefaultArchivedFileSet (org.codehaus.plexus.archiver.util.DefaultArchivedFileSet)1 FixedStringSearchInterpolator (org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator)1