Skip to content

Commit

Permalink
* Fix package names to point to Oyster::Provision::Backend::*
Browse files Browse the repository at this point in the history
* Make sure the EC2 backend does the Provision::API
* Add a builder to EC2 backend so that it uses the right image
* Fix some typos and missing variables
* Add stubby resize() method to conform to the API
* Move checking for the ENV variables up to the top level of the configuration
* Make sure that the Provision class creates a new Config object to pass through
* Remove builders from ::Provision::API since we're passing that in now
* Make Provision::Config mutable for now
* Fix up the tests so that we pass in all the correct data
* Remove extraneous and stale test directory

Conflicts:

	t/Provision/01-EC2/01-basic.t
  • Loading branch information
perigrin committed Jan 31, 2011
1 parent e429268 commit 187d1fd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 61 deletions.
22 changes: 19 additions & 3 deletions lib/Oyster/Provision.pm
Expand Up @@ -2,6 +2,8 @@ package Oyster::Provision;
use Moose; use Moose;
use namespace::autoclean; use namespace::autoclean;


use Oyster::Provision::Config;

has config => ( has config => (
isa => 'HashRef', isa => 'HashRef',
is => 'ro', is => 'ro',
Expand All @@ -11,7 +13,7 @@ has config => (
has provision_class => ( has provision_class => (
isa => 'Str', isa => 'Str',
is => 'ro', is => 'ro',
default => 'Oyster::Provision::Rackspace' default => 'Oyster::Provision::Backend::Rackspace'
); );


has 'provision_backend' => ( has 'provision_backend' => (
Expand All @@ -22,8 +24,22 @@ has 'provision_backend' => (
); );


sub _build_provision_backend { sub _build_provision_backend {
my $self = shift; my $self = shift;
$self->provision_class->new( ${ $self->config } ); my $class = $self->provision_class;
Class::MOP::load_class($class);

my $config = $self->config;

my $user = ( $config->{api_username} || $ENV{CLOUDSERVERS_USER} )
or die 'Need api_username in the config or CLOUDSERVERS_USER in the environment';
my $pass = ( $config->{api_password} || $ENV{CLOUDSERVERS_KEY} )
or die 'Need api_password in the config or CLOUDSERVERS_KEY in the environment';

$class->new(
config => Oyster::Provision::Config->new($self->config),
api_username => $user,
api_password => $pass,
);
} }
1; 1;


Expand Down
28 changes: 7 additions & 21 deletions lib/Oyster/Provision/API.pm
Expand Up @@ -6,40 +6,26 @@ requires qw(
create create
delete delete
resize resize
_build_api_username
_build_api_password
); );


has config => ( has config => (
isa => 'Oyster::Provision::Config', isa => 'Oyster::Provision::Config',
is => 'ro', is => 'ro',
required => 1, required => 1,
coerce => 1, handles => [qw( name size image pub_ssh )],
); );


has api_username => ( has api_username => (
is => 'ro', is => 'ro',
isa => 'Str', isa => 'Str',
lazy => 1, required => 1,
builder => '_build_api_username',
); );


sub _build_api_username {
return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER};
die "Need api_username or CLOUDSERVERS_USER in environment";
}

has api_password => ( has api_password => (
is => 'ro', is => 'ro',
isa => 'Str', isa => 'Str',
lazy => 1, required => 1,
builder => '_build_api_password',
); );


sub _build_api_password {
return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY};
die "Need api_password or CLOUDSERVERS_KEY in environment";
}

1; 1;
__END__ __END__
21 changes: 12 additions & 9 deletions lib/Oyster/Provision/Backend/AmazonEC2.pm
Expand Up @@ -2,20 +2,21 @@ package Oyster::Provision::Backend::AmazonEC2;
use Moose; use Moose;
use namespace::autoclean; use namespace::autoclean;


with qw(Oyster::Provision::API);

use Net::Amazon::EC2; use Net::Amazon::EC2;


sub BUILD {
my $self = shift;
$self->config->_image('ami-be6e99d7') unless $self->config->has_image;
}

has ec2_oyster_key => ( has ec2_oyster_key => (
is => 'rw', is => 'rw',
isa => 'Str', isa => 'Str',
default => 'OysterDefault' default => 'OysterDefault'
); );


has image => (
isa => 'Str',
is => 'ro',
default => 'ami-be6e99d7'
);

has ec2 => ( has ec2 => (
isa => 'Net::Amazon::EC2', isa => 'Net::Amazon::EC2',
is => 'ro', is => 'ro',
Expand Down Expand Up @@ -77,7 +78,7 @@ sub _wait_for_instance {
my $name = $self->name; # cache the name my $name = $self->name; # cache the name
for ( 1 .. 10 ) { # XXX: try 10 times before giving up for ( 1 .. 10 ) { # XXX: try 10 times before giving up
my $result = my $result =
$self->ec2->describe_instances( InstanceId => [$instance], ); $self->ec2->describe_instances( InstanceId => [$instance_id], );


confess $result->errors->[0]->message confess $result->errors->[0]->message
if $result->isa('Net::Amazon::EC2::Errors'); if $result->isa('Net::Amazon::EC2::Errors');
Expand All @@ -102,8 +103,8 @@ sub _wait_for_instance {
} }


sub delete { sub delete {
my $self = shift; my $self = shift;
$self->ec2->terminate_instances( my $result = $self->ec2->terminate_instances(
InstanceId => [ $self->instance->instance_id ] ); InstanceId => [ $self->instance->instance_id ] );


confess $result->errors->[0]->message confess $result->errors->[0]->message
Expand All @@ -112,6 +113,8 @@ sub delete {
$self->remove_instance; $self->remove_instance;
} }


sub resize { confess "ABSTRACT" }

1; 1;


__END__ __END__
Expand Down
16 changes: 10 additions & 6 deletions lib/Oyster/Provision/Config.pm
Expand Up @@ -2,11 +2,15 @@ package Oyster::Provision::Config;
use Moose; use Moose;
use namespace::autoclean; use namespace::autoclean;


has [qw( name size image pub_ssh )] => ( for my $attr (qw( name size image pub_ssh )) {
isa => 'Str', has $attr => (
is => 'ro', isa => 'Str',
required => 1, is => 'ro',
); writer => "_${attr}",
predicate => "has_${attr}",
required => 1,
);
}


1 1
__END__ __END__
31 changes: 31 additions & 0 deletions t/Provision/01-EC2/01-basic.t
@@ -0,0 +1,31 @@
use strict;
use Test::More;

use Oyster::Provision;

unless ( $ENV{TEST_AWS_USER} && $ENV{TEST_AWS_KEY} ) {
plan skip_all => << 'END';
Set TEST_AWS_USER and TEST_AWS_KEY in your environment for this test to run'
END
}

ok(
my $server = Oyster::Provision->new(
provision_class => 'Oyster::Provision::Backend::AmazonEC2',
config => {
api_username => $ENV{TEST_AWS_USER},
api_password => $ENV{TEST_AWS_KEY},
size => 'm1.small',
name => 'Oyster-Test',
image => 'ami-be6e99d7',
pub_ssh => "$ENV{HOME}/Dropbox/Public/id_rsa.pub",
}
),
'created server instance'
);

ok( $server->create, 'deployed server' );

ok( $server->delete, 'destroyed server' );

done_testing();
22 changes: 0 additions & 22 deletions t/Provision/EC2/01-basic.t

This file was deleted.

0 comments on commit 187d1fd

Please sign in to comment.