Search in sources :

Example 1 with AssemblyFileEntry

use of org.eclipse.jkube.kit.common.AssemblyFileEntry in project jkube by eclipse.

the class AssemblyManager method createAssemblyArchive.

private void createAssemblyArchive(AssemblyConfiguration assemblyConfig, JKubeConfiguration params, BuildDirs buildDirs, ArchiveCompression compression, Map<Assembly, List<AssemblyFileEntry>> layers) throws IOException {
    if (layers.isEmpty()) {
        return;
    }
    AssemblyConfigurationSource source = new AssemblyConfigurationSource(params, buildDirs, assemblyConfig);
    JKubeBuildTarArchiver jkubeTarArchiver = new JKubeBuildTarArchiver();
    AssemblyMode buildMode = assemblyConfig.getMode();
    try {
        layers.values().stream().flatMap(List::stream).filter(afe -> StringUtils.isNotBlank(afe.getFileMode())).forEach(jkubeTarArchiver::setFileMode);
        jkubeTarArchiver.createArchive(source.getOutputDirectory(), buildDirs, compression);
    } catch (IOException e) {
        String error = "Failed to create assembly for docker image " + " (with mode '" + buildMode + "'): " + e.getMessage() + ".";
        if (params.getProject().getArtifact() == 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 IOException(error, e);
    }
}
Also used : JKubeProjectUtil(org.eclipse.jkube.kit.common.util.JKubeProjectUtil) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) AssemblyConfigurationUtils.createDockerFileBuilder(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder) DockerFileUtil(org.eclipse.jkube.kit.build.api.helper.DockerFileUtil) StringUtils(org.apache.commons.lang3.StringUtils) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) LinkedHashMap(java.util.LinkedHashMap) AssemblyConfigurationUtils.getJKubeAssemblyFiles(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) Nonnull(javax.annotation.Nonnull) JavaProject(org.eclipse.jkube.kit.common.JavaProject) DockerFileBuilder(org.eclipse.jkube.kit.config.image.build.DockerFileBuilder) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Properties(java.util.Properties) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) Files(java.nio.file.Files) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) BufferedWriter(java.io.BufferedWriter) AssemblyFileUtils.getAssemblyFileOutputDirectory(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.getAssemblyFileOutputDirectory) FileWriter(java.io.FileWriter) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) IOException(java.io.IOException) AssemblyConfigurationUtils.getJKubeAssemblyFileSets(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets) Collectors(java.util.stream.Collectors) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault) KitLogger(org.eclipse.jkube.kit.common.KitLogger) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assembly(org.eclipse.jkube.kit.common.Assembly) JKubeTarArchiver(org.eclipse.jkube.kit.common.archive.JKubeTarArchiver) Optional(java.util.Optional) AssemblyFileSetUtils.processAssemblyFileSet(org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 2 with AssemblyFileEntry

use of org.eclipse.jkube.kit.common.AssemblyFileEntry in project jkube by eclipse.

the class AssemblyManager method copyFilesToFinalTarballDirectory.

public Map<Assembly, List<AssemblyFileEntry>> copyFilesToFinalTarballDirectory(JKubeConfiguration configuration, BuildDirs buildDirs, AssemblyConfiguration assemblyConfiguration) throws IOException {
    final Map<Assembly, List<AssemblyFileEntry>> entries = new LinkedHashMap<>();
    FileUtil.createDirectory(new File(buildDirs.getOutputDirectory(), assemblyConfiguration.getTargetDir()));
    final List<Assembly> layers = assemblyConfiguration.getProcessedLayers(configuration);
    if (layers.size() > 1 && layers.stream().anyMatch(l -> StringUtils.isBlank(l.getId()))) {
        throw new IllegalStateException("Assemblies with more than one layer require a proper id for each layer");
    }
    for (Assembly layer : layers) {
        entries.put(layer, copyLayerFilesToFinalTarballDirectory(configuration.getProject(), buildDirs, assemblyConfiguration, layer));
    }
    return entries;
}
Also used : JKubeProjectUtil(org.eclipse.jkube.kit.common.util.JKubeProjectUtil) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) AssemblyConfigurationUtils.createDockerFileBuilder(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder) DockerFileUtil(org.eclipse.jkube.kit.build.api.helper.DockerFileUtil) StringUtils(org.apache.commons.lang3.StringUtils) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) LinkedHashMap(java.util.LinkedHashMap) AssemblyConfigurationUtils.getJKubeAssemblyFiles(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) Nonnull(javax.annotation.Nonnull) JavaProject(org.eclipse.jkube.kit.common.JavaProject) DockerFileBuilder(org.eclipse.jkube.kit.config.image.build.DockerFileBuilder) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Properties(java.util.Properties) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) Files(java.nio.file.Files) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) BufferedWriter(java.io.BufferedWriter) AssemblyFileUtils.getAssemblyFileOutputDirectory(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.getAssemblyFileOutputDirectory) FileWriter(java.io.FileWriter) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) IOException(java.io.IOException) AssemblyConfigurationUtils.getJKubeAssemblyFileSets(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets) Collectors(java.util.stream.Collectors) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault) KitLogger(org.eclipse.jkube.kit.common.KitLogger) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assembly(org.eclipse.jkube.kit.common.Assembly) JKubeTarArchiver(org.eclipse.jkube.kit.common.archive.JKubeTarArchiver) Optional(java.util.Optional) AssemblyFileSetUtils.processAssemblyFileSet(org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet) ArrayList(java.util.ArrayList) List(java.util.List) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with AssemblyFileEntry

use of org.eclipse.jkube.kit.common.AssemblyFileEntry in project jkube by eclipse.

the class JibServiceUtil method layers.

@Nonnull
public static List<FileEntriesLayer> layers(BuildDirs buildDirs, Map<Assembly, List<AssemblyFileEntry>> layers) {
    final List<FileEntriesLayer> fileEntriesLayers = new ArrayList<>();
    for (Map.Entry<Assembly, List<AssemblyFileEntry>> layer : layers.entrySet()) {
        final FileEntriesLayer.Builder fel = FileEntriesLayer.builder();
        final String layerId = layer.getKey().getId();
        final Path outputPath;
        if (StringUtils.isBlank(layerId)) {
            outputPath = buildDirs.getOutputDirectory().toPath();
        } else {
            fel.setName(layerId);
            outputPath = new File(buildDirs.getOutputDirectory(), layerId).toPath();
        }
        for (AssemblyFileEntry afe : layer.getValue()) {
            final Path source = afe.getSource().toPath();
            final AbsoluteUnixPath target = AbsoluteUnixPath.get(StringUtils.prependIfMissing(FilenameUtils.separatorsToUnix(outputPath.relativize(afe.getDest().toPath()).normalize().toString()), "/"));
            final FilePermissions permissions = StringUtils.isNotBlank(afe.getFileMode()) ? FilePermissions.fromOctalString(StringUtils.right(afe.getFileMode(), 3)) : DEFAULT_FILE_PERMISSIONS_PROVIDER.get(source, target);
            fel.addEntry(source, target, permissions);
        }
        fileEntriesLayers.add(fel.build());
    }
    return fileEntriesLayers;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) Nonnull(javax.annotation.Nonnull)

Example 4 with AssemblyFileEntry

use of org.eclipse.jkube.kit.common.AssemblyFileEntry in project jkube by eclipse.

the class JibServiceUtilTest method layers_withMultipleLayers_shouldReturnTransformedLayers.

@Test
public void layers_withMultipleLayers_shouldReturnTransformedLayers() throws IOException {
    // Given
    final BuildDirs buildDirs = new BuildDirs("layers-test", JKubeConfiguration.builder().outputDirectory("target/docker").project(JavaProject.builder().baseDirectory(temporaryFolder.getRoot()).build()).build());
    final Map<Assembly, List<AssemblyFileEntry>> originalLayers = new LinkedHashMap<>();
    originalLayers.put(Assembly.builder().id("layer-1").build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.1.txt").toFile()).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.2.txt").toFile()).build()));
    originalLayers.put(Assembly.builder().build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.1.txt")).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.2.txt")).build()));
    // Creates a denormalized path in JDK 8
    originalLayers.put(Assembly.builder().id("jkube-generated-layer-final-artifact").build(), Collections.singletonList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("jkube-generated-layer-final-artifact").resolve("deployments").resolve(".").resolve("edge.case").toFile()).build()));
    // When
    final List<FileEntriesLayer> result = JibServiceUtil.layers(buildDirs, originalLayers);
    // Then
    assertThat(result).hasSize(3).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "layer-1").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l1.1.txt", "/l1.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l2.1.txt", "/l2.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "jkube-generated-layer-final-artifact").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/deployments/edge.case")).extracting(FileEntriesLayer::getName).containsExactly("layer-1", "", "jkube-generated-layer-final-artifact");
}
Also used : Arrays(java.util.Arrays) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Port(com.google.cloud.tools.jib.api.buildplan.Port) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) Arguments(org.eclipse.jkube.kit.config.image.build.Arguments) JavaProject(org.eclipse.jkube.kit.common.JavaProject) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) List(java.util.List) Rule(org.junit.Rule) Assembly(org.eclipse.jkube.kit.common.Assembly) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) JibServiceUtil.containerFromImageConfiguration(org.eclipse.jkube.kit.service.jib.JibServiceUtil.containerFromImageConfiguration) Collections(java.util.Collections) Mocked(mockit.Mocked) Verifications(mockit.Verifications) TemporaryFolder(org.junit.rules.TemporaryFolder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) List(java.util.List) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with AssemblyFileEntry

use of org.eclipse.jkube.kit.common.AssemblyFileEntry in project jkube by eclipse.

the class AssemblyFileSetUtils method processAssemblyFileSet.

/**
 * Will copy files from the provided <code>baseDirectory</code> into
 * <code>outputDirectory/assemblyConfiguration.targetDir[/layer.directory]</code>
 * considering the inclusion and exclusion rules defined in the provided {@link AssemblyFileSet}.
 *
 * @param baseDirectory directory from where to resolve source files.
 * @param outputDirectory directory where files should be output.
 * @param assemblyFileSet fileSet to process.
 * @param layer the layer to which fileSet belongs to.
 * @param assemblyConfiguration configuration for assembly.
 * @return List containing the copied {@link AssemblyFileEntry} for the processed {@link AssemblyFileSet}
 * @throws IOException in case something goes wrong when performing File operations.
 */
@SuppressWarnings("squid:S3864")
@Nonnull
public static List<AssemblyFileEntry> processAssemblyFileSet(File baseDirectory, File outputDirectory, AssemblyFileSet assemblyFileSet, Assembly layer, AssemblyConfiguration assemblyConfiguration) throws IOException {
    final File sourceDirectory = resolveSourceDirectory(baseDirectory, assemblyFileSet);
    Objects.requireNonNull(assemblyConfiguration.getTargetDir(), "Assembly Configuration target dir is required");
    if (!sourceDirectory.exists()) {
        return Collections.emptyList();
    }
    final File targetDirectory;
    if (StringUtils.isNotBlank(layer.getId())) {
        targetDirectory = new File(new File(outputDirectory, layer.getId()), assemblyConfiguration.getTargetDir());
    } else {
        targetDirectory = new File(outputDirectory, assemblyConfiguration.getTargetDir());
    }
    final File destinationDirectory;
    if (assemblyFileSet.getOutputDirectory() == null) {
        destinationDirectory = new File(targetDirectory, sourceDirectory.getName());
    } else if (assemblyFileSet.getOutputDirectory().isAbsolute()) {
        destinationDirectory = assemblyFileSet.getOutputDirectory();
    } else if (assemblyFileSet.getOutputDirectory().getPath().equals(PATH_TO_SELF)) {
        destinationDirectory = targetDirectory;
    } else {
        destinationDirectory = targetDirectory.toPath().resolve(assemblyFileSet.getOutputDirectory().getPath()).toFile();
    }
    final List<String> includes = Optional.ofNullable(assemblyFileSet.getIncludes()).filter(i -> !i.isEmpty()).orElse(Collections.singletonList(PATH_TO_SELF));
    final List<AssemblyFileEntry> allEntries = new ArrayList<>();
    for (String include : includes) {
        final String effectiveInclude = isSelfPath(include) ? "**" : include;
        allEntries.addAll(processInclude(sourceDirectory.toPath(), effectiveInclude, destinationDirectory.toPath(), assemblyFileSet));
    }
    return allEntries;
}
Also used : AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Assembly(org.eclipse.jkube.kit.common.Assembly) PathMatcher(java.nio.file.PathMatcher) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) Optional(java.util.Optional) Path(java.nio.file.Path) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) FileSystems(java.nio.file.FileSystems) LinkedHashSet(java.util.LinkedHashSet) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) ArrayList(java.util.ArrayList) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Aggregations

File (java.io.File)32 AssemblyFileEntry (org.eclipse.jkube.kit.common.AssemblyFileEntry)32 AssemblyFileSet (org.eclipse.jkube.kit.common.AssemblyFileSet)25 Test (org.junit.Test)24 Assembly (org.eclipse.jkube.kit.common.Assembly)21 AssemblyConfiguration (org.eclipse.jkube.kit.common.AssemblyConfiguration)20 AssemblyFileSetUtils.processAssemblyFileSet (org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet)16 AssemblyFile (org.eclipse.jkube.kit.common.AssemblyFile)8 List (java.util.List)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 IOException (java.io.IOException)5 Collectors (java.util.stream.Collectors)5 Nonnull (javax.annotation.Nonnull)5 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)5 Collections (java.util.Collections)4 AssemblyConfigurationUtils.createDockerFileBuilder (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder)4 AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault)4 AssemblyConfigurationUtils.getJKubeAssemblyFileSets (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets)4 AssemblyConfigurationUtils.getJKubeAssemblyFiles (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles)4