Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker compose override #2371

Merged
merged 21 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-annotations:$fasterxmlVersion"
implementation "com.fasterxml.jackson.core:jackson-databind:$fasterxmlVersion"
implementation "org.apache.commons:commons-text:$commonsTextVersion"
implementation "org.yaml:snakeyaml:${snakeyamlVersion}"

implementation ("de.cau.cs.kieler.klighd:de.cau.cs.kieler.klighd.lsp:$klighdVersion") {
exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt.*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.text.StringEscapeUtils;
import org.lflang.generator.LFGeneratorContext;
import org.lflang.target.property.DockerProperty;
import org.lflang.util.FileUtil;
import org.lflang.util.LFCommand;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

/**
* Code generator for docker-compose configurations.
Expand Down Expand Up @@ -122,6 +129,44 @@ public void writeDockerComposeFile(List<DockerData> services, String networkName
String.join(
"\n", this.generateDockerServices(services), this.generateDockerNetwork(networkName));
FileUtil.writeToFile(contents, dockerComposeDir.resolve("docker-compose.yml"));
var dockerConfigFile =
context.getTargetConfig().get(DockerProperty.INSTANCE).dockerConfigFile();
if (!dockerConfigFile.isEmpty()) {
var found = FileUtil.findInPackage(Path.of(dockerConfigFile), context.getFileConfig());
if (found != null) {
try {
Yaml yamlLoader = new Yaml(new SafeConstructor(new LoaderOptions()));
Map<String, Object> yamlData = yamlLoader.load(Files.newBufferedReader(found));
if (yamlData.containsKey("services")) {
@SuppressWarnings("unchecked") // This will be caught by the try-catch block
var servicesMap = (Map<String, Object>) yamlData.get("services");
var serviceNames = new ArrayList<>(servicesMap.keySet());
for (String serviceName : serviceNames) {
Object value = servicesMap.get(serviceName);
servicesMap.remove(serviceName);
servicesMap.put("federate__" + serviceName, value);
}
}
var destination = dockerComposeDir.resolve("docker-compose-override.yml");
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setIndent(4);
dumperOptions.setPrettyFlow(true);
dumperOptions.setIndicatorIndent(2);
Yaml yamlDumper = new Yaml(dumperOptions);
yamlDumper.dump(yamlData, Files.newBufferedWriter(destination));
this.context
.getErrorReporter()
.at(found.toAbsolutePath())
.info("Docker compose override file copied to " + destination);
} catch (Exception e) {
context
.getErrorReporter()
.at(found.toAbsolutePath())
.error("Error parsing docker config file: %s".formatted(e.getMessage()));
}
}
}
var envFile = context.getTargetConfig().get(DockerProperty.INSTANCE).envFile();
if (!envFile.isEmpty()) {
var found = FileUtil.findInPackage(Path.of(envFile), context.getFileConfig());
Expand Down Expand Up @@ -170,9 +215,14 @@ public void createLauncher() {
set -euo pipefail
cd $(dirname "$0")
cd "%s/%s"
docker compose up --abort-on-container-failure
docker compose -f docker-compose.yml %s up --abort-on-container-failure
"""
.formatted(relPath, packageRoot.relativize(srcGenPath));
.formatted(
relPath,
packageRoot.relativize(srcGenPath),
Files.exists(fileConfig.getSrcGenPath().resolve("docker-compose-override.yml"))
? "-f docker-compose-override.yml"
: "");
var messageReporter = context.getErrorReporter();
try {
var writer = new BufferedWriter(new FileWriter(file));
Expand Down
15 changes: 11 additions & 4 deletions core/src/main/java/org/lflang/target/property/DockerProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
var postBuildScript = "";
var runScript = "";
var envFile = "";
var dockerConfigFile = "";

if (node.getLiteral() != null) {
if (ASTUtils.toBoolean(node)) {
Expand All @@ -63,6 +64,8 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
postBuildScript = ASTUtils.elementToSingleString(entry.getValue());
case RTI_IMAGE -> rti = ASTUtils.elementToSingleString(entry.getValue());
case ENV_FILE -> envFile = ASTUtils.elementToSingleString(entry.getValue());
case DOCKER_CONFIG_FILE ->
dockerConfigFile = ASTUtils.elementToSingleString(entry.getValue());
}
}
}
Expand All @@ -76,7 +79,8 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
preBuildScript,
postBuildScript,
runScript,
envFile);
envFile,
dockerConfigFile);
}

@Override
Expand Down Expand Up @@ -113,6 +117,7 @@ public Element toAstElement(DockerOptions value) {
case POST_BUILD_SCRIPT -> pair.setValue(ASTUtils.toElement(value.postBuildScript));
case RTI_IMAGE -> pair.setValue(ASTUtils.toElement(value.rti));
case ENV_FILE -> pair.setValue(ASTUtils.toElement(value.envFile));
case DOCKER_CONFIG_FILE -> pair.setValue(ASTUtils.toElement(value.dockerConfigFile));
}
kvp.getPairs().add(pair);
}
Expand Down Expand Up @@ -140,7 +145,8 @@ public record DockerOptions(
String preBuildScript,
String postBuildScript,
String preRunScript,
String envFile) {
String envFile,
String dockerConfigFile) {

/** Default location to pull the rti from. */
public static final String DOCKERHUB_RTI_IMAGE = "lflang/rti:rti";
Expand All @@ -151,7 +157,7 @@ public record DockerOptions(
public static final String LOCAL_RTI_IMAGE = "rti:local";

public DockerOptions(boolean enabled) {
this(enabled, false, "", "", DOCKERHUB_RTI_IMAGE, DEFAULT_SHELL, "", "", "", "");
this(enabled, false, "", "", DOCKERHUB_RTI_IMAGE, DEFAULT_SHELL, "", "", "", "", "");
}
}

Expand All @@ -168,7 +174,8 @@ public enum DockerOption implements DictionaryElement {
RTI_IMAGE("rti-image", PrimitiveType.STRING),
PRE_BUILD_SCRIPT("pre-build-script", PrimitiveType.STRING),
PRE_RUN_SCRIPT("pre-run-script", PrimitiveType.STRING),
POST_BUILD_SCRIPT("post-build-script", PrimitiveType.STRING);
POST_BUILD_SCRIPT("post-build-script", PrimitiveType.STRING),
DOCKER_CONFIG_FILE("docker-compose-override", PrimitiveType.STRING);

public final PrimitiveType type;

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ swtVersion=3.124.0
spotbugsToolVersion=4.7.3
jcipVersion=1.0
commonsTextVersion=1.11.0
snakeyamlVersion=2.0

[manifestPropertyNames]
org.eclipse.xtext=xtextVersion
Expand Down
13 changes: 13 additions & 0 deletions test/Python/src/docker/DockerComposeConfig.docker.yml
Depetrol marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
server:
environment:
- NVIDIA_DISABLE_REQUIRE=1
shm_size: '4gb'
volumes:
- hugging_face_cache:/root/.cache

client:
shm_size: '2gb'

volumes:
hugging_face_cache:
55 changes: 55 additions & 0 deletions test/Python/src/docker/DockerComposeConfig.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
target Python {
coordination: decentralized,
docker: {
docker-compose-override: "./DockerComposeConfig.docker.yml"
}
}

reactor Client(STP_offset = 2 s) {
input server_message
output client_message

reaction(startup) {=
print("Client Startup!")
=}

reaction(server_message) -> client_message {=
val = server_message.value
val += 1
print("client:", val)
if val==49:
print("client done")
request_stop()
if val<49:
client_message.set(val)
=}
}

reactor Server(STP_offset = 2 s) {
output server_message
input client_message

reaction(startup) -> server_message {=
print("Server Startup!")
server_message.set(0)
=}

reaction(client_message) -> server_message {=
val = client_message.value
val += 1
print("server:", val)
if val==48:
print("server done")
server_message.set(val)
request_stop()
if val<48:
server_message.set(val)
=}
}

federated reactor(STP_offset = 2 s) {
client = new Client()
server = new Server()
server.server_message -> client.server_message after 100 ms
client.client_message -> server.client_message
}
Loading