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/website/index.txt
CHANGED
@@ -1,30 +1,70 @@
|
|
1
|
-
|
1
|
+
h2. Amazon Web Services EC2 Ruby Gem
|
2
2
|
|
3
|
-
|
3
|
+
h2. → 'amazon-ec2'
|
4
4
|
|
5
5
|
h2. About
|
6
6
|
|
7
|
-
Amazon Web Services offers a compute power on demand capability known as the Elastic Compute Cloud (EC2).
|
7
|
+
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 HTTP Query API calls to EC2.
|
8
8
|
|
9
|
-
This
|
9
|
+
This 'amazon-ec2' Ruby Gem is an interface library that can be used to interact with the Amazon EC2 system and control server resources on demand from your Ruby scripts, or from Ruby on Rails applications.
|
10
10
|
|
11
|
-
h2. Important Note
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
All I ask in return is if you have some Ruby skills and can contribute documentation, automated unit tests, bug fixes, or enhancements as Subversion patch files that you please do so. I will be happy to incorporate those changes directly into the library as appropriate.
|
16
|
-
|
17
|
-
Alternatively, if you are interested in becoming a more seriously involved contributing developer with full check-in privileges on the source code please feel free to contact me and we can discuss the best way to do so.
|
12
|
+
h2. Important! : Are you upgrading from an earlier release?
|
18
13
|
|
19
|
-
|
14
|
+
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).
|
20
15
|
|
21
|
-
|
16
|
+
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 EC2 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 "Capsize":http://capsize.rubyforge.org/ to learn more about this exciting new release!
|
17
|
+
|
18
|
+
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.
|
19
|
+
|
20
|
+
|
21
|
+
h2. What's new?
|
22
|
+
|
23
|
+
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:
|
24
|
+
|
25
|
+
* MAJOR library changes : THESE CHANGES ARE NOT BACKWARD COMPATIBLE!! You will need to update
|
26
|
+
the way in which you make calls, handle responses, and rescue exceptions from this library.
|
27
|
+
If you prefer not to make these changes you can feel free to continue to use the older version
|
28
|
+
of the gem. These older versions however will no longer be maintained.
|
29
|
+
|
30
|
+
* MAJOR refactoring of how methods calls are made. Now all methods are called with a simple hash
|
31
|
+
of arguments and none of them are positional. This feels much more "Ruby'ish".
|
32
|
+
|
33
|
+
* MAJOR refactoring of how responses are returned to users. No longer do you have to call the
|
34
|
+
.parse method, and no longer are you getting back simple arrays of information. Responses
|
35
|
+
now come in the form of OpenStruct objects that contain all of the data for an object in
|
36
|
+
Enumerable form so you can use iterators (.each, .each_pair, etc). All methods return an EC2::Response object
|
37
|
+
which inherits from OpenStruct. The return data from EC2, which is in XML form, is parsed
|
38
|
+
with XmlSimple and is used to directly construct the return data structure. This allows us
|
39
|
+
to know with some confidence that the data structure returned from AWS will always be consistent
|
40
|
+
with this library's responses. There is also an .xml attribute for each response object that lets you
|
41
|
+
see the full and complete XML response from AWS if that is useful to you.
|
42
|
+
|
43
|
+
* Added an exception framework which will now throw appropriate Ruby exceptions
|
44
|
+
that match those handed to us by Amazon EC2. ArgumentError exceptions will also
|
45
|
+
be thrown if you are making calls we know to be illegal or malformed. You should rescue
|
46
|
+
these exceptions in your application instead of parsing text responses. All exceptions
|
47
|
+
descend from EC2::Error. You can see them all in exceptions.rb in the gem install.
|
48
|
+
|
49
|
+
* Added a full suite of test/spec unit tests which currently cover 100% of the public methods
|
50
|
+
in this library. We have abot 92% code coverage according to rcov. This has greatly enhanced
|
51
|
+
the reliability of the library as well as our confidence in the code.
|
52
|
+
We used to have 0% test coverage. :-/
|
53
|
+
|
54
|
+
* Added an EC2 command shell : 'ec2sh' which can be called from anywhere and gives you
|
55
|
+
an interactive irb session with an EC2 connection pre-made for you as @ec2. You can use this
|
56
|
+
to interactively execute any command on EC2 that this library supports. Try @ec2.methods.sort
|
57
|
+
or @ec2.describe_images to test it out. You must first setup two shell environment variables
|
58
|
+
which contain your ACCESS_KEY_ID and SECRET_ACCESS_KEY for this to work. Otherwise an error
|
59
|
+
will be thrown when you try to start it. This is way cool and shamelessly borrowed from
|
60
|
+
Marcel Molina's fine AWS::S3 library.
|
22
61
|
|
23
|
-
|
62
|
+
* Updated API version in the query API request to 2007-01-19, and added all known method calls
|
63
|
+
in this version of the API to the gem (including reboot, viewing console output, NAT addressing
|
64
|
+
and more!)
|
24
65
|
|
25
|
-
|
66
|
+
* Removed .parse method as it is no longer needed or wanted.
|
26
67
|
|
27
|
-
"http://amazon-ec2.rubyforge.org/rdoc/":http://amazon-ec2.rubyforge.org/rdoc/
|
28
68
|
|
29
69
|
|
30
70
|
h2. Installing
|
@@ -33,24 +73,149 @@ This gem follows the standard conventions for installation on any system with Ru
|
|
33
73
|
|
34
74
|
h3. Installation pre-requisites
|
35
75
|
|
36
|
-
|
76
|
+
h4. "Amazon Web Services developer account":https://aws-portal.amazon.com/gp/aws/developer/registration/index.html.
|
77
|
+
|
78
|
+
You'll need an account with AWS 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 EC2 usage. After signup you'll be provided with an 'Access Key ID' and a 'Secret Access Key'. These allow you to authenticate any API 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!).
|
79
|
+
|
80
|
+
h4. Gem Dependencies
|
81
|
+
|
82
|
+
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.
|
83
|
+
|
84
|
+
"XmlSimple":http://xml-simple.rubyforge.org/ (required)
|
85
|
+
|
86
|
+
"Mocha":http://mocha.rubyforge.org/ (optional for testing)
|
87
|
+
|
88
|
+
"Rcov":http://eigenclass.org/hiki.rb?rcov (optional for testing)
|
89
|
+
|
90
|
+
"Test-Spec":http://test-spec.rubyforge.org/test-spec/ (optional for testing)
|
91
|
+
|
92
|
+
"Syntax":http://syntax.rubyforge.org/ (optional for building your own copy of the gem and its docs)
|
93
|
+
|
94
|
+
"RedCloth":http://whytheluckystiff.net/ruby/redcloth (optional for building your own copy of the gem and its docs)
|
95
|
+
|
37
96
|
|
38
97
|
h3. Installing the gem
|
39
98
|
|
40
|
-
Linux / OS X : <pre syntax="ruby">sudo gem install amazon-ec2</pre>
|
99
|
+
Linux / OS X : <pre syntax="ruby">sudo gem install amazon-ec2 --include-dependencies</pre>
|
100
|
+
|
101
|
+
Microsoft Windows : <pre syntax="ruby">gem install amazon-ec2 --include-dependencies</pre>
|
102
|
+
|
103
|
+
|
104
|
+
h2. Using the library
|
41
105
|
|
42
|
-
|
106
|
+
h3. Setting up...
|
43
107
|
|
108
|
+
The 'ec2sh' and 'ec2-gem-example.rb' scripts which will be introduced to you shortly expect your AWS EC2 credentials to
|
109
|
+
be stored as shell environment variables which are accessible to those scripts. This makes them convenient to use whenever
|
110
|
+
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
|
111
|
+
instance on EC2. You'll find 'ec2sh' to be a very handy tool. I'll describe only the OS X route for setting up (of course
|
112
|
+
the setup steps will vary depending on your particular system and preferred shell). If you don't want to
|
113
|
+
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.
|
44
114
|
|
45
|
-
|
115
|
+
h4. OS X Setup
|
46
116
|
|
47
|
-
|
117
|
+
Edit the file ~/.bash_login and add the following to the existing contents:
|
48
118
|
|
49
|
-
|
119
|
+
<pre syntax="ruby">
|
120
|
+
|
121
|
+
export RUBYOPT="rubygems"
|
122
|
+
|
123
|
+
# For amazon-ec2 and amazon s3 ruby gems
|
124
|
+
export AMAZON_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID"
|
125
|
+
export AMAZON_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY_ID"
|
126
|
+
|
127
|
+
</pre>
|
128
|
+
|
129
|
+
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.
|
130
|
+
|
131
|
+
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.
|
50
132
|
|
51
|
-
|
133
|
+
h3. The basics...
|
52
134
|
|
53
|
-
|
135
|
+
The library exposes one main interface module <pre syntax="ruby">EC2::Base</pre>
|
136
|
+
|
137
|
+
This method requires arguments which include your AWS credentials and it will return an object that you can use to make
|
138
|
+
method calls directly against EC2. All the operations for using the EC2 service, including query string header signing,
|
139
|
+
are handled automatically for you. The connection string will look something like this:
|
140
|
+
|
141
|
+
<pre syntax="ruby">
|
142
|
+
@ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
|
143
|
+
</pre>
|
144
|
+
|
145
|
+
We have tried to keep the public methods on 'amazon-ec2' as close as possible to the AWS EC2 Query API.
|
146
|
+
This similarity allows you to reference the Query API Reference in the "EC2 Developer Guide":http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84 and be able to get started right away.
|
147
|
+
In most cases the methods names only differ in how they are presented. e.g. 'DescribeImages' becomes '#describe_images() in Ruby.
|
148
|
+
Feel free to browse the full "RDoc documentation":http://amazon-ec2.rubyforge.org/rdoc/ for all classes and methods of 'amazon-ec2' if you want more details.
|
149
|
+
|
150
|
+
|
151
|
+
h3. Examples
|
152
|
+
|
153
|
+
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.
|
154
|
+
|
155
|
+
|
156
|
+
h4. Using the 'ec2-gem-example.rb' sample test script
|
157
|
+
|
158
|
+
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 :
|
159
|
+
|
160
|
+
<pre syntax="ruby">[your amazon-ec2 gem dir]/examples/ec2-example.rb</pre>
|
161
|
+
|
162
|
+
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).
|
163
|
+
|
164
|
+
|
165
|
+
h4. Using the 'ec2sh' command shell
|
166
|
+
|
167
|
+
The 'ec2sh' command shell is actually a standard 'irb' Ruby shell, with the main difference being we read your AWS credentials from your environment and pre-configure a connection string for you. This lets you run any EC2 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...
|
168
|
+
|
169
|
+
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:
|
170
|
+
|
171
|
+
<pre syntax="ruby">
|
172
|
+
|
173
|
+
hostname:/tmp/rails/amazon_test glenn$ ec2sh
|
174
|
+
|
175
|
+
'ec2sh' usage :
|
176
|
+
This is an interactive 'irb' command shell that allows you to use all
|
177
|
+
commands available to the amazon-ec2 gem. You'll find this to be a
|
178
|
+
great tool to help you debug issues and practice running commands
|
179
|
+
against the live EC2 servers prior to putting them in your code.
|
180
|
+
|
181
|
+
The EC2 connection is wired to the class instance '@ec2'. Make method calls
|
182
|
+
on this to execute commands on EC2. Adding a #to_s
|
183
|
+
at the end of any command should give you a full String representation of the
|
184
|
+
response. The #xml data is available for each response
|
185
|
+
which allows you to view the full and complete XML response returned by
|
186
|
+
EC2 without any parsing applied. This is useful for viewing the
|
187
|
+
hierarchy of an entire response in a friendly way (if XML is friendly
|
188
|
+
to you!). Understanding the hierarchy of the XML response is critical
|
189
|
+
to making effective use of this library.
|
190
|
+
|
191
|
+
Examples to try:
|
192
|
+
|
193
|
+
returns : all ec2 public methods
|
194
|
+
>> @ec2.methods.sort
|
195
|
+
|
196
|
+
returns : a string representation of ALL images
|
197
|
+
>> @ec2.describe_images.to_s
|
198
|
+
|
199
|
+
returns : an Array of EC2::Response objects, each an EC2 image and its data
|
200
|
+
>> @ec2.describe_images.imagesSet.item
|
201
|
+
>> @ec2.describe_images.imagesSet.item[0] (an OpenStruct of a single item in that array)
|
202
|
+
>> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that OpenStruct item)
|
203
|
+
|
204
|
+
returns : an XML representation of all images
|
205
|
+
>> puts @ec2.describe_images.xml
|
206
|
+
|
207
|
+
returns : an XML representation of all images owned by Amazon
|
208
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').xml
|
209
|
+
|
210
|
+
>> @ec2.describe_images.imagesSet.item[0].to_s
|
211
|
+
=> "#<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 ...>>"
|
212
|
+
|
213
|
+
</pre>
|
214
|
+
|
215
|
+
|
216
|
+
h4. Using 'amazon-ec2' in Ruby scripts
|
217
|
+
|
218
|
+
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 XML hierarchy of data returned from EC2 which we parse to Ruby OpenStruct objects (EC2::Response).
|
54
219
|
|
55
220
|
<pre syntax="ruby">
|
56
221
|
#!/usr/bin/env ruby
|
@@ -58,103 +223,187 @@ h3. Standalone Ruby Script Usage Sample
|
|
58
223
|
require 'rubygems'
|
59
224
|
require 'ec2'
|
60
225
|
|
61
|
-
|
62
|
-
|
226
|
+
ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
|
227
|
+
SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'
|
63
228
|
|
64
|
-
|
229
|
+
ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
|
65
230
|
|
66
|
-
puts "----- listing images -----"
|
67
|
-
|
231
|
+
puts "----- listing images owned by 'amazon' -----"
|
232
|
+
ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
|
233
|
+
# OpenStruct objects have members!
|
234
|
+
image.members.each do |member|
|
235
|
+
puts "#{member} => #{image[member]}"
|
236
|
+
end
|
237
|
+
end
|
68
238
|
</pre>
|
69
239
|
|
70
|
-
An example Ruby script which exercises the library is provided for use as a starting point in the gem installation. Consult the file which is installed at :
|
71
|
-
|
72
|
-
<pre syntax="ruby">[your amazon-ec2 gem dir]/examples/ec2-example.rb</pre>
|
73
240
|
|
74
|
-
|
241
|
+
h4. Using 'amazon-ec2' in Ruby on Rails applications
|
75
242
|
|
76
243
|
in config/environment.rb:
|
77
244
|
|
78
245
|
<pre syntax="ruby">
|
79
|
-
#
|
80
|
-
|
246
|
+
# Require the amazon-ec2 gem and make its methods available in your Rails app
|
247
|
+
# Put this at the bottom of your environment.rb
|
248
|
+
require 'EC2'
|
81
249
|
</pre>
|
82
|
-
|
250
|
+
|
83
251
|
in app/controllers/your_controller.rb:
|
84
252
|
<pre syntax="ruby">
|
85
|
-
...
|
86
|
-
conn = EC2::AWSAuthConnection.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
|
87
253
|
|
88
|
-
|
89
|
-
# values from the API response that you can
|
90
|
-
# use in your view
|
91
|
-
@ec2_images = conn.describe_images().parse
|
92
|
-
|
93
|
-
# Or with some parameters
|
94
|
-
# (in this case specific owner ID's)
|
95
|
-
@ec2_images_mine = ec2.describe_images([],["522821470517"],[]).parse
|
96
|
-
...
|
97
|
-
</pre>
|
254
|
+
[some controller code ...]
|
98
255
|
|
99
|
-
|
256
|
+
ec2 = EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")
|
100
257
|
|
101
|
-
|
258
|
+
# get ALL public images
|
259
|
+
@ec2_images = ec2.describe_images().imagesSet.item
|
102
260
|
|
103
|
-
|
104
|
-
|
105
|
-
<%= debug(@ec2_images) %>
|
106
|
-
<%= debug(@ec2_images_mine) %>
|
107
|
-
...
|
108
|
-
</pre>
|
261
|
+
# Get info on all public EC2 images created by the Amazon EC2 team.
|
262
|
+
@ec2_images_amazon = ec2.describe_images(:owner_id => "amazon").imagesSet.item
|
109
263
|
|
110
|
-
|
264
|
+
[some more controller code ...]
|
111
265
|
|
112
|
-
<pre syntax="ruby">
|
113
|
-
...
|
114
|
-
<% @ec2_images.each do |image| %>
|
115
|
-
<% image.each_with_index do |value, index| %>
|
116
|
-
<%= "#{index} => #{value}" %><br />
|
117
|
-
<% end %>
|
118
|
-
<% end %>
|
119
|
-
...
|
120
266
|
</pre>
|
121
|
-
|
122
|
-
|
123
|
-
|
267
|
+
|
268
|
+
and then you can show off your EC2 image data with some code in app/views/your_view.rhtml:
|
269
|
+
|
124
270
|
<pre syntax="ruby">
|
125
|
-
|
126
|
-
<
|
271
|
+
|
272
|
+
<h1>EC2 Test#index</h1>
|
273
|
+
|
274
|
+
<h1>Sample 1 - debug() view</h1>
|
275
|
+
|
276
|
+
<%= debug(@ec2_images_amazon) %>
|
277
|
+
|
278
|
+
<h1>Sample 2 - Build a table</h1>
|
279
|
+
|
280
|
+
<table border='1'>
|
127
281
|
<tr>
|
128
|
-
<th>
|
129
|
-
<th>
|
130
|
-
<th>
|
131
|
-
<th>
|
132
|
-
<th>
|
282
|
+
<th>image.imageId</th>
|
283
|
+
<th>image.imageLocation</th>
|
284
|
+
<th>image.imageOwnerId</th>
|
285
|
+
<th>image.imageState</th>
|
286
|
+
<th>image.isPublic</th>
|
133
287
|
</tr>
|
134
288
|
|
135
|
-
<% for
|
289
|
+
<% for image in @ec2_images_amazon %>
|
136
290
|
<tr>
|
137
|
-
<td><%=h
|
138
|
-
<td><%=h
|
139
|
-
<td><%=h
|
140
|
-
<td><%=h
|
141
|
-
<td><%=h
|
291
|
+
<td><%=h image.imageId %></td>
|
292
|
+
<td><%=h image.imageLocation %></td>
|
293
|
+
<td><%=h image.imageOwnerId %></td>
|
294
|
+
<td><%=h image.imageState %></td>
|
295
|
+
<td><%=h image.isPublic %></td>
|
142
296
|
</tr>
|
143
297
|
<% end %>
|
144
298
|
</table>
|
145
|
-
|
299
|
+
|
300
|
+
<h1>Sample 3 - Iterate</h1>
|
301
|
+
|
302
|
+
<% @ec2_images_amazon.each do |image| %>
|
303
|
+
<% image.each_pair do |key, value| %>
|
304
|
+
<% unless key == 'parent' %>
|
305
|
+
<%= "#{key} => #{value}" %><br />
|
306
|
+
<% end %>
|
307
|
+
<% end %>
|
308
|
+
<br />
|
309
|
+
<% end %>
|
310
|
+
|
311
|
+
</pre>
|
312
|
+
|
313
|
+
|
314
|
+
h4. Important notes regarding the structure of EC2::Response Objects
|
315
|
+
|
316
|
+
One of the key benefits of this new version of the library is that all responses from EC2 are bundled up in
|
317
|
+
a real data structure and no longer require parsing of text. We use an OpenStruct as the parent for the EC2::Response
|
318
|
+
object and we populate it directly from the XML given to us by EC2 in response to any command we issue. This means that
|
319
|
+
future changes to the API and what is returned by EC2 will largely be handled transparently by the gem. This is a huge
|
320
|
+
benefit. What this means though, is that you may have to do a little homework on what actually gets returned by EC2 as XML.
|
321
|
+
For example, when you make a #describe_images call in ec2sh to EC2 what you will get back will look like:
|
322
|
+
|
323
|
+
|
324
|
+
<pre syntax="ruby">
|
325
|
+
$ ec2sh
|
326
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').xml
|
327
|
+
|
328
|
+
<?xml version="1.0"?>
|
329
|
+
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2007-01-19/">
|
330
|
+
<imagesSet>
|
331
|
+
<item>
|
332
|
+
<imageId>ami-20b65349</imageId>
|
333
|
+
<imageLocation>ec2-public-images/fedora-core4-base.manifest.xml</imageLocation>
|
334
|
+
<imageState>available</imageState>
|
335
|
+
<imageOwnerId>amazon</imageOwnerId>
|
336
|
+
<isPublic>true</isPublic>
|
337
|
+
</item>
|
338
|
+
<item>
|
339
|
+
<imageId>ami-22b6534b</imageId>
|
340
|
+
<imageLocation>ec2-public-images/fedora-core4-mysql.manifest.xml</imageLocation>
|
341
|
+
<imageState>available</imageState>
|
342
|
+
<imageOwnerId>amazon</imageOwnerId>
|
343
|
+
<isPublic>true</isPublic>
|
344
|
+
</item>
|
345
|
+
<item>
|
346
|
+
<imageId>ami-23b6534a</imageId>
|
347
|
+
<imageLocation>ec2-public-images/fedora-core4-apache.manifest.xml</imageLocation>
|
348
|
+
<imageState>available</imageState>
|
349
|
+
<imageOwnerId>amazon</imageOwnerId>
|
350
|
+
<isPublic>true</isPublic>
|
351
|
+
</item>
|
352
|
+
<item>
|
353
|
+
<imageId>ami-25b6534c</imageId>
|
354
|
+
<imageLocation>ec2-public-images/fedora-core4-apache-mysql.manifest.xml</imageLocation>
|
355
|
+
<imageState>available</imageState>
|
356
|
+
<imageOwnerId>amazon</imageOwnerId>
|
357
|
+
<isPublic>true</isPublic>
|
358
|
+
</item>
|
359
|
+
<item>
|
360
|
+
<imageId>ami-26b6534f</imageId>
|
361
|
+
<imageLocation>ec2-public-images/developer-image.manifest.xml</imageLocation>
|
362
|
+
<imageState>available</imageState>
|
363
|
+
<imageOwnerId>amazon</imageOwnerId>
|
364
|
+
<isPublic>true</isPublic>
|
365
|
+
</item>
|
366
|
+
<item>
|
367
|
+
<imageId>ami-2bb65342</imageId>
|
368
|
+
<imageLocation>ec2-public-images/getting-started.manifest.xml</imageLocation>
|
369
|
+
<imageState>available</imageState>
|
370
|
+
<imageOwnerId>amazon</imageOwnerId>
|
371
|
+
<isPublic>true</isPublic>
|
372
|
+
</item>
|
373
|
+
</imagesSet>
|
374
|
+
</DescribeImagesResponse>
|
375
|
+
|
146
376
|
</pre>
|
147
377
|
|
378
|
+
You can see in the XML the structure that you will need to follow when constructing queries for information and parsing responses from EC2.
|
148
379
|
|
149
|
-
|
380
|
+
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:
|
381
|
+
|
382
|
+
<pre syntax="ruby">
|
383
|
+
>> puts @ec2.describe_images(:owner_id => 'amazon').imagesSet.item[2].imageId
|
384
|
+
ami-23b6534a
|
385
|
+
</pre>
|
386
|
+
|
387
|
+
EC2 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 EC2::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 EC2::Response#xml method like I did above to see the exact XML returned which allows you to easily derive the structure for the Ruby OpenStruct object.
|
150
388
|
|
151
|
-
h3. Project Related
|
152
389
|
|
153
|
-
|
154
|
-
"http://amazon-ec2.rubyforge.org/":http://amazon-ec2.rubyforge.org/
|
390
|
+
h2. Contributing
|
155
391
|
|
156
|
-
|
392
|
+
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.
|
157
393
|
|
394
|
+
h2. Project Info
|
395
|
+
|
396
|
+
This project is managed as a RubyForge project which you can find at "http://amazon-ec2.rubyforge.org/":http://amazon-ec2.rubyforge.org/ and this is always the best place to find the latest news, report any bugs, submit feature requests, or provide patches.
|
397
|
+
|
398
|
+
h2. Learning More
|
399
|
+
|
400
|
+
h3. Documentation
|
401
|
+
|
402
|
+
Complete RDoc generated documentation can be found at "http://amazon-ec2.rubyforge.org/rdoc/":http://amazon-ec2.rubyforge.org/rdoc/
|
403
|
+
|
404
|
+
h3. Websites
|
405
|
+
|
406
|
+
"Amazon Web Services Home":http://aws.amazon.com/
|
158
407
|
"Project Home":http://rubyforge.org/projects/amazon-ec2/
|
159
408
|
"Downloads":http://rubyforge.org/frs/?group_id=2753
|
160
409
|
"Browse Code":http://rubyforge.org/scm/?group_id=2753
|
@@ -165,12 +414,14 @@ h3. Project Tools
|
|
165
414
|
|
166
415
|
h3. Related Projects
|
167
416
|
|
168
|
-
"
|
417
|
+
"Capsize":http://capsize.rubyforge.org/ : A Capistrano (>= 2.x) plugin that allows complete control of Amazon EC2 from Capistrano recipes.
|
169
418
|
|
170
419
|
|
171
420
|
h2. Credits
|
172
421
|
|
173
|
-
The original sample code for this library was provided by Amazon Web Services, LLC. Thanks to them for providing all of us with samples that got this started.
|
422
|
+
The original sample code for this library was provided by Amazon Web Services, LLC. 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 EC2 team for including Ruby in their plans. We hope to see more AWS Ruby code.
|
423
|
+
|
424
|
+
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.
|
174
425
|
|
175
426
|
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 "http://newgem.rubyforge.org/":http://newgem.rubyforge.org/
|
176
427
|
|