-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Adds -H:+GenerateBuildArtifactsFile, copies .so from remote container #41201
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,19 @@ | |
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.builder.JsonReader; | ||
import io.quarkus.builder.json.JsonArray; | ||
import io.quarkus.builder.json.JsonObject; | ||
import io.quarkus.builder.json.JsonString; | ||
import io.quarkus.builder.json.JsonValue; | ||
import io.quarkus.deployment.pkg.NativeConfig; | ||
|
||
public class NativeImageBuildRemoteContainerRunner extends NativeImageBuildContainerRunner { | ||
|
@@ -34,59 +40,94 @@ protected void preBuild(Path outputDir, List<String> buildArgs) throws Interrupt | |
final List<String> containerRuntimeArgs = Arrays.asList("-v", | ||
CONTAINER_BUILD_VOLUME_NAME + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH); | ||
final String[] createTempContainerCommand = buildCommand("create", containerRuntimeArgs, Collections.emptyList()); | ||
containerId = runCommandAndReadOutput(createTempContainerCommand, "Failed to create temp container."); | ||
try { | ||
containerId = runCommandAndReadOutput(createTempContainerCommand).get(0); | ||
} catch (RuntimeException | InterruptedException | IOException e) { | ||
throw new RuntimeException("Failed to create temp container.", e); | ||
} | ||
// docker cp <files> <containerID>:/project | ||
String[] copyCommand = new String[] { containerRuntime.getExecutableName(), "cp", outputDir.toAbsolutePath() + "/.", | ||
final String[] copyCommand = new String[] { | ||
containerRuntime.getExecutableName(), "cp", outputDir.toAbsolutePath() + "/.", | ||
containerId + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH }; | ||
runCommand(copyCommand, "Failed to copy source-jar and libs from host to builder container", null); | ||
runCommand(copyCommand, "Failed to copy source-jar and libs from host to builder container"); | ||
super.preBuild(outputDir, buildArgs); | ||
} | ||
|
||
private String runCommandAndReadOutput(String[] command, String errorMsg) throws IOException, InterruptedException { | ||
private List<String> runCommandAndReadOutput(String[] command) throws IOException, InterruptedException { | ||
log.info(String.join(" ", command).replace("$", "\\$")); | ||
Process process = new ProcessBuilder(command).start(); | ||
final Process process = new ProcessBuilder(command).start(); | ||
if (process.waitFor() != 0) { | ||
throw new RuntimeException(errorMsg); | ||
throw new RuntimeException("Command failed: " + String.join(" ", command)); | ||
} | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
return reader.readLine(); | ||
return reader.lines().toList(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void postBuild(Path outputDir, String nativeImageName, String resultingExecutableName) { | ||
copyFromContainerVolume(outputDir, resultingExecutableName, | ||
"Failed to copy native image from container volume back to the host."); | ||
|
||
// Note that podman cp does not support globbing i.e. cp /project/*.so will not work. | ||
// Why only .so? How about .dynlib and .lib? Regardless of the host platform, | ||
// the builder container is always Linux. So, we only need to copy .so files. | ||
// | ||
// We could either start the container again, exec `find' or `ls' to list the .so files, | ||
// stop the container again and use that list. We could also use the build-artifacts.json | ||
// to get the list of artifacts straight away which is what ended up doing here: | ||
copyFromContainerVolume(outputDir, "build-artifacts.json", null); | ||
try { | ||
final Path buildArtifactsFile = outputDir.resolve("build-artifacts.json"); | ||
if (Files.exists(buildArtifactsFile)) { | ||
// The file is small enough to afford this read | ||
final String buildArtifactsJson = Files.readString(buildArtifactsFile); | ||
final JsonObject jsonRead = JsonReader.of(buildArtifactsJson).read(); | ||
final JsonValue jdkLibraries = jsonRead.get("jdk_libraries"); | ||
// The jdk_libraries field is optional, there might not be any. | ||
if (jdkLibraries instanceof JsonArray) { | ||
for (JsonValue lib : ((JsonArray) jdkLibraries).value()) { | ||
copyFromContainerVolume(outputDir, ((JsonString) lib).value(), | ||
"Failed to copy " + lib + " from container volume back to the host."); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
log.errorf(e, "Failed to list .so files in the build-artifacts.json. Skipping the step."); | ||
} | ||
|
||
if (nativeConfig.debug().enabled()) { | ||
copyFromContainerVolume(outputDir, "sources", "Failed to copy sources from container volume back to the host."); | ||
String symbols = String.format("%s.debug", nativeImageName); | ||
copyFromContainerVolume(outputDir, symbols, "Failed to copy debug symbols from container volume back to the host."); | ||
copyFromContainerVolume(outputDir, "sources", | ||
"Failed to copy sources from container volume back to the host."); | ||
final String symbols = String.format("%s.debug", nativeImageName); | ||
copyFromContainerVolume(outputDir, symbols, | ||
"Failed to copy debug symbols from container volume back to the host."); | ||
Comment on lines
-61
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these changes necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are not necessary to fix the issue. |
||
} | ||
// docker container rm <containerID> | ||
final String[] rmTempContainerCommand = new String[] { containerRuntime.getExecutableName(), "container", "rm", | ||
containerId }; | ||
runCommand(rmTempContainerCommand, "Failed to remove container: " + containerId, null); | ||
runCommand(rmTempContainerCommand, "Failed to remove container: " + containerId); | ||
// docker volume rm <volumeID> | ||
rmVolume("Failed to remove volume: " + CONTAINER_BUILD_VOLUME_NAME); | ||
} | ||
|
||
private void rmVolume(String errorMsg) { | ||
final String[] rmVolumeCommand = new String[] { containerRuntime.getExecutableName(), "volume", "rm", | ||
CONTAINER_BUILD_VOLUME_NAME }; | ||
runCommand(rmVolumeCommand, errorMsg, null); | ||
runCommand(rmVolumeCommand, errorMsg); | ||
} | ||
|
||
private void copyFromContainerVolume(Path outputDir, String path, String errorMsg) { | ||
// docker cp <containerID>:/project/<path> <dest> | ||
String[] copyCommand = new String[] { containerRuntime.getExecutableName(), "cp", | ||
final String[] copyCommand = new String[] { containerRuntime.getExecutableName(), "cp", | ||
containerId + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH + "/" + path, | ||
outputDir.toAbsolutePath().toString() }; | ||
runCommand(copyCommand, errorMsg, null); | ||
runCommand(copyCommand, errorMsg); | ||
} | ||
|
||
@Override | ||
protected List<String> getContainerRuntimeBuildArgs(Path outputDir) { | ||
List<String> containerRuntimeArgs = super.getContainerRuntimeBuildArgs(outputDir); | ||
final List<String> containerRuntimeArgs = super.getContainerRuntimeBuildArgs(outputDir); | ||
Collections.addAll(containerRuntimeArgs, "-v", | ||
CONTAINER_BUILD_VOLUME_NAME + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH); | ||
return containerRuntimeArgs; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-integration-tests-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-integration-test-awt-packaging</artifactId> | ||
<name>Quarkus - Integration Tests - AWT Packaging</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-jaxb</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-awt</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-amazon-lambda</artifactId> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5-mockito</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-test-amazon-lambda</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-jaxb-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-awt-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-amazon-lambda-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
36 changes: 36 additions & 0 deletions
36
integration-tests/awt-packaging/src/main/java/io/quarkus/it/jaxb/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.it.jaxb; | ||
|
||
import java.awt.Image; | ||
|
||
import jakarta.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
public class Book { | ||
|
||
private String title; | ||
private Image cover; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, Image cover) { | ||
this.title = title; | ||
this.cover = cover; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Image getCover() { | ||
return cover; | ||
} | ||
|
||
public void setCover(Image cover) { | ||
this.cover = cover; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation behind this change? I understand that it's more generic this way but since we only use the method in a single place where this is not needed it seems to unnecessarily (for the time being) complicate things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zakkak I used that to read the output of
exec
forfind
to list the .so files in the started container. Then I abandoned that approach in favor of using the-H:+GenerateBuildArtifactsFile
. So strictly speaking, we can leave in the thing that returns only the first line of output, however confusing that might be. Would rename the method though then, to ...readOneLineOutput...