Restore support for docker compose versions earlier than 2.24
Fixes gh-43710
This commit is contained in:
parent
9dea1e1736
commit
962514601f
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -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;
|
||||
|
@ -53,6 +54,8 @@ class DockerCli {
|
|||
|
||||
private final Set<String> activeProfiles;
|
||||
|
||||
private final ComposeVersion composeVersion;
|
||||
|
||||
/**
|
||||
* Create a new {@link DockerCli} instance.
|
||||
* @param workingDirectory the working directory or {@code null}
|
||||
|
@ -65,6 +68,7 @@ class DockerCli {
|
|||
(key) -> new DockerCommands(this.processRunner));
|
||||
this.composeFile = composeFile;
|
||||
this.activeProfiles = (activeProfiles != null) ? activeProfiles : Collections.emptySet();
|
||||
this.composeVersion = ComposeVersion.of(this.dockerCommands.get(Type.DOCKER_COMPOSE).version());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,7 +79,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);
|
||||
|
@ -90,9 +94,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());
|
||||
if (this.composeFile != null) {
|
||||
result.add("--file");
|
||||
result.add(this.composeFile.toString());
|
||||
|
@ -121,20 +125,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?",
|
||||
|
@ -149,13 +153,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
|
||||
|
@ -165,7 +169,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(
|
||||
|
@ -174,7 +178,7 @@ class DockerCli {
|
|||
}
|
||||
}
|
||||
|
||||
List<String> get(Type type) {
|
||||
DockerCommand get(Type type) {
|
||||
return switch (type) {
|
||||
case DOCKER -> this.dockerCommand;
|
||||
case DOCKER_COMPOSE -> this.dockerComposeCommand;
|
||||
|
@ -183,4 +187,8 @@ class DockerCli {
|
|||
|
||||
}
|
||||
|
||||
private record DockerCommand(String version, List<String> command) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -218,4 +233,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);
|
||||
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");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
|
@ -79,7 +91,7 @@ class DockerCliCommandTests {
|
|||
void composeDown() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeDown(Duration.ofSeconds(1));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("down", "--timeout", "1");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("down", "--timeout", "1");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
|
@ -88,7 +100,7 @@ class DockerCliCommandTests {
|
|||
DockerCliCommand<?> command = new DockerCliCommand.ComposeStart(LogLevel.INFO);
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getLogLevel()).isEqualTo(LogLevel.INFO);
|
||||
assertThat(command.getCommand()).containsExactly("start");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("start");
|
||||
assertThat(command.deserialize("[]")).isNull();
|
||||
}
|
||||
|
||||
|
@ -96,8 +108,23 @@ class DockerCliCommandTests {
|
|||
void composeStop() {
|
||||
DockerCliCommand<?> command = new DockerCliCommand.ComposeStop(Duration.ofSeconds(1));
|
||||
assertThat(command.getType()).isEqualTo(DockerCliCommand.Type.DOCKER_COMPOSE);
|
||||
assertThat(command.getCommand()).containsExactly("stop", "--timeout", "1");
|
||||
assertThat(command.getCommand(COMPOSE_VERSION)).containsExactly("stop", "--timeout", "1");
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue