diff --git a/README.md b/README.md index 4951d86..92b7ada 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/vagrant-hostmanager/action/update_all.rb b/lib/vagrant-hostmanager/action/update_all.rb index 660e2b5..3ec6562 100644 --- a/lib/vagrant-hostmanager/action/update_all.rb +++ b/lib/vagrant-hostmanager/action/update_all.rb @@ -20,6 +20,12 @@ def call(env) # skip if machine is not active on destroy action return @app.call(env) if !@machine.id && 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' diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 1ae26f4..96f8794 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -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 @@ -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! @@ -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 @@ -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 diff --git a/lib/vagrant-hostmanager/plugin.rb b/lib/vagrant-hostmanager/plugin.rb index b299315..715648d 100644 --- a/lib/vagrant-hostmanager/plugin.rb +++ b/lib/vagrant-hostmanager/plugin.rb @@ -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