fix some error message suppression in node#ping
[arvados.git] / app / models / node.rb
index 29233748dd1cf77cc61b77fc08b23130dee0102c..026865e9575cff26b229ff8c5cfcc5917774b7fd 100644 (file)
@@ -2,6 +2,7 @@ class Node < ActiveRecord::Base
   include AssignUuid
   serialize :info, Hash
   before_validation :ensure_ping_secret
+  after_update :dnsmasq_update
 
   MAX_SLOTS = 64
 
@@ -42,12 +43,15 @@ class Node < ActiveRecord::Base
       try_slot = 0
       begin
         self.slot_number = try_slot
-        try_slot += 1
-        break if self.save rescue nil
+        begin
+          self.save!
+          break
+        rescue ActiveRecord::RecordNotUnique
+          try_slot += 1
+        end
         raise "No available node slots" if try_slot == MAX_SLOTS
       end while true
       self.hostname = self.class.hostname_for_slot(self.slot_number)
-      self.class.dnsmasq_update(self.hostname, self.ip_address)
     end
 
     save
@@ -77,6 +81,14 @@ class Node < ActiveRecord::Base
     self.info[:ping_secret] ||= rand(2**256).to_s(36)
   end
 
+  def dnsmasq_update
+    if self.hostname_changed? or self.ip_address_changed?
+      if self.hostname and self.ip_address
+        self.class.dnsmasq_update(self.hostname, self.ip_address)
+      end
+    end
+  end
+
   def self.dnsmasq_update(hostname, ip_address)
     return unless @@confdir
     ptr_domain = ip_address.