1 class Node < ArvadosModel
4 include CommonApiTemplate
6 serialize :properties, Hash
7 before_validation :ensure_ping_secret
8 after_update :dns_server_update
10 # Only a controller can figure out whether or not the current API tokens
11 # have access to the associated Job. They're expected to set
12 # job_readable=true if the Job UUID can be included in the API response.
13 belongs_to(:job, foreign_key: :job_uuid, primary_key: :uuid)
14 attr_accessor :job_readable
16 UNUSED_NODE_IP = '127.40.4.0'
18 api_accessible :user, :extend => :common do |t|
25 t.add :api_job_uuid, as: :job_uuid
26 t.add :crunch_worker_state
29 api_accessible :superuser, :extend => :user do |t|
32 t.add lambda { |x| Rails.configuration.compute_node_nameservers }, :as => :nameservers
36 @bypass_arvados_authorization = false
40 super || Rails.configuration.compute_node_domain
44 job_readable ? job_uuid : nil
47 def crunch_worker_state
48 return 'down' if slot_number.nil?
49 case self.info.andand['slurm_state']
61 if db_current_time - self.created_at > 5.minutes
66 elsif db_current_time - self.last_ping_at > 1.hours
74 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
76 if o[:ping_secret] != self.info['ping_secret']
77 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info['ping_secret']}\""
78 raise ArvadosModel::UnauthorizedError.new("Incorrect ping_secret")
81 current_time = db_current_time
82 self.last_ping_at = current_time
84 @bypass_arvados_authorization = true
87 if self.ip_address.nil?
88 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
89 self.ip_address = o[:ip]
90 self.first_ping_at = current_time
93 # Record instance ID if not already known
94 if o[:ec2_instance_id]
95 if !self.info['ec2_instance_id']
96 self.info['ec2_instance_id'] = o[:ec2_instance_id]
97 elsif self.info['ec2_instance_id'] != o[:ec2_instance_id]
98 logger.debug "Multiple nodes have credentials for #{self.uuid}"
99 raise "#{self.uuid} is already running at #{self.info['ec2_instance_id']} so rejecting ping from #{o[:ec2_instance_id]}"
104 if self.slot_number.nil?
107 self.slot_number = try_slot
111 rescue ActiveRecord::RecordNotUnique
114 raise "No available node slots" if try_slot == Rails.configuration.max_compute_nodes
119 if self.hostname.nil? and Rails.configuration.assign_node_hostname
120 self.hostname = self.class.hostname_for_slot(self.slot_number)
123 # Record other basic stats
124 ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key|
125 if value = (o[key] or o[key.to_sym])
126 self.properties[key] = value.to_i
128 self.properties.delete(key)
137 def ensure_ping_secret
138 self.info['ping_secret'] ||= rand(2**256).to_s(36)
141 def dns_server_update
142 if hostname_changed? && hostname_was
143 self.class.dns_server_update(hostname_was, UNUSED_NODE_IP)
145 if hostname_changed? or ip_address_changed?
147 Node.where('id != ? and ip_address = ? and last_ping_at < ?',
148 id, ip_address, 10.minutes.ago).each do |stale_node|
149 # One or more stale compute node records have the same IP
150 # address as the new node. Clear the ip_address field on
152 stale_node.ip_address = nil
157 self.class.dns_server_update(hostname, ip_address || UNUSED_NODE_IP)
162 def self.dns_server_update hostname, ip_address
165 ptr_domain = ip_address.
166 split('.').reverse.join('.').concat('.in-addr.arpa')
170 uuid_prefix: Rails.configuration.uuid_prefix,
171 ip_address: ip_address,
172 ptr_domain: ptr_domain,
175 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template
178 template = IO.read(Rails.configuration.dns_server_conf_template)
180 logger.error "Reading #{Rails.configuration.dns_server_conf_template}: #{e.message}"
184 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
185 File.open hostfile+'.tmp', 'w' do |f|
186 f.puts template % template_vars
188 File.rename hostfile+'.tmp', hostfile
190 logger.error "Writing #{hostfile}: #{e.message}"
195 if Rails.configuration.dns_server_update_command
196 cmd = Rails.configuration.dns_server_update_command % template_vars
198 logger.error "dns_server_update_command #{cmd.inspect} failed: #{$?}"
203 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_reload_command
204 restartfile = File.join(Rails.configuration.dns_server_conf_dir, 'restart.txt')
206 File.open(restartfile, 'w') do |f|
207 # Typically, this is used to trigger a dns server restart
208 f.puts Rails.configuration.dns_server_reload_command
211 logger.error "Unable to write #{restartfile}: #{e.message}"
219 def self.hostname_for_slot(slot_number)
220 config = Rails.configuration.assign_node_hostname
222 return nil if !config
224 sprintf(config, {:slot_number => slot_number})
227 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
228 # will refuse to start.
229 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template and Rails.configuration.assign_node_hostname
230 (0..Rails.configuration.max_compute_nodes-1).each do |slot_number|
231 hostname = hostname_for_slot(slot_number)
232 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
233 if !File.exist? hostfile
234 n = Node.where(:slot_number => slot_number).first
235 if n.nil? or n.ip_address.nil?
236 dns_server_update(hostname, UNUSED_NODE_IP)
238 dns_server_update(hostname, n.ip_address)
244 def permission_to_update
245 @bypass_arvados_authorization or super
248 def permission_to_create
249 current_user and current_user.is_admin