Class: Awsome::Ec2::Instance

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property_hash) ⇒ Instance

Returns a new instance of Instance.



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

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.



4
5
6
# File 'lib/awsome/ec2/instance.rb', line 4

def properties
  @properties
end

Instance Method Details

#associate_ips(*elastic_ips) ⇒ Object



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

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

#deregister_from_elbsObject



66
67
68
# File 'lib/awsome/ec2/instance.rb', line 66

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

#install_hosts_entries(ip_address, *hostnames) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/awsome/ec2/instance.rb', line 42

def install_hosts_entries(ip_address, *hostnames)
  sed = []
  cmd = []

  # we will remove all hosts entries for the given "ip_address"
  sed << "sed '/^#{ip_address}/d"

  # we will remove all hosts entries for each of the given "hostnames"
  sed += hostnames.collect { |h| "sed '/ #{h} /d'" }

  cmd << "sudo cat /etc/hosts | #{sed.join(' | ')} > /etc/hosts.temp"
  cmd << "sudo echo '#{ip_address} #{hostnames.join(' ')} # GENERATED' >> /etc/hosts.temp"
  cmd << "sudo mv /etc/hosts.temp /etc/hosts"
  ssh(cmd)
end

#install_packages(*packages) ⇒ Object



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

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

#packagesObject



11
12
13
# File 'lib/awsome/ec2/instance.rb', line 11

def packages
  Awsome::Debian.describe_debian_packages(@properties['public_dns_name']).to_set
end

#reattach_volumes(*volumes) ⇒ Object



58
59
60
61
62
63
64
# 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) { Awsome::Ec2.volume_available?(info['id']) }
    Awsome::Ec2.attach_volume(info['id'], @properties['instance_id'], info['device']) 
  end
end

#register_with_elbs(*load_balancer_names) ⇒ Object



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

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

#remove_packages(*packages) ⇒ Object



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

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

#ssh(*args) ⇒ Object



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

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

#terminateObject



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

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

#volumesObject



15
16
17
18
19
# File 'lib/awsome/ec2/instance.rb', line 15

def volumes
  Awsome::Ec2.describe_attachments('attachment.instance-id' => @properties['instance_id']).collect do |p| 
    p['volume_id'] 
  end.to_set
end

#wait_for_ssh!Object



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

def wait_for_ssh!
  Awsome.wait_until(interval: 10) { has_ssh? }
end

#wait_until_running!Object



21
22
23
24
25
26
# File 'lib/awsome/ec2/instance.rb', line 21

def wait_until_running!
  Awsome.wait_until(interval: 10) do
    reload! 
    @properties['state'] =~ /^running/
  end
end