Merge pull request #34 from jeremiahsnapp/define_active_as_running

Add hooks & define active/offline as running/not
This commit is contained in:
Shawn Dahlen 2013-08-15 08:15:51 -07:00
commit 2f1e3e0b63
4 changed files with 33 additions and 17 deletions

View File

@ -18,8 +18,9 @@ command:
$ vagrant hostmanager $ vagrant hostmanager
The plugin may hook into the `vagrant up` and `vagrant destroy` commands The plugin hooks into the `vagrant up`, `vagrant destroy`, `vagrant halt`,
automatically. When a machine is created or destroyed, all active `vagrant resume` and `vagrant suspend` commands automatically.
When a machine enters or exits the running state , all active
machines with the same provider will have their `/etc/hosts` file updated machines with the same provider will have their `/etc/hosts` file updated
accordingly. Set the `hostmanager.enabled` attribute to `true` in the accordingly. Set the `hostmanager.enabled` attribute to `true` in the
Vagrantfile to activate this behavior. Vagrantfile to activate this behavior.

View File

@ -15,10 +15,14 @@ module VagrantPlugins
end end
def call(env) def call(env)
# skip if machine is already active on up action # skip if machine is running and the action is resume or up
return @app.call(env) if @machine.id && env[:machine_action] == :up return @app.call(env) if @machine.state.id == :running && [:resume, :up].include?(env[:machine_action])
# skip if machine is not active on destroy action # skip if machine is not running and the action is destroy, halt or suspend
return @app.call(env) if !@machine.id && env[:machine_action] == :destroy return @app.call(env) if @machine.state.id != :running && [:destroy, :halt, :suspend].include?(env[:machine_action])
# skip if machine is not saved and the action is resume
return @app.call(env) if @machine.state.id != :saved && env[:machine_action] == :resume
# skip if machine is not running and the action is suspend
return @app.call(env) if @machine.state.id != :running && env[:machine_action] == :suspend
# check config to see if the hosts file should be update automatically # check config to see if the hosts file should be update automatically
return @app.call(env) unless @machine.config.hostmanager.enabled? return @app.call(env) unless @machine.config.hostmanager.enabled?

View File

@ -72,20 +72,19 @@ module VagrantPlugins
end end
def get_machines def get_machines
# check if offline machines should be included in host entries machines = []
if @global_env.config_global.hostmanager.include_offline? @global_env.machine_names.each do |name|
machines = [] begin
@global_env.machine_names.each do |name| machine = @global_env.machine(name, @provider)
begin
@global_env.machine(name, @provider) # check if offline machines should be included in host entries
if @global_env.config_global.hostmanager.include_offline? || machine.state.id == :running
machines << [name, @provider] machines << [name, @provider]
rescue Vagrant::Errors::MachineNotFound
end end
rescue Vagrant::Errors::MachineNotFound
end end
machines
else
@global_env.active_machines
end end
machines
end end
end end

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name 'HostManager' name 'HostManager'
description <<-DESC description <<-DESC
This plugin manages the /etc/hosts file for the host and guest machines. This plugin manages the /etc/hosts file for the host and guest machines.
An entry is created for each active machine using the hostname attribute. An entry is created for each running machine using the hostname attribute.
You can also use the hostmanager provisioner to update the hosts file. You can also use the hostmanager provisioner to update the hosts file.
DESC DESC
@ -24,6 +24,18 @@ module VagrantPlugins
hook.prepend(Action.update_all) hook.prepend(Action.update_all)
end end
action_hook(:hostmanager, :machine_action_halt) do |hook|
hook.prepend(Action.update_all)
end
action_hook(:hostmanager, :machine_action_resume) do |hook|
hook.prepend(Action.update_all)
end
action_hook(:hostmanager, :machine_action_suspend) do |hook|
hook.prepend(Action.update_all)
end
provisioner(:hostmanager) do provisioner(:hostmanager) do
require_relative 'provisioner' require_relative 'provisioner'
Provisioner Provisioner