summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java
blob: fe884a156fe46332f07fd82ea0b1394d6a5591e8 (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
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.I18n;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.thrifthelper.TConst;
import org.openslx.virtualization.configuration.VirtualizationConfigurationException;
import org.openslx.virtualization.configuration.container.ContainerDefinition;
import org.openslx.virtualization.disk.DiskImage;
import org.openslx.virtualization.disk.DiskImage.ImageFormat;
import org.openslx.virtualization.disk.DiskImageException;

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

/**
 * This Class handle the unpacking of Images downloaded from
 * the server individually (VM-Images, Docker-Images (Container Images)).
 *
 * In case of Container-Images the Image-Blob is not important because it can be recreated with the
 * recipe (e.g. dockerfile).
 * TODO Do not download the Image-Blob of a Container-Image.
 */
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! Maybe the part file
	 * @param imageName         The name of the 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.

		String ext = virtualizerId;
		boolean imageIsCompressed = false;
		ImageFormat imageFormat = null;

		// 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 diskImage = DiskImage.newInstance(tmpDiskFile)) {
				imageFormat = diskImage.getFormat();
				ext = imageFormat.getExtension();
				imageIsCompressed = diskImage.isCompressed();
			} catch (IOException | DiskImageException e) {
				LOGGER.warn("Could not open/analyze downloaded image", e);
			}

			if (imageIsCompressed && imageFormat != ImageFormat.QCOW2) {
				Gui.asyncMessageBox(I18n.THRIFT.getString("ThriftActions.Message.warning.diskImageCompressed",
						imageName, Branding.getServiceFAQWebsite()), 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 (DiskImage diskImage = DiskImage.newInstance(destImage)) {
				VmWrapper.wrapVm(destImage, imageName, virtualizerConfig, virtualizerId, osId, diskImage);
			} catch (VirtualizationConfigurationException | IOException | DiskImageException e) {
				Gui.asyncMessageBox(I18n.THRIFT.getString("ThriftActions.Message.warning.couldNotWrapVM"),
						MessageType.WARNING, LOGGER, e);
			}
		}
	}
}