only update hosts file if it has changed - for both host and guest

This commit is contained in:
Matthew Hopkins 2014-02-24 12:51:34 -05:00
parent 93fd353135
commit 3051f4b3e3
1 changed files with 12 additions and 7 deletions

View File

@ -19,11 +19,13 @@ module VagrantPlugins
# download and modify file with Vagrant-managed entries
file = @global_env.tmp_path.join("hosts.#{machine.name}")
machine.communicate.download(realhostfile, file)
update_file(file, machine, false)
if update_file(file, machine, false)
# upload modified file and remove temporary file
machine.communicate.upload(file, '/tmp/hosts')
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
end
# upload modified file and remove temporary file
machine.communicate.upload(file, '/tmp/hosts')
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
# i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
begin
FileUtils.rm(file)
@ -49,16 +51,19 @@ module VagrantPlugins
end
FileUtils.cp(hosts_location, file)
update_file(file)
copy_proc.call
if update_file(file)
copy_proc.call
end
end
private
def update_file(file, resolving_machine = nil, include_id = true)
file = Pathname.new(file)
new_file_content = update_content(file.read, resolving_machine, include_id)
old_file_content = file.read
new_file_content = update_content(old_file_content, resolving_machine, include_id)
file.open('w') { |io| io.write(new_file_content) }
old_file_content != new_file_content
end
def update_content(file_content, resolving_machine, include_id)