Class: Hugo::Aws::Ec2

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Ec2

Returns a new instance of Ec2.



15
16
17
# File 'lib/hugo/aws/ec2.rb', line 15

def initialize(options = {})
  set_attributes(options)
end

Instance Attribute Details

#create_timeObject

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_idObject

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_nameObject

Returns the value of attribute key_name.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def key_name
  @key_name
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def name
  @name
end

#security_groupObject

Returns the value of attribute security_group.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def security_group
  @security_group
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def status
  @status
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def type
  @type
end

#uriObject

Returns the value of attribute uri.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def uri
  @uri
end

#zoneObject

Returns the value of attribute zone.



13
14
15
# File 'lib/hugo/aws/ec2.rb', line 13

def zone
  @zone
end

Class Method Details

.allObject

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



100
101
102
103
# File 'lib/hugo/aws/ec2.rb', line 100

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

.find(instance) ⇒ Object



105
106
107
108
# File 'lib/hugo/aws/ec2.rb', line 105

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



115
116
117
118
119
120
121
# File 'lib/hugo/aws/ec2.rb', line 115

def self.find_or_create(options)
  if options[:name]
    self.find(options[:name]) 
  else
    self.new(options).create
  end
end

Instance Method Details

#createObject



51
52
53
54
55
56
57
58
59
# 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,
    :security_group => self.security_group) unless self.create_time
  set_attributes(result.instancesSet.item[0]) if result.instancesSet.item[0]
  self
end

#destroyObject



61
62
63
64
# File 'lib/hugo/aws/ec2.rb', line 61

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

#saveObject



66
67
68
# File 'lib/hugo/aws/ec2.rb', line 66

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(options = {})
  @name = options["instanceId"] 

  if options["placement"] and options["placement"]["availabilityZone"]
    @zone = options["placement"]["availabilityZone"] 
  elsif options[:zone]
    @zone = options[:zone]
  else
    @zone = ZONE
  end

  @uri = options["dnsName"] || ""
  @type = options[:type] || options["instanceType"]
  @image_id = options[:image_id] || options["imageId"]
  @key_name = options[:key_name] || options["keyName"]

  @create_time = options["launchTime"] || nil

  if options["instanceState"] and options["instanceState"]["name"]
    @status = options["instanceState"]["name"]
  else
    @status = "unknown"
  end

  @security_group = options[:security_group] || "default"
  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

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hugo/aws/ec2.rb', line 70

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