Skip to content

Commit

Permalink
Merge pull request #34 from jeremiahsnapp/define_active_as_running
Browse files Browse the repository at this point in the history
Add hooks & define active/offline as running/not
  • Loading branch information
smdahlen committed Aug 15, 2013
2 parents 063c749 + 1cb7fc9 commit 2f1e3e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions lib/vagrant-hostmanager/action/update_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ def initialize(app, env)
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?
Expand Down
19 changes: 9 additions & 10 deletions lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,19 @@ def get_ip_address(machine)
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
Expand Down
14 changes: 13 additions & 1 deletion lib/vagrant-hostmanager/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Plugin < Vagrant.plugin('2')
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
Expand All @@ -24,6 +24,18 @@ class Plugin < Vagrant.plugin('2')
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
Expand Down

0 comments on commit 2f1e3e0

Please sign in to comment.