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












                                                         
                                

                           






























                                                                                                                





                                                                                                                                    
                                                                                
                                                                  

                                                                                   

                                                    















































                                                                                                                                             











                                                                                
 
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.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

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 {
				// TODO: (RALPH) dockerfile == virtualizerconfig(table imageversion)  ... maybe not a goot solution!
				writeDockerfile(destDir, fTransInf.getMachineDescription());

				// delete image file because thats also the dockerfile
				Files.delete(tmpDiskFile.toPath());

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

	private static void writeDockerfile(File destDir, byte[] dockerfile_b) {
		String filename = "dockerfile";
		File dockerfile = new File(destDir,filename);
		try {
			FileOutputStream fos = new FileOutputStream(dockerfile);
			fos.write(dockerfile_b);
			fos.close();
		} catch (IOException e) {
			LOGGER.error("could not write dockerfile", e);
		}
	}
}