Restore support for docker compose versions earlier than 2.24
Fixes gh-43710
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user