VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/samples/perl/clienttest.pl@ 62425

最後變更 在這個檔案從62425是 42063,由 vboxsync 提交於 12 年 前

webservice/clienttest.pl: finally make SOAP::Lite deal with enum types - what a totally non-scalable hack.

  • 屬性 svn:eol-style 設為 native
檔案大小: 7.2 KB
 
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
33use strict;
34use SOAP::Lite;
35use vboxService; # generated by stubmaker, see SDKRef.pdf
36use Data::Dumper;
37
38my $cmd = 'clienttest';
39my $optMode;
40my $vmname;
41my $disk;
42
43while (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 " $cmd openhd <disk>: open disk image <disk>.\n";
56 exit 0;
57 }
58 elsif ( ($this eq 'version')
59 || ($this eq 'list')
60 )
61 {
62 $optMode = $this;
63 }
64 elsif ( ($this eq 'startvm')
65 || ($this eq 'acpipowerbutton')
66 )
67 {
68 $optMode = $this;
69
70 if (!($vmname = shift(@ARGV)))
71 {
72 die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
73 }
74 }
75 elsif ($this eq 'openhd')
76 {
77 $optMode = $this;
78
79 if (!($disk = shift(@ARGV)))
80 {
81 die "[$cmd] Missing parameter: You must specify the name of the disk to open.\nStopped";
82 }
83 }
84 else
85 {
86 die "[$cmd] Unknown option \"$this\"; stopped";
87 }
88}
89
90$optMode = "list"
91 if (!$optMode);
92
93# SOAP::Lite hacking to make it serialize the enum types we use correctly.
94# In the long run, this needs to be done either by stubmaker.pl or something
95# else, because the WSDL clearly says they're restricted strings. Quite silly
96# that the default behavior is to ignore the parameter and just let the server
97# use the default value for the type.
98
99sub SOAP::Serializer::as_LockType
100{
101 my ($self, $value, $name, $type, $attr) = @_;
102 die "String value expected instead of @{[ref $value]} reference\n"
103 if ref $value;
104 return [
105 $name,
106 {'xsi:type' => 'vbox:LockType', %$attr},
107 SOAP::Utils::encode_data($value)
108 ];
109}
110
111sub SOAP::Serializer::as_DeviceType
112{
113 my ($self, $value, $name, $type, $attr) = @_;
114 die "String value expected instead of @{[ref $value]} reference\n"
115 if ref $value;
116 return [
117 $name,
118 {'xsi:type' => 'vbox:DeviceType', %$attr},
119 SOAP::Utils::encode_data($value)
120 ];
121}
122
123sub SOAP::Serializer::as_AccessMode
124{
125 my ($self, $value, $name, $type, $attr) = @_;
126 die "String value expected instead of @{[ref $value]} reference\n"
127 if ref $value;
128 return [
129 $name,
130 {'xsi:type' => 'vbox:AccessMode', %$attr},
131 SOAP::Utils::encode_data($value)
132 ];
133}
134
135## @todo needs much more error handling, e.g. openhd never complains
136
137my $vbox = vboxService->IWebsessionManager_logon("test", "test");
138
139if (!$vbox)
140{
141 die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
142}
143
144if ($optMode eq "version")
145{
146 my $v = vboxService->IVirtualBox_getVersion($vbox);
147 print "[$cmd] Version number of running VirtualBox web service: $v\n";
148}
149elsif ($optMode eq "list")
150{
151 print "[$cmd] Listing machines:\n";
152 my @result = vboxService->IVirtualBox_getMachines($vbox);
153 foreach my $idMachine (@result)
154 {
155 my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
156 my $name = vboxService->IMachine_getName($idMachine);
157
158 print "machine $if $idMachine: $name\n";
159 }
160}
161elsif ($optMode eq "startvm")
162{
163 my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
164
165 die "[$cmd] Cannot find VM \"$vmname\"; stopped"
166 if (!$machine);
167
168 my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
169 die "[$cmd] Cannot get session object; stopped"
170 if (!$session);
171
172 my $uuid = vboxService->IMachine_getId($machine);
173 die "[$cmd] Cannot get uuid for machine; stopped"
174 if (!$uuid);
175
176 print "[$cmd] UUID: $uuid\n";
177
178 my $progress = vboxService->IMachine_launchVMProcess($machine,
179 $session,
180 "headless",
181 "");
182 die "[$cmd] Cannot launch VM; stopped"
183 if (!$progress);
184
185 print("[$cmd] Waiting for the VM to start...\n");
186 vboxService->IProgress_waitForCompletion($progress, -1);
187
188 my $fCompleted;
189 $fCompleted = vboxService->IProgress_getCompleted($progress);
190 print("[$cmd] Completed: $fCompleted\n");
191
192 my $resultCode;
193 $resultCode = vboxService->IProgress_getResultCode($progress);
194
195 print("[$cmd] Result: $resultCode\n");
196
197 vboxService->ISession_unlockMachine($session);
198
199 vboxService->IWebsessionManager_logoff($vbox);
200}
201elsif ($optMode eq "acpipowerbutton")
202{
203 my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
204
205 die "[$cmd] Cannot find VM \"$vmname\"; stopped"
206 if (!$machine);
207
208 my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
209 die "[$cmd] Cannot get session object; stopped"
210 if (!$session);
211
212 vboxService->IMachine_lockMachine($machine, $session, 'Shared');
213
214 my $console = vboxService->ISession_getConsole($session);
215
216 vboxService->IConsole_powerButton($console);
217
218 vboxService->ISession_unlockMachine($session);
219
220 vboxService->IWebsessionManager_logoff($vbox);
221}
222elsif ($optMode eq "openhd")
223{
224 my $medium = vboxService->IVirtualBox_openMedium($vbox, $disk,
225 'HardDisk',
226 'ReadWrite',
227 0);
228}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette