amazon-ec2 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ http://amazon-ec2.rubyforge.org/
2
+
3
+
4
+
5
+
6
+ Hello Ruby'ists,
7
+
8
+ I wanted to announce the availability of a major new release (version 0.2.2) of the open source 'amazon-ec2' Ruby Gem. This is for Ruby/Ruby on Rails developers that want their applications to interface with EC2.
9
+
10
+ http://amazon-ec2.rubyforge.org/
11
+
12
+ The original release of this gem (<= 0.1.0 about six months ago) was based on the original sample code provided by the AWS team (Thanks guys!). This new release represents an almost complete re-write of that code and should prove to be much more reliable, easier to use, and feature complete.
13
+
14
+ Important : Please note that this is such a major re-write that we were not able to maintain backwards compatibility with the older versions of the gem. So if you are using a version of 'amazon-ec2' that is less than v 0.2.2 you should carefully read the docs before you upgrade as you will be required to update your code. You will also want to exercise caution if you are actively using the 'Capazon' gem. 'Capazon' has been deprecated and we'll have more news on that topic soon. If this is your first install and use of the gem then you are good to go!
15
+
16
+ Some of the major changes are:
17
+
18
+ * Re-factored all methods to use Ruby Hashes for all parameters. Makes figuring out what options to pass and actually passing them much much easier. Order of params no longer matters and your code will be self documenting when you can read the hash key names for each parameter.
19
+
20
+ * All responses from EC2 are now sent back as an EC2::Response object. This is an OpenStruct object that contains everything in the EC2 XML response converted to a real Ruby object data structure. No more parsing responses or error responses. The OpenStruct object is created using XmlSimple and is a one-to-one mapping of the structure in the EC2 response XML.
21
+
22
+ * A complete exception handling framework has been added. Any exception (either from incorrect args, or as part of the response from EC2) now throw real Ruby exceptions that are subclassed from EC2::Error. All of the exceptions defined by AWS in the developer guide each have their own exception sub-class. This lets you handle exceptions on a case by case basis with a great degree of granularity. Just wrap any calls to EC2 using the gem in a begin/rescue clause. There are no longer any errors that will come back in some kind of text form. Either the command is OK and you get an EC2::Response object, or their is a problem and you get EC2::Error or one of its sub-classes.
23
+
24
+ * The gem now has a full unit test suite with several hundred test cases. All public methods on the gem have 100% test case coverage, and the overall code coverage according to rcov is 92%. This has provided us with a much more robust gem and should help us avoid future regression errors with new releases.
25
+
26
+ * The gem is now up to date with the latest version of the EC2 API (2007-01-19) and all API methods should be covered. This includes new additions such as the ability to get console output, reboot instances, and running instances with the new NAT addressing scheme.
27
+
28
+ * The rdocs, website, and README have undergone significant surgery. The website content is now automatically generated as part of the build and deploy of the gem.
29
+
30
+ I encourage you to visit the link above for the full information about what's new and why you may want to think about transitioning to this new release. A lot of work by a handful of people has gone into this new release and we hope you enjoy it. Stand by for some more news regarding using this gem with Capistrano in the near future.
31
+
32
+ Enjoy, and please feel free to reach out to me with any questions at grempe -at- rubyforge -dot- org.
33
+
34
+ Glenn
35
+
36
+
@@ -32,8 +32,8 @@
32
32
 
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
- Get Version
36
- <a href="http://rubyforge.org/projects/amazon-ec2" class="numbers">0.2.4</a>
35
+ <p>Get Version</p>
36
+ <a href="http://rubyforge.org/projects/amazon-ec2" class="numbers">0.2.5</a>
37
37
  </div>
38
38
  <h2>&#x2192; &#8216;amazon-ec2&#8217;</h2>
39
39
 
@@ -174,10 +174,10 @@ Marcel Molina&#8217;s fine <span class="caps">AWS</span>::S3 library.</li>
174
174
  <h3>Installing the gem</h3>
175
175
 
176
176
 
177
- <p>Linux / <span class="caps">OS X</span> : <pre syntax="ruby">sudo gem install amazon-ec2 --include-dependencies</pre></p>
177
+ <p>Linux / <span class="caps">OS X</span> : <pre class='syntax'><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">amazon</span><span class="punct">-</span><span class="ident">ec2</span> <span class="punct">--</span><span class="ident">include</span><span class="punct">-</span><span class="ident">dependencies</span></pre></p>
178
178
 
179
179
 
180
- <p>Microsoft Windows : <pre syntax="ruby">gem install amazon-ec2 --include-dependencies</pre></p>
180
+ <p>Microsoft Windows : <pre class='syntax'><span class="ident">gem</span> <span class="ident">install</span> <span class="ident">amazon</span><span class="punct">-</span><span class="ident">ec2</span> <span class="punct">--</span><span class="ident">include</span><span class="punct">-</span><span class="ident">dependencies</span></pre></p>
181
181
 
182
182
 
183
183
  <h2>Using the library</h2>
@@ -200,15 +200,16 @@ do it this way, feel free to copy these scripts from the gem dir to any location
200
200
  <p>Edit the file ~/.bash_login and add the following to the existing contents:</p>
201
201
 
202
202
 
203
- <pre syntax="ruby">
203
+ <p><pre class='syntax'>
204
204
 
205
- export RUBYOPT="rubygems"
205
+ <span class="ident">export</span> <span class="constant">RUBYOPT</span><span class="punct">=&quot;</span><span class="string">rubygems</span><span class="punct">&quot;</span>
206
206
 
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"
207
+ <span class="comment"># For amazon-ec2 and amazon s3 ruby gems</span>
208
+ <span class="ident">export</span> <span class="constant">AMAZON_ACCESS_KEY_ID</span><span class="punct">=&quot;</span><span class="string">YOUR_ACCESS_KEY_ID</span><span class="punct">&quot;</span>
209
+ <span class="ident">export</span> <span class="constant">AMAZON_SECRET_ACCESS_KEY</span><span class="punct">=&quot;</span><span class="string">YOUR_SECRET_ACCESS_KEY_ID</span><span class="punct">&quot;</span>
210
+
211
+ </pre></p>
210
212
 
211
- </pre>
212
213
 
213
214
  <p>Once you save the file you should close and re-open your command terminal so the new variables are made available. You&#8217;ll need to do this close/re-open step for each terminal window you have open (or issue the &#8216;source ~/.bash_login&#8217; command in each). Make sure that this file is only readable by your user so you don&#8217;t inadvertantly expose your credentials to other users on your system.</p>
214
215
 
@@ -219,7 +220,7 @@ export AMAZON_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY_ID"
219
220
  <h3>The basics&#8230;</h3>
220
221
 
221
222
 
222
- <p>The library exposes one main interface module <pre syntax="ruby">EC2::Base</pre></p>
223
+ <p>The library exposes one main interface module <pre class='syntax'><span class="constant">EC2</span><span class="punct">::</span><span class="constant">Base</span></pre></p>
223
224
 
224
225
 
225
226
  <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
@@ -227,9 +228,10 @@ method calls directly against <span class="caps">EC2</span>. All the operations
227
228
  are handled automatically for you. The connection string will look something like this:</p>
228
229
 
229
230
 
230
- <pre syntax="ruby">
231
- @ec2 = EC2::Base.new(:access_key_id =&gt; ACCESS_KEY_ID, :secret_access_key =&gt; SECRET_ACCESS_KEY)
232
- </pre>
231
+ <p><pre class='syntax'>
232
+ <span class="attribute">@ec2</span> <span class="punct">=</span> <span class="constant">EC2</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="symbol">:access_key_id</span> <span class="punct">=&gt;</span> <span class="constant">ACCESS_KEY_ID</span><span class="punct">,</span> <span class="symbol">:secret_access_key</span> <span class="punct">=&gt;</span> <span class="constant">SECRET_ACCESS_KEY</span><span class="punct">)</span>
233
+ </pre></p>
234
+
233
235
 
234
236
  <p>We have tried to keep the public methods on &#8216;amazon-ec2&#8217; as close as possible to the <span class="caps">AWS EC2</span> Query <span class="caps">API</span>.
235
237
  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.
@@ -249,7 +251,8 @@ Feel free to browse the full <a href="http://amazon-ec2.rubyforge.org/rdoc/">RDo
249
251
  <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>
250
252
 
251
253
 
252
- <pre syntax="ruby">[your amazon-ec2 gem dir]/examples/ec2-example.rb</pre>
254
+ <p><pre class='syntax'><span class="punct">[</span><span class="ident">your</span> <span class="ident">amazon</span><span class="punct">-</span><span class="ident">ec2</span> <span class="ident">gem</span> <span class="ident">dir</span><span class="punct">]/</span><span class="ident">examples</span><span class="punct">/</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">example</span><span class="punct">.</span><span class="ident">rb</span></pre></p>
255
+
253
256
 
254
257
  <p>Since we also package this sample file in the gem&#8217;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>
255
258
 
@@ -263,17 +266,17 @@ Feel free to browse the full <a href="http://amazon-ec2.rubyforge.org/rdoc/">RDo
263
266
  <p>If your not in front of a terminal shell now (perhaps you&#8217;re browsing this site on your iPhone) this is what you would see:</p>
264
267
 
265
268
 
266
- <pre syntax="ruby">
269
+ <p><pre class='syntax'>
267
270
 
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
271
+ <span class="ident">hostname</span><span class="punct">:/</span><span class="regex">tmp</span><span class="punct">/</span><span class="ident">rails</span><span class="punct">/</span><span class="ident">amazon_test</span> <span class="ident">glenn</span><span class="global">$ </span><span class="ident">ec2sh</span>
272
+
273
+ <span class="punct">'</span><span class="string">ec2sh</span><span class="punct">'</span> <span class="ident">usage</span> <span class="punct">:</span>
274
+ <span class="constant">This</span> <span class="ident">is</span> <span class="ident">an</span> <span class="ident">interactive</span> <span class="punct">'</span><span class="string">irb</span><span class="punct">'</span> <span class="ident">command</span> <span class="ident">shell</span> <span class="ident">that</span> <span class="ident">allows</span> <span class="ident">you</span> <span class="ident">to</span> <span class="ident">use</span> <span class="ident">all</span>
275
+ <span class="ident">commands</span> <span class="ident">available</span> <span class="ident">to</span> <span class="ident">the</span> <span class="ident">amazon</span><span class="punct">-</span><span class="ident">ec2</span> <span class="ident">gem</span><span class="punct">.</span> <span class="ident">You</span><span class="punct">'</span><span class="string">ll find this to be a
273
276
  great tool to help you debug issues and practice running commands
274
277
  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
278
+
279
+ The EC2 connection is wired to the class instance </span><span class="punct">'</span><span class="attribute">@ec2</span><span class="punct">'</span><span class="string">. Make method calls
277
280
  on this to execute commands on EC2. Adding a #to_s
278
281
  at the end of any command should give you a full String representation of the
279
282
  response. The #xml data is available for each response
@@ -282,30 +285,31 @@ hostname:/tmp/rails/amazon_test glenn$ ec2sh
282
285
  hierarchy of an entire response in a friendly way (if XML is friendly
283
286
  to you!). Understanding the hierarchy of the XML response is critical
284
287
  to making effective use of this library.
285
-
288
+
286
289
  Examples to try:
287
-
290
+
288
291
  returns : all ec2 public methods
289
292
  &gt;&gt; @ec2.methods.sort
290
-
293
+
291
294
  returns : a string representation of ALL images
292
295
  &gt;&gt; @ec2.describe_images.to_s
293
-
296
+
294
297
  returns : an Array of EC2::Response objects, each an EC2 image and its data
295
298
  &gt;&gt; @ec2.describe_images.imagesSet.item
296
299
  &gt;&gt; @ec2.describe_images.imagesSet.item[0] (an OpenStruct of a single item in that array)
297
300
  &gt;&gt; @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that OpenStruct item)
298
-
301
+
299
302
  returns : an XML representation of all images
300
303
  &gt;&gt; puts @ec2.describe_images.xml
301
-
304
+
302
305
  returns : an XML representation of all images owned by Amazon
303
- &gt;&gt; puts @ec2.describe_images(:owner_id =&gt; 'amazon').xml
304
-
306
+ &gt;&gt; puts @ec2.describe_images(:owner_id =&gt; </span><span class="punct">'</span><span class="ident">amazon</span><span class="punct">'</span><span class="string">).xml
307
+
305
308
  &gt;&gt; @ec2.describe_images.imagesSet.item[0].to_s
306
- =&gt; "#&lt;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=#&lt;EC2::Response:0x100A469A6 ...&gt;&gt;"
309
+ =&gt; &quot;#&lt;EC2::Response:0x100A465B4 imageId=\&quot;ami-018e6b68\&quot; imageLocation=\&quot;rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\&quot; imageOwnerId=\&quot;099034111737\&quot; imageState=\&quot;available\&quot; isPublic=\&quot;true\&quot; parent=#&lt;EC2::Response:0x100A469A6 ...&gt;&gt;&quot;
310
+ <span class="normal">
311
+ </span></span></pre></p>
307
312
 
308
- </pre>
309
313
 
310
314
  <h4>Using &#8216;amazon-ec2&#8217; in Ruby scripts</h4>
311
315
 
@@ -313,25 +317,26 @@ hostname:/tmp/rails/amazon_test glenn$ ec2sh
313
317
  <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 &#8217;.each&#8217; iterator (You&#8217;ll get errors if you try). You need to instead walk through the Array of items which are in the &#8216;imagesSet&#8217; 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
318
 
315
319
 
316
- <pre syntax="ruby">
317
- #!/usr/bin/env ruby
320
+ <p><pre class='syntax'>
321
+ <span class="comment">#!/usr/bin/env ruby</span>
318
322
 
319
- require 'rubygems'
320
- require 'ec2'
323
+ <span class="ident">require</span> <span class="punct">'</span><span class="string">rubygems</span><span class="punct">'</span>
324
+ <span class="ident">require</span> <span class="punct">'</span><span class="string">ec2</span><span class="punct">'</span>
321
325
 
322
- ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
323
- SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'
326
+ <span class="constant">ACCESS_KEY_ID</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">--YOUR AWS ACCESS KEY ID--</span><span class="punct">'</span>
327
+ <span class="constant">SECRET_ACCESS_KEY</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">--YOUR AWS SECRET ACCESS KEY--</span><span class="punct">'</span>
328
+
329
+ <span class="ident">ec2</span> <span class="punct">=</span> <span class="constant">EC2</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="symbol">:access_key_id</span> <span class="punct">=&gt;</span> <span class="constant">ACCESS_KEY_ID</span><span class="punct">,</span> <span class="symbol">:secret_access_key</span> <span class="punct">=&gt;</span> <span class="constant">SECRET_ACCESS_KEY</span><span class="punct">)</span>
330
+
331
+ <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">----- listing images owned by 'amazon' -----</span><span class="punct">&quot;</span>
332
+ <span class="ident">ec2</span><span class="punct">.</span><span class="ident">describe_images</span><span class="punct">(</span><span class="symbol">:owner_id</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">amazon</span><span class="punct">&quot;).</span><span class="ident">imagesSet</span><span class="punct">.</span><span class="ident">item</span><span class="punct">.</span><span class="ident">each</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">image</span><span class="punct">|</span>
333
+ <span class="comment"># OpenStruct objects have members!</span>
334
+ <span class="ident">image</span><span class="punct">.</span><span class="ident">members</span><span class="punct">.</span><span class="ident">each</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">member</span><span class="punct">|</span>
335
+ <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{member}</span> =&gt; <span class="expr">#{image[member]}</span></span><span class="punct">&quot;</span>
336
+ <span class="keyword">end</span>
337
+ <span class="keyword">end</span>
338
+ </pre></p>
324
339
 
325
- ec2 = EC2::Base.new(:access_key_id =&gt; ACCESS_KEY_ID, :secret_access_key =&gt; SECRET_ACCESS_KEY)
326
-
327
- puts "----- listing images owned by 'amazon' -----"
328
- ec2.describe_images(:owner_id =&gt; "amazon").imagesSet.item.each do |image|
329
- # OpenStruct objects have members!
330
- image.members.each do |member|
331
- puts "#{member} =&gt; #{image[member]}"
332
- end
333
- end
334
- </pre>
335
340
 
336
341
  <h4>Using &#8216;amazon-ec2&#8217; in Ruby on Rails applications</h4>
337
342
 
@@ -339,58 +344,60 @@ end
339
344
  <p>in config/environment.rb:</p>
340
345
 
341
346
 
342
- <pre syntax="ruby">
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'
346
- </pre>
347
+ <p><pre class='syntax'>
348
+ <span class="comment"># Require the amazon-ec2 gem and make its methods available in your Rails app</span>
349
+ <span class="comment"># Put this at the bottom of your environment.rb</span>
350
+ <span class="ident">require</span> <span class="punct">'</span><span class="string">EC2</span><span class="punct">'</span>
351
+ </pre></p>
352
+
347
353
 
348
- in app/controllers/your_controller.rb:
349
- <pre syntax="ruby">
354
+ <p>in app/controllers/your_controller.rb:
355
+ <pre class='syntax'>
350
356
 
351
- [some controller code ...]
357
+ <span class="punct">[</span><span class="ident">some</span> <span class="ident">controller</span> <span class="ident">code</span> <span class="punct">...]</span>
352
358
 
353
- ec2 = EC2::Base.new(:access_key_id =&gt; "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key =&gt; "YOUR_AWS_SECRET_ACCESS_KEY")
359
+ <span class="ident">ec2</span> <span class="punct">=</span> <span class="constant">EC2</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="symbol">:access_key_id</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">YOUR_AWS_ACCESS_KEY_ID</span><span class="punct">&quot;,</span> <span class="symbol">:secret_access_key</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">YOUR_AWS_SECRET_ACCESS_KEY</span><span class="punct">&quot;)</span>
354
360
 
355
- # get ALL public images
356
- @ec2_images = ec2.describe_images().imagesSet.item
361
+ <span class="comment"># get ALL public images</span>
362
+ <span class="attribute">@ec2_images</span> <span class="punct">=</span> <span class="ident">ec2</span><span class="punct">.</span><span class="ident">describe_images</span><span class="punct">().</span><span class="ident">imagesSet</span><span class="punct">.</span><span class="ident">item</span>
357
363
 
358
- # Get info on all public EC2 images created by the Amazon EC2 team.
359
- @ec2_images_amazon = ec2.describe_images(:owner_id =&gt; "amazon").imagesSet.item
364
+ <span class="comment"># Get info on all public EC2 images created by the Amazon EC2 team.</span>
365
+ <span class="attribute">@ec2_images_amazon</span> <span class="punct">=</span> <span class="ident">ec2</span><span class="punct">.</span><span class="ident">describe_images</span><span class="punct">(</span><span class="symbol">:owner_id</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">amazon</span><span class="punct">&quot;).</span><span class="ident">imagesSet</span><span class="punct">.</span><span class="ident">item</span>
360
366
 
361
- [some more controller code ...]
367
+ <span class="punct">[</span><span class="ident">some</span> <span class="ident">more</span> <span class="ident">controller</span> <span class="ident">code</span> <span class="punct">...]</span>
368
+
369
+ </pre></p>
362
370
 
363
- </pre>
364
371
 
365
372
  <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>
366
373
 
367
374
 
368
- <pre syntax="ruby">
375
+ <p><pre class='syntax'>
369
376
 
370
- &lt;h1&gt;EC2 Test#index&lt;/h1&gt;
377
+ <span class="punct">&lt;</span><span class="ident">h1</span><span class="punct">&gt;</span><span class="constant">EC2</span> <span class="constant">Test</span><span class="comment">#index&lt;/h1&gt;</span>
371
378
 
372
- &lt;h1&gt;Sample 1 - debug() view&lt;/h1&gt;
379
+ <span class="punct">&lt;</span><span class="ident">h1</span><span class="punct">&gt;</span><span class="constant">Sample</span> <span class="number">1</span> <span class="punct">-</span> <span class="ident">debug</span><span class="punct">()</span> <span class="ident">view</span><span class="punct">&lt;/</span><span class="regex">h1&gt;
373
380
 
374
381
  &lt;%= debug(@ec2_images_amazon) %&gt;
375
382
 
376
- &lt;h1&gt;Sample 2 - Build a table&lt;/h1&gt;
377
-
378
- &lt;table border='1'&gt;
379
- &lt;tr&gt;
380
- &lt;th&gt;image.imageId&lt;/th&gt;
381
- &lt;th&gt;image.imageLocation&lt;/th&gt;
382
- &lt;th&gt;image.imageOwnerId&lt;/th&gt;
383
- &lt;th&gt;image.imageState&lt;/th&gt;
384
- &lt;th&gt;image.isPublic&lt;/th&gt;
385
- &lt;/tr&gt;
386
-
387
- &lt;% for image in @ec2_images_amazon %&gt;
388
- &lt;tr&gt;
389
- &lt;td&gt;&lt;%=h image.imageId %&gt;&lt;/td&gt;
390
- &lt;td&gt;&lt;%=h image.imageLocation %&gt;&lt;/td&gt;
391
- &lt;td&gt;&lt;%=h image.imageOwnerId %&gt;&lt;/td&gt;
392
- &lt;td&gt;&lt;%=h image.imageState %&gt;&lt;/td&gt;
393
- &lt;td&gt;&lt;%=h image.isPublic %&gt;&lt;/td&gt;
383
+ &lt;h1&gt;Sample 2 - Build a table&lt;</span><span class="punct">/</span><span class="ident">h1</span><span class="punct">&gt;</span>
384
+
385
+ <span class="punct">&lt;</span><span class="ident">table</span> <span class="ident">border</span><span class="punct">='</span><span class="string">1</span><span class="punct">'&gt;</span>
386
+ <span class="punct">&lt;</span><span class="ident">tr</span><span class="punct">&gt;</span>
387
+ <span class="punct">&lt;</span><span class="ident">th</span><span class="punct">&gt;</span><span class="ident">image</span><span class="punct">.</span><span class="ident">imageId</span><span class="punct">&lt;/</span><span class="regex">th&gt;
388
+ &lt;th&gt;image.imageLocation&lt;</span><span class="punct">/</span><span class="ident">th</span><span class="punct">&gt;</span>
389
+ <span class="punct">&lt;</span><span class="ident">th</span><span class="punct">&gt;</span><span class="ident">image</span><span class="punct">.</span><span class="ident">imageOwnerId</span><span class="punct">&lt;/</span><span class="regex">th&gt;
390
+ &lt;th&gt;image.imageState&lt;</span><span class="punct">/</span><span class="ident">th</span><span class="punct">&gt;</span>
391
+ <span class="punct">&lt;</span><span class="ident">th</span><span class="punct">&gt;</span><span class="ident">image</span><span class="punct">.</span><span class="ident">isPublic</span><span class="punct">&lt;/</span><span class="regex">th&gt;
392
+ &lt;</span><span class="punct">/</span><span class="ident">tr</span><span class="punct">&gt;</span>
393
+
394
+ <span class="punct">&lt;%</span> <span class="keyword">for</span> <span class="ident">image</span> <span class="keyword">in</span> <span class="attribute">@ec2_images_amazon</span> <span class="punct">%&gt;</span><span class="string">
395
+ &lt;tr</span><span class="punct">&gt;</span>
396
+ <span class="punct">&lt;</span><span class="ident">td</span><span class="punct">&gt;&lt;%=</span><span class="string">h image.imageId %&gt;&lt;/td&gt;
397
+ &lt;td&gt;&lt;%</span><span class="punct">=</span><span class="ident">h</span> <span class="ident">image</span><span class="punct">.</span><span class="ident">imageLocation</span> <span class="punct">%&gt;</span><span class="string">&lt;/td</span><span class="punct">&gt;</span>
398
+ <span class="punct">&lt;</span><span class="ident">td</span><span class="punct">&gt;&lt;%=</span><span class="string">h image.imageOwnerId %&gt;&lt;/td&gt;
399
+ &lt;td&gt;&lt;%</span><span class="punct">=</span><span class="ident">h</span> <span class="ident">image</span><span class="punct">.</span><span class="ident">imageState</span> <span class="punct">%&gt;</span><span class="string">&lt;/td</span><span class="punct">&gt;</span>
400
+ <span class="punct">&lt;</span><span class="ident">td</span><span class="punct">&gt;&lt;%=</span><span class="string">h image.isPublic %&gt;&lt;/td&gt;
394
401
  &lt;/tr&gt;
395
402
  &lt;% end %&gt;
396
403
  &lt;/table&gt;
@@ -398,15 +405,16 @@ ec2 = EC2::Base.new(:access_key_id =&gt; "YOUR_AWS_ACCESS_KEY_ID", :secret_acces
398
405
  &lt;h1&gt;Sample 3 - Iterate&lt;/h1&gt;
399
406
 
400
407
  &lt;% @ec2_images_amazon.each do |image| %&gt;
401
- &lt;% image.each_pair do |key, value| %&gt;
402
- &lt;% unless key == 'parent' %&gt;
403
- &lt;%= "#{key} =&gt; #{value}" %&gt;&lt;br /&gt;
404
- &lt;% end %&gt;
405
- &lt;% end %&gt;
406
- &lt;br /&gt;
407
- &lt;% end %&gt;
408
+ &lt;% image.each_pair do |key, value| %&gt;
409
+ &lt;% unless key </span><span class="punct">==</span> <span class="punct">'</span><span class="string">parent</span><span class="punct">'</span> <span class="punct">%&gt;</span><span class="string">
410
+ &lt;%= &quot;<span class="expr">#{key}</span> =</span><span class="punct">&gt;</span> <span class="comment">#{value}&quot; %&gt;&lt;br /&gt;</span>
411
+ <span class="punct">&lt;%</span> <span class="keyword">end</span> <span class="punct">%&gt;</span><span class="string">
412
+ &lt;% end %</span><span class="punct">&gt;</span>
413
+ <span class="punct">&lt;</span><span class="ident">br</span> <span class="punct">/&gt;</span>
414
+ <span class="punct">&lt;%</span> <span class="keyword">end</span> <span class="punct">%&gt;</span><span class="string">
415
+ <span class="normal">
416
+ </span></span></pre></p>
408
417
 
409
- </pre>
410
418
 
411
419
  <h4>Important notes regarding the structure of <span class="caps">EC2</span>::Response Objects</h4>
412
420
 
@@ -419,59 +427,60 @@ benefit. What this means though, is that you may have to do a little homework o
419
427
  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
428
 
421
429
 
422
- <pre syntax="ruby">
423
- $ ec2sh
424
- &gt;&gt; puts @ec2.describe_images(:owner_id =&gt; 'amazon').xml
425
-
426
- &lt;?xml version="1.0"?&gt;
427
- &lt;DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2007-01-19/"&gt;
428
- &lt;imagesSet&gt;
430
+ <p><pre class='syntax'>
431
+ <span class="global">$ </span><span class="ident">ec2sh</span>
432
+ <span class="punct">&gt;&gt;</span> <span class="ident">puts</span> <span class="attribute">@ec2</span><span class="punct">.</span><span class="ident">describe_images</span><span class="punct">(</span><span class="symbol">:owner_id</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">amazon</span><span class="punct">').</span><span class="ident">xml</span>
433
+
434
+ <span class="punct">&lt;</span><span class="char">?x</span><span class="ident">ml</span> <span class="ident">version</span><span class="punct">=&quot;</span><span class="string">1.0</span><span class="punct">&quot;</span><span class="char">?&gt;</span>
435
+ <span class="punct">&lt;</span><span class="constant">DescribeImagesResponse</span> <span class="ident">xmlns</span><span class="punct">=&quot;</span><span class="string">http://ec2.amazonaws.com/doc/2007-01-19/</span><span class="punct">&quot;&gt;</span>
436
+ <span class="punct">&lt;</span><span class="ident">imagesSet</span><span class="punct">&gt;</span>
437
+ <span class="punct">&lt;</span><span class="ident">item</span><span class="punct">&gt;</span>
438
+ <span class="punct">&lt;</span><span class="ident">imageId</span><span class="punct">&gt;</span><span class="ident">ami</span><span class="punct">-</span><span class="number">20</span><span class="ident">b65349</span><span class="punct">&lt;/</span><span class="regex">imageId&gt;
439
+ &lt;imageLocation&gt;ec2-public-images</span><span class="punct">/</span><span class="ident">fedora</span><span class="punct">-</span><span class="ident">core4</span><span class="punct">-</span><span class="ident">base</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
440
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
441
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
442
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
443
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
429
444
  &lt;item&gt;
430
- &lt;imageId&gt;ami-20b65349&lt;/imageId&gt;
431
- &lt;imageLocation&gt;ec2-public-images/fedora-core4-base.manifest.xml&lt;/imageLocation&gt;
432
- &lt;imageState&gt;available&lt;/imageState&gt;
433
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
434
- &lt;isPublic&gt;true&lt;/isPublic&gt;
435
- &lt;/item&gt;
445
+ &lt;imageId&gt;ami-22b6534b&lt;</span><span class="punct">/</span><span class="ident">imageId</span><span class="punct">&gt;</span>
446
+ <span class="punct">&lt;</span><span class="ident">imageLocation</span><span class="punct">&gt;</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">public</span><span class="punct">-</span><span class="ident">images</span><span class="punct">/</span><span class="ident">fedora</span><span class="punct">-</span><span class="ident">core4</span><span class="punct">-</span><span class="ident">mysql</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
447
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
448
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
449
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
450
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
436
451
  &lt;item&gt;
437
- &lt;imageId&gt;ami-22b6534b&lt;/imageId&gt;
438
- &lt;imageLocation&gt;ec2-public-images/fedora-core4-mysql.manifest.xml&lt;/imageLocation&gt;
439
- &lt;imageState&gt;available&lt;/imageState&gt;
440
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
441
- &lt;isPublic&gt;true&lt;/isPublic&gt;
442
- &lt;/item&gt;
452
+ &lt;imageId&gt;ami-23b6534a&lt;</span><span class="punct">/</span><span class="ident">imageId</span><span class="punct">&gt;</span>
453
+ <span class="punct">&lt;</span><span class="ident">imageLocation</span><span class="punct">&gt;</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">public</span><span class="punct">-</span><span class="ident">images</span><span class="punct">/</span><span class="ident">fedora</span><span class="punct">-</span><span class="ident">core4</span><span class="punct">-</span><span class="ident">apache</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
454
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
455
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
456
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
457
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
443
458
  &lt;item&gt;
444
- &lt;imageId&gt;ami-23b6534a&lt;/imageId&gt;
445
- &lt;imageLocation&gt;ec2-public-images/fedora-core4-apache.manifest.xml&lt;/imageLocation&gt;
446
- &lt;imageState&gt;available&lt;/imageState&gt;
447
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
448
- &lt;isPublic&gt;true&lt;/isPublic&gt;
449
- &lt;/item&gt;
459
+ &lt;imageId&gt;ami-25b6534c&lt;</span><span class="punct">/</span><span class="ident">imageId</span><span class="punct">&gt;</span>
460
+ <span class="punct">&lt;</span><span class="ident">imageLocation</span><span class="punct">&gt;</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">public</span><span class="punct">-</span><span class="ident">images</span><span class="punct">/</span><span class="ident">fedora</span><span class="punct">-</span><span class="ident">core4</span><span class="punct">-</span><span class="ident">apache</span><span class="punct">-</span><span class="ident">mysql</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
461
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
462
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
463
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
464
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
450
465
  &lt;item&gt;
451
- &lt;imageId&gt;ami-25b6534c&lt;/imageId&gt;
452
- &lt;imageLocation&gt;ec2-public-images/fedora-core4-apache-mysql.manifest.xml&lt;/imageLocation&gt;
453
- &lt;imageState&gt;available&lt;/imageState&gt;
454
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
455
- &lt;isPublic&gt;true&lt;/isPublic&gt;
456
- &lt;/item&gt;
466
+ &lt;imageId&gt;ami-26b6534f&lt;</span><span class="punct">/</span><span class="ident">imageId</span><span class="punct">&gt;</span>
467
+ <span class="punct">&lt;</span><span class="ident">imageLocation</span><span class="punct">&gt;</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">public</span><span class="punct">-</span><span class="ident">images</span><span class="punct">/</span><span class="ident">developer</span><span class="punct">-</span><span class="ident">image</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
468
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
469
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
470
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
471
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
457
472
  &lt;item&gt;
458
- &lt;imageId&gt;ami-26b6534f&lt;/imageId&gt;
459
- &lt;imageLocation&gt;ec2-public-images/developer-image.manifest.xml&lt;/imageLocation&gt;
460
- &lt;imageState&gt;available&lt;/imageState&gt;
461
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
462
- &lt;isPublic&gt;true&lt;/isPublic&gt;
463
- &lt;/item&gt;
464
- &lt;item&gt;
465
- &lt;imageId&gt;ami-2bb65342&lt;/imageId&gt;
466
- &lt;imageLocation&gt;ec2-public-images/getting-started.manifest.xml&lt;/imageLocation&gt;
467
- &lt;imageState&gt;available&lt;/imageState&gt;
468
- &lt;imageOwnerId&gt;amazon&lt;/imageOwnerId&gt;
469
- &lt;isPublic&gt;true&lt;/isPublic&gt;
470
- &lt;/item&gt;
471
- &lt;/imagesSet&gt;
472
- &lt;/DescribeImagesResponse&gt;
473
+ &lt;imageId&gt;ami-2bb65342&lt;</span><span class="punct">/</span><span class="ident">imageId</span><span class="punct">&gt;</span>
474
+ <span class="punct">&lt;</span><span class="ident">imageLocation</span><span class="punct">&gt;</span><span class="ident">ec2</span><span class="punct">-</span><span class="ident">public</span><span class="punct">-</span><span class="ident">images</span><span class="punct">/</span><span class="ident">getting</span><span class="punct">-</span><span class="ident">started</span><span class="punct">.</span><span class="ident">manifest</span><span class="punct">.</span><span class="ident">xml</span><span class="punct">&lt;/</span><span class="regex">imageLocation&gt;
475
+ &lt;imageState&gt;available&lt;</span><span class="punct">/</span><span class="ident">imageState</span><span class="punct">&gt;</span>
476
+ <span class="punct">&lt;</span><span class="ident">imageOwnerId</span><span class="punct">&gt;</span><span class="ident">amazon</span><span class="punct">&lt;/</span><span class="regex">imageOwnerId&gt;
477
+ &lt;isPublic&gt;true&lt;</span><span class="punct">/</span><span class="ident">isPublic</span><span class="punct">&gt;</span>
478
+ <span class="punct">&lt;/</span><span class="regex">item&gt;
479
+ &lt;</span><span class="punct">/</span><span class="ident">imagesSet</span><span class="punct">&gt;</span>
480
+ <span class="punct">&lt;/</span><span class="regex">DescribeImagesResponse&gt;
481
+ <span class="normal">
482
+ </span></span></pre></p>
473
483
 
474
- </pre>
475
484
 
476
485
  <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
486
 
@@ -479,10 +488,11 @@ $ ec2sh
479
488
  <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
489
 
481
490
 
482
- <pre syntax="ruby">
483
- &gt;&gt; puts @ec2.describe_images(:owner_id =&gt; 'amazon').imagesSet.item[2].imageId
484
- ami-23b6534a
485
- </pre>
491
+ <p><pre class='syntax'>
492
+ <span class="punct">&gt;&gt;</span> <span class="ident">puts</span> <span class="attribute">@ec2</span><span class="punct">.</span><span class="ident">describe_images</span><span class="punct">(</span><span class="symbol">:owner_id</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">amazon</span><span class="punct">').</span><span class="ident">imagesSet</span><span class="punct">.</span><span class="ident">item</span><span class="punct">[</span><span class="number">2</span><span class="punct">].</span><span class="ident">imageId</span>
493
+ <span class="ident">ami</span><span class="punct">-</span><span class="number">23</span><span class="ident">b6534a</span>
494
+ </pre></p>
495
+
486
496
 
487
497
  <p><span class="caps">EC2</span> will typically return &#8216;sets&#8217; 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&#8217;ll find that you can use the &#8216;ec2sh&#8217; 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
498
 
@@ -543,7 +553,7 @@ ami-23b6534a
543
553
 
544
554
  <p>Comments, patches, and bug reports are welcome. Send an email to the address below or use the RubyForge forum for this project.</p>
545
555
  <p class="coda">
546
- <a href="mailto:grempe-at-rubyforge-dot-org">Glenn Rempe</a>, 6th July 2007<br>
556
+ <a href="mailto:grempe-at-rubyforge-dot-org">Glenn Rempe</a>, 26th September 2007<br>
547
557
  </p>
548
558
  </div>
549
559