1 class Node < OrvosModel
4 include CommonApiTemplate
6 before_validation :ensure_ping_secret
7 after_update :dnsmasq_update
11 @@confdir = if Rails.configuration.respond_to? :dnsmasq_conf_dir
12 Rails.configuration.dnsmasq_conf_dir
13 elsif File.exists? '/etc/dnsmasq.d/.'
18 @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
19 @@nameservers = Rails.configuration.compute_node_nameservers
21 api_accessible :superuser, :extend => :common do |t|
29 t.add lambda { |x| @@nameservers }, :as => :nameservers
43 if Time.now - self.created_at > 5.minutes
48 elsif Time.now - self.last_ping_at > 1.hours
56 raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
58 if o[:ping_secret] != self.info[:ping_secret]
59 logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
62 self.last_ping_at = Time.now
65 if self.ip_address.nil?
66 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
67 self.ip_address = o[:ip]
68 self.first_ping_at = Time.now
71 # Record instance ID if not already known
72 if !self.info[:ec2_instance_id] and o[:ec2_instance_id]
73 self.info[:ec2_instance_id] = o[:ec2_instance_id]
74 `ec2-create-tags #{self.info[:ec2_instance_id]} --tag 'Name=#{self.uuid}'`
78 if self.slot_number.nil?
81 self.slot_number = try_slot
85 rescue ActiveRecord::RecordNotUnique
88 raise "No available node slots" if try_slot == MAX_SLOTS
90 self.hostname = self.class.hostname_for_slot(self.slot_number)
91 if info[:ec2_instance_id]
92 `ec2-create-tags #{self.info[:ec2_instance_id]} --tag 'hostname=#{self.hostname}'`
99 def start!(ping_url_method)
100 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
101 cmd = ["ec2-run-instances",
102 "--user-data '#{ping_url}'",
103 "-t c1.xlarge -n 1 -g orvos-compute",
104 "--client-token", self.uuid,
105 Rails.configuration.compute_node_ami
107 self.info[:ec2_start_command] = cmd
108 logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
109 result = `#{cmd} 2>&1`
110 self.info[:ec2_start_result] = result
111 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
112 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
114 self.info[:ec2_instance_id] = instance_id
115 `ec2-create-tags #{instance_id} --tag 'Name=#{self.uuid}'`
122 def ensure_ping_secret
123 self.info[:ping_secret] ||= rand(2**256).to_s(36)
127 if self.hostname_changed? or self.ip_address_changed?
128 if self.hostname and self.ip_address
129 self.class.dnsmasq_update(self.hostname, self.ip_address)
134 def self.dnsmasq_update(hostname, ip_address)
135 return unless @@confdir
136 ptr_domain = ip_address.
137 split('.').reverse.join('.').concat('.in-addr.arpa')
138 hostfile = File.join @@confdir, hostname
139 File.open hostfile, 'w' do |f|
140 f.puts "address=/#{hostname}/#{ip_address}"
141 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
142 f.puts "ptr-record=#{ptr_domain},#{hostname}"
144 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
145 # this should trigger a dnsmasq restart
149 def self.hostname_for_slot(slot_number)
150 "compute#{slot_number}"
153 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
154 # will refuse to start.
156 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
157 (0..MAX_SLOTS-1).each do |slot_number|
158 hostname = hostname_for_slot(slot_number)
159 hostfile = File.join @@confdir, hostname
160 if !File.exists? hostfile
161 dnsmasq_update(hostname, '127.40.4.0')