amazon-ec2 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +166 -95
- data/License.txt +67 -20
- data/Manifest.txt +17 -6
- data/README.txt +112 -2
- data/Rakefile +15 -5
- data/bin/ec2-gem-example.rb +61 -0
- data/bin/ec2sh +73 -0
- data/bin/setup.rb +19 -0
- data/lib/EC2.rb +142 -61
- data/lib/EC2/console.rb +44 -0
- data/lib/EC2/exceptions.rb +136 -0
- data/lib/EC2/image_attributes.rb +137 -29
- data/lib/EC2/images.rb +120 -73
- data/lib/EC2/instances.rb +168 -98
- data/lib/EC2/keypairs.rb +79 -23
- data/lib/EC2/responses.rb +142 -321
- data/lib/EC2/security_groups.rb +209 -117
- data/lib/EC2/version.rb +11 -2
- data/test/test_EC2.rb +44 -13
- data/test/test_EC2_console.rb +54 -0
- data/test/test_EC2_image_attributes.rb +188 -0
- data/test/test_EC2_images.rb +191 -0
- data/test/test_EC2_instances.rb +303 -0
- data/test/test_EC2_keypairs.rb +123 -0
- data/test/test_EC2_responses.rb +102 -0
- data/test/test_EC2_security_groups.rb +205 -0
- data/test/test_EC2_version.rb +44 -0
- data/test/test_helper.rb +16 -8
- data/website/index.html +378 -86
- data/website/index.txt +339 -88
- data/website/stylesheets/screen.css +8 -8
- metadata +89 -16
- data/examples/ec2-example.rb +0 -48
- data/test/test_responses.rb +0 -17
data/test/test_helper.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
-
|
1
|
+
#--
|
2
|
+
# Amazon Web Services EC2 Query API Ruby library
|
3
|
+
#
|
4
|
+
# Ruby Gem Name:: amazon-ec2
|
5
|
+
# Author:: Glenn Rempe (mailto:glenn@elasticworkbench.com)
|
6
|
+
# Copyright:: Copyright (c) 2007 Glenn Rempe
|
7
|
+
# License:: Distributes under the same terms as Ruby
|
8
|
+
# Home:: http://amazon-ec2.rubyforge.org
|
9
|
+
#++
|
2
10
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
11
|
+
%w[ test/unit rubygems test/spec mocha stubba ].each { |f|
|
12
|
+
begin
|
13
|
+
require f
|
14
|
+
rescue LoadError
|
15
|
+
abort "Unable to load required gem for test: #{f}"
|
16
|
+
end
|
17
|
+
}
|
10
18
|
|
11
19
|
require File.dirname(__FILE__) + '/../lib/EC2'
|
12
20
|
|
data/website/index.html
CHANGED
@@ -33,45 +33,103 @@
|
|
33
33
|
<h1>Amazon Web Services EC2 Ruby Gem</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/amazon-ec2"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/amazon-ec2" class="numbers">0.
|
36
|
+
<a href="http://rubyforge.org/projects/amazon-ec2" class="numbers">0.2.0</a>
|
37
37
|
</div>
|
38
|
-
<
|
38
|
+
<h2>→ ‘amazon-ec2’</h2>
|
39
39
|
|
40
40
|
|
41
41
|
<h2>About</h2>
|
42
42
|
|
43
43
|
|
44
|
-
<p>Amazon Web Services offers a compute power on demand capability known as the Elastic Compute Cloud (EC2).
|
44
|
+
<p>Amazon Web Services offers a compute power on demand capability known as the Elastic Compute Cloud (EC2). The server resources in the cloud can be provisioned on demand by making <span class="caps">HTTP</span> Query <span class="caps">API</span> calls to <span class="caps">EC2</span>.</p>
|
45
45
|
|
46
46
|
|
47
|
-
<p>This
|
47
|
+
<p>This ‘amazon-ec2’ Ruby Gem is an interface library that can be used to interact with the Amazon <span class="caps">EC2</span> system and control server resources on demand from your Ruby scripts, or from Ruby on Rails applications.</p>
|
48
48
|
|
49
49
|
|
50
|
-
<h2>Important
|
50
|
+
<h2>Important! : Are you upgrading from an earlier release?</h2>
|
51
51
|
|
52
52
|
|
53
|
-
<p>
|
53
|
+
<p>This latest release of the gem has undergone a pretty massive re-write. It is no longer compatible with code you may have written that made use of an older version of ‘amazon-ec2’ (< 0.2.0).</p>
|
54
54
|
|
55
55
|
|
56
|
-
<p>
|
56
|
+
<p>Any other gems which depended on ‘amazon-ec2’ < 0.2.0 will also no longer work with the new release. The only project I am aware of that depended on the earlier version of ‘amazon-ec2’ was ‘Capazon’. This project, which allows you to control <span class="caps">EC2</span> from your Capistrano recipies, has now been deprecated and is being reborn as ‘Capsize’. I rewrote the code for Capsize with the assistance of the original creator of Capazon; Jesse Newland. Feel free to check out <a href="http://capsize.rubyforge.org/">Capsize</a> to learn more about this exciting new release!</p>
|
57
57
|
|
58
58
|
|
59
|
-
<p>
|
59
|
+
<p>While we apologize for not being able to maintain backward compatibility with the original ‘amazon-ec2’ gem, there were just too many major enhancements that needed to be made and it just was’nt possible. I hope that the new robustness and new features in this new release make any transitional pain worthwhile! Of course the old versions are still out there on RubyForge, and you can still install them if you pass the version string into the gem install command. I just don’t recommend it unless you really have to.</p>
|
60
60
|
|
61
61
|
|
62
|
-
<h2>
|
62
|
+
<h2>What’s new?</h2>
|
63
63
|
|
64
64
|
|
65
|
-
<p>This
|
65
|
+
<p>This release (>= 0.2.0) represents a major re-write of this gem and contains contributions from several people who make use of this gem for their own applications. Trust us, its worth the pain of upgrading if you have been using an older version. Some of the major enhancements you will find are:</p>
|
66
|
+
|
67
|
+
|
68
|
+
<ul>
|
69
|
+
<li><span class="caps">MAJOR</span> library changes : <span class="caps">THESE CHANGES ARE NOT BACKWARD COMPATIBLE</span>!! You will need to update
|
70
|
+
the way in which you make calls, handle responses, and rescue exceptions from this library.
|
71
|
+
If you prefer not to make these changes you can feel free to continue to use the older version
|
72
|
+
of the gem. These older versions however will no longer be maintained.</li>
|
73
|
+
</ul>
|
74
|
+
|
75
|
+
|
76
|
+
<ul>
|
77
|
+
<li><span class="caps">MAJOR</span> refactoring of how methods calls are made. Now all methods are called with a simple hash
|
78
|
+
of arguments and none of them are positional. This feels much more “Ruby’ish”.</li>
|
79
|
+
</ul>
|
80
|
+
|
81
|
+
|
82
|
+
<ul>
|
83
|
+
<li><span class="caps">MAJOR</span> refactoring of how responses are returned to users. No longer do you have to call the
|
84
|
+
.parse method, and no longer are you getting back simple arrays of information. Responses
|
85
|
+
now come in the form of OpenStruct objects that contain all of the data for an object in
|
86
|
+
Enumerable form so you can use iterators (.each, .each_pair, etc). All methods return an <span class="caps">EC2</span>::Response object
|
87
|
+
which inherits from OpenStruct. The return data from <span class="caps">EC2</span>, which is in <span class="caps">XML</span> form, is parsed
|
88
|
+
with XmlSimple and is used to directly construct the return data structure. This allows us
|
89
|
+
to know with some confidence that the data structure returned from <span class="caps">AWS</span> will always be consistent
|
90
|
+
with this library’s responses. There is also an .xml attribute for each response object that lets you
|
91
|
+
see the full and complete <span class="caps">XML</span> response from <span class="caps">AWS</span> if that is useful to you.</li>
|
92
|
+
</ul>
|
93
|
+
|
66
94
|
|
95
|
+
<ul>
|
96
|
+
<li>Added an exception framework which will now throw appropriate Ruby exceptions
|
97
|
+
that match those handed to us by Amazon <span class="caps">EC2</span>. ArgumentError exceptions will also
|
98
|
+
be thrown if you are making calls we know to be illegal or malformed. You should rescue
|
99
|
+
these exceptions in your application instead of parsing text responses. All exceptions
|
100
|
+
descend from <span class="caps">EC2</span>::Error. You can see them all in exceptions.rb in the gem install.</li>
|
101
|
+
</ul>
|
67
102
|
|
68
|
-
<h2>Documentation</h2>
|
69
103
|
|
104
|
+
<ul>
|
105
|
+
<li>Added a full suite of test/spec unit tests which currently cover 100% of the public methods
|
106
|
+
in this library. We have abot 92% code coverage according to rcov. This has greatly enhanced
|
107
|
+
the reliability of the library as well as our confidence in the code.
|
108
|
+
We used to have 0% test coverage. :-/</li>
|
109
|
+
</ul>
|
70
110
|
|
71
|
-
<p>You can browse the complete RDoc generated documentation at:</p>
|
72
111
|
|
112
|
+
<ul>
|
113
|
+
<li>Added an <span class="caps">EC2</span> command shell : ‘ec2sh’ which can be called from anywhere and gives you
|
114
|
+
an interactive irb session with an <span class="caps">EC2</span> connection pre-made for you as @ec2. You can use this
|
115
|
+
to interactively execute any command on <span class="caps">EC2</span> that this library supports. Try @ec2.methods.sort
|
116
|
+
or @ec2.describe_images to test it out. You must first setup two shell environment variables
|
117
|
+
which contain your <span class="caps">ACCESS</span>_KEY_ID and <span class="caps">SECRET</span>_ACCESS_KEY for this to work. Otherwise an error
|
118
|
+
will be thrown when you try to start it. This is way cool and shamelessly borrowed from
|
119
|
+
Marcel Molina’s fine <span class="caps">AWS</span>::S3 library.</li>
|
120
|
+
</ul>
|
73
121
|
|
74
|
-
|
122
|
+
|
123
|
+
<ul>
|
124
|
+
<li>Updated <span class="caps">API</span> version in the query <span class="caps">API</span> request to 2007-01-19, and added all known method calls
|
125
|
+
in this version of the <span class="caps">API</span> to the gem (including reboot, viewing console output, <span class="caps">NAT</span> addressing
|
126
|
+
and more!)</li>
|
127
|
+
</ul>
|
128
|
+
|
129
|
+
|
130
|
+
<ul>
|
131
|
+
<li>Removed .parse method as it is no longer needed or wanted.</li>
|
132
|
+
</ul>
|
75
133
|
|
76
134
|
|
77
135
|
<h2>Installing</h2>
|
@@ -83,147 +141,378 @@
|
|
83
141
|
<h3>Installation pre-requisites</h3>
|
84
142
|
|
85
143
|
|
86
|
-
<
|
144
|
+
<h4><a href="https://aws-portal.amazon.com/gp/aws/developer/registration/index.html">Amazon Web Services developer account</a>.</h4>
|
145
|
+
|
146
|
+
|
147
|
+
<p>You’ll need an account with <span class="caps">AWS</span> in order to use this gem at all. That should be your first stop on this tour. Your account must also be enabled for Amazon <span class="caps">EC2</span> usage. After signup you’ll be provided with an ‘Access Key ID’ and a ‘Secret Access Key’. These allow you to authenticate any <span class="caps">API</span> calls you make and ensure correct billing to you for usage of their service. Take note of these keys (and keep them safe and secret!).</p>
|
148
|
+
|
149
|
+
|
150
|
+
<h4>Gem Dependencies</h4>
|
151
|
+
|
152
|
+
|
153
|
+
<p>The following gems should be installed automatically as part of your install of amazon-ec2. Most of them are testing or build dependencies but they should be painless to install even if you don’t plan on running the tests or building this gem manually on your own.</p>
|
154
|
+
|
155
|
+
|
156
|
+
<p><a href="http://xml-simple.rubyforge.org/">XmlSimple</a> (required)</p>
|
157
|
+
|
158
|
+
|
159
|
+
<p><a href="http://mocha.rubyforge.org/">Mocha</a> (optional for testing)</p>
|
160
|
+
|
161
|
+
|
162
|
+
<p><a href="http://eigenclass.org/hiki.rb?rcov">Rcov</a> (optional for testing)</p>
|
163
|
+
|
164
|
+
|
165
|
+
<p><a href="http://test-spec.rubyforge.org/test-spec/">Test-Spec</a> (optional for testing)</p>
|
166
|
+
|
167
|
+
|
168
|
+
<p><a href="http://syntax.rubyforge.org/">Syntax</a> (optional for building your own copy of the gem and its docs)</p>
|
169
|
+
|
170
|
+
|
171
|
+
<p><a href="http://whytheluckystiff.net/ruby/redcloth">RedCloth</a> (optional for building your own copy of the gem and its docs)</p>
|
87
172
|
|
88
173
|
|
89
174
|
<h3>Installing the gem</h3>
|
90
175
|
|
91
176
|
|
92
|
-
<p>Linux / <span class="caps">OS X</span> : <pre syntax="ruby">sudo gem install amazon-ec2</pre></p>
|
177
|
+
<p>Linux / <span class="caps">OS X</span> : <pre syntax="ruby">sudo gem install amazon-ec2 --include-dependencies</pre></p>
|
93
178
|
|
94
179
|
|
95
|
-
<p>Microsoft Windows : <pre syntax="ruby">gem install amazon-ec2</pre></p>
|
180
|
+
<p>Microsoft Windows : <pre syntax="ruby">gem install amazon-ec2 --include-dependencies</pre></p>
|
96
181
|
|
97
182
|
|
98
|
-
<h2>
|
183
|
+
<h2>Using the library</h2>
|
99
184
|
|
100
185
|
|
101
|
-
<
|
186
|
+
<h3>Setting up…</h3>
|
102
187
|
|
103
188
|
|
104
|
-
<p>
|
189
|
+
<p>The ‘ec2sh’ and ‘ec2-gem-example.rb’ scripts which will be introduced to you shortly expect your <span class="caps">AWS EC2</span> credentials to
|
190
|
+
be stored as shell environment variables which are accessible to those scripts. This makes them convenient to use whenever
|
191
|
+
you need to do a quick query to see what images you have available to you, whats running now, or to start or stop an
|
192
|
+
instance on <span class="caps">EC2</span>. You’ll find ‘ec2sh’ to be a very handy tool. I’ll describe only the <span class="caps">OS X</span> route for setting up (of course
|
193
|
+
the setup steps will vary depending on your particular system and preferred shell). If you don’t want to
|
194
|
+
do it this way, feel free to copy these scripts from the gem dir to any location where you can run them from and modify them directly to include your credentials.</p>
|
105
195
|
|
106
196
|
|
107
|
-
<
|
197
|
+
<h4><span class="caps">OS X</span> Setup</h4>
|
108
198
|
|
109
199
|
|
110
|
-
<
|
200
|
+
<p>Edit the file ~/.bash_login and add the following to the existing contents:</p>
|
111
201
|
|
112
202
|
|
113
203
|
<pre syntax="ruby">
|
114
|
-
#!/usr/bin/env ruby
|
115
204
|
|
116
|
-
|
117
|
-
require 'ec2'
|
205
|
+
export RUBYOPT="rubygems"
|
118
206
|
|
119
|
-
|
120
|
-
|
207
|
+
# For amazon-ec2 and amazon s3 ruby gems
|
208
|
+
export AMAZON_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID"
|
209
|
+
export AMAZON_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY_ID"
|
121
210
|
|
122
|
-
|
211
|
+
</pre>
|
212
|
+
|
213
|
+
<p>Once you save the file you should close and re-open your command terminal so the new variables are made available. You’ll need to do this close/re-open step for each terminal window you have open (or issue the ‘source ~/.bash_login’ command in each). Make sure that this file is only readable by your user so you don’t inadvertantly expose your credentials to other users on your system.</p>
|
214
|
+
|
215
|
+
|
216
|
+
<p>You can verify that this setup is complete by running the ‘set’ in a command window and seeing that your credentials are in the list of shell variables.</p>
|
217
|
+
|
218
|
+
|
219
|
+
<h3>The basics…</h3>
|
220
|
+
|
221
|
+
|
222
|
+
<p>The library exposes one main interface module <pre syntax="ruby">EC2::Base</pre></p>
|
123
223
|
|
124
|
-
|
125
|
-
|
224
|
+
|
225
|
+
<p>This method requires arguments which include your <span class="caps">AWS</span> credentials and it will return an object that you can use to make
|
226
|
+
method calls directly against <span class="caps">EC2</span>. All the operations for using the <span class="caps">EC2</span> service, including query string header signing,
|
227
|
+
are handled automatically for you. The connection string will look something like this:</p>
|
228
|
+
|
229
|
+
|
230
|
+
<pre syntax="ruby">
|
231
|
+
@ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
|
126
232
|
</pre>
|
127
233
|
|
128
|
-
<p>
|
234
|
+
<p>We have tried to keep the public methods on ‘amazon-ec2’ as close as possible to the <span class="caps">AWS EC2</span> Query <span class="caps">API</span>.
|
235
|
+
This similarity allows you to reference the Query <span class="caps">API</span> Reference in the <a href="http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84"><span class="caps">EC2</span> Developer Guide</a> and be able to get started right away.
|
236
|
+
In most cases the methods names only differ in how they are presented. e.g. ‘DescribeImages’ becomes ’#describe_images() in Ruby.
|
237
|
+
Feel free to browse the full <a href="http://amazon-ec2.rubyforge.org/rdoc/">RDoc documentation</a> for all classes and methods of ‘amazon-ec2’ if you want more details.</p>
|
238
|
+
|
239
|
+
|
240
|
+
<h3>Examples</h3>
|
241
|
+
|
242
|
+
|
243
|
+
<p>The best way to become familiar with ‘amazon-ec2’ is to take it for a test drive. We have provided a few simple ways to get you started. There is also some sample code below that should help out in using ‘amazon-ec2’ with a plain Ruby script, or as part of a Ruby on Rails application.</p>
|
244
|
+
|
245
|
+
|
246
|
+
<h4>Using the ‘ec2-gem-example.rb’ sample test script</h4>
|
247
|
+
|
248
|
+
|
249
|
+
<p>An example Ruby script which exercises the library a bit more is installed for you to check out when you install this gem. You can run this script to verify that everything is setup and working correctly in your environment. Consult the file which is installed at :</p>
|
129
250
|
|
130
251
|
|
131
252
|
<pre syntax="ruby">[your amazon-ec2 gem dir]/examples/ec2-example.rb</pre>
|
132
253
|
|
133
|
-
<
|
254
|
+
<p>Since we also package this sample file in the gem’s bin/ dir you should also be able to run it from anywhere on your shell path (once you have set your environment variables as described above).</p>
|
134
255
|
|
135
256
|
|
136
|
-
<
|
257
|
+
<h4>Using the ‘ec2sh’ command shell</h4>
|
258
|
+
|
259
|
+
|
260
|
+
<p>The ‘ec2sh’ command shell is actually a standard ‘irb’ Ruby shell, with the main difference being we read your <span class="caps">AWS</span> credentials from your environment and pre-configure a connection string for you. This lets you run any <span class="caps">EC2</span> command very simply. This has proven to be a valuable tool during the development of this gem and you should try it out. Since we install this tool in your system path as part of the installation of this gem, you should be able to simply run ‘ec2sh’ from any terminal command prompt on your local system. You’ll see some basic instructions for use, and a few examples when you start ‘ec2sh’. Go ahead and try it out now. We’ll wait…</p>
|
261
|
+
|
262
|
+
|
263
|
+
<p>If your not in front of a terminal shell now (perhaps you’re browsing this site on your iPhone) this is what you would see:</p>
|
137
264
|
|
138
265
|
|
139
266
|
<pre syntax="ruby">
|
140
|
-
|
141
|
-
|
267
|
+
|
268
|
+
hostname:/tmp/rails/amazon_test glenn$ ec2sh
|
269
|
+
|
270
|
+
'ec2sh' usage :
|
271
|
+
This is an interactive 'irb' command shell that allows you to use all
|
272
|
+
commands available to the amazon-ec2 gem. You'll find this to be a
|
273
|
+
great tool to help you debug issues and practice running commands
|
274
|
+
against the live EC2 servers prior to putting them in your code.
|
275
|
+
|
276
|
+
The EC2 connection is wired to the class instance '@ec2'. Make method calls
|
277
|
+
on this to execute commands on EC2. Adding a #to_s
|
278
|
+
at the end of any command should give you a full String representation of the
|
279
|
+
response. The #xml data is available for each response
|
280
|
+
which allows you to view the full and complete XML response returned by
|
281
|
+
EC2 without any parsing applied. This is useful for viewing the
|
282
|
+
hierarchy of an entire response in a friendly way (if XML is friendly
|
283
|
+
to you!). Understanding the hierarchy of the XML response is critical
|
284
|
+
to making effective use of this library.
|
285
|
+
|
286
|
+
Examples to try:
|
287
|
+
|
288
|
+
returns : all ec2 public methods
|
289
|
+
>> @ec2.methods.sort
|
290
|
+
|
291
|
+
returns : a string representation of ALL images
|
292
|
+
>> @ec2.describe_images.to_s
|
293
|
+
|
294
|
+
returns : an Array of EC2::Response objects, each an EC2 image and its data
|
295
|
+
>> @ec2.describe_images.imagesSet.item
|
296
|
+
>> @ec2.describe_images.imagesSet.item[0] (an OpenStruct of a single item in that array)
|
297
|
+
>> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that OpenStruct item)
|
298
|
+
|
299
|
+
returns : an XML representation of all images
|
300
|
+
>> puts @ec2.describe_images.xml
|
301
|
+
|
302
|
+
returns : an XML representation of all images owned by Amazon
|
303
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').xml
|
304
|
+
|
305
|
+
>> @ec2.describe_images.imagesSet.item[0].to_s
|
306
|
+
=> "#<EC2::Response:0x100A465B4 imageId=\"ami-018e6b68\" imageLocation=\"rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\" imageOwnerId=\"099034111737\" imageState=\"available\" isPublic=\"true\" parent=#<EC2::Response:0x100A469A6 ...>>"
|
307
|
+
|
142
308
|
</pre>
|
143
309
|
|
144
|
-
in
|
310
|
+
<h4>Using ‘amazon-ec2’ in Ruby scripts</h4>
|
311
|
+
|
312
|
+
|
313
|
+
<p>Try out the following bit of code. This should walk through each image returned by a call to #describe_images and print out its key data. Note in the example below that you cannot walk through the results of the #describe_images call with the ’.each’ iterator (You’ll get errors if you try). You need to instead walk through the Array of items which are in the ‘imagesSet’ embedded in the response. This reflects exactly the <span class="caps">XML</span> hierarchy of data returned from <span class="caps">EC2</span> which we parse to Ruby OpenStruct objects (EC2::Response).</p>
|
314
|
+
|
315
|
+
|
145
316
|
<pre syntax="ruby">
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
317
|
+
#!/usr/bin/env ruby
|
318
|
+
|
319
|
+
require 'rubygems'
|
320
|
+
require 'ec2'
|
321
|
+
|
322
|
+
ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
|
323
|
+
SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'
|
324
|
+
|
325
|
+
ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
|
326
|
+
|
327
|
+
puts "----- listing images owned by 'amazon' -----"
|
328
|
+
ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
|
329
|
+
# OpenStruct objects have members!
|
330
|
+
image.members.each do |member|
|
331
|
+
puts "#{member} => #{image[member]}"
|
332
|
+
end
|
333
|
+
end
|
158
334
|
</pre>
|
159
335
|
|
160
|
-
<
|
336
|
+
<h4>Using ‘amazon-ec2’ in Ruby on Rails applications</h4>
|
161
337
|
|
162
338
|
|
163
|
-
<p>
|
339
|
+
<p>in config/environment.rb:</p>
|
164
340
|
|
165
341
|
|
166
342
|
<pre syntax="ruby">
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
...
|
343
|
+
# Require the amazon-ec2 gem and make its methods available in your Rails app
|
344
|
+
# Put this at the bottom of your environment.rb
|
345
|
+
require 'EC2'
|
171
346
|
</pre>
|
172
347
|
|
173
|
-
|
348
|
+
in app/controllers/your_controller.rb:
|
349
|
+
<pre syntax="ruby">
|
174
350
|
|
351
|
+
[some controller code ...]
|
352
|
+
|
353
|
+
ec2 = EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")
|
354
|
+
|
355
|
+
# get ALL public images
|
356
|
+
@ec2_images = ec2.describe_images().imagesSet.item
|
357
|
+
|
358
|
+
# Get info on all public EC2 images created by the Amazon EC2 team.
|
359
|
+
@ec2_images_amazon = ec2.describe_images(:owner_id => "amazon").imagesSet.item
|
360
|
+
|
361
|
+
[some more controller code ...]
|
175
362
|
|
176
|
-
<pre syntax="ruby">
|
177
|
-
...
|
178
|
-
<% @ec2_images.each do |image| %>
|
179
|
-
<% image.each_with_index do |value, index| %>
|
180
|
-
<%= "#{index} => #{value}" %><br />
|
181
|
-
<% end %>
|
182
|
-
<% end %>
|
183
|
-
...
|
184
363
|
</pre>
|
185
364
|
|
186
|
-
<p>
|
365
|
+
<p>and then you can show off your <span class="caps">EC2</span> image data with some code in app/views/your_view.rhtml:</p>
|
187
366
|
|
188
367
|
|
189
368
|
<pre syntax="ruby">
|
190
|
-
|
191
|
-
<
|
369
|
+
|
370
|
+
<h1>EC2 Test#index</h1>
|
371
|
+
|
372
|
+
<h1>Sample 1 - debug() view</h1>
|
373
|
+
|
374
|
+
<%= debug(@ec2_images_amazon) %>
|
375
|
+
|
376
|
+
<h1>Sample 2 - Build a table</h1>
|
377
|
+
|
378
|
+
<table border='1'>
|
192
379
|
<tr>
|
193
|
-
<th>
|
194
|
-
<th>
|
195
|
-
<th>
|
196
|
-
<th>
|
197
|
-
<th>
|
380
|
+
<th>image.imageId</th>
|
381
|
+
<th>image.imageLocation</th>
|
382
|
+
<th>image.imageOwnerId</th>
|
383
|
+
<th>image.imageState</th>
|
384
|
+
<th>image.isPublic</th>
|
198
385
|
</tr>
|
199
386
|
|
200
|
-
<% for
|
387
|
+
<% for image in @ec2_images_amazon %>
|
201
388
|
<tr>
|
202
|
-
<td><%=h
|
203
|
-
<td><%=h
|
204
|
-
<td><%=h
|
205
|
-
<td><%=h
|
206
|
-
<td><%=h
|
389
|
+
<td><%=h image.imageId %></td>
|
390
|
+
<td><%=h image.imageLocation %></td>
|
391
|
+
<td><%=h image.imageOwnerId %></td>
|
392
|
+
<td><%=h image.imageState %></td>
|
393
|
+
<td><%=h image.isPublic %></td>
|
207
394
|
</tr>
|
208
395
|
<% end %>
|
209
396
|
</table>
|
210
|
-
|
397
|
+
|
398
|
+
<h1>Sample 3 - Iterate</h1>
|
399
|
+
|
400
|
+
<% @ec2_images_amazon.each do |image| %>
|
401
|
+
<% image.each_pair do |key, value| %>
|
402
|
+
<% unless key == 'parent' %>
|
403
|
+
<%= "#{key} => #{value}" %><br />
|
404
|
+
<% end %>
|
405
|
+
<% end %>
|
406
|
+
<br />
|
407
|
+
<% end %>
|
408
|
+
|
409
|
+
</pre>
|
410
|
+
|
411
|
+
<h4>Important notes regarding the structure of <span class="caps">EC2</span>::Response Objects</h4>
|
412
|
+
|
413
|
+
|
414
|
+
<p>One of the key benefits of this new version of the library is that all responses from <span class="caps">EC2</span> are bundled up in
|
415
|
+
a real data structure and no longer require parsing of text. We use an OpenStruct as the parent for the <span class="caps">EC2</span>::Response
|
416
|
+
object and we populate it directly from the <span class="caps">XML</span> given to us by <span class="caps">EC2</span> in response to any command we issue. This means that
|
417
|
+
future changes to the <span class="caps">API</span> and what is returned by <span class="caps">EC2</span> will largely be handled transparently by the gem. This is a huge
|
418
|
+
benefit. What this means though, is that you may have to do a little homework on what actually gets returned by <span class="caps">EC2</span> as <span class="caps">XML</span>.
|
419
|
+
For example, when you make a #describe_images call in ec2sh to <span class="caps">EC2</span> what you will get back will look like:</p>
|
420
|
+
|
421
|
+
|
422
|
+
<pre syntax="ruby">
|
423
|
+
$ ec2sh
|
424
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').xml
|
425
|
+
|
426
|
+
<?xml version="1.0"?>
|
427
|
+
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2007-01-19/">
|
428
|
+
<imagesSet>
|
429
|
+
<item>
|
430
|
+
<imageId>ami-20b65349</imageId>
|
431
|
+
<imageLocation>ec2-public-images/fedora-core4-base.manifest.xml</imageLocation>
|
432
|
+
<imageState>available</imageState>
|
433
|
+
<imageOwnerId>amazon</imageOwnerId>
|
434
|
+
<isPublic>true</isPublic>
|
435
|
+
</item>
|
436
|
+
<item>
|
437
|
+
<imageId>ami-22b6534b</imageId>
|
438
|
+
<imageLocation>ec2-public-images/fedora-core4-mysql.manifest.xml</imageLocation>
|
439
|
+
<imageState>available</imageState>
|
440
|
+
<imageOwnerId>amazon</imageOwnerId>
|
441
|
+
<isPublic>true</isPublic>
|
442
|
+
</item>
|
443
|
+
<item>
|
444
|
+
<imageId>ami-23b6534a</imageId>
|
445
|
+
<imageLocation>ec2-public-images/fedora-core4-apache.manifest.xml</imageLocation>
|
446
|
+
<imageState>available</imageState>
|
447
|
+
<imageOwnerId>amazon</imageOwnerId>
|
448
|
+
<isPublic>true</isPublic>
|
449
|
+
</item>
|
450
|
+
<item>
|
451
|
+
<imageId>ami-25b6534c</imageId>
|
452
|
+
<imageLocation>ec2-public-images/fedora-core4-apache-mysql.manifest.xml</imageLocation>
|
453
|
+
<imageState>available</imageState>
|
454
|
+
<imageOwnerId>amazon</imageOwnerId>
|
455
|
+
<isPublic>true</isPublic>
|
456
|
+
</item>
|
457
|
+
<item>
|
458
|
+
<imageId>ami-26b6534f</imageId>
|
459
|
+
<imageLocation>ec2-public-images/developer-image.manifest.xml</imageLocation>
|
460
|
+
<imageState>available</imageState>
|
461
|
+
<imageOwnerId>amazon</imageOwnerId>
|
462
|
+
<isPublic>true</isPublic>
|
463
|
+
</item>
|
464
|
+
<item>
|
465
|
+
<imageId>ami-2bb65342</imageId>
|
466
|
+
<imageLocation>ec2-public-images/getting-started.manifest.xml</imageLocation>
|
467
|
+
<imageState>available</imageState>
|
468
|
+
<imageOwnerId>amazon</imageOwnerId>
|
469
|
+
<isPublic>true</isPublic>
|
470
|
+
</item>
|
471
|
+
</imagesSet>
|
472
|
+
</DescribeImagesResponse>
|
473
|
+
|
474
|
+
</pre>
|
475
|
+
|
476
|
+
<p>You can see in the <span class="caps">XML</span> the structure that you will need to follow when constructing queries for information and parsing responses from <span class="caps">EC2</span>.</p>
|
477
|
+
|
478
|
+
|
479
|
+
<p>So, for example, if you wanted to get the image ID of the third image listed in the response above you would need to do:</p>
|
480
|
+
|
481
|
+
|
482
|
+
<pre syntax="ruby">
|
483
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').imagesSet.item[2].imageId
|
484
|
+
ami-23b6534a
|
211
485
|
</pre>
|
212
486
|
|
213
|
-
<
|
487
|
+
<p><span class="caps">EC2</span> will typically return ‘sets’ of things (imagesSet, reservationSet, etc.) which we map to ruby Arrays (.imagesSet.item in the example above). If you want to iterate over a response set you will need to iterate over this array. The Arrays will typically contain additional <span class="caps">EC2</span>::Response objects that represent each individual item. You’ll find that you can use the ‘ec2sh’ to help you understand the structure more completely if you try issuing commands there as a way to practice seeing what will be returned and making sure you get exactly what you want. You can always call the <span class="caps">EC2</span>::Response#xml method like I did above to see the exact <span class="caps">XML</span> returned which allows you to easily derive the structure for the Ruby OpenStruct object.</p>
|
488
|
+
|
489
|
+
|
490
|
+
<h2>Contributing</h2>
|
491
|
+
|
492
|
+
|
493
|
+
<p>We can always use your help! Do you have Ruby skills? Do you see a bug or enhancement that you’d like to see fixed? We would love to have your patches for documentation, test cases, or enhancements.</p>
|
494
|
+
|
495
|
+
|
496
|
+
<h2>Project Info</h2>
|
497
|
+
|
498
|
+
|
499
|
+
<p>This project is managed as a RubyForge project which you can find at <a href="http://amazon-ec2.rubyforge.org/">http://amazon-ec2.rubyforge.org/</a> and this is always the best place to find the latest news, report any bugs, submit feature requests, or provide patches.</p>
|
214
500
|
|
215
501
|
|
216
|
-
<
|
502
|
+
<h2>Learning More</h2>
|
217
503
|
|
218
504
|
|
219
|
-
<
|
220
|
-
<a href="http://amazon-ec2.rubyforge.org/">http://amazon-ec2.rubyforge.org/</a></p>
|
505
|
+
<h3>Documentation</h3>
|
221
506
|
|
222
507
|
|
223
|
-
<
|
508
|
+
<p>Complete RDoc generated documentation can be found at <a href="http://amazon-ec2.rubyforge.org/rdoc/">http://amazon-ec2.rubyforge.org/rdoc/</a></p>
|
224
509
|
|
225
510
|
|
226
|
-
<
|
511
|
+
<h3>Websites</h3>
|
512
|
+
|
513
|
+
|
514
|
+
<p><a href="http://aws.amazon.com/">Amazon Web Services Home</a>
|
515
|
+
<a href="http://rubyforge.org/projects/amazon-ec2/">Project Home</a>
|
227
516
|
<a href="http://rubyforge.org/frs/?group_id=2753">Downloads</a>
|
228
517
|
<a href="http://rubyforge.org/scm/?group_id=2753">Browse Code</a>
|
229
518
|
<a href="http://rubyforge.org/tracker/?group_id=2753">Report Bugs</a>
|
@@ -234,13 +523,16 @@ conn = EC2::AWSAuthConnection.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
|
234
523
|
<h3>Related Projects</h3>
|
235
524
|
|
236
525
|
|
237
|
-
<p><a href="http://
|
526
|
+
<p><a href="http://capsize.rubyforge.org/">Capsize</a> : A Capistrano (>= 2.x) plugin that allows complete control of Amazon <span class="caps">EC2</span> from Capistrano recipes.</p>
|
238
527
|
|
239
528
|
|
240
529
|
<h2>Credits</h2>
|
241
530
|
|
242
531
|
|
243
|
-
<p>The original sample code for this library was provided by Amazon Web Services, <span class="caps">LLC</span>. Thanks to them for providing all of us with samples that got this started.</p>
|
532
|
+
<p>The original sample code for this library was provided by Amazon Web Services, <span class="caps">LLC</span>. Thanks to them for providing all of us with samples that got this started. This latest version of amazon-ec2 doesn’t much resemble the original. They got us going though and thanks to the <span class="caps">EC2</span> team for including Ruby in their plans. We hope to see more <span class="caps">AWS</span> Ruby code.</p>
|
533
|
+
|
534
|
+
|
535
|
+
<p>Thanks to all the great folks who submitted patches and kept this project rolling. I would especially like to thank Sean Knapp, Kevin Clark, and Randy Bias. Your patches and help are much appreciated.</p>
|
244
536
|
|
245
537
|
|
246
538
|
<p>Thanks to Dr. Nic Williams and his great ‘NewGem’ Ruby Gem Generator. This gem of a Gem helped me package up this code for distribution in a flash! You can find Dr. Nic’s NewGem generator at <a href="http://newgem.rubyforge.org/">http://newgem.rubyforge.org/</a></p>
|
@@ -251,7 +543,7 @@ conn = EC2::AWSAuthConnection.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
|
251
543
|
|
252
544
|
<p>Comments, patches, and bug reports are welcome. Send an email to the address below or use the RubyForge forum for this project.</p>
|
253
545
|
<p class="coda">
|
254
|
-
<a href="mailto:grempe-at-rubyforge-dot-org">Glenn Rempe</a>,
|
546
|
+
<a href="mailto:grempe-at-rubyforge-dot-org">Glenn Rempe</a>, 3rd July 2007<br>
|
255
547
|
</p>
|
256
548
|
</div>
|
257
549
|
|