Search in sources :

Example 1 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver 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 TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method createChangedFilesTarBall.

private File createChangedFilesTarBall(File archive, File archiveDir) throws MojoExecutionException {
    try {
        TarArchiver archiver = (TarArchiver) archiverManager.getArchiver("tar");
        archiver.setLongfile(TarLongFileMode.posix);
        archiver.addFileSet(DefaultFileSet.fileSet(archiveDir));
        archiver.setDestFile(archive);
        archiver.createArchive();
        return archive;
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("No archiver for type 'tar' found", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Cannot create archive " + archive, e);
    } catch (RuntimeException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 3 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method getS2ICustomizer.

private ArchiverCustomizer getS2ICustomizer(ImageConfiguration imageConfiguration) throws Fabric8ServiceException {
    try {
        if (imageConfiguration.getBuildConfiguration() != null && imageConfiguration.getBuildConfiguration().getEnv() != null) {
            String fileName = IoUtil.sanitizeFileName("s2i-env-" + imageConfiguration.getName());
            final File environmentFile = new File(config.getBuildDirectory(), fileName);
            try (PrintWriter out = new PrintWriter(new FileWriter(environmentFile))) {
                for (Map.Entry<String, String> e : imageConfiguration.getBuildConfiguration().getEnv().entrySet()) {
                    out.println(e.getKey() + "=" + e.getValue());
                }
            }
            return new ArchiverCustomizer() {

                @Override
                public TarArchiver customize(TarArchiver tarArchiver) throws IOException {
                    tarArchiver.addFile(environmentFile, ".s2i/environment");
                    return tarArchiver;
                }
            };
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new Fabric8ServiceException("Unable to add environment variables to the S2I build archive", e);
    }
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) ArchiverCustomizer(io.fabric8.maven.docker.assembly.ArchiverCustomizer) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 4 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project maven-plugins by apache.

the class DefaultAssemblyArchiver method createTarArchiver.

protected Archiver createTarArchiver(final String format, final TarLongFileMode tarLongFileMode) throws NoSuchArchiverException {
    final TarArchiver tarArchiver = (TarArchiver) archiverManager.getArchiver("tar");
    final int index = format.indexOf('.');
    if (index >= 0) {
        TarArchiver.TarCompressionMethod tarCompressionMethod;
        // TODO: this should accept gz and bz2 as well so we can skip
        // TODO: over the switch
        final String compression = format.substring(index + 1);
        if ("gz".equals(compression)) {
            tarCompressionMethod = TarArchiver.TarCompressionMethod.gzip;
        } else if ("bz2".equals(compression)) {
            tarCompressionMethod = TarArchiver.TarCompressionMethod.bzip2;
        } else if ("xz".equals(compression)) {
            tarCompressionMethod = TarArchiver.TarCompressionMethod.xz;
        } else if ("snappy".equals(compression)) {
            tarCompressionMethod = TarArchiver.TarCompressionMethod.snappy;
        } else {
            // TODO: better handling
            throw new IllegalArgumentException("Unknown compression format: " + compression);
        }
        tarArchiver.setCompression(tarCompressionMethod);
    } else if ("tgz".equals(format)) {
        tarArchiver.setCompression(TarArchiver.TarCompressionMethod.gzip);
    } else if ("tbz2".equals(format)) {
        tarArchiver.setCompression(TarArchiver.TarCompressionMethod.bzip2);
    } else if ("txz".equals(format)) {
        tarArchiver.setCompression(TarArchiver.TarCompressionMethod.xz);
    }
    tarArchiver.setLongfile(tarLongFileMode);
    return tarArchiver;
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver)

Example 5 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project docker-maven-plugin by fabric8io.

the class AllFilesExecCustomizer method customize.

@Override
public TarArchiver customize(TarArchiver archiver) throws IOException {
    log.warn("/--------------------- SECURITY WARNING ---------------------\\");
    log.warn("|You are building a Docker image with normalized permissions.|");
    log.warn("|All files and directories added to build context will have  |");
    log.warn("|'-rwxr-xr-x' permissions. It is recommended to double check |");
    log.warn("|and reset permissions for sensitive files and directories.  |");
    log.warn("\\------------------------------------------------------------/");
    TarArchiver newArchiver = new TarArchiver();
    newArchiver.setDestFile(archiver.getDestFile());
    newArchiver.setLongfile(TarLongFileMode.posix);
    ResourceIterator resources = archiver.getResources();
    while (resources.hasNext()) {
        ArchiveEntry ae = resources.next();
        String fileName = ae.getName();
        PlexusIoResource resource = ae.getResource();
        String name = StringUtils.replace(fileName, File.separatorChar, '/');
        // See docker source:
        // https://github.com/docker/docker/blob/3d13fddd2bc4d679f0eaa68b0be877e5a816ad53/pkg/archive/archive_windows.go#L45
        int mode = ae.getMode() & 0777;
        int newMode = mode;
        newMode &= 0755;
        newMode |= 0111;
        if (newMode != mode) {
            log.debug("Changing permissions of '%s' from %o to %o.", name, mode, newMode);
        }
        newArchiver.addResource(resource, name, newMode);
    }
    archiver = newArchiver;
    return archiver;
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) PlexusIoResource(org.codehaus.plexus.components.io.resources.PlexusIoResource) ArchiveEntry(org.codehaus.plexus.archiver.ArchiveEntry) ResourceIterator(org.codehaus.plexus.archiver.ResourceIterator)

Aggregations

TarArchiver (org.codehaus.plexus.archiver.tar.TarArchiver)8 File (java.io.File)5 IOException (java.io.IOException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)2 DefaultFileSet (org.codehaus.plexus.archiver.util.DefaultFileSet)2 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)1 ArchiverCustomizer (io.fabric8.maven.docker.assembly.ArchiverCustomizer)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 AssemblyMode (io.fabric8.maven.docker.config.AssemblyMode)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Artifact (org.apache.maven.artifact.Artifact)1 DependencyGraphBuilderException (org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException)1 ArchiveEntry (org.codehaus.plexus.archiver.ArchiveEntry)1 Archiver (org.codehaus.plexus.archiver.Archiver)1