get(
"/restart",
(req, res) -> {
try {
int[] rs = new int[4];
rs[0] =
Runtime.getRuntime()
.exec(new String[] {"bash", "-c", "echo \"restart\" > /hostpipe/mypipe"})
.waitFor();
rs[1] =
Runtime.getRuntime()
.exec(
new String[] {
"bash", "-c", "while [ ! -f /hostpipe/output_ready ]; do sleep 1; done"
})
.waitFor();
List<String> lines = Files.readAllLines(Path.of("/hostpipe/output.txt"));
rs[2] = Path.of("/hostpipe/output.txt").toFile().delete() ? 0 : 1;
rs[3] = Path.of("/hostpipe/output_ready").toFile().delete() ? 0 : 1;
List<String> restartResults = new LinkedList<>(lines);
restartResults.add(Arrays.toString(rs));
Map<String, Object> model = new HashMap<>();
model.put("restart", restartResults);
return new VelocityTemplateEngine().render(new ModelAndView(model, "index-view.html"));
} catch (Exception e) {
return "Error: " + e.getMessage();
}
});