summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java
blob: c86ddfa94b1ab7102369f8d5df5c075189a38ba3 (plain) (tree)
1
2
3
4
5
6
7
8


                                
 
                                   

                                                 
                                                    





                                       




                                                                                  










                                                                                                                   




                                                                                     


                                           

                                                                                                           
                                                               
 

                                                                                                          
 

                                                                                  

                                                                   
                                                 

                                                                                                                                   
                         
 




                                                                                                   
                         













                                                                                                                                      
                         
                                                                                                                         
 
                                           
 




                                                                                                                          
 




                                                                                                                                                     

                 
 
package org.openslx.dozmod.util;

import org.apache.log4j.Logger;

import org.openslx.dozmod.Branding;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.model.ContainerDefinition;
import org.openslx.thrifthelper.TConst;
import org.openslx.util.vm.DiskImage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class ImageWrapper {

	private static final Logger LOGGER = Logger.getLogger(ImageWrapper.class);

	/**
	 * @param virtualizerId     string constant defined in @link org.openslx.thrifthelper.TConst
	 * @param tmpDiskFile       dont no!
	 * @param imageName         The name of that image in the list.
	 * @param destDir           Destination directory on the local host.
	 * @param osId              Operating system identifier of the selected image.
	 * @param virtualizerConfig raw byte array of virtualizerconfig in imageversion table. Only the Image knows
	 *                          how to use it.
	 */
	public static void unpack(String virtualizerId, File tmpDiskFile, String imageName, File destDir,
			int osId, byte[] virtualizerConfig) {

		// 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)

		DiskImage diskImage = null;
		String ext = virtualizerId;

		// unwrap each image individually
		// TODO In future maybe this is a check to distinguish between VM-Image and Container Image
		if (virtualizerId.equals(TConst.VIRT_DOCKER)) {

			ContainerDefinition conDef = ContainerDefinition.fromByteArray(virtualizerConfig);
			conDef.saveLocal(destDir);

			try {
				// delete image file, unused in container Context.
				Files.delete(tmpDiskFile.toPath());

			} catch (IOException e) {
				Gui.asyncMessageBox("Konnte temporäre Download Datei nicht löschen", MessageType.WARNING, LOGGER,
						e);
			}

		} else {
			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, virtualizerConfig, 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);
			}
		}
	}
}