Class: Awsome::Ec2::Instance
- Inherits:
-
Object
- Object
- Awsome::Ec2::Instance
- Defined in:
- lib/awsome/ec2/instance.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Class Method Summary collapse
Instance Method Summary collapse
- #ami_id ⇒ Object
- #associate_cnames(*cnames) ⇒ Object
- #associate_ips(*elastic_ips) ⇒ Object
- #autoremove_packages ⇒ Object
- #availability_zone ⇒ Object
- #create_tags(tags) ⇒ Object
- #deregister_from_elbs ⇒ Object
- #detach_volumes(*volumes) ⇒ Object
- #elbs ⇒ Object
- #id ⇒ Object
-
#initialize(property_hash) ⇒ Instance
constructor
A new instance of Instance.
- #install_packages(*packages) ⇒ Object
- #instance_type ⇒ Object
- #key ⇒ Object
- #packages(options = {}) ⇒ Object
- #public_dns_name ⇒ Object
- #reattach_volumes(*volumes) ⇒ Object
- #register_with_elbs(*load_balancer_names) ⇒ Object
- #remove_packages(*packages) ⇒ Object
- #security_group_ids ⇒ Object
- #ssh(*args) ⇒ Object
- #terminate ⇒ Object
- #volumes(options = {}) ⇒ Object
- #wait_for_ssh! ⇒ Object
- #wait_until_running! ⇒ Object
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
#properties ⇒ Object (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_id ⇒ Object
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_packages ⇒ Object
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_zone ⇒ Object
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 () Awsome::Ec2.(@properties['instance_id'], ) end |
#deregister_from_elbs ⇒ Object
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 |
#elbs ⇒ Object
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 |
#id ⇒ Object
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_type ⇒ Object
150 151 152 |
# File 'lib/awsome/ec2/instance.rb', line 150 def instance_type @properties['instance_type'] end |
#key ⇒ Object
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(={}) return @cached_packages if @cached_packages && [:cached_ok] @cached_packages = Awsome::Debian.describe_debian_packages(@properties['public_dns_name']).to_set end |
#public_dns_name ⇒ Object
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_ids ⇒ Object
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 |
#terminate ⇒ Object
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(={}) return @cached_volumes if @cached_volumes && [:cached_ok] @cached_volumes = Awsome::Ec2.( '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 |