Fix config validation.

This commit fixes issue #5. Additionally, it deactivates the auto
update behavior by default. Users must set `enabled` to true to activate
auto updates for the hosts file.
This commit is contained in:
Shawn Dahlen 2013-04-27 08:10:45 -04:00
parent ba0decdaa2
commit 264856478f
5 changed files with 23 additions and 21 deletions

View File

@ -21,17 +21,17 @@ Install the plugin following the typical Vagrant 1.1 procedure:
Usage Usage
----- -----
The plugin hooks into the `vagrant up` and `vagrant destroy` commands To update the `/etc/hosts` file on each active machine, run the following
automatically. When a machine is created or destroyed, all active command:
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.
To update the `/etc/hosts` file on each active machine manually, run the
following command:
$ vagrant hostmanager $ vagrant hostmanager
The plugin may hook 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. Set the `hostmanager.enabled` attribute to `true` in the
Vagrantfile to activate this behavior.
A machine's IP address is defined by either the static IP for a private A machine's IP address is defined by either the static IP for a private
network configuration or by the SSH host configuration. To disable network configuration or by the SSH host configuration. To disable
using the private network IP address, set `config.hostmanger.ignore_private_ip` using the private network IP address, set `config.hostmanger.ignore_private_ip`
@ -47,7 +47,7 @@ Example configuration:
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.hostmanager.auto_update = true config.hostmanager.enabled = true
config.hostmanager.ignore_private_ip = false config.hostmanager.ignore_private_ip = false
config.vm.define "example-box" do |node| config.vm.define "example-box" do |node|
node.vm.hostname = "example-box-hostname" node.vm.hostname = "example-box-hostname"

View File

@ -17,7 +17,7 @@ module VagrantPlugins
return @app.call(env) if @machine.id return @app.call(env) if @machine.id
# 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) if !@machine.config.hostmanager.auto_update return @app.call(env) unless @machine.config.hostmanager.enabled?
@logger.info 'Updating /etc/hosts file automatically' @logger.info 'Updating /etc/hosts file automatically'
# continue the action stack so the machine will be created # continue the action stack so the machine will be created

View File

@ -1,31 +1,33 @@
module VagrantPlugins module VagrantPlugins
module HostManager module HostManager
class Config < Vagrant.plugin('2', :config) class Config < Vagrant.plugin('2', :config)
attr_accessor :auto_update attr_accessor :enabled
attr_accessor :ignore_private_ip attr_accessor :ignore_private_ip
attr_accessor :aliases attr_accessor :aliases
alias_method :enabled?, :enabled
def initialize def initialize
@auto_update = UNSET_VALUE @enabled = false
@ignore_private_ip = UNSET_VALUE @ignore_private_ip = UNSET_VALUE
@aliases = Array.new @aliases = Array.new
end end
def finalize! def finalize!
@auto_update = true if @auto_update == UNSET_VALUE
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
end end
def validate(machine) def validate(machine)
errors = Array.new errors = Array.new
# check if auto_update option is either true or false # check if enabled option is either true or false
if ![TrueClass, FalseClass].include?(auto_update.class) if ![TrueClass, FalseClass].include?(enabled.class)
errors << "A value for hostmanager.auto_update can be true or false." errors << "A value for hostmanager.enabled can be true or false."
end end
# check if ignore_private_ip option is either true or false # check if ignore_private_ip option is either true or false
if ![TrueClass, FalseClass].include?(ignore_private_ip.class) if ![TrueClass, FalseClass].include?(ignore_private_ip.class) &&
@ignore_private_ip != UNSET_VALUE
errors << "A value for hostmanager.ignore_private_ip can be true or false." errors << "A value for hostmanager.ignore_private_ip can be true or false."
end end

View File

@ -1,5 +1,5 @@
module VagrantPlugins module VagrantPlugins
module HostManager module HostManager
VERSION = '0.1.2' VERSION = '0.2.0'
end end
end end

2
test/Vagrantfile vendored
View File

@ -7,7 +7,7 @@ Vagrant.configure('2') do |config|
config.vm.box = 'precise64-chef11.2' 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.vm.box_url = 'https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.2.0.box'
config.hostmanager.auto_update = true config.hostmanager.enabled = true
config.vm.define :server1 do |server| config.vm.define :server1 do |server|
server.vm.hostname = 'fry' server.vm.hostname = 'fry'