From: iain Date: Sat, 20 Nov 2010 19:04:37 +0000 (+0000) Subject: changes to provision backends better support overridable properties X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a0402ec798af800cd25fe0d26fd1e5ef1d0fef0;p=p5sagit%2FOyster.git changes to provision backends better support overridable properties --- diff --git a/lib/Oyster/Provision.pm b/lib/Oyster/Provision.pm index 36cd7cc..0c1aaa6 100644 --- a/lib/Oyster/Provision.pm +++ b/lib/Oyster/Provision.pm @@ -7,15 +7,16 @@ 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 ); - -sub config { - return {provision_backend => 'Oyster::Provision::Rackspace'}; -} +has 'config' => (is => 'rw', isa => 'HashRef', required => 1 ); sub BUILD { my $self = shift; + if(!exists($self->config()->{provision_backend})) { + $self->config()->{provision_backend} = 'Oyster::Provision::Rackspace'; + } + my $role = $self->config()->{provision_backend}; eval "use $role"; diff --git a/lib/Oyster/Provision/AmazonEC2.pm b/lib/Oyster/Provision/AmazonEC2.pm index 0c58926..8fdd7af 100644 --- a/lib/Oyster/Provision/AmazonEC2.pm +++ b/lib/Oyster/Provision/AmazonEC2.pm @@ -4,36 +4,46 @@ use Net::Amazon::EC2; requires 'config'; -my $ec2_image_id = "ami-1a837773"; -my $ec2_username = "AKIAJXSD25MPWFYTQWIQ"; -my $ec2_password = "m76s9DyoXrHdpVy8HkhjgD0RAjy14bhkQ5Zts/gg"; - -my $ec2 = Net::Amazon::EC2->new( - AWSAccessKeyId => $ec2_username, - SecretAccessKey => $ec2_password, -); - -my $ec2_oyster_key = "OysterDefault"; -my $key_pairs = $ec2->describe_key_pairs({ KeyName => $ec2_oyster_key }); - -unless(defined($key_pairs)) { - - print("Creating $ec2_oyster_key key pair\n"); - $ec2->create_key_pair({ KeyName => $ec2_oyster_key }); - +has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, default => sub { + return "AKIAJXSD25MPWFYTQWIQ"; + confess "Need api_username"; +}); +has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, default => sub { + return "m76s9DyoXrHdpVy8HkhjgD0RAjy14bhkQ5Zts/gg"; + confess "Need api_password"; +}); + +has ec2_oyster_key => (is => 'rw', isa => 'Str', default => "OysterDefault"); + +sub ec2 { + my $self = shift; + + my $ec2 = Net::Amazon::EC2->new( + AWSAccessKeyId => $self->api_username, + SecretAccessKey => $self->api_password, + ); + + my $key_pairs = $ec2->describe_key_pairs({ KeyName => $self->ec2_oyster_key }); + + unless(defined($key_pairs)) { + + print("Creating $ec2_oyster_key key pair\n"); + $ec2->create_key_pair({ KeyName => $self->ec2_oyster_key }); + + } + + return $ec2; } - sub create { my $self = shift; - $self->config(); - + $self->config(); # Start 1 new instance from AMI: ami-XXXXXXXX my $instance = $ec2->run_instances( - ImageId => $ec2_image_id, - KeyName => $ec2_oyster_key, + ImageId => $self->image() or "ami-1a837773", + KeyName => $self->ec2_oyster_key, MinCount => 1, MaxCount => 1, ); @@ -74,6 +84,10 @@ The following are required to instantiate a backend: The name of your new/existing rackspace server. +pub_ssh + +This is a key name to pass to EC2 + =item size The size ID of the rackspace server you want to create. diff --git a/lib/Oyster/Provision/Rackspace.pm b/lib/Oyster/Provision/Rackspace.pm index 31e5973..1f31cfc 100644 --- a/lib/Oyster/Provision/Rackspace.pm +++ b/lib/Oyster/Provision/Rackspace.pm @@ -11,24 +11,28 @@ has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, default => sub return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER}; confess "Need api_username or CLOUDSERVERS_USER in environment"; }); -has 'api_key' => ( is => 'ro', isa => 'Str', required => 1, default => sub { + +has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, default => sub { return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY}; - confess "Need api_key or RACKSPACE_KEY in environment"; + confess "Need api_password or CLOUDSERVERS_KEY in environment"; }); has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', default => sub { my $self = shift; my $rs = Net::RackSpace::CloudServers->new( user => $self->api_username, - key => $self->api_key, + key => $self->api_password, ); $rs; }); -sub BUILD { +after BUILD => sub { my $self = shift; # get api username and key from config? my $config = $self->config; + + + # ... } @@ -111,7 +115,9 @@ The following are required to instantiate a backend: The rackspace API username, or C<$ENV{RACKSPACE_USER}> will be used if that is not given -=item api_key +=item password + +This is your rackspace API Key The rackspace API key, or C<$ENV{RACKSPACE_KEY}> will be used if that is not given