attempting to fix ::Provision
[p5sagit/Oyster.git] / lib / Oyster / Provision / AmazonEC2.pm
1 package Oyster::Provision::AmazonEC2;
2 use Carp;
3 use Moose::Role;
4 use Net::Amazon::EC2;
5
6 has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1);
7 sub _build_api_username {
8     my $self = shift;
9     return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER};
10     die "Need api_username or CLOUDSERVERS_USER in environment";
11 }
12
13 has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1);
14 sub _build_api_password {
15     my $self = shift;
16     return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY};
17     die "Need api_password or CLOUDSERVERS_KEY in environment";
18 }
19
20 has ec2_oyster_key => (is => 'rw', isa => 'Str', default => "OysterDefault");
21
22 sub ec2 {
23     my $self = shift;
24     
25     my $ec2 = Net::Amazon::EC2->new(
26         AWSAccessKeyId  => $self->api_username,
27         SecretAccessKey => $self->api_password,
28    );
29    
30     my $key_pairs = $ec2->describe_key_pairs({ KeyName => $self->ec2_oyster_key });
31     
32     unless(defined($key_pairs)) {
33     
34         printf("Creating %s pair\n", $self->ec2_oyster_key);
35         $ec2->create_key_pair({ KeyName => $self->ec2_oyster_key });
36     
37     }
38    
39    return $ec2;
40 }
41
42 sub create {
43    my $self = shift;
44
45    # Start 1 new instance from AMI: ami-XXXXXXXX
46    my $instance = $self->ec2->run_instances(
47        ImageId  => $self->image() || "ami-be6e99d7",
48        KeyName  => $self->ec2_oyster_key,
49        MinCount => 1,
50        MaxCount => 1,
51    );
52
53 }
54
55 sub delete {
56    my $self = shift;
57
58 }
59
60 sub resize {
61    my $self = shift;
62
63    $self->config();
64 }
65
66 1;
67
68 __END__
69
70 =head1 NAME
71
72 Oyster::Provision::AmazonEC2 -- Provision your Oyster on Amazon EC2
73
74 =head1 SYNOPSIS
75
76 Use the Amazon backend on your Oyster configuration file
77
78 =head1 REQUIRED PARAMETERS
79
80 The following are required to instantiate a backend:
81
82 =over
83
84 =item name
85
86 The name of your new/existing rackspace server.
87
88 pub_ssh
89
90 This is a key name to pass to EC2 
91
92 =item size
93
94 The size ID of the rackspace server you want to create.
95 Use the following incantation to see them:
96
97     perl -MNet::RackSpace::CloudServers -e'
98         $r=Net::RackSpace::CloudServers->new(
99             user=>$ENV{CLOUDSERVERS_USER},
100             key=>$ENV{CLOUDSERVERS_KEY},
101         );
102         print map
103             { "id $_->{id} ram $_->{ram} disk $_->{disk}\n" }
104             $r->get_flavor_detail
105     '
106     id 1 ram 256 disk 10
107     id 2 ram 512 disk 20
108     id 3 ram 1024 disk 40
109     id 4 ram 2048 disk 80
110     id 5 ram 4096 disk 160
111     id 6 ram 8192 disk 320
112     id 7 ram 15872 disk 620
113
114 =item image
115
116 The image ID of the rackspace server you want to create.
117 Use the following incantation to see them:
118
119     perl -MNet::RackSpace::CloudServers -e'
120         $r=Net::RackSpace::CloudServers->new(
121             user=>$ENV{CLOUDSERVERS_USER},
122             key=>$ENV{CLOUDSERVERS_KEY},
123         );
124         print map
125             { "id $_->{id} name $_->{name}\n" }
126             $r->get_image_detail
127     '
128     id 29 name Windows Server 2003 R2 SP2 x86
129     id 69 name Ubuntu 10.10 (maverick)
130     id 41 name Oracle EL JeOS Release 5 Update 3
131     id 40 name Oracle EL Server Release 5 Update 4
132     id 187811 name CentOS 5.4
133     id 4 name Debian 5.0 (lenny)
134     id 10 name Ubuntu 8.04.2 LTS (hardy)
135     id 23 name Windows Server 2003 R2 SP2 x64
136     id 24 name Windows Server 2008 SP2 x64
137     id 49 name Ubuntu 10.04 LTS (lucid)
138     id 14362 name Ubuntu 9.10 (karmic)
139     id 62 name Red Hat Enterprise Linux 5.5
140     id 53 name Fedora 13
141     id 17 name Fedora 12
142     id 71 name Fedora 14
143     id 31 name Windows Server 2008 SP2 x86
144     id 51 name CentOS 5.5
145     id 14 name Red Hat Enterprise Linux 5.4
146     id 19 name Gentoo 10.1
147     id 28 name Windows Server 2008 R2 x64
148     id 55 name Arch 2010.05
149
150 Oyster only supports Linux images, specifically
151 Ubuntu 10.10 (maverick).
152
153 =item pub_ssh
154
155 The public ssh key you would like copied to the
156 new server's C</root/.ssh/authorized_keys> file
157 to allow you to ssh in the box without providing
158 a root password.
159
160 =back
161
162 =cut