Skip to content

Commit

Permalink
Normalizes podman vs. podman.exe and Podman Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
Karm committed May 31, 2023
1 parent 68e7c31 commit 196f78d
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import io.smallrye.common.os.OS;
import io.smallrye.config.SmallRyeConfig;

public final class ContainerRuntimeUtil {

private static final Logger log = Logger.getLogger(ContainerRuntimeUtil.class);
private static final String CONTAINER_EXECUTABLE = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class)
.getOptionalValue("quarkus.native.container-runtime", String.class).orElse(null);
private static final Pattern PODMAN_PATTERN = Pattern.compile("^podman(?:\\.exe)? version.*", Pattern.DOTALL);

/**
* Static variable is not used because the class gets loaded by different classloaders at
Expand All @@ -44,7 +45,7 @@ public static ContainerRuntime detectContainerRuntime(boolean required) {
return containerRuntime;
}

ContainerRuntime containerRuntimeEnvironment = getContainerRuntimeEnvironment();
final ContainerRuntime containerRuntimeEnvironment = getContainerRuntimeEnvironment();
if (containerRuntimeEnvironment == ContainerRuntime.UNAVAILABLE) {
storeContainerRuntimeInSystemProperty(ContainerRuntime.UNAVAILABLE);

Expand Down Expand Up @@ -83,7 +84,7 @@ private static ContainerRuntime getContainerRuntimeEnvironment() {
}
if (CONTAINER_EXECUTABLE.trim().equalsIgnoreCase("podman")) {
podmanVersionOutput = getVersionOutputFor(ContainerRuntime.PODMAN);
podmanAvailable = podmanVersionOutput.startsWith("podman version");
podmanAvailable = PODMAN_PATTERN.matcher(podmanVersionOutput).matches();
if (podmanAvailable) {
return ContainerRuntime.PODMAN;
}
Expand All @@ -96,19 +97,16 @@ private static ContainerRuntime getContainerRuntimeEnvironment() {
dockerAvailable = dockerVersionOutput.contains("Docker version");
if (dockerAvailable) {
// Check if "docker" is an alias to "podman"
if (dockerVersionOutput.startsWith("podman version") ||
dockerVersionOutput.startsWith("podman.exe version")) {
if (PODMAN_PATTERN.matcher(dockerVersionOutput).matches()) {
return ContainerRuntime.PODMAN;
}
return ContainerRuntime.DOCKER;
}
podmanVersionOutput = getVersionOutputFor(ContainerRuntime.PODMAN);
podmanAvailable = podmanVersionOutput.startsWith("podman version") ||
podmanVersionOutput.startsWith("podman.exe version");
podmanAvailable = PODMAN_PATTERN.matcher(podmanVersionOutput).matches();
if (podmanAvailable) {
return ContainerRuntime.PODMAN;
}

return ContainerRuntime.UNAVAILABLE;
}

Expand All @@ -130,14 +128,13 @@ private static ContainerRuntime loadContainerRuntimeFromSystemProperty() {
return null;
}

ContainerRuntime containerRuntime = ContainerRuntime.valueOf(runtime);

if (containerRuntime == null) {
log.warnf("System property %s contains an unknown value %s. Ignoring it.",
CONTAINER_RUNTIME_SYS_PROP, runtime);
try {
return ContainerRuntime.valueOf(runtime);
} catch (IllegalArgumentException | NullPointerException e) {
log.warnf("System property %s contains an unknown value %s. Ignoring it. %s",
CONTAINER_RUNTIME_SYS_PROP, runtime, e);
}

return containerRuntime;
return null;
}

private static void storeContainerRuntimeInSystemProperty(ContainerRuntime containerRuntime) {
Expand Down Expand Up @@ -225,7 +222,7 @@ public enum ContainerRuntime {
private final boolean rootless;

ContainerRuntime(String executableName, boolean rootless) {
this.executableName = executableName + (OS.current() == OS.WINDOWS ? ".exe" : "");
this.executableName = executableName;
this.rootless = rootless;
}

Expand Down

0 comments on commit 196f78d

Please sign in to comment.