From: hakim Date: Sat, 20 Nov 2010 22:48:51 +0000 (+0000) Subject: attempting to fix ::Provision X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a9e65ceef2ae9d568156215f278436c53fd2a80d;p=p5sagit%2FOyster.git attempting to fix ::Provision --- diff --git a/lib/Dist/Zilla/App/Command/provision.pm b/lib/Dist/Zilla/App/Command/provision.pm index 8b9cab7..e394b67 100644 --- a/lib/Dist/Zilla/App/Command/provision.pm +++ b/lib/Dist/Zilla/App/Command/provision.pm @@ -9,6 +9,7 @@ use Dist::Zilla::App -command; use Moose; use Config::Any; use Hash::Merge 'merge'; +Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' ); use Oyster::Provision; sub abstract { 'provision a new Oyster VM' } @@ -34,6 +35,8 @@ sub execute { my @hashes = grep $_, $Provision->{Default}, $Provision->{$name} or die "No section for <$name>, and no "; + warn Dumper(\@hashes); + my %hash = @hashes > 1 ? %{ merge( @hashes ) } : %{ $hashes[0] }; my $type = delete $hash{type} || 'Oyster::Provision::Rackspace'; @@ -42,9 +45,10 @@ sub execute { $hash{size} ||= 1; # id 1 - ram 256 MiB - disk 10 GiB $hash{image} ||= 69; # id 69 - Ubuntu 10.10 (meerkat) + warn Dumper(\%hash); use Data::Dumper; + my $server = Oyster::Provision->new( name => $name, - config => \%hash, %hash, ); $server->create; diff --git a/lib/Oyster/Provision.pm b/lib/Oyster/Provision.pm index 0c1aaa6..38b8b43 100644 --- a/lib/Oyster/Provision.pm +++ b/lib/Oyster/Provision.pm @@ -2,23 +2,20 @@ package Oyster::Provision; use Moose; -has 'name' => ( is => 'ro', isa => 'Str', required => 1 ); -has 'size' => ( is => 'ro', isa => 'Str', required => 1 ); -has 'image' => ( is => 'ro', isa => 'Str', required => 1 ); -has 'pub_ssh' => ( is => 'ro', isa => 'Str', required => 1 ); - -has 'config' => (is => 'rw', isa => 'HashRef', required => 1 ); +has 'api_username' => ( is => 'ro', isa => 'Str'); +has 'api_password' => ( is => 'ro', isa => 'Str'); +has 'name' => ( is => 'ro', isa => 'Str'); +has 'size' => ( is => 'ro', isa => 'Str'); +has 'image' => ( is => 'ro', isa => 'Str'); +has 'pub_ssh' => ( is => 'ro', isa => 'Str'); +has 'provision_backend' => (is => 'rw', isa => 'Str', required => 1, default => 'Oyster::Provision::Rackspace' ); sub BUILD { my $self = shift; - if(!exists($self->config()->{provision_backend})) { - $self->config()->{provision_backend} = 'Oyster::Provision::Rackspace'; - } + my $role = $self->provision_backend; - my $role = $self->config()->{provision_backend}; - eval "use $role"; "$role"->meta->apply($self); } diff --git a/lib/Oyster/Provision/AmazonEC2.pm b/lib/Oyster/Provision/AmazonEC2.pm index 565e1ec..b064253 100644 --- a/lib/Oyster/Provision/AmazonEC2.pm +++ b/lib/Oyster/Provision/AmazonEC2.pm @@ -3,14 +3,19 @@ use Carp; use Moose::Role; use Net::Amazon::EC2; -requires 'config'; +has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); +sub _build_api_username { + my $self = shift; + return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER}; + die "Need api_username or CLOUDSERVERS_USER in environment"; +} -has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, default => sub { - die "Need api_username"; -}); -has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, default => sub { - die "Need api_password"; -}); +has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); +sub _build_api_password { + my $self = shift; + return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY}; + die "Need api_password or CLOUDSERVERS_KEY in environment"; +} has ec2_oyster_key => (is => 'rw', isa => 'Str', default => "OysterDefault"); @@ -26,7 +31,7 @@ sub ec2 { unless(defined($key_pairs)) { - print("Creating $self->ec2_oyster_key key pair\n"); + printf("Creating %s pair\n", $self->ec2_oyster_key); $ec2->create_key_pair({ KeyName => $self->ec2_oyster_key }); } @@ -37,11 +42,9 @@ sub ec2 { sub create { my $self = shift; - $self->config(); - # Start 1 new instance from AMI: ami-XXXXXXXX my $instance = $self->ec2->run_instances( - ImageId => $self->image() or "ami-be6e99d7", + ImageId => $self->image() || "ami-be6e99d7", KeyName => $self->ec2_oyster_key, MinCount => 1, MaxCount => 1, @@ -52,7 +55,6 @@ sub create { sub delete { my $self = shift; - $self->config(); } sub resize { diff --git a/lib/Oyster/Provision/Rackspace.pm b/lib/Oyster/Provision/Rackspace.pm index 61e8977..ed14944 100644 --- a/lib/Oyster/Provision/Rackspace.pm +++ b/lib/Oyster/Provision/Rackspace.pm @@ -9,13 +9,15 @@ requires 'config'; has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); sub _build_api_username { return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER}; - die "Need api_username or CLOUDSERVERS_USER in environment"; + return $self->config->{api_username} + or die "Need api_username or CLOUDSERVERS_USER in environment"; } has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); sub _build_api_password { return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY}; - die "Need api_password or CLOUDSERVERS_KEY in environment"; + return $self->config->{api_password} + or die "Need api_password or CLOUDSERVERS_KEY in environment"; } has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', default => sub {