Skip to content
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

Update devc metadata file write behavior for container build and start #430

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@

import com.sun.nio.file.SensitivityWatchEventModifier;

import io.openliberty.tools.ant.ServerTask;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
Expand All @@ -96,8 +98,6 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import io.openliberty.tools.ant.ServerTask;

/**
* Utility class for dev mode.
*/
Expand Down Expand Up @@ -1280,7 +1280,9 @@ private void buildContainerImage(File tempContainerfile, File userContainerfile,
info("The RUN features.sh command is detected in the Containerfile and extra time may be necessary when installing features.");
}
long startTime = System.currentTimeMillis();
execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout);

execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout, false);

checkContainerBuildTime(startTime, buildContext);
info("Completed building container image.");
} catch (IllegalThreadStateException e) {
Expand Down Expand Up @@ -1338,7 +1340,7 @@ private void startContainer() throws PluginExecutionException {
String startContainerCommand = getContainerCommand();
info(startContainerCommand);
containerRunProcess = getRunProcess(startContainerCommand);
execContainerCmdAndLog(containerRunProcess, 0);
execContainerCmdAndLog(containerRunProcess, 0, true);
} catch (IOException e) {
error("Error starting container: " + e.getMessage());
throw new RuntimeException(e);
Expand Down Expand Up @@ -1374,7 +1376,7 @@ private Process getRunProcess(String command) throws IOException {
return processBuilder.start();
}

private void execContainerCmdAndLog(final Process startingProcess, int timeout) throws InterruptedException {
private void execContainerCmdAndLog(final Process startingProcess, int timeout, boolean isStartCommand) throws InterruptedException {
Thread logCopyInputThread = new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -1396,7 +1398,10 @@ public void run() {
});
logCopyErrorThread.start();

writeDevcMetadata(true);
if (isStartCommand) {
writeDevcMetadata(true);
}

if (timeout == 0) {
startingProcess.waitFor();
} else {
Expand All @@ -1411,7 +1416,9 @@ public void run() {
debug("Unexpected exit running container command, return value=" + startingProcess.exitValue());
// show first message from standard err
String errorMessage = new String(firstErrorLine).trim() + " RC=" + startingProcess.exitValue();
writeDevcMetadata(false);
if (isStartCommand) {
writeDevcMetadata(false);
}
throw new RuntimeException(errorMessage);
}
}
Expand Down Expand Up @@ -5695,8 +5702,12 @@ private Set<String> getGeneratedFeatures() {
*/
public void writeDevcMetadata(boolean alive) {
File metaFile = new File(buildDirectory, serverDirectory.getName() + "-liberty-devc-metadata.xml");

FileWriter metaFileWriter = null;
XMLStreamWriter metadataWriter = null;
try {
XMLStreamWriter metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(metaFile)) ;
metaFileWriter = new FileWriter(metaFile);
metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(metaFileWriter) ;
metadataWriter.writeStartDocument();
metadataWriter.writeStartElement("devcModeMetaData");
writeElement(metadataWriter, "containerEngine", isDocker ? DEVC_CONTAINER_DOCKER : DEVC_CONTAINER_PODMAN);
Expand All @@ -5713,10 +5724,21 @@ public void writeDevcMetadata(boolean alive) {
writeElement(metadataWriter, "containerRunOpts", containerRunOpts);
metadataWriter.writeEndElement();
metadataWriter.writeEndDocument();
metadataWriter.flush();
metadataWriter.close();
} catch (Exception e) {
warn("Failed to write metadata.\n" + e.getMessage());
} finally {
try {
if (metadataWriter != null) {
metadataWriter.flush();
metadataWriter.close();
}
if (metaFileWriter != null) {
metaFileWriter.flush();
metaFileWriter.close();
}
} catch (Exception e) {
warn("Failed to close metadata writer due to an error.\n" + e.getMessage());
}
}
}

Expand Down