1 class Node < ActiveRecord::Base
4 before_validation :ensure_ping_secret
5 after_update :dnsmasq_update
9 @@confdir = if Rails.configuration.respond_to? :dnsmasq_conf_dir
10 Rails.configuration.dnsmasq_conf_dir
11 elsif File.exists? '/etc/dnsmasq.d/.'
24 if Time.now - self.created_at > 5.minutes
29 elsif Time.now - self.last_ping_at > 1.hours
37 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
39 if o[:ping_secret] != self.info[:ping_secret]
40 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
43 self.last_ping_at = Time.now
46 if self.ip_address.nil?
47 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
48 self.ip_address = o[:ip]
49 self.first_ping_at = Time.now
52 # Record instance ID if not already known
53 self.info[:ec2_instance_id] ||= o[:ec2_instance_id]
56 if self.slot_number.nil?
59 self.slot_number = try_slot
63 rescue ActiveRecord::RecordNotUnique
66 raise "No available node slots" if try_slot == MAX_SLOTS
68 self.hostname = self.class.hostname_for_slot(self.slot_number)
74 def start!(ping_url_method)
75 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
76 cmd = ["ec2-run-instances",
77 "--user-data '#{ping_url}'",
78 "-t c1.xlarge -n 1 -g orvos-compute",
81 self.info[:ec2_start_command] = cmd
82 logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
83 result = `#{cmd} 2>&1`
84 self.info[:ec2_start_result] = result
85 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
86 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
87 self.info[:ec2_instance_id] = m[1]
94 def ensure_ping_secret
95 self.info[:ping_secret] ||= rand(2**256).to_s(36)
99 if self.hostname_changed? or self.ip_address_changed?
100 if self.hostname and self.ip_address
101 self.class.dnsmasq_update(self.hostname, self.ip_address)
106 def self.dnsmasq_update(hostname, ip_address)
107 return unless @@confdir
108 ptr_domain = ip_address.
109 split('.').reverse.join('.').concat('.in-addr.arpa')
110 hostfile = File.join @@confdir, hostname
111 File.open hostfile, 'w' do |f|
112 f.puts "address=/#{hostname}/#{ip_address}"
113 f.puts "ptr-record=#{ptr_domain},#{hostname}"
115 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
116 # this should trigger a dnsmasq restart
120 def self.hostname_for_slot(slot_number)
121 "compute#{slot_number}"
124 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
125 # will refuse to start.
127 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
128 (0..MAX_SLOTS-1).each do |slot_number|
129 hostname = hostname_for_slot(slot_number)
130 hostfile = File.join @@confdir, hostname
131 if !File.exists? hostfile
132 dnsmasq_update(hostname, '127.40.4.0')