fix ownership-change permission check
[arvados.git] / app / models / node.rb
1 class Node < OrvosModel
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     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}'`
75     end
76
77     # Assign hostname
78     if self.slot_number.nil?
79       try_slot = 0
80       begin
81         self.slot_number = try_slot
82         begin
83           self.save!
84           break
85         rescue ActiveRecord::RecordNotUnique
86           try_slot += 1
87         end
88         raise "No available node slots" if try_slot == MAX_SLOTS
89       end while true
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}'`
93       end
94     end
95
96     save
97   end
98
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
106           ].join(' ')
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|
113       instance_id = m[1]
114       self.info[:ec2_instance_id] = instance_id
115       `ec2-create-tags #{instance_id} --tag 'Name=#{self.uuid}'`
116     end
117     self.save!
118   end
119
120   protected
121
122   def ensure_ping_secret
123     self.info[:ping_secret] ||= rand(2**256).to_s(36)
124   end
125
126   def dnsmasq_update
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)
130       end
131     end
132   end
133
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}"
143     end
144     File.open(File.join(@@confdir, 'restart.txt'), 'w') do |f|
145       # this should trigger a dnsmasq restart
146     end
147   end
148
149   def self.hostname_for_slot(slot_number)
150     "compute#{slot_number}"
151   end
152
153   # At startup, make sure all DNS entries exist.  Otherwise, slurmctld
154   # will refuse to start.
155   if @@confdir and
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')
162       end
163     end
164   end
165 end