diff options
author | ralph isenmann | 2020-07-17 13:57:33 +0200 |
---|---|---|
committer | ralph isenmann | 2020-09-15 13:45:40 +0200 |
commit | e836cb7b54716aab6e1e4d1771b5a729bc628b63 (patch) | |
tree | 57fc8ed2f3aec5f0b96fffebcb3deedab8ecf498 | |
parent | [Client] minor changes in DockerfileUpdatePage; use DockerMetaDataDummy (VmMe... (diff) | |
download | tutor-module-e836cb7b54716aab6e1e4d1771b5a729bc628b63.tar.gz tutor-module-e836cb7b54716aab6e1e4d1771b5a729bc628b63.tar.xz tutor-module-e836cb7b54716aab6e1e4d1771b5a729bc628b63.zip |
[client] Modify Image Download to handle Dockerfiles
rename after successful download the ".part" file to "dockerfile"
Misunderstanding: The docker file is not a diskImage, but is downloaded as such. The docker file is rather a description file (~ .vmx ?)
3 files changed, 107 insertions, 48 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java index 66f9efe9..8c51b75f 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java @@ -4,7 +4,6 @@ import java.awt.Frame; import java.awt.Window; import java.io.File; import java.io.FileNotFoundException; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -32,11 +31,9 @@ import org.openslx.bwlp.thrift.iface.TAuthorizationException; import org.openslx.bwlp.thrift.iface.TInvocationException; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.bwlp.thrift.iface.TransferInformation; -import org.openslx.bwlp.thrift.iface.TransferState; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.bwlp.thrift.iface.WhoamiInfo; import org.openslx.dozmod.App; -import org.openslx.dozmod.Branding; import org.openslx.dozmod.Config; import org.openslx.dozmod.Config.SavedSession; import org.openslx.dozmod.authentication.Authenticator.AuthenticationData; @@ -54,14 +51,12 @@ import org.openslx.dozmod.thrift.cache.LectureCache; import org.openslx.dozmod.thrift.cache.MetaDataCache; import org.openslx.dozmod.thrift.cache.UserCache; import org.openslx.dozmod.util.FormatHelper; +import org.openslx.dozmod.util.ImageWrapper; import org.openslx.dozmod.util.VmWrapper; -import org.openslx.dozmod.util.VmWrapper.MetaDataMissingException; import org.openslx.sat.thrift.version.Version; import org.openslx.thrifthelper.ThriftManager; import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; -import org.openslx.util.vm.DiskImage; -import org.openslx.util.vm.DiskImage.UnknownImageFormatException; public class ThriftActions { @@ -471,46 +466,8 @@ public class ThriftActions { dlTask.addListener(new TransferEventListener() { @Override public void update(TransferEvent event) { - if (event.state != TransferState.FINISHED) - return; - DiskImage diskImage = null; - String ext = virtualizerId; - try { - diskImage = new DiskImage(tmpDiskFile); - } catch (IOException | UnknownImageFormatException e) { - LOGGER.warn("Could not open downloaded image for analyze step", e); - } - if (diskImage != null) { - if (diskImage.format != null) { - ext = diskImage.format.extension; - } - if (diskImage.isCompressed) { - String msg = "<html>Die heruntergeladene VM '" + imageName + "' ist ein komprimiertes " - + "Abbild.<br>Sie müssen das Abbild dekomprimieren, bevor Sie es verändern " - + "können.<br> Die VM wird lokal voraussichtlich nicht startfähig sein!" - + "<br><br>Bitte lesen Sie die Hinweise unter " - + "<a href=\"" + Branding.getServiceFAQWebsite() + "\">" - + "VMDK Disk Types</a>"; - - Gui.asyncMessageBox(msg, MessageType.WARNING, null, - null); - } - } - File destImage = new File(destDir.getAbsolutePath(), VmWrapper.generateFilename( - imageName, ext)); - destImage.delete(); - if (!tmpDiskFile.renameTo(destImage)) { - destImage = tmpDiskFile; // Must be Windows... - } - try { - VmWrapper.wrapVm(destImage, imageName, fTransInf.getMachineDescription(), - virtualizerId, osId, diskImage); - } catch (MetaDataMissingException | IOException e) { - Gui.asyncMessageBox( - "Zur heruntergeladenen VM konnte keine vmx-Datei angelegt werden." - + "\nSie können versuchen, das Abbild manuell in den VMWare-Player zu importieren.", - MessageType.WARNING, LOGGER, e); - } + ImageWrapper.unpack(event, virtualizerId, tmpDiskFile, imageName, destDir, osId, + fTransInf); } }); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java new file mode 100644 index 00000000..82e45472 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java @@ -0,0 +1,101 @@ +package org.openslx.dozmod.util; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.TransferInformation; +import org.openslx.bwlp.thrift.iface.TransferState; +import org.openslx.dozmod.Branding; +import org.openslx.dozmod.filetransfer.TransferEvent; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.thrifthelper.TConst; +import org.openslx.util.vm.DiskImage; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +public class ImageWrapper { + + private static final Logger LOGGER = Logger.getLogger(ImageWrapper.class); + + + public static void unpack(TransferEvent event, String virtualizerId, File tmpDiskFile, String imageName, + File destDir, int osId, TransferInformation fTransInf) { + + // after the whole image is downloaded and persisted as a .part file, + // this will be executed to unpack it. + // TODO Move into new Class (implements TransferEventListener) + + /* + * It seems only the diskfile of UploadWizardState will be downloaded. + * In Context of Container this would be a Container Image. + * + */ + if (event.state != TransferState.FINISHED) + return; + DiskImage diskImage = null; + String ext = virtualizerId; + + // TODO: Currently when downloading a Docker Image, the dockerfile will be downloaded + // just rename the .part file to dockerfile + + if (virtualizerId.equals(TConst.VIRT_DOCKER)) { + // + File destImage = new File(destDir.getAbsolutePath(),"dockerfile"); + + try { + // use move(..) because of platform independence + Files.move(tmpDiskFile.toPath(), + destImage.toPath(), + StandardCopyOption.REPLACE_EXISTING + ); + } catch (IOException e) { + Gui.asyncMessageBox("Konnte dockerfile nicht an Ziel kopieren", + MessageType.WARNING, LOGGER, e); + } + return; + } + // TODO: Container images have currently no diskImage, prevent creating one + try { + diskImage = new DiskImage(tmpDiskFile); + } catch (IOException | DiskImage.UnknownImageFormatException e) { + LOGGER.warn("Could not open downloaded image for analyze step", e); + } + + if (diskImage != null) { + if (diskImage.format != null) { + ext = diskImage.format.extension; + } + if (diskImage.isCompressed) { + String msg = "<html>Die heruntergeladene VM '" + imageName + "' ist ein komprimiertes " + + "Abbild.<br>Sie müssen das Abbild dekomprimieren, bevor Sie es verändern " + + "können.<br> Die VM wird lokal voraussichtlich nicht startfähig sein!" + + "<br><br>Bitte lesen Sie die Hinweise unter " + + "<a href=\"" + Branding.getServiceFAQWebsite() + "\">" + + "VMDK Disk Types</a>"; + + Gui.asyncMessageBox(msg, MessageType.WARNING, null, + null); + } + } + File destImage = new File(destDir.getAbsolutePath(), VmWrapper.generateFilename( + imageName, ext)); + + destImage.delete(); + + if (!tmpDiskFile.renameTo(destImage)) { + destImage = tmpDiskFile; // Must be Windows... + } + try { + VmWrapper.wrapVm(destImage, imageName, fTransInf.getMachineDescription(), + virtualizerId, osId, diskImage); + + } catch (VmWrapper.MetaDataMissingException | IOException e) { + Gui.asyncMessageBox( + "Zur heruntergeladenen VM konnte keine vmx-Datei angelegt werden." + + "\nSie können versuchen, das Abbild manuell in den VMWare-Player zu importieren.", + MessageType.WARNING, LOGGER, e); + } + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java index 38a61282..58093471 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java @@ -103,8 +103,9 @@ public class VmWrapper { if (vmMeta instanceof VmwareMetaData) { fileType = ".vmx"; // Copy BIOS with both floppies enabled - try (FileOutputStream output = new FileOutputStream(new File(diskFile.getAbsoluteFile() - .getParentFile(), "nvram")); InputStream input = ResourceLoader.getStream("/data/nvram")) { + try { + FileOutputStream output = new FileOutputStream(new File(diskFile.getAbsoluteFile().getParentFile(), "nvram")); + InputStream input = ResourceLoader.getStream("/data/nvram"); IOUtils.copy(input, output); } catch (Exception e) { Gui.asyncMessageBox("Konnte das BIOS für die VM nicht kopieren", MessageType.ERROR, LOGGER, e); |