Class: Hugo::Aws::Ec2
- Inherits:
-
Object
- Object
- Hugo::Aws::Ec2
- Defined in:
- lib/hugo/aws/ec2.rb
Constant Summary collapse
- ACCESS_KEY =
ENV['AMAZON_ACCESS_KEY_ID']
- SECRET_KEY =
ENV['AMAZON_SECRET_ACCESS_KEY']
- KEY_NAME =
ENV['KEY_NAME']
- AMI =
ENV['EC2_AMI_ID'] || 'ami-1515f67c'
- ZONE =
"us-east-1c"
- TYPE =
"m1.small"
Instance Attribute Summary collapse
-
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
-
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
-
#create_time ⇒ Object
Returns the value of attribute create_time.
-
#image_id ⇒ Object
Returns the value of attribute image_id.
-
#key_name ⇒ Object
Returns the value of attribute key_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#security_group ⇒ Object
Returns the value of attribute security_group.
-
#status ⇒ Object
Returns the value of attribute status.
-
#type ⇒ Object
Returns the value of attribute type.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#zone ⇒ Object
Returns the value of attribute zone.
Class Method Summary collapse
-
.all(access_key_id, secret_access_key) ⇒ Object
def self.find_or_create_security_group(name, description) @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY) begin @security_groups = @ec2.describe_security_groups(:group_name => name) rescue @security_groups = @ec2.create_security_group(:group_name => name, :group_description => description) end @security_groups end.
- .find(instance, access_key_id, secret_access_key) ⇒ Object
-
.find_or_create(options) ⇒ Object
def self.find(url) @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY) self.new(@ec2.describe_instances(:instance_id => instance).reservationSet.item.instancesSet.item) end.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
-
#initialize(options = {}) ⇒ Ec2
constructor
A new instance of Ec2.
- #save ⇒ Object
- #set_attributes(options = {}) ⇒ Object
- #ssh(commands, dna = nil, key_pair_file = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Ec2
Returns a new instance of Ec2.
17 18 19 |
# File 'lib/hugo/aws/ec2.rb', line 17 def initialize( = {}) set_attributes() end |
Instance Attribute Details
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def aws_access_key_id @aws_access_key_id end |
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def aws_secret_access_key @aws_secret_access_key end |
#create_time ⇒ Object
Returns the value of attribute create_time.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def create_time @create_time end |
#image_id ⇒ Object
Returns the value of attribute image_id.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def image_id @image_id end |
#key_name ⇒ Object
Returns the value of attribute key_name.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def key_name @key_name end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def name @name end |
#security_group ⇒ Object
Returns the value of attribute security_group.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def security_group @security_group end |
#status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def status @status end |
#type ⇒ Object
Returns the value of attribute type.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def type @type end |
#uri ⇒ Object
Returns the value of attribute uri.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def uri @uri end |
#zone ⇒ Object
Returns the value of attribute zone.
13 14 15 |
# File 'lib/hugo/aws/ec2.rb', line 13 def zone @zone end |
Class Method Details
.all(access_key_id, secret_access_key) ⇒ Object
def self.find_or_create_security_group(name, description)
@ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
begin
@security_groups = @ec2.describe_security_groups(:group_name => name)
rescue
@security_groups = @ec2.create_security_group(:group_name => name, :group_description => description)
end
@security_groups
end
def self.destroy_security_group(name)
@ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
@ec2.delete_security_group(:group_name => name)
end
105 106 107 108 |
# File 'lib/hugo/aws/ec2.rb', line 105 def self.all(access_key_id, secret_access_key) @ec2 = AWS::EC2::Base.new(:access_key_id => access_key_id, :secret_access_key => secret_access_key) @ec2.describe_instances().reservationSet.item[0].instancesSet.item.map { |i| self.new(i) } end |
.find(instance, access_key_id, secret_access_key) ⇒ Object
110 111 112 113 114 |
# File 'lib/hugo/aws/ec2.rb', line 110 def self.find(instance, access_key_id, secret_access_key) raise ArgumentError, "Instance Required" unless instance @ec2 = AWS::EC2::Base.new(:access_key_id => access_key_id, :secret_access_key => secret_access_key) self.new(@ec2.describe_instances(:instance_id => instance).reservationSet.item[0].instancesSet.item[0]) end |
.find_or_create(options) ⇒ Object
def self.find(url)
@ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
self.new(@ec2.describe_instances(:instance_id => instance).reservationSet.item[0].instancesSet.item[0])
end
121 122 123 124 125 126 127 |
# File 'lib/hugo/aws/ec2.rb', line 121 def self.find_or_create() if [:name] self.find([:name], [:access_key_id], [:secret_access_key]) else self.new().create end end |
Instance Method Details
#create ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hugo/aws/ec2.rb', line 56 def create result = ec2.run_instances( :image_id => self.image_id, :key_name => self.key_name, :max_count => 1, :availability_zone => self.zone, :security_group => self.security_group) unless self.create_time set_attributes(result.instancesSet.item[0]) if result.instancesSet.item[0] self end |
#destroy ⇒ Object
67 68 69 |
# File 'lib/hugo/aws/ec2.rb', line 67 def destroy ec2.terminate_instances(:instance_id => self.name) end |
#save ⇒ Object
71 72 73 |
# File 'lib/hugo/aws/ec2.rb', line 71 def save self.create end |
#set_attributes(options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hugo/aws/ec2.rb', line 21 def set_attributes( = {}) @name = ["instanceId"] if ["placement"] and ["placement"]["availabilityZone"] @zone = ["placement"]["availabilityZone"] elsif [:zone] @zone = [:zone] else @zone = ZONE end @uri = ["dnsName"] || "" @type = [:type] || ["instanceType"] @image_id = [:image_id] || ["imageId"] @key_name = [:key_name] || ["keyName"] @create_time = ["launchTime"] || nil if ["instanceState"] and ["instanceState"]["name"] @status = ["instanceState"]["name"] else @status = "unknown" end @security_group = [:security_group] || "default" if ["groupSet"] and ["groupSet"]["item"] and ["groupSet"]["item"][0] @security_group = ["groupSet"]["item"][0]["groupId"] end @aws_access_key_id = [:aws_access_key_id] || ACCESS_KEY @aws_secret_access_key = [:aws_secret_access_key] || SECRET_KEY end |
#ssh(commands, dna = nil, key_pair_file = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hugo/aws/ec2.rb', line 75 def ssh(commands, dna=nil, key_pair_file=nil) raise ArgumentError.new("Key Pair File is required") if key_pair_file.nil? begin Net::SSH.start(self.uri, "ubuntu", :keys => key_pair_file) do |ssh| if dna ssh.exec!("echo \"#{dna.to_json.gsub('"','\"')}\" > ~/dna.json") end commands.each do |cmd| print ssh.exec!(cmd) end end rescue end end |