Skip to content

Commit

Permalink
fix: KarafGenerator update
Browse files Browse the repository at this point in the history
- Created Karaf Quickstart
- Fixed problems with FileSet assembly config to include complete directories
- Upgraded images to use quay.io/jkube/jkube-karaf-binary-s2i

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed May 28, 2020
1 parent 2076887 commit 3658bef
Show file tree
Hide file tree
Showing 20 changed files with 961 additions and 162 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Usage:
* Fix #210: WebAppGenerator\>JettyAppSeverHandler uses quay.io/jkube/jkube-jetty9-binary-s2i as base image
* Fix #211: pom.xml configured runtime mode `<mode>` is considered instead of `<configuredRuntimeMode>`
* Fix #209: WildFlySwarmGenerator includes required env variables + java options
* Fix #285: fix: KarafGenerator update (Created Karaf Quickstart #188, fix FileSet problems, upgraded base images)


### 1.0.0-alpha-3 (2020-05-06)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFileSets;
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFileSetsExcludes;
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFiles;
import static org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.calculateFilePermissions;

/**
* Tool for creating a docker image tar ball including a Dockerfile for building
Expand Down Expand Up @@ -412,43 +413,42 @@ private Map<File, String> copyFilesToFinalTarballDirectory(
return filesToPermissionsMap;
}

private Map<File, String> processJKubeProjectAssemblyFileSet(JavaProject project,
AssemblyFileSet jkubeProjectAssemblyFileSet, BuildDirs buildDirs,
AssemblyConfiguration jkubeProjectAssemblyConfiguration) throws IOException {
private Map<File, String> processJKubeProjectAssemblyFileSet(
JavaProject project, AssemblyFileSet assemblyFileSet, BuildDirs buildDirs,
AssemblyConfiguration assemblyConfiguration) throws IOException {

final Map<File, String> fileToPermissionsMap = new HashMap<>();

final List<String> includes = Optional.ofNullable(jkubeProjectAssemblyFileSet.getIncludes())
final List<String> includes = Optional.ofNullable(assemblyFileSet.getIncludes())
.filter(i -> !i.isEmpty())
.orElse(Collections.singletonList(""));
for (String relativePathInclude : includes) {
final File sourceDirectory = project.getBaseDirectory().toPath()
.resolve(jkubeProjectAssemblyFileSet.getDirectory().toPath())
.resolve(assemblyFileSet.getDirectory().toPath())
.toFile();
final File sourceFile = new File(sourceDirectory, FileUtil.trimWildcardCharactersFromPath(relativePathInclude));
File destParentFile = new File(buildDirs.getOutputDirectory(), jkubeProjectAssemblyConfiguration.getName());
if (jkubeProjectAssemblyFileSet.getOutputDirectory() != null
&& !jkubeProjectAssemblyFileSet.getOutputDirectory().getName().equals(".")) {
destParentFile = buildDirs.getOutputDirectory().toPath()
.resolve(jkubeProjectAssemblyConfiguration.getName())
.resolve(jkubeProjectAssemblyFileSet.getOutputDirectory().toPath())
.toFile();
final File destParentFile = new File(buildDirs.getOutputDirectory(), assemblyConfiguration.getName());
final File destFile;
if (assemblyFileSet.getOutputDirectory() != null) {
if (assemblyFileSet.getOutputDirectory().getPath().equals(".")) {
destFile = new File(destParentFile, sourceFile.getName());
} else if (assemblyFileSet.getOutputDirectory().isAbsolute()) {
destFile = assemblyFileSet.getOutputDirectory();
} else {
destFile = new File(destParentFile, assemblyFileSet.getOutputDirectory().getPath());
}
} else {
destFile = new File(destParentFile, sourceFile.getName());
}
FileUtil.createDirectory(destParentFile);
File destFile = new File(destParentFile, sourceFile.getName());
FileUtil.createDirectory(destFile.getParentFile());

if (sourceFile.exists()) {
if (sourceFile.isDirectory()) {
FileUtil.copyDirectoryIfNotExists(sourceFile, destFile);
} else {
FileUtil.copy(sourceFile, destFile);
}
final String fileMode = jkubeProjectAssemblyFileSet.getFileMode();
if (destFile.isFile()) {
fileToPermissionsMap.put(destFile, fileMode);
} else {
final int directoryCanListPermission = Integer.parseInt(fileMode, 8) + 0111;
fileToPermissionsMap.put(destFile, Integer.toOctalString(directoryCanListPermission));
}
fileToPermissionsMap.putAll(calculateFilePermissions(destFile, assemblyFileSet));
}
}
return fileToPermissionsMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("JavaDoc")
Expand Down Expand Up @@ -88,20 +87,17 @@ public class AssemblyFileSet implements Serializable {
* @return The assembly fileSet file mode value.
*/
private String fileMode;

public void addInclude(String item) {
if (includes == null) {
includes = new ArrayList<>();
}
includes.add(item);
}

public void addExclude(String item) {
if (excludes == null) {
excludes = new ArrayList<>();
}
excludes.add(item);
}
/**
* Similar to a UNIX permission, sets the directory mode of the directories included.
*
* <p> Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1.
*
* <p> For example, the value 0755 translates to User read-write, Group and Other read-only.
*
* @param directoryMode New file mode value for the assembly fileSet.
* @return The assembly fileSet directory mode value.
*/
private String directoryMode;

// Plexus deserialization specific setters
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.archive;

import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.util.FileUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class AssemblyFileSetUtils {

private static final String DIRECTORY_CAN_LIST_PERMISSION = "040111";

private AssemblyFileSetUtils() {}

public static Map<File, String> calculateFilePermissions(File destFile, AssemblyFileSet assemblyFileSet) {
final Map<File, String> ret = new HashMap<>();
if (destFile.isDirectory()) {
final String directoryMode = Optional.ofNullable(assemblyFileSet.getDirectoryMode())
.orElse(DIRECTORY_CAN_LIST_PERMISSION);
ret.put(destFile, directoryMode);
FileUtil.listFilesAndDirsRecursivelyInDirectory(destFile).forEach(f -> {
if (f.isDirectory()) {
ret.put(f, directoryMode);
} else if(f.isFile() && assemblyFileSet.getFileMode() != null) {
ret.put(f, assemblyFileSet.getFileMode());
}
});
} else if (destFile.isFile() && assemblyFileSet.getFileMode() != null) {
ret.put(destFile, assemblyFileSet.getFileMode());
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
/**
* File related methods which cannot be found elsewhere
* @author roland
* @since 23.05.17
*/
public class FileUtil {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.archive;

import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.eclipse.jkube.kit.common.AssemblyFileSet;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.calculateFilePermissions;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;

public class AssemblyFileSetUtilsTest {

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Test
public void calculateFilePermissionsFileWithNoFileMode() throws Exception {
// Given
final AssemblyFileSet afs = AssemblyFileSet.builder().build();
final File aFile = temp.newFile("just-a-file.txt");
// When
final Map<File, String> result = calculateFilePermissions(aFile, afs);
// Then
assertThat(result.entrySet(), empty());
}

@Test
public void calculateFilePermissionsFileWithFileMode() throws Exception {
// Given
final AssemblyFileSet afs = AssemblyFileSet.builder().fileMode("0644").build();
final File aFile = temp.newFile("just-a-file.txt");
// When
final Map<File, String> result = calculateFilePermissions(aFile, afs);
// Then
assertThat(result.entrySet(), hasSize(1));
assertThat(result, hasEntry(aFile, "0644"));
}

@Test
public void calculateFilePermissionsDirectoryWithNoDirectoryMode() throws Exception {
// Given
final AssemblyFileSet afs = AssemblyFileSet.builder().build();
final File aDirectory = temp.newFolder("just-a-directory");
// When
final Map<File, String> result = calculateFilePermissions(aDirectory, afs);
// Then
assertThat(result.entrySet(), hasSize(1));
assertThat(result, hasEntry(aDirectory, "040111"));
}

@Test
public void calculateFilePermissionsDirectoryWithDirectoryMode() throws Exception {
// Given
final AssemblyFileSet afs = AssemblyFileSet.builder().directoryMode("040755").build();
final File aDirectory = temp.newFolder("just-a-directory");
final File aSubdirectory = new File(aDirectory, "subdirectory");
FileUtils.forceMkdir(aSubdirectory);
final File aFile = new File(aDirectory, "file.txt");
assertThat(aFile.createNewFile(), equalTo(true));
// When
final Map<File, String> result = calculateFilePermissions(aDirectory, afs);
// Then
assertThat(result.entrySet(), hasSize(2));
assertThat(result, hasEntry(aDirectory, "040755"));
assertThat(result, hasEntry(aSubdirectory, "040755"));
}

@Test
public void calculateFilePermissionsDirectoryWithDirectoryAndFileMode() throws Exception {
// Given
final AssemblyFileSet afs = AssemblyFileSet.builder().directoryMode("040755").fileMode("0644").build();
final File aDirectory = temp.newFolder("just-a-directory");
final File aSubdirectory = new File(aDirectory, "subdirectory");
FileUtils.forceMkdir(aSubdirectory);
final File aFile = new File(aDirectory, "file.txt");
assertThat(aFile.createNewFile(), equalTo(true));
// When
final Map<File, String> result = calculateFilePermissions(aDirectory, afs);
// Then
assertThat(result.entrySet(), hasSize(3));
assertThat(result, hasEntry(aDirectory, "040755"));
assertThat(result, hasEntry(aSubdirectory, "040755"));
assertThat(result, hasEntry(aFile, "0644"));
}
}
Loading

0 comments on commit 3658bef

Please sign in to comment.