VirtualBox

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

最後變更 在這個檔案從69383是 69383,由 vboxsync 提交於 7 年 前

webservices: scm updates

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 7.2 KB
 
1#!/usr/bin/perl
2# $Id: clienttest.pl 69383 2017-10-26 16:39:15Z vboxsync $
3## @file
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# Note! The following license applies to this file only
9#
10
11#
12# Copyright (C) 2008-2012 Oracle Corporation
13#
14# Permission is hereby granted, free of charge, to any person
15# obtaining a copy of this software and associated documentation
16# files (the "Software"), to deal in the Software without
17# restriction, including without limitation the rights to use,
18# copy, modify, merge, publish, distribute, sublicense, and/or sell
19# copies of the Software, and to permit persons to whom the
20# Software is furnished to do so, subject to the following
21# conditions:
22#
23# The above copyright notice and this permission notice shall be
24# included in all copies or substantial portions of the Software.
25#
26# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33# OTHER DEALINGS IN THE SOFTWARE.
34#
35
36use strict;
37use SOAP::Lite;
38use vboxService; # generated by stubmaker, see SDKRef.pdf
39use Data::Dumper;
40
41my $cmd = 'clienttest';
42my $optMode;
43my $vmname;
44my $disk;
45
46while (my $this = shift(@ARGV))
47{
48 if (($this =~ /^-h/) || ($this =~ /^--help/))
49 {
50 print "$cmd: test the VirtualBox web service.\n".
51 "Usage:\n".
52 " $cmd <mode>\n".
53 "with <mode> being one of 'version', 'list', 'start'; default is 'list'.\n".
54 " $cmd version: print version of VirtualBox web service.\n".
55 " $cmd list: list installed virtual machines.\n".
56 " $cmd startvm <vm>: start the virtual machine named <vm>.\n".
57 " $cmd acpipowerbutton <vm>: shutdown of the irtual machine named <vm>.\n";
58 " $cmd openhd <disk>: open disk image <disk>.\n";
59 exit 0;
60 }
61 elsif ( ($this eq 'version')
62 || ($this eq 'list')
63 )
64 {
65 $optMode = $this;
66 }
67 elsif ( ($this eq 'startvm')
68 || ($this eq 'acpipowerbutton')
69 )
70 {
71 $optMode = $this;
72
73 if (!($vmname = shift(@ARGV)))
74 {
75 die "[$cmd] Missing parameter: You must specify the name of the VM to start.\nStopped";
76 }
77 }
78 elsif ($this eq 'openhd')
79 {
80 $optMode = $this;
81
82 if (!($disk = shift(@ARGV)))
83 {
84 die "[$cmd] Missing parameter: You must specify the name of the disk to open.\nStopped";
85 }
86 }
87 else
88 {
89 die "[$cmd] Unknown option \"$this\"; stopped";
90 }
91}
92
93$optMode = "list"
94 if (!$optMode);
95
96# SOAP::Lite hacking to make it serialize the enum types we use correctly.
97# In the long run, this needs to be done either by stubmaker.pl or something
98# else, because the WSDL clearly says they're restricted strings. Quite silly
99# that the default behavior is to ignore the parameter and just let the server
100# use the default value for the type.
101
102sub SOAP::Serializer::as_LockType
103{
104 my ($self, $value, $name, $type, $attr) = @_;
105 die "String value expected instead of @{[ref $value]} reference\n"
106 if ref $value;
107 return [
108 $name,
109 {'xsi:type' => 'vbox:LockType', %$attr},
110 SOAP::Utils::encode_data($value)
111 ];
112}
113
114sub SOAP::Serializer::as_DeviceType
115{
116 my ($self, $value, $name, $type, $attr) = @_;
117 die "String value expected instead of @{[ref $value]} reference\n"
118 if ref $value;
119 return [
120 $name,
121 {'xsi:type' => 'vbox:DeviceType', %$attr},
122 SOAP::Utils::encode_data($value)
123 ];
124}
125
126sub SOAP::Serializer::as_AccessMode
127{
128 my ($self, $value, $name, $type, $attr) = @_;
129 die "String value expected instead of @{[ref $value]} reference\n"
130 if ref $value;
131 return [
132 $name,
133 {'xsi:type' => 'vbox:AccessMode', %$attr},
134 SOAP::Utils::encode_data($value)
135 ];
136}
137
138## @todo needs much more error handling, e.g. openhd never complains
139
140my $vbox = vboxService->IWebsessionManager_logon("test", "test");
141
142if (!$vbox)
143{
144 die "[$cmd] Logon to session manager with user \"test\" and password \"test\" failed.\nStopped";
145}
146
147if ($optMode eq "version")
148{
149 my $v = vboxService->IVirtualBox_getVersion($vbox);
150 print "[$cmd] Version number of running VirtualBox web service: $v\n";
151}
152elsif ($optMode eq "list")
153{
154 print "[$cmd] Listing machines:\n";
155 my @result = vboxService->IVirtualBox_getMachines($vbox);
156 foreach my $idMachine (@result)
157 {
158 my $if = vboxService->IManagedObjectRef_getInterfaceName($idMachine);
159 my $name = vboxService->IMachine_getName($idMachine);
160
161 print "machine $if $idMachine: $name\n";
162 }
163}
164elsif ($optMode eq "startvm")
165{
166 my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
167
168 die "[$cmd] Cannot find VM \"$vmname\"; stopped"
169 if (!$machine);
170
171 my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
172 die "[$cmd] Cannot get session object; stopped"
173 if (!$session);
174
175 my $uuid = vboxService->IMachine_getId($machine);
176 die "[$cmd] Cannot get uuid for machine; stopped"
177 if (!$uuid);
178
179 print "[$cmd] UUID: $uuid\n";
180
181 my $progress = vboxService->IMachine_launchVMProcess($machine,
182 $session,
183 "headless",
184 "");
185 die "[$cmd] Cannot launch VM; stopped"
186 if (!$progress);
187
188 print("[$cmd] Waiting for the VM to start...\n");
189 vboxService->IProgress_waitForCompletion($progress, -1);
190
191 my $fCompleted;
192 $fCompleted = vboxService->IProgress_getCompleted($progress);
193 print("[$cmd] Completed: $fCompleted\n");
194
195 my $resultCode;
196 $resultCode = vboxService->IProgress_getResultCode($progress);
197
198 print("[$cmd] Result: $resultCode\n");
199
200 vboxService->ISession_unlockMachine($session);
201
202 vboxService->IWebsessionManager_logoff($vbox);
203}
204elsif ($optMode eq "acpipowerbutton")
205{
206 my $machine = vboxService->IVirtualBox_findMachine($vbox, $vmname);
207
208 die "[$cmd] Cannot find VM \"$vmname\"; stopped"
209 if (!$machine);
210
211 my $session = vboxService->IWebsessionManager_getSessionObject($vbox);
212 die "[$cmd] Cannot get session object; stopped"
213 if (!$session);
214
215 vboxService->IMachine_lockMachine($machine, $session, 'Shared');
216
217 my $console = vboxService->ISession_getConsole($session);
218
219 vboxService->IConsole_powerButton($console);
220
221 vboxService->ISession_unlockMachine($session);
222
223 vboxService->IWebsessionManager_logoff($vbox);
224}
225elsif ($optMode eq "openhd")
226{
227 my $medium = vboxService->IVirtualBox_openMedium($vbox, $disk,
228 'HardDisk',
229 'ReadWrite',
230 0);
231}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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