1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | #
|
---|
4 | # This little perl program attempts to connect to a running VirtualBox
|
---|
5 | # webservice and calls various methods on it. Please refer to the SDK
|
---|
6 | # programming reference (SDKRef.pdf) for how to use this sample.
|
---|
7 | #
|
---|
8 | # Copyright (C) 2008-2010 Oracle Corporation
|
---|
9 | #
|
---|
10 | # The following license applies to this file only:
|
---|
11 | #
|
---|
12 | # Permission is hereby granted, free of charge, to any person
|
---|
13 | # obtaining a copy of this software and associated documentation
|
---|
14 | # files (the "Software"), to deal in the Software without
|
---|
15 | # restriction, including without limitation the rights to use,
|
---|
16 | # copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
17 | # sell copies of the Software, and to permit persons to whom the
|
---|
18 | # Software is furnished to do so, subject to the following conditions:
|
---|
19 | #
|
---|
20 | # The above copyright notice and this permission notice shall be
|
---|
21 | # included in all copies or substantial portions of the Software.
|
---|
22 | #
|
---|
23 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
24 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
25 | # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
26 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
27 | # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
28 | # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
29 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
30 | # OTHER DEALINGS IN THE SOFTWARE.
|
---|
31 | #
|
---|
32 |
|
---|
33 | use strict;
|
---|
34 | use SOAP::Lite;
|
---|
35 | use vboxService; # generated by stubmaker, see SDKRef.pdf
|
---|
36 | use Data::Dumper;
|
---|
37 |
|
---|
38 | my $cmd = 'clienttest';
|
---|
39 | my $optMode;
|
---|
40 | my $vmname;
|
---|
41 | my $disk;
|
---|
42 |
|
---|
43 | while (my $this = shift(@ARGV))
|
---|
44 | {
|
---|
45 | if (($this =~ /^-h/) || ($this =~ /^--help/))
|
---|
46 | {
|
---|
47 | print "$cmd: test the VirtualBox web service.\n".
|
---|
48 | "Usage:\n".
|
---|
49 | " $cmd <mode>\n".
|
---|
50 | "with <mode> being one of 'version', 'list', 'start'; default is 'list'.\n".
|
---|
51 | " $cmd version: print version of VirtualBox web service.\n".
|
---|
52 | " $cmd list: list installed virtual machines.\n".
|
---|
53 | " $cmd startvm <vm>: start the virtual machine named <vm>.\n";
|
---|
54 | exit 0;
|
---|
55 | }
|
---|
56 | elsif ( ($this eq 'version')
|
---|
57 | || ($this eq 'list')
|
---|
58 | )
|
---|
59 | {
|
---|
60 | $optMode = $this;
|
---|
61 | }
|
---|
62 | elsif ($this eq 'startvm')
|
---|
63 | {
|
---|
64 | $optMode = $this;
|
---|
65 |
|
---|
66 | if (!($vmname = shift(@ARGV)))
|
---|
67 | {
|
---|
68 | die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
|
---|
69 | }
|
---|
70 | }
|
---|
71 | elsif ($this eq 'openhd')
|
---|
72 | {
|
---|
73 | $optMode = $this;
|
---|
74 |
|
---|
75 | if (!($disk = shift(@ARGV)))
|
---|
76 | {
|
---|
77 | die "[$cmd] Missing parameter: You must specify the name of the disk to open.\nStopped";
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | die "[$cmd] Unknown option \"$this\"; stopped";
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | $optMode = "list"
|
---|
87 | if (!$optMode);
|
---|
88 |
|
---|
89 | my $vbox = vboxService->IWebsessionManager_logon("test", "test");
|
---|
90 |
|
---|
91 | if (!$vbox)
|
---|
92 | {
|
---|
93 | die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
|
---|
94 | }
|
---|
95 |
|
---|
96 | if ($optMode eq "version")
|
---|
97 | {
|
---|
98 | my $v = vboxService->IVirtualBox_getVersion($vbox);
|
---|
99 | print "[$cmd] Version number of running VirtualBox web service: $v\n";
|
---|
100 | }
|
---|
101 | elsif ($optMode eq "list")
|
---|
102 | {
|
---|
103 | print "[$cmd] Listing machines:\n";
|
---|
104 | my @result = vboxService->IVirtualBox_getMachines($vbox);
|
---|
105 | foreach my $idMachine (@result)
|
---|
106 | {
|
---|
107 | my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
|
---|
108 | my $name = vboxService->IMachine_getName($idMachine);
|
---|
109 |
|
---|
110 | print "machine $if $idMachine: $name\n";
|
---|
111 | }
|
---|
112 | }
|
---|
113 | elsif ($optMode eq "startvm")
|
---|
114 | {
|
---|
115 | # assume it's a UUID
|
---|
116 | my $machine = vboxService->IVirtualBox_getMachine($vbox, $vmname);
|
---|
117 | if (!$machine)
|
---|
118 | {
|
---|
119 | # no: then try a name
|
---|
120 | $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
|
---|
121 | }
|
---|
122 |
|
---|
123 | die "[$cmd] Cannot find VM \"$vmname\"; stopped"
|
---|
124 | if (!$machine);
|
---|
125 |
|
---|
126 | my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
|
---|
127 | die "[$cmd] Cannot get session object; stopped"
|
---|
128 | if (!$session);
|
---|
129 |
|
---|
130 | my $uuid = vboxService->IMachine_getId($machine);
|
---|
131 | die "[$cmd] Cannot get uuid for machine; stopped"
|
---|
132 | if (!$uuid);
|
---|
133 |
|
---|
134 | print "[$cmd] UUID: $uuid\n";
|
---|
135 |
|
---|
136 | my $progress = vboxService->IVirtualBox_openRemoteSession($vbox,
|
---|
137 | $session,
|
---|
138 | $uuid,
|
---|
139 | "vrdp",
|
---|
140 | "");
|
---|
141 | die "[$cmd] Cannot open remote session; stopped"
|
---|
142 | if (!$progress);
|
---|
143 |
|
---|
144 | print("[$cmd] Waiting for the remote session to open...\n");
|
---|
145 | vboxService->IProgress_waitForCompletion($progress, -1);
|
---|
146 |
|
---|
147 | my $fCompleted;
|
---|
148 | $fCompleted = vboxService->IProgress_getCompleted($progress);
|
---|
149 | print("[$cmd] Completed: $fCompleted\n");
|
---|
150 |
|
---|
151 | my $resultCode;
|
---|
152 | $resultCode = vboxService->IProgress_getResultCode($progress);
|
---|
153 |
|
---|
154 | print("[$cmd] Result: $resultCode\n");
|
---|
155 |
|
---|
156 | vboxService->ISession_close($session);
|
---|
157 |
|
---|
158 | vboxService->IWebsessionManager_logoff($vbox);
|
---|
159 | }
|
---|
160 | elsif ($optMode eq "openhd")
|
---|
161 | {
|
---|
162 | my $harddisk = vboxService->IVirtualBox_openHardDisk($vbox, $disk, 1, 0, "", 0, "");
|
---|
163 | }
|
---|