From 2498e053f985c7351e65920d193d9dd8726131bc Mon Sep 17 00:00:00 2001 From: Andrew Yongjoon Kong Date: Thu, 16 Jan 2014 17:18:49 +0900 Subject: [PATCH 01/12] only first private_network is used this will prevent using last private_network for the hostname --- lib/vagrant-hostmanager/hosts_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index e7f0f80..d11f695 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -104,7 +104,7 @@ module VagrantPlugins machine.config.vm.networks.each do |network| key, options = network[0], network[1] ip = options[:ip] if key == :private_network - next if ip + break if ip end end ip || (machine.ssh_info ? machine.ssh_info[:host] : nil) From ab5d82275a9ac7509b4a73e94085e66e664a4d0c Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Feb 2014 16:32:52 -0500 Subject: [PATCH 02/12] sync hosts file state in blocks instead of lines --- lib/vagrant-hostmanager/hosts_file.rb | 86 +++++++++++++++------------ 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 94d0933..5f2bb18 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -19,7 +19,7 @@ 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) + update_file(file, machine, false) # upload modified file and remove temporary file machine.communicate.upload(file, '/tmp/hosts') @@ -55,43 +55,29 @@ module VagrantPlugins private - def update_file(file, resolving_machine=nil) - # build array of host file entries from Vagrant configuration - entries = [] - destroyed_entries = [] - ids = [] - 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 + 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) + file.open('w') { |io| io.write(new_file_content) } + end - tmp_file = Tempfile.open('hostmanager', @global_env.tmp_path, 'a') - begin - # copy each line not managed by Vagrant - File.open(file).each_line do |line| - # Eliminate lines for machines that have been destroyed - next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ } - tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ } - end + def update_content(file_content, resolving_machine, include_id) + id = include_id ? " id: #{read_or_create_id}" : "" + header = "## vagrant-hostmanager-start#{id}\n" + footer = "## vagrant-hostmanager-end\n" + body = get_machines + .select { |name, provider| provider == @provider} + .collect { |name, _| @global_env.machine(name, @provider) } + .map { |machine| get_hosts_file_entry(machine, resolving_machine) } + .join + get_new_content(header, footer, body, file_content) + end - # write a line for each Vagrant-managed entry - entries.each { |entry| tmp_file << entry } - ensure - tmp_file.close - FileUtils.cp(tmp_file, file) - tmp_file.unlink - end + def get_hosts_file_entry(machine, resolving_machine) + ip = get_ip_address(machine, resolving_machine) + host = machine.config.vm.hostname || machine.name + aliases = machine.config.hostmanager.aliases.join(' ').chomp + "#{ip}\t#{host} #{aliases}\n" end def get_ip_address(machine, resolving_machine) @@ -108,7 +94,7 @@ module VagrantPlugins end end ip || (machine.ssh_info ? machine.ssh_info[:host] : nil) - end + end end def get_machines @@ -127,6 +113,32 @@ module VagrantPlugins end 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 + ## Windows support for copying files, requesting elevated privileges if necessary module WindowsSupport require 'rbconfig' From 8f041f5ef66b7610d66f49396708581f21b0ca3e Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Feb 2014 16:32:56 -0500 Subject: [PATCH 03/12] make get_machines return machine instances instead of machine names --- lib/vagrant-hostmanager/hosts_file.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 5f2bb18..56258db 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -66,8 +66,6 @@ module VagrantPlugins header = "## vagrant-hostmanager-start#{id}\n" footer = "## vagrant-hostmanager-end\n" body = get_machines - .select { |name, provider| provider == @provider} - .collect { |name, _| @global_env.machine(name, @provider) } .map { |machine| get_hosts_file_entry(machine, resolving_machine) } .join get_new_content(header, footer, body, file_content) @@ -99,18 +97,20 @@ module VagrantPlugins def get_machines if @global_env.config_global.hostmanager.include_offline? - machines = [] - @global_env.machine_names.each do |name| - begin - @global_env.machine(name, @provider) - machines << [name, @provider] - rescue Vagrant::Errors::MachineNotFound - end - end - machines + machines = @global_env.machine_names else - @global_env.active_machines + machines = @global_env.active_machines 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) From fec2c501fdd7661d7850755d950fce3a7d9adb52 Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 24 Feb 2014 13:10:37 -0500 Subject: [PATCH 04/12] Fix regression in #52. We accidentally stopped filtering out machines from other providers. --- lib/vagrant-hostmanager/hosts_file.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 3e5e831..123ffa5 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -100,9 +100,11 @@ module VagrantPlugins machines = @global_env.machine_names else machines = @global_env.active_machines + .select { |name, provider| provider == @provider } + .collect { |name, provider| name } end # Collect only machines that exist for the current provider - machines.collect do |name, _| + machines.collect do |name| begin machine = @global_env.machine(name, @provider) rescue Vagrant::Errors::MachineNotFound From 3051f4b3e302d55b3ebba11e4514d48790b4c2eb Mon Sep 17 00:00:00 2001 From: Matthew Hopkins Date: Mon, 24 Feb 2014 12:51:34 -0500 Subject: [PATCH 05/12] only update hosts file if it has changed - for both host and guest --- lib/vagrant-hostmanager/hosts_file.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 3e5e831..23ba0e0 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -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) From c6219655f5e3d2a8a6bce3b90225c441021f817f Mon Sep 17 00:00:00 2001 From: januzis Date: Wed, 12 Mar 2014 11:51:14 +0100 Subject: [PATCH 06/12] Replaced deprecated config_global with vagrantfile.config --- lib/vagrant-hostmanager/action/update_all.rb | 10 +++++++++- lib/vagrant-hostmanager/action/update_host.rb | 10 +++++++++- lib/vagrant-hostmanager/hosts_file.rb | 2 +- lib/vagrant-hostmanager/provisioner.rb | 10 +++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/vagrant-hostmanager/action/update_all.rb b/lib/vagrant-hostmanager/action/update_all.rb index 7dcdcbe..d2671d9 100644 --- a/lib/vagrant-hostmanager/action/update_all.rb +++ b/lib/vagrant-hostmanager/action/update_all.rb @@ -11,6 +11,14 @@ module VagrantPlugins @machine = env[:machine] @global_env = @machine.env @provider = @machine.provider_name + + # config_global is deprecated from v1.5 + if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5') + @config = @global_env.vagrantfile.config + else + @config = @global_env.config_global + end + @logger = Log4r::Logger.new('vagrant::hostmanager::update_all') end @@ -21,7 +29,7 @@ module VagrantPlugins return @app.call(env) if !@machine.id && env[:machine_action] == :destroy # check config to see if the hosts file should be update automatically - return @app.call(env) unless @global_env.config_global.hostmanager.enabled? + return @app.call(env) unless @config.hostmanager.enabled? @logger.info 'Updating /etc/hosts file automatically' @app.call(env) diff --git a/lib/vagrant-hostmanager/action/update_host.rb b/lib/vagrant-hostmanager/action/update_host.rb index 5418953..8d48255 100644 --- a/lib/vagrant-hostmanager/action/update_host.rb +++ b/lib/vagrant-hostmanager/action/update_host.rb @@ -10,11 +10,19 @@ module VagrantPlugins @app = app @global_env = env[:global_env] @provider = env[:provider] + + # config_global is deprecated from v1.5 + if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5') + @config = @global_env.vagrantfile.config + else + @config = @global_env.config_global + end + @logger = Log4r::Logger.new('vagrant::hostmanager::update_host') end def call(env) - if @global_env.config_global.hostmanager.manage_host? + if @config.hostmanager.manage_host? env[:ui].info I18n.t('vagrant_hostmanager.action.update_host') update_host end diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 0dc7a2e..582ba53 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -101,7 +101,7 @@ module VagrantPlugins end def get_machines - if @global_env.config_global.hostmanager.include_offline? + if @config.hostmanager.include_offline? machines = @global_env.machine_names else machines = @global_env.active_machines diff --git a/lib/vagrant-hostmanager/provisioner.rb b/lib/vagrant-hostmanager/provisioner.rb index 4c5397e..afe027a 100644 --- a/lib/vagrant-hostmanager/provisioner.rb +++ b/lib/vagrant-hostmanager/provisioner.rb @@ -7,11 +7,19 @@ module VagrantPlugins super(machine, config) @global_env = machine.env @provider = machine.provider_name + + # config_global is deprecated from v1.5 + if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5') + @config = @global_env.vagrantfile.config + else + @config = @global_env.config_global + end + end def provision update_guest(@machine) - if @global_env.config_global.hostmanager.manage_host? + if @config.hostmanager.manage_host? update_host end end From 5a227847539455cc5af62862bb3df085dfb1e1d0 Mon Sep 17 00:00:00 2001 From: januzis Date: Wed, 12 Mar 2014 21:23:33 +0100 Subject: [PATCH 07/12] Fixes introduced error running vagrant hostmanager command --- lib/vagrant-hostmanager/action/update_guest.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/vagrant-hostmanager/action/update_guest.rb b/lib/vagrant-hostmanager/action/update_guest.rb index 73a7cb1..7ab7352 100644 --- a/lib/vagrant-hostmanager/action/update_guest.rb +++ b/lib/vagrant-hostmanager/action/update_guest.rb @@ -11,6 +11,14 @@ module VagrantPlugins @machine = env[:machine] @global_env = @machine.env @provider = env[:provider] + + # config_global is deprecated from v1.5 + if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5') + @config = @global_env.vagrantfile.config + else + @config = @global_env.config_global + end + @logger = Log4r::Logger.new('vagrant::hostmanager::update_guest') end From 3350c07bdcd6cd76699c39721e8d3af505d71d04 Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Mar 2014 21:07:43 -0400 Subject: [PATCH 08/12] update Gemfile and test Vagrantfile to support vagrant 1.5 relates to #80 --- Gemfile | 6 ++++-- test/Vagrantfile | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 137bac0..fecf40e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,9 @@ source 'https://rubygems.org' group :development do - gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.2.7' + gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.5.1' end -gemspec +group :plugins do + gemspec +end diff --git a/test/Vagrantfile b/test/Vagrantfile index b3a4f31..4c624d1 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -1,7 +1,9 @@ # -*- mode: ruby -*- # vi: set ft=ruby : -Vagrant.require_plugin('vagrant-hostmanager') +if Gem::Version.new(::Vagrant::VERSION) < Gem::Version.new('1.5') + Vagrant.require_plugin('vagrant-hostmanager') +end Vagrant.configure('2') do |config| config.vm.box = 'precise64' From 552a791ef4350609a277344e3263c9f8fcc4e126 Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Mar 2014 21:11:02 -0400 Subject: [PATCH 09/12] fix provisioner issue in vagrant 1.5 relates to #80 --- lib/vagrant-hostmanager/plugin.rb | 6 ++++++ test/Vagrantfile | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/plugin.rb b/lib/vagrant-hostmanager/plugin.rb index 8369d19..86dab08 100644 --- a/lib/vagrant-hostmanager/plugin.rb +++ b/lib/vagrant-hostmanager/plugin.rb @@ -29,6 +29,12 @@ module VagrantPlugins Provisioner end + # Work-around for vagrant >= 1.5 + # It breaks without a provisioner config, so we provide a dummy one + config(:hostmanager, :provisioner) do + ::Vagrant::Config::V2::DummyConfig.new + end + command(:hostmanager) do require_relative 'command' Command diff --git a/test/Vagrantfile b/test/Vagrantfile index 4c624d1..ecaa38c 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -22,4 +22,10 @@ Vagrant.configure('2') do |config| server.vm.hostname = 'bender' server.vm.network :private_network, :ip => '10.0.5.3' end -end + + config.vm.define :server3 do |server| + server.vm.hostname = 'leena' + server.vm.network :private_network, :ip => '10.0.5.4' + server.vm.provision :hostmanager + end +end \ No newline at end of file From f22cccb74185c0502b167f71b40f4ea7703b42ac Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Mar 2014 21:13:41 -0400 Subject: [PATCH 10/12] allow overriding vagrant box in test Vagrantfile via ENV variable --- test/Vagrantfile | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/Vagrantfile b/test/Vagrantfile index ecaa38c..24683e6 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -6,8 +6,13 @@ if Gem::Version.new(::Vagrant::VERSION) < Gem::Version.new('1.5') end Vagrant.configure('2') do |config| - config.vm.box = 'precise64' - config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box' + + if ENV.key? 'VAGRANT_BOX' + config.vm.box = ENV['VAGRANT_BOX'] + else + config.vm.box = 'precise64' + config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box' + end config.hostmanager.enabled = true config.hostmanager.manage_host = true From 68aee1d3d938b9f63b232917dcb811ceb11c8bdb Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Mar 2014 22:11:26 -0400 Subject: [PATCH 11/12] create changelog --- CHANGELOG.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e3cdad0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,30 @@ +# Changelog + +## Upcoming +### Features +* supports vagrant 1.5 [[#80](https://github.com/smdahlen/vagrant-hostmanager/issues/80), [#81](https://github.com/smdahlen/vagrant-hostmanager/pull/81)] +* only updates hosts file if contents have changed [[#78](https://github.com/smdahlen/vagrant-hostmanager/pull/78)] +* custom ip resolver now has access to the machine whose hosts file is being updated [[#62](https://github.com/smdahlen/vagrant-hostmanager/pull/62)] + +### Bug fixes +* custom IP resolver result no longer ignored [[#57](https://github.com/smdahlen/vagrant-hostmanager/pull/57)] +* when multiple private_networks are configured, the first one is used [[#64](https://github.com/smdahlen/vagrant-hostmanager/pull/64)] +* destroyed machines are now removed from hosts file [[#52](https://github.com/smdahlen/vagrant-hostmanager/pull/52)] + +[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...master) + + +## 1.3.0 +### Features +* allow defining a custom IP resolver block [[#15](https://github.com/smdahlen/vagrant-hostmanager/pull/15)] +* handle removing destroyed machines from hosts file (currently only works with `include_offline = true`) [[#45](https://github.com/smdahlen/vagrant-hostmanager/pull/45)] +* attempt to elevate privileges when needed in Windows hosts [[#48](https://github.com/smdahlen/vagrant-hostmanager/pull/48)] + +### Bug fixes +* `--provider` command-line option now finds machines as expected [[#46](https://github.com/smdahlen/vagrant-hostmanager/pull/46)] +* uses proper `hosts` file location in Windows under cygwin [[#49](https://github.com/smdahlen/vagrant-hostmanager/pull/49)] + +### Miscelaneous +* MIT license added to gemspec + +[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.2.3...v1.3.0) From b768485fa335a5882944e0eda4c977d24a070063 Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Mon, 17 Mar 2014 22:30:20 -0400 Subject: [PATCH 12/12] bump for 1.4.0 release --- CHANGELOG.md | 4 ++-- lib/vagrant-hostmanager/version.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3cdad0..18b2379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## Upcoming +## 1.4.0 ### Features * supports vagrant 1.5 [[#80](https://github.com/smdahlen/vagrant-hostmanager/issues/80), [#81](https://github.com/smdahlen/vagrant-hostmanager/pull/81)] * only updates hosts file if contents have changed [[#78](https://github.com/smdahlen/vagrant-hostmanager/pull/78)] @@ -11,7 +11,7 @@ * when multiple private_networks are configured, the first one is used [[#64](https://github.com/smdahlen/vagrant-hostmanager/pull/64)] * destroyed machines are now removed from hosts file [[#52](https://github.com/smdahlen/vagrant-hostmanager/pull/52)] -[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...master) +[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...v1.4.0) ## 1.3.0 diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 6f78a08..6410bd0 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.3.0' + VERSION = '1.4.0' end end