blib/lib/Xen/API.pm

filter: Statementstmt 12.2% 48 / 393 Branchbran 0.0% 0 / 210 Conditioncond 0.0% 0 / 189 MC/DCmcdc 0.0% 0 / 91 Subroutinesub 30.4% 14 / 46 Podpod 93.5% 29 / 31 totaltotal 9.4% 91 / 960 CC 182 SCAR 58.6
CC
182
Cov
6%
transfer_vm
1056.0
script
650.0
import_vm
420.0
CRAP 350.0
↑ Summary
1 =head1 NAME
2  
3 Xen::API Perl interface to the Xen RPC-XML API.
4  
5 =head1 SYNOPSIS
6  
7 use Xen::API;
8 my $x = Xen::API->new;
9  
10 my %vms = $x->list_vms
11 my %templates = $x->list_templates
12  
13 my $vm = $x->create_vm(template=>'my_template',cpu=>4,memory=>'16G',vmname=>'this_vm');
14  
15 my $vm_records = $x->Xen::API::VM::get_all_records();
16  
17 =head1 DESCRIPTION
18  
19 Perl interface to the Xen RPC-XML API. Contains some shortcuts for creating,
20 destroying, importing, and exporting VMs. All RPC API commands are available in
21 the Xen::API:: package space. Simply replace the dots with :: and prepend
22 Xen::API:: to the command, and execute it as if it were a perl function. Be
23 sure to pass the Xen object as the first parameter.
24  
25 =head1 METHODS
26  
27 =cut
28  
29 package Xen::API;
30 1 ▶use RPC::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 $RPC::XML::FORCE_STRING_ENCODING = 1;
32 1 ▶use RPC::XML::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use IO::Prompt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Net::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Number::Format qw(:subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 require Exporter;
46 1 our @ISA = qw(Exporter);
47  
48 1 our @EXPORT_OK=qw(bool true false string Int i4 i8 double datetime
49 nil base64 array struct fault prompt mem xen run);
50 1 our %EXPORT_TAGS=(all=>\@EXPORT_OK);
51 1 our $PACKAGE_PREFIX = __PACKAGE__;
52  
53 1 our $VERSION = '0.08';
54  
55 =head2 prompt
56  
57 Display a password prompt.
58  
59 =cut
60  
61 0 ▶sub prompt {
Subroutine prompt: not called CC=2 SCAR=17.9
Pod documented
62 0 ▶ my $message = shift || 'Enter password: ';
Condition0%0/2
shift() || 'Enter password: '
l!l
00
MC/DC0%0/1
shift() || 'Enter password: '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
63 0 IO::Prompt::prompt($message, -e=>'', '-tty').'';
64 }
65  
66 =head2 mem
67  
68 Convert suffix notation (k, M, G) to byte count. Useful for writing memory to give
69 to VM.
70  
71 =cut
72  
73 0 ▶sub mem { unformat_number(@_) }
Subroutine mem: not called CC=1 SCAR=6.9
Pod documented
74  
75 =head2 bool true false string Int i4 i8 double datetime nil base64 array struct fault
76  
77 shortcuts for RPC::XML explicit typecasts
78  
79 =cut
80  
81 0 ▶sub bool { RPC::XML::boolean->new(@_) }
Subroutine bool: not called CC=1 SCAR=6.9
Pod documented
82 0 ▶sub true { RPC::XML::boolean->new(1) }
Subroutine true: not called CC=1 SCAR=6.9
Pod documented
83 0 ▶sub false { RPC::XML::boolean->new(0) }
Subroutine false: not called CC=1 SCAR=6.9
Pod documented
84 0 ▶sub string { RPC::XML::string->new(@_) }
Subroutine string: not called CC=1 SCAR=6.9
Pod documented
85 0 ▶sub Int { RPC::XML::int->new(@_) }
Subroutine Int: not called CC=1 SCAR=6.9
Pod documented
86 0 ▶sub i4 { RPC::XML::i4->new(@_) }
Subroutine i4: not called CC=1 SCAR=6.9
Pod documented
87 0 ▶sub i8 { RPC::XML::i8->new(@_) }
Subroutine i8: not called CC=1 SCAR=6.9
Pod documented
88 0 ▶sub double { RPC::XML::double->new(@_) }
Subroutine double: not called CC=1 SCAR=6.9
Pod documented
89 0 ▶sub datetime { RPC::XML::datetime_iso8601->new(@_) }
Subroutine datetime: not called CC=1 SCAR=6.9
Pod documented
90 0 ▶sub nil { RPC::XML::nil->new(@_) }
Subroutine nil: not called CC=1 SCAR=6.9
Pod documented
91 0 ▶sub base64 { RPC::XML::base64->new(@_) }
Subroutine base64: not called CC=1 SCAR=6.9
Pod documented
92 0 ▶sub array { RPC::XML::array->new(@_) }
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
93 0 ▶sub struct { RPC::XML::struct->new(@_) }
Subroutine struct: not called CC=1 SCAR=6.9
Pod documented
94 0 ▶sub fault { RPC::XML::fault->new(@_) }
Subroutine fault: not called CC=1 SCAR=6.9
Pod documented
95  
96 =head2 xen
97  
98 Create a new instance of a Xen class.
99  
100 =cut
101  
102 0 ▶sub xen {Xen::API->new(@_)}
Subroutine xen: not called CC=1 SCAR=6.9
Pod documented
103  
104 =head2 new($uri, $user, $password)
105  
106 New Xen instance.
107  
108 =cut
109  
110 0 ▶sub new {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
111 0 ▶ my $class = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or return 0 0 0
112 0 ▶ my $uri = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or return 0 0 0
113 0 ▶ my $user = shift || 'root';
Condition0%0/2
shift() || 'root'
l!l
00
MC/DC0%0/1
shift() || 'root'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
114 0 my $password = shift;
115  
116 0 my $self = {};
117 0 bless $self, $class;
118  
119 0 ▶ $uri = "http://$uri" if !URI->new($uri)->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless "URI"->new($uri)->scheme 0 0 0
120 0 $self->{host} = URI->new($uri)->host;
121 0 $self->{uri} = $uri;
122 0 $self->{xen} = RPC::XML::Client->new($self->{uri});
123  
124 # set up autoload packages for Xen API.
125 0 my %seen;
126 0 my $listMethods = $self->{xen}->simple_request('system.listMethods');
127 0 ▶ die $RPC::XML::ERROR if $RPC::XML::ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::XML::ERROR 0 0 0
128 my %classes =
129 map {(
130 0 ▶ __PACKAGE__."::$_"=>__PACKAGE__,
Branch: 1 branch
BranchTrueFalseTotal
$Xen::API::PACKAGE_PREFIX ? : 0 0 0
131 $PACKAGE_PREFIX? ("${PACKAGE_PREFIX}::$_"=>$PACKAGE_PREFIX) : ($_=>undef),
132 )}
133 0 ▶ map {s/\.[^.]*$//; s/\./::/g; !$seen{$_}++?$_:()}
Branch: 1 branch
BranchTrueFalseTotal
!$seen{$_}++ ? : 0 0 0
134 0 ▶ @{$listMethods||[]};
Condition0%0/2
$listMethods || []
l!l
00
MC/DC0%0/1
$listMethods || []
$listMethods
Truth table0%0/2
A
A= $listMethods
Aresult
11
00
135 0 for my $c (keys %classes) {
136 0 my $package = $classes{$c};
137 0 my $eval = <<EOS;
138 package $c;
139 no warnings 'redefine';
140 our \$AUTOLOAD;
141 sub AUTOLOAD {
142 my \$self = shift;
143 \$AUTOLOAD=~s/^\\Q\${package}::\\E// if defined \$package;
144 \$AUTOLOAD=~s/::/./g;
145 \$self->request(\$AUTOLOAD,\@_);
146 };
147 EOS
148 0 eval $eval;
149 }
150  
151 # login
152 0 $self->{user} = $user;
153 0 ▶ $password = prompt("Enter xen admin password for ".$self->{uri}.": ")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 0 0
154 if !defined($password);
155 0 my $response = $self->{xen}->simple_request('session.login_with_password',$user,$password);
156 0 ▶ die $RPC::XML::ERROR if $RPC::XML::ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::XML::ERROR 0 0 0
157 0 $self->{session} = $self->value($response);
158 0 return $self;
159 }
160  
161 =head2 create_vm
162  
163 Create a new VM.
164  
165 Arguments:
166 - vmname - The xen name of the VM.
167 - template - The template to base the VM from.
168 - cpu - How many CPUs to assign
169 - memory - How much memory to assign
170  
171 Returns a ref to the newly created VM.
172  
173 =cut
174  
175 0 ▶sub create_vm {
Subroutine create_vm: not called CC=15 SCAR=54.8
Pod documented
176 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
177  
178 # read arguments
179 0 my %args = @_;
180 0 my $vmname = $args{vmname};
181 0 my $template = $args{template};
182 0 ▶ die "No template name given" if !defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 0 0
183 0 my $cpu=$args{cpu};
184 0 my $memory=$args{memory};
185 0 ▶ die "No VM name given" if !defined $vmname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vmname 0 0 0
186  
187 # get the list of VMs and templates in this pool
188 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
189 0 ▶ my @templates = grep {$vms{$_}{is_a_template} && @{$vms{$_}{VBDs}||[]}} keys %vms;
Condition0%0/5
$vms{$_}{'is_a_template'} && @{$vms{$_}{'VBDs'} or [];}
!ll&&!rl&&r
000
$vms{$_}{'VBDs'} || []
l!l
00
MC/DC0%0/2
$vms{$_}{'is_a_template'} && @{$vms{$_}{'VBDs'} or [];}
$vms{$_}{'is_a_template'} @{$vms{$_}{'VBDs'} or [];}
MC/DC0%0/1
$vms{$_}{'VBDs'} || []
$vms{$_}{'VBDs'}
Truth table0%0/3
A && B
A= $vms{$_}{'is_a_template'}B= @{$vms{$_}{'VBDs'} or [];}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $vms{$_}{'VBDs'}
Aresult
11
00
190  
191 # query for the template by name or uuid
192 my @use_template = grep {
193 0 $vms{$_}{name_label} eq $template
194 0 ▶ || $vms{$_}{uuid} eq $template
Condition0%0/6
$vms{$_}{'name_label'} eq $template || $vms{$_}{'uuid'} eq $template || $_ eq $template
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $template or $vms{$_}{"uuid"} eq $template
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $template || $vms{$_}{'uuid'} eq $template || $_ eq $template
$vms{$_}{"name_label"} eq $template $vms{$_}{"uuid"} eq $template $_ eq $template
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $templateB= $vms{$_}{"uuid"} eq $templateC= $_ eq $template
ABCresult
1XX1
01X1
0011
0000
195 || $_ eq $template} @templates;
196 0 ▶ die "No template named \"$template\"!\n" if !@use_template;
Branch: 1 branch
BranchTrueFalseTotal
unless @use_template 0 0 0
197 die "Multiple templates found matching \"$template\":\n"
198 0 ▶ .join(', ',map {"\"$vms{$_}{name_label}\" ($vms{$_}{uuid})"} @use_template)
Branch: 1 branch
BranchTrueFalseTotal
if @use_template > 1 0 0 0
199 if @use_template>1;
200 0 my $use_template = $use_template[0];
201  
202 # clone the template into a new VM
203 0 my $new_vm = $self->Xen::API::VM::clone($use_template,$vmname);
204  
205 # set number of VCPUs
206 0 ▶ if (defined($cpu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cpu) 0 0 0
207 0 $self->Xen::API::VM::set_VCPUs_max($new_vm,$cpu);
208 0 $self->Xen::API::VM::set_VCPUs_at_startup($new_vm,$cpu);
209 }
210  
211 # set memory. There seem to be two mutually incompatible APIs: One used by
212 # XenAPI 6.1, and another for earlier XenAPI versions. Try both, and
213 # hopefully one of them will succeed.
214 0 ▶ if (defined($memory)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $memory) 0 0 0
215 0 my $mem = unformat_number($memory);
216  
217 # new API, try this first
218 0 my @err;
219 0 eval {
220 0 $self->Xen::API::VM::set_memory_limits($new_vm,$mem,$mem,$mem,$mem);
221 };
222 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
223 0 push @err, $@;
224 # try the old API if the new API call fails
225 0 eval {
226 0 $self->Xen::API::VM::set_memory_dynamic_min($new_vm,$mem);
227 0 $self->Xen::API::VM::set_memory_dynamic_max($new_vm,$mem);
228 0 $self->Xen::API::VM::set_memory_static_min($new_vm,$mem);
229 0 $self->Xen::API::VM::set_memory_static_max($new_vm,$mem);
230 };
231 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
232 0 push @err, $@;
233 0 die "Could not set memory for $vmname: \n".join("\n",@err);
234 }
235 }
236 }
237  
238 # provision the VM
239 0 $self->Xen::API::VM::provision($new_vm);
240  
241 # start the VM
242 0 $self->Xen::API::VM::start($new_vm,false,true);
243  
244 0 my $ip = $self->get_ip($new_vm);
245 0 print STDERR "IP address for $vmname: $ip\n";
246 0 return $new_vm;
247 }
248  
249 =head2 script
250  
251 Run a remote script on a VM guest over SSH.
252  
253 Arguments:
254 - script - Remote script file to run on the guest via SSH
255 - vmname - Name of the VM where the script should be run
256 - user - SSH user name for running a remote command on the guest
257 - password - SSH password for running a remote command on the guest
258 - port - SSH port for running a remote command on the guest
259 - sudo - Should sudo be used to run a remote command on the guest?
260  
261 =cut
262  
263 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
264 1 my $lastpassword;
265 0 ▶ sub script {
Subroutine script: not called CC=25 SCAR=64.8
Pod documented
266 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
267 0 my %args = @_;
268 0 ▶ my $vmname = $args{vmname} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = $args{"vmname"} or return 0 0 0
269 0 my $script = $args{script};
270 0 my $command = $args{command};
271 0 my $user = $args{user};
272 0 my $port = $args{port};
273 0 my $sudo = $args{sudo};
274 0 ▶ my $password = exists($args{password})?$args{password}:$lastpassword;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'password'} ? : 0 0 0
275 0 ▶ die "No command or script was given" if !defined($command) && !defined($script);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command or defined $script 0 0 0
Condition0%0/3
defined $command or defined $script
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $command or defined $script
defined $command defined $script
Truth table0%0/3
A or B
A= defined $commandB= defined $script
ABresult
1X1
011
000
276  
277 # find the VM
278 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
279 0 my @vms = grep {$vms{$_}{name_label} eq $vmname
280 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
281 || $_ eq $vmname} keys %vms;
282 0 ▶ die "Multiple VMs matched $vmname" if @vms > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @vms > 1 0 0 0
283 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
284 0 ▶ die "VM $vmname is not running" if ($vms{$vm}{power_state}||'') ne 'Running';
Branch: 1 branch
BranchTrueFalseTotal
if ($vms{$vm}{"power_state"} || "") ne "Running" 0 0 0
Condition0%0/2
$vms{$vm}{'power_state'} || ''
l!l
00
MC/DC0%0/1
$vms{$vm}{'power_state'} || ''
$vms{$vm}{'power_state'}
Truth table0%0/2
A
A= $vms{$vm}{'power_state'}
Aresult
11
00
285  
286 # prompt for password
287 0 ▶ if ((exists($args{password}) || $sudo) && !defined($password)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"password"} || $sudo and not defined $password) 0 0 0
Condition0%0/6
exists $args{"password"} || $sudo and not defined $password
!ll&&!rl&&r
000
exists $args{"password"} or $sudo
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $args{"password"} || $sudo and not defined $password
exists $args{"password"} $sudo not defined $password
Truth table0%0/5
A or B and not(C)
A= exists $args{"password"}B= $sudoC= not defined $password
ABCresult
00X0
1X00
0100
1X11
0111
288 0 $password = prompt("Enter login password: ");
289 }
290 0 $lastpassword = $password;
291  
292 0 ▶ my $ip = $self->get_ip($vm)
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $self->get_ip($vm) or die "Could not determine IP address of $vmname" 0 0 0
293 or die "Could not determine IP address of $vmname";
294  
295 # read the contents of the file to a string
296 0 ▶ if (defined($script) && !defined($command)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script and not defined $command) 0 0 0
Condition0%0/3
defined $script and not defined $command
!ll&&!rl&&r
000
MC/DC0%0/2
defined $script and not defined $command
defined $script not defined $command
Truth table0%0/3
A and not(B)
A= defined $scriptB= not defined $command
ABresult
0X0
100
111
297 0 ▶ die "Could not read script file $script" if !-r $script;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $script 0 0 0
298 0 $command = do {local(@ARGV, $/) = $script; <>};
299 }
300  
301 # Run the remote command using SSH.
302 0 ▶ my $ssh = Net::OpenSSH->new($ip,
Branch: 3 branches
BranchTrueFalseTotal
defined $user ? : 0 0 0
defined $password ? : 0 0 0
defined $port ? : 0 0 0
303 defined($user)?(user=>$user):(),
304 defined($password)?(password=>$password):(),
305 defined($port)?(port=>$port):(),
306 master_opts=>[-o=>'StrictHostKeyChecking=no'],
307 );
308 0 ▶ die "Couldn't establish SSH connection: ".$ssh->error if $ssh->error;
Branch: 1 branch
BranchTrueFalseTotal
if $ssh->error 0 0 0
309 0 ▶ if ($sudo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sudo) { } 0 0 0
310 0 $ssh->system({stdin_data=>"$password\n$command"},
311 'sudo -Sk -p "" -- "$SHELL"');
312 }
313 else {
314 0 $ssh->system({stdin_data=>$command}, '"$SHELL"');
315 }
316 }
317 }
318  
319 =head2 get_ip
320  
321 Gets the IP address of a VM.
322  
323 =cut
324  
325 0 ▶sub get_ip {
Subroutine get_ip: not called CC=9 SCAR=45.0
Pod documented
326 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
327 0 ▶ my $vmname = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = shift() or return 0 0 0
328 0 my $maxwait = shift;
329 0 ▶ $maxwait = 60 if !defined($maxwait);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxwait 0 0 0
330  
331 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
332 my @vms = grep {
333 0 $vms{$_}{name_label} eq $vmname
334 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
335 || $_ eq $vmname} keys %vms;
336 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
337 0 ▶ my $ip = $self->_get_ip($vm, $maxwait)
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $self->_get_ip($vm, $maxwait) or die "Could not get IP address of VM ${vmname}: timeout" 0 0 0
338 or die "Could not get IP address of VM $vmname: timeout";
339 0 return $ip;
340 }
341  
342 0 ▶sub _get_ip {
Subroutine _get_ip: not called CC=12 SCAR=50.5
343 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
344 0 ▶ my $vm = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vm = shift() or return 0 0 0
345 0 my $maxwait = shift;
346 0 ▶ $maxwait=60 if !defined $maxwait;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxwait 0 0 0
347  
348 # get the IP address of the VM
349 0 my $wait=0;
350 0 my $ip;
351 0 ▶ while (!$ip && $wait < $maxwait) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ip and $wait < $maxwait) 0 0 0
Condition0%0/3
not $ip and $wait < $maxwait
!ll&&!rl&&r
000
MC/DC0%0/2
not $ip and $wait < $maxwait
not $ip $wait < $maxwait
Truth table0%0/3
not(A) and B
A= not $ipB= $wait < $maxwait
ABresult
0X0
100
111
352 0 eval {
353 0 my $vgm = $self->Xen::API::VM::get_guest_metrics($vm);
354 0 my $net = $self->Xen::API::VM_guest_metrics::get_networks($vgm);
355 0 ▶ $ip = $net->{'0/ip'} || $net->{'1/ip'} || $net->{'2/ip'} || $net->{'3/ip'} if $net;
Branch: 1 branch
BranchTrueFalseTotal
if $net 0 0 0
Condition0%0/9
$net->{'0/ip'} || $net->{'1/ip'} || $net->{'2/ip'} || $net->{'3/ip'}
l!l&&r!l&&!r
000
$net->{"0/ip"} or $net->{"1/ip"} or $net->{"2/ip"}
l!l&&r!l&&!r
000
$net->{"0/ip"} or $net->{"1/ip"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$net->{'0/ip'} || $net->{'1/ip'} || $net->{'2/ip'} || $net->{'3/ip'}
$net->{"0/ip"} $net->{"1/ip"} $net->{"2/ip"} $net->{'3/ip'}
Truth table0%0/5
A or B or C || D
A= $net->{"0/ip"}B= $net->{"1/ip"}C= $net->{"2/ip"}D= $net->{'3/ip'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
356 };
357 0 $wait++;
358 0 ▶ sleep 1 if !$ip && $wait < $maxwait;
Branch: 1 branch
BranchTrueFalseTotal
if not $ip and $wait < $maxwait 0 0 0
Condition0%0/3
not $ip and $wait < $maxwait
!ll&&!rl&&r
000
MC/DC0%0/2
not $ip and $wait < $maxwait
not $ip $wait < $maxwait
Truth table0%0/3
not(A) and B
A= not $ipB= $wait < $maxwait
ABresult
0X0
100
111
359 }
360 0 return $ip;
361 }
362  
363 =head2 destroy_vm
364  
365 Destroys a VM and its associated VDIs.
366  
367 =cut
368  
369 0 ▶sub destroy_vm {
Subroutine destroy_vm: not called CC=15 SCAR=54.8
Pod documented
370 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
371 0 ▶ my $vmname = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = shift() or return 0 0 0
372  
373 # find the VM
374 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
375 my @vms = grep {
376 0 $vms{$_}{name_label} eq $vmname
377 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
378 || $_ eq $vmname} keys %vms;
379 0 ▶ die "Multiple VMs matched $vmname" if @vms > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @vms > 1 0 0 0
380 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
381  
382 # make sure the VM is shut down
383 0 ▶ if (($vms{$vm}{power_state}||'') ne 'Halted') {
Branch: 1 branch
BranchTrueFalseTotal
if (($vms{$vm}{"power_state"} || "") ne "Halted") 0 0 0
Condition0%0/2
$vms{$vm}{'power_state'} || ''
l!l
00
MC/DC0%0/1
$vms{$vm}{'power_state'} || ''
$vms{$vm}{'power_state'}
Truth table0%0/2
A
A= $vms{$vm}{'power_state'}
Aresult
11
00
384 0 $self->Xen::API::VM::hard_shutdown($vm);
385 }
386  
387 # destroy the attached VDIs
388 0 ▶ for my $vbd (@{$vms{$vm}{VBDs}||[]}) {
Condition0%0/2
$vms{$vm}{'VBDs'} || []
l!l
00
MC/DC0%0/1
$vms{$vm}{'VBDs'} || []
$vms{$vm}{'VBDs'}
Truth table0%0/2
A
A= $vms{$vm}{'VBDs'}
Aresult
11
00
389 0 my $vbd_record = $self->Xen::API::VBD::get_record($vbd);
390 $self->Xen::API::VDI::destroy($vbd_record->{VDI})
391 if $vbd_record->{VDI}
392 && $vbd_record->{VDI} ne 'OpaqueRef:NULL'
393 0 ▶ && $vbd_record->{type} ne 'CD';
Branch: 1 branch
BranchTrueFalseTotal
if $vbd_record->{"VDI"} and $vbd_record->{"VDI"} ne "OpaqueRef:NULL" and $vbd_record->{"type"} ne "CD" 0 0 0
Condition0%0/6
$vbd_record->{"VDI"} and $vbd_record->{"VDI"} ne "OpaqueRef:NULL" and $vbd_record->{"type"} ne "CD"
!ll&&!rl&&r
000
$vbd_record->{"VDI"} and $vbd_record->{"VDI"} ne "OpaqueRef:NULL"
!ll&&!rl&&r
000
MC/DC0%0/3
$vbd_record->{"VDI"} and $vbd_record->{"VDI"} ne "OpaqueRef:NULL" and $vbd_record->{"type"} ne "CD"
$vbd_record->{"VDI"} $vbd_record->{"VDI"} ne "OpaqueRef:NULL" $vbd_record->{"type"} ne "CD"
Truth table0%0/4
A and B and C
A= $vbd_record->{"VDI"}B= $vbd_record->{"VDI"} ne "OpaqueRef:NULL"C= $vbd_record->{"type"} ne "CD"
ABCresult
0XX0
10X0
1100
1111
394 }
395  
396 #destroy the VM
397 0 $self->Xen::API::VM::destroy($vm);
398 0 return '';
399 }
400  
401 =head2 import_vm
402  
403 Import a VM from a xva file.
404  
405 =cut
406  
407 0 ▶sub import_vm {
Subroutine import_vm: not called CC=20 SCAR=60.4
Pod documented
408 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
409 0 ▶ my $filename = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or return 0 0 0
410 0 my $sr_id = shift;
411  
412 # find the storage repository if specified
413 0 my $sr_uuid;
414 0 ▶ if ($sr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sr_id) 0 0 0
415 0 ▶ my %sr = %{$self->Xen::API::SR::get_all_records||{}};
Condition0%0/2
$self->Xen::API::SR::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::SR::get_all_records || {}
$self->Xen::API::SR::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::SR::get_all_records
Aresult
11
00
416 my @srs = grep {
417 0 $sr{$_}{name_label} eq $sr_id
418 0 ▶ || $sr{$_}{uuid} eq $sr_id
Condition0%0/6
$sr{$_}{'name_label'} eq $sr_id || $sr{$_}{'uuid'} eq $sr_id || $_ eq $sr_id
l!l&&r!l&&!r
000
$sr{$_}{"name_label"} eq $sr_id or $sr{$_}{"uuid"} eq $sr_id
l!l&&r!l&&!r
000
MC/DC0%0/3
$sr{$_}{'name_label'} eq $sr_id || $sr{$_}{'uuid'} eq $sr_id || $_ eq $sr_id
$sr{$_}{"name_label"} eq $sr_id $sr{$_}{"uuid"} eq $sr_id $_ eq $sr_id
Truth table0%0/4
A or B || C
A= $sr{$_}{"name_label"} eq $sr_idB= $sr{$_}{"uuid"} eq $sr_idC= $_ eq $sr_id
ABCresult
1XX1
01X1
0011
0000
419 || $_ eq $sr_id} keys %sr;
420 0 ▶ my $sr = $srs[0]
Branch: 1 branch
BranchTrueFalseTotal
my $sr = $srs[0] or die "Could not find storage repository $sr_id" 0 0 0
421 or die "Could not find storage repository $sr_id";
422 0 $sr_uuid = $sr{$sr}{uuid};
423 }
424  
425 # create the source and destination tasks
426 0 my $task = $self->Xen::API::task::create("import_$filename","Import VM $filename");
427  
428 0 eval {
429 # URI
430 0 my $uri = URI->new($self->{uri});
431 0 $uri->path('import');
432 0 $uri->query_param(session_id=>$self->{session});
433 0 $uri->query_param(task_id=>$task);
434 0 ▶ $uri->query_param(sr_uuid=>$sr_uuid) if $sr_uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $sr_uuid 0 0 0
435  
436 0 ▶ my $import = Net::HTTP->new(Host=>$uri->host_port)
Branch: 1 branch
BranchTrueFalseTotal
my $import = "Net::HTTP"->new("Host", $uri->host_port) or die 'Could not connect to host at ' . $uri->host_port . ": $@" 0 0 0
437 or die "Could not connect to host at ".$uri->host_port.": $@";
438 0 $import->write_request(
439 PUT=>$uri->path_query,
440 'User-Agent'=>'perl-Xen-API');
441  
442 0 ▶ my $fh = FileHandle->new($filename, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new($filename, "r") or die "Could not open $filename for reading: $!" 0 0 0
443 or die "Could not open $filename for reading: $!";
444 0 $fh->binmode;
445 0 ▶ $import->print($_) while <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) and do { $import->print($_) } 0 0 0
446 0 $fh->close;
447  
448 # check HTTP status code
449 0 my ($code, $message, %headers) = $import->read_response_headers;
450 0 ▶ die "import returned HTTP Status code: $code" if $code != HTTP_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 200 0 0 0
451 };
452  
453 0 my $task_record = $self->Xen::API::task::get_record($task);
454  
455 # Wait for the task status to be updated
456 0 my $wait=0;
457 0 my $maxwait=60;
458 0 ▶ while ($task_record && ($task_record->{status}||'') eq 'pending'
Branch: 1 branch
BranchTrueFalseTotal
if ($task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait) 0 0 0
Condition0%0/8
$task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait
!ll&&!rl&&r
000
$task_record and ($task_record->{"status"} || "") eq "pending"
!ll&&!rl&&r
000
$task_record->{'status'} || ''
l!l
00
MC/DC0%0/3
$task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait
$task_record $task_record->{'status'} $wait < $maxwait
Truth table0%0/4
A and B and C
A= $task_recordB= $task_record->{'status'}C= $wait < $maxwait
ABCresult
0XX0
10X0
1100
1111
459 && $wait < $maxwait)
460 {
461 0 $task_record = $self->Xen::API::task::get_record($task);
462 0 sleep 1;
463 0 $wait++;
464 }
465  
466 0 $self->Xen::API::task::destroy($task);
467  
468 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
469 die "Import task returned status $task_record->{status}: "
470 0 ▶ .join(', ',@{$task_record->{error_info}||[]})
Condition0%0/2
$task_record->{'error_info'} || []
l!l
00
MC/DC0%0/1
$task_record->{'error_info'} || []
$task_record->{'error_info'}
Truth table0%0/2
A
A= $task_record->{'error_info'}
Aresult
11
00
471 0 ▶ if $task_record->{status} ne 'success';
Branch: 1 branch
BranchTrueFalseTotal
if $task_record->{"status"} ne "success" 0 0 0
472 0 return '';
473 }
474  
475 =head2 export_vm
476  
477 Export a VM to a xva file.
478  
479 =cut
480  
481 0 ▶sub export_vm {
Subroutine export_vm: not called CC=19 SCAR=59.4
Pod documented
482 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
483 0 ▶ my $vmname = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = shift() or return 0 0 0
484 0 ▶ my $filename = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or return 0 0 0
485  
486 # find the VM
487 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
488 my @vms = grep {
489 0 $vms{$_}{name_label} eq $vmname
490 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
491 || $_ eq $vmname} keys %vms;
492 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
493  
494 0 my $task = $self->Xen::API::task::create("export_$vm","Export VM $vm");
495  
496 # URI
497 0 my $uri = URI->new($self->{uri});
498 0 $uri->path('export');
499 0 $uri->query_param(session_id=>$self->{session});
500 0 $uri->query_param(task_id=>$task);
501 0 $uri->query_param(ref=>$vm);
502  
503 0 eval {
504 # export socket connection
505 0 ▶ my $export = Net::HTTP->new(Host=>$uri->host_port)
Branch: 1 branch
BranchTrueFalseTotal
my $export = "Net::HTTP"->new("Host", $uri->host_port) or die 'Could not connect to host at ' . $uri->host_port . ": $@" 0 0 0
506 or die "Could not connect to host at ".$uri->host_port.": $@";
507 0 $export->write_request(
508 GET=>$uri->path_query,
509 'User-Agent'=>'perl-Xen-API');
510  
511 # check HTTP status code
512 0 my ($code, $message, %headers) = $export->read_response_headers;
513 0 ▶ die "import returned HTTP Status code: $code" if $code != HTTP_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 200 0 0 0
514  
515 0 ▶ my $fh = FileHandle->new($filename, 'w')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new($filename, "w") or die "Could not open $filename for writing: $!" 0 0 0
516 or die "Could not open $filename for writing: $!";
517 0 $fh->binmode;
518 0 ▶ $fh->print($_) while <$export>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $export) and do { $fh->print($_) } 0 0 0
519 0 $fh->close;
520 };
521
522 0 my $task_record = $self->Xen::API::task::get_record($task);
523  
524 # Wait for the task status to be updated
525 0 my $wait=0;
526 0 my $maxwait=60;
527 0 ▶ while ($task_record && ($task_record->{status}||'') eq 'pending'
Branch: 1 branch
BranchTrueFalseTotal
if ($task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait) 0 0 0
Condition0%0/8
$task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait
!ll&&!rl&&r
000
$task_record and ($task_record->{"status"} || "") eq "pending"
!ll&&!rl&&r
000
$task_record->{'status'} || ''
l!l
00
MC/DC0%0/3
$task_record and ($task_record->{"status"} || "") eq "pending" and $wait < $maxwait
$task_record $task_record->{'status'} $wait < $maxwait
Truth table0%0/4
A and B and C
A= $task_recordB= $task_record->{'status'}C= $wait < $maxwait
ABCresult
0XX0
10X0
1100
1111
528 && $wait < $maxwait)
529 {
530 0 $task_record = $self->Xen::API::task::get_record($task);
531 0 sleep 1;
532 0 $wait++;
533 }
534 0 $self->Xen::API::task::destroy($task);
535  
536 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
537 die "Export task returned status $task_record->{status}: "
538 0 ▶ .join(', ',@{$task_record->{error_info}||[]})
Condition0%0/2
$task_record->{'error_info'} || []
l!l
00
MC/DC0%0/1
$task_record->{'error_info'} || []
$task_record->{'error_info'}
Truth table0%0/2
A
A= $task_record->{'error_info'}
Aresult
11
00
539 0 ▶ if $task_record->{status} ne 'success';
Branch: 1 branch
BranchTrueFalseTotal
if $task_record->{"status"} ne "success" 0 0 0
540 0 return '';
541 }
542  
543  
544 =head2 transfer_vm
545  
546 Transfer a VM from one xen server to another without creating an intermediate file.
547  
548 =cut
549  
550 0 ▶sub transfer_vm {
Subroutine transfer_vm: not called CC=32 SCAR=69.6
Pod documented
551 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
552 0 ▶ my $vmname = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = shift() or return 0 0 0
553 0 ▶ my $dest_xen = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dest_xen = shift() or return 0 0 0
554 0 my $sr_id = shift;
555  
556 # find the VM
557 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
558 my @vms = grep {
559 0 $vms{$_}{name_label} eq $vmname
560 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
561 || $_ eq $vmname} keys %vms;
562 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
563  
564 # find the storage repository if specified
565 0 my $sr_uuid;
566 0 ▶ if ($sr_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sr_id) 0 0 0
567 0 ▶ my %sr = %{$dest_xen->Xen::API::SR::get_all_records||{}};
Condition0%0/2
$dest_xen->Xen::API::SR::get_all_records || {}
l!l
00
MC/DC0%0/1
$dest_xen->Xen::API::SR::get_all_records || {}
$dest_xen->Xen::API::SR::get_all_records
Truth table0%0/2
A
A= $dest_xen->Xen::API::SR::get_all_records
Aresult
11
00
568 my @srs = grep {
569 0 $sr{$_}{name_label} eq $sr_id
570 0 ▶ || $sr{$_}{uuid} eq $sr_id
Condition0%0/6
$sr{$_}{'name_label'} eq $sr_id || $sr{$_}{'uuid'} eq $sr_id || $_ eq $sr_id
l!l&&r!l&&!r
000
$sr{$_}{"name_label"} eq $sr_id or $sr{$_}{"uuid"} eq $sr_id
l!l&&r!l&&!r
000
MC/DC0%0/3
$sr{$_}{'name_label'} eq $sr_id || $sr{$_}{'uuid'} eq $sr_id || $_ eq $sr_id
$sr{$_}{"name_label"} eq $sr_id $sr{$_}{"uuid"} eq $sr_id $_ eq $sr_id
Truth table0%0/4
A or B || C
A= $sr{$_}{"name_label"} eq $sr_idB= $sr{$_}{"uuid"} eq $sr_idC= $_ eq $sr_id
ABCresult
1XX1
01X1
0011
0000
571 || $_ eq $sr_id} keys %sr;
572 0 ▶ my $sr = $srs[0]
Branch: 1 branch
BranchTrueFalseTotal
my $sr = $srs[0] or die "Could not find storage repository $sr_id" 0 0 0
573 or die "Could not find storage repository $sr_id";
574 0 $sr_uuid = $sr{$sr}{uuid};
575 }
576  
577 # export task
578 0 my $export_task = $self->Xen::API::task::create("export_$vm","Export VM $vm");
579 # import task
580 0 my $import_task = $dest_xen->Xen::API::task::create("import_$vm","Import VM $vm");
581  
582 0 eval {
583 # export URI
584 0 my $export_uri = URI->new($self->{uri});
585 0 $export_uri->path('export');
586 0 $export_uri->query_param(session_id=>$self->{session});
587 0 $export_uri->query_param(task_id=>$export_task);
588 0 $export_uri->query_param(ref=>$vm);
589
590 # export socket connection
591 0 ▶ my $export = Net::HTTP->new(Host=>$export_uri->host_port)
Branch: 1 branch
BranchTrueFalseTotal
my $export = "Net::HTTP"->new("Host", $export_uri->host_port) or die 'Could not connect to host at ' . $export_uri->host_port . ": $@" 0 0 0
592 or die "Could not connect to host at ".$export_uri->host_port.": $@";
593 0 $export->write_request(
594 GET=>$export_uri->path_query,
595 'User-Agent'=>'perl-Xen-API');
596 0 { my ($code, $message, %headers) = $export->read_response_headers;
597 0 ▶ die "export returned HTTP Status code: $code" if $code != HTTP_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 200 0 0 0
598 }
599  
600 # import URI
601 0 my $import_uri = URI->new($dest_xen->{uri});
602 0 $import_uri->path('import');
603 0 $import_uri->query_param(session_id=>$dest_xen->{session});
604 0 $import_uri->query_param(task_id=>$import_task);
605 0 ▶ $import_uri->query_param(sr_uuid=>$sr_uuid) if $sr_uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $sr_uuid 0 0 0
606
607 # import socket connection
608 0 ▶ my $import = Net::HTTP->new(Host=>$import_uri->host_port)
Branch: 1 branch
BranchTrueFalseTotal
my $import = "Net::HTTP"->new("Host", $import_uri->host_port) or die 'Could not connect to host at ' . $import_uri->host_port . ": $@" 0 0 0
609 or die "Could not connect to host at ".$import_uri->host_port.": $@";
610 0 $import->write_request(
611 PUT=>$import_uri->path_query,
612 'User-Agent'=>'perl-Xen-API');
613  
614 # transfer the VM
615 0 ▶ $import->print($_) while <$export>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $export) and do { $import->print($_) } 0 0 0
616  
617 0 { my ($code, $message, %headers) = $export->read_response_headers;
618 0 ▶ die "export returned HTTP Status code: $code" if $code != HTTP_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != 200 0 0 0
619 }
620 };
621  
622 # get task statuses
623 0 my $export_task_record = $self->Xen::API::task::get_record($export_task);
624 0 my $import_task_record = $dest_xen->Xen::API::task::get_record($import_task);
625  
626 # Wait for the task statuses to be updated
627 0 my $wait=0;
628 0 my $maxwait=60;
629 0 ▶ while ((($export_task_record && ($export_task_record->{status}||'') eq 'pending')
Branch: 1 branch
BranchTrueFalseTotal
if ($export_task_record && ($export_task_record->{"status"} || "") eq "pending" || $import_task_record && ($import_task_record->{"status"} || "") eq "pending" and $wait < $maxwait) 0 0 0
Condition0%0/16
$export_task_record && ($export_task_record->{"status"} || "") eq "pending" || $import_task_record && ($import_task_record->{"status"} || "") eq "pending" and $wait < $maxwait
!ll&&!rl&&r
000
$export_task_record and ($export_task_record->{"status"} || "") eq "pending" or $import_task_record and ($import_task_record->{"status"} || "") eq "pending"
l!l&&r!l&&!r
000
$export_task_record and ($export_task_record->{"status"} || "") eq "pending"
!ll&&!rl&&r
000
$export_task_record->{'status'} || ''
l!l
00
$import_task_record and ($import_task_record->{"status"} || "") eq "pending"
!ll&&!rl&&r
000
$import_task_record->{'status'} || ''
l!l
00
MC/DC0%0/5
$export_task_record && ($export_task_record->{"status"} || "") eq "pending" || $import_task_record && ($import_task_record->{"status"} || "") eq "pending" and $wait < $maxwait
$export_task_record $export_task_record->{'status'} $import_task_record $import_task_record->{'status'} $wait < $maxwait
Truth table0%0/10
A and B or C and D and E
A= $export_task_recordB= $export_task_record->{'status'}C= $import_task_recordD= $import_task_record->{'status'}E= $wait < $maxwait
ABCDEresult
0X0XX0
0X10X0
100XX0
1010X0
11XX00
0X1100
101100
11XX11
0X1111
101111
630 || ($import_task_record && ($import_task_record->{status}||'') eq 'pending'))
631 && $wait < $maxwait)
632 {
633 0 $export_task_record = $self->Xen::API::task::get_record($export_task);
634 0 $import_task_record = $dest_xen->Xen::API::task::get_record($import_task);
635 0 sleep 1;
636 0 $wait++;
637 }
638  
639 # remove task statuses
640 0 $self->Xen::API::task::destroy($export_task);
641 0 $dest_xen->Xen::API::task::destroy($import_task);
642  
643 # error handling
644 0 my @errors;
645 0 ▶ push @errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
646 push @errors, "Import task returned status $import_task_record->{status}: "
647 0 ▶ .join(', ',@{$import_task_record->{error_info}||[]})
Condition0%0/2
$import_task_record->{'error_info'} || []
l!l
00
MC/DC0%0/1
$import_task_record->{'error_info'} || []
$import_task_record->{'error_info'}
Truth table0%0/2
A
A= $import_task_record->{'error_info'}
Aresult
11
00
648 0 ▶ if $import_task_record->{status} ne 'success';
Branch: 1 branch
BranchTrueFalseTotal
if $import_task_record->{"status"} ne "success" 0 0 0
649 push @errors, "Export task returned status $export_task_record->{status}: "
650 0 ▶ .join(', ',@{$export_task_record->{error_info}||[]})
Condition0%0/2
$export_task_record->{'error_info'} || []
l!l
00
MC/DC0%0/1
$export_task_record->{'error_info'} || []
$export_task_record->{'error_info'}
Truth table0%0/2
A
A= $export_task_record->{'error_info'}
Aresult
11
00
651 0 ▶ if $export_task_record->{status} ne 'success';
Branch: 1 branch
BranchTrueFalseTotal
if $export_task_record->{"status"} ne "success" 0 0 0
652 0 ▶ die join("\n",@errors) if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
653  
654 0 return '';
655 }
656  
657 =head2 set_template
658  
659 Set the is_a_template flag for a VM.
660  
661 =cut
662  
663 0 ▶sub set_template {
Subroutine set_template: not called CC=10 SCAR=47.0
Pod documented
664 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
665 0 ▶ my $vmname = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $vmname = shift() or return 0 0 0
666 0 my $set_template = shift;
667 0 ▶ $set_template = 1 if !defined($set_template);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set_template 0 0 0
668
669 # find the VM
670 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
671 my @vms = grep {
672 0 $vms{$_}{name_label} eq $vmname
673 0 ▶ || $vms{$_}{uuid} eq $vmname
Condition0%0/6
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
l!l&&r!l&&!r
000
$vms{$_}{"name_label"} eq $vmname or $vms{$_}{"uuid"} eq $vmname
l!l&&r!l&&!r
000
MC/DC0%0/3
$vms{$_}{'name_label'} eq $vmname || $vms{$_}{'uuid'} eq $vmname || $_ eq $vmname
$vms{$_}{"name_label"} eq $vmname $vms{$_}{"uuid"} eq $vmname $_ eq $vmname
Truth table0%0/4
A or B || C
A= $vms{$_}{"name_label"} eq $vmnameB= $vms{$_}{"uuid"} eq $vmnameC= $_ eq $vmname
ABCresult
1XX1
01X1
0011
0000
674 || $_ eq $vmname} keys %vms;
675 0 ▶ my $vm = $vms[0] or die "Could not find vm $vmname";
Branch: 1 branch
BranchTrueFalseTotal
my $vm = $vms[0] or die "Could not find vm $vmname" 0 0 0
676  
677 0 ▶ $self->Xen::API::VM::set_is_a_template(
Branch: 2 branches
BranchTrueFalseTotal
$set_template ? : 0 0 0
ref $set_template ? : 0 0 0
678 $vm,
679 $set_template?
680 ref($set_template)? $set_template : true
681 : false);
682 0 return '';
683 }
684  
685 =head2 list_vms
686  
687 List the VMs on this Xen server.
688  
689 =cut
690
691 0 ▶sub list_vms {
Subroutine list_vms: not called CC=5 SCAR=34.0
Pod documented
692 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
693 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
694 0 my @vms = grep {!$vms{$_}{is_a_template}} keys %vms;
695 return map {{
696 name_label=>$vms{$_}{name_label},
697 uuid=>$vms{$_}{uuid},
698 ref=>$_,
699 power_state=>$vms{$_}{power_state},
700 0 ▶ ip=>($vms{$_}{power_state}||'') eq 'Running' ? $self->_get_ip($_,1) : undef,
Branch: 1 branch
BranchTrueFalseTotal
($vms{$_}{'power_state'} || '') eq 'Running' ? : 0 0 0
Condition0%0/2
$vms{$_}{'power_state'} || ''
l!l
00
MC/DC0%0/1
$vms{$_}{'power_state'} || ''
$vms{$_}{'power_state'}
Truth table0%0/2
A
A= $vms{$_}{'power_state'}
Aresult
11
00
701 }}
702 0 sort {$vms{$a}{name_label} cmp $vms{$b}{name_label}} @vms;
703 }
704  
705 =head2 list_templates
706  
707 List the templates on this Xen server.
708  
709 =cut
710  
711 0 ▶sub list_templates {
Subroutine list_templates: not called CC=6 SCAR=37.4
Pod documented
712 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
713 0 my $vbds_only = shift;
714 0 ▶ my %vms = %{$self->Xen::API::VM::get_all_records||{}};
Condition0%0/2
$self->Xen::API::VM::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::VM::get_all_records || {}
$self->Xen::API::VM::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::VM::get_all_records
Aresult
11
00
715 0 ▶ my @templates = grep {$vms{$_}{is_a_template} && (!$vbds_only || @{$vms{$_}{VBDs}||[]})} keys %vms;
Condition0%0/8
$vms{$_}{'is_a_template'} && (!$vbds_only || @{$vms{$_}{'VBDs'} or [];})
!ll&&!rl&&r
000
not $vbds_only or @{$vms{$_}{"VBDs"} or [];}
l!l&&r!l&&!r
000
$vms{$_}{'VBDs'} || []
l!l
00
MC/DC0%0/3
$vms{$_}{'is_a_template'} && (!$vbds_only || @{$vms{$_}{'VBDs'} or [];})
$vms{$_}{'is_a_template'} not $vbds_only @{$vms{$_}{"VBDs"} or [];}
MC/DC0%0/1
$vms{$_}{'VBDs'} || []
$vms{$_}{'VBDs'}
Truth table0%0/4
A && not(B) or C
A= $vms{$_}{'is_a_template'}B= not $vbds_onlyC= @{$vms{$_}{"VBDs"} or [];}
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/2
A
A= $vms{$_}{'VBDs'}
Aresult
11
00
716 return map {{
717 name_label=>$vms{$_}{name_label},
718 uuid=>$vms{$_}{uuid},
719 0 ref=>$_,
720 }}
721 0 sort {$vms{$a}{name_label} cmp $vms{$b}{name_label}} @templates;
722 }
723  
724 =head2 list_hosts
725  
726 List the physical hosts and related information.
727  
728 =cut
729  
730 0 ▶sub list_hosts {
Subroutine list_hosts: not called CC=5 SCAR=34.0
Pod documented
731 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
732 0 ▶ my %hosts = %{$self->Xen::API::host::get_all_records||{}};
Condition0%0/2
$self->Xen::API::host::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::host::get_all_records || {}
$self->Xen::API::host::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::host::get_all_records
Aresult
11
00
733 0 ▶ my %cpus = %{$self->Xen::API::host_cpu::get_all_records||{}};
Condition0%0/2
$self->Xen::API::host_cpu::get_all_records || {}
l!l
00
MC/DC0%0/1
$self->Xen::API::host_cpu::get_all_records || {}
$self->Xen::API::host_cpu::get_all_records
Truth table0%0/2
A
A= $self->Xen::API::host_cpu::get_all_records
Aresult
11
00
734 0 my %metrics = map {$_=>$self->Xen::API::host_metrics::get_record($hosts{$_}{metrics})} keys %hosts;
735  
736 return map {{
737 name_label=>$hosts{$_}{name_label},
738 uuid=>$hosts{$_}{uuid},
739 ref=>$_,
740 0 ▶ cpus=>scalar(@{$hosts{$_}{host_CPUs}||[]}),
Condition0%0/2
$hosts{$_}{'host_CPUs'} || []
l!l
00
MC/DC0%0/1
$hosts{$_}{'host_CPUs'} || []
$hosts{$_}{'host_CPUs'}
Truth table0%0/2
A
A= $hosts{$_}{'host_CPUs'}
Aresult
11
00
741 0 %{$metrics{$_}},
742 memory_free=>format_bytes($metrics{$_}{memory_free}, mode=>'iec'),
743 0 memory_total=>format_bytes($metrics{$_}{memory_total}, mode=>'iec'),
744 0 }} sort {$hosts{$a}{name_label} cmp $hosts{$b}{name_labe}} keys %hosts;
745 }
746  
747 0 ▶sub value {
Subroutine value: not called CC=6 SCAR=37.4
Pod undocumented
748 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
749 0 my ($val) = @_;
750 return $val && ($val->{Status}||'') eq "Success"
751 ? $val->{Value}
752 : die "Received status \"$val->{Status}\" from xen server at ".$self->{uri}.": "
753 0 ▶ .join(', ',@{$val->{ErrorDescription}||[]});
Branch: 1 branch
BranchTrueFalseTotal
$val && ($val->{'Status'} || '') eq 'Success' ? : 0 0 0
Condition0%0/7
$val and ($val->{"Status"} || "") eq "Success"
!ll&&!rl&&r
000
$val->{'Status'} || ''
l!l
00
$val->{'ErrorDescription'} || []
l!l
00
MC/DC0%0/2
$val and ($val->{"Status"} || "") eq "Success"
$val $val->{'Status'}
MC/DC0%0/1
$val->{'ErrorDescription'} || []
$val->{'ErrorDescription'}
Truth table0%0/3
A and B
A= $valB= $val->{'Status'}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $val->{'ErrorDescription'}
Aresult
11
00
754 }
755  
756 0 ▶sub request {
Subroutine request: not called CC=4 SCAR=30.0
Pod undocumented
757 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
758 0 ▶ my $request = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $request = shift() or return 0 0 0
759 0 my $response = $self->{xen}->simple_request($request, $self->{session}, @_);
760 0 ▶ die $RPC::XML::ERROR if $RPC::XML::ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::XML::ERROR 0 0 0
761 0 return $self->value($response);
762 }
763  
764 1 1;
765  
766 =head1 AUTHOR
767  
768 Ben Booth, benwbooth@gmail.com
769  
770 =head1 COPYRIGHT AND LICENSE
771  
772 Copyright (C) 2013 by Ben Booth
773  
774 This library is free software; you can redistribute it and/or modify
775 it under the same terms as Perl itself, either Perl version 5.10.1 or,
776 at your option, any later version of Perl 5 you may have available.
777  
778  
779 =cut
780  
↑ Summary