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-2012 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 | " $cmd acpipowerbutton <vm>: shutdown of the irtual machine named <vm>.\n";
|
---|
55 | exit 0;
|
---|
56 | }
|
---|
57 | elsif ( ($this eq 'version')
|
---|
58 | || ($this eq 'list')
|
---|
59 | )
|
---|
60 | {
|
---|
61 | $optMode = $this;
|
---|
62 | }
|
---|
63 | elsif ( ($this eq 'startvm')
|
---|
64 | || ($this eq 'acpipowerbutton')
|
---|
65 | )
|
---|
66 | {
|
---|
67 | $optMode = $this;
|
---|
68 |
|
---|
69 | if (!($vmname = shift(@ARGV)))
|
---|
70 | {
|
---|
71 | die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
|
---|
72 | }
|
---|
73 | }
|
---|
74 | elsif ($this eq 'openhd')
|
---|
75 | {
|
---|
76 | $optMode = $this;
|
---|
77 |
|
---|
78 | if (!($disk = shift(@ARGV)))
|
---|
79 | {
|
---|
80 | die "[$cmd] Missing parameter: You must specify the name of the disk to open.\nStopped";
|
---|
81 | }
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | die "[$cmd] Unknown option \"$this\"; stopped";
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | $optMode = "list"
|
---|
90 | if (!$optMode);
|
---|
91 |
|
---|
92 | my $vbox = vboxService->IWebsessionManager_logon("test", "test");
|
---|
93 |
|
---|
94 | if (!$vbox)
|
---|
95 | {
|
---|
96 | die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
|
---|
97 | }
|
---|
98 |
|
---|
99 | if ($optMode eq "version")
|
---|
100 | {
|
---|
101 | my $v = vboxService->IVirtualBox_getVersion($vbox);
|
---|
102 | print "[$cmd] Version number of running VirtualBox web service: $v\n";
|
---|
103 | }
|
---|
104 | elsif ($optMode eq "list")
|
---|
105 | {
|
---|
106 | print "[$cmd] Listing machines:\n";
|
---|
107 | my @result = vboxService->IVirtualBox_getMachines($vbox);
|
---|
108 | foreach my $idMachine (@result)
|
---|
109 | {
|
---|
110 | my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
|
---|
111 | my $name = vboxService->IMachine_getName($idMachine);
|
---|
112 |
|
---|
113 | print "machine $if $idMachine: $name\n";
|
---|
114 | }
|
---|
115 | }
|
---|
116 | elsif ($optMode eq "startvm")
|
---|
117 | {
|
---|
118 | my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
|
---|
119 |
|
---|
120 | die "[$cmd] Cannot find VM \"$vmname\"; stopped"
|
---|
121 | if (!$machine);
|
---|
122 |
|
---|
123 | my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
|
---|
124 | die "[$cmd] Cannot get session object; stopped"
|
---|
125 | if (!$session);
|
---|
126 |
|
---|
127 | my $uuid = vboxService->IMachine_getId($machine);
|
---|
128 | die "[$cmd] Cannot get uuid for machine; stopped"
|
---|
129 | if (!$uuid);
|
---|
130 |
|
---|
131 | print "[$cmd] UUID: $uuid\n";
|
---|
132 |
|
---|
133 | my $progress = vboxService->IMachine_launchVMProcess($machine,
|
---|
134 | $session,
|
---|
135 | "headless",
|
---|
136 | "");
|
---|
137 | die "[$cmd] Cannot launch VM; stopped"
|
---|
138 | if (!$progress);
|
---|
139 |
|
---|
140 | print("[$cmd] Waiting for the VM to start...\n");
|
---|
141 | vboxService->IProgress_waitForCompletion($progress, -1);
|
---|
142 |
|
---|
143 | my $fCompleted;
|
---|
144 | $fCompleted = vboxService->IProgress_getCompleted($progress);
|
---|
145 | print("[$cmd] Completed: $fCompleted\n");
|
---|
146 |
|
---|
147 | my $resultCode;
|
---|
148 | $resultCode = vboxService->IProgress_getResultCode($progress);
|
---|
149 |
|
---|
150 | print("[$cmd] Result: $resultCode\n");
|
---|
151 |
|
---|
152 | vboxService->ISession_unlockMachine($session);
|
---|
153 |
|
---|
154 | vboxService->IWebsessionManager_logoff($vbox);
|
---|
155 | }
|
---|
156 | elsif ($optMode eq "acpipowerbutton")
|
---|
157 | {
|
---|
158 | my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
|
---|
159 |
|
---|
160 | die "[$cmd] Cannot find VM \"$vmname\"; stopped"
|
---|
161 | if (!$machine);
|
---|
162 |
|
---|
163 | my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
|
---|
164 | die "[$cmd] Cannot get session object; stopped"
|
---|
165 | if (!$session);
|
---|
166 |
|
---|
167 | my $shared = SOAP::Data->type('vbox:LockType')->name('session')->value('Shared');
|
---|
168 | vboxService->IMachine_lockMachine($machine, $session, $shared);
|
---|
169 |
|
---|
170 | my $console = vboxService->ISession_getConsole($session);
|
---|
171 |
|
---|
172 | vboxService->IConsole_powerButton($console);
|
---|
173 |
|
---|
174 | vboxService->ISession_unlockMachine($session);
|
---|
175 |
|
---|
176 | vboxService->IWebsessionManager_logoff($vbox);
|
---|
177 | }
|
---|
178 | elsif ($optMode eq "openhd")
|
---|
179 | {
|
---|
180 | my $medium = vboxService->IVirtualBox_openMedium($vbox, $disk, 3, # DeviceType_HardDisk
|
---|
181 | 1, # AccessMode_ReadWrite
|
---|
182 | 0, # false
|
---|
183 | "", # empty uuid
|
---|
184 | 0, # false
|
---|
185 | ""); #empty uuid
|
---|
186 | }
|
---|