Skip to content

Commit

Permalink
Improve logging in native image builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Feb 24, 2021
1 parent a8969db commit 7319acc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.nio.file.Path;
import java.util.List;

import org.jboss.logging.Logger;

import io.quarkus.deployment.pkg.NativeConfig;

public class NativeImageBuildRemoteContainerRunner extends NativeImageBuildContainerRunner {
Expand All @@ -19,19 +21,21 @@ public NativeImageBuildRemoteContainerRunner(NativeConfig nativeConfig, Path out
}

@Override
protected void preBuild(List<String> buildArgs) throws InterruptedException, IOException {
protected void preBuild(Logger log, List<String> buildArgs) throws InterruptedException, IOException {
List<String> containerRuntimeArgs = getContainerRuntimeBuildArgs();
String[] createContainerCommand = buildCommand("create", containerRuntimeArgs, buildArgs);
log.info(String.join(" ", createContainerCommand).replace("$", "\\$"));
Process createContainerProcess = new ProcessBuilder(createContainerCommand).start();
createContainerProcess.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(createContainerProcess.getInputStream()))) {
containerId = reader.readLine();
}
String[] copyCommand = new String[] { containerRuntime.getExecutableName(), "cp", outputPath + "/.",
containerId + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH };
log.info(String.join(" ", copyCommand).replace("$", "\\$"));
Process copyProcess = new ProcessBuilder(copyCommand).start();
copyProcess.waitFor();
super.preBuild(buildArgs);
super.preBuild(log, buildArgs);
}

@Override
Expand All @@ -40,18 +44,20 @@ protected String[] getBuildCommand(List<String> args) {
}

@Override
protected void postBuild() throws InterruptedException, IOException {
copy(nativeImageName);
copy("sources");
protected void postBuild(Logger log) throws InterruptedException, IOException {
copy(log, nativeImageName);
copy(log, "sources");
String[] removeCommand = new String[] { containerRuntime.getExecutableName(), "container", "rm", "--volumes",
containerId };
log.info(String.join(" ", removeCommand).replace("$", "\\$"));
Process removeProcess = new ProcessBuilder(removeCommand).start();
removeProcess.waitFor();
}

private void copy(String path) throws IOException, InterruptedException {
private void copy(Logger log, String path) throws IOException, InterruptedException {
String[] copyCommand = new String[] { containerRuntime.getExecutableName(), "cp",
containerId + ":" + NativeImageBuildStep.CONTAINER_BUILD_VOLUME_PATH + "/" + path, outputPath };
log.info(String.join(" ", copyCommand).replace("$", "\\$"));
Process copyProcess = new ProcessBuilder(copyCommand).start();
copyProcess.waitFor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.logging.Logger;

import io.quarkus.deployment.pkg.steps.NativeImageBuildStep.GraalVM;
import io.quarkus.deployment.util.ProcessUtil;

Expand Down Expand Up @@ -41,13 +43,15 @@ public void setup(boolean processInheritIODisabled) {
public void cleanupServer(File outputDir, boolean processInheritIODisabled) throws InterruptedException, IOException {
}

public int build(List<String> args, Path outputDir, boolean processInheritIODisabled)
public int build(List<String> args, Logger log, Path outputDir, boolean processInheritIODisabled)
throws InterruptedException, IOException {
preBuild(args);
preBuild(log, args);
try {
CountDownLatch errorReportLatch = new CountDownLatch(1);
final ProcessBuilder processBuilder = new ProcessBuilder(getBuildCommand(args))
final String[] buildCommand = getBuildCommand(args);
final ProcessBuilder processBuilder = new ProcessBuilder(buildCommand)
.directory(outputDir.toFile());
log.info(String.join(" ", buildCommand).replace("$", "\\$"));
final Process process = ProcessUtil.launchProcessStreamStdOut(processBuilder, processInheritIODisabled);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new ErrorReplacingProcessReader(process.getErrorStream(), outputDir.resolve("reports").toFile(),
Expand All @@ -56,18 +60,18 @@ public int build(List<String> args, Path outputDir, boolean processInheritIODisa
errorReportLatch.await();
return process.waitFor();
} finally {
postBuild();
postBuild(log);
}
}

protected abstract String[] getGraalVMVersionCommand(List<String> args);

protected abstract String[] getBuildCommand(List<String> args);

protected void preBuild(List<String> buildArgs) throws IOException, InterruptedException {
protected void preBuild(Logger log, List<String> buildArgs) throws IOException, InterruptedException {
}

protected void postBuild() throws InterruptedException, IOException {
protected void postBuild(Logger log) throws InterruptedException, IOException {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa

List<String> nativeImageArgs = commandAndExecutable.args;

log.info(String.join(" ", nativeImageArgs).replace("$", "\\$"));
int exitCode = buildRunner.build(nativeImageArgs, outputDir, processInheritIODisabled.isPresent());
int exitCode = buildRunner.build(nativeImageArgs, log, outputDir, processInheritIODisabled.isPresent());
if (exitCode != 0) {
throw imageGenerationFailed(exitCode, nativeImageArgs);
}
Expand Down

0 comments on commit 7319acc

Please sign in to comment.