1 class Node < ArvadosModel
4 include CommonApiTemplate
6 serialize :properties, Hash
7 before_validation :ensure_ping_secret
8 after_update :dnsmasq_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 @@confdir = Rails.configuration.dnsmasq_conf_dir
19 @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
20 @@nameservers = Rails.configuration.compute_node_nameservers
22 api_accessible :user, :extend => :common do |t|
29 t.add :api_job_uuid, as: :job_uuid
30 t.add :crunch_worker_state
33 api_accessible :superuser, :extend => :user do |t|
36 t.add lambda { |x| @@nameservers }, :as => :nameservers
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 Time.now - self.created_at > 5.minutes
66 elsif Time.now - 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")
80 self.last_ping_at = Time.now
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 = Time.now
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 == MAX_SLOTS
114 self.hostname = self.class.hostname_for_slot(self.slot_number)
117 # Record other basic stats
118 ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key|
119 if value = (o[key] or o[key.to_sym])
120 self.properties[key] = value.to_i
122 self.properties.delete(key)
131 def ensure_ping_secret
132 self.info['ping_secret'] ||= rand(2**256).to_s(36)
136 if self.hostname_changed? or self.ip_address_changed?
137 if self.hostname and self.ip_address
138 self.class.dnsmasq_update(self.hostname, self.ip_address)
143 def self.dnsmasq_update(hostname, ip_address)
144 return unless @@confdir
145 ptr_domain = ip_address.
146 split('.').reverse.join('.').concat('.in-addr.arpa')
147 hostfile = File.join @@confdir, hostname
148 File.open hostfile, 'w' do |f|
149 f.puts "address=/#{hostname}/#{ip_address}"
150 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
151 f.puts "ptr-record=#{ptr_domain},#{hostname}"
153 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
154 # this should trigger a dnsmasq restart
158 def self.hostname_for_slot(slot_number)
159 "compute#{slot_number}"
162 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
163 # will refuse to start.
165 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
166 (0..MAX_SLOTS-1).each do |slot_number|
167 hostname = hostname_for_slot(slot_number)
168 hostfile = File.join @@confdir, hostname
169 if !File.exists? hostfile
170 dnsmasq_update(hostname, '127.40.4.0')
175 def permission_to_update
176 @bypass_arvados_authorization or super
179 def permission_to_create
180 current_user and current_user.is_admin