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
The plugin may hook into the `vagrant up` and `vagrant destroy` commands
automatically. When a machine is created or destroyed, all active
The plugin hooks into the `vagrant up`, `vagrant destroy`, `vagrant halt`,
`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
accordingly. Set the `hostmanager.enabled` attribute to `true` in the
Vagrantfile to activate this behavior.

View File

@ -15,10 +15,14 @@ module VagrantPlugins
end
def call(env)
# skip if machine is already active on up action
return @app.call(env) if @machine.id && env[:machine_action] == :up
# skip if machine is not active on destroy action
return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
# skip if machine is running and the action is resume or up
return @app.call(env) if @machine.state.id == :running && [:resume, :up].include?(env[:machine_action])
# skip if machine is not running and the action is destroy, halt or suspend
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
return @app.call(env) unless @machine.config.hostmanager.enabled?

View File

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

View File

@ -6,7 +6,7 @@ module VagrantPlugins
name 'HostManager'
description <<-DESC
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.
DESC
@ -24,6 +24,18 @@ module VagrantPlugins
hook.prepend(Action.update_all)
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
require_relative 'provisioner'
Provisioner