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 api_accessible :user, :extend => :common do |t|
23 t.add :api_job_uuid, as: :job_uuid
24 t.add :crunch_worker_state
27 api_accessible :superuser, :extend => :user do |t|
30 t.add lambda { |x| Rails.configuration.compute_node_nameservers }, :as => :nameservers
34 @bypass_arvados_authorization = false
38 super || Rails.configuration.compute_node_domain
42 job_readable ? job_uuid : nil
45 def crunch_worker_state
46 return 'down' if slot_number.nil?
47 case self.info.andand['slurm_state']
59 if db_current_time - self.created_at > 5.minutes
64 elsif db_current_time - self.last_ping_at > 1.hours
72 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
74 if o[:ping_secret] != self.info['ping_secret']
75 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info['ping_secret']}\""
76 raise ArvadosModel::UnauthorizedError.new("Incorrect ping_secret")
79 current_time = db_current_time
80 self.last_ping_at = current_time
82 @bypass_arvados_authorization = true
85 if self.ip_address.nil?
86 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
87 self.ip_address = o[:ip]
88 self.first_ping_at = current_time
91 # Record instance ID if not already known
92 if o[:ec2_instance_id]
93 if !self.info['ec2_instance_id']
94 self.info['ec2_instance_id'] = o[:ec2_instance_id]
95 elsif self.info['ec2_instance_id'] != o[:ec2_instance_id]
96 logger.debug "Multiple nodes have credentials for #{self.uuid}"
97 raise "#{self.uuid} is already running at #{self.info['ec2_instance_id']} so rejecting ping from #{o[:ec2_instance_id]}"
102 if self.slot_number.nil?
105 self.slot_number = try_slot
109 rescue ActiveRecord::RecordNotUnique
112 raise "No available node slots" if try_slot == Rails.configuration.max_compute_nodes
117 if self.hostname.nil? and Rails.configuration.assign_node_hostname
118 self.hostname = self.class.hostname_for_slot(self.slot_number)
121 # Record other basic stats
122 ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key|
123 if value = (o[key] or o[key.to_sym])
124 self.properties[key] = value.to_i
126 self.properties.delete(key)
135 def ensure_ping_secret
136 self.info['ping_secret'] ||= rand(2**256).to_s(36)
139 def dns_server_update
140 if self.hostname_changed? or self.ip_address_changed?
141 if not self.ip_address.nil?
142 stale_conflicting_nodes = Node.where('id != ? and ip_address = ? and last_ping_at < ?',self.id,self.ip_address,10.minutes.ago)
143 if not stale_conflicting_nodes.empty?
144 # One or more stale compute node records have the same IP address as the new node.
145 # Clear the ip_address field on the stale nodes.
146 stale_conflicting_nodes.each do |stale_node|
147 stale_node.ip_address = nil
152 if self.hostname and self.ip_address
153 self.class.dns_server_update(self.hostname, self.ip_address)
158 def self.dns_server_update hostname, ip_address
161 ptr_domain = ip_address.
162 split('.').reverse.join('.').concat('.in-addr.arpa')
166 uuid_prefix: Rails.configuration.uuid_prefix,
167 ip_address: ip_address,
168 ptr_domain: ptr_domain,
171 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template
174 template = IO.read(Rails.configuration.dns_server_conf_template)
176 logger.error "Reading #{Rails.configuration.dns_server_conf_template}: #{e.message}"
180 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
181 File.open hostfile+'.tmp', 'w' do |f|
182 f.puts template % template_vars
184 File.rename hostfile+'.tmp', hostfile
186 logger.error "Writing #{hostfile}: #{e.message}"
191 if Rails.configuration.dns_server_update_command
192 cmd = Rails.configuration.dns_server_update_command % template_vars
194 logger.error "dns_server_update_command #{cmd.inspect} failed: #{$?}"
199 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_reload_command
200 restartfile = File.join(Rails.configuration.dns_server_conf_dir, 'restart.txt')
202 File.open(restartfile, 'w') do |f|
203 # Typically, this is used to trigger a dns server restart
204 f.puts Rails.configuration.dns_server_reload_command
207 logger.error "Unable to write #{restartfile}: #{e.message}"
215 def self.hostname_for_slot(slot_number)
216 config = Rails.configuration.assign_node_hostname
218 return nil if !config
220 sprintf(config, {:slot_number => slot_number})
223 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
224 # will refuse to start.
225 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template and Rails.configuration.assign_node_hostname
226 (0..Rails.configuration.max_compute_nodes-1).each do |slot_number|
227 hostname = hostname_for_slot(slot_number)
228 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
229 if !File.exist? hostfile
230 n = Node.where(:slot_number => slot_number).first
231 if n.nil? or n.ip_address.nil?
232 dns_server_update(hostname, '127.40.4.0')
234 dns_server_update(hostname, n.ip_address)
240 def permission_to_update
241 @bypass_arvados_authorization or super
244 def permission_to_create
245 current_user and current_user.is_admin