lib/Rex/Box/Amazon.pm

filter: Statementstmt 35.1% 33 / 94 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 44.0% 11 / 25 Podpod 100% 14 / 14 totaltotal 33.9% 58 / 171 CC 13 SCAR 29.3
CC
13
Cov
27%
import_vm
72.0
new
12.0
list_boxes
12.0
CRAP 18.7
« lib/Rex/File/Parser/Ini.pm ↑ Summary lib/Rex/Helper/INI.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Box::Amazon - Rex/Boxes Amazon Module
8  
9 =head1 DESCRIPTION
10  
11 This is a Rex/Boxes module to use Amazon EC2.
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 => "Amazon", {
19 access_key => "your-access-key",
20 private_access_key => "your-private-access-key",
21 region => "ec2.eu-west-1.amazonaws.com",
22 zone => "eu-west-1a",
23 authkey => "default",
24 };
25  
26 task "prepare_box", sub {
27 box {
28 my ($box) = @_;
29  
30 $box->name("mybox");
31 $box->ami("ami-c1aaabb5");
32 $box->type("m1.large");
33  
34 $box->security_group("default");
35  
36 $box->auth(
37 user => "root",
38 password => "box",
39 );
40  
41 $box->setup("setup_task");
42 };
43 };
44  
45 If you want to use a YAML file you can use the following template.
46  
47 type: Amazon
48 amazon:
49 access_key: your-access-key
50 private_access_key: your-private-access-key
51 region: ec2.eu-west-1.amazonaws.com
52 zone: eu-west-1a
53 auth_key: default
54 vms:
55 vmone:
56 ami: ami-c1aaabb5
57 type: m1.large
58 security_group: default
59 setup: setup_task
60  
61 And then you can use it the following way in your Rexfile.
62  
63 use Rex::Commands::Box init_file => "file.yml";
64  
65 task "prepare_vms", sub {
66 boxes "init";
67 };
68  
69  
70 =head1 METHODS
71  
72 See also the Methods of Rex::Box::Base. This module inherits all methods of it.
73  
74 =cut
75  
76 package Rex::Box::Amazon;
77  
78 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use Rex::Box::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use Rex::Commands -no => [qw/auth/];
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Rex::Commands::Cloud;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1 our $VERSION = '1.16.1'; # VERSION
87  
88 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 LWP::UserAgent->use;
90 }
91  
92 1 ▶use Time::HiRes qw(tv_interval gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 1 ▶use base qw(Rex::Box::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 1 $|++;
98  
99 ################################################################################
100 # BEGIN of class methods
101 ################################################################################
102  
103 =head2 new(name => $vmname)
104  
105 Constructor if used in OO mode.
106  
107 my $box = Rex::Box::VBox->new(name => "vmname");
108  
109 =cut
110  
111 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
112 0 my $class = shift;
113 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
114 0 my $self = $proto->SUPER::new(@_);
115  
116 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
117  
118 0 cloud_service "Amazon";
119 cloud_auth $self->{options}->{access_key},
120 0 $self->{options}->{private_access_key};
121 0 cloud_region $self->{options}->{region};
122  
123 0 return $self;
124 }
125  
126 0 ▶sub import_vm {
Subroutine import_vm: not called CC=8 SCAR=42.8
Pod documented
127 0 my ($self) = @_;
128  
129 # check if machine already exists
130  
131 # Rex::Logger::debug("VM already exists. Don't import anything.");
132 #my @vms = cloud_instance_list;
133 0 my @vms = $self->list_boxes;
134  
135 0 my $vminfo;
136 0 my $vm_exists = 0;
137 0 for my $vm (@vms) {
138 0 ▶ if ( $vm->{name} && $vm->{name} eq $self->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vm->{"name"} and $vm->{"name"} eq $self->{"name"}) 0 0 0
Condition0%0/3
$vm->{"name"} and $vm->{"name"} eq $self->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$vm->{"name"} and $vm->{"name"} eq $self->{"name"}
$vm->{"name"} $vm->{"name"} eq $self->{"name"}
Truth table0%0/3
A and B
A= $vm->{"name"}B= $vm->{"name"} eq $self->{"name"}
ABresult
0X0
100
111
139 0 Rex::Logger::debug("VM already exists. Don't import anything.");
140 0 $vm_exists = 1;
141 0 $vminfo = $vm;
142 }
143 }
144  
145 0 ▶ if ( !$vm_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vm_exists) 0 0 0
146  
147 # if not, create it
148 0 Rex::Logger::info("Creating Amazon instance $self->{name}.");
149 $vminfo = cloud_instance create => {
150 image_id => $self->{ami},
151 name => $self->{name},
152 key => $self->{options}->{auth_key},
153 zone => $self->{options}->{zone},
154 type => $self->{type} || "m1.large",
155 0 ▶ security_group => $self->{security_group} || "default",
Condition0%0/4
$self->{'type'} || 'm1.large'
l!l
00
$self->{'security_group'} || 'default'
l!l
00
MC/DC0%0/1
$self->{'type'} || 'm1.large'
$self->{'type'}
MC/DC0%0/1
$self->{'security_group'} || 'default'
$self->{'security_group'}
Truth table0%0/2
A
A= $self->{'type'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'security_group'}
Aresult
11
00
156 options => $self->options,
157 };
158 }
159  
160 # start if stopped
161 0 ▶ if ( $vminfo->{state} eq "stopped" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vminfo->{"state"} eq "stopped") 0 0 0
162 0 cloud_instance start => $vminfo->{id};
163 }
164  
165 0 $self->{info} = $vminfo;
166 }
167  
168 =head2 ami($ami_id)
169  
170 Set the AMI ID for the box.
171  
172 =cut
173  
174 0 ▶sub ami {
Subroutine ami: not called CC=1 SCAR=6.9
Pod documented
175 0 my ( $self, $ami ) = @_;
176 0 $self->{ami} = $ami;
177 }
178  
179 =head2 type($type)
180  
181 Set the type of the Instance. For example "m1.large".
182  
183 =cut
184  
185 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
186 0 my ( $self, $type ) = @_;
187 0 $self->{type} = $type;
188 }
189  
190 =head2 security_group($sec_group)
191  
192 Set the Amazon security group for this Instance.
193  
194 =cut
195  
196 0 ▶sub security_group {
Subroutine security_group: not called CC=1 SCAR=6.9
Pod documented
197 0 my ( $self, $sec_group ) = @_;
198 0 $self->{security_group} = $sec_group;
199 }
200  
201 =head2 forward_port(%option)
202  
203 Not available for Amazon Boxes.
204  
205 =cut
206  
207 0 ▶sub forward_port { Rex::Logger::debug("Not available for Amazon Boxes."); }
Subroutine forward_port: not called CC=1 SCAR=6.9
Pod documented
208  
209 =head2 share_folder(%option)
210  
211 Not available for Amazon Boxes.
212  
213 =cut
214  
215 0 ▶sub share_folder { Rex::Logger::debug("Not available for Amazon Boxes."); }
Subroutine share_folder: not called CC=1 SCAR=6.9
Pod documented
216  
217 0 ▶sub list_boxes {
Subroutine list_boxes: not called CC=3 SCAR=24.8
Pod documented
218 0 my ($self) = @_;
219  
220 0 my @vms = cloud_instance_list;
221 my @ret = grep {
222 0 $_->{name}
223 && $_->{state} ne "terminated"
224 0 ▶ && $_->{state} ne "shutting-down"
Condition0%0/6
$_->{'name'} && $_->{'state'} ne 'terminated' && $_->{'state'} ne 'shutting-down'
!ll&&!rl&&r
000
$_->{"name"} and $_->{"state"} ne "terminated"
!ll&&!rl&&r
000
MC/DC0%0/3
$_->{'name'} && $_->{'state'} ne 'terminated' && $_->{'state'} ne 'shutting-down'
$_->{"name"} $_->{"state"} ne "terminated" $_->{'state'} ne 'shutting-down'
Truth table0%0/4
A and B && C
A= $_->{"name"}B= $_->{"state"} ne "terminated"C= $_->{'state'} ne 'shutting-down'
ABCresult
0XX0
10X0
1100
1111
225 } @vms; # only vms with names...
226  
227 0 return @ret;
228 }
229  
230 0 ▶sub status {
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
231 0 my ($self) = @_;
232  
233 0 $self->info;
234  
235 0 ▶ if ( $self->{info}->{state} eq "running" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'info'}{'state'} eq 'running') { } 0 0 0
236 0 return "running";
237 }
238 else {
239 0 return "stopped";
240 }
241 }
242  
243 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
244 0 my ($self) = @_;
245  
246 0 $self->info;
247  
248 0 Rex::Logger::info( "Starting instance: " . $self->{name} );
249  
250 0 cloud_instance start => $self->{info}->{id};
251 }
252  
253 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
254 0 my ($self) = @_;
255  
256 0 Rex::Logger::info( "Stopping instance: " . $self->{name} );
257  
258 0 $self->info;
259  
260 0 cloud_instance stop => $self->{info}->{id};
261 }
262  
263 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
264 0 my ($self) = @_;
265  
266 0 Rex::Logger::info( "Destroying instance: " . $self->{name} );
267  
268 0 $self->info;
269  
270 0 cloud_instance terminate => $self->{info}->{id};
271 }
272  
273 =head2 info
274  
275 Returns a hashRef of vm information.
276  
277 =cut
278  
279 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
280 0 my ($self) = @_;
281 0 ( $self->{info} ) = grep { $_->{name} eq $self->{name} } $self->list_boxes;
282 0 return $self->{info};
283 }
284  
285 0 ▶sub ip {
Subroutine ip: not called CC=1 SCAR=6.9
Pod documented
286 0 my ($self) = @_;
287  
288 # get instance info
289 0 ( $self->{info} ) = grep { $_->{name} eq $self->{name} } $self->list_boxes;
290  
291 0 return $self->{info}->{ip};
292 }
293  
294 1 1;
« lib/Rex/File/Parser/Ini.pm ↑ Summary lib/Rex/Helper/INI.pm »