Skip to content

Commit

Permalink
Merge pull request lf-lang#2371 from lf-lang/docker-compose-override
Browse files Browse the repository at this point in the history
Docker compose override
  • Loading branch information
lhstrh authored Jul 26, 2024
2 parents d587b50 + 31e5ff2 commit d07547c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.List;
import java.util.Objects;
Expand Down Expand Up @@ -122,6 +123,19 @@ 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) {
var destination = dockerComposeDir.resolve("docker-compose-override.yml");
FileUtil.copyFile(found, destination);
this.context
.getErrorReporter()
.nowhere()
.info("Docker compose override file copied to " + destination);
}
}
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 +184,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
13 changes: 13 additions & 0 deletions test/Python/src/docker/DockerComposeConfig.docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
federate__server:
environment:
- NVIDIA_DISABLE_REQUIRE=1
shm_size: '4gb'
volumes:
- hugging_face_cache:/root/.cache

federate__client:
shm_size: '2gb'

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

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
}

0 comments on commit d07547c

Please sign in to comment.