Skip to content

Commit

Permalink
Implement check for already active machine.
Browse files Browse the repository at this point in the history
This commit re-instates the behavior of auto-updating the
/etc/hosts file on each active machine. If machine is already
active, no update will occur.
  • Loading branch information
smdahlen committed Apr 8, 2013
1 parent 39c197d commit 33c391c
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 116 deletions.
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,21 @@ Install the plugin following the typical Vagrant 1.1 procedure:

Usage
-----
To update the `/etc/hosts` file on each active machine, run the following
command:
The plugin hooks into the `vagrant up` and `vagrant destroy` commands
automatically. When a machine is created or destroyed, all active
machines with the same provider will have their `/etc/hosts` file updated
accordingly. Auto update may be disabled by setting the
`config.hostmanager.auto_update` attribute to false in the Vagrantfile.

$ vagrant hostmanager

The plugin may hook into the `vagrant up` and `vagrant destroy` commands
automatically to update the `/etc/hosts` file on each active machine that
is using the same provider. To enable this, add the following configuration
to your Vagrant file:
To update the `/etc/hosts` file on each active machine manually, run the
following command:

```ruby
Vagrant.configure('2') do |config|
config.hostmanager.auto_update = true
end
```
$ vagrant hostmanager

A machine's IP address is defined by either the static IP for a private
network configuration or by the SSH host configuration.
network configuration or by the SSH host configuration. To disable
using the private network IP address, set `config.hostmanger.ignore_private_ip`
to true.

A machine's host name is defined by `config.vm.hostname`. If this is not
set, it falls back to the symbol defining the machine in the Vagrantfile.
Expand Down
4 changes: 3 additions & 1 deletion lib/vagrant-hostmanager.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'vagrant'
require 'vagrant-hostmanager/plugin'
require 'vagrant-hostmanager/version'
require 'vagrant-hostmanager/errors'
Expand All @@ -8,5 +7,8 @@ module HostManager
def self.source_root
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
end

I18n.load_path << File.expand_path('locales/en.yml', source_root)
I18n.reload!
end
end
21 changes: 11 additions & 10 deletions lib/vagrant-hostmanager/action/update_hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ class UpdateHostsFile
def initialize(app, env)
@app = app
@machine = env[:machine]
@translator = Helpers::Translator.new('action.update_hosts_file')
@logger =
Log4r::Logger.new('vagrant_hostmanager::action::update')
@logger = Log4r::Logger.new('vagrant::hostmanager::update_hosts_file')
end

def call(env)
# check config to see if the hosts file should updated automatically
if @machine.config.hostmanager.auto_update
# generate temporary hosts file
machines = generate(@machine.env, @machine.provider_name)
# check if machine is already active
return @app.call(env) if @machine.id

# update /etc/hosts file on each active machine
machines.each { |machine| update(machine) }
end
# check config to see if the hosts file should be update automatically
return @app.call(env) if !@machine.config.hostmanager.auto_update
@logger.info 'Updating /etc/hosts file automatically'

# continue the action stack so the machine will be created
@app.call(env)

# update /etc/hosts file on each active machine
machines = generate(@machine.env, @machine.provider_name)
machines.each { |machine| update(machine) }
end
end
end
Expand Down
14 changes: 6 additions & 8 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ module VagrantPlugins
module HostManager
class Config < Vagrant.plugin('2', :config)
attr_accessor :auto_update
attr_accessor :ignore_private_ip

def initialize
@auto_update = false
@auto_update = UNSET_VALUE
@ignore_private_ip = UNSET_VALUE
end

def validate(machine)
errors = []
if !(!!@auto_update == @auto_update)
errors << 'auto_update must be a boolean'
end

{ 'hostmanager' => errors }
def finalize!
@auto_update = true if @auto_update == UNSET_VALUE
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
end
end
end
Expand Down
20 changes: 0 additions & 20 deletions lib/vagrant-hostmanager/helpers/translator.rb

This file was deleted.

18 changes: 9 additions & 9 deletions lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ def generate(env, provider)
# define a lambda for looking up a machine's ip address
get_ip_address = lambda do |machine|
ip = nil
machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
next if ip
unless machine.config.hostmanager.ignore_private_ip
machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
next if ip
end
end
ip || machine.ssh_info[:host]
end
Expand Down Expand Up @@ -43,15 +45,13 @@ def generate(env, provider)
def update(machine)
path = machine.env.tmp_path.join('hosts')
if machine.communicate.ready?
machine.env.ui.info translator.t('update', { :name => machine.name })
machine.env.ui.info I18n.t('vagrant_hostmanager.action.update', {
:name => machine.name
})
machine.communicate.upload(path, '/tmp/hosts')
machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
end
end

def translator
Helpers::Translator.new('hosts_file')
end
end
end
end
56 changes: 8 additions & 48 deletions lib/vagrant-hostmanager/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,30 @@
require 'vagrant-hostmanager/helpers/translator'
require 'vagrant-hostmanager/action/update_hosts_file'

module VagrantPlugins
module HostManager
class Plugin < Vagrant.plugin('2')
name 'HostManager'
description <<-DESC
This plugin manages the /etc/hosts file for guest machines. A entry is
This plugin manages the /etc/hosts file for guest machines. An entry is
created for each active machine using the hostname attribute.
DESC

def self.update(hook)
setup_i18n
setup_logging
hook.append(Action::UpdateHostsFile)
end

config(:hostmanager) do
require_relative 'config'
Config
end

action_hook(:hostmanager_up, :machine_action_up, &method(:update))
action_hook(:hostmanger_destroy, :machine_action_destroy, &method(:update))

# TODO remove duplication of i18n and logging setup
command(:hostmanager) do
setup_i18n
setup_logging
require_relative 'command'
Command
action_hook(:hostmanager, :machine_action_up) do |hook|
hook.prepend(Action::UpdateHostsFile)
end

def self.setup_i18n
I18n.load_path << File.expand_path(
'locales/en.yml',
HostManager.source_root)
I18n.reload!

Helpers::Translator.plugin_namespace = 'vagrant_hostmanager'
action_hook(:hostmanager, :machine_action_destroy) do |hook|
hook.append(Action::UpdateHostsFile)
end

def self.setup_logging
level = nil
begin
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
rescue NameError
# This means that the logging constant wasn't found,
# which is fine. We just keep `level` as `nil`. But
# we tell the user.
level = nil
end

# Some constants, such as "true" resolve to booleans, so the
# above error checking doesn't catch it. This will check to make
# sure that the log level is an integer, as Log4r requires.
level = nil if !level.is_a?(Integer)

# Set the logging level on all "vagrant" namespaced
# logs as long as we have a valid level.
if level
logger = Log4r::Logger.new("vagrant_hostmanager")
logger.outputters = Log4r::Outputter.stderr
logger.level = level
logger = nil
end
command(:hostmanager) do
require_relative 'command'
Command
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-hostmanager/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module VagrantPlugins
module HostManager
VERSION = '0.0.4'
VERSION = '0.1.0'
end
end
2 changes: 1 addition & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
en:
vagrant_hostmanager:
hosts_file:
action:
update: "[%{name}] Updating /etc/hosts file"
2 changes: 2 additions & 0 deletions test/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Vagrant.configure('2') do |config|
config.vm.box = 'precise64-chef11.2'
config.vm.box_url = 'https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.2.0.box'

config.hostmanager.auto_update = false

config.vm.define :server1 do |server|
server.vm.hostname = 'fry'
server.vm.network :private_network, :ip => '10.0.5.2'
Expand Down
4 changes: 0 additions & 4 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
cd test

vagrant up
echo "[server1] /etc/hosts file:"
vagrant ssh server1 -c 'cat /etc/hosts'
echo "[server2] /etc/hosts file:"
vagrant ssh server2 -c 'cat /etc/hosts'

vagrant hostmanager

Expand Down

0 comments on commit 33c391c

Please sign in to comment.