Merge pull request #125 from damienjoldersma/feature/manage-guest-flag

Fix for issue #108, add a flag to manage guest hosts file.
This commit is contained in:
Seth Reeser 2016-02-11 10:50:44 -05:00
commit 3b840be5fe
7 changed files with 30 additions and 11 deletions

View File

@ -46,6 +46,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`
@ -66,6 +69,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|

View File

@ -27,6 +27,7 @@ module VagrantPlugins
@app.call(env)
# update /etc/hosts file on active machines
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
@ -34,6 +35,7 @@ module VagrantPlugins
@updater.update_guest(machine)
end
end
end
# update /etc/hosts files on host if enabled
if @machine.config.hostmanager.manage_host?

View File

@ -8,12 +8,15 @@ 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)
if @config.hostmanager.manage_guest?
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
:name => @machine.name
})
@ -24,4 +27,5 @@ module VagrantPlugins
end
end
end
end
end

View File

@ -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]
})

View File

@ -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!

View File

@ -12,7 +12,9 @@ module VagrantPlugins
end
def provision
if @config.hostmanager.manage_guest?
@updater.update_guest(@machine)
end
if @config.hostmanager.manage_host?
@updater.update_host
end

1
test/Vagrantfile vendored
View File

@ -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'