krisr-amazon-ec2 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +6 -0
  2. data/ChangeLog +284 -0
  3. data/LICENSE +66 -0
  4. data/README.rdoc +329 -0
  5. data/Rakefile +101 -0
  6. data/VERSION +1 -0
  7. data/amazon-ec2.gemspec +123 -0
  8. data/bin/ec2-gem-example.rb +66 -0
  9. data/bin/ec2-gem-profile.rb +10 -0
  10. data/bin/ec2sh +62 -0
  11. data/bin/setup.rb +26 -0
  12. data/deps.rip +1 -0
  13. data/lib/AWS.rb +239 -0
  14. data/lib/AWS/EC2.rb +67 -0
  15. data/lib/AWS/EC2/availability_zones.rb +43 -0
  16. data/lib/AWS/EC2/console.rb +46 -0
  17. data/lib/AWS/EC2/elastic_ips.rb +154 -0
  18. data/lib/AWS/EC2/image_attributes.rb +168 -0
  19. data/lib/AWS/EC2/images.rb +136 -0
  20. data/lib/AWS/EC2/instances.rb +218 -0
  21. data/lib/AWS/EC2/keypairs.rb +96 -0
  22. data/lib/AWS/EC2/products.rb +45 -0
  23. data/lib/AWS/EC2/security_groups.rb +234 -0
  24. data/lib/AWS/EC2/snapshots.rb +96 -0
  25. data/lib/AWS/EC2/volumes.rb +172 -0
  26. data/lib/AWS/ELB.rb +57 -0
  27. data/lib/AWS/ELB/load_balancers.rb +201 -0
  28. data/lib/AWS/exceptions.rb +169 -0
  29. data/lib/AWS/responses.rb +61 -0
  30. data/lib/EC2.rb +31 -0
  31. data/perftools/ec2prof +0 -0
  32. data/perftools/ec2prof-results.dot +193 -0
  33. data/perftools/ec2prof-results.txt +126 -0
  34. data/perftools/ec2prof.symbols +129 -0
  35. data/test/test_EC2.rb +68 -0
  36. data/test/test_EC2_availability_zones.rb +49 -0
  37. data/test/test_EC2_console.rb +54 -0
  38. data/test/test_EC2_elastic_ips.rb +144 -0
  39. data/test/test_EC2_image_attributes.rb +238 -0
  40. data/test/test_EC2_images.rb +197 -0
  41. data/test/test_EC2_instances.rb +348 -0
  42. data/test/test_EC2_keypairs.rb +123 -0
  43. data/test/test_EC2_products.rb +48 -0
  44. data/test/test_EC2_responses.rb +52 -0
  45. data/test/test_EC2_s3_xmlsimple.rb +80 -0
  46. data/test/test_EC2_security_groups.rb +205 -0
  47. data/test/test_EC2_snapshots.rb +83 -0
  48. data/test/test_EC2_volumes.rb +142 -0
  49. data/test/test_ELB_load_balancers.rb +238 -0
  50. data/test/test_helper.rb +19 -0
  51. data/wsdl/2007-08-29.ec2.wsdl +1269 -0
  52. data/wsdl/2008-02-01.ec2.wsdl +1614 -0
  53. data/wsdl/2008-05-05.ec2.wsdl +2052 -0
  54. data/wsdl/2008-12-01.ec2.wsdl +2354 -0
  55. metadata +184 -0
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+
@@ -0,0 +1,284 @@
1
+ === 0.5.0 2009-06-06
2
+ * Added support for the ELB API
3
+ * Refactored code into EC2 and ELB directories so that base EC2 functionality can be reused.
4
+
5
+ === 0.4.0 2009-06-06
6
+ * Using technicalpickles/jeweler to manage gem now. See : http://github.com/technicalpickles/jeweler/tree/master
7
+
8
+ === 0.3.8 2009-04-16
9
+ * Applied patch, with updated tests to fix issue with base64 encoded user data (rsanheim).
10
+ * Applied patch, fixing an issue with EU Signature version 2 creation (delano).
11
+
12
+ === 0.3.6 2009-03-02
13
+ * You can now specify any arbitrary SSL tcp port to connect to (kerryb)
14
+ * Added rake target to quickly install the gem without docs
15
+
16
+ === 0.3.5 2009-02-26
17
+ * Honor the EC2_URL env var if provided in the main lib
18
+ * Some minor modifications to the exceptions raised when unknown
19
+
20
+ === 0.3.4 2009-01-31
21
+ * Added support in the helper binaries for EC2_URL server url for Euro servers
22
+ * AWS Signature v2 support
23
+
24
+ === 0.3.3 2009-01-24
25
+ * Removed docs related to the ability to get Response#xml which is deprecated
26
+ * Fixed conflict when aws/s3 gem was included in user code
27
+
28
+ === 0.3.2 2008-12-11
29
+ * Updated API version to 2008-12-01
30
+ * Note : The new European EC2 zone should just work if you specify ':server => "eu-west-1.ec2.amazonaws.com"' when initializing AWS::EC2::Base.new(). Please report any issues you have with using this new service.
31
+
32
+ === 0.3.1 2008-09-18
33
+ * Added basic custom kernel id support to run_instances. Thanks to Keith Hudgins for the patch.
34
+
35
+ === 0.3.0 2008-08-24
36
+ * Changed response object from AmazonAWS::Response to Hash (patch from Yan Pritzker 'skwp') Thanks!
37
+ * Added support for Elastic Block Storage (patch from Yann Klis 'yannski') Thanks!
38
+ * Bumped AWS EC2 API version to 2008-05-05
39
+ * Bumped gem version number
40
+ * Whitespace cleanup
41
+
42
+ === 0.2.15 2008-08-20
43
+ * Updated gem install instructions, and dependency specifications in the gemspec.
44
+
45
+ === 0.2.14 2008-06-30
46
+ * Merged patch from 'orionz' which adds support for EC2 Availability Zones. Thanks!
47
+
48
+ === 0.2.12 2008-04-25
49
+ * A few adjustments to work with GitHub.
50
+
51
+ === 0.2.11 2008-04-25
52
+ * Moved gem completely to GitHub.
53
+ * Removed a LOT of RubyForge related cruft that's no longer needed.
54
+
55
+ === 0.2.10 2008-04-13
56
+ * Fix bug where ec2#disassociate_address would return 'undefined method 'reject!' for "xxx.xxx.xxx.xxx":String'.
57
+ Thanks to Brian Thomas for reporting.
58
+
59
+ === 0.2.9 2008-03-31
60
+ * Added initial support for API version 2008-02-01. Support for new 'Elastic IP' functionality added.
61
+
62
+ === 0.2.8 2008-02-25
63
+ * Change HTTP API requests to use POST instead of GET. Based on patch #18380 from 'anonymous'.
64
+
65
+ === 0.2.7 2008-02-17
66
+ * Add support for HTTP Proxy when :proxy_server argument is passed. Based on patch from Mathias Dalheimer.
67
+
68
+ === 0.2.6 2007-10-16
69
+ * Updated to support EC2 API version 2007-08-29 released on 2007-10-16
70
+ * Supports new instances type feature. Specify an instance type to launch
71
+ (m1.small, m1.large, m1.xlarge) when you call the 'run_instances' method.
72
+
73
+ e.g. add to the params hash ':instance_type => "m1.small"'
74
+ * RunInstances and DescribeInstances now return the time when the Amazon EC2 instance was launched.
75
+ * Code Tidyup : Removed trailing spaces in code docs.
76
+
77
+ === 0.2.5 2007-09-26
78
+ * Updated using Dr. Nic's newgem v. 0.13.5
79
+ * Updated email address.
80
+ * No functional changes in the gem.
81
+
82
+ === 0.2.4 2007-07-09
83
+ * Changed how require statement for xmlsimple is called so we don't get warnings when running in rails.
84
+ Now it looks like: require 'xmlsimple' unless defined? XmlSimple
85
+
86
+ === 0.2.3 2007-07-06
87
+ * Updated gem to work with new official release of AWS API version 2007-03-01 which occurred
88
+ on July 6, 2007.
89
+ * Added support for primary new feature which supports Paid AMI's and the product codes
90
+ associated with them. Modified several methods to support this change, updated the test
91
+ cases, and added a new method EC2#confirm_product_instance.
92
+
93
+ === 0.2.2 2007-07-03
94
+ * Minor change to test case.
95
+
96
+ === 0.2.1 2007-07-03
97
+ * Some minor changes to the RDocs and README.txt
98
+
99
+ === 0.2.0 2007-07-03
100
+
101
+ * MAJOR library changes : THESE CHANGES ARE NOT BACKWARD COMPATIBLE!! You will need to update
102
+ the way in which you make calls, handle responses, and rescue exceptions from this library.
103
+ If you prefer not to make these changes you can feel free to continue to use the older version
104
+ of the gem. These older versions however will no longer be maintained.
105
+
106
+ * MAJOR refactoring of how methods calls are made. Now all methods are called with a simple hash
107
+ of arguments and none of them are positional. This feels much more "Ruby'ish".
108
+
109
+ * MAJOR refactoring of how responses are returned to users. No longer do you have to call the
110
+ .parse method, and no longer are you getting back simple arrays of information. Responses
111
+ now come in the form of OpenStruct objects that contain all of the data for an object in
112
+ Enumerable form so you can use iterators (.each, .each_pair, etc). All methods return an AWS::Response object
113
+ which inherits from OpenStruct. The return data from EC2, which is in XML form, is parsed
114
+ with XmlSimple and is used to directly construct the return data structure. This allows us
115
+ to know with some confidence that the data structure returned from AWS will always be consistent
116
+ with this library's responses. There is also an .xml attribute for each response object that lets you
117
+ see the full and complete XML response from AWS if that is useful to you.
118
+
119
+ * Added an exception framework which will now throw appropriate Ruby exceptions
120
+ that match those handed to us by Amazon EC2. ArgumentError exceptions will also
121
+ be thrown if you are making calls we know to be illegal or malformed. You should rescue
122
+ these exceptions in your application instead of parsing text responses. All exceptions
123
+ descend from AWS::Error. You can see them all in exceptions.rb in the gem install.
124
+
125
+ * Added a full suite of test/spec unit tests which currently cover 100% of the public methods
126
+ in this library. We have abot 92% code coverage according to rcov. This has greatly enhanced
127
+ the reliability of the library as well as our confidence in the code.
128
+ We used to have 0% test coverage. :-/
129
+
130
+ * Added an EC2 command shell : 'ec2sh' which can be called from anywhere and gives you
131
+ an interactive irb session with an EC2 connection pre-made for you as @ec2. You can use this
132
+ to interactively execute any command on EC2 that this library supports. Try @ec2.methods.sort
133
+ or @ec2.describe_images to test it out. You must first setup two shell environment variables
134
+ which contain your ACCESS_KEY_ID and SECRET_ACCESS_KEY for this to work. Otherwise an error
135
+ will be thrown when you try to start it. This is way cool and shamelessly borrowed from
136
+ Marcel Molina's fine AWS::S3 library.
137
+
138
+ * Updated API version in the query API request to 2007-01-19, and added all known method calls
139
+ in this version of the API to the gem (including reboot, viewing console output, NAT addressing
140
+ and more!)
141
+
142
+ * Removed .parse method as it is no longer needed or wanted.
143
+
144
+ * Removed 'verbose' attribute writer and a couple of related debug 'puts' calls in EC2.rb.
145
+
146
+ * Removed deprecated alias's used in the library that were added in v0.0.3
147
+
148
+ * Gem now requires XmlSimple gem to be installed.
149
+
150
+
151
+ === 0.1.0 2007-05-30
152
+
153
+ * Repackaged with updated newgem generator v. 0.10.3 from http://newgem.rubyforge.org/ to take advantage of new deployment goodness.
154
+
155
+
156
+ === 0.0.7 2007-03-15
157
+
158
+ * Applied patch from Kevin Clark which does the following:
159
+ * New method to the instances.rb to allow for issuing a reboot command to an EC2 instance.
160
+ * New Mocha based tests (Mocha test suite must be installed to run tests. 'sudo gem install mocha')
161
+ * Cleanup of the test directory.
162
+ * Applied patch from Randy Bias related to CGI escaping error. From his notes:
163
+
164
+ I finally figured out what was going on. It was a compound problem. The first being that you can't
165
+ CGI::encode the UserData early. You have to just Base64 encode it. Unfortunately, when you Base64
166
+ encode it, it means that some encodings will be padded with extra chars, specifically the '=', but &, ?
167
+ and = get stripped by EC2.canonial_string. So if a Base64 encoding has trailing ='s for padding,
168
+ these get stripped and then you sign the UserData Base64 payload sans the padding. But it looks
169
+ like Amazon's side is doing the right thing and is signing the padding. So, the signatures mis-match.
170
+
171
+ I've got the complete patch here and it's about as elegant/clean as I could make it in a short
172
+ period of time. It's a little tough to strip those equal signs when there are so many in the URI.
173
+ I think this works pretty well, though. I'm splitting the URI on '&', then for each field ripping
174
+ out all '&' and '?' and only the first '='. Then stitching it back together.
175
+
176
+
177
+ === 0.0.6 2007-03-02
178
+ * Patched instances.rb with a bugfix and patch provided by Randy Bias (Thanks Randy!).
179
+ Only minimally tested so please let me know if this causes any problems for anyone.
180
+
181
+ From his notes:
182
+ I wanted to let you know that there appears to be a bug in how user data is passed. In
183
+ instances.rb the line that creates the base64 encoded package of user data is as follows:
184
+
185
+ userData = CGI::escape(Base64.encode64(in_params[:userData]).strip())
186
+
187
+ This may have worked prior, but at least right now the CGI::escape appears to cause a
188
+ breakage. More specifically it turns something like this (the actual base64 encoded string):
189
+
190
+ YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
191
+ eHh4eHh4eHh4eHh4eHh4eHh4eHgg
192
+
193
+ Into this:
194
+
195
+ YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh%
196
+ 0AeHh4eHh4eHh4eHh4eHh4eHh4eHgg
197
+
198
+ And it looks like EC2 chokes on the %0, which is CGI::escape's way for handling an EOL.
199
+ Unfortunately, the Base64.encode64 (at least in my 1.8.5 version of Ruby) inserts a
200
+ EOL at every 47 chars, no matter what:
201
+
202
+ ---
203
+ [randyb@master randyb] cat /tmp/foo.rb
204
+ #!/usr/bin/env ruby
205
+ #
206
+ require 'rubygems'
207
+ require_gem 'amazon-ec2'
208
+ require 'getoptlong'
209
+ puts Base64.encode64
210
+ ("012345678901234567890123456789012345678901234567890123456789")
211
+
212
+ [randyb@master randyb] /tmp/foo.rb
213
+ MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0
214
+ NTY3ODkwMTIzNDU2Nzg5
215
+ ---
216
+
217
+ The best way to handle this is to strip the CRLFs/EOLs before passing to CGI.escape.
218
+
219
+
220
+ === 0.0.5 2006-12-21
221
+
222
+ * Changes to home page documentation and example files to indicate that you
223
+ should use 'require_gem' instead of a simple require. Not sure if this
224
+ is a result of something I am doing in the packaging of the gem that is
225
+ incorrect or if this is right and proper. I will investigate further.
226
+ * Patched instances.rb run_instances method to allow for submission of
227
+ user data with the command to start an instance. Patch submitted
228
+ anonymously on RubyForge. This had not been functionally implemented
229
+ in the Amazon Web Services sample library prior to this patch.
230
+ * Added simple framework for adding unit tests for the library under test dir.
231
+ No functional unit tests exist yet.
232
+
233
+
234
+ === 0.0.4 2006-12-21
235
+
236
+ * Applied patch from Kevin Clark to svn version 7. Thanks for the
237
+ patch and the description Kevin! Please report if you
238
+ encounter any issues with this patched version. Here is Kevin's
239
+ description which I requested : "It helps me to think of modules as boxes full of classes and
240
+ methods. REXML is a module which holds various classes related to parsing XML
241
+ including REXML::Node, REXML::Document and REXML::XPath. When you
242
+ include a module it takes everything out of the box and places it in
243
+ the local context. So, when you include REXML in the global namespace
244
+ on line 27 of EC2.rb, it includes classes called Node, Document and XPath in the
245
+ global object space. This means that I can't have a class called Node
246
+ in my own project (which I do). The library would be a much better
247
+ neighbor if it instead accessed the REXML::Document and REXML::XPath
248
+ classes explicitly through the module."
249
+
250
+
251
+ === 0.0.3 2006-12-16
252
+
253
+ * API CHANGE : Changed method name 'authorize' to 'authorize_security_group_ingress' to ensure consistent
254
+ naming of Ruby library methods to match AWS EC2 API actions. Alias to 'authorize' for backwards compatibility.
255
+ * API CHANGE : Changed method name 'revoke' to 'revoke_security_group_ingress' to ensure consistent
256
+ naming of Ruby library methods to match AWS EC2 API actions. Alias to 'revoke' for backwards compatibility.
257
+ * API CHANGE : Changed method name 'delete_securitygroup' to 'delete_security_group' to ensure consistent
258
+ naming of Ruby library methods to match AWS EC2 API actions. Alias to 'delete_securitygroup' for backwards compatibility.
259
+ * API CHANGE : Changed method name 'describe_securitygroups' to 'describe_security_group' to ensure consistent
260
+ naming of Ruby library methods to match AWS EC2 API actions. Alias to 'describe_securitygroups' for backwards compatibility.
261
+ * API CHANGE : Changed method name 'create_securitygroup' to 'create_security_group' to ensure consistent
262
+ naming of Ruby library methods to match AWS EC2 API actions. Alias to 'create_securitygroup' for backwards compatibility.
263
+ * Added many API rdoc's, some method descriptions copied from Amazon Query API Developer Guide.
264
+ * Extracted some parts of the formerly monolithic EC2 library out into separate files for manageability.
265
+ * Changed the HTTP 'User-Agent' string used for each request so that we have our own user agent
266
+ to identify this library's calls. Now set the version # in the user agent string based on the
267
+ master version number for this library which is stored in lib/EC2/version.rb and should only
268
+ be defined in one place.
269
+ * Set @http.verify_mode = OpenSSL::SSL::VERIFY_NONE to avoid seeing SSL Cert warning
270
+ "warning: peer certificate won't be verified in this SSL session". File EC2.rb:96
271
+ * Make 'pathlist' utility method a private method (EC2.rb:111). No reason I can see for this to be exposed.
272
+
273
+
274
+ === 0.0.2 2006-12-14
275
+
276
+ * Bugfix in run_instances method. Method works now. Patch submitted by Stephen Caudill on AWS forums. Thanks!
277
+
278
+
279
+ === 0.0.1 2006-12-13
280
+
281
+ * Initial release of the Ruby Gem. This includes the version of the library exactly as provided by
282
+ Amazon Web Services as example code. No changes or enhancements to that code were made other than
283
+ packaging it as a Ruby Gem.
284
+ * RubyForge project created. http://github.com/grempe/amazon-ec2/tree/master
data/LICENSE ADDED
@@ -0,0 +1,66 @@
1
+ Copyright (c) 2007-2008 Glenn Rempe
2
+
3
+ This software is distributed under the Ruby License. A copy of which is
4
+ provided below.
5
+
6
+ RUBY LICENSE
7
+
8
+ http://www.ruby-lang.org/en/LICENSE.txt
9
+
10
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
11
+ You can redistribute it and/or modify it under either the terms of the GPL
12
+ (see COPYING.txt file), or the conditions below:
13
+
14
+ 1. You may make and give away verbatim copies of the source form of the
15
+ software without restriction, provided that you duplicate all of the
16
+ original copyright notices and associated disclaimers.
17
+
18
+ 2. You may modify your copy of the software in any way, provided that
19
+ you do at least ONE of the following:
20
+
21
+ a) place your modifications in the Public Domain or otherwise
22
+ make them Freely Available, such as by posting said
23
+ modifications to Usenet or an equivalent medium, or by allowing
24
+ the author to include your modifications in the software.
25
+
26
+ b) use the modified software only within your corporation or
27
+ organization.
28
+
29
+ c) rename any non-standard executables so the names do not conflict
30
+ with standard executables, which must also be provided.
31
+
32
+ d) make other distribution arrangements with the author.
33
+
34
+ 3. You may distribute the software in object code or executable
35
+ form, provided that you do at least ONE of the following:
36
+
37
+ a) distribute the executables and library files of the software,
38
+ together with instructions (in the manual page or equivalent)
39
+ on where to get the original distribution.
40
+
41
+ b) accompany the distribution with the machine-readable source of
42
+ the software.
43
+
44
+ c) give non-standard executables non-standard names, with
45
+ instructions on where to get the original software distribution.
46
+
47
+ d) make other distribution arrangements with the author.
48
+
49
+ 4. You may modify and include the part of the software into any other
50
+ software (possibly commercial). But some files in the distribution
51
+ are not written by the author, so that they are not under this terms.
52
+
53
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
54
+ files under the ./missing directory. See each file for the copying
55
+ condition.
56
+
57
+ 5. The scripts and library files supplied as input to or produced as
58
+ output from the software do not automatically fall under the
59
+ copyright of the software, but belong to whomever generated them,
60
+ and may be sold commercially, and may be aggregated with this
61
+ software.
62
+
63
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
64
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
65
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
66
+ PURPOSE.
@@ -0,0 +1,329 @@
1
+ = Amazon Web Services Elastic Compute Cloud (EC2) Ruby Gem
2
+
3
+ == About amazon-ec2
4
+
5
+ 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.
6
+
7
+ 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 applications written in your Ruby framework of choice (Ruby on Rails, Merb, etc.).
8
+
9
+ For the most complete and up-to date README information please visit the project homepage at:
10
+
11
+ http://github.com/grempe/amazon-ec2/tree/master
12
+
13
+ or the EC2 website at http://aws.amazon.com/ec2
14
+
15
+
16
+ == Installation
17
+
18
+ This gem follows the standard conventions for installation on any system with Ruby and RubyGems installed. If you have worked with gems before this will look very familiar.
19
+
20
+ === Get an AWS account
21
+
22
+ Before you can make use of this gem you will need an Amazon Web Services developer account which you can sign up for at https://aws-portal.amazon.com/gp/aws/developer/registration/index.html. This account must also be specifically enabled for Amazon EC2 usage. AWS will provide you with an 'AWS Access Key ID' and a 'Secret Access Key' which will allow you to authenticate any API calls you make and ensure correct billing to you for usage of the service. Take note of these (and keep them secret!).
23
+
24
+ === Install required gem pre-requisites
25
+
26
+ The following gems should be installed automatically as part of your install of amazon-ec2. Most of them are needed for testing 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.
27
+
28
+ XmlSimple (required)
29
+ Mocha (optional for testing)
30
+ Rcov (optional for testing)
31
+ Test-Spec (optional for testing)
32
+
33
+ === Install the amazon-ec2 gem
34
+
35
+ # ONE TIME ONLY
36
+ # Execute this on each machine where you install gems to add GitHub as a gem source
37
+ # Do this only once or you'll end up with multiple entries in 'gem sources'
38
+ gem sources -a http://gems.github.com/
39
+
40
+ # Install the stable release gem from RubyForge
41
+ sudo gem install amazon-ec2
42
+
43
+ # OR
44
+
45
+ # Install the gem from the GitHub bleeding edge as a normal runtime install
46
+ sudo gem install grempe-amazon-ec2
47
+
48
+ # OR
49
+
50
+ # Install the gem from the GitHub bleeding edge with all developer dependencies
51
+ sudo gem install grempe-amazon-ec2 --development
52
+
53
+ # OR
54
+
55
+ # Install using Rip (hellorip.com) instead of RubyGems
56
+ rip install git://github.com/grempe/amazon-ec2.git
57
+
58
+ == Using amazon-ec2
59
+
60
+ The library exposes one main interface class AWS::EC2::Base. It is through an instance of this class that you will perform all the operations for using the EC2 service including query string header signing.
61
+
62
+ The public methods on AWS::EC2::Base closely mirror the EC2 Query API, and as such the Query API Reference in the EC2 Developer Guide ( http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84 ) will prove helpful.
63
+
64
+
65
+ === Setting up
66
+
67
+ The 'ec2sh' and 'ec2-gem-example.rb' scripts which will be introduced to you shortly expect your AWS EC2 credentials to be stored as shell environment variables which are accessible to those scripts. This makes them convenient to use whenever you need to do a quick query to see what images you have available to you, what's running now, or to start or stop an 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 the setup steps will vary depending on your particular system and preferred shell). If you don't want to 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.
68
+
69
+ Edit the file ~/.bash_login and add the following to the existing contents:
70
+
71
+ export RUBYOPT="rubygems"
72
+
73
+ Why 'export RUBYOPT'? Because we leave loading libs up to you... See : http://gist.github.com/54177
74
+
75
+ # For amazon-ec2 and amazon s3 ruby gems
76
+ export AMAZON_ACCESS_KEY_ID="YOUR_ACCESS_KEY_ID"
77
+ export AMAZON_SECRET_ACCESS_KEY="YOUR_SECRET_ACCESS_KEY_ID"
78
+
79
+ If you are using EC2 in the EU region, make sure you also set:
80
+
81
+ export EC2_URL="https://eu-west-1.ec2.amazonaws.com"
82
+
83
+ (which you have already if you configured standard EC2 command line tools to work with this region).
84
+
85
+ Once you save the file you should close and re-open your 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 inadvertently expose your credentials to other users on your system.
86
+
87
+ 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.
88
+
89
+ === The basics
90
+
91
+ The library exposes one main interface module
92
+
93
+ AWS::EC2::Base
94
+
95
+ This method requires arguments which include your AWS credentials and it will return an object that you can use to make method calls directly against EC2. All the operations for using the EC2 service, including query string header signing, are handled automatically for you. The connection string will look something like this:
96
+
97
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
98
+
99
+ I've tried to keep the public methods on 'amazon-ec2' as close as possible to the AWS EC2 Query API. This similarity allows you to reference the Query API Reference in the EC2 Developer Guide and be able to get started right away. In most cases the methods names only differ in how they are presented. e.g. 'DescribeImages' becomes '#describe_images()' in Ruby. Feel free to browse the full RDoc documentation for all classes and methods of 'amazon-ec2' if you want more details.
100
+
101
+
102
+ === Examples
103
+
104
+ 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.
105
+
106
+ ==== Using the 'ec2-gem-example.rb' sample test script
107
+
108
+ 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 :
109
+
110
+ [your amazon-ec2 gem dir]/examples/ec2-example.rb
111
+
112
+ 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).
113
+
114
+ ==== Using the 'ec2sh' command shell
115
+
116
+ 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...
117
+
118
+ If you're not in front of a terminal shell now (perhaps you're browsing this site on your iPhone) this is what you would see:
119
+
120
+
121
+ hostname:/tmp/rails/amazon_test glenn$ ec2sh
122
+
123
+ 'ec2sh' usage :
124
+ This is an interactive 'irb' command shell that allows you to use all
125
+ commands available to the amazon-ec2 gem. You'll find this to be a
126
+ great tool to help you debug issues and practice running commands
127
+ against the live EC2 servers prior to putting them in your code.
128
+
129
+ The EC2 connection is wired to the class instance '@ec2'. Make method calls
130
+ on this to execute commands on EC2. Adding a #to_s
131
+ at the end of any command should give you a full String representation of the
132
+ response. The #xml data is available for each response
133
+ which allows you to view the full and complete XML response returned by
134
+ EC2 without any parsing applied. This is useful for viewing the
135
+ hierarchy of an entire response in a friendly way (if XML is friendly
136
+ to you!). Understanding the hierarchy of the XML response is critical
137
+ to making effective use of this library.
138
+
139
+ Examples to try:
140
+
141
+ returns : all ec2 public methods
142
+ >> @ec2.methods.sort
143
+
144
+ returns : a string representation of ALL images
145
+ >> @ec2.describe_images.to_s
146
+
147
+ returns : an Array of AWS::Response objects, each an EC2 image and its data
148
+ >> @ec2.describe_images.imagesSet.item
149
+ >> @ec2.describe_images.imagesSet.item[0] (a hash representing a single item in that array)
150
+ >> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that item)
151
+
152
+ >> @ec2.describe_images.imagesSet.item[0].to_s
153
+ => "#<AWS::Response:0x100A465B4 imageId=\"ami-018e6b68\" imageLocation=\"rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\" imageOwnerId=\"099034111737\" imageState=\"available\" isPublic=\"true\" parent=#<AWS::Response:0x100A469A6 ...>>"
154
+
155
+
156
+ === Ruby script usage example:
157
+
158
+ 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 (AWS::Response).
159
+
160
+ #!/usr/bin/env ruby
161
+
162
+ require 'rubygems'
163
+ require 'AWS'
164
+
165
+ ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
166
+ SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'
167
+
168
+ ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
169
+
170
+ puts "----- listing images owned by 'amazon' -----"
171
+ ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
172
+ # OpenStruct objects have members!
173
+ image.members.each do |member|
174
+ puts "#{member} => #{image[member]}"
175
+ end
176
+ end
177
+
178
+ === Ruby on Rails usage example:
179
+
180
+ <b>config/environment.rb</b>
181
+
182
+ Rails::Initializer.run do |config|
183
+ ...
184
+ # INSERT THIS LINE IN YOUR CONFIG
185
+ config.gem "amazon-ec2", :lib => "EC2"
186
+ ...
187
+ end
188
+
189
+
190
+ <b>app/controllers/my_controller.rb</b>
191
+
192
+ [some controller code ...]
193
+
194
+ ec2 = AWS::EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")
195
+
196
+ # get ALL public images
197
+ @ec2_images = ec2.describe_images().imagesSet.item
198
+
199
+ # Get info on all public EC2 images created by the Amazon EC2 team.
200
+ @ec2_images_amazon = ec2.describe_images(:owner_id => "amazon").imagesSet.item
201
+
202
+ [some more controller code ...]
203
+
204
+
205
+ <b>app/views/my/index.rhtml</b>
206
+
207
+ <h1>EC2 Test#index</h1>
208
+
209
+ <h1>Sample 1 - debug() view</h1>
210
+
211
+ <%= debug(@ec2_images_amazon) %>
212
+
213
+ <h1>Sample 2 - Build a table</h1>
214
+
215
+ <table border='1'>
216
+ <tr>
217
+ <th>image.imageId</th>
218
+ <th>image.imageLocation</th>
219
+ <th>image.imageOwnerId</th>
220
+ <th>image.imageState</th>
221
+ <th>image.isPublic</th>
222
+ </tr>
223
+
224
+ <% for image in @ec2_images_amazon %>
225
+ <tr>
226
+ <td><%=h image.imageId %></td>
227
+ <td><%=h image.imageLocation %></td>
228
+ <td><%=h image.imageOwnerId %></td>
229
+ <td><%=h image.imageState %></td>
230
+ <td><%=h image.isPublic %></td>
231
+ </tr>
232
+ <% end %>
233
+ </table>
234
+
235
+ <h1>Sample 3 - Iterate</h1>
236
+
237
+ <% @ec2_images_amazon.each do |image| %>
238
+ <% image.each_pair do |key, value| %>
239
+ <% unless key == 'parent' %>
240
+ <%= "#{key} => #{value}" %><br />
241
+ <% end %>
242
+ <% end %>
243
+ <br />
244
+ <% end %>
245
+
246
+
247
+ === Important notes regarding the structure of AWS::Response Objects
248
+
249
+ One of the key benefits of this new version of the library is that all responses from EC2 are bundled up in a real data structure and no longer require parsing of text. The hash returned is populated directly from the XML given to us by EC2 in response to any command we issue. This means that future changes to the API and what is returned by EC2 will be handled transparently by the gem. This is a huge benefit. What this means though, is that you may have to do a little homework on what actually gets returned by EC2 as XML. For example, when you make a #describe_images call in 'ec2sh' what AWS returns behind the scenes looks like:
250
+
251
+ <?xml version="1.0"?>
252
+ <DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2007-01-19/">
253
+ <imagesSet>
254
+ <item>
255
+ <imageId>ami-20b65349</imageId>
256
+ <imageLocation>ec2-public-images/fedora-core4-base.manifest.xml</imageLocation>
257
+ <imageState>available</imageState>
258
+ <imageOwnerId>amazon</imageOwnerId>
259
+ <isPublic>true</isPublic>
260
+ </item>
261
+ <item>
262
+ <imageId>ami-22b6534b</imageId>
263
+ <imageLocation>ec2-public-images/fedora-core4-mysql.manifest.xml</imageLocation>
264
+ <imageState>available</imageState>
265
+ <imageOwnerId>amazon</imageOwnerId>
266
+ <isPublic>true</isPublic>
267
+ </item>
268
+ <item>
269
+ <imageId>ami-23b6534a</imageId>
270
+ <imageLocation>ec2-public-images/fedora-core4-apache.manifest.xml</imageLocation>
271
+ <imageState>available</imageState>
272
+ <imageOwnerId>amazon</imageOwnerId>
273
+ <isPublic>true</isPublic>
274
+ </item>
275
+ <item>
276
+ <imageId>ami-25b6534c</imageId>
277
+ <imageLocation>ec2-public-images/fedora-core4-apache-mysql.manifest.xml</imageLocation>
278
+ <imageState>available</imageState>
279
+ <imageOwnerId>amazon</imageOwnerId>
280
+ <isPublic>true</isPublic>
281
+ </item>
282
+ <item>
283
+ <imageId>ami-26b6534f</imageId>
284
+ <imageLocation>ec2-public-images/developer-image.manifest.xml</imageLocation>
285
+ <imageState>available</imageState>
286
+ <imageOwnerId>amazon</imageOwnerId>
287
+ <isPublic>true</isPublic>
288
+ </item>
289
+ <item>
290
+ <imageId>ami-2bb65342</imageId>
291
+ <imageLocation>ec2-public-images/getting-started.manifest.xml</imageLocation>
292
+ <imageState>available</imageState>
293
+ <imageOwnerId>amazon</imageOwnerId>
294
+ <isPublic>true</isPublic>
295
+ </item>
296
+ </imagesSet>
297
+ </DescribeImagesResponse>
298
+
299
+ You can see here the XML the structure that you will need to follow when constructing queries for information and parsing responses from EC2.
300
+
301
+ 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:
302
+
303
+ >> puts @ec2.describe_images(:owner_id => 'amazon').imagesSet.item[2].imageId
304
+ ami-23b6534a
305
+
306
+ 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 AWS::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.
307
+
308
+
309
+ == Additional Resources
310
+
311
+ === Project Websites
312
+
313
+ * Project Home : http://github.com/grempe/amazon-ec2/tree/master
314
+ * Discussion Group : http://groups.google.com/group/amazon-ec2
315
+ * Report Bugs / Request Features : http://github.com/grempe/amazon-ec2/issues
316
+ * Amazon Web Services : http://aws.amazon.com
317
+
318
+ == Credits
319
+
320
+ The original code for this library was provided by Amazon Web Services, LLC as sample code. Thanks to them for providing all of us with something to get us started.
321
+
322
+ == Contact
323
+
324
+ Comments, patches, Git pull requests and bug reports are welcome. Send an email to mailto:glenn@rempe.us or use the Google Groups forum for this project.
325
+
326
+ Enjoy!
327
+
328
+ Glenn Rempe
329
+