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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeVersion;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -34,11 +35,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class DockerCliCommandTests {
|
||||
|
||||
private static final ComposeVersion COMPOSE_VERSION = ComposeVersion.of("2.31.0");
|
||||
|
||||
@Test
|
||||
void context() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.Context();
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER);
|
||||
assertThat(command.getCommand()).containsExactly("context", "ls", "--format={{ json . }}");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("context", "ls", "--format={{ json . }}");
|
||||
assertThat(command.deserialize("[]")).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@@ -46,7 +49,8 @@ class DockerCliCommandTests {
|
||||
void inspect() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.Inspect(List.of("123", "345"));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER);
|
||||
assertThat(command.getCommand()).containsExactly("inspect", "--format={{ json . }}", "123", "345");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("inspect", "--format={{ json . }}", "123",
|
||||
"345");
|
||||
assertThat(command.deserialize("[]")).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@@ -54,7 +58,7 @@ class DockerCliCommandTests {
|
||||
void composeConfig() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeConfig();
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("config", "--format=json");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("config", "--format=json");
|
||||
assertThat(command.deserialize("{}")).isInstanceOf(DockerCliComposeConfigResponse.class);
|
||||
}
|
||||
|
||||
@@ -62,7 +66,15 @@ class DockerCliCommandTests {
|
||||
void composePs() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposePs();
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("ps", "--orphans=false", "--format=json");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("ps", "--orphans=false", "--format=json");
|
||||
assertThat(command.deserialize("[]")).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void composePsWhenLessThanV224() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposePs();
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand(ComposeVersion.of("2.23"))).containsExactly("ps", "--format=json");
|
||||
assertThat(command.deserialize("[]")).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@@ -71,7 +83,7 @@ class DockerCliCommandTests {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeUp(LogLevel.INFO, List.of("--renew-anon-volumes"));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getLogLevel()).isEqualTo(LogLevel.INFO);
|
||||
assertThat(command.getCommand()).containsExactly("up", "--no-color", "--detach", "--wait",
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("up", "--no-color", "--detach", "--wait",
|
||||
"--renew-anon-volumes");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
@@ -81,7 +93,7 @@ class DockerCliCommandTests {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeDown(Duration.ofSeconds(1),
|
||||
List.of("--remove-orphans"));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("down", "--timeout", "1", "--remove-orphans");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("down", "--timeout", "1", "--remove-orphans");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
@@ -90,7 +102,7 @@ class DockerCliCommandTests {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeStart(LogLevel.INFO, List.of("--dry-run"));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getLogLevel()).isEqualTo(LogLevel.INFO);
|
||||
assertThat(command.getCommand()).containsExactly("start", "--dry-run");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("start", "--dry-run");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
@@ -98,8 +110,23 @@ class DockerCliCommandTests {
|
||||
void composeStop() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeStop(Duration.ofSeconds(1), List.of("--dry-run"));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("stop", "--timeout", "1", "--dry-run");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("stop", "--timeout", "1", "--dry-run");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void composeVersionTests() {
|
||||
ComposeVersion version = ComposeVersion.of("2.31.0-desktop");
|
||||
assertThat(version.major()).isEqualTo(2);
|
||||
assertThat(version.minor()).isEqualTo(31);
|
||||
assertThat(version.isLessThan(1, 0)).isFalse();
|
||||
assertThat(version.isLessThan(2, 0)).isFalse();
|
||||
assertThat(version.isLessThan(2, 31)).isFalse();
|
||||
assertThat(version.isLessThan(2, 32)).isTrue();
|
||||
assertThat(version.isLessThan(3, 0)).isTrue();
|
||||
ComposeVersion versionWithPrefix = ComposeVersion.of("v2.31.0-desktop");
|
||||
assertThat(versionWithPrefix.major()).isEqualTo(2);
|
||||
assertThat(versionWithPrefix.minor()).isEqualTo(31);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user