X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FOyster%2FProvision%2FRackspace.pm;h=d9627296f0f9fc7e39c4892fe79f4bf48609d373;hb=a92449de4172e1f7e8d6de63545cfa40e43a4e84;hp=6929a9a279016de4691e9c1afd3a5be08485252e;hpb=6895749d358fc3388e5b52208725f5d0d6e35af6;p=p5sagit%2FOyster.git diff --git a/lib/Oyster/Provision/Rackspace.pm b/lib/Oyster/Provision/Rackspace.pm index 6929a9a..d962729 100644 --- a/lib/Oyster/Provision/Rackspace.pm +++ b/lib/Oyster/Provision/Rackspace.pm @@ -1,41 +1,75 @@ package Oyster::Provision::Rackspace; use Moose::Role; -use Carp; use Net::RackSpace::CloudServers; use Net::RackSpace::CloudServers::Server; use MIME::Base64; +# TODO http://failverse.com/manually-creating-a-cloud-server-from-a-cloud-files-image/ +# in order to use an already created image to build the server, a la EC2 way + requires 'config'; -has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); -sub _build_api_username { +has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub { + my $self = shift; return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER}; - confess "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 { +has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub { + my $self = shift; return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY}; - confess "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 { +has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', lazy => 1, default => sub { my $self = shift; - my $rs = Net::RackSpace::CloudServers->new( - user => $self->api_username, - key => $self->api_password, - ); + my $rs = eval { + Net::RackSpace::CloudServers->new( + user => $self->api_username, + key => $self->api_password, + ); + }; + if ( $@ ) { + die + "Could not instantiate a backend connection to RackSpace CloudServers.\n", + "Check the api_username and api_password on the configuration file\n"; + } $rs; }); sub create { my $self = shift; + die "Rackspace Provisioning backend requires a server name\n" if !defined $self->name; + # Do nothing if the server named $self->name already exists return if scalar grep { $_->name eq $self->name } $self->_rs->get_server(); + # Validate size and image + { + die "Rackspace Provisioning backend requires a server image\n" if !defined $self->image; + my @allowed_images = $self->_rs->get_image(); + my $image_id = $self->image; + if ( !scalar grep { $_->{id} eq $image_id } @allowed_images ) { + die "Rackspace Provisioning backend requires a valid image id\nValid images:\n", + (map { sprintf("id %-10s -- %s\n", $_->{id}, $_->{name}) } @allowed_images), + "\n"; + } + + die "Rackspace Provisioning backend requires a server size\n" if !defined $self->size; + my @allowed_flavors = $self->_rs->get_flavor(); + my $flavor_id = $self->size; + if ( !scalar grep { $_->{id} eq $flavor_id } @allowed_flavors ) { + die "Rackspace Provisioning backend requires a valid size id\nValid flavors:\n", + (map { sprintf("id %-10s -- %s\n", $_->{id}, $_->{name}) } @allowed_flavors), + "\n"; + } + } + # 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, ": $!"; @@ -43,7 +77,7 @@ 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( @@ -53,18 +87,94 @@ sub create { imageid => $self->image, personality => [ { - path => $self->pub_ssh, + path => '/root/.ssh/authorized_keys', contents => encode_base64($pub_ssh), }, ], ); - $server->create_server; + 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 IPs are: @{$server->public_address}\n"; + my $public_ip = (@{$server->public_address})[0]; + my $servername = sprintf("oyster-%s", $self->name); + + # Adds the server's name to the user's ~/.ssh/config + # using oyster-servername + { + open my $fh, '>>', "$ENV{HOME}/.ssh/config" + or die "Error opening $ENV{HOME}/.ssh/config for appending: $!"; + my $template = "\nHost %s\n" . + " User root\n" . + " Port 22\n" . + " Compression yes\n" . + " HostName %s\n" . + "\n"; + print $fh sprintf($template, $servername, $public_ip); + close $fh or die "Error closing $ENV{HOME}/.ssh/config: $!"; + } + + # Connect to server and execute installation routines -- unlike EC2 each + # server needs instantiated from scratch every time + warn "Initializing the server..."; + $self->initialise(); + + warn "Deploying the application..."; + $self->deploy(); +} - warn "Server public IP is: ", ($server->public_address)[0], "\n"; - warn "Server root password: ", $server->adminpass, "\n"; +sub initialise { + my $self = shift; + my $servername = sprintf("oyster-%s", $self->name); + + # Adds the server's key to the user's ~/.ssh/authorized_keys + # FIXME there must be a better way?! + warn "Adding SSH key for $servername to ~/.ssh/authorized_keys\n"; + qx{/usr/bin/ssh -o StrictHostKeyChecking=no -l root $servername 'echo oyster'}; + + # FIXME should call the module which does the installation... + warn "Installing wget, lighttpd and git...\n"; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /usr/bin/apt-get install --yes wget lighttpd git git-core'}; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /usr/sbin/service lighttpd stop'}; + warn "Adding user perloyster...\n"; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /usr/sbin/adduser --disabled-password --gecos "Perl Oyster" perloyster'}; + warn "Copying keys to ~perloyster...\n"; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /bin/mkdir ~perloyster/.ssh/'}; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /bin/cp ~/.ssh/authorized_keys ~perloyster/.ssh/'}; + print qx{/usr/bin/ssh -l root $servername 'LC_ALL=C /bin/chown --recursive perloyster ~perloyster/.ssh/'}; + warn "Making perloyster readable...\n"; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /bin/chmod a+r ~perloyster/'}; + #warn "Installing cpanminus...\n"; + #print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /usr/bin/wget --no-check-certificate http://xrl.us/cpanm ; chmod +x cpanm'}; + #warn "Installing prerequisites for Oyster::Deploy::Git...\n"; + #print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C ./cpanm --local-lib=~/perl5 App::cpanminus Dist::Zilla'}; + warn "Getting and unpacking base system...\n"; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /usr/bin/wget --no-check-certificate https://darkpan.com/files/oyster-prereqs-20101122-2217.tgz'}; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /bin/tar xvf oyster-prereqs-20101122-2217.tgz'}; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /bin/echo export PERL5LIB="/home/perloyster/perl5/lib/perl5:/home/perloyster/perl/lib/perl5/x86_64-linux-gnu-thread-multi" >> ~/.bashrc'}; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /bin/echo export PATH="/home/perloyster/perl5/bin:\$PATH" >> ~/.bashrc'}; + + warn "Pushing and unpacking Oyster::Deploy::Git...\n"; + print qx{/usr/bin/ssh -l perloyster $servername 'LC_ALL=C /bin/mkdir -p perl5/lib/perl5/Oyster/Deploy'}; + print qx{/usr/bin/scp lib/Oyster/Deploy/Git.pm perloyster\@$servername:perl5/lib/perl5/Oyster/Deploy/}; +} - # Connect to server and execute installation routines? - # Use Net::SSH? +sub deploy { + my $self = shift; + my $servername = sprintf("oyster-%s", $self->name); + warn "Deploying application to $servername...\n"; + print qx{/usr/bin/ssh -l perloyster $servername "perl -MOyster::Deploy::Git -le'\$g=Oyster::Deploy::Git->new;\$g->deploy(q,/home/perloyster/oyster,)'"}; } sub delete { @@ -72,7 +182,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(); @@ -107,7 +217,7 @@ 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 password +=item api_password This is your rackspace API Key