our
$VERSION
=
'1.7.0'
;
BEGIN {
LWP::UserAgent->
use
;
}
$|++;
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
) =
@_
;
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
) {
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"
,
options
=>
$self
->options,
};
}
if
(
$vminfo
->{state} eq
"stopped"
) {
cloud_instance
start
=>
$vminfo
->{id};
}
$self
->{info} =
$vminfo
;
}
sub
ami {
my
(
$self
,
$ami
) =
@_
;
$self
->{ami} =
$ami
;
}
sub
type {
my
(
$self
,
$type
) =
@_
;
$self
->{type} =
$type
;
}
sub
security_group {
my
(
$self
,
$sec_group
) =
@_
;
$self
->{security_group} =
$sec_group
;
}
sub
forward_port { Rex::Logger::debug(
"Not available for Amazon Boxes."
); }
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
;
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};
}
sub
destroy {
my
(
$self
) =
@_
;
Rex::Logger::info(
"Destroying instance: "
.
$self
->{name} );
$self
->info;
cloud_instance
terminate
=>
$self
->{info}->{id};
}
sub
info {
my
(
$self
) =
@_
;
(
$self
->{info} ) =
grep
{
$_
->{name} eq
$self
->{name} }
$self
->list_boxes;
return
$self
->{info};
}
sub
ip {
my
(
$self
) =
@_
;
(
$self
->{info} ) =
grep
{
$_
->{name} eq
$self
->{name} }
$self
->list_boxes;
return
$self
->{info}->{ip};
}
1;