From 7c06c812f6cd6fd56e97fcdd2bb04e248f7247c8 Mon Sep 17 00:00:00 2001 From: Damien Joldersma Date: Fri, 14 Nov 2014 12:42:54 -0800 Subject: [PATCH] Fix for issue #108, add a flag to manage guest hosts file. --- README.md | 4 ++++ lib/vagrant-hostmanager/action/update_all.rb | 12 +++++++----- lib/vagrant-hostmanager/action/update_guest.rb | 14 +++++++++----- lib/vagrant-hostmanager/command.rb | 1 + lib/vagrant-hostmanager/config.rb | 5 +++++ lib/vagrant-hostmanager/provisioner.rb | 4 +++- test/Vagrantfile | 1 + 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f851728..282b6b2 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ Vagrantfile to activate this behavior. To update the host's `/etc/hosts` file, set the `hostmanager.manage_host` attribute to `true`. +To update the guests' `/etc/hosts` file, set the `hostmanager.manage_guest` +attribute to `true`. + A machine's IP address is defined by either the static IP for a private network configuration or by the SSH host configuration. To disable using the private network IP address, set `config.hostmanager.ignore_private_ip` @@ -60,6 +63,7 @@ Example configuration: Vagrant.configure("2") do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true + config.hostmanager.manage_guest = true config.hostmanager.ignore_private_ip = false config.hostmanager.include_offline = true config.vm.define 'example-box' do |node| diff --git a/lib/vagrant-hostmanager/action/update_all.rb b/lib/vagrant-hostmanager/action/update_all.rb index f7b88ee..ee87734 100644 --- a/lib/vagrant-hostmanager/action/update_all.rb +++ b/lib/vagrant-hostmanager/action/update_all.rb @@ -27,11 +27,13 @@ module VagrantPlugins @app.call(env) # update /etc/hosts file on active machines - env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests') - @global_env.active_machines.each do |name, p| - if p == @provider - machine = @global_env.machine(name, p) - @updater.update_guest(machine) + if @machine.config.hostmanager.manage_guest? + env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests') + @global_env.active_machines.each do |name, p| + if p == @provider + machine = @global_env.machine(name, p) + @updater.update_guest(machine) + end end end diff --git a/lib/vagrant-hostmanager/action/update_guest.rb b/lib/vagrant-hostmanager/action/update_guest.rb index 3ca99d9..a957608 100644 --- a/lib/vagrant-hostmanager/action/update_guest.rb +++ b/lib/vagrant-hostmanager/action/update_guest.rb @@ -8,18 +8,22 @@ module VagrantPlugins def initialize(app, env) @app = app + global_env = env[:global_env] + @config = Util.get_config(global_env) @machine = env[:machine] @updater = HostsFile::Updater.new(@machine.env, env[:provider]) @logger = Log4r::Logger.new('vagrant::hostmanager::update_guest') end def call(env) - env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', { - :name => @machine.name - }) - @updater.update_guest(@machine) + if @config.hostmanager.manage_guest? + env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', { + :name => @machine.name + }) + @updater.update_guest(@machine) - @app.call(env) + @app.call(env) + end end end end diff --git a/lib/vagrant-hostmanager/command.rb b/lib/vagrant-hostmanager/command.rb index 5d320c3..a52fe4e 100644 --- a/lib/vagrant-hostmanager/command.rb +++ b/lib/vagrant-hostmanager/command.rb @@ -27,6 +27,7 @@ module VagrantPlugins # update /etc/hosts file for specified guest machines with_target_vms(argv, options) do |machine| @env.action_runner.run(Action.update_guest, { + :global_env => @env, :machine => machine, :provider => options[:provider] }) diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 4e3d3dc..45a90c5 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -3,6 +3,7 @@ module VagrantPlugins class Config < Vagrant.plugin('2', :config) attr_accessor :enabled attr_accessor :manage_host + attr_accessor :manage_guest attr_accessor :ignore_private_ip attr_accessor :aliases attr_accessor :include_offline @@ -11,10 +12,12 @@ module VagrantPlugins alias_method :enabled?, :enabled alias_method :include_offline?, :include_offline alias_method :manage_host?, :manage_host + alias_method :manage_guest?, :manage_guest def initialize @enabled = UNSET_VALUE @manage_host = UNSET_VALUE + @manage_guest = UNSET_VALUE @ignore_private_ip = UNSET_VALUE @include_offline = UNSET_VALUE @aliases = UNSET_VALUE @@ -24,6 +27,7 @@ module VagrantPlugins def finalize! @enabled = false if @enabled == UNSET_VALUE @manage_host = false if @manage_host == UNSET_VALUE + @manage_guest = false if @manage_guest == UNSET_VALUE @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE @include_offline = false if @include_offline == UNSET_VALUE @aliases = [] if @aliases == UNSET_VALUE @@ -37,6 +41,7 @@ module VagrantPlugins errors << validate_bool('hostmanager.enabled', @enabled) errors << validate_bool('hostmanager.manage_host', @manage_host) + errors << validate_bool('hostmanager.manage_guest', @manage_guest) errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip) errors << validate_bool('hostmanager.include_offline', @include_offline) errors.compact! diff --git a/lib/vagrant-hostmanager/provisioner.rb b/lib/vagrant-hostmanager/provisioner.rb index 1576c73..cbe6f7e 100644 --- a/lib/vagrant-hostmanager/provisioner.rb +++ b/lib/vagrant-hostmanager/provisioner.rb @@ -12,7 +12,9 @@ module VagrantPlugins end def provision - @updater.update_guest(@machine) + if @config.hostmanager.manage_guest? + @updater.update_guest(@machine) + end if @config.hostmanager.manage_host? @updater.update_host end diff --git a/test/Vagrantfile b/test/Vagrantfile index 59ca656..33c5280 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -16,6 +16,7 @@ Vagrant.configure('2') do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true + config.hostmanager.manage_guest = true config.vm.define :server1 do |server| server.vm.hostname = 'fry'