Commit | Line | Data |
c9ecd647 |
1 | package Oyster::Provision::Rackspace; |
3ded6347 |
2 | use Moose::Role; |
4bd125ea |
3 | use Carp; |
4 | use Net::RackSpace::CloudServers; |
5 | use Net::RackSpace::CloudServers::Server; |
6 | use MIME::Base64; |
3ded6347 |
7 | |
8 | requires 'config'; |
9 | |
189564ce |
10 | has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); |
11 | sub _build_api_username { |
758d921f |
12 | return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER}; |
13 | confess "Need api_username or CLOUDSERVERS_USER in environment"; |
4bd125ea |
14 | }); |
8a0402ec |
15 | |
189564ce |
16 | has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1); |
17 | sub _build_api_password { |
758d921f |
18 | return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY}; |
8a0402ec |
19 | confess "Need api_password or CLOUDSERVERS_KEY in environment"; |
189564ce |
20 | } |
4bd125ea |
21 | |
1f1bb35e |
22 | has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', default => sub { |
4bd125ea |
23 | my $self = shift; |
24 | my $rs = Net::RackSpace::CloudServers->new( |
25 | user => $self->api_username, |
8a0402ec |
26 | key => $self->api_password, |
4bd125ea |
27 | ); |
28 | $rs; |
29 | }); |
30 | |
8a0402ec |
31 | after BUILD => sub { |
4bd125ea |
32 | my $self = shift; |
33 | # get api username and key from config? |
34 | my $config = $self->config; |
8a0402ec |
35 | |
36 | |
37 | |
4bd125ea |
38 | # ... |
fd5557b2 |
39 | }; |
4bd125ea |
40 | |
3ded6347 |
41 | sub create { |
42 | my $self = shift; |
43 | |
4bd125ea |
44 | # Do nothing if the server named $self->name already exists |
45 | return if scalar grep { $_->name eq $self->name } $self->_rs->get_server(); |
46 | |
47 | # Check the ssh pub key exists and is <10K |
48 | confess "SSH pubkey needs to exist" if !-f $self->pub_ssh; |
49 | my $pub_ssh = do { |
50 | local $/=undef; |
51 | open my $fh, '<', $self->pub_ssh or die "Cannot open ", $self->pub_ssh, ": $!"; |
52 | my $_data = <$fh>; |
53 | close $fh or die "Cannot close ", $self->pub_ssh, ": $!"; |
54 | $_data; |
55 | }; |
56 | confess "SSH pubkey needs to be < 10KiB" if length $pub_ssh > 10*1024; |
57 | |
58 | # Build the server |
59 | my $server = Net::RackSpace::CloudServers::Server->new( |
60 | cloudservers => $self->_cs, |
61 | name => $self->name, |
62 | flavor => $self->size, |
63 | image => $self->image, |
64 | personality => [ |
65 | { |
66 | path => $self->pub_ssh, |
67 | contents => encode_base64($pub_ssh), |
68 | }, |
69 | ], |
70 | ); |
71 | $server->create_server; |
72 | |
73 | warn "Server public IP is: ", ($server->public_address)[0], "\n"; |
74 | warn "Server root password: ", $server->adminpass, "\n"; |
75 | |
76 | # Connect to server and execute installation routines? |
77 | # Use Net::SSH? |
3ded6347 |
78 | } |
79 | |
3ded6347 |
80 | sub delete { |
81 | my $self = shift; |
82 | |
4bd125ea |
83 | # Die if the server named $self->name already exists |
84 | my ($server) = grep { $_->name eq $self->name } $self->_rs->get_server(); |
85 | confess "No such server: ", $self->name if !$server; |
86 | |
87 | # Goodbye cruel user! |
88 | $server->delete_server(); |
3ded6347 |
89 | } |
90 | |
91 | sub resize { |
92 | my $self = shift; |
93 | |
94 | $self->config(); |
95 | } |
c9ecd647 |
96 | |
97 | 1; |
dd69a60d |
98 | |
99 | __END__ |
100 | |
101 | =head1 NAME |
102 | |
103 | Oyster::Provision::Rackspace -- Provision your Oyster on Rackspace |
104 | |
105 | =head1 SYNOPSIS |
106 | |
107 | Use the Rackspace backend on your Oyster configuration file |
108 | |
109 | =head1 REQUIRED PARAMETERS |
110 | |
111 | The following are required to instantiate a backend: |
112 | |
113 | =over |
114 | |
4bd125ea |
115 | =item api_username |
116 | |
117 | The rackspace API username, or C<$ENV{RACKSPACE_USER}> will be used if that is |
118 | not given |
119 | |
8a0402ec |
120 | =item password |
121 | |
122 | This is your rackspace API Key |
4bd125ea |
123 | |
124 | The rackspace API key, or C<$ENV{RACKSPACE_KEY}> will be used if that is not |
125 | given |
126 | |
dd69a60d |
127 | =item name |
128 | |
129 | The name of your new/existing rackspace server. |
130 | |
131 | =item size |
132 | |
133 | The size ID of the rackspace server you want to create. |
134 | Use the following incantation to see them: |
135 | |
136 | perl -MNet::RackSpace::CloudServers -e' |
137 | $r=Net::RackSpace::CloudServers->new( |
138 | user=>$ENV{CLOUDSERVERS_USER}, |
139 | key=>$ENV{CLOUDSERVERS_KEY}, |
140 | ); |
141 | print map |
142 | { "id $_->{id} ram $_->{ram} disk $_->{disk}\n" } |
143 | $r->get_flavor_detail |
144 | ' |
145 | id 1 ram 256 disk 10 |
146 | id 2 ram 512 disk 20 |
147 | id 3 ram 1024 disk 40 |
148 | id 4 ram 2048 disk 80 |
149 | id 5 ram 4096 disk 160 |
150 | id 6 ram 8192 disk 320 |
151 | id 7 ram 15872 disk 620 |
152 | |
153 | =item image |
154 | |
155 | The image ID of the rackspace server you want to create. |
156 | Use the following incantation to see them: |
157 | |
158 | perl -MNet::RackSpace::CloudServers -e' |
159 | $r=Net::RackSpace::CloudServers->new( |
160 | user=>$ENV{CLOUDSERVERS_USER}, |
161 | key=>$ENV{CLOUDSERVERS_KEY}, |
162 | ); |
163 | print map |
164 | { "id $_->{id} name $_->{name}\n" } |
165 | $r->get_image_detail |
166 | ' |
167 | id 29 name Windows Server 2003 R2 SP2 x86 |
168 | id 69 name Ubuntu 10.10 (maverick) |
169 | id 41 name Oracle EL JeOS Release 5 Update 3 |
170 | id 40 name Oracle EL Server Release 5 Update 4 |
171 | id 187811 name CentOS 5.4 |
172 | id 4 name Debian 5.0 (lenny) |
173 | id 10 name Ubuntu 8.04.2 LTS (hardy) |
174 | id 23 name Windows Server 2003 R2 SP2 x64 |
175 | id 24 name Windows Server 2008 SP2 x64 |
176 | id 49 name Ubuntu 10.04 LTS (lucid) |
177 | id 14362 name Ubuntu 9.10 (karmic) |
178 | id 62 name Red Hat Enterprise Linux 5.5 |
179 | id 53 name Fedora 13 |
180 | id 17 name Fedora 12 |
181 | id 71 name Fedora 14 |
182 | id 31 name Windows Server 2008 SP2 x86 |
183 | id 51 name CentOS 5.5 |
184 | id 14 name Red Hat Enterprise Linux 5.4 |
185 | id 19 name Gentoo 10.1 |
186 | id 28 name Windows Server 2008 R2 x64 |
187 | id 55 name Arch 2010.05 |
188 | |
189 | Oyster only supports Linux images, specifically |
190 | Ubuntu 10.10 (maverick). |
191 | |
192 | =item pub_ssh |
193 | |
194 | The public ssh key you would like copied to the |
195 | new server's C</root/.ssh/authorized_keys> file |
196 | to allow you to ssh in the box without providing |
197 | a root password. |
198 | |
199 | =back |
200 | |
201 | =cut |