Skip to content

Commit

Permalink
Fix config validation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smdahlen committed Apr 27, 2013
1 parent ba0decd commit 2648564
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ Install the plugin following the typical Vagrant 1.1 procedure:

Usage
-----
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.

To update the `/etc/hosts` file on each active machine manually, run the
following command:
To update the `/etc/hosts` file on each active machine, run the following
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
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
network configuration or by the SSH host configuration. To disable
using the private network IP address, set `config.hostmanger.ignore_private_ip`
Expand All @@ -47,7 +47,7 @@ Example configuration:

```ruby
Vagrant.configure("2") do |config|
config.hostmanager.auto_update = true
config.hostmanager.enabled = true
config.hostmanager.ignore_private_ip = false
config.vm.define "example-box" do |node|
node.vm.hostname = "example-box-hostname"
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-hostmanager/action/update_hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call(env)
return @app.call(env) if @machine.id

# 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'

# continue the action stack so the machine will be created
Expand Down
20 changes: 11 additions & 9 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
module VagrantPlugins
module HostManager
class Config < Vagrant.plugin('2', :config)
attr_accessor :auto_update
attr_accessor :enabled
attr_accessor :ignore_private_ip
attr_accessor :aliases

alias_method :enabled?, :enabled

def initialize
@auto_update = UNSET_VALUE
@enabled = false
@ignore_private_ip = UNSET_VALUE
@aliases = Array.new
@aliases = Array.new
end

def finalize!
@auto_update = true if @auto_update == UNSET_VALUE
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
end

def validate(machine)
errors = Array.new

# check if auto_update option is either true or false
if ![TrueClass, FalseClass].include?(auto_update.class)
errors << "A value for hostmanager.auto_update can be true or false."
# check if enabled option is either true or false
if ![TrueClass, FalseClass].include?(enabled.class)
errors << "A value for hostmanager.enabled can be true or false."
end

# 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."
end

# check if aliases option is an Array
if !machine.config.hostmanager.aliases.kind_of?(Array)
errors << "A value for hostmanager.aliases must be an Array."
end

{ "HostManager configuration" => errors }
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.1.2'
VERSION = '0.2.0'
end
end
2 changes: 1 addition & 1 deletion test/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 = true
config.hostmanager.enabled = true

config.vm.define :server1 do |server|
server.vm.hostname = 'fry'
Expand Down

0 comments on commit 2648564

Please sign in to comment.