improve default nameservers list
[arvados.git] / app / models / node.rb
1 class Node < ActiveRecord::Base
2   include AssignUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   serialize :info, Hash
6   before_validation :ensure_ping_secret
7   after_update :dnsmasq_update
8
9   MAX_SLOTS = 64
10
11   @@confdir = if Rails.configuration.respond_to? :dnsmasq_conf_dir
12                 Rails.configuration.dnsmasq_conf_dir
13               elsif File.exists? '/etc/dnsmasq.d/.'
14                 '/etc/dnsmasq.d'
15               else
16                 nil
17               end
18   @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
19   @@nameservers = Rails.configuration.compute_node_nameservers
20
21   api_accessible :superuser, :extend => :common do |t|
22     t.add :hostname
23     t.add :domain
24     t.add :ip_address
25     t.add :first_ping_at
26     t.add :last_ping_at
27     t.add :info
28     t.add :status
29     t.add lambda { |x| @@nameservers }, :as => :nameservers
30   end
31
32   def info
33     @info ||= Hash.new
34     super
35   end
36
37   def domain
38     super || @@domain
39   end
40
41   def status
42     if !self.last_ping_at
43       if Time.now - self.created_at > 5.minutes
44         'startup-fail'
45       else
46         'pending'
47       end
48     elsif Time.now - self.last_ping_at > 1.hours
49       'missing'
50     else
51       'running'
52     end
53   end
54
55   def ping(o)
56     raise "must have :ip and :ping_secret" unless o[:ip] and o[:ping_secret]
57
58     if o[:ping_secret] != self.info[:ping_secret]
59       logger.info "Ping: secret mismatch: received \"#{o[:ping_secret]}\" != \"#{self.info[:ping_secret]}\""
60       return nil
61     end
62     self.last_ping_at = Time.now
63
64     # Record IP address
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
69     end
70
71     # Record instance ID if not already known
72     self.info[:ec2_instance_id] ||= o[:ec2_instance_id]
73
74     # Assign hostname
75     if self.slot_number.nil?
76       try_slot = 0
77       begin
78         self.slot_number = try_slot
79         begin
80           self.save!
81           break
82         rescue ActiveRecord::RecordNotUnique
83           try_slot += 1
84         end
85         raise "No available node slots" if try_slot == MAX_SLOTS
86       end while true
87       self.hostname = self.class.hostname_for_slot(self.slot_number)
88     end
89
90     save
91   end
92
93   def start!(ping_url_method)
94     ping_url = ping_url_method.call({ uuid: self.uuid, ping_secret: self.info[:ping_secret] })
95     cmd = ["ec2-run-instances",
96            "--user-data '#{ping_url}'",
97            "-t c1.xlarge -n 1 -g orvos-compute",
98            "ami-68ca6901"
99           ].join(' ')
100     self.info[:ec2_start_command] = cmd
101     logger.info "#{self.uuid} ec2_start_command= #{cmd.inspect}"
102     result = `#{cmd} 2>&1`
103     self.info[:ec2_start_result] = result
104     logger.info "#{self.uuid} ec2_start_result= #{result.inspect}"
105     result.match(/INSTANCE\s*(i-[0-9a-f]+)/) do |m|
106       self.info[:ec2_instance_id] = m[1]
107     end
108     self.save!
109   end
110
111   protected
112
113   def ensure_ping_secret
114     self.info[:ping_secret] ||= rand(2**256).to_s(36)
115   end
116
117   def dnsmasq_update
118     if self.hostname_changed? or self.ip_address_changed?
119       if self.hostname and self.ip_address
120         self.class.dnsmasq_update(self.hostname, self.ip_address)
121       end
122     end
123   end
124
125   def self.dnsmasq_update(hostname, ip_address)
126     return unless @@confdir
127     ptr_domain = ip_address.
128       split('.').reverse.join('.').concat('.in-addr.arpa')
129     hostfile = File.join @@confdir, hostname
130     File.open hostfile, 'w' do |f|
131       f.puts "address=/#{hostname}/#{ip_address}"
132       f.puts "address=/#{hostname}.#{@@domain}/#{ip_address}" if @@domain
133       f.puts "ptr-record=#{ptr_domain},#{hostname}"
134     end
135     File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
136       # this should trigger a dnsmasq restart
137     end
138   end
139
140   def self.hostname_for_slot(slot_number)
141     "compute#{slot_number}"
142   end
143
144   # At startup, make sure all DNS entries exist.  Otherwise, slurmctld
145   # will refuse to start.
146   if @@confdir and
147       !File.exists? (File.join(@@confdir, hostname_for_slot(MAX_SLOTS-1)))
148     (0..MAX_SLOTS-1).each do |slot_number|
149       hostname = hostname_for_slot(slot_number)
150       hostfile = File.join @@confdir, hostname
151       if !File.exists? hostfile
152         dnsmasq_update(hostname, '127.40.4.0')
153       end
154     end
155   end
156 end