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