Class: Awsome::Ec2::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/awsome/ec2/instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property_hash) ⇒ Instance

Returns a new instance of Instance.



8
9
10
11
# File 'lib/awsome/ec2/instance.rb', line 8

def initialize(property_hash)
  raise 'properties must be a hash' unless property_hash.is_a?(Hash)
  @properties = property_hash.clone
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



6
7
8
# File 'lib/awsome/ec2/instance.rb', line 6

def properties
  @properties
end

Class Method Details

.to_table(instances, title = 'Instances') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/awsome/ec2/instance.rb', line 103

def self.to_table(instances, title='Instances')
  rows = []

  # add instance rows
  instances.each do |instance|
    rows << [
      instance.id,
      instance.public_dns_name,
      instance.packages.to_a.join("\n"),
      instance.volumes.to_a.join("\n"),
      instance.elbs.collect(&:name).join("\n"),
      instance.ami_id,
      instance.key,
      instance.instance_type,
      instance.availability_zone,
      instance.security_group_ids
    ]
    rows << :separator
  end

  # remove last unnecessary separator
  rows.pop if rows.any?

  headings = %w(id dns packages volumes elbs ami key type zone secgroup)
  Terminal::Table.new :headings => headings, :rows => rows, :title => title
end

Instance Method Details

#ami_idObject



142
143
144
# File 'lib/awsome/ec2/instance.rb', line 142

def ami_id
  @properties['ami_id']
end

#associate_cnames(*cnames) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/awsome/ec2/instance.rb', line 40

def associate_cnames(*cnames)
  cnames.each do |cname| 
    zone = cname['zone']
    (cname['private'] || []).each do |name| 
      Awsome::R53.redefine_cname(zone, name, @properties['private_dns_name'])
    end
    (cname['public'] || []).each do |name| 
      Awsome::R53.redefine_cname(zone, name, @properties['public_dns_name'])
    end
  end
end

#associate_ips(*elastic_ips) ⇒ Object



52
53
54
55
56
# File 'lib/awsome/ec2/instance.rb', line 52

def associate_ips(*elastic_ips)
  elastic_ips.each do |ip|
    Awsome::Ec2.associate_address(@properties['instance_id'], ip)
  end
end

#autoremove_packagesObject



91
92
93
# File 'lib/awsome/ec2/instance.rb', line 91

def autoremove_packages
  Awsome::Debian.autoremove_debian_packages(@properties['public_dns_name'])
end

#availability_zoneObject



154
155
156
# File 'lib/awsome/ec2/instance.rb', line 154

def availability_zone
  @properties['availability_zone']
end

#create_tags(tags) ⇒ Object



99
100
101
# File 'lib/awsome/ec2/instance.rb', line 99

def create_tags(tags)
  Awsome::Ec2.create_tags(@properties['instance_id'], tags)
end

#deregister_from_elbsObject



74
75
76
# File 'lib/awsome/ec2/instance.rb', line 74

def deregister_from_elbs
  elbs.each { |elb| elb.deregister(@properties['instance_id']) }
end

#detach_volumes(*volumes) ⇒ Object



68
69
70
71
72
# File 'lib/awsome/ec2/instance.rb', line 68

def detach_volumes(*volumes)
  volumes.each do |info|
    Awsome::Ec2.detach_volume(info['id'], info['dir'], info['preumount'])
  end
end

#elbsObject



130
131
132
# File 'lib/awsome/ec2/instance.rb', line 130

def elbs
  Awsome::Elb.describe_lbs.select { |elb| elb.instances.include?(@properties['instance_id']) }
end

#idObject



134
135
136
# File 'lib/awsome/ec2/instance.rb', line 134

def id
  @properties['instance_id']
end

#install_packages(*packages) ⇒ Object



87
88
89
# File 'lib/awsome/ec2/instance.rb', line 87

def install_packages(*packages)
  Awsome::Debian.install_debian_packages(@properties['public_dns_name'], *packages)
end

#instance_typeObject



150
151
152
# File 'lib/awsome/ec2/instance.rb', line 150

def instance_type
  @properties['instance_type']
end

#keyObject



146
147
148
# File 'lib/awsome/ec2/instance.rb', line 146

def key
  @properties['key']
end

#packages(options = {}) ⇒ Object



13
14
15
16
# File 'lib/awsome/ec2/instance.rb', line 13

def packages(options={})
  return @cached_packages if @cached_packages && options[:cached_ok]
  @cached_packages = Awsome::Debian.describe_debian_packages(@properties['public_dns_name']).to_set
end

#public_dns_nameObject



138
139
140
# File 'lib/awsome/ec2/instance.rb', line 138

def public_dns_name
  @properties['public_dns_name']
end

#reattach_volumes(*volumes) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/awsome/ec2/instance.rb', line 58

def reattach_volumes(*volumes)
  volumes.each do |info| 
    Awsome::Ec2.detach_volume(info['id'], info['dir'], info['preumount'])
    Awsome.wait_until(interval: 10, task: "waiting for volume #{info['id']} to become available") do 
      Awsome::Ec2.volume_available?(info['id'])
    end
    Awsome::Ec2.attach_volume(info['id'], @properties['instance_id'], info['device']) 
  end
end

#register_with_elbs(*load_balancer_names) ⇒ Object



78
79
80
81
# File 'lib/awsome/ec2/instance.rb', line 78

def register_with_elbs(*load_balancer_names)
  return if load_balancer_names.empty?
  Awsome::Elb.describe_lbs(*load_balancer_names).each { |elb| elb.register(@properties['instance_id']) }
end

#remove_packages(*packages) ⇒ Object



83
84
85
# File 'lib/awsome/ec2/instance.rb', line 83

def remove_packages(*packages)
  Awsome::Debian.remove_debian_packages(@properties['public_dns_name'], *packages)
end

#security_group_idsObject



158
159
160
# File 'lib/awsome/ec2/instance.rb', line 158

def security_group_ids
  @properties['security_group_ids']
end

#ssh(*args) ⇒ Object



36
37
38
# File 'lib/awsome/ec2/instance.rb', line 36

def ssh(*args)
  Awsome::Ssh.ssh(@properties['public_dns_name'], *args)
end

#terminateObject



95
96
97
# File 'lib/awsome/ec2/instance.rb', line 95

def terminate
  Awsome::Ec2.terminate_instances(@properties['instance_id'])
end

#volumes(options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/awsome/ec2/instance.rb', line 18

def volumes(options={})
  return @cached_volumes if @cached_volumes && options[:cached_ok]
  @cached_volumes = Awsome::Ec2.describe_attachments(
    'attachment.instance-id' => @properties['instance_id']
  ).collect{ |p| p['volume_id'] }.to_set
end

#wait_for_ssh!Object



32
33
34
# File 'lib/awsome/ec2/instance.rb', line 32

def wait_for_ssh!
  Awsome.wait_until(interval: 20, task: "waiting for instance #{id} ssh access") { has_ssh? }
end

#wait_until_running!Object



25
26
27
28
29
30
# File 'lib/awsome/ec2/instance.rb', line 25

def wait_until_running!
  Awsome.wait_until(interval: 20, task: "waiting for instance #{id} 'running' status") do
    reload! 
    @properties['state'] =~ /^running/
  end
end