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) def call(env)
# check if machine is already active # check if machine is already active
return @app.call(env) if @machine.id 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 # check config to see if the hosts file should be update automatically
return @app.call(env) unless @machine.config.hostmanager.enabled? return @app.call(env) unless @machine.config.hostmanager.enabled?
@ -23,10 +24,12 @@ module VagrantPlugins
@app.call(env) @app.call(env)
# update /etc/hosts file on active machines # 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_guests(@machine.env, @machine.provider_name)
# update /etc/hosts files on host if enabled # update /etc/hosts files on host if enabled
if @machine.config.hostmanager.manage_host? if @machine.config.hostmanager.manage_host?
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
update_host(@machine.env, @machine.provider_name) update_host(@machine.env, @machine.provider_name)
end end
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,38 +12,30 @@ module VagrantPlugins
alias_method :manage_host?, :manage_host alias_method :manage_host?, :manage_host
def initialize def initialize
@enabled = false @enabled = UNSET_VALUE
@manage_local = false @manage_host = UNSET_VALUE
@ignore_private_ip = UNSET_VALUE @ignore_private_ip = UNSET_VALUE
@aliases = Array.new @include_offline = UNSET_VALUE
@include_offline = false @aliases = []
end end
def finalize! 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 @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
@include_offline = false if @include_offline == UNSET_VALUE
@aliases = [ @aliases ].flatten @aliases = [ @aliases ].flatten
end end
def validate(machine) 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) !machine.config.hostmanager.aliases.kind_of?(String)
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', { errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
@ -52,22 +44,21 @@ module VagrantPlugins
}) })
end end
errors.compact! { 'HostManager configuration' => errors }
{ "HostManager configuration" => errors }
end end
private private
def validate_bool(key, value) def validate_bool(key, value)
if ![TrueClass, FalseClass].include?(value.class) if ![TrueClass, FalseClass].include?(value.class)
I18n.t('vagrant_hostmanager.config.not_a_bool', { I18n.t('vagrant_hostmanager.config.not_a_bool', {
:config_key => key, :config_key => key,
:value => value.class.to_s, :value => value.class.to_s
}) })
else else
nil nil
end end
end end
end end
end end
end end

View File

@ -5,14 +5,19 @@ module VagrantPlugins
module HostsFile module HostsFile
def update_guests(env, provider) def update_guests(env, provider)
entries = get_entries(env, provider) entries = get_entries(env, provider)
# update hosts file on each active machine with matching provider
env.active_machines.each do |name, p| env.active_machines.each do |name, p|
if provider == p if provider == p
target = env.machine(name, p) target = env.machine(name, p)
next unless target.communicate.ready? next unless target.communicate.ready?
# download and modify file with Vagrant-managed entries
file = env.tmp_path.join("hosts.#{name}") file = env.tmp_path.join("hosts.#{name}")
target.communicate.download('/etc/hosts', file) target.communicate.download('/etc/hosts', file)
update_file(file, entries, env.tmp_path) update_file(file, entries, env.tmp_path)
# upload modified file and remove temporary file
target.communicate.upload(file, '/tmp/hosts') target.communicate.upload(file, '/tmp/hosts')
target.communicate.sudo('mv /tmp/hosts /etc/hosts') target.communicate.sudo('mv /tmp/hosts /etc/hosts')
FileUtils.rm(file) FileUtils.rm(file)
@ -22,9 +27,13 @@ module VagrantPlugins
def update_host(env, provider) def update_host(env, provider)
entries = get_entries(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') file = env.tmp_path.join('hosts.local')
FileUtils.cp('/etc/hosts', file) FileUtils.cp('/etc/hosts', file)
update_file(file, entries, env.tmp_path) update_file(file, entries, env.tmp_path)
# copy modified file using sudo for permission
`sudo cp #{file} /etc/hosts` `sudo cp #{file} /etc/hosts`
end end
@ -33,9 +42,12 @@ module VagrantPlugins
def update_file(file, entries, tmp_path) def update_file(file, entries, tmp_path)
tmp_file = Tempfile.open('hostmanager', tmp_path, 'a') tmp_file = Tempfile.open('hostmanager', tmp_path, 'a')
begin begin
# copy each line not managed by Vagrant
File.open(file).each_line do |line| File.open(file).each_line do |line|
tmp_file << line unless line =~ /# VAGRANT ID:/ tmp_file << line unless line =~ /# VAGRANT ID:/
end end
# write a line for each Vagrant-managed entry
entries.each { |entry| tmp_file << entry } entries.each { |entry| tmp_file << entry }
ensure ensure
tmp_file.close tmp_file.close
@ -73,6 +85,7 @@ module VagrantPlugins
end end
def get_machines(env, provider) def get_machines(env, provider)
# check if offline machines should be included in host entries
if env.config_global.hostmanager.include_offline? if env.config_global.hostmanager.include_offline?
machines = [] machines = []
env.machine_names.each do |name| env.machine_names.each do |name|

View File

@ -5,8 +5,8 @@ module VagrantPlugins
class Plugin < Vagrant.plugin('2') class Plugin < Vagrant.plugin('2')
name 'HostManager' name 'HostManager'
description <<-DESC description <<-DESC
This plugin manages the /etc/hosts file for guest machines. An entry is This plugin manages the /etc/hosts file for the host and guest machines.
created for each active machine using the hostname attribute. An entry is created for each active machine using the hostname attribute.
You can also use the hostmanager provisioner to update the hosts file. You can also use the hostmanager provisioner to update the hosts file.
DESC DESC

View File

@ -1,12 +1,8 @@
en: en:
vagrant_hostmanager: vagrant_hostmanager:
action: action:
update_guest: "[%{name}] Updating /etc/hosts file" update_guests: "Updating /etc/hosts file on active guest machines..."
update_host: "Updating local /etc/hosts file" update_host: "Updating /etc/hosts file on host machine (password may be required)..."
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"
config: config:
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'" 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}'" 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.enabled = true
config.hostmanager.manage_host = true config.hostmanager.manage_host = true
config.hostmanager.include_offline = true
config.vm.define :server1 do |server| config.vm.define :server1 do |server|
server.vm.hostname = 'fry' server.vm.hostname = 'fry'