Merge pull request #14 from vStone/feature/include_offline_boxes

Added option to include offline boxes in the hosts file.
This commit is contained in:
Shawn Dahlen 2013-05-07 07:14:56 -07:00
commit ad1febbe27
4 changed files with 51 additions and 12 deletions

View File

@ -40,6 +40,10 @@ to true.
A machine's host name is defined by `config.vm.hostname`. If this is not
set, it falls back to the symbol defining the machine in the Vagrantfile.
When using include_offline set to true, only boxes that are up or have a
private ip configured will be added to the hosts file. You will receive a
warning on skipped boxes.
In addition, the `hostmanager.aliases` configuration attribute can be used
to provide aliases for your host names.
@ -49,6 +53,7 @@ Example configuration:
Vagrant.configure("2") do |config|
config.hostmanager.enabled = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.vm.define "example-box" do |node|
node.vm.hostname = "example-box-hostname"
node.vm.network :private_network, ip: "192.168.42.42"

View File

@ -4,13 +4,16 @@ module VagrantPlugins
attr_accessor :enabled
attr_accessor :ignore_private_ip
attr_accessor :aliases
attr_accessor :include_offline
alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
def initialize
@enabled = false
@ignore_private_ip = UNSET_VALUE
@aliases = Array.new
@include_offline = false
end
def finalize!
@ -24,6 +27,9 @@ module VagrantPlugins
# check if enabled option is either true or false
errors << validate_bool('hostmanager.enabled', enabled)
# check if include_offline is either true or false
errors << validate_bool('hostmanager.include_offline', include_offline)
# 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)

View File

@ -17,31 +17,34 @@ module VagrantPlugins
next if ip
end
end
ip || machine.ssh_info[:host]
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
end
# create the temporary hosts file
path = env.tmp_path.join('hosts')
File.open(path, 'w') do |file|
file << "127.0.0.1\tlocalhost\slocalhost.localdomain\n"
# add a hosts entry for each active machine matching the provider
env.active_machines.each do |name, p|
get_machines(env, provider).each do |name, p|
if provider == p
machines << machine = env.machine(name, provider)
host = machine.config.vm.hostname || name
ip = get_ip_address.call(machine)
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', {
:ip => ip,
:host => host,
:aliases => host_aliases,
})
file << "#{ip}\t#{host}\s#{host_aliases}\n"
if ip
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', {
:ip => ip,
:host => host,
:aliases => host_aliases,
})
file << "#{ip}\t#{host}\s#{host_aliases}\n"
else
machine.env.ui.warn I18n.t('vagrant_hostmanager.action.host_no_ip', {
:name => name,
})
end
end
end
end
machines
end
@ -57,6 +60,30 @@ module VagrantPlugins
machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
end
end
private
# Either use the active machines, or loop over all available machines and
# get those with the same provider (aka, ignore boxes that throw MachineNotFound errors).
#
# Returns an array with the same structure as env.active_machines:
# [ [:machine, :virtualbox], [:foo, :virtualbox] ]
def get_machines(env, provider)
if env.config_global.hostmanager.include_offline?
machines = []
env.machine_names.each do |name|
begin
m = env.machine(name, provider)
machines << [name, provider]
rescue Vagrant::Errors::MachineNotFound => ex
# ignore this box.
end
end
machines
else
env.active_machines
end
end
end
end
end

View File

@ -2,6 +2,7 @@ en:
vagrant_hostmanager:
action:
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:
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"