Merge pull request #52 from pbitty/hosts_file_sync
Sync state with hosts file in blocks
This commit is contained in:
commit
4ba0d56bdb
|
@ -19,7 +19,7 @@ module VagrantPlugins
|
||||||
# download and modify file with Vagrant-managed entries
|
# download and modify file with Vagrant-managed entries
|
||||||
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
||||||
machine.communicate.download(realhostfile, file)
|
machine.communicate.download(realhostfile, file)
|
||||||
update_file(file, machine)
|
update_file(file, machine, false)
|
||||||
|
|
||||||
# upload modified file and remove temporary file
|
# upload modified file and remove temporary file
|
||||||
machine.communicate.upload(file, '/tmp/hosts')
|
machine.communicate.upload(file, '/tmp/hosts')
|
||||||
|
@ -55,43 +55,27 @@ module VagrantPlugins
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def update_file(file, resolving_machine=nil)
|
def update_file(file, resolving_machine = nil, include_id = true)
|
||||||
# build array of host file entries from Vagrant configuration
|
file = Pathname.new(file)
|
||||||
entries = []
|
new_file_content = update_content(file.read, resolving_machine, include_id)
|
||||||
destroyed_entries = []
|
file.open('w') { |io| io.write(new_file_content) }
|
||||||
ids = []
|
end
|
||||||
get_machines.each do |name, p|
|
|
||||||
if @provider == p
|
|
||||||
machine = @global_env.machine(name, p)
|
|
||||||
host = machine.config.vm.hostname || name
|
|
||||||
id = machine.id
|
|
||||||
ip = get_ip_address(machine, resolving_machine)
|
|
||||||
aliases = machine.config.hostmanager.aliases.join(' ').chomp
|
|
||||||
if id.nil?
|
|
||||||
destroyed_entries << "#{ip}\t#{host} #{aliases}"
|
|
||||||
else
|
|
||||||
entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n"
|
|
||||||
ids << id unless ids.include?(id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
tmp_file = Tempfile.open('hostmanager', @global_env.tmp_path, 'a')
|
def update_content(file_content, resolving_machine, include_id)
|
||||||
begin
|
id = include_id ? " id: #{read_or_create_id}" : ""
|
||||||
# copy each line not managed by Vagrant
|
header = "## vagrant-hostmanager-start#{id}\n"
|
||||||
File.open(file).each_line do |line|
|
footer = "## vagrant-hostmanager-end\n"
|
||||||
# Eliminate lines for machines that have been destroyed
|
body = get_machines
|
||||||
next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ }
|
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
|
||||||
tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ }
|
.join
|
||||||
end
|
get_new_content(header, footer, body, file_content)
|
||||||
|
end
|
||||||
|
|
||||||
# write a line for each Vagrant-managed entry
|
def get_hosts_file_entry(machine, resolving_machine)
|
||||||
entries.each { |entry| tmp_file << entry }
|
ip = get_ip_address(machine, resolving_machine)
|
||||||
ensure
|
host = machine.config.vm.hostname || machine.name
|
||||||
tmp_file.close
|
aliases = machine.config.hostmanager.aliases.join(' ').chomp
|
||||||
FileUtils.cp(tmp_file, file)
|
"#{ip}\t#{host} #{aliases}\n"
|
||||||
tmp_file.unlink
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_ip_address(machine, resolving_machine)
|
def get_ip_address(machine, resolving_machine)
|
||||||
|
@ -108,23 +92,51 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_machines
|
def get_machines
|
||||||
if @global_env.config_global.hostmanager.include_offline?
|
if @global_env.config_global.hostmanager.include_offline?
|
||||||
machines = []
|
machines = @global_env.machine_names
|
||||||
@global_env.machine_names.each do |name|
|
|
||||||
begin
|
|
||||||
@global_env.machine(name, @provider)
|
|
||||||
machines << [name, @provider]
|
|
||||||
rescue Vagrant::Errors::MachineNotFound
|
|
||||||
end
|
|
||||||
end
|
|
||||||
machines
|
|
||||||
else
|
else
|
||||||
@global_env.active_machines
|
machines = @global_env.active_machines
|
||||||
end
|
end
|
||||||
|
# Collect only machines that exist for the current provider
|
||||||
|
machines.collect do |name, _|
|
||||||
|
begin
|
||||||
|
machine = @global_env.machine(name, @provider)
|
||||||
|
rescue Vagrant::Errors::MachineNotFound
|
||||||
|
# ignore
|
||||||
|
end
|
||||||
|
machine
|
||||||
|
end
|
||||||
|
.reject(&:nil?)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_new_content(header, footer, body, old_content)
|
||||||
|
if body.empty?
|
||||||
|
block = "\n"
|
||||||
|
else
|
||||||
|
block = "\n\n" + header + body + footer + "\n"
|
||||||
|
end
|
||||||
|
# Pattern for finding existing block
|
||||||
|
header_pattern = Regexp.quote(header)
|
||||||
|
footer_pattern = Regexp.quote(footer)
|
||||||
|
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
|
||||||
|
# Replace existing block or append
|
||||||
|
old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_or_create_id
|
||||||
|
file = Pathname.new("#{@global_env.local_data_path}/hostmanager/id")
|
||||||
|
if (file.file?)
|
||||||
|
id = file.read.strip
|
||||||
|
else
|
||||||
|
id = SecureRandom.uuid
|
||||||
|
file.dirname.mkpath
|
||||||
|
file.open('w') { |io| io.write(id) }
|
||||||
|
end
|
||||||
|
id
|
||||||
end
|
end
|
||||||
|
|
||||||
## Windows support for copying files, requesting elevated privileges if necessary
|
## Windows support for copying files, requesting elevated privileges if necessary
|
||||||
|
|
Loading…
Reference in New Issue