Skip to content

Commit

Permalink
Merge pull request #2249 from lf-lang/fix-docker-error-handling
Browse files Browse the repository at this point in the history
Fix in error handling for Docker builds
  • Loading branch information
petervdonovan committed Apr 1, 2024
2 parents 78addb2 + 9296f07 commit 22666f0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws
});

context.finish(Status.COMPILED, codeMapMap);
return false;
return context.getErrorReporter().getErrorsOccurred();
}

/**
Expand All @@ -218,9 +218,14 @@ private void buildUsingDocker(LFGeneratorContext context, List<SubContext> subCo
dockerGen.writeDockerComposeFile(createDockerFiles(context, subContexts));
if (dockerGen.build()) {
dockerGen.createLauncher();
} else {
context.getErrorReporter().nowhere().error("Docker build failed.");
}
} catch (IOException e) {
context.getErrorReporter().nowhere().error("Unsuccessful Docker build.");
context
.getErrorReporter()
.nowhere()
.error("Docker build failed due to invalid file system state.");
}
}

Expand All @@ -237,8 +242,7 @@ private void prepareRtiBuildEnvironment(LFGeneratorContext context) {
try {
Files.createDirectories(dest);
// 2. Copy reactor-c source files into it
FileUtil.copyFromClassPath("/lib/c/reactor-c/core", dest, true, false);
FileUtil.copyFromClassPath("/lib/c/reactor-c/include", dest, true, false);
FileUtil.copyFromClassPath("/lib/c/reactor-c", dest, true, true);
// 3. Generate a Dockerfile for the rti
new RtiDockerGenerator(context).generateDockerData(dest).writeDockerFile();
} catch (IOException e) {
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
context.getMode());
context.finish(GeneratorResult.Status.COMPILED, null);
} else if (dockerBuild.enabled()) {
buildUsingDocker();
boolean success = buildUsingDocker();
if (!success) {
context.unsuccessfulFinish();
}
} else {
var cleanCode = code.removeLines("#line");
var cCompiler = new CCompiler(targetConfig, fileConfig, messageReporter, cppMode);
Expand Down Expand Up @@ -557,7 +560,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
}

/** Create Dockerfiles and docker-compose.yml, build, and create a launcher. */
private void buildUsingDocker() {
private boolean buildUsingDocker() {
// Create docker file.
var dockerCompose = new DockerComposeGenerator(context);
var dockerData = getDockerGenerator(context).generateDockerData();
Expand All @@ -568,9 +571,13 @@ private void buildUsingDocker() {
throw new RuntimeException("Error while writing Docker files", e);
}
var success = dockerCompose.build();
if (!success) {
messageReporter.nowhere().error("Docker-compose build failed.");
}
if (success && mainDef != null) {
dockerCompose.createLauncher();
}
return success;
}

private void generateCodeFor(String lfModuleName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.lflang.generator.docker;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import org.lflang.generator.LFGeneratorContext;

/**
Expand All @@ -15,24 +19,12 @@ public RtiDockerGenerator(LFGeneratorContext context) {

@Override
protected String generateDockerFileContent() {
return """
# Docker file for building the image of the rti
FROM %s
COPY core /reactor-c/core
COPY include /reactor-c/include
WORKDIR /reactor-c/core/federated/RTI
%s
RUN rm -rf build && \\
mkdir build && \\
cd build && \\
cmake ../ && \\
make && \\
make install
# Use ENTRYPOINT not CMD so that command-line arguments go through
ENTRYPOINT ["RTI"]
"""
.formatted(baseImage(), generateRunForBuildDependencies());
InputStream stream =
RtiDockerGenerator.class.getResourceAsStream(
"/lib/c/reactor-c/core/federated/RTI/rti.Dockerfile");
return new BufferedReader(new InputStreamReader(stream))
.lines()
.collect(Collectors.joining("\n"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public TSDockerGenerator(LFGeneratorContext context) {
/** Return the content of the docker file for [tsFileName]. */
public String generateDockerFileContent() {
return """
|FROM %s
|WORKDIR /linguafranca/$name
|%s
|COPY . .
|ENTRYPOINT ["node", "dist/%s.js"]
FROM %s
WORKDIR /linguafranca/$name
%s
COPY . .
ENTRYPOINT ["node", "dist/%s.js"]
"""
.formatted(baseImage(), generateRunForBuildDependencies(), context.getFileConfig().name);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/lib/c/reactor-c
Submodule reactor-c updated 53 files
+0 −1 .gitignore
+17 −37 core/CMakeLists.txt
+21 −1 core/environment.c
+1 −2 core/federated/RTI/rti.Dockerfile
+2 −2 core/federated/RTI/rti_common.h
+8 −9 core/federated/RTI/rti_remote.c
+8 −5 core/federated/clock-sync.c
+34 −30 core/federated/federate.c
+5 −10 core/federated/network/net_util.c
+15 −13 core/lf_token.c
+11 −0 core/lf_utils.cmake
+27 −8 core/reactor.c
+9 −9 core/reactor_common.c
+2 −2 core/tag.c
+10 −5 core/threaded/reactor_threaded.c
+4 −1 core/threaded/scheduler_GEDF_NP.c
+10 −10 core/threaded/scheduler_NP.c
+18 −24 core/threaded/scheduler_adaptive.c
+1 −2 core/threaded/watchdog.c
+10 −16 core/tracepoint.c
+4 −1 core/utils/CMakeLists.txt
+7 −3 core/utils/pqueue.c
+2 −2 core/utils/pqueue_support.h
+1 −1 core/utils/pqueue_tag.c
+9 −13 core/utils/util.c
+0 −3 include/core/federated/network/net_common.h
+2 −2 include/core/federated/network/net_util.h
+1 −1 include/core/lf_token.h
+1 −1 include/core/threaded/reactor_threaded.h
+134 −103 include/core/tracepoint.h
+0 −1 include/core/utils/impl/hashmap.h
+5 −0 lib/CMakeLists.txt
+3 −9 lib/schedule.c
+1 −1 lingua-franca-ref.txt
+1 −1 logging/api/logging_macros.h
+1 −1 low_level_platform/api/low_level_platform.h
+1 −1 low_level_platform/api/platform/lf_POSIX_threads_support.h
+1 −1 low_level_platform/api/platform/lf_nrf52_support.h
+55 −18 low_level_platform/impl/CMakeLists.txt
+0 −18 low_level_platform/impl/Platform.cmake
+17 −1 low_level_platform/impl/src/lf_C11_threads_support.c
+1 −0 low_level_platform/impl/src/lf_linux_support.c
+1 −0 low_level_platform/impl/src/lf_macos_support.c
+1 −1 platform/impl/CMakeLists.txt
+2 −1 platform/impl/platform.c
+3 −2 test/RTI/rti_common_test.c
+2 −3 test/general/utils/hashmap_test.c
+1 −1 test/src_gen_stub.c
+4 −0 trace/impl/CMakeLists.txt
+7 −4 trace/impl/include/trace_impl.h
+10 −19 trace/impl/src/trace_impl.c
+59 −0 util/tracing/trace_util.c
+5 −0 util/tracing/trace_util.h
14 changes: 7 additions & 7 deletions core/src/testFixtures/java/org/lflang/tests/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ private void prepare(LFTest test, Transformer transformer, Configurator configur
FileConfig.findPackageRoot(test.getSrcPath(), s -> {})
.resolve(FileConfig.DEFAULT_SRC_GEN_DIR)
.toString());

// Update the test by applying the transformation.
if (transformer != null) {
if (!transformer.transform(resource)) {
throw new TestError("Test transformation unsuccessful.", Result.TRANSFORM_FAIL);
}
}
var context =
new MainContext(
LFGeneratorContext.Mode.STANDALONE,
Expand All @@ -391,13 +398,6 @@ private void prepare(LFTest test, Transformer transformer, Configurator configur
fileAccess,
fileConfig -> new DefaultMessageReporter());

// Update the test by applying the transformation.
if (transformer != null) {
if (!transformer.transform(resource)) {
throw new TestError("Test transformation unsuccessful.", Result.TRANSFORM_FAIL);
}
}

// Reload the context because properties may have changed as part of the transformation.
test.loadContext(context);

Expand Down

0 comments on commit 22666f0

Please sign in to comment.