kerryb-amazon-ec2 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ # read the contents of the gemspec, eval it, and assign it to 'spec'
7
+ # this lets us maintain all gemspec info in one place. Nice and DRY.
8
+ spec = eval(IO.read("amazon-ec2.gemspec"))
9
+
10
+ Rake::GemPackageTask.new(spec) do |pkg|
11
+ pkg.gem_spec = spec
12
+ end
13
+
14
+ desc "Package and then install the gem locally"
15
+ task :install => [:package] do
16
+ sh %{sudo gem install pkg/#{GEM}-#{VER}}
17
+ end
18
+
19
+ desc "Package and then install the gem locally omitting documentation"
20
+ task :install_nodoc => [:package] do
21
+ sh %{sudo gem install --no-ri --no-rdoc pkg/#{GEM}-#{VER}}
22
+ end
23
+
24
+ Rake::TestTask.new do |t|
25
+ t.libs << "test"
26
+ t.test_files = FileList['test/test*.rb']
27
+ t.verbose = true
28
+ end
29
+
30
+ Rake::RDocTask.new do |rd|
31
+ rd.main = "README.rdoc"
32
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
33
+ rd.rdoc_dir = 'doc'
34
+ rd.options = spec.rdoc_options
35
+ end
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Amazon Web Services EC2 Query API Ruby library
4
+ #
5
+ # Ruby Gem Name:: amazon-ec2
6
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
7
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
8
+ # License:: Distributes under the same terms as Ruby
9
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
10
+ #++
11
+
12
+ require 'rubygems'
13
+ require File.dirname(__FILE__) + '/../lib/EC2'
14
+ require 'pp'
15
+
16
+ # pull these from the local shell environment variables set in ~/.bash_login
17
+ # or using appropriate methods specific to your login shell.
18
+ #
19
+ # e.g. in ~/.bash_login
20
+ #
21
+ # # For amazon-ec2 and amazon s3 ruby gems
22
+ # export AMAZON_ACCESS_KEY_ID="FOO"
23
+ # export AMAZON_SECRET_ACCESS_KEY="BAR"
24
+
25
+ ACCESS_KEY_ID = ENV['AMAZON_ACCESS_KEY_ID']
26
+ SECRET_ACCESS_KEY = ENV['AMAZON_SECRET_ACCESS_KEY']
27
+
28
+ if ACCESS_KEY_ID.nil? || ACCESS_KEY_ID.empty?
29
+ puts "Error : You must add the shell environment variables AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY before calling #{$0}!"
30
+ exit
31
+ end
32
+
33
+ # us-east-1.ec2.amazonaws.com == ec2.amazonaws.com
34
+ # eu-west-1.ec2.amazonaws.com for the european region
35
+ # test different servers by running something like:
36
+ # export EC2_URL='https://ec2.amazonaws.com';./bin/ec2-gem-example.rb
37
+ if ENV['EC2_URL']
38
+ ec2 = EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['EC2_URL']).host )
39
+ else
40
+ # default server is US ec2.amazonaws.com
41
+ ec2 = EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY )
42
+ end
43
+
44
+ puts "----- ec2.methods.sort -----"
45
+ p ec2.methods.sort
46
+
47
+ puts "----- listing images owned by 'amazon' -----"
48
+ ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
49
+ image.keys.each do |key|
50
+ puts "#{key} => #{image[key]}"
51
+ end
52
+ end
53
+
54
+ puts "----- listing all running instances -----"
55
+ pp ec2.describe_instances()
56
+
57
+ puts "----- creating a security group -----"
58
+ pp ec2.create_security_group(:group_name => "ec2-example-rb-test-group", :group_description => "ec-example.rb test group description.")
59
+
60
+ puts "----- listing security groups -----"
61
+ pp ec2.describe_security_groups()
62
+
63
+ puts "----- deleting a security group -----"
64
+ pp ec2.delete_security_group(:group_name => "ec2-example-rb-test-group")
65
+
66
+ puts "----- listing my keypairs (verbose mode) -----"
67
+ pp ec2.describe_keypairs()
data/bin/ec2sh ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Amazon Web Services EC2 Query API Ruby library
4
+ #
5
+ # Ruby Gem Name:: amazon-ec2
6
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
7
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
8
+ # License:: Distributes under the same terms as Ruby
9
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
10
+ #++
11
+
12
+ # CREDITS : Credit for this bit of shameful ripoff coolness
13
+ # goes to Marcel Molina and his AWS::S3 gem. Thanks!
14
+
15
+ # Usage : running this starts up an irb session and
16
+ # sets up the connection to EC2 as a class variable called
17
+ # '@ec2'. So just do something like the following on the
18
+ # shell command line:
19
+
20
+ # macbook-pro:~ glenn$ ec2sh
21
+ # >> @ec2.describe_images
22
+ # => [#<EC2::Item image_location...
23
+
24
+ ec2_lib = File.dirname(__FILE__) + '/../lib/EC2'
25
+ setup = File.dirname(__FILE__) + '/setup'
26
+ irb_name = RUBY_PLATFORM =~ /mswin32/ ? 'irb.bat' : 'irb'
27
+
28
+ if ( ENV['AMAZON_ACCESS_KEY_ID'] && ENV['AMAZON_SECRET_ACCESS_KEY'] )
29
+
30
+ welcome_message = <<-MESSAGE
31
+
32
+ 'ec2sh' usage :
33
+ This is an interactive 'irb' command shell that allows you to use all
34
+ commands available to the amazon-ec2 gem. You'll find this to be a
35
+ great tool to help you debug issues and practice running commands
36
+ against the live EC2 servers prior to putting them in your code.
37
+
38
+ The EC2 connection is wired to the class instance '@ec2'. Make method calls
39
+ on this to execute commands on EC2. Adding a #to_s
40
+ at the end of any command should give you a full String representation of the
41
+ response.
42
+
43
+ Examples to try:
44
+
45
+ returns : all ec2 public methods
46
+ >> @ec2.methods.sort
47
+
48
+ returns : a string representation of ALL images
49
+ >> @ec2.describe_images.to_s
50
+
51
+ returns : an Array of EC2::Response objects, each an EC2 image and its data
52
+ >> @ec2.describe_images.imagesSet.item
53
+ >> @ec2.describe_images.imagesSet.item[0] (an OpenStruct of a single item in that array)
54
+ >> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that OpenStruct item)
55
+
56
+ MESSAGE
57
+
58
+ puts welcome_message
59
+ exec "#{irb_name} -r #{ec2_lib} -r #{setup} --simple-prompt"
60
+ else
61
+ puts "You must define AMAZON_ACCESS_KEY_ID and AMAZON_SECRET_ACCESS_KEY as shell environment variables before running #{$0}!"
62
+ end
data/bin/setup.rb ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Amazon Web Services EC2 Query API Ruby library
4
+ #
5
+ # Ruby Gem Name:: amazon-ec2
6
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
7
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
8
+ # License:: Distributes under the same terms as Ruby
9
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
10
+ #++
11
+
12
+ if ENV['AMAZON_ACCESS_KEY_ID'] && ENV['AMAZON_SECRET_ACCESS_KEY']
13
+ opts = {
14
+ :access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
15
+ :secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY']
16
+ }
17
+ if ENV['EC2_URL']
18
+ opts[:server] = URI.parse(ENV['EC2_URL']).host
19
+ end
20
+ @ec2 = EC2::Base.new(opts)
21
+ end
22
+
23
+ puts "EC2 Server: #{opts[:server]}"
24
+
25
+ include EC2
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+ #Amazon Developer Guide Docs:
16
+ #
17
+ # The DescribeAvailabilityZones operation describes availability zones that are currently
18
+ # available to the account and their states.
19
+ #
20
+ # An optional list of zone names can be passed.
21
+ #
22
+ #Required Arguments:
23
+ #
24
+ # none
25
+ #
26
+ #Optional Arguments:
27
+ #
28
+ # :zone_name => Array (default : [])
29
+ #
30
+
31
+ def describe_availability_zones( options = {} )
32
+
33
+ options = { :zone_name => [] }.merge(options)
34
+
35
+ params = pathlist("ZoneName", options[:zone_name] )
36
+
37
+ return response_generator(:action => "DescribeAvailabilityZones", :params => params)
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+ #Amazon Developer Guide Docs:
16
+ #
17
+ # The GetConsoleOutput operation retrieves console output that has been posted for the specified instance.
18
+ #
19
+ # Instance console output is buffered and posted shortly after instance boot, reboot and once the instance
20
+ # is terminated. Only the most recent 64 KB of posted output is available. Console output is available for
21
+ # at least 1 hour after the most recent post.
22
+ #
23
+ #Required Arguments:
24
+ #
25
+ # :instance_id => String (default : "")
26
+ #
27
+ #Optional Arguments:
28
+ #
29
+ # none
30
+ #
31
+ def get_console_output( options ={} )
32
+
33
+ options = {:instance_id => ""}.merge(options)
34
+
35
+ raise ArgumentError, "No instance ID provided" if options[:instance_id].nil? || options[:instance_id].empty?
36
+
37
+ params = { "InstanceId" => options[:instance_id] }
38
+
39
+ return response_generator(:action => "GetConsoleOutput", :params => params)
40
+
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,153 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ class Base
14
+
15
+
16
+ #Amazon Developer Guide Docs:
17
+ #
18
+ # The AllocateAddress operation acquires an elastic IP address for use with your account.
19
+ #
20
+ #Required Arguments:
21
+ #
22
+ # none
23
+ #
24
+ #Optional Arguments:
25
+ #
26
+ # none
27
+ #
28
+ def allocate_address
29
+
30
+ return response_generator(:action => "AllocateAddress")
31
+
32
+ end
33
+
34
+ #Amazon Developer Guide Docs:
35
+ #
36
+ # The DescribeAddresses operation lists elastic IP addresses assigned to your account.
37
+ #
38
+ #Required Arguments:
39
+ #
40
+ # :public_ip => Array (default : [], can be empty)
41
+ #
42
+ #Optional Arguments:
43
+ #
44
+ # none
45
+ #
46
+ def describe_addresses( options = {} )
47
+
48
+ options = { :public_ip => [] }.merge(options)
49
+
50
+ params = pathlist("PublicIp", options[:public_ip])
51
+
52
+ return response_generator(:action => "DescribeAddresses", :params => params)
53
+
54
+ end
55
+
56
+ #Amazon Developer Guide Docs:
57
+ #
58
+ # The ReleaseAddress operation releases an elastic IP address associated with your account.
59
+ #
60
+ # If you run this operation on an elastic IP address that is already released, the address
61
+ # might be assigned to another account which will cause Amazon EC2 to return an error.
62
+ #
63
+ # Note : Releasing an IP address automatically disassociates it from any instance
64
+ # with which it is associated. For more information, see DisassociateAddress.
65
+ #
66
+ # Important! After releasing an elastic IP address, it is released to the IP
67
+ # address pool and might no longer be available to your account. Make sure
68
+ # to update your DNS records and any servers or devices that communicate
69
+ # with the address.
70
+ #
71
+ #Required Arguments:
72
+ #
73
+ # :public_ip => String (default : '')
74
+ #
75
+ #Optional Arguments:
76
+ #
77
+ # none
78
+ #
79
+ def release_address( options = {} )
80
+
81
+ options = { :public_ip => '' }.merge(options)
82
+
83
+ raise ArgumentError, "No ':public_ip' provided" if options[:public_ip].nil? || options[:public_ip].empty?
84
+
85
+ params = { "PublicIp" => options[:public_ip] }
86
+
87
+ return response_generator(:action => "ReleaseAddress", :params => params)
88
+
89
+ end
90
+
91
+ #Amazon Developer Guide Docs:
92
+ #
93
+ # The AssociateAddress operation associates an elastic IP address with an instance.
94
+ #
95
+ # If the IP address is currently assigned to another instance, the IP address
96
+ # is assigned to the new instance. This is an idempotent operation. If you enter
97
+ # it more than once, Amazon EC2 does not return an error.
98
+ #
99
+ #Required Arguments:
100
+ #
101
+ # :instance_id => String (default : '')
102
+ # :public_ip => String (default : '')
103
+ #
104
+ #Optional Arguments:
105
+ #
106
+ # none
107
+ #
108
+ def associate_address( options = {} )
109
+
110
+ options = { :instance_id => '', :public_ip => '' }.merge(options)
111
+
112
+ raise ArgumentError, "No ':instance_id' provided" if options[:instance_id].nil? || options[:instance_id].empty?
113
+ raise ArgumentError, "No ':public_ip' provided" if options[:public_ip].nil? || options[:public_ip].empty?
114
+
115
+ params = {
116
+ "InstanceId" => options[:instance_id],
117
+ "PublicIp" => options[:public_ip]
118
+ }
119
+
120
+ return response_generator(:action => "AssociateAddress", :params => params)
121
+
122
+ end
123
+
124
+ #Amazon Developer Guide Docs:
125
+ #
126
+ # The DisassociateAddress operation disassociates the specified elastic IP
127
+ # address from the instance to which it is assigned. This is an idempotent
128
+ # operation. If you enter it more than once, Amazon EC2 does not return
129
+ # an error.
130
+ #
131
+ #Required Arguments:
132
+ #
133
+ # :public_ip => String (default : '')
134
+ #
135
+ #Optional Arguments:
136
+ #
137
+ # none
138
+ #
139
+ def disassociate_address( options = {} )
140
+
141
+ options = { :public_ip => '' }.merge(options)
142
+
143
+ raise ArgumentError, "No ':public_ip' provided" if options[:public_ip].nil? || options[:public_ip].empty?
144
+
145
+ params = { "PublicIp" => options[:public_ip] }
146
+
147
+ return response_generator(:action => "DisassociateAddress", :params => params)
148
+
149
+ end
150
+
151
+ end
152
+
153
+ end
@@ -0,0 +1,147 @@
1
+ #--
2
+ # Amazon Web Services EC2 Query API Ruby library
3
+ #
4
+ # Ruby Gem Name:: amazon-ec2
5
+ # Author:: Glenn Rempe (mailto:glenn@rempe.us)
6
+ # Copyright:: Copyright (c) 2007-2008 Glenn Rempe
7
+ # License:: Distributes under the same terms as Ruby
8
+ # Home:: http://github.com/grempe/amazon-ec2/tree/master
9
+ #++
10
+
11
+ module EC2
12
+
13
+ # OUR CUSTOM ERROR CODES
14
+
15
+ # All of our errors are superclassed by Error < RuntimeError
16
+ class Error < RuntimeError #:nodoc:
17
+ end
18
+
19
+ # A client side only argument error
20
+ class ArgumentError < Error #:nodoc:
21
+ end
22
+
23
+
24
+ # AWS EC2 CLIENT ERROR CODES
25
+
26
+ # AWS EC2 can throw error exceptions that contain a '.' in them.
27
+ # since we can't name an exception class with that '.' I compressed
28
+ # each class name into the non-dot version. This allows us to retain
29
+ # the granularity of the exception.
30
+
31
+ # User not authorized.
32
+ class AuthFailure < Error #:nodoc:
33
+ end
34
+
35
+ # Invalid AWS Account
36
+ class InvalidClientTokenId < Error #:nodoc:
37
+ end
38
+
39
+ # Invalid Parameters for Value
40
+ class InvalidParameterValue < Error #:nodoc:
41
+ end
42
+
43
+ # Specified AMI has an unparsable manifest.
44
+ class InvalidManifest < Error #:nodoc:
45
+ end
46
+
47
+ # Specified AMI ID is not valid.
48
+ class InvalidAMIIDMalformed < Error #:nodoc:
49
+ end
50
+
51
+ # Specified AMI ID does not exist.
52
+ class InvalidAMIIDNotFound < Error #:nodoc:
53
+ end
54
+
55
+ # Specified AMI ID has been deregistered and is no longer available.
56
+ class InvalidAMIIDUnavailable < Error #:nodoc:
57
+ end
58
+
59
+ # Specified instance ID is not valid.
60
+ class InvalidInstanceIDMalformed < Error #:nodoc:
61
+ end
62
+
63
+ # Specified instance ID does not exist.
64
+ class InvalidInstanceIDNotFound < Error #:nodoc:
65
+ end
66
+
67
+ # Specified keypair name does not exist.
68
+ class InvalidKeyPairNotFound < Error #:nodoc:
69
+ end
70
+
71
+ # Attempt to create a duplicate keypair.
72
+ class InvalidKeyPairDuplicate < Error #:nodoc:
73
+ end
74
+
75
+ # Specified group name does not exist.
76
+ class InvalidGroupNotFound < Error #:nodoc:
77
+ end
78
+
79
+ # Attempt to create a duplicate group.
80
+ class InvalidGroupDuplicate < Error #:nodoc:
81
+ end
82
+
83
+ # Specified group can not be deleted because it is in use.
84
+ class InvalidGroupInUse < Error #:nodoc:
85
+ end
86
+
87
+ # Specified group name is a reserved name.
88
+ class InvalidGroupReserved < Error #:nodoc:
89
+ end
90
+
91
+ # Attempt to authorize a permission that has already been authorized.
92
+ class InvalidPermissionDuplicate < Error #:nodoc:
93
+ end
94
+
95
+ # Specified permission is invalid.
96
+ class InvalidPermissionMalformed < Error #:nodoc:
97
+ end
98
+
99
+ # Specified reservation ID is invalid.
100
+ class InvalidReservationIDMalformed < Error #:nodoc:
101
+ end
102
+
103
+ # Specified reservation ID does not exist.
104
+ class InvalidReservationIDNotFound < Error #:nodoc:
105
+ end
106
+
107
+ # User has reached max allowed concurrent running instances.
108
+ class InstanceLimitExceeded < Error #:nodoc:
109
+ end
110
+
111
+ # An invalid set of parameters were passed as arguments
112
+ # e.g. RunInstances was called with minCount and maxCount set to 0 or minCount > maxCount.
113
+ class InvalidParameterCombination < Error #:nodoc:
114
+ end
115
+
116
+ # An unknown parameter was passed as an argument
117
+ class UnknownParameter < Error #:nodoc:
118
+ end
119
+
120
+ # The user ID is neither in the form of an AWS account ID or one
121
+ # of the special values accepted by the owner or executableBy flags
122
+ # in the DescribeImages call.
123
+ class InvalidUserIDMalformed < Error #:nodoc:
124
+ end
125
+
126
+ # The value of an item added to, or removed from, an image attribute is invalid.
127
+ class InvalidAMIAttributeItemValue < Error #:nodoc:
128
+ end
129
+
130
+ # AWS EC2 SERVER ERROR CODES
131
+
132
+ # Internal AWS EC2 Error.
133
+ class InternalError < Error #:nodoc:
134
+ end
135
+
136
+ # There are not enough available instances to satify your minimum request.
137
+ class InsufficientInstanceCapacity < Error #:nodoc:
138
+ end
139
+
140
+ # The server is overloaded and cannot handle request.
141
+ class Unavailable < Error #:nodoc:
142
+ end
143
+
144
+ class SignatureDoesNotMatch < Error #:nodoc:
145
+ end
146
+
147
+ end