Skip to content

Commit

Permalink
Merge pull request #179 from swissquote/slim-dependencies
Browse files Browse the repository at this point in the history
Remove some dependencies
  • Loading branch information
sonthanh authored Feb 8, 2024
2 parents 722053d + 83b0eee commit 0c8e443
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 171 deletions.
20 changes: 0 additions & 20 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@
<artifactId>maven-model</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
Expand All @@ -56,11 +41,6 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>net.jodah</groupId>
<artifactId>failsafe</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-exec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang3.SystemUtils;

import com.github.swissquote.carnotzet.core.maven.CarnotzetModuleCoordinates;
import com.github.swissquote.carnotzet.core.maven.MavenDependencyResolver;
import com.github.swissquote.carnotzet.core.maven.ResourcesManager;
import com.google.common.base.Strings;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
Expand Down Expand Up @@ -79,6 +76,18 @@ public class Carnotzet {
@Getter
private final Boolean supportLegacyDnsNames;

private static final boolean IS_OS_WINDOWS = isWindows();

private static boolean isWindows() {
try {
String osName = System.getProperty("os.name");
return osName.startsWith("Windows");
}
catch (SecurityException e) {
return false;
}
}

@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public Carnotzet(CarnotzetConfig config) {
log.debug("Creating new carnotzet with config [{}]", config);
Expand Down Expand Up @@ -156,13 +165,13 @@ public Carnotzet(CarnotzetConfig config) {
public List<CarnotzetModule> getModules() {
if (modules == null) {
modules = resolver.resolve(config.getTopLevelModuleId(), failOnDependencyCycle);
if (!SystemUtils.IS_OS_WINDOWS || !getResourcesFolder().resolve("expanded-jars").toFile().exists()) {
if (!IS_OS_WINDOWS || !getResourcesFolder().resolve("expanded-jars").toFile().exists()) {
log.debug("extracting resources");
resourceManager.extractResources(modules);
}
log.debug("computing service ids");
modules = computeServiceIds(modules);
if (!SystemUtils.IS_OS_WINDOWS || !getResourcesFolder().resolve("resolved").toFile().exists()) {
if (!IS_OS_WINDOWS || !getResourcesFolder().resolve("resolved").toFile().exists()) {
resourceManager.resolveResources(modules);
}
log.debug("configuring modules");
Expand Down Expand Up @@ -299,7 +308,7 @@ public String computeImageName(CarnotzetModule module, Map<String, String> prope
.map(m -> m.getId().getVersion())
.findFirst()
.orElse("");
if (Strings.isNullOrEmpty(myModuleVersion)) {
if (myModuleVersion.trim().isEmpty()) {
// complain nicely with a list of modules
String modulesList = modules.stream()
.map(CarnotzetModule::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Map;
Expand All @@ -33,8 +32,6 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -128,17 +125,32 @@ private DistributionManifestV2 getDistributionManifest(ImageRef imageRef) {
}
}

private String downloadWithRetry(URL url, String accept, String auth) throws IOException {
RetryPolicy<Object> retryPolicy = new RetryPolicy<>()
.handle(Exception.class)
.withDelay(Duration.ofSeconds(Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_RETRY_DELAY_SECONDS, "1"))))
.withMaxRetries(Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_DOWNLOAD_RETRIES, "0")))
.onRetry((o) -> log.info("Download attempt failed: {} : Retrying... ", o.getLastFailure().toString()))
.onFailure((o) -> {
log.error("Download failed: {} ", o.getFailure().toString());
throw new IllegalStateException(o.getFailure());
});
return Failsafe.with(retryPolicy).get(() -> downloadWithoutRetry(url, accept, auth));
private String downloadWithRetry(URL url, String accept, String auth) {
int retryTimes = Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_DOWNLOAD_RETRIES, "0"));
int retryAfter = Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_RETRY_DELAY_SECONDS, "1"));

while (retryTimes > 0) {
try {
return downloadWithoutRetry(url, accept, auth);
}
catch (Exception e) {
--retryTimes;
if (retryTimes > 0) {
log.info("Download attempt failed: {} : Retrying... ", e.getMessage());
try {
Thread.sleep(retryAfter);
}
catch (InterruptedException e0) {
throw new RuntimeException(e0);
}
} else {
log.error("Download failed: {} ", e);
throw e;
}
}
}

throw new RuntimeException("Download failed after " + retryTimes + " tries");
}

private String downloadWithoutRetry(URL url, String accept, String auth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@
import java.util.Arrays;
import java.util.StringJoiner;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@EqualsAndHashCode
@ToString
public class ImageRef {

private static final String DEFAULT_REGISTRY = "docker.io";
private static final String DEFAULT_REGISTRY_URL = "https://index.docker.io";
private static final String DEFAULT_TAG = "latest";

@Getter
@ToString.Exclude
private final String registryUrl;
private final String registry;
@Getter
private final String image;
private final String tag;

Expand Down Expand Up @@ -73,10 +76,6 @@ private static boolean isRegistry(String part) {
return part.contains(".");
}

public String getImage() {
return image;
}

// The image tag, or null if not set.
public String getTag() {
if (tag == null) {
Expand All @@ -85,26 +84,17 @@ public String getTag() {
return tag;
}

// Hostname/ip address and port of the registry.
/**
* Hostname/ip address and port of the registry.
*/

public String getRegistryName() {
return registry;
}

public String getRegistryUrl() {
return registryUrl;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("registry", registry)
.add("image", image)
.add("tag", tag)
.toString();
}

// registry server address given first part of image.
@VisibleForTesting
/**
* registry server address given first part of image.
*/
static String parseRegistryUrl(final String url) {
if ("docker.io".equals(url) || "index.docker.io".equals(url)) {
return DEFAULT_REGISTRY_URL;
Expand All @@ -127,8 +117,8 @@ public String getImageName() {

StringJoiner joiner = new StringJoiner("/");
Arrays.stream(splitted)
.filter(token -> token.indexOf(".") < 0)
.forEach(token -> joiner.add(token));
.filter(token -> !token.contains("."))
.forEach(joiner::add);

return joiner.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationOutputHandler;
Expand Down Expand Up @@ -221,7 +220,7 @@ private void writeDotConfigCache() {
}
Path localRepoPathCache = userConfigFolder.resolve("m2LocalRepoPath");
try {
FileUtils.writeStringToFile(localRepoPathCache.toFile(), this.localRepoPath.toString(), "UTF-8");
Files.write(localRepoPathCache, this.localRepoPath.toString().getBytes(StandardCharsets.UTF_8));
}
catch (IOException e) {
log.warn("Could not write file [{}]", localRepoPathCache);
Expand All @@ -238,7 +237,7 @@ private void readUserConfigCache() {
Path localRepoPathCache = getUserConfigFolder().resolve("m2LocalRepoPath");
if (localRepoPathCache.toFile().exists()) {
try {
this.localRepoPath = Paths.get(FileUtils.readFileToString(localRepoPathCache.toFile()));
this.localRepoPath = Paths.get(new String(Files.readAllBytes(localRepoPathCache), StandardCharsets.UTF_8));
}
catch (IOException e) {
log.warn("unable to read file [{}]", localRepoPathCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.function.BiPredicate;

import org.apache.commons.io.FileUtils;
import java.util.stream.Stream;

import com.github.swissquote.carnotzet.core.CarnotzetDefinitionException;
import com.github.swissquote.carnotzet.core.CarnotzetModule;
Expand Down Expand Up @@ -56,6 +57,21 @@ public Path getOwnModuleResourcesPath(CarnotzetModule module) {
return expandedJars.resolve(module.getName());
}

private static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation)
throws IOException {
try (Stream<Path> walk = Files.walk(Paths.get(sourceDirectoryLocation))) {
for (Path source : (Iterable<Path>) walk::iterator) {
Path destination = Paths.get(destinationDirectoryLocation, source.toString()
.substring(sourceDirectoryLocation.length()));

// Don't try to overwrite directories that already exist
if (!(Files.isDirectory(source) && Files.exists(destination))) {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}

/**
* Extract all jar resources to a single directory with the following structure :<br>
* resourcesRoot/expanded-jars/module1/...<br>
Expand All @@ -67,7 +83,10 @@ public void extractResources(List<CarnotzetModule> modules) {

try {
log.debug("Extracting jars resources to [{}]", resourcesRoot);
FileUtils.deleteDirectory(resourcesRoot.toFile());
Files.walk(resourcesRoot)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
if (!expandedJars.toFile().mkdirs()) {
throw new CarnotzetDefinitionException("Could not create directory [" + resourcesRoot + "]");
}
Expand All @@ -82,8 +101,9 @@ public void extractResources(List<CarnotzetModule> modules) {
if (module.getName().equals(topLevelModuleName)
&& topLevelModuleResourcesPath != null
&& topLevelModuleResourcesPath.toFile().exists()) {
FileUtils.copyDirectory(topLevelModuleResourcesPath.toFile(),
expandedJars.resolve(topLevelModuleName).toFile());

copyDirectory(topLevelModuleResourcesPath.toString(),
expandedJars.resolve(topLevelModuleName).toString());
}
}
}
Expand Down Expand Up @@ -133,7 +153,7 @@ private void copyOwnResources(List<CarnotzetModule> processedModules, CarnotzetM
if (Files.isRegularFile(source)) {
Files.copy(source, resolvedModulePath.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} else if (Files.isDirectory(source)) {
FileUtils.copyDirectory(source.toFile(), resolvedModulePath.resolve(source.getFileName()).toFile());
copyDirectory(source.toString(), resolvedModulePath.resolve(source.getFileName()).toString());
}
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.io.Reader;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
* Parses the text ouptut of "mvn dependency:tree -Dverbose" into a java object tree.
* Copied from https://github.com/adutra/maven-dependency-tree-parser/ (apache 2) and modified to keep only text parsing
Expand Down Expand Up @@ -101,6 +99,34 @@ private int getArtifactIndex(final String line) {
return -1;
}

private boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}

private String substringAfter(String str, String separator) {
if (isEmpty(str)) {
return str;
} else if (separator == null) {
return "";
} else {
int pos = str.indexOf(separator);
return pos == -1 ? "" : str.substring(pos + separator.length());
}
}

private String substringBefore(String str, String separator) {
if (!isEmpty(str) && separator != null) {
if (separator.isEmpty()) {
return "";
} else {
int pos = str.indexOf(separator);
return pos == -1 ? str : str.substring(0, pos);
}
} else {
return str;
}
}

/**
* When doing an install at the same time on a multi-module project, one can get this kind of output:
* <pre>
Expand Down Expand Up @@ -134,7 +160,7 @@ protected String extractActiveProjectArtifact() {
boolean projectLine = artifactFound && tempLine.contains("project: ");
if (artifactLine || projectLine) {
if (tempLine.contains("artifact = ") && !tempLine.contains("active project artifact:")) {
artifact = StringUtils.substringBefore(StringUtils.substringAfter(tempLine, "artifact = "), ";");
artifact = substringBefore(substringAfter(tempLine, "artifact = "), ";");
artifactFound = true;
}
this.lineIndex++;
Expand Down
Loading

0 comments on commit 0c8e443

Please sign in to comment.