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
18 @@dns_server_conf_dir = Rails.configuration.dns_server_conf_dir
19 @@dns_server_conf_template = Rails.configuration.dns_server_conf_template
20 @@dns_server_reload_command = Rails.configuration.dns_server_reload_command
21 @@uuid_prefix = Rails.configuration.uuid_prefix
22 @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
23 @@nameservers = Rails.configuration.compute_node_nameservers
25 api_accessible :user, :extend => :common do |t|
32 t.add :api_job_uuid, as: :job_uuid
33 t.add :crunch_worker_state
36 api_accessible :superuser, :extend => :user do |t|
39 t.add lambda { |x| @@nameservers }, :as => :nameservers
47 job_readable ? job_uuid : nil
50 def crunch_worker_state
51 return 'down' if slot_number.nil?
52 case self.info.andand['slurm_state']
64 if Time.now - self.created_at > 5.minutes
69 elsif Time.now - self.last_ping_at > 1.hours
77 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
79 if o[:ping_secret] != self.info['ping_secret']
80 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info['ping_secret']}\""
81 raise ArvadosModel::UnauthorizedError.new("Incorrect ping_secret")
83 self.last_ping_at = Time.now
85 @bypass_arvados_authorization = true
88 if self.ip_address.nil?
89 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
90 self.ip_address = o[:ip]
91 self.first_ping_at = Time.now
94 # Record instance ID if not already known
95 if o[:ec2_instance_id]
96 if !self.info['ec2_instance_id']
97 self.info['ec2_instance_id'] = o[:ec2_instance_id]
98 elsif self.info['ec2_instance_id'] != o[:ec2_instance_id]
99 logger.debug "Multiple nodes have credentials for #{self.uuid}"
100 raise "#{self.uuid} is already running at #{self.info['ec2_instance_id']} so rejecting ping from #{o[:ec2_instance_id]}"
105 if self.slot_number.nil?
108 self.slot_number = try_slot
112 rescue ActiveRecord::RecordNotUnique
115 raise "No available node slots" if try_slot == MAX_SLOTS
117 self.hostname = self.class.hostname_for_slot(self.slot_number)
120 # Record other basic stats
121 ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key|
122 if value = (o[key] or o[key.to_sym])
123 self.properties[key] = value.to_i
125 self.properties.delete(key)
134 def ensure_ping_secret
135 self.info['ping_secret'] ||= rand(2**256).to_s(36)
138 def dns_server_update
139 if self.hostname_changed? or self.ip_address_changed?
140 if not self.ip_address.nil?
141 stale_conflicting_nodes = Node.where('id != ? and ip_address = ? and last_ping_at < ?',self.id,self.ip_address,10.minutes.ago)
142 if not stale_conflicting_nodes.empty?
143 # One or more stale compute node records have the same IP address as the new node.
144 # Clear the ip_address field on the stale nodes.
145 stale_conflicting_nodes.each do |stale_node|
146 stale_node.ip_address = nil
151 if self.hostname and self.ip_address
152 self.class.dns_server_update(self.hostname, self.ip_address)
157 def self.dns_server_update(hostname, ip_address)
158 return unless @@dns_server_conf_dir and @@dns_server_conf_template
159 ptr_domain = ip_address.
160 split('.').reverse.join('.').concat('.in-addr.arpa')
161 hostfile = File.join @@dns_server_conf_dir, "#{hostname}.conf"
164 template = IO.read(@@dns_server_conf_template)
166 STDERR.puts "Unable to read dns_server_conf_template #{@@dns_server_conf_template}: #{e.message}"
170 populated = template % {hostname:hostname, uuid_prefix:@@uuid_prefix, ip_address:ip_address, ptr_domain:ptr_domain}
173 File.open hostfile, 'w' do |f|
177 STDERR.puts "Unable to write #{hostfile}: #{e.message}"
180 File.open(File.join(@@dns_server_conf_dir, 'restart.txt'), 'w') do |f|
181 # this will trigger a dns server restart
182 f.puts @@dns_server_reload_command
186 def self.hostname_for_slot(slot_number)
187 "compute#{slot_number}"
190 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
191 # will refuse to start.
192 if @@dns_server_conf_dir and @@dns_server_conf_template
193 (0..MAX_SLOTS-1).each do |slot_number|
194 hostname = hostname_for_slot(slot_number)
195 hostfile = File.join @@dns_server_conf_dir, "#{hostname}.conf"
196 if !File.exists? hostfile
197 n = Node.where(:slot_number => slot_number).first
198 if n.nil? or n.ip_address.nil?
199 dns_server_update(hostname, '127.40.4.0')
201 dns_server_update(hostname, n.ip_address)
207 def permission_to_update
208 @bypass_arvados_authorization or super
211 def permission_to_create
212 current_user and current_user.is_admin