summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ImageWrapper.java
blob: 2c0e612318bc9ffa9de3bc4e8d9d0aade4d75adb (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
package org.openslx.dozmod.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.disk.DiskImage;
import org.openslx.virtualization.disk.DiskImage.ImageFormat;
import org.openslx.virtualization.disk.DiskImageException;

import java.io.File;
import java.io.IOException;

/**
 * This Class handle the unpacking of Images downloaded from
 * the server individually (VM-Images, Docker-Images (Container Images)).
 */
public class ImageWrapper {

	private static final Logger LOGGER = LogManager.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 container image individually
		if (virtualizerId.equals(TConst.VIRT_DOCKER)) {

			ContainerWrapper.unwrapContainer(tmpDiskFile, imageName, destDir, virtualizerConfig);

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