summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/viewer/ViewerVirtViewer.java
blob: d696e109067c29433782d376e4516f7ac9b13bf0 (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
94
95
96
97
package org.openslx.runvirt.viewer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openslx.runvirt.virtualization.LibvirtHypervisor;
import org.openslx.runvirt.virtualization.LibvirtHypervisorException;
import org.openslx.runvirt.virtualization.LibvirtVirtualMachine;
import org.openslx.virtualization.Version;

/**
 * Virtual Viewer (virt-viewer) to view one or several displays of a virtual machine.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class ViewerVirtViewer extends Viewer
{
	/**
	 * Name of the Virtual Machine Manager program.
	 */
	private final static String NAME = "virt-viewer";

	/**
	 * Maximum number of supported displays by the Virtual Viewer.
	 */
	private final static int NUM_SUPPORTED_DISPLAYS = Integer.MAX_VALUE;

	/**
	 * Creates a new Virtual Viewer for a Libvirt virtual machine running on a Libvirt hypervisor.
	 * 
	 * @param machine virtual machine to display.
	 * @param hypervisor remote (hypervisor) endpoint for the viewer to connect to.
	 */
	public ViewerVirtViewer( LibvirtVirtualMachine machine, LibvirtHypervisor hypervisor )
	{
		super( ViewerVirtViewer.NAME, ViewerVirtViewer.NUM_SUPPORTED_DISPLAYS, machine, hypervisor );
	}

	@Override
	public Version getVersion() throws ViewerException
	{
		final Version version;

		// execute viewer process with arguments: 
		// "virt-viewer --version"
		final String versionOutput = ViewerUtils.executeViewer( ViewerVirtViewer.NAME,
				new String[] { "--version" } );

		if ( versionOutput == null ) {
			version = null;
		} else {
			// parse version from the viewer's process output
			final Pattern viewerVersionPattern = Pattern.compile( "(\\d+).(\\d+)" );
			final Matcher viewerVersionMatcher = viewerVersionPattern.matcher( versionOutput );

			// check if version pattern was found
			if ( viewerVersionMatcher.find() ) {
				final short major = Short.valueOf( viewerVersionMatcher.group( 1 ) );
				final short minor = Short.valueOf( viewerVersionMatcher.group( 2 ) );
				version = new Version( major, minor );
			} else {
				version = null;
			}
		}

		return version;
	}

	@Override
	public void render() throws ViewerException
	{
		String connectionUri = null;
		String machineUuid = null;

		// get URI of the hypervisor connection and UUID of the machine
		try {
			connectionUri = this.getHypervisor().getConnectionUri();
			machineUuid = this.getMachine().getConfiguration().getUuid();
		} catch ( LibvirtHypervisorException e ) {
			throw new ViewerException(
					"Failed to retrieve the URI of the hypervisor backend or the UUID of the machine to display: "
							+ e.getLocalizedMessage() );
		}

		// check if URI of the hypervisor connection and UUID of the machine is specified, otherwise abort
		if ( connectionUri == null || connectionUri.isEmpty() || machineUuid == null || machineUuid.isEmpty() ) {
			throw new ViewerException(
					"The URI of the hypervisor backend or the UUID of the machine to display is missing!" );
		}

		// execute viewer process with arguments:
		// "virt-viewer --full-screen --wait --attach --connect=<URI> --domain-name -- <DOMAIN-UUID>"
		ViewerUtils.executeViewer( ViewerVirtViewer.NAME, new String[] { "--full-screen", "--wait",
				"--attach", "--connect=" + connectionUri, "--uuid", "--", machineUuid } );
	}
}