1 class Node < ActiveRecord::Base
4 include CommonApiTemplate
6 before_validation :ensure_ping_secret
7 after_update :dnsmasq_update
11 @@confdir = if Rails.configuration.respond_to? :dnsmasq_conf_dir
12 Rails.configuration.dnsmasq_conf_dir
13 elsif File.exists? '/etc/dnsmasq.d/.'
18 @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
19 @@nameservers = Rails.configuration.compute_node_nameservers
21 api_accessible :superuser, :extend => :common do |t|
29 t.add lambda { |x| @@nameservers }, :as => :nameservers
43 if Time.now - self.created_at > 5.minutes
48 elsif Time.now - self.last_ping_at > 1.hours
56 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
58 if o[:ping_secret] != self.info[:ping_secret]
59 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
62 self.last_ping_at = Time.now
65 if self.ip_address.nil?
66 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
67 self.ip_address = o[:ip]
68 self.first_ping_at = Time.now
71 # Record instance ID if not already known
72 self.info[:ec2_instance_id] ||= o[:ec2_instance_id]
75 if self.slot_number.nil?
78 self.slot_number = try_slot
82 rescue ActiveRecord::RecordNotUnique
85 raise "No available node slots" if try_slot == MAX_SLOTS
87 self.hostname = self.class.hostname_for_slot(self.slot_number)
93 def start!(ping_url_method)
94 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
95 cmd = ["ec2-run-instances",
96 "--user-data '#{ping_url}'",
97 "-t c1.xlarge -n 1 -g orvos-compute",
100 self.info[:ec2_start_command] = cmd
101 logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
102 result = `#{cmd} 2>&1`
103 self.info[:ec2_start_result] = result
104 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
105 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
106 self.info[:ec2_instance_id] = m[1]
113 def ensure_ping_secret
114 self.info[:ping_secret] ||= rand(2**256).to_s(36)
118 if self.hostname_changed? or self.ip_address_changed?
119 if self.hostname and self.ip_address
120 self.class.dnsmasq_update(self.hostname, self.ip_address)
125 def self.dnsmasq_update(hostname, ip_address)
126 return unless @@confdir
127 ptr_domain = ip_address.
128 split('.').reverse.join('.').concat('.in-addr.arpa')
129 hostfile = File.join @@confdir, hostname
130 File.open hostfile, 'w' do |f|
131 f.puts "address=/#{hostname}/#{ip_address}"
132 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
133 f.puts "ptr-record=#{ptr_domain},#{hostname}"
135 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
136 # this should trigger a dnsmasq restart
140 def self.hostname_for_slot(slot_number)
141 "compute#{slot_number}"
144 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
145 # will refuse to start.
147 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
148 (0..MAX_SLOTS-1).each do |slot_number|
149 hostname = hostname_for_slot(slot_number)
150 hostfile = File.join @@confdir, hostname
151 if !File.exists? hostfile
152 dnsmasq_update(hostname, '127.40.4.0')