Skip to content

Commit

Permalink
Restore support for docker compose versions earlier than 2.24
Browse files Browse the repository at this point in the history
Fixes gh-43710
  • Loading branch information
philwebb committed Jan 8, 2025
1 parent 9dea1e1 commit 9625146
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,6 +28,7 @@
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;
Expand All @@ -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}
Expand All @@ -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());
}

/**
Expand All @@ -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);
Expand All @@ -90,9 +94,9 @@ private Consumer<String> createOutputConsumer(LogLevel logLevel) {

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());
Expand Down Expand Up @@ -121,20 +125,20 @@ DockerComposeFile getDockerComposeFile() {
*/
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?",
Expand All @@ -149,13 +153,13 @@ private List<String> getDockerCommand(ProcessRunner processRunner) {
}
}

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
Expand All @@ -165,7 +169,7 @@ private List<String> getDockerComposeCommand(ProcessRunner processRunner) {
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(
Expand All @@ -174,7 +178,7 @@ private List<String> getDockerComposeCommand(ProcessRunner processRunner) {
}
}

List<String> get(Type type) {
DockerCommand get(Type type) {
return switch (type) {
case DOCKER -> this.dockerCommand;
case DOCKER_COMPOSE -> this.dockerComposeCommand;
Expand All @@ -183,4 +187,8 @@ List<String> get(Type type) {

}

private record DockerCommand(String version, List<String> command) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
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;

Expand All @@ -42,19 +44,24 @@ 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);
}

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() {
Expand All @@ -65,8 +72,8 @@ LogLevel getLogLevel() {
return this.logLevel;
}

List<String> getCommand() {
return this.command;
List<String> getCommand(ComposeVersion composeVersion) {
return this.command.apply(composeVersion);
}

@SuppressWarnings("unchecked")
Expand All @@ -90,7 +97,8 @@ public boolean equals(Object obj) {
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;
}

Expand Down Expand Up @@ -150,9 +158,16 @@ static final class ComposeConfig extends DockerCliCommand<DockerCliComposeConfig
*/
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;
}

}
Expand Down Expand Up @@ -218,4 +233,31 @@ enum Type {

}

/**
* 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;
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand All @@ -34,35 +35,46 @@
*/
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);
}

@Test
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);
}

@Test
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);
}

@Test
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);
}

Expand All @@ -71,15 +83,15 @@ void composeUp() {
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();
}

@Test
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();
}

Expand All @@ -88,16 +100,31 @@ void composeStart() {
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();
}

@Test
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);
}

}

0 comments on commit 9625146

Please sign in to comment.