-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Detect container runtime when using Jib
- Loading branch information
Showing
6 changed files
with
92 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
core/deployment/src/main/java/io/quarkus/deployment/util/ContainerRuntimeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.quarkus.deployment.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
public final class ContainerRuntimeUtil { | ||
|
||
private static final Logger log = Logger.getLogger(ContainerRuntimeUtil.class); | ||
|
||
private ContainerRuntimeUtil() { | ||
} | ||
|
||
/** | ||
* @return {@link ContainerRuntime#DOCKER} if it's available, or {@link ContainerRuntime#PODMAN} | ||
* if the podman | ||
* executable exists in the environment or if the docker executable is an alias to podman | ||
* @throws IllegalStateException if no container runtime was found to build the image | ||
*/ | ||
public static ContainerRuntime detectContainerRuntime() { | ||
// Docker version 19.03.14, build 5eb3275d40 | ||
String dockerVersionOutput = getVersionOutputFor(ContainerRuntime.DOCKER); | ||
boolean dockerAvailable = dockerVersionOutput.contains("Docker version"); | ||
// Check if Podman is installed | ||
// podman version 2.1.1 | ||
String podmanVersionOutput = getVersionOutputFor(ContainerRuntime.PODMAN); | ||
boolean podmanAvailable = podmanVersionOutput.startsWith("podman version"); | ||
if (dockerAvailable) { | ||
// Check if "docker" is an alias to "podman" | ||
if (dockerVersionOutput.equals(podmanVersionOutput)) { | ||
return ContainerRuntime.PODMAN; | ||
} | ||
return ContainerRuntime.DOCKER; | ||
} else if (podmanAvailable) { | ||
return ContainerRuntime.PODMAN; | ||
} else { | ||
throw new IllegalStateException("No container runtime was found to. " | ||
+ "Make sure you have Docker or Podman installed in your environment."); | ||
} | ||
} | ||
|
||
private static String getVersionOutputFor(ContainerRuntime containerRuntime) { | ||
Process versionProcess = null; | ||
try { | ||
ProcessBuilder pb = new ProcessBuilder(containerRuntime.getExecutableName(), "--version") | ||
.redirectErrorStream(true); | ||
versionProcess = pb.start(); | ||
versionProcess.waitFor(); | ||
return new String(FileUtil.readFileContents(versionProcess.getInputStream()), StandardCharsets.UTF_8); | ||
} catch (IOException | InterruptedException e) { | ||
// If an exception is thrown in the process, just return an empty String | ||
log.debugf(e, "Failure to read version output from %s", containerRuntime.getExecutableName()); | ||
return ""; | ||
} finally { | ||
if (versionProcess != null) { | ||
versionProcess.destroy(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Supported Container runtimes | ||
*/ | ||
public enum ContainerRuntime { | ||
DOCKER, | ||
PODMAN; | ||
|
||
public String getExecutableName() { | ||
return this.name().toLowerCase(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters