customink-webops / hostsfile

Easily manage your hosts file with Chef
Apache License 2.0
155 stars 82 forks source link

update entry without priority set in attributes results to priority 0 #96

Open WeetA34 opened 7 years ago

WeetA34 commented 7 years ago

Hello

i had an issue with priority. When i use two update actions without specifying the priority in attributes, chef always update my entries.

hostsfile_entry '192.168.1.2' do
        hostname        'server01.weeta.lab'
        aliases         ['server01', 'server01.test.lab']
        comment         'Update by Chef'
        action          :update
end

hostsfile_entry '192.168.1.3' do
        hostname        'server02.weeta.lab'
        aliases         ['server02', 'server02.test.lab']
        comment         'Update by Chef'
        action          :update
end

After some investigations, i found when priority is not defined in attributes, options[:priority] is nil. Calculated priority of an ipv4 ip (60) is replaced by 0 due to -e.priority.to_i in manipulator unique_entries method. So the updated entry is always moved to the end. When you only have one entry in your recipe, it's OK but when you have 2 or more entries without static priority, the first one is moved to the end then the second one is moved to the end. It happens each time the chef client is executed.

I resolved my issue by modifying the code like this:

--- entry.rb.orig       2017-08-23 02:34:23.000000000 +0200
+++ entry.rb    2017-11-27 18:51:18.854049200 +0100
@@ -127,8 +127,12 @@
     # @param [Integer] new_priority
     #   the new priority to set
     def priority=(new_priority)
-      @calculated_priority = false
-      @priority = new_priority
+      if new_priority.nil?
+        @priority = calculated_priority
+      else
+        @calculated_priority = false
+        @priority = new_priority
+      end
     end

     # The line representation of this entry.

Rgds Stéphane Olivier