Skip to content

Commit 7c06c81

Browse files
author
Damien Joldersma
committed
Fix for issue #108, add a flag to manage guest hosts file.
1 parent 176d70e commit 7c06c81

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Vagrantfile to activate this behavior.
4040
To update the host's `/etc/hosts` file, set the `hostmanager.manage_host`
4141
attribute to `true`.
4242

43+
To update the guests' `/etc/hosts` file, set the `hostmanager.manage_guest`
44+
attribute to `true`.
45+
4346
A machine's IP address is defined by either the static IP for a private
4447
network configuration or by the SSH host configuration. To disable
4548
using the private network IP address, set `config.hostmanager.ignore_private_ip`
@@ -60,6 +63,7 @@ Example configuration:
6063
Vagrant.configure("2") do |config|
6164
config.hostmanager.enabled = true
6265
config.hostmanager.manage_host = true
66+
config.hostmanager.manage_guest = true
6367
config.hostmanager.ignore_private_ip = false
6468
config.hostmanager.include_offline = true
6569
config.vm.define 'example-box' do |node|

lib/vagrant-hostmanager/action/update_all.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ def call(env)
2727
@app.call(env)
2828

2929
# update /etc/hosts file on active machines
30-
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests')
31-
@global_env.active_machines.each do |name, p|
32-
if p == @provider
33-
machine = @global_env.machine(name, p)
34-
@updater.update_guest(machine)
30+
if @machine.config.hostmanager.manage_guest?
31+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests')
32+
@global_env.active_machines.each do |name, p|
33+
if p == @provider
34+
machine = @global_env.machine(name, p)
35+
@updater.update_guest(machine)
36+
end
3537
end
3638
end
3739

lib/vagrant-hostmanager/action/update_guest.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ class UpdateGuest
88

99
def initialize(app, env)
1010
@app = app
11+
global_env = env[:global_env]
12+
@config = Util.get_config(global_env)
1113
@machine = env[:machine]
1214
@updater = HostsFile::Updater.new(@machine.env, env[:provider])
1315
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
1416
end
1517

1618
def call(env)
17-
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
18-
:name => @machine.name
19-
})
20-
@updater.update_guest(@machine)
19+
if @config.hostmanager.manage_guest?
20+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
21+
:name => @machine.name
22+
})
23+
@updater.update_guest(@machine)
2124

22-
@app.call(env)
25+
@app.call(env)
26+
end
2327
end
2428
end
2529
end

lib/vagrant-hostmanager/command.rb

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def execute
2727
# update /etc/hosts file for specified guest machines
2828
with_target_vms(argv, options) do |machine|
2929
@env.action_runner.run(Action.update_guest, {
30+
:global_env => @env,
3031
:machine => machine,
3132
:provider => options[:provider]
3233
})

lib/vagrant-hostmanager/config.rb

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module HostManager
33
class Config < Vagrant.plugin('2', :config)
44
attr_accessor :enabled
55
attr_accessor :manage_host
6+
attr_accessor :manage_guest
67
attr_accessor :ignore_private_ip
78
attr_accessor :aliases
89
attr_accessor :include_offline
@@ -11,10 +12,12 @@ class Config < Vagrant.plugin('2', :config)
1112
alias_method :enabled?, :enabled
1213
alias_method :include_offline?, :include_offline
1314
alias_method :manage_host?, :manage_host
15+
alias_method :manage_guest?, :manage_guest
1416

1517
def initialize
1618
@enabled = UNSET_VALUE
1719
@manage_host = UNSET_VALUE
20+
@manage_guest = UNSET_VALUE
1821
@ignore_private_ip = UNSET_VALUE
1922
@include_offline = UNSET_VALUE
2023
@aliases = UNSET_VALUE
@@ -24,6 +27,7 @@ def initialize
2427
def finalize!
2528
@enabled = false if @enabled == UNSET_VALUE
2629
@manage_host = false if @manage_host == UNSET_VALUE
30+
@manage_guest = false if @manage_guest == UNSET_VALUE
2731
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
2832
@include_offline = false if @include_offline == UNSET_VALUE
2933
@aliases = [] if @aliases == UNSET_VALUE
@@ -37,6 +41,7 @@ def validate(machine)
3741

3842
errors << validate_bool('hostmanager.enabled', @enabled)
3943
errors << validate_bool('hostmanager.manage_host', @manage_host)
44+
errors << validate_bool('hostmanager.manage_guest', @manage_guest)
4045
errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip)
4146
errors << validate_bool('hostmanager.include_offline', @include_offline)
4247
errors.compact!

lib/vagrant-hostmanager/provisioner.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def initialize(machine, config)
1212
end
1313

1414
def provision
15-
@updater.update_guest(@machine)
15+
if @config.hostmanager.manage_guest?
16+
@updater.update_guest(@machine)
17+
end
1618
if @config.hostmanager.manage_host?
1719
@updater.update_host
1820
end

test/Vagrantfile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Vagrant.configure('2') do |config|
1616

1717
config.hostmanager.enabled = true
1818
config.hostmanager.manage_host = true
19+
config.hostmanager.manage_guest = true
1920

2021
config.vm.define :server1 do |server|
2122
server.vm.hostname = 'fry'

0 commit comments

Comments
 (0)