1 class Node < ArvadosModel
4 include CommonApiTemplate
6 before_validation :ensure_ping_secret
7 after_update :dnsmasq_update
11 @@confdir = Rails.configuration.dnsmasq_conf_dir
12 @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
13 @@nameservers = Rails.configuration.compute_node_nameservers
15 api_accessible :user, :extend => :common do |t|
22 t.add :crunch_worker_state
24 api_accessible :superuser, :extend => :user do |t|
27 t.add lambda { |x| @@nameservers }, :as => :nameservers
39 def crunch_worker_state
40 case self.info.andand[:slurm_state]
52 if Time.now - self.created_at > 5.minutes
57 elsif Time.now - self.last_ping_at > 1.hours
65 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
67 if o[:ping_secret] != self.info[:ping_secret]
68 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
69 raise ArvadosModel::UnauthorizedError.new("Incorrect ping_secret")
71 self.last_ping_at = Time.now
73 @bypass_arvados_authorization = true
76 if self.ip_address.nil?
77 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
78 self.ip_address = o[:ip]
79 self.first_ping_at = Time.now
82 # Record instance ID if not already known
83 if o[:ec2_instance_id]
84 if !self.info[:ec2_instance_id]
85 self.info[:ec2_instance_id] = o[:ec2_instance_id]
86 if (Rails.configuration.compute_node_ec2_tag_enable rescue true)
87 tag_cmd = ("ec2-create-tags #{o[:ec2_instance_id]} " +
88 "--tag 'Name=#{self.uuid}'")
91 elsif self.info[:ec2_instance_id] != o[:ec2_instance_id]
92 logger.debug "Multiple nodes have credentials for #{self.uuid}"
93 raise "#{self.uuid} is already running at #{self.info[:ec2_instance_id]} so rejecting ping from #{o[:ec2_instance_id]}"
98 if self.slot_number.nil?
101 self.slot_number = try_slot
105 rescue ActiveRecord::RecordNotUnique
108 raise "No available node slots" if try_slot == MAX_SLOTS
110 self.hostname = self.class.hostname_for_slot(self.slot_number)
111 if info[:ec2_instance_id]
112 if (Rails.configuration.compute_node_ec2_tag_enable rescue true)
113 `ec2-create-tags #{self.info[:ec2_instance_id]} --tag 'hostname=#{self.hostname}'`
121 def start!(ping_url_method)
122 ensure_permission_to_update
123 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
124 if (Rails.configuration.compute_node_ec2run_args and
125 Rails.configuration.compute_node_ami)
126 ec2_args = ["--user-data '#{ping_url}'",
128 Rails.configuration.compute_node_ec2run_args,
129 Rails.configuration.compute_node_ami
131 ec2run_cmd = ["ec2-run-instances",
132 "--client-token", self.uuid,
133 ec2_args].flatten.join(' ')
134 ec2spot_cmd = ["ec2-request-spot-instances",
135 "-p #{Rails.configuration.compute_node_spot_bid} --type one-time",
136 ec2_args].flatten.join(' ')
141 self.info[:ec2_run_command] = ec2run_cmd
142 self.info[:ec2_spot_command] = ec2spot_cmd
143 self.info[:ec2_start_command] = ec2spot_cmd
144 logger.info "#{self.uuid} ec2_start_command= #{ec2spot_cmd.inspect}"
145 result = `#{ec2spot_cmd} 2>&1`
146 self.info[:ec2_start_result] = result
147 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
148 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
150 self.info[:ec2_instance_id] = instance_id
151 if (Rails.configuration.compute_node_ec2_tag_enable rescue true)
152 `ec2-create-tags #{instance_id} --tag 'Name=#{self.uuid}'`
155 result.match(/SPOTINSTANCEREQUEST\s*(sir-[0-9a-f]+)/) do |m|
157 self.info[:ec2_sir_id] = sir_id
158 if (Rails.configuration.compute_node_ec2_tag_enable rescue true)
159 `ec2-create-tags #{sir_id} --tag 'Name=#{self.uuid}'`
167 def ensure_ping_secret
168 self.info[:ping_secret] ||= rand(2**256).to_s(36)
172 if self.hostname_changed? or self.ip_address_changed?
173 if self.hostname and self.ip_address
174 self.class.dnsmasq_update(self.hostname, self.ip_address)
179 def self.dnsmasq_update(hostname, ip_address)
180 return unless @@confdir
181 ptr_domain = ip_address.
182 split('.').reverse.join('.').concat('.in-addr.arpa')
183 hostfile = File.join @@confdir, hostname
184 File.open hostfile, 'w' do |f|
185 f.puts "address=/#{hostname}/#{ip_address}"
186 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
187 f.puts "ptr-record=#{ptr_domain},#{hostname}"
189 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
190 # this should trigger a dnsmasq restart
194 def self.hostname_for_slot(slot_number)
195 "compute#{slot_number}"
198 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
199 # will refuse to start.
201 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
202 (0..MAX_SLOTS-1).each do |slot_number|
203 hostname = hostname_for_slot(slot_number)
204 hostfile = File.join @@confdir, hostname
205 if !File.exists? hostfile
206 dnsmasq_update(hostname, '127.40.4.0')
211 def permission_to_update
212 @bypass_arvados_authorization or super
215 def permission_to_create
216 current_user and current_user.is_admin