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
20 Rails.configuration.compute_node_nameservers
22 [Net::HTTP.get(URI('http://169.254.169.254/latest/meta-data/local-ipv4')).
23 match(/^[\d\.]+$/)[0]]
26 api_accessible :superuser, :extend => :common do |t|
34 t.add lambda { |x| @@nameservers }, :as => :nameservers
48 if Time.now - self.created_at > 5.minutes
53 elsif Time.now - self.last_ping_at > 1.hours
61 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
63 if o[:ping_secret] != self.info[:ping_secret]
64 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
67 self.last_ping_at = Time.now
70 if self.ip_address.nil?
71 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
72 self.ip_address = o[:ip]
73 self.first_ping_at = Time.now
76 # Record instance ID if not already known
77 self.info[:ec2_instance_id] ||= o[:ec2_instance_id]
80 if self.slot_number.nil?
83 self.slot_number = try_slot
87 rescue ActiveRecord::RecordNotUnique
90 raise "No available node slots" if try_slot == MAX_SLOTS
92 self.hostname = self.class.hostname_for_slot(self.slot_number)
98 def start!(ping_url_method)
99 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
100 cmd = ["ec2-run-instances",
101 "--user-data '#{ping_url}'",
102 "-t c1.xlarge -n 1 -g orvos-compute",
105 self.info[:ec2_start_command] = cmd
106 logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
107 result = `#{cmd} 2>&1`
108 self.info[:ec2_start_result] = result
109 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
110 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
111 self.info[:ec2_instance_id] = m[1]
118 def ensure_ping_secret
119 self.info[:ping_secret] ||= rand(2**256).to_s(36)
123 if self.hostname_changed? or self.ip_address_changed?
124 if self.hostname and self.ip_address
125 self.class.dnsmasq_update(self.hostname, self.ip_address)
130 def self.dnsmasq_update(hostname, ip_address)
131 return unless @@confdir
132 ptr_domain = ip_address.
133 split('.').reverse.join('.').concat('.in-addr.arpa')
134 hostfile = File.join @@confdir, hostname
135 File.open hostfile, 'w' do |f|
136 f.puts "address=/#{hostname}/#{ip_address}"
137 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
138 f.puts "ptr-record=#{ptr_domain},#{hostname}"
140 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
141 # this should trigger a dnsmasq restart
145 def self.hostname_for_slot(slot_number)
146 "compute#{slot_number}"
149 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
150 # will refuse to start.
152 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
153 (0..MAX_SLOTS-1).each do |slot_number|
154 hostname = hostname_for_slot(slot_number)
155 hostfile = File.join @@confdir, hostname
156 if !File.exists? hostfile
157 dnsmasq_update(hostname, '127.40.4.0')