attempting to fix ::Provision
[p5sagit/Oyster.git] / lib / Oyster / Provision / Rackspace.pm
index 9abae87..ed14944 100644 (file)
@@ -1,37 +1,34 @@
 package Oyster::Provision::Rackspace;
 use Moose::Role;
-use Carp;
 use Net::RackSpace::CloudServers;
 use Net::RackSpace::CloudServers::Server;
 use MIME::Base64;
 
 requires 'config';
 
-has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, default => sub {
-    return $ENV{RACKSPACE_USER} if exists $ENV{RACKSPACE_USER};
-    confess "Need api_username or RACKSPACE_USER in environment";
-});
-has 'api_key' => ( is => 'ro', isa => 'Str', required => 1, default => sub {
-    return $ENV{RACKSPACE_KEY} if exists $ENV{RACKSPACE_KEY};
-    confess "Need api_key or RACKSPACE_KEY in environment";
-});
+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};
+    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};
+    return $self->config->{api_password}
+        or die "Need api_password or CLOUDSERVERS_KEY in environment";
+}
 
-has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', lazy_build => 1, default => sub {
+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 {
-    my $self = shift;
-    # get api username and key from config?
-    my $config = $self->config;
-    # ...
-}
-
 sub create {
    my $self = shift;
 
@@ -39,7 +36,7 @@ sub create {
    return if scalar grep { $_->name eq $self->name } $self->_rs->get_server();
 
    # Check the ssh pub key exists and is <10K
-   confess "SSH pubkey needs to exist" if !-f $self->pub_ssh;
+   die "SSH pubkey needs to exist" if !-f $self->pub_ssh;
    my $pub_ssh = do {
        local $/=undef;
        open my $fh, '<', $self->pub_ssh or die "Cannot open ", $self->pub_ssh, ": $!";
@@ -47,25 +44,36 @@ sub create {
        close $fh or die "Cannot close ", $self->pub_ssh, ": $!";
        $_data;
    };
-   confess "SSH pubkey needs to be < 10KiB" if length $pub_ssh > 10*1024;
+   die "SSH pubkey needs to be < 10KiB" if length $pub_ssh > 10*1024;
 
    # Build the server
    my $server = Net::RackSpace::CloudServers::Server->new(
-       cloudservers => $self->_cs,
-       name => $self->name,
-       flavor => $self->size,
-       image => $self->image,
-       personality => [
+      cloudservers => $self->_rs,
+      name         => $self->name,
+      flavorid     => $self->size,
+      imageid      => $self->image,
+      personality => [
            {
-               path     => $self->pub_ssh,
+               path     => '/root/.ssh/authorized_keys',
                contents => encode_base64($pub_ssh),
            },
-       ],
+      ],
    );
-   $server->create_server;
-
-   warn "Server public IP is:  ", ($server->public_address)[0], "\n";
-   warn "Server root password: ", $server->adminpass, "\n";
+   my $newserver = $server->create_server;
+   warn "Server root password: ", $newserver->adminpass, "\n";
+
+   do {
+      $|=1;
+      my @tmpservers = $self->_rs->get_server_detail();
+      $server = ( grep { $_->name eq $self->name } @tmpservers )[0];
+      print "\rServer status: ", ($server->status || '?'), " progress: ", ($server->progress || '?');
+      if ( ( $server->status // '' ) ne 'ACTIVE' ) {
+        print " sleeping..";
+        sleep 2;
+      }
+   } while ( ( $server->status // '' ) ne 'ACTIVE' );
+
+   warn "Server public IP is: @{$server->public_address}\n";
 
    # Connect to server and execute installation routines?
    # Use Net::SSH?
@@ -76,7 +84,7 @@ sub delete {
 
    # Die if the server named $self->name already exists
    my ($server) = grep { $_->name eq $self->name } $self->_rs->get_server();
-   confess "No such server: ", $self->name if !$server;
+   die "No such server: ", $self->name if !$server;
 
    # Goodbye cruel user!
    $server->delete_server();
@@ -111,7 +119,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