From 9a516660027565dba812df7337ea461e241b0b03 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 3 Dec 2014 16:42:30 +0100 Subject: [client] read the hardware version from the vmdk file and use it in the vmx. --- dozentenmodul/src/main/java/ftp/DownloadTask.java | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'dozentenmodul/src/main/java/ftp/DownloadTask.java') diff --git a/dozentenmodul/src/main/java/ftp/DownloadTask.java b/dozentenmodul/src/main/java/ftp/DownloadTask.java index ae607c8a..fae25c68 100644 --- a/dozentenmodul/src/main/java/ftp/DownloadTask.java +++ b/dozentenmodul/src/main/java/ftp/DownloadTask.java @@ -1,9 +1,14 @@ package ftp; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Map; @@ -142,6 +147,7 @@ public class DownloadTask extends SwingWorker { } /** * Helper to generate the vmx for the downloaded image + * * @return true|false indicating the success of the file creation */ private boolean generateVmx() { @@ -166,6 +172,12 @@ public class DownloadTask extends SwingWorker { LOGGER.error("Image's version: " + Image.image.getVersion()); return false; } + int hardwareVersion = extractHardwareVersion(saveDir + File.separator + imageData.get("path").replaceFirst("^prod/", "")); + if (hardwareVersion == 0) { + LOGGER.error("'extractHardwareVersion' returned 0 indicating some problem. See logs."); + LOGGER.error("Falling back to default hardware version of '10'."); + hardwareVersion = 10; + } // TODO: sanity checks on the content of imageData would be good here... // use the information we received about the image vmxTemplate = vmxTemplate.replace("%VM_DISPLAY_NAME%", imageData.get("name")); @@ -173,6 +185,7 @@ public class DownloadTask extends SwingWorker { vmxTemplate = vmxTemplate.replace("%VM_CPU_COUNT%", imageData.get("cpu")); vmxTemplate = vmxTemplate.replace("%VM_RAM_SIZE%", String.valueOf(Integer.valueOf(imageData.get("ram")) * 1024)); vmxTemplate = vmxTemplate.replace("%VM_DISK_PATH%", imageData.get("path").replaceFirst("^prod/", "")); + vmxTemplate = vmxTemplate.replace("%VM_HW_VERSION%", String.valueOf(hardwareVersion)); // build filename for the vmx, basicly the same as the path of the vmdk // just without the leading "prod/" and "vmx" instead of "vmdk" at the end. @@ -187,4 +200,61 @@ public class DownloadTask extends SwingWorker { } return true; } + + /** + * Helper to extract the hardware version of the VMDK file by inspecting its content. + * + * @return value of hardware version as integer. A return value of 0 indicates an error. + */ + private int extractHardwareVersion(String path) { + BufferedReader br = null; + try { + try { + br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); + String line; + // first 4 characters of a VMDK file start with 'KDMV' + // first lets check if this is the case + line = br.readLine(); + if (!line.subSequence(0, 4).equals("KDMV")) { + LOGGER.error("Did not see 'KDMV' as first chars of the VMDK! Returning 0."); + LOGGER.debug("First line was: " + line); + LOGGER.debug("First 4 characters of it: " + line.subSequence(0, 4)); + return 0; + } + // only read a maximum of 20 lines, just in case... + int round = 0; + while ((line = br.readLine()) != null || round > 20) { + if (line.matches("^ddb\\.virtualHWVersion.*")) { + String[] tmp = line.split("="); + // we should get 2 strings only after the split, lets be sure + if (tmp.length != 2) { + LOGGER.debug("Splitting returned more than 2 parts, this should not happen!"); + return 0; + } + int candidate = Integer.parseInt(tmp[1].trim().replace("\"", "")); + LOGGER.debug("Considering hardware version: " + candidate); + if (candidate > 0) { + LOGGER.debug("Valid value of the candidate. Using hardware version of: " + candidate); + return candidate; + } else { + LOGGER.error("Candidate is not > 0! Returning 0."); + return 0; + } + } + round++; + } + LOGGER.error("Failed to find hardware version. Tried " + round + " rounds."); + } + finally { + br.close(); + } + } + catch (FileNotFoundException e) { + LOGGER.debug("File not found, see trace: ", e); + } + catch (IOException e) { + LOGGER.debug("I/O Exception, see trace: ", e); + } + return 0; + } } -- cgit v1.2.3-55-g7522