diff options
author | Tim Smith <tsmith@chef.io> | 2020-11-16 16:24:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-16 16:24:12 -0800 |
commit | e1806bd6170540c4ec2727812774869251f47711 (patch) | |
tree | 0af5eefe13ca57bee97a9985adab1db65681ac0c /lib/chef | |
parent | c2e507d27269bac1056764a3757984518ac20a83 (diff) | |
parent | f390e98e5dfed55a3c9a8315a6da2f426e5c6640 (diff) | |
download | chef-e1806bd6170540c4ec2727812774869251f47711.tar.gz |
Merge pull request #10472 from MsysTechnologiesllc/antima/fixes_for_mutltiple_entry_in_fstab
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/provider/mount/mount.rb | 68 |
1 files changed, 38 insertions, 30 deletions
diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb index e065ce09e5..71cecb47aa 100644 --- a/lib/chef/provider/mount/mount.rb +++ b/lib/chef/provider/mount/mount.rb @@ -70,10 +70,6 @@ class Chef @current_resource.dump($4.to_i) @current_resource.pass($5.to_i) logger.trace("Found mount #{device_fstab} to #{@new_resource.mount_point} in /etc/fstab") - next - when %r{^[/\w]+\s+#{Regexp.escape(@new_resource.mount_point)}\s+} - enabled = false - logger.trace("Found conflicting mount point #{@new_resource.mount_point} in /etc/fstab") end end @current_resource.enabled(enabled) @@ -168,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. + edit_fstab + 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 + edit_fstab(remove: true) end def network_device? @@ -268,6 +247,35 @@ class Chef @current_resource.pass == @new_resource.pass end + # It will update or delete the entry from fstab. + def edit_fstab(remove: 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 remove + logger.trace("#{@new_resource} is removed from fstab") + else + 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}") + logger.trace("#{@new_resource} is updated with new content in 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 |