From 264856478ff782ebad0e3be8b9ba28541d4d16da Mon Sep 17 00:00:00 2001 From: Shawn Dahlen Date: Sat, 27 Apr 2013 08:10:45 -0400 Subject: [PATCH] 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. --- README.md | 18 ++++++++--------- .../action/update_hosts_file.rb | 2 +- lib/vagrant-hostmanager/config.rb | 20 ++++++++++--------- lib/vagrant-hostmanager/version.rb | 2 +- test/Vagrantfile | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5b82741..62f487f 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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" diff --git a/lib/vagrant-hostmanager/action/update_hosts_file.rb b/lib/vagrant-hostmanager/action/update_hosts_file.rb index 8165cb1..292b7f6 100644 --- a/lib/vagrant-hostmanager/action/update_hosts_file.rb +++ b/lib/vagrant-hostmanager/action/update_hosts_file.rb @@ -17,7 +17,7 @@ module VagrantPlugins 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 diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index a8079a2..feeeeaa 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -1,31 +1,33 @@ 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 @@ -33,7 +35,7 @@ module VagrantPlugins if !machine.config.hostmanager.aliases.kind_of?(Array) errors << "A value for hostmanager.aliases must be an Array." end - + { "HostManager configuration" => errors } end end diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 74b2b01..dc96dc4 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '0.1.2' + VERSION = '0.2.0' end end diff --git a/test/Vagrantfile b/test/Vagrantfile index 6c1ce52..f1923cf 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -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'