summaryrefslogtreecommitdiff
path: root/lib/chef/provider
diff options
context:
space:
mode:
authorantima-gupta <agupta@msystechnologies.com>2020-11-03 02:40:14 -0800
committerantima-gupta <agupta@msystechnologies.com>2020-11-12 21:31:30 -0800
commit1a8713b2a14eaef29f4639d1ba1980b0dfda47df (patch)
tree3509a39ac360d3f7a6cc5f59f72c7047b64ac27e /lib/chef/provider
parent5951200470b3db0fd75b121db0fc08c3946eafe8 (diff)
downloadchef-1a8713b2a14eaef29f4639d1ba1980b0dfda47df.tar.gz
Added update_or_delete_fs method to fix the order change.
Added specs for the changes. Signed-off-by: antima-gupta <agupta@msystechnologies.com>
Diffstat (limited to 'lib/chef/provider')
-rw-r--r--lib/chef/provider/mount/mount.rb64
1 files changed, 38 insertions, 26 deletions
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