Use Util::StringBlockEditor to modify /etc/exports
This commit is contained in:
parent
7e83edd643
commit
40dc0de665
|
@ -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")
|
||||||
|
|
||||||
|
editor = Util::StringBlockEditor.new(File.read("/etc/exports"))
|
||||||
|
remove_ids = Array(remove_ids)
|
||||||
|
|
||||||
|
# Remove all invalid ID entries
|
||||||
|
remove_ids.each do |r_id|
|
||||||
|
editor.delete(r_id)
|
||||||
|
end
|
||||||
|
nfs_write_exports(editor.value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.nfs_write_exports(new_exports_content)
|
||||||
|
# Write contents out to temporary file
|
||||||
|
new_exports_file = Tempfile.create('vagrant')
|
||||||
|
new_exports_file.puts(new_exports_content)
|
||||||
|
new_exports_file.close
|
||||||
|
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
|
# Only use "sudo" if we can't write to /etc/exports directly
|
||||||
sudo_command = ""
|
sudo_command = ""
|
||||||
sudo_command = "sudo " if !File.writable?("/etc/exports")
|
sudo_command = "sudo " if !File.writable?("/etc/exports")
|
||||||
|
|
||||||
# Strip out the block of code which was inserted by Vagrant
|
# Ensure new file mode and uid/gid match existing file to replace
|
||||||
user = Regexp.escape(Process.uid.to_s)
|
existing_stat = File.stat("/etc/exports")
|
||||||
id = Regexp.escape(id.to_s)
|
new_stat = File.stat(new_exports_path)
|
||||||
exports_in = File.read('/etc/exports')
|
if existing_stat.mode != new_stat.mode
|
||||||
exports_out = exports_in.gsub(%r{
|
File.chmod(existing_stat.mode, new_exports_path)
|
||||||
^\#\ VAGRANT-BEGIN:((?:\ #{user})?\ #{id})$
|
|
||||||
.*?
|
|
||||||
^\#\ VAGRANT-END:\1$
|
|
||||||
\n?
|
|
||||||
}mx, '')
|
|
||||||
if exports_out != exports_in
|
|
||||||
open(%Q[|#{sudo_command}tee /etc/exports >/dev/null], 'w+') do |p|
|
|
||||||
p.write(exports_out)
|
|
||||||
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue