Polish "Add support for Docker Compose"

See gh-1417
This commit is contained in:
Stephane Nicoll
2023-05-31 14:39:36 +02:00
parent 8430cccecf
commit 9995d6a2e9
26 changed files with 807 additions and 856 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
/**
* Model for a Docker Compose file.
*
* @author Moritz Halbritter
* @author Stephane Nicoll
*/
public class ComposeFile {
private final ComposeServiceContainer services = new ComposeServiceContainer();
/**
* Return the {@linkplain ComposeServiceContainer service container} to use to
* configure services.
* @return the {@link ComposeServiceContainer}
*/
public ComposeServiceContainer services() {
return this.services;
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import io.spring.initializr.generator.io.IndentingWriter;
/**
* A {@link ComposeFile} writer for {@code compose.yaml}.
*
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
public class ComposeFileWriter {
/**
* Write a {@linkplain ComposeFile compose.yaml} using the specified
* {@linkplain IndentingWriter writer}.
* @param writer the writer to use
* @param compose the compose file to write
*/
public void writeTo(IndentingWriter writer, ComposeFile compose) {
writer.println("services:");
compose.services()
.values()
.sorted(Comparator.comparing(ComposeService::getName))
.forEach((service) -> writeService(writer, service));
}
private void writeService(IndentingWriter writer, ComposeService service) {
writer.indented(() -> {
writer.println(service.getName() + ":");
writer.indented(() -> {
writer.println("image: '%s:%s'".formatted(service.getImage(), service.getImageTag()));
writerServiceEnvironment(writer, service.getEnvironment());
writerServicePorts(writer, service.getPorts());
});
});
}
private void writerServiceEnvironment(IndentingWriter writer, Map<String, String> environment) {
if (environment.isEmpty()) {
return;
}
writer.println("environment:");
writer.indented(() -> {
for (Map.Entry<String, String> env : environment.entrySet()) {
writer.println("- '%s=%s'".formatted(env.getKey(), env.getValue()));
}
});
}
private void writerServicePorts(IndentingWriter writer, Set<Integer> ports) {
if (ports.isEmpty()) {
return;
}
writer.println("ports:");
writer.indented(() -> {
for (Integer port : ports) {
writer.println("- '%d'".formatted(port));
}
});
}
}

View File

@@ -0,0 +1,151 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* A service to be declared in a Docker Compose file.
*
* @author Moritz Halbritter
* @author Stephane Nicoll
*/
public final class ComposeService {
private final String name;
private final String image;
private final String imageTag;
private final String imageWebsite;
private final Map<String, String> environment;
private final Set<Integer> ports;
private ComposeService(Builder builder) {
this.name = builder.name;
this.image = builder.image;
this.imageTag = builder.imageTag;
this.imageWebsite = builder.imageWebsite;
this.environment = Collections.unmodifiableMap(new TreeMap<>(builder.environment));
this.ports = Collections.unmodifiableSet(new TreeSet<>(builder.ports));
}
public String getName() {
return this.name;
}
public String getImage() {
return this.image;
}
public String getImageTag() {
return this.imageTag;
}
public String getImageWebsite() {
return this.imageWebsite;
}
public Map<String, String> getEnvironment() {
return this.environment;
}
public Set<Integer> getPorts() {
return this.ports;
}
/**
* Builder for {@link ComposeService}.
*/
public static class Builder {
private final String name;
private String image;
private String imageTag = "latest";
private String imageWebsite;
private final Map<String, String> environment = new TreeMap<>();
private final Set<Integer> ports = new TreeSet<>();
protected Builder(String name) {
this.name = name;
}
public Builder imageAndTag(String imageAndTag) {
String[] split = imageAndTag.split(":", 2);
String tag = (split.length == 1) ? "latest" : split[1];
return image(split[0]).imageTag(tag);
}
public Builder image(String image) {
this.image = image;
return this;
}
public Builder imageTag(String imageTag) {
this.imageTag = imageTag;
return this;
}
public Builder imageWebsite(String imageWebsite) {
this.imageWebsite = imageWebsite;
return this;
}
public Builder environment(String key, String value) {
this.environment.put(key, value);
return this;
}
public Builder environment(Map<String, String> environment) {
this.environment.putAll(environment);
return this;
}
public Builder ports(Collection<Integer> ports) {
this.ports.addAll(ports);
return this;
}
public Builder ports(int... ports) {
return ports(Arrays.stream(ports).boxed().toList());
}
/**
* Builds the {@link ComposeService} instance.
* @return the built instance
*/
public ComposeService build() {
return new ComposeService(this);
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;
import io.spring.initializr.generator.container.docker.compose.ComposeService.Builder;
/**
* A container for {@linkplain ComposeService Docker Compose services}.
*
* @author Stephane Nicoll
*/
public class ComposeServiceContainer {
private final Map<String, Builder> services = new LinkedHashMap<>();
/**
* Specify if this container is empty.
* @return {@code true} if no service is registered
*/
public boolean isEmpty() {
return this.services.isEmpty();
}
/**
* Specify if this container has a service customization with the specified
* {@code name}.
* @param name the name of a service
* @return {@code true} if a customization for a service with the specified
* {@code name} exists
*/
public boolean has(String name) {
return this.services.containsKey(name);
}
/**
* Return the {@link ComposeService services} to customize.
* @return the compose services
*/
public Stream<ComposeService> values() {
return this.services.values().stream().map(Builder::build);
}
/**
* Add a {@link ComposeService} with the specified name and {@link Consumer} to
* customize the object. If the service has already been added, the consumer can be
* used to further tune the existing service configuration.
* @param name the name of the service
* @param service a {@link Consumer} to customize the {@link ComposeService}
*/
public void add(String name, Consumer<Builder> service) {
service.accept(this.services.computeIfAbsent(name, Builder::new));
}
/**
* Remove the service with the specified {@code name}.
* @param name the name of the service
* @return {@code true} if such a service was registered, {@code false} otherwise
*/
public boolean remove(String name) {
return this.services.remove(name) != null;
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Docker Compose support.
*/
package io.spring.initializr.generator.container.docker.compose;

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
import java.io.StringWriter;
import java.util.function.Consumer;
import io.spring.initializr.generator.container.docker.compose.ComposeService.Builder;
import io.spring.initializr.generator.io.IndentingWriter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ComposeFile}.
*
* @author Moritz Halbritter
* @author Stephane Nicoll
*/
class ComposeFileWriterTests {
@Test
void writeBasicServices() {
ComposeFile file = new ComposeFile();
file.services().add("first", withSuffix(1));
file.services().add("second", withSuffix(2));
assertThat(write(file)).isEqualToIgnoringNewLines("""
services:
first:
image: 'image-1:image-tag-1'
second:
image: 'image-2:image-tag-2'
""");
}
@Test
void writeDetailedService() {
ComposeFile file = new ComposeFile();
file.services()
.add("elasticsearch",
(builder) -> builder.image("elasticsearch")
.imageTag("8.6.1")
.imageWebsite("https://www.docker.elastic.co/r/elasticsearch")
.environment("ELASTIC_PASSWORD", "secret")
.environment("discovery.type", "single-node")
.ports(9200, 9300));
assertThat(write(file)).isEqualToIgnoringNewLines("""
services:
elasticsearch:
image: 'elasticsearch:8.6.1'
environment:
- 'ELASTIC_PASSWORD=secret'
- 'discovery.type=single-node'
ports:
- '9200'
- '9300'
""");
}
@Test
void servicesAreOrderedByName() {
ComposeFile file = new ComposeFile();
file.services().add("b", withSuffix(2));
file.services().add("a", withSuffix(1));
assertThat(write(file)).isEqualToIgnoringNewLines("""
services:
a:
image: 'image-1:image-tag-1'
b:
image: 'image-2:image-tag-2'
""");
}
private Consumer<Builder> withSuffix(int suffix) {
return (builder) -> builder.image("image-" + suffix).imageTag("image-tag-" + suffix);
}
private String write(ComposeFile file) {
StringWriter out = new StringWriter();
IndentingWriter writer = new IndentingWriter(out, "\t"::repeat);
new ComposeFileWriter().writeTo(writer, file);
return out.toString();
}
}

View File

@@ -0,0 +1,182 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.container.docker.compose;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
/**
* Tests for {@link ComposeServiceContainer}.
*
* @author Stephane Nicoll
*/
class ComposeServiceContainerTests {
@Test
void isEmptyWithEmptyContainer() {
ComposeServiceContainer container = new ComposeServiceContainer();
assertThat(container.isEmpty()).isTrue();
}
@Test
void isEmptyWithService() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.isEmpty()).isFalse();
}
@Test
void hasWithMatchingService() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.has("test")).isTrue();
}
@Test
void hasWithNonMatchingName() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.has("another")).isFalse();
}
@Test
void tagIsSetToLatestIfNotGiven() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getImage()).isEqualTo("my-image");
assertThat(service.getImageTag()).isEqualTo("latest");
});
}
@Test
void tagIsSetToLatestIfNotGivenInImageTag() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image"));
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getImage()).isEqualTo("my-image");
assertThat(service.getImageTag()).isEqualTo("latest");
});
}
@Test
void tagIsSetToGivenInImageTag() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image:1.2.3"));
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getImage()).isEqualTo("my-image");
assertThat(service.getImageTag()).isEqualTo("1.2.3");
});
}
@Test
void portsAreSorted() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image").ports(8080));
container.add("test", (service) -> service.ports(7070));
assertThat(container.values()).singleElement()
.satisfies((service) -> assertThat(service.getPorts()).containsExactly(7070, 8080));
}
@Test
void environmentKeysAreSorted() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image").environment("z", "zz"));
container.add("test", (service) -> service.environment("a", "aa"));
assertThat(container.values()).singleElement()
.satisfies((service) -> assertThat(service.getEnvironment()).containsExactly(entry("a", "aa"),
entry("z", "zz")));
}
@Test
void environmentIsMerged() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.imageAndTag("my-image").environment(Map.of("a", "aa", "z", "zz")));
container.add("test", (service) -> service.environment(Map.of("a", "aaa", "b", "bb")));
assertThat(container.values()).singleElement()
.satisfies((service) -> assertThat(service.getEnvironment()).containsExactly(entry("a", "aaa"),
entry("b", "bb"), entry("z", "zz")));
}
@Test
void customizeService() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> {
service.image("my-image");
service.imageTag("my-image-tag");
service.imageWebsite("https://example.com/my-image");
service.environment("param", "value");
service.ports(8080);
});
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getName()).isEqualTo("test");
assertThat(service.getImage()).isEqualTo("my-image");
assertThat(service.getImageTag()).isEqualTo("my-image-tag");
assertThat(service.getImageWebsite()).isEqualTo("https://example.com/my-image");
assertThat(service.getEnvironment()).containsOnly(entry("param", "value"));
assertThat(service.getPorts()).containsOnly(8080);
});
}
@Test
void customizeTaskSeveralTimeReuseConfiguration() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> {
service.image("my-image");
service.imageTag("my-image-tag");
service.imageWebsite("https://example.com/my-image");
service.environment("param", "value");
service.ports(7070);
});
container.add("test", (service) -> {
service.image("my-image");
service.imageTag("my-image-tag");
service.imageWebsite("https://example.com/my-image");
service.environment("param", "value2");
service.ports(8080);
});
assertThat(container.values()).singleElement().satisfies((service) -> {
assertThat(service.getName()).isEqualTo("test");
assertThat(service.getImage()).isEqualTo("my-image");
assertThat(service.getImageTag()).isEqualTo("my-image-tag");
assertThat(service.getImageWebsite()).isEqualTo("https://example.com/my-image");
assertThat(service.getEnvironment()).containsOnly(entry("param", "value2"));
assertThat(service.getPorts()).containsOnly(7070, 8080);
});
}
@Test
void removeWithMatchingService() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.remove("test")).isTrue();
assertThat(container.isEmpty()).isTrue();
}
@Test
void removeWithNonMatchingName() {
ComposeServiceContainer container = new ComposeServiceContainer();
container.add("test", (service) -> service.image("my-image"));
assertThat(container.remove("another")).isFalse();
assertThat(container.isEmpty()).isFalse();
}
}