[ARVADOS] created: 6da9f3666bc0ddee2d0be079cc30ba8b82804706

Git user git at public.curoverse.com
Wed Apr 5 13:28:37 EDT 2017


        at  6da9f3666bc0ddee2d0be079cc30ba8b82804706 (commit)


commit 6da9f3666bc0ddee2d0be079cc30ba8b82804706
Author: Lucas Di Pentima <lucas at curoverse.com>
Date:   Wed Apr 5 14:23:49 2017 -0300

    6304: Avoid possible dns update race condition by writing the temp file with unique name to avoid collisions.
    Catch IO specific exceptions when trying to write the config files.
    Added test to assert that no config file is written when the update command is not set up but the config dir is.

diff --git a/services/api/app/models/node.rb b/services/api/app/models/node.rb
index c7ac090..b57abfd 100644
--- a/services/api/app/models/node.rb
+++ b/services/api/app/models/node.rb
@@ -1,3 +1,5 @@
+require 'tempfile'
+
 class Node < ArvadosModel
   include HasUuid
   include KindAndEtag
@@ -174,17 +176,19 @@ class Node < ArvadosModel
       begin
         begin
           template = IO.read(Rails.configuration.dns_server_conf_template)
-        rescue => e
+        rescue IOError, SystemCallError => e
           logger.error "Reading #{Rails.configuration.dns_server_conf_template}: #{e.message}"
           raise
         end
 
         hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf"
-        File.open hostfile+'.tmp', 'w' do |f|
+        tmpfile = Tempfile.open(["#{hostname}", ".conf.tmp"],
+                                 Rails.configuration.dns_server_conf_dir) do |f|
           f.puts template % template_vars
+          f.path
         end
-        File.rename hostfile+'.tmp', hostfile
-      rescue => e
+        File.rename tmpfile, hostfile
+      rescue IOError, SystemCallError => e
         logger.error "Writing #{hostfile}: #{e.message}"
         ok = false
       end
@@ -205,7 +209,7 @@ class Node < ArvadosModel
           # Typically, this is used to trigger a dns server restart
           f.puts Rails.configuration.dns_server_reload_command
         end
-      rescue => e
+      rescue IOError, SystemCallError => e
         logger.error "Unable to write #{restartfile}: #{e.message}"
         ok = false
       end
diff --git a/services/api/test/unit/node_test.rb b/services/api/test/unit/node_test.rb
index c1e77f6..f9e0f4b 100644
--- a/services/api/test/unit/node_test.rb
+++ b/services/api/test/unit/node_test.rb
@@ -76,6 +76,14 @@ class NodeTest < ActiveSupport::TestCase
     assert Node.dns_server_update 'compute65535', '127.0.0.127'
   end
 
+  test "dns update with dir configured but no command configured" do
+    Rails.configuration.dns_server_update_command = false
+    Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
+    conffile = Rails.root.join 'tmp', 'compute65535.conf'
+    assert Node.dns_server_update 'compute65535', '127.0.0.127'
+    refute File.exist? conffile
+  end
+
   test "ping new node with no hostname and default config" do
     node = ping_node(:new_with_no_hostname, {})
     slot_number = node.slot_number

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list