diff --git a/lib/vagrant-hostmanager/action/delete_local_entry.rb b/lib/vagrant-hostmanager/action/delete_local_entry.rb deleted file mode 100644 index 7022b06..0000000 --- a/lib/vagrant-hostmanager/action/delete_local_entry.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'vagrant-hostmanager/hosts_file' - -module VagrantPlugins - module HostManager - module Action - class DeleteLocalEntry - include HostsFile - - def initialize(app, env) - @app = app - @machine = env[:machine] - @logger = Log4r::Logger.new('vagrant::hostmanager::delete_local_entry') - end - - def call(env) - - @logger.info 'delete called' - # check config to see if the hosts file should be updated automatically - return @app.call(env) unless @machine.config.hostmanager.enabled? - @logger.info 'Updating /etc/hosts file automatically' - - # delete entry for id - delete_local(@machine) - - @app.call(env) - end - end - end - end -end diff --git a/lib/vagrant-hostmanager/action/update_hosts_file.rb b/lib/vagrant-hostmanager/action/update_hosts_file.rb index a281511..34266ba 100644 --- a/lib/vagrant-hostmanager/action/update_hosts_file.rb +++ b/lib/vagrant-hostmanager/action/update_hosts_file.rb @@ -15,6 +15,7 @@ module VagrantPlugins def call(env) # check if machine is already active return @app.call(env) if @machine.id + @logger.info 'Continuing update of hosts file for new machine' # check config to see if the hosts file should be update automatically return @app.call(env) unless @machine.config.hostmanager.enabled? @@ -23,10 +24,12 @@ module VagrantPlugins @app.call(env) # update /etc/hosts file on active machines + env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests') update_guests(@machine.env, @machine.provider_name) # update /etc/hosts files on host if enabled if @machine.config.hostmanager.manage_host? + env[:ui].info I18n.t('vagrant_hostmanager.action.update_host') update_host(@machine.env, @machine.provider_name) end end diff --git a/lib/vagrant-hostmanager/action/update_local_entry.rb b/lib/vagrant-hostmanager/action/update_local_entry.rb deleted file mode 100644 index abe2ed1..0000000 --- a/lib/vagrant-hostmanager/action/update_local_entry.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'vagrant-hostmanager/hosts_file' - -module VagrantPlugins - module HostManager - module Action - class UpdateLocalEntry - include HostsFile - - def initialize(app, env) - @app = app - @machine = env[:machine] - @logger = Log4r::Logger.new('vagrant::hostmanager::update_local_entry') - end - - def call(env) - # check if machine is already active - @logger.info "Called update" - return @app.call(env) if @machine.id - - # check config to see if the hosts file should be update automatically - 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 - @app.call(env) - - # delete entry for id - update_local(@machine) - - end - end - end - end -end diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index ec83717..8b4f148 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -12,39 +12,31 @@ module VagrantPlugins alias_method :manage_host?, :manage_host def initialize - @enabled = false - @manage_local = false + @enabled = UNSET_VALUE + @manage_host = UNSET_VALUE @ignore_private_ip = UNSET_VALUE - @aliases = Array.new - @include_offline = false + @include_offline = UNSET_VALUE + @aliases = [] end def finalize! + @enabled = false if @enabled == UNSET_VALUE + @manage_host = false if @managed_host == UNSET_VALUE @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE + @include_offline = false if @include_offline == UNSET_VALUE @aliases = [ @aliases ].flatten end def validate(machine) - errors = Array.new + errors = [] - # check if enabled option is either true or false - errors << validate_bool('hostmanager.enabled', enabled) + errors << validate_bool('hostmanager.enabled', @enabled) + errors << validate_bool('hostmanager.manage_host', @manage_host) + errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip) + errors << validate_bool('hostmanager.include_offline', @include_offline) + errors.compact! - # check if include_offline is either true or false - errors << validate_bool('hostmanager.include_offline', include_offline) - - # check if manage_local option is either true or false - # if ![TrueClass, FalseClass].include?(manage_local.class) - # errors << "A value for hostmanager.manage_local can be true or false." - # end - - # check if ignore_private_ip option is either true or false (or UNSET_VALUE) - if @ignore_private_ip != UNSET_VALUE - errors << validate_bool('hostmanager.ignore_private_ip', ignore_private_ip) - end - - # check if aliases option is an Array - if !machine.config.hostmanager.aliases.kind_of?(Array) and + if !machine.config.hostmanager.aliases.kind_of?(Array) and !machine.config.hostmanager.aliases.kind_of?(String) errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', { :config_key => 'hostmanager.aliases', @@ -52,22 +44,21 @@ module VagrantPlugins }) end - errors.compact! - { "HostManager configuration" => errors } + { 'HostManager configuration' => errors } end private + def validate_bool(key, value) if ![TrueClass, FalseClass].include?(value.class) I18n.t('vagrant_hostmanager.config.not_a_bool', { :config_key => key, - :value => value.class.to_s, + :value => value.class.to_s }) else nil end end - end end end diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index d35ae72..8699fff 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -5,14 +5,19 @@ module VagrantPlugins module HostsFile def update_guests(env, provider) entries = get_entries(env, provider) + + # update hosts file on each active machine with matching provider env.active_machines.each do |name, p| if provider == p target = env.machine(name, p) next unless target.communicate.ready? + # download and modify file with Vagrant-managed entries file = env.tmp_path.join("hosts.#{name}") target.communicate.download('/etc/hosts', file) update_file(file, entries, env.tmp_path) + + # upload modified file and remove temporary file target.communicate.upload(file, '/tmp/hosts') target.communicate.sudo('mv /tmp/hosts /etc/hosts') FileUtils.rm(file) @@ -22,9 +27,13 @@ module VagrantPlugins def update_host(env, provider) entries = get_entries(env, provider) + + # copy and modify hosts file on host with Vagrant-managed entries file = env.tmp_path.join('hosts.local') FileUtils.cp('/etc/hosts', file) update_file(file, entries, env.tmp_path) + + # copy modified file using sudo for permission `sudo cp #{file} /etc/hosts` end @@ -33,9 +42,12 @@ module VagrantPlugins def update_file(file, entries, tmp_path) tmp_file = Tempfile.open('hostmanager', tmp_path, 'a') begin + # copy each line not managed by Vagrant File.open(file).each_line do |line| tmp_file << line unless line =~ /# VAGRANT ID:/ end + + # write a line for each Vagrant-managed entry entries.each { |entry| tmp_file << entry } ensure tmp_file.close @@ -73,6 +85,7 @@ module VagrantPlugins end def get_machines(env, provider) + # check if offline machines should be included in host entries if env.config_global.hostmanager.include_offline? machines = [] env.machine_names.each do |name| diff --git a/lib/vagrant-hostmanager/plugin.rb b/lib/vagrant-hostmanager/plugin.rb index f73ce6e..820b3b7 100644 --- a/lib/vagrant-hostmanager/plugin.rb +++ b/lib/vagrant-hostmanager/plugin.rb @@ -5,8 +5,8 @@ module VagrantPlugins class Plugin < Vagrant.plugin('2') name 'HostManager' description <<-DESC - This plugin manages the /etc/hosts file for guest machines. An entry is - created for each active machine using the hostname attribute. + 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. You can also use the hostmanager provisioner to update the hosts file. DESC diff --git a/locales/en.yml b/locales/en.yml index e27083b..17d2858 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -1,12 +1,8 @@ en: vagrant_hostmanager: action: - update_guest: "[%{name}] Updating /etc/hosts file" - update_host: "Updating local /etc/hosts file" - run_sudo: "Running sudo to modify host machine's /etc/hosts file" - add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}" - host_no_ip: "Could not determine ip for machine '%{name}': no private ip configured or machine not up." - update: "[%{name}] Updating /etc/hosts file" + update_guests: "Updating /etc/hosts file on active guest machines..." + update_host: "Updating /etc/hosts file on host machine (password may be required)..." config: not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'" not_an_array_or_string: "A value for %{config_key} must be an Array or String, not type '%{is_class}'" diff --git a/test/Vagrantfile b/test/Vagrantfile index 011c54e..b3a4f31 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -9,7 +9,6 @@ Vagrant.configure('2') do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true - config.hostmanager.include_offline = true config.vm.define :server1 do |server| server.vm.hostname = 'fry'