Module Rudy::Routines::Handlers::Host

  1. lib/rudy/routines/handlers/host.rb

Public instance methods

is_available? (rset, port=22)
[show source]
    # File lib/rudy/routines/handlers/host.rb, line 70
70:     def is_available?(rset, port=22)
71:       raise NoMachines if rset.boxes.empty?
72:       rset.boxes.each do |rbox|
73:         mach = rbox.stash
74:         # This updates the DNS. It's important this happens
75:         # before and after the address is updated otherwise
76:         # certain errors will causes it to not be updated.  
77:         mach.refresh!
78:         msg = "Waiting for port #{port} on #{rbox.nickname} ..."
79:         port = 3389 if mach.windows?
80:         multi = mach.windows? ? 3 : 2
81:         interval, max = 1*multi, 30*multi
82:         Rudy::Utils.waiter(interval, max, STDOUT, msg, 0) {
83:           Rudy::Utils.service_available?(mach.dns_public, port)
84:         }
85:       end
86:     end
is_running? (rset)

NOTE: This handler doesn’t use Rudy::Routines.add_handler

[show source]
    # File lib/rudy/routines/handlers/host.rb, line 9
 9:     def is_running?(rset)
10:       raise NoMachines if rset.boxes.empty?
11:       rset.boxes.each do |rbox|
12:         msg = "Waiting for #{rbox.nickname} to boot..."
13:         multi = rbox.stash.windows? ? 6 : 3
14:         interval, max = 1*multi, 80*multi
15:         Rudy::Utils.waiter(interval, max, Rudy::Huxtable.logger, msg, 0) {
16:           inst = rbox.stash.get_instance
17:           inst && inst.running?
18:         }
19:       end
20:     end
set_hostname (rset)
[show source]
     # File lib/rudy/routines/handlers/host.rb, line 88
 88:     def set_hostname(rset)
 89:       raise NoMachines if rset.boxes.empty?
 90:       
 91:       # Set the hostname if specified in the machines config. 
 92:       # :rudy -> change to Rudy's machine name
 93:       # :default -> leave the hostname as it is
 94:       # Anything else other than nil -> change to that value
 95:       # NOTE: This will set hostname every time a routine is
 96:       # run so we may want to make this an explicit action.
 97:       hntype = current_machine_hostname || :rudy
 98:       return if hntype.to_s.to_sym == :default
 99:       rset.batch do
100:         unless self.stash.os == :windows
101:           hn = hntype == :rudy ? self.stash.name : hntype
102:           if self.user.to_s == 'root'  # ubuntu has a root user
103:             hostname hn
104:           else
105:             sudo do
106:               hostname hn
107:             end
108:           end
109:         end
110:       end
111:     end
update_dns (rset)

Add instance info to machine and save it. This is really important for the initial startup so the metadata is updated right away. But it’s also important to call here because if a routine was executed and an unexpected exception occurs before this update is executed the machine metadata won’t contain the DNS information. Calling it here ensures that the metadata is always up-to-date.

If a machine has an associated elastic IP address, it will also be assigned in this step.

Each Rye:Box instance has a Rudy::Machine instance in its stash so rbox.stash.refresh! == machine.refresh!

[show source]
    # File lib/rudy/routines/handlers/host.rb, line 34
34:     def update_dns(rset)
35:       raise NoMachines if rset.boxes.empty?
36:       rset.boxes.each do |rbox|
37:         mach = rbox.stash
38:         # Assign IP address only if we have one for that position
39:         if !mach.address.nil? && !mach.address.empty?
40:           begin
41:             # Make sure the address is associated to the current account
42:             if Rudy::AWS::EC2::Addresses.exists?(mach.address)
43:               li "Associating #{mach.address} to #{mach.instid}"
44:               Rudy::AWS::EC2::Addresses.associate(mach.address, mach.instid)
45:             else
46:               le "Unknown address: #{mach.address}"
47:             end
48:           rescue => ex
49:             le "Error associating address: #{ex.message}"
50:             ld ex.backtrace
51:           end
52:         end
53:         
54:         # Give EC2 some time to update their metadata
55:         msg = "Waiting for public DNS on #{rbox.nickname} ..."
56:         multi = rbox.stash.windows? ? 3 : 2
57:         interval, max = 2*multi, 60*multi
58:         Rudy::Utils.waiter(interval, max, STDOUT, msg, 0) {
59:           mach.refresh!
60:           if mach.address
61:             mach.dns_public.to_s =~ /#{mach.address.to_s.gsub('.', '-')}/
62:           else
63:             !mach.dns_public.nil? && !mach.dns_public.empty?
64:           end
65:         }
66:         rbox.host = mach.dns_public
67:       end
68:     end