Skip to content

Commit

Permalink
Merge pull request #2349 from lf-lang/docker-env-file
Browse files Browse the repository at this point in the history
Support for Docker environment files
  • Loading branch information
lhstrh committed Jul 2, 2024
2 parents 9459715 + cebcbba commit 1d2ea73
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
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;
Expand Down Expand Up @@ -65,9 +67,22 @@ protected String getServiceDescription(DockerData data) {
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
LF_TELEGRAF_HOST_NAME: ${LF_TELEGRAF_HOST_NAME:-host.docker.internal}
- "LF_TELEGRAF_HOST_NAME=${LF_TELEGRAF_HOST_NAME:-host.docker.internal}"
%s
"""
.formatted(getServiceName(data), getBuildContext(data), getContainerName(data));
.formatted(
getServiceName(data),
getBuildContext(data),
getContainerName(data),
getEnvironmentFile());
}

private String getEnvironmentFile() {
var file = context.getTargetConfig().get(DockerProperty.INSTANCE).envFile();
if (!file.isEmpty()) {
return "env_file: \"%s\"".formatted(StringEscapeUtils.escapeXSI(file));
}
return "";
}

/** Return the name of the service represented by the given data. */
Expand Down Expand Up @@ -102,11 +117,23 @@ public void writeDockerComposeFile(List<DockerData> services) throws IOException
*/
public void writeDockerComposeFile(List<DockerData> services, String networkName)
throws IOException {
var dockerComposeDir = context.getFileConfig().getSrcGenPath();
var contents =
String.join(
"\n", this.generateDockerServices(services), this.generateDockerNetwork(networkName));
FileUtil.writeToFile(
contents, context.getFileConfig().getSrcGenPath().resolve("docker-compose.yml"));
FileUtil.writeToFile(contents, dockerComposeDir.resolve("docker-compose.yml"));
var envFile = context.getTargetConfig().get(DockerProperty.INSTANCE).envFile();
if (!envFile.isEmpty()) {
var found = FileUtil.findInPackage(Path.of(envFile), context.getFileConfig());
if (found != null) {
var destination = dockerComposeDir.resolve(found.getFileName());
FileUtil.copyFile(found, destination);
this.context
.getErrorReporter()
.nowhere()
.info("Environment file written to " + destination);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
var preBuildScript = "";
var postBuildScript = "";
var runScript = "";
var envFile = "";

if (node.getLiteral() != null) {
if (ASTUtils.toBoolean(node)) {
Expand All @@ -61,6 +62,7 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
case POST_BUILD_SCRIPT ->
postBuildScript = ASTUtils.elementToSingleString(entry.getValue());
case RTI_IMAGE -> rti = ASTUtils.elementToSingleString(entry.getValue());
case ENV_FILE -> envFile = ASTUtils.elementToSingleString(entry.getValue());
}
}
}
Expand All @@ -73,7 +75,8 @@ public DockerOptions fromAst(Element node, MessageReporter reporter) {
shell,
preBuildScript,
postBuildScript,
runScript);
runScript,
envFile);
}

@Override
Expand Down Expand Up @@ -109,6 +112,7 @@ public Element toAstElement(DockerOptions value) {
case PRE_RUN_SCRIPT -> pair.setValue(ASTUtils.toElement(value.preRunScript));
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));
}
kvp.getPairs().add(pair);
}
Expand All @@ -135,7 +139,8 @@ public record DockerOptions(
String shell,
String preBuildScript,
String postBuildScript,
String preRunScript) {
String preRunScript,
String envFile) {

/** Default location to pull the rti from. */
public static final String DOCKERHUB_RTI_IMAGE = "lflang/rti:rti";
Expand All @@ -146,7 +151,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 @@ -158,6 +163,7 @@ public DockerOptions(boolean enabled) {
public enum DockerOption implements DictionaryElement {
NO_BUILD("no-build", PrimitiveType.BOOLEAN),
BUILDER_BASE("builder-base", PrimitiveType.STRING),
ENV_FILE("env-file", PrimitiveType.STRING),
RUNNER_BASE("runner-base", PrimitiveType.STRING),
RTI_IMAGE("rti-image", PrimitiveType.STRING),
PRE_BUILD_SCRIPT("pre-build-script", PrimitiveType.STRING),
Expand Down
2 changes: 2 additions & 0 deletions test/C/src/docker/federated/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# define COMPOSE_DEBUG based on DEV_MODE, defaults to false
COMPOSE_DEBUG=${DEV_MODE:-false}
3 changes: 2 additions & 1 deletion test/C/src/docker/federated/DockerOptions.lf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ target C {
pre-build-script: "foo.sh",
pre-run-script: "bar.sh",
post-build-script: "baz.sh",
no-build: false
no-build: false,
env-file: ".env"
},
cmake-include: "cmake-check-environment-variable.cmake"
}
Expand Down

0 comments on commit 1d2ea73

Please sign in to comment.