From 1a8713b2a14eaef29f4639d1ba1980b0dfda47df Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Tue, 3 Nov 2020 02:40:14 -0800 Subject: Added update_or_delete_fs method to fix the order change. Added specs for the changes. Signed-off-by: antima-gupta --- lib/chef/provider/mount/mount.rb | 64 ++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 26 deletions(-) (limited to 'lib/chef/provider') diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb index ad3f74ebc8..85045eddfa 100644 --- a/lib/chef/provider/mount/mount.rb +++ b/lib/chef/provider/mount/mount.rb @@ -164,36 +164,19 @@ class Chef if @current_resource.enabled # The current options don't match what we have, so - # disable, then enable. - disable_fs - end - ::File.open("/etc/fstab", "a") do |fstab| - fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") - logger.trace("#{@new_resource} is enabled at #{@new_resource.mount_point}") + # update the last matching entry with current option + # and order will remain the same. + update_or_delete_fs(true) + else + ::File.open("/etc/fstab", "a") do |fstab| + fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") + logger.trace("#{@new_resource} is enabled at #{@new_resource.mount_point}") + end end end def disable_fs - if @current_resource.enabled - contents = [] - - found = false - ::File.readlines("/etc/fstab").reverse_each do |line| - if !found && line =~ /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}\s/ - found = true - logger.trace("#{@new_resource} is removed from fstab") - next - else - contents << line - end - end - - ::File.open("/etc/fstab", "w") do |fstab| - contents.reverse_each { |line| fstab.puts line } - end - else - logger.trace("#{@new_resource} is not enabled - nothing to do") - end + update_or_delete_fs end def network_device? @@ -264,6 +247,35 @@ class Chef @current_resource.pass == @new_resource.pass end + # It will update or delete the entry from fstab. + def update_or_delete_fs(update_fs=false) + if @current_resource.enabled + contents = [] + + found = false + ::File.readlines("/etc/fstab").reverse_each do |line| + if !found && line =~ /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}\s/ + found = true + if update_fs + logger.trace("#{@new_resource} is updated with new content in fstab") + contents << ("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") + else + logger.trace("#{@new_resource} is removed from fstab") + end + next + else + contents << line + end + end + + ::File.open("/etc/fstab", "w") do |fstab| + contents.reverse_each { |line| fstab.puts line } + end + else + logger.trace("#{@new_resource} is not enabled - nothing to do") + end + end + end end end -- cgit v1.2.1