diff options
| author | Jonathan Bauer | 2014-12-03 16:42:30 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2014-12-03 16:42:30 +0100 |
| commit | 9a516660027565dba812df7337ea461e241b0b03 (patch) | |
| tree | e9c230aaf7cc9161d3271da5ee5675aa7c947879 | |
| parent | • minor changes (diff) | |
| download | tutor-module-9a516660027565dba812df7337ea461e241b0b03.tar.gz tutor-module-9a516660027565dba812df7337ea461e241b0b03.tar.xz tutor-module-9a516660027565dba812df7337ea461e241b0b03.zip | |
[client] read the hardware version from the vmdk file and use it in the vmx.
| -rw-r--r-- | dozentenmodul/src/main/java/ftp/DownloadTask.java | 70 | ||||
| -rwxr-xr-x | dozentenmodul/src/main/resources/txt/vmx_template | 2 |
2 files changed, 71 insertions, 1 deletions
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<Void, Void> { } /** * 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<Void, Void> { 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<Void, Void> { 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<Void, Void> { } 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; + } } diff --git a/dozentenmodul/src/main/resources/txt/vmx_template b/dozentenmodul/src/main/resources/txt/vmx_template index bc576e27..1b666d2f 100755 --- a/dozentenmodul/src/main/resources/txt/vmx_template +++ b/dozentenmodul/src/main/resources/txt/vmx_template @@ -11,7 +11,7 @@ monitor.virtual_mmu = "automatic" monitor.virtual_exec = "automatic" # id -virtualHW.version = "7" +virtualHW.version = "%VM_HW_VERSION%" displayName = "%VM_DISPLAY_NAME%" guestOS = "%VM_GUEST_OS%" |
