3 class Node < ArvadosModel
6 include CommonApiTemplate
8 serialize :properties, Hash
9 before_validation :ensure_ping_secret
10 after_update :dns_server_update
12 # Only a controller can figure out whether or not the current API tokens
13 # have access to the associated Job. They're expected to set
14 # job_readable=true if the Job UUID can be included in the API response.
15 belongs_to(:job, foreign_key: :job_uuid, primary_key: :uuid)
16 attr_accessor :job_readable
18 UNUSED_NODE_IP = '127.40.4.0'
20 api_accessible :user, :extend => :common do |t|
27 t.add :api_job_uuid, as: :job_uuid
28 t.add :crunch_worker_state
31 api_accessible :superuser, :extend => :user do |t|
34 t.add lambda { |x| Rails.configuration.compute_node_nameservers }, :as => :nameservers
38 @bypass_arvados_authorization = false
42 super || Rails.configuration.compute_node_domain
46 job_readable ? job_uuid : nil
49 def crunch_worker_state
50 return 'down' if slot_number.nil?
51 case self.info.andand['slurm_state']
52 when 'alloc', 'comp', 'mix', 'drng'
63 if db_current_time - self.created_at > 5.minutes
68 elsif db_current_time - self.last_ping_at > 1.hours
76 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
78 if o[:ping_secret] != self.info['ping_secret']
79 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info['ping_secret']}\""
80 raise ArvadosModel::UnauthorizedError.new("Incorrect ping_secret")
83 current_time = db_current_time
84 self.last_ping_at = current_time
86 @bypass_arvados_authorization = true
89 if self.ip_address.nil?
90 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
91 self.ip_address = o[:ip]
92 self.first_ping_at = current_time
95 # Record instance ID if not already known
96 if o[:ec2_instance_id]
97 if !self.info['ec2_instance_id']
98 self.info['ec2_instance_id'] = o[:ec2_instance_id]
99 elsif self.info['ec2_instance_id'] != o[:ec2_instance_id]
100 logger.debug "Multiple nodes have credentials for #{self.uuid}"
101 raise "#{self.uuid} is already running at #{self.info['ec2_instance_id']} so rejecting ping from #{o[:ec2_instance_id]}"
106 if self.slot_number.nil?
109 self.slot_number = try_slot
113 rescue ActiveRecord::RecordNotUnique
116 raise "No available node slots" if try_slot == Rails.configuration.max_compute_nodes
121 if self.hostname.nil? and Rails.configuration.assign_node_hostname
122 self.hostname = self.class.hostname_for_slot(self.slot_number)
125 # Record other basic stats
126 ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key|
127 if value = (o[key] or o[key.to_sym])
128 self.properties[key] = value.to_i
130 self.properties.delete(key)
139 def ensure_ping_secret
140 self.info['ping_secret'] ||= rand(2**256).to_s(36)
143 def dns_server_update
144 if ip_address_changed? && ip_address
145 Node.where('id != ? and ip_address = ?',
146 id, ip_address).each do |stale_node|
147 # One or more(!) stale node records have the same IP address
148 # as the new node. Clear the ip_address field on the stale
149 # nodes. Otherwise, we (via SLURM) might inadvertently connect
150 # to the new node using the old node's hostname.
151 stale_node.update_attributes!(ip_address: nil)
154 if hostname_was && hostname_changed?
155 self.class.dns_server_update(hostname_was, UNUSED_NODE_IP)
157 if hostname && (hostname_changed? || ip_address_changed?)
158 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
179 template = IO.read(Rails.configuration.dns_server_conf_template)
180 rescue IOError, SystemCallError => e
181 logger.error "Reading #{Rails.configuration.dns_server_conf_template}: #{e.message}"
185 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
186 Tempfile.open(["#{hostname}-", ".conf.tmp"],
187 Rails.configuration.dns_server_conf_dir) do |f|
189 f.puts template % template_vars
191 File.rename tmpfile, hostfile
192 rescue IOError, SystemCallError => e
193 logger.error "Writing #{hostfile}: #{e.message}"
196 if tmpfile and File.file? tmpfile
197 # Cleanup remaining temporary file.
203 if Rails.configuration.dns_server_update_command
204 cmd = Rails.configuration.dns_server_update_command % template_vars
206 logger.error "dns_server_update_command #{cmd.inspect} failed: #{$?}"
211 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_reload_command
212 restartfile = File.join(Rails.configuration.dns_server_conf_dir, 'restart.txt')
214 File.open(restartfile, 'w') do |f|
215 # Typically, this is used to trigger a dns server restart
216 f.puts Rails.configuration.dns_server_reload_command
218 rescue IOError, SystemCallError => e
219 logger.error "Unable to write #{restartfile}: #{e.message}"
227 def self.hostname_for_slot(slot_number)
228 config = Rails.configuration.assign_node_hostname
230 return nil if !config
232 sprintf(config, {:slot_number => slot_number})
235 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
236 # will refuse to start.
237 if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template and Rails.configuration.assign_node_hostname
238 (0..Rails.configuration.max_compute_nodes-1).each do |slot_number|
239 hostname = hostname_for_slot(slot_number)
240 hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
241 if !File.exist? hostfile
242 n = Node.where(:slot_number => slot_number).first
243 if n.nil? or n.ip_address.nil?
244 dns_server_update(hostname, UNUSED_NODE_IP)
246 dns_server_update(hostname, n.ip_address)
252 def permission_to_update
253 @bypass_arvados_authorization or super
256 def permission_to_create
257 current_user and current_user.is_admin