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
-
#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 ⇒ Object
- .destroy_security_group(name) ⇒ Object
- .find(instance) ⇒ 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.
- .find_or_create_security_group(name, description) ⇒ Object
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.
15 16 17 |
# File 'lib/hugo/aws/ec2.rb', line 15 def initialize( = {}) set_attributes() end |
Instance Attribute Details
#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 ⇒ Object
99 100 101 102 |
# File 'lib/hugo/aws/ec2.rb', line 99 def self.all @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY) @ec2.describe_instances().reservationSet.item[0].instancesSet.item.map { |i| self.new(i) } end |
.destroy_security_group(name) ⇒ Object
94 95 96 97 |
# File 'lib/hugo/aws/ec2.rb', line 94 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 |
.find(instance) ⇒ Object
104 105 106 107 |
# File 'lib/hugo/aws/ec2.rb', line 104 def self.find(instance) @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 |
.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
114 115 116 117 118 119 120 |
# File 'lib/hugo/aws/ec2.rb', line 114 def self.find_or_create() if [:name] self.find([:name]) else self.new().create end end |
.find_or_create_security_group(name, description) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/hugo/aws/ec2.rb', line 84 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 |
Instance Method Details
#create ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/hugo/aws/ec2.rb', line 51 def create @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY) result = @ec2.run_instances(:image_id => self.image_id, :key_name => self.key_name, :max_count => 1, :availability_zone => self.zone) unless self.create_time set_attributes(result.instancesSet.item[0]) if result.instancesSet.item[0] self end |
#destroy ⇒ Object
60 61 62 63 |
# File 'lib/hugo/aws/ec2.rb', line 60 def destroy @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY) @ec2.terminate_instances(:instance_id => self.name) end |
#save ⇒ Object
65 66 67 |
# File 'lib/hugo/aws/ec2.rb', line 65 def save self.create end |
#set_attributes(options = {}) ⇒ Object
19 20 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 |
# File 'lib/hugo/aws/ec2.rb', line 19 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 = options[:security_group] || nil # if options["groupSet"] and options["groupSet"]["item"] and options["groupSet"]["item"][0] # @security_group = options["groupSet"]["item"][0]["groupId"] # end end |
#ssh(commands, dna = nil, key_pair_file = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hugo/aws/ec2.rb', line 69 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| puts ssh.exec!(cmd) end end rescue end end |