Skip to content

Commit 56ce9f8

Browse files
committed
Merge pull request #78 from thematthopkins/less_sudo_2
only update hosts file if it has changed - for both host and guest Closes #68.
2 parents a7a90d5 + 3051f4b commit 56ce9f8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/vagrant-hostmanager/hosts_file.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ def update_guest(machine)
1919
# download and modify file with Vagrant-managed entries
2020
file = @global_env.tmp_path.join("hosts.#{machine.name}")
2121
machine.communicate.download(realhostfile, file)
22-
update_file(file, machine, false)
22+
if update_file(file, machine, false)
23+
24+
# upload modified file and remove temporary file
25+
machine.communicate.upload(file, '/tmp/hosts')
26+
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
27+
end
2328

24-
# upload modified file and remove temporary file
25-
machine.communicate.upload(file, '/tmp/hosts')
26-
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
2729
# i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
2830
begin
2931
FileUtils.rm(file)
@@ -49,16 +51,19 @@ class << self
4951
end
5052

5153
FileUtils.cp(hosts_location, file)
52-
update_file(file)
53-
copy_proc.call
54+
if update_file(file)
55+
copy_proc.call
56+
end
5457
end
5558

5659
private
5760

5861
def update_file(file, resolving_machine = nil, include_id = true)
5962
file = Pathname.new(file)
60-
new_file_content = update_content(file.read, resolving_machine, include_id)
63+
old_file_content = file.read
64+
new_file_content = update_content(old_file_content, resolving_machine, include_id)
6165
file.open('w') { |io| io.write(new_file_content) }
66+
old_file_content != new_file_content
6267
end
6368

6469
def update_content(file_content, resolving_machine, include_id)

0 commit comments

Comments
 (0)