Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update hosts on halt comand #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ command:
$ vagrant hostmanager

The plugin hooks into the `vagrant up` and `vagrant destroy` commands
automatically.
automatically. If you want to update the hosts on the `vagrant halt` command as well,
set the `hostmanager.update_on_halt` attribute to `true`
When a machine enters or exits the running state , all active
machines with the same provider will have their `hosts` file updated
accordingly. Set the `hostmanager.enabled` attribute to `true` in the
Expand Down
6 changes: 6 additions & 0 deletions lib/vagrant-hostmanager/action/update_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def call(env)
# skip if machine is not active on destroy action
return @app.call(env) if [email protected] && env[:machine_action] == :destroy


# check if update_on_halt is true on halt action
if env[:machine_action] == :halt
return @app.call(env) unless @config.hostmanager.update_on_halt?
end

# check config to see if the hosts file should be update automatically
return @app.call(env) unless @config.hostmanager.enabled?
@logger.info 'Updating /etc/hosts file automatically'
Expand Down
5 changes: 5 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :aliases
attr_accessor :include_offline
attr_accessor :ip_resolver
attr_accessor :update_on_halt

alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
alias_method :manage_host?, :manage_host
alias_method :manage_guest?, :manage_guest
alias_method :update_on_halt?, :update_on_halt

def initialize
@enabled = UNSET_VALUE
Expand All @@ -22,6 +24,7 @@ def initialize
@include_offline = UNSET_VALUE
@aliases = UNSET_VALUE
@ip_resolver = UNSET_VALUE
@update_on_halt = UNSET_VALUE
end

def finalize!
Expand All @@ -32,6 +35,7 @@ def finalize!
@include_offline = false if @include_offline == UNSET_VALUE
@aliases = [] if @aliases == UNSET_VALUE
@ip_resolver = nil if @ip_resolver == UNSET_VALUE
@update_on_halt = false if @update_on_halt == UNSET_VALUE

@aliases = [ @aliases ].flatten
end
Expand All @@ -44,6 +48,7 @@ def validate(machine)
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 << validate_bool('hostmanager.update_on_halt', @update_on_halt)
errors.compact!

# check if aliases option is an Array
Expand Down
5 changes: 5 additions & 0 deletions lib/vagrant-hostmanager/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Plugin < Vagrant.plugin('2')
hook.after(Vagrant::Action::Builtin::Provision, Action.update_all)
end

action_hook(:hostmanager, :machine_action_halt) do |hook|
hook.prepend(Action.update_all)
end


action_hook(:hostmanager, :machine_action_destroy) do |hook|
hook.prepend(Action.update_all)
end
Expand Down