Search in sources :

Example 1 with Logger

use of io.fabric8.maven.docker.util.Logger 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 Logger

use of io.fabric8.maven.docker.util.Logger 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 Logger

use of io.fabric8.maven.docker.util.Logger in project docker-maven-plugin by fabric8io.

the class DockerAccessFactory method getDefaultDockerHostProviders.

/**
 * Return a list of providers which could delive connection parameters from
 * calling external commands. For this plugin this is docker-machine, but can be overridden
 * to add other config options, too.
 *
 * @return list of providers or <code>null</code> if none are applicable
 */
private List<DockerConnectionDetector.DockerHostProvider> getDefaultDockerHostProviders(DockerAccessContext dockerAccessContext, Logger log) {
    DockerMachineConfiguration config = dockerAccessContext.getMachine();
    if (dockerAccessContext.isSkipMachine()) {
        config = null;
    } else if (config == null) {
        Properties projectProps = dockerAccessContext.getProjectProperties();
        if (projectProps.containsKey(DockerMachineConfiguration.DOCKER_MACHINE_NAME_PROP)) {
            config = new DockerMachineConfiguration(projectProps.getProperty(DockerMachineConfiguration.DOCKER_MACHINE_NAME_PROP), projectProps.getProperty(DockerMachineConfiguration.DOCKER_MACHINE_AUTO_CREATE_PROP));
        }
    }
    List<DockerConnectionDetector.DockerHostProvider> ret = new ArrayList<>();
    ret.add(new DockerMachine(log, config));
    return ret;
}
Also used : DockerMachine(io.fabric8.maven.docker.access.DockerMachine) DockerMachineConfiguration(io.fabric8.maven.docker.config.DockerMachineConfiguration) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Example 4 with Logger

use of io.fabric8.maven.docker.util.Logger in project docker-maven-plugin by fabric8io.

the class EcrExtendedAuthTest method testHeaders.

@Test
public void testHeaders() throws ParseException {
    EcrExtendedAuth eea = new EcrExtendedAuth(logger, "123456789012.dkr.ecr.eu-west-1.amazonaws.com");
    AuthConfig localCredentials = new AuthConfig("username", "password", null, null);
    Date signingTime = AwsSigner4Request.TIME_FORMAT.parse("20161217T211058Z");
    HttpPost request = eea.createSignedRequest(localCredentials, signingTime);
    assertEquals("ecr.eu-west-1.amazonaws.com", request.getFirstHeader("host").getValue());
    assertEquals("20161217T211058Z", request.getFirstHeader("X-Amz-Date").getValue());
    assertEquals("AWS4-HMAC-SHA256 Credential=username/20161217/eu-west-1/ecr/aws4_request, SignedHeaders=content-type;host;x-amz-target, Signature=1bab0f5c269debe913e532011d5d192b190bb4c55d3de1bc1506eefb93e058e1", request.getFirstHeader("Authorization").getValue());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Date(java.util.Date) Test(org.junit.Test)

Example 5 with Logger

use of io.fabric8.maven.docker.util.Logger in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManagerTest method testCopyChownValidVerifyGivenDockerfile.

@Test
public void testCopyChownValidVerifyGivenDockerfile(@Injectable final Logger logger) throws IOException {
    BuildImageConfiguration buildConfig = createBuildConfig();
    assemblyManager.verifyGivenDockerfile(new File(getClass().getResource("/docker/Dockerfile_assembly_verify_copy_chown_valid.test").getPath()), buildConfig, createInterpolator(buildConfig), logger);
    new Verifications() {

        {
            logger.warn(anyString, (Object[]) any);
            times = 0;
        }
    };
}
Also used : Verifications(mockit.Verifications) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)30 File (java.io.File)11 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 URL (java.net.URL)8 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)6 ResourceValidator (io.fabric8.maven.core.util.validator.ResourceValidator)6 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)6 Logger (io.fabric8.maven.docker.util.Logger)6 Util.readAsString (io.fabric8.arquillian.utils.Util.readAsString)5 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 Pod (io.fabric8.kubernetes.api.model.Pod)5 OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)5 Expectations (mockit.Expectations)5 Logger (io.fabric8.arquillian.kubernetes.log.Logger)4 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 BuildService (io.fabric8.maven.core.service.BuildService)4 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)4