Search in sources :

Example 1 with ArchiverCustomizer

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

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

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

the class OpenshiftBuildServiceTest method checkTarPackage.

@Test
public void checkTarPackage() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image).buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()).env(Collections.singletonMap("FOO", "BAR")).build()).build();
            service.createBuildArchive(imageWithEnv);
            final List<ArchiverCustomizer> customizer = new LinkedList<>();
            new Verifications() {

                {
                    archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer));
                    assertTrue(customizer.size() == 1);
                }
            };
            customizer.get(0).customize(tarArchiver);
            final List<File> file = new LinkedList<>();
            new Verifications() {

                {
                    String path;
                    tarArchiver.addFile(withCapture(file), path = withCapture());
                    assertEquals(".s2i/environment", path);
                }
            };
            assertEquals(1, file.size());
            List<String> lines;
            try (FileReader reader = new FileReader(file.get(0))) {
                lines = IOUtils.readLines(reader);
            }
            assertTrue(lines.contains("FOO=BAR"));
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : BuildService(io.fabric8.maven.core.service.BuildService) ArchiverCustomizer(io.fabric8.maven.docker.assembly.ArchiverCustomizer) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) IOException(java.io.IOException) Verifications(mockit.Verifications) LinkedList(java.util.LinkedList) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)3 IOException (java.io.IOException)3 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)2 ArchiverCustomizer (io.fabric8.maven.docker.assembly.ArchiverCustomizer)2 TarArchiver (org.codehaus.plexus.archiver.tar.TarArchiver)2 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 BuildService (io.fabric8.maven.core.service.BuildService)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)1 BuildBuilder (io.fabric8.openshift.api.model.BuildBuilder)1 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)1 ImageStreamBuilder (io.fabric8.openshift.api.model.ImageStreamBuilder)1 ImageStreamStatusBuilder (io.fabric8.openshift.api.model.ImageStreamStatusBuilder)1 NamedTagEventListBuilder (io.fabric8.openshift.api.model.NamedTagEventListBuilder)1 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)1 FileReader (java.io.FileReader)1