Use Util::StringBlockEditor to modify /etc/exports

This commit is contained in:
Chris Roberts 2016-10-29 15:46:12 -07:00
parent 7e83edd643
commit 40dc0de665
1 changed files with 53 additions and 41 deletions

View File

@ -37,15 +37,7 @@ module VagrantPlugins
sleep 0.5 sleep 0.5
nfs_cleanup(id) nfs_cleanup(id)
nfs_write_exports(output)
# Only use "sudo" if we can't write to /etc/exports directly
sudo_command = ""
sudo_command = "sudo " if !File.writable?("/etc/exports")
output.split("\n").each do |line|
line = Vagrant::Util::ShellQuote.escape(line, "'")
system(%Q[echo '#{line}' | #{sudo_command}tee -a /etc/exports >/dev/null])
end
if nfs_running?(nfs_check_command) if nfs_running?(nfs_check_command)
system("sudo #{nfs_apply_command}") system("sudo #{nfs_apply_command}")
@ -67,50 +59,70 @@ module VagrantPlugins
logger = Log4r::Logger.new("vagrant::hosts::linux") logger = Log4r::Logger.new("vagrant::hosts::linux")
logger.info("Pruning invalid NFS entries...") logger.info("Pruning invalid NFS entries...")
output = false
user = Process.uid user = Process.uid
File.read("/etc/exports").lines.each do |line| # Create editor instance for removing invalid IDs
if id = line[/^# VAGRANT-BEGIN:( #{user})? ([\.\/A-Za-z0-9\-_:]+?)$/, 2] editor = Util::StringBlockEditor.new(File.read("/etc/exports"))
if valid_ids.include?(id)
logger.debug("Valid ID: #{id}")
else
if !output
# We want to warn the user but we only want to output once
ui.info I18n.t("vagrant.hosts.linux.nfs_prune")
output = true
end
logger.info("Invalid ID, pruning: #{id}") # Build composite IDs with UID information and discover invalid entries
nfs_cleanup(id) composite_ids = valid_ids.map do |v_id|
end "#{user} #{v_id}"
end end
remove_ids = editor.keys - composite_keys
logger.debug("Known valid NFS export IDs: #{valid_ids}")
logger.debug("Composite valid NFS export IDs with user: #{composite_ids}")
logger.debug("NFS export IDs to be removed: #{remove_ids}")
if !remove_ids.empty?
ui.info I18n.t("vagrant.hosts.linux.nfs_prune")
nfs_cleanup(remove_ids)
end end
end end
protected protected
def self.nfs_cleanup(id) def self.nfs_cleanup(remove_ids)
return if !File.exist?("/etc/exports") return if !File.exist?("/etc/exports")
# Only use "sudo" if we can't write to /etc/exports directly editor = Util::StringBlockEditor.new(File.read("/etc/exports"))
sudo_command = "" remove_ids = Array(remove_ids)
sudo_command = "sudo " if !File.writable?("/etc/exports")
# Strip out the block of code which was inserted by Vagrant # Remove all invalid ID entries
user = Regexp.escape(Process.uid.to_s) remove_ids.each do |r_id|
id = Regexp.escape(id.to_s) editor.delete(r_id)
exports_in = File.read('/etc/exports') end
exports_out = exports_in.gsub(%r{ nfs_write_exports(editor.value)
^\#\ VAGRANT-BEGIN:((?:\ #{user})?\ #{id})$ end
.*?
^\#\ VAGRANT-END:\1$ def self.nfs_write_exports(new_exports_content)
\n? # Write contents out to temporary file
}mx, '') new_exports_file = Tempfile.create('vagrant')
if exports_out != exports_in new_exports_file.puts(new_exports_content)
open(%Q[|#{sudo_command}tee /etc/exports >/dev/null], 'w+') do |p| new_exports_file.close
p.write(exports_out) new_exports_path = new_exports_file.path
if !FileUtils.compare_file(new_exports_path, "/etc/exports")
# Only use "sudo" if we can't write to /etc/exports directly
sudo_command = ""
sudo_command = "sudo " if !File.writable?("/etc/exports")
# Ensure new file mode and uid/gid match existing file to replace
existing_stat = File.stat("/etc/exports")
new_stat = File.stat(new_exports_path)
if existing_stat.mode != new_stat.mode
File.chmod(existing_stat.mode, new_exports_path)
end end
# TODO: Error check
if existing_stat.uid != new_stat.uid || existing_stat.gid != new_stat.gid
system("#{sudo_command}chown #{existing_stat.uid}:#{existing_stat.gid} #{new_exports_path}")
end
# Replace existing exports file
system("#{sudo_command}mv #{new_exports_path} /etc/exports")
end
ensure
if File.exist?(new_exports_path)
File.unlink(new_exports_path)
end end
end end