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.



17
18
19
# File 'lib/hugo/aws/ec2.rb', line 17

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

Instance Attribute Details

#aws_access_key_idObject

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_keyObject

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_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

.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

Raises:

  • (ArgumentError)


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(options)
  if options[:name]
    self.find(options[:name], options[:access_key_id], options[:secret_access_key]) 
  else
    self.new(options).create
  end
end

Instance Method Details

#createObject



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

#destroyObject



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

def destroy
  ec2.terminate_instances(:instance_id => self.name)
end

#saveObject



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(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
  
  @aws_access_key_id = options[:aws_access_key_id] || ACCESS_KEY
  @aws_secret_access_key = options[:aws_secret_access_key] || SECRET_KEY
  
end

#ssh(commands, dna = nil, key_pair_file = nil) ⇒ Object

Raises:

  • (ArgumentError)


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