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);
}
}
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;
}
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;
}
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");
}
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;
}
Aggregations