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