Merge branch '3.4.x'
Closes gh-43718
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.function.Consumer;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeVersion;
|
||||
import org.springframework.boot.docker.compose.core.DockerCliCommand.Type;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
@@ -52,6 +53,8 @@ class DockerCli {
|
||||
|
||||
private final DockerComposeOptions dockerComposeOptions;
|
||||
|
||||
private final ComposeVersion composeVersion;
|
||||
|
||||
/**
|
||||
* Create a new {@link DockerCli} instance.
|
||||
* @param workingDirectory the working directory or {@code null}
|
||||
@@ -62,6 +65,7 @@ class DockerCli {
|
||||
this.dockerCommands = dockerCommandsCache.computeIfAbsent(workingDirectory,
|
||||
(key) -> new DockerCommands(this.processRunner));
|
||||
this.dockerComposeOptions = (dockerComposeOptions != null) ? dockerComposeOptions : DockerComposeOptions.none();
|
||||
this.composeVersion = ComposeVersion.of(this.dockerCommands.get(Type.DOCKER_COMPOSE).version());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +76,7 @@ class DockerCli {
|
||||
*/
|
||||
<R> R run(DockerCliCommand<R> dockerCommand) {
|
||||
List<String> command = createCommand(dockerCommand.getType());
|
||||
command.addAll(dockerCommand.getCommand());
|
||||
command.addAll(dockerCommand.getCommand(this.composeVersion));
|
||||
Consumer<String> outputConsumer = createOutputConsumer(dockerCommand.getLogLevel());
|
||||
String json = this.processRunner.run(outputConsumer, command.toArray(new String[0]));
|
||||
return dockerCommand.deserialize(json);
|
||||
@@ -87,9 +91,9 @@ class DockerCli {
|
||||
|
||||
private List<String> createCommand(Type type) {
|
||||
return switch (type) {
|
||||
case DOCKER -> new ArrayList<>(this.dockerCommands.get(type));
|
||||
case DOCKER -> new ArrayList<>(this.dockerCommands.get(type).command());
|
||||
case DOCKER_COMPOSE -> {
|
||||
List<String> result = new ArrayList<>(this.dockerCommands.get(type));
|
||||
List<String> result = new ArrayList<>(this.dockerCommands.get(type).command());
|
||||
DockerComposeFile composeFile = this.dockerComposeOptions.composeFile();
|
||||
if (composeFile != null) {
|
||||
for (File file : composeFile.getFiles()) {
|
||||
@@ -128,20 +132,20 @@ class DockerCli {
|
||||
*/
|
||||
private static class DockerCommands {
|
||||
|
||||
private final List<String> dockerCommand;
|
||||
private final DockerCommand dockerCommand;
|
||||
|
||||
private final List<String> dockerComposeCommand;
|
||||
private final DockerCommand dockerComposeCommand;
|
||||
|
||||
DockerCommands(ProcessRunner processRunner) {
|
||||
this.dockerCommand = getDockerCommand(processRunner);
|
||||
this.dockerComposeCommand = getDockerComposeCommand(processRunner);
|
||||
}
|
||||
|
||||
private List<String> getDockerCommand(ProcessRunner processRunner) {
|
||||
private DockerCommand getDockerCommand(ProcessRunner processRunner) {
|
||||
try {
|
||||
String version = processRunner.run("docker", "version", "--format", "{{.Client.Version}}");
|
||||
logger.trace(LogMessage.format("Using docker %s", version));
|
||||
return List.of("docker");
|
||||
return new DockerCommand(version, List.of("docker"));
|
||||
}
|
||||
catch (ProcessStartException ex) {
|
||||
throw new DockerProcessStartException("Unable to start docker process. Is docker correctly installed?",
|
||||
@@ -156,13 +160,13 @@ class DockerCli {
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getDockerComposeCommand(ProcessRunner processRunner) {
|
||||
private DockerCommand getDockerComposeCommand(ProcessRunner processRunner) {
|
||||
try {
|
||||
DockerCliComposeVersionResponse response = DockerJson.deserialize(
|
||||
processRunner.run("docker", "compose", "version", "--format", "json"),
|
||||
DockerCliComposeVersionResponse.class);
|
||||
logger.trace(LogMessage.format("Using Docker Compose %s", response.version()));
|
||||
return List.of("docker", "compose");
|
||||
return new DockerCommand(response.version(), List.of("docker", "compose"));
|
||||
}
|
||||
catch (ProcessExitException ex) {
|
||||
// Ignore and try docker-compose
|
||||
@@ -172,7 +176,7 @@ class DockerCli {
|
||||
processRunner.run("docker-compose", "version", "--format", "json"),
|
||||
DockerCliComposeVersionResponse.class);
|
||||
logger.trace(LogMessage.format("Using docker-compose %s", response.version()));
|
||||
return List.of("docker-compose");
|
||||
return new DockerCommand(response.version(), List.of("docker-compose"));
|
||||
}
|
||||
catch (ProcessStartException ex) {
|
||||
throw new DockerProcessStartException(
|
||||
@@ -181,7 +185,7 @@ class DockerCli {
|
||||
}
|
||||
}
|
||||
|
||||
List<String> get(Type type) {
|
||||
DockerCommand get(Type type) {
|
||||
return switch (type) {
|
||||
case DOCKER -> this.dockerCommand;
|
||||
case DOCKER_COMPOSE -> this.dockerComposeCommand;
|
||||
@@ -190,6 +194,10 @@ class DockerCli {
|
||||
|
||||
}
|
||||
|
||||
private record DockerCommand(String version, List<String> command) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for Docker Compose.
|
||||
*
|
||||
@@ -198,6 +206,7 @@ class DockerCli {
|
||||
* @param arguments the arguments to pass to Docker Compose
|
||||
*/
|
||||
record DockerComposeOptions(DockerComposeFile composeFile, Set<String> activeProfiles, List<String> arguments) {
|
||||
|
||||
DockerComposeOptions {
|
||||
activeProfiles = (activeProfiles != null) ? activeProfiles : Collections.emptySet();
|
||||
arguments = (arguments != null) ? arguments : Collections.emptyList();
|
||||
@@ -206,6 +215,7 @@ class DockerCli {
|
||||
static DockerComposeOptions none() {
|
||||
return new DockerComposeOptions(null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
|
||||
@@ -42,7 +44,7 @@ abstract sealed class DockerCliCommand<R> {
|
||||
|
||||
private final boolean listResponse;
|
||||
|
||||
private final List<String> command;
|
||||
private final Function<ComposeVersion, List<String>> command;
|
||||
|
||||
private DockerCliCommand(Type type, Class<?> responseType, boolean listResponse, String... command) {
|
||||
this(type, LogLevel.OFF, responseType, listResponse, command);
|
||||
@@ -50,11 +52,16 @@ abstract sealed class DockerCliCommand<R> {
|
||||
|
||||
private DockerCliCommand(Type type, LogLevel logLevel, Class<?> responseType, boolean listResponse,
|
||||
String... command) {
|
||||
this(type, logLevel, responseType, listResponse, (version) -> List.of(command));
|
||||
}
|
||||
|
||||
private DockerCliCommand(Type type, LogLevel logLevel, Class<?> responseType, boolean listResponse,
|
||||
Function<ComposeVersion, List<String>> command) {
|
||||
this.type = type;
|
||||
this.logLevel = logLevel;
|
||||
this.responseType = responseType;
|
||||
this.listResponse = listResponse;
|
||||
this.command = List.of(command);
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
Type getType() {
|
||||
@@ -65,8 +72,8 @@ abstract sealed class DockerCliCommand<R> {
|
||||
return this.logLevel;
|
||||
}
|
||||
|
||||
List<String> getCommand() {
|
||||
return this.command;
|
||||
List<String> getCommand(ComposeVersion composeVersion) {
|
||||
return this.command.apply(composeVersion);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -90,7 +97,8 @@ abstract sealed class DockerCliCommand<R> {
|
||||
boolean result = this.type == other.type;
|
||||
result = result && this.responseType == other.responseType;
|
||||
result = result && this.listResponse == other.listResponse;
|
||||
result = result && this.command.equals(other.command);
|
||||
result = result
|
||||
&& this.command.apply(ComposeVersion.UNKNOWN).equals(other.command.apply(ComposeVersion.UNKNOWN));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -150,9 +158,16 @@ abstract sealed class DockerCliCommand<R> {
|
||||
*/
|
||||
static final class ComposePs extends DockerCliCommand<List<DockerCliComposePsResponse>> {
|
||||
|
||||
private static final List<String> WITHOUT_ORPHANS = List.of("ps", "--format=json");
|
||||
|
||||
private static final List<String> WITH_ORPHANS = List.of("ps", "--orphans=false", "--format=json");
|
||||
|
||||
ComposePs() {
|
||||
super(Type.DOCKER_COMPOSE, DockerCliComposePsResponse.class, true, "ps", "--orphans=false",
|
||||
"--format=json");
|
||||
super(Type.DOCKER_COMPOSE, LogLevel.OFF, DockerCliComposePsResponse.class, true, ComposePs::getPsCommand);
|
||||
}
|
||||
|
||||
private static List<String> getPsCommand(ComposeVersion composeVersion) {
|
||||
return (composeVersion.isLessThan(2, 24)) ? WITHOUT_ORPHANS : WITH_ORPHANS;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -253,4 +268,31 @@ abstract sealed class DockerCliCommand<R> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Docker compose version.
|
||||
*
|
||||
* @param major the major component
|
||||
* @param minor the minor component
|
||||
*/
|
||||
record ComposeVersion(int major, int minor) {
|
||||
|
||||
public static final ComposeVersion UNKNOWN = new ComposeVersion(0, 0);
|
||||
|
||||
boolean isLessThan(int major, int minor) {
|
||||
return major() < major || major() == major && minor() < minor;
|
||||
}
|
||||
|
||||
static ComposeVersion of(String value) {
|
||||
try {
|
||||
value = (!value.toLowerCase(Locale.ROOT).startsWith("v")) ? value : value.substring(1);
|
||||
String[] parts = value.split("\\.");
|
||||
return new ComposeVersion(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user