blib/lib/VM/Libvirt/CloneHelper.pm

filter: Statementstmt 8.7% 16 / 183 Branchbran 0.0% 0 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.2% 5 / 16 Podpod 100% 11 / 11 totaltotal 11.1% 32 / 288 SCAR 76.6
CC
50
Cov
6%
new
132.0
net_xml
90.0
delete_clones
56.0
CRAP 2117.8
↑ Summary
1 package VM::Libvirt::CloneHelper;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Slurp qw(write_file read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 VM::Libvirt::CloneHelper - Create a bunch of cloned VMs in via libvirt.
12  
13 =head1 VERSION
14  
15 Version 0.1.0
16  
17 =cut
18  
19 1 our $VERSION = '0.1.0';
20  
21 =head1 SYNOPSIS
22  
23 # initialize it
24 my $clone_helper=VM::Libvirt::CloneHelper->new({
25 blank_domains=>'/usr/local/etc/clonehelper/blank_domains',
26 net_head=>'/usr/local/etc/clonehelper/net_head',
27 net_tail=>'/usr/local/etc/clonehelper/net_tail',
28 windows_blank=>0,
29 mac_base=>'00:08:74:2d:dd:',
30 ipv4_base=>'192.168.1.',
31 start=>100,
32 to_clone=>'baseVM',
33 clone_name_base=>'foo',
34 count=>10,
35 verbose=>1,
36 snapshot_name=>'clean',
37 net=>'default',
38 wait=>360,
39 });
40  
41 $clone_helper->delete_vms;
42 $clone_helper->clone_vms;
43 $clone_helper->start_vms;
44 sleep 500;
45 $clone_helper->snapshot_vms;
46 $clone_helper->shutdown_vms;
47  
48 It should be noted that this is effectively limited to 253 VMs.
49  
50 This script lib is primarily meant for creating a bunch of cloned VMs on a
51 box for testing purposes, so this is not really a major issue given the
52 design scope.
53  
54 VMs should be set to us DHCP so they will get their expected IP when they boot.
55  
56 =head1 METHODS
57  
58 =head2 new
59  
60 Initialize the module.
61  
62 net=>'default'
63 Name of the libvirt network in question.
64  
65 blank_domains=>'/usr/local/etc/clonehelper/blank_domains',
66 List of domains to blank via setting 'dnsmasq:option value='address=/foo.bar/'.
67 If not this file does not exist, it will be skipped.
68  
69 net_head=>'/usr/local/etc/clonehelper/net_head',
70 The top part of the net XML config that that dnsmasq options will be
71 sandwhiched between.
72  
73 net_tail=>'/usr/local/etc/clonehelper/net_tail',
74 The bottom part of the net XML config that that dnsmasq options will
75 be sandwhiched between.
76  
77 windows_blank=>1,
78 Blank commonly used MS domains. This is handy for reducing network noise
79 when testing as well as making sure they any VMs don't do something like
80 run updates when one does not want it to.
81  
82 mac_base=>'00:08:74:2d:dd:',
83 Base to use for the MAC.
84  
85 ipv4_base=>'192.168.1.',
86 Base to use for the IPs for adding static assignments.
87  
88 start=>'100',
89 Where to start in set.
90  
91 to_clone=>'baseVM',
92 The name of the VM to clone.
93  
94 clone_name_base=>'cloneVM',
95 Base name to use for creating the clones. 'foo' will become 'foo$current', so
96 for a start of 100, the first one would be 'foo100' and with a count of 10 the
97 last will be 'foo109'.
98  
99 count=>10,
100 How many clones to create.
101  
102 snapshot_name=>'clean',
103 The name to use for the snapshot.
104  
105 wait=>360,
106 How long to wait if auto-doing all.
107  
108 =cut
109  
110 sub new {
111 0 ▶ my %args;
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
112 0 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
113 0 %args = %{ $_[1] };
114 }
115  
116 0 my $self = {
117 blank_domains => '/usr/local/etc/clonehelper/blank_domains',
118 net_head => '/usr/local/etc/clonehelper/net_head',
119 net_tail => '/usr/local/etc/clonehelper/net_tail',
120 windows_blank => 1,
121 mac_base => '00:08:74:2d:dd:',
122 ipv4_base => '192.168.1.',
123 start => 100,
124 to_clone => 'baseVM',
125 delete_old => 1,
126 clone_name_base => 'foo',
127 uuid_auto => 1,
128 count => 10,
129 verbose => 1,
130 snapshot_name => 'clean',
131 net => 'default',
132 wait => 360,
133 };
134 0 bless $self;
135  
136 # do very basic value sanity checks and reel values in
137 0 my @keys = keys(%args);
138 0 foreach my $key (@keys) {
139 0 ▶ if ( $key eq 'mac_base' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'mac_base') { } 0 0 0
elsif ($key eq 'ipv4_base') { } 0 0 0
elsif ($key eq 'to_clone') { } 0 0 0
elsif ($key eq 'clone_name_base') { } 0 0 0
140  
141 # make sure we got a sane base MAC
142 0 ▶ if ( $args{mac_base}
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"mac_base"} =~ /^[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:$/) 0 0 0
143 !~ /^[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:[0-9aAbBcCdDeEfF][0-9aAbBcCdDeEfF]\:$/
144 )
145 {
146 0 die( '"' . $args{mac_base} . '" does not appear to be a valid base for a MAC address' );
147 }
148 }
149 elsif ( $key eq 'ipv4_base' ) {
150  
151 # make sure we have a likely sane base for the IPv4 address
152 0 ▶ if ( $args{ipv4_base} !~ /^[0-9]+\.[0-9]+\.[0-9]+\.$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"ipv4_base"} =~ /^[0-9]+\.[0-9]+\.[0-9]+\.$/) 0 0 0
153 0 die( '"' . $args{ipv4_base} . '" does not appear to be a valid base for a IPv4 address' );
154 }
155 }
156 elsif ( $key eq 'to_clone' ) {
157  
158 # make sure we have a likely sane base VM name
159 0 ▶ if ( $args{to_clone} !~ /^[A-Za-z0-9\-\.]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"to_clone"} =~ /^[A-Za-z0-9\-\.]+$/) 0 0 0
160 0 die( '"' . $args{to_clone} . '" does not appear to be a valid VM name' );
161 }
162 }
163 elsif ( $key eq 'clone_name_base' ) {
164  
165 # make sure we have a likely sane base name to use for creating clones
166 0 ▶ if ( $args{clone_name_base} !~ /^[A-Za-z0-9\-\.]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"clone_name_base"} =~ /^[A-Za-z0-9\-\.]+$/) 0 0 0
167 0 die( '"' . $args{clone_name_base} . '" does not appear to be a valid VM name' );
168 }
169 }
170  
171 # likely good, adding
172 0 $self->{$key} = $args{$key};
173 }
174  
175 0 $self->{end} = $self->{start} + $self->{count} - 1;
176  
177 0 $self->{VMs} = $self->vm_list;
178  
179 0 return $self;
180 }
181  
182 =head2 clone
183  
184 Create the clones.
185  
186 One optional argument is taken and that is the VM to operate on.
187 Otherwise all is ran for them all.
188  
189 $clone_helper->clone;
190  
191 =cut
192  
193 sub clone {
194 0 ▶ my $self = $_[0];
Subroutine clone: not called CC=5 SCAR=34.0
Pod documented
195 0 my $name = $_[1];
196  
197 0 my $VMs = $self->vm_list;
198  
199 0 my @VM_names;
200 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
201 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
202 0 die( '"' . $VMs . '" is not a known VM' );
203 }
204 0 push( @VM_names, $name );
205 }
206 else {
207 0 @VM_names = sort( keys( %{$VMs} ) );
208 }
209 0 foreach my $name (@VM_names) {
210 print "Cloning '"
211 . $self->{to_clone}
212 . "' to '"
213 . $name . "'("
214 . $VMs->{$name}{mac} . ", "
215 . $VMs->{$name}{ip}
216 0 . ")...\n";
217  
218 0 my @args = ( 'virt-clone', '-m', $VMs->{$name}{mac}, '-o', $self->{to_clone}, '--auto-clone', '-n', $name );
219 0 ▶ system(@args) == 0 or die("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system '@{args}' failed... $?" 0 0 0
220 }
221 }
222  
223 =head2 delete_clones
224  
225 Delete all the clones
226  
227 One optional argument is taken and that is the VM to operate on.
228 Otherwise all is ran for them all.
229  
230 $clone_helper->delete_clones;
231  
232 =cut
233  
234 sub delete_clones {
235 0 ▶ my $self = $_[0];
Subroutine delete_clones: not called CC=7 SCAR=40.3
Pod documented
236 0 my $name = $_[1];
237  
238 # virsh undefine --snapshots-metadata
239 # the VM under /var/lib/libvirt/images needs to be removed manually given
240 # the shit show that is libvirt does not have a means of sanely removing
241 # VMs and relevant storage... for example it will include ISOs in relevant
242 # VMs to be removed if you let it... and it is likely to fail to remove the
243 # base disk image for a VM, even if you pass it any/every combination of
244 # possible flags...
245  
246 0 my $VMs = $self->vm_list;
247  
248 0 my @VM_names;
249 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
250 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
251 0 die( '"' . $VMs . '" is not a known VM' );
252 }
253 0 push( @VM_names, $name );
254 }
255 else {
256 0 @VM_names = sort( keys( %{$VMs} ) );
257 }
258 0 foreach my $name (@VM_names) {
259 0 print "Undefining " . $name . "\n";
260 0 my @args = ( 'virsh', 'undefine', '--snapshots-metadata', $name );
261 0 ▶ system(@args) == 0 or warn("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or warn "system '@{args}' failed... $?" 0 0 0
262  
263 0 my $image = '/var/lib/libvirt/images/' . $name . '.qcow2';
264  
265 0 ▶ if ( -f $image ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $image) 0 0 0
266 0 print "Unlinking " . $image . "\n";
267 0 ▶ unlink($image) or die( 'unlinking "' . $image . '" failed... ' . $! );
Branch: 1 branch
BranchTrueFalseTotal
unlink $image or die 'unlinking "' . $image . '" failed... ' . $! 0 0 0
268 }
269 }
270 }
271  
272 =head2 net_xml
273  
274 Returns a string with the full net config XML.
275  
276 my $net_config_xml=$clone_helper->net_xml;
277 print $net_config_xml;
278  
279 =cut
280  
281 sub net_xml {
282 0 ▶ my $self = $_[0];
Subroutine net_xml: not called CC=9 SCAR=45.0
Pod documented
283  
284 0 my $VMs = $self->vm_list;
285  
286 0 ▶ my $xml = read_file( $self->{net_head} ) or die( 'Failed to read "' . $self->{net_head} . '"' );
Branch: 1 branch
BranchTrueFalseTotal
my $xml = read_file($self->{"net_head"}) or die 'Failed to read "' . $self->{"net_head"} . '"' 0 0 0
287 0 ▶ my $xml_tail = read_file( $self->{net_tail} ) or die( 'Failed to read "' . $self->{net_tail} . '"' );
Branch: 1 branch
BranchTrueFalseTotal
my $xml_tail = read_file($self->{"net_tail"}) or die 'Failed to read "' . $self->{"net_tail"} . '"' 0 0 0
288  
289 0 ▶ if ( $self->{windows_blank} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"windows_blank"}) 0 0 0
290 0 $xml = $xml . ' <dnsmasq:option value=\'address=/microsoft.com/\'/>
291 <dnsmasq:option value=\'address=/windowsupdate.com/\'/>
292 <dnsmasq:option value=\'address=/windows.com/\'/>
293 <dnsmasq:option value=\'address=/microsoft.com.nsatc.net/\'/>
294 <dnsmasq:option value=\'address=/bing.net/\'/>
295 <dnsmasq:option value=\'address=/live.com/\'/>
296 <dnsmasq:option value=\'address=/cloudapp.net/\'/>
297 <dnsmasq:option value=\'address=/cs1.wpc.v0cdn.net/\'/>
298 <dnsmasq:option value=\'address=/a-msedge.net/\'/>
299 <dnsmasq:option value=\'address=/-msedge.net/\'/>
300 <dnsmasq:option value=\'address=/msedge.net/\'/>
301 <dnsmasq:option value=\'address=/microsoft.com.akadns.net/\'/>
302 <dnsmasq:option value=\'address=/footprintpredict.com/\'/>
303 <dnsmasq:option value=\'address=/microsoft-hohm.com/\'/>
304 <dnsmasq:option value=\'address=/msn.com/\'/>
305 <dnsmasq:option value=\'address=/social.ms.akadns.net/\'/>
306 <dnsmasq:option value=\'address=/msedge.net/\'/>
307 <dnsmasq:option value=\'address=/dc-msedge.net/\'/>
308 <dnsmasq:option value=\'address=/bing.com/\'/>
309 <dnsmasq:option value=\'address=/edgekey.net/\'/>
310 <dnsmasq:option value=\'address=/azureedge.net/\'/>
311 <dnsmasq:option value=\'address=/amsn.net/\'/>
312 <dnsmasq:option value=\'address=/moiawsorigin.clo.footprintdns.com/\'/>
313 <dnsmasq:option value=\'address=/office365.com/\'/>
314 <dnsmasq:option value=\'address=/skype.com/\'/>
315 <dnsmasq:option value=\'address=/trafficmanager.net/\'/>
316 ';
317 }
318  
319 0 ▶ if ( -f $self->{blank_domains} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"blank_domains"}) 0 0 0
320 0 ▶ my $blank_raw = read_file( $self->{blank_domains} ) or die( 'Failed to read "' . $self->{blank_domains} . '"' );
Branch: 1 branch
BranchTrueFalseTotal
my $blank_raw = read_file($self->{"blank_domains"}) or die 'Failed to read "' . $self->{"blank_domains"} . '"' 0 0 0
321  
322 # remove any blank lines or anyhting commented out
323 0 my @blank_split = grep( !/^[\ \t]*]$/, grep( !/^[\ \t]*#/, split( /\n/, $blank_raw ) ) );
324 0 foreach my $line (@blank_split) {
325 0 chomp($line);
326 0 $line =~ s/^[\ \t]*//;
327 0 $line =~ s/[\ \t]*$//;
328 0 foreach my $domain ( split( /[\ \t]+/, $line ) ) {
329 0 $xml = $xml . " <dnsmasq:option value='address=/" . $domain . "/'/>\n";
330 }
331 }
332 }
333  
334 0 my @VM_names = sort( keys( %{$VMs} ) );
335 0 foreach my $name (@VM_names) {
336 $xml
337 = $xml
338 . ' <dnsmasq:option value=\'dhcp-host='
339 . $VMs->{$name}{mac} . ','
340 0 . $VMs->{$name}{ip} . '\'/>' . "\n";
341 }
342  
343 0 return $xml . $xml_tail;
344 }
345  
346 =head2 net_redefine
347  
348 Redefines the network in question.
349  
350 =cut
351  
352 sub net_redefine {
353 0 ▶ my $self = $_[0];
Subroutine net_redefine: not called CC=5 SCAR=34.0
Pod documented
354  
355 0 my $xml = $self->net_xml;
356  
357 0 print "Undefining the the network('" . $self->{net} . "') for readding it...\n";
358 0 my @args = ( 'virsh', 'net-undefine', $self->{net} );
359 0 ▶ system(@args) == 0 or die("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system '@{args}' failed... $?" 0 0 0
360  
361 0 my $fh = File::Temp->new;
362 0 my $tmp_file = $fh->filename;
363  
364 0 ▶ write_file( $tmp_file, $xml ) or die( 'Failed to write tmp net config to "' . $tmp_file . '"... ' . $@ );
Branch: 1 branch
BranchTrueFalseTotal
write_file($tmp_file, $xml) or die 'Failed to write tmp net config to "' . $tmp_file . '"... ' . $@ 0 0 0
365  
366 0 print "Defining the the network('" . $self->{net} . "') for readding it...\n";
367 0 @args = ( 'virsh', 'net-define', '--file', $tmp_file );
368 0 ▶ system(@args) == 0 or die("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system '@{args}' failed... $?" 0 0 0
369  
370 0 ▶ unlink($tmp_file) or die( 'Failed to unlink net config "' . $tmp_file . '"... ' . $@ );
Branch: 1 branch
BranchTrueFalseTotal
unlink $tmp_file or die 'Failed to unlink net config "' . $tmp_file . '"... ' . $@ 0 0 0
371  
372 0 return;
373 }
374  
375 =head2 recreate
376  
377 Recreate the specified VM.
378  
379 One optional argument is taken and that is the VM to operate on.
380 Otherwise all is ran for them all.
381  
382 If you wish to recreate all, you should likely use recreate_all, to avoid
383 any issues caused by starting them all at the same time.
384  
385 $clone_helper->recreate('foo100');
386  
387 =cut
388  
389 sub recreate {
390 0 ▶ my $self = $_[0];
Subroutine recreate: not called CC=3 SCAR=24.8
Pod documented
391 0 my $name = $_[0];
392  
393 0 ▶ if ( !defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
394 0 die('No VM specified to recreate');
395 }
396  
397 0 my $VMs = $self->vm_list;
398  
399 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
400 0 die( '"' . $VMs . '" is not a known VM' );
401 }
402  
403 0 $self->delete_clones($name);
404 0 $self->clone($name);
405 0 $self->start_clones($name);
406 0 sleep($self->{wait});
407 0 $self->snapshot_clones($name);
408 0 $self->stop_clones($name);
409  
410 0 return;
411 }
412  
413 =head2 recreate_all
414  
415 Recreate all VMs.
416  
417 Does one at a time.
418  
419 $clone_helper->recreate_all;
420  
421 =cut
422  
423 sub recreate_all {
424 0 ▶ my $self = $_[0];
Subroutine recreate_all: not called CC=2 SCAR=17.9
Pod documented
425  
426 0 my $VMs = $self->vm_list;
427  
428 0 my @VM_names = sort( keys( %{$VMs} ) );
429 0 foreach my $name (@VM_names) {
430 0 $self->delete_clones($name);
431 0 $self->clone($name);
432 0 $self->start_clones($name);
433 0 sleep($self->{wait});
434 0 $self->snapshot_clones($name);
435 0 $self->stop_clones($name);
436 }
437  
438 0 return;
439 }
440  
441 =head2 snapshot_clones
442  
443 Snapshot all the clones
444  
445 One optional argument is taken and that is the VM to operate on.
446 Otherwise all is ran for them all.
447  
448 $clone_helper->snapshot_clones;
449  
450 =cut
451  
452 sub snapshot_clones {
453 0 ▶ my $self = $_[0];
Subroutine snapshot_clones: not called CC=5 SCAR=34.0
Pod documented
454 0 my $name = $_[1];
455  
456 0 my $VMs = $self->vm_list;
457  
458 0 my @VM_names;
459 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
460 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
461 0 die( '"' . $VMs . '" is not a known VM' );
462 }
463 0 push( @VM_names, $name );
464 }
465 else {
466 0 @VM_names = sort( keys( %{$VMs} ) );
467 }
468 0 foreach my $name (@VM_names) {
469 0 print "Snapshotting " . $name . "...\n";
470 0 my @args = ( 'virsh', 'snapshot-create-as', '--name', $self->{snapshot_name}, $name );
471 0 ▶ system(@args) == 0 or die("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system '@{args}' failed... $?" 0 0 0
472 }
473 }
474  
475 =head2 start_clones
476  
477 Start all the clones
478  
479 One optional argument is taken and that is the VM to operate on.
480 Otherwise all is ran for them all.
481  
482 $clone_helper->start_clones;
483  
484 =cut
485  
486 sub start_clones {
487 0 ▶ my $self = $_[0];
Subroutine start_clones: not called CC=5 SCAR=34.0
Pod documented
488 0 my $name = $_[1];
489  
490 0 my $VMs = $self->vm_list;
491  
492 0 my @VM_names;
493 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
494 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
495 0 die( '"' . $VMs . '" is not a known VM' );
496 }
497 0 push( @VM_names, $name );
498 }
499 else {
500 0 @VM_names = sort( keys( %{$VMs} ) );
501 }
502 0 foreach my $name (@VM_names) {
503 0 print "Starting " . $name . "...\n";
504 0 my @args = ( 'virsh', 'start', $name );
505 0 ▶ system(@args) == 0 or die("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system '@{args}' failed... $?" 0 0 0
506 }
507 }
508  
509 =head2 stop_clones
510  
511 Stop all the clones. This does not stop them gracefully as we don't
512 need to as they are being started via snapshot.
513  
514 One optional argument is taken and that is the VM to operate on.
515 Otherwise all is ran for them all.
516  
517 $clone_helper->stop_clones;
518  
519 =cut
520  
521 sub stop_clones {
522 0 ▶ my $self = $_[0];
Subroutine stop_clones: not called CC=5 SCAR=34.0
Pod documented
523 0 my $name = $_[1];
524  
525 0 my $VMs = $self->vm_list;
526  
527 0 my @VM_names;
528 0 ▶ if ( defined($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
529 0 ▶ if ( !defined( $VMs->{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $VMs->{$name}) 0 0 0
530 0 die( '"' . $VMs . '" is not a known VM' );
531 }
532 0 push( @VM_names, $name );
533 }
534 else {
535 0 @VM_names = sort( keys( %{$VMs} ) );
536 }
537 0 foreach my $name (@VM_names) {
538 0 print "Stopping " . $name . "...\n";
539 0 my @args = ( 'virsh', 'destroy', $name );
540 0 ▶ system(@args) == 0 or warn("system '@args' failed... $?");
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or warn "system '@{args}' failed... $?" 0 0 0
541 }
542 }
543  
544 =head2 vm_list
545  
546 Generate a list of VMs.
547  
548 =cut
549  
550 sub vm_list {
551 0 ▶ my $self = $_[0];
Subroutine vm_list: not called CC=3 SCAR=24.8
Pod documented
552  
553 # only need to create this all once
554 0 ▶ if ( defined( $self->{VMs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"VMs"}) 0 0 0
555 0 return $self->{VMs};
556 }
557  
558 0 my $VMs = {};
559  
560 0 my $current = $self->{start};
561 0 my $till = $current + $self->{count} - 1;
562 0 ▶ while ( $current <= $till ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current <= $till) 0 0 0
563 0 my $name = $self->{clone_name_base} . $current;
564 0 my $hex = sprintf( '%#x', $current );
565 0 $hex =~ s/^0[Xx]//;
566  
567 $VMs->{$name} = {
568 ip => $self->{ipv4_base} . $current,
569 0 mac => $self->{mac_base} . $hex,
570 };
571  
572 0 $current++;
573 }
574  
575 0 return $VMs;
576 }
577  
578 =head1 BLANKED MS DOMAINS
579  
580 microsoft.com
581 windowsupdate.com
582 windows.com
583 microsoft.com.nsatc.net
584 bing.net
585 live.com
586 cloudapp.net
587 cs1.wpc.v0cdn.net
588 -msedge.net
589 msedge.net
590 microsoft.com.akadns.net
591 footprintpredict.com
592 microsoft-hohm.com
593 msn.com
594 social.ms.akadns.net
595 msedge.net
596 dc-msedge.net
597 bing.com
598 edgekey.net
599 azureedge.net
600 amsn.net
601 moiawsorigin.clo.footprintdns.com
602 office365.com
603 skype.com
604 trafficmanager.net
605  
606 =head1 AUTHOR
607  
608 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
609  
610 =head1 BUGS
611  
612 Please report any bugs or feature requests to C<bug-vm-libvirt-clonehelper at rt.cpan.org>, or through
613 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=VM-Libvirt-CloneHelper>. I will be notified, and then you'll
614 automatically be notified of progress on your bug as I make changes.
615  
616  
617  
618  
619 =head1 SUPPORT
620  
621 You can find documentation for this module with the perldoc command.
622  
623 perldoc VM::Libvirt::CloneHelper
624  
625  
626 You can also look for information at:
627  
628 =over 4
629  
630 =item * RT: CPAN's request tracker (report bugs here)
631  
632 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=VM-Libvirt-CloneHelper>
633  
634 =item * CPAN Ratings
635  
636 L<https://cpanratings.perl.org/d/VM-Libvirt-CloneHelper>
637  
638 =item * Search CPAN
639  
640 L<https://metacpan.org/release/VM-Libvirt-CloneHelper>
641  
642 =back
643  
644  
645 =head1 ACKNOWLEDGEMENTS
646  
647  
648 =head1 LICENSE AND COPYRIGHT
649  
650 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
651  
652 This is free software, licensed under:
653  
654 The Artistic License 2.0 (GPL Compatible)
655  
656  
657 =cut
658  
659 1 1; # End of VM::Libvirt::CloneHelper
↑ Summary