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
64 @bypass_orvos_authorization = true
67 if self.ip_address.nil?
68 logger.info "#{self.uuid} ip_address= #{o[:ip]}"
69 self.ip_address = o[:ip]
70 self.first_ping_at = Time.now
73 # Record instance ID if not already known
74 if !self.info[:ec2_instance_id] and o[:ec2_instance_id]
75 self.info[:ec2_instance_id] = o[:ec2_instance_id]
76 `ec2-create-tags #{self.info[:ec2_instance_id]} --tag 'Name=#{self.uuid}'`
80 if self.slot_number.nil?
83 self.slot_number = try_slot
87 rescue ActiveRecord::RecordNotUnique
90 raise "No available node slots" if try_slot == MAX_SLOTS
92 self.hostname = self.class.hostname_for_slot(self.slot_number)
93 if info[:ec2_instance_id]
94 `ec2-create-tags #{self.info[:ec2_instance_id]} --tag 'hostname=#{self.hostname}'`
101 def start!(ping_url_method)
102 ensure_permission_to_update
103 ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
104 cmd = ["ec2-run-instances",
105 "--user-data '#{ping_url}'",
106 "-t c1.xlarge -n 1 -g orvos-compute",
107 "--client-token", self.uuid,
108 Rails.configuration.compute_node_ami
110 self.info[:ec2_start_command] = cmd
111 logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
112 result = `#{cmd} 2>&1`
113 self.info[:ec2_start_result] = result
114 logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
115 result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
117 self.info[:ec2_instance_id] = instance_id
118 `ec2-create-tags #{instance_id} --tag 'Name=#{self.uuid}'`
125 def ensure_ping_secret
126 self.info[:ping_secret] ||= rand(2**256).to_s(36)
130 if self.hostname_changed? or self.ip_address_changed?
131 if self.hostname and self.ip_address
132 self.class.dnsmasq_update(self.hostname, self.ip_address)
137 def self.dnsmasq_update(hostname, ip_address)
138 return unless @@confdir
139 ptr_domain = ip_address.
140 split('.').reverse.join('.').concat('.in-addr.arpa')
141 hostfile = File.join @@confdir, hostname
142 File.open hostfile, 'w' do |f|
143 f.puts "address=/#{hostname}/#{ip_address}"
144 f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
145 f.puts "ptr-record=#{ptr_domain},#{hostname}"
147 File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
148 # this should trigger a dnsmasq restart
152 def self.hostname_for_slot(slot_number)
153 "compute#{slot_number}"
156 # At startup, make sure all DNS entries exist. Otherwise, slurmctld
157 # will refuse to start.
159 !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
160 (0..MAX_SLOTS-1).each do |slot_number|
161 hostname = hostname_for_slot(slot_number)
162 hostfile = File.join @@confdir, hostname
163 if !File.exists? hostfile
164 dnsmasq_update(hostname, '127.40.4.0')
169 def permission_to_update
170 @bypass_orvos_authorization or super
173 def permission_to_create
174 current_user and current_user.is_admin