lib/Rex/Box/KVM.pm

filter: Statementstmt 48.0% 37 / 77 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 12 / 18 Podpod 100% 6 / 6 totaltotal 46.2% 55 / 119 CC 8 SCAR 25.1
CC
8
Cov
41%
import_vm
42.0
new
12.0
memory
2.0
CRAP 12.3
« blib/lib/Rex/Args.pm ↑ Summary lib/Rex/Task.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Box::KVM - Rex/Boxes KVM Module
8  
9 =head1 DESCRIPTION
10  
11 This is a Rex/Boxes module to use KVM VMs. You need to have libvirt installed.
12  
13 =head1 EXAMPLES
14  
15 To use this module inside your Rexfile you can use the following commands.
16  
17 use Rex::Commands::Box;
18 set box => "KVM";
19  
20 task "prepare_box", sub {
21 box {
22 my ($box) = @_;
23  
24 $box->name("mybox");
25 $box->url("http://box.rexify.org/box/ubuntu-server-12.10-amd64.kvm.qcow2");
26  
27 $box->network(1 => {
28 name => "default",
29 });
30  
31 $box->auth(
32 user => "root",
33 password => "box",
34 );
35  
36 $box->setup("setup_task");
37 };
38 };
39  
40 If you want to use a YAML file you can use the following template.
41  
42 type: KVM
43 vms:
44 vmone:
45 url: http://box.rexify.org/box/ubuntu-server-12.10-amd64.kvm.qcow2
46 setup: setup_task
47  
48 And then you can use it the following way in your Rexfile.
49  
50 use Rex::Commands::Box init_file => "file.yml";
51  
52 task "prepare_vms", sub {
53 boxes "init";
54 };
55  
56 =head1 METHODS
57  
58 See also the Methods of Rex::Box::Base. This module inherits all methods of it.
59  
60 =cut
61  
62 package Rex::Box::KVM;
63  
64 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Rex::Box::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Rex::Commands -no => [qw/auth/];
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Rex::Commands::Virtualization;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Rex::Commands::SimpleCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 our $VERSION = '1.16.1'; # VERSION
74  
75 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 LWP::UserAgent->use;
77 }
78  
79 1 ▶use Time::HiRes qw(tv_interval gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 1 ▶use base qw(Rex::Box::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 1 set virtualization => "LibVirt";
85  
86 1 $|++;
87  
88 ################################################################################
89 # BEGIN of class methods
90 ################################################################################
91  
92 =head2 new(name => $vmname)
93  
94 Constructor if used in OO mode.
95  
96 my $box = Rex::Box::KVM->new(name => "vmname");
97  
98 =cut
99  
100 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
101 0 my $class = shift;
102 0 ▶ my $proto = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
103 0 my $self = $proto->SUPER::new(@_);
104  
105 0 ▶ bless( $self, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
106  
107 0 return $self;
108 }
109  
110 =head2 memory($memory_size)
111  
112 Sets the memory of a VM in megabyte.
113  
114 =cut
115  
116 0 ▶sub memory {
Subroutine memory: not called CC=1 SCAR=6.9
Pod documented
117 0 my ( $self, $mem ) = @_;
118 0 $self->{memory} = $mem * 1024; # libvirt wants kilobytes
119 }
120  
121 0 ▶sub import_vm {
Subroutine import_vm: not called CC=6 SCAR=37.4
Pod documented
122 0 my ($self) = @_;
123  
124 # check if machine already exists
125 0 my $vms = vm list => "all";
126  
127 0 my $vm_exists = 0;
128 0 for my $vm ( @{$vms} ) {
129 0 ▶ if ( $vm->{name} eq $self->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vm->{"name"} eq $self->{"name"}) 0 0 0
130 0 Rex::Logger::debug("VM already exists. Don't import anything.");
131 0 $vm_exists = 1;
132 }
133 }
134  
135 0 ▶ if ( !$vm_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vm_exists) 0 0 0
136  
137 # if not, create it
138 0 $self->_download;
139  
140 0 my $filename = basename( $self->{url} );
141  
142 0 Rex::Logger::info("Importing VM ./tmp/$filename");
143  
144 my @options = (
145 import => $self->{name},
146 file => "./tmp/$filename",
147 0 %{$self},
148 );
149  
150 0 ▶ if (Rex::Config::get_use_rex_kvm_agent) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::Config::get_use_rex_kvm_agent()) 0 0 0
151 0 my $tcp_port = int( rand(40000) ) + 10000;
152  
153 0 push @options, 'serial_devices',
154 [
155 {
156 type => 'tcp',
157 host => '127.0.0.1',
158 port => $tcp_port,
159 },
160 ];
161  
162 0 Rex::Logger::info(
163 "Binding a serial device to TCP port $tcp_port for rex-kvm-agent");
164 }
165  
166 0 vm @options;
167  
168 #unlink "./tmp/$filename";
169 }
170  
171 0 my $vminfo = vm info => $self->{name};
172  
173 0 ▶ if ( $vminfo->{State} eq "shut off" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vminfo->{"State"} eq "shut off") 0 0 0
174 0 $self->start;
175 }
176  
177 0 $self->{info} = vm guestinfo => $self->{name};
178 }
179  
180 0 ▶sub list_boxes {
Subroutine list_boxes: not called CC=1 SCAR=6.9
Pod documented
181 0 my ($self) = @_;
182  
183 0 my $vms = vm list => "all";
184  
185 0 return @{$vms};
186 }
187  
188 =head2 info
189  
190 Returns a hashRef of vm information.
191  
192 =cut
193  
194 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
195 0 my ($self) = @_;
196 0 $self->ip;
197 0 return $self->{info};
198 }
199  
200 =head2 ip
201  
202 This method return the ip of a vm on which the ssh daemon is listening.
203  
204 =cut
205  
206 0 ▶sub ip {
Subroutine ip: not called CC=1 SCAR=6.9
Pod documented
207 0 my ($self) = @_;
208 0 $self->{info} = vm guestinfo => $self->{name};
209 0 return $self->{info}->{network}->[0]->{ip};
210 }
211  
212 1 1;
« blib/lib/Rex/Args.pm ↑ Summary lib/Rex/Task.pm »