Clean up plugin configuration and add comments.

This commit cleaned up the plugin's configuration and added code
comments for the hosts_file module. Additionally, display messages were
re-organized based on the single action.

The command still requires code to validate the configuration before
execution.

This closes pull request #18. Thanks to @b2jrock for his contribution.
This commit is contained in:
Shawn Dahlen 2013-06-19 12:05:24 -04:00
parent a39877ac27
commit 064d9b8658
8 changed files with 37 additions and 99 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -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

View File

@ -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}'"

1
test/Vagrantfile vendored
View File

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