summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java
blob: c86ddfa94b1ab7102369f8d5df5c075189a38ba3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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);
			}
		}
	}
}