Support include_offline boxes.

If include_offline is enabled, we loop over all available boxes picking
those with the correct provider.

If a box has no private ip set or is offline, it is skipped (a warning is shown).
The lambda that gets the ip will return nil for the boxes.
This commit is contained in:
Jan Vansteenkiste 2013-05-06 08:32:02 +02:00
parent 0b3e60cb5d
commit 00b8e75a7d
2 changed files with 32 additions and 10 deletions

View File

@ -17,7 +17,7 @@ module VagrantPlugins
next if ip next if ip
end end
end end
ip || machine.ssh_info[:host] ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
end end
# create the temporary hosts file # create the temporary hosts file
@ -29,6 +29,7 @@ module VagrantPlugins
machines << machine = env.machine(name, provider) machines << machine = env.machine(name, provider)
host = machine.config.vm.hostname || name host = machine.config.vm.hostname || name
ip = get_ip_address.call(machine) ip = get_ip_address.call(machine)
if ip
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', { machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', {
:ip => ip, :ip => ip,
@ -36,10 +37,14 @@ module VagrantPlugins
:aliases => host_aliases, :aliases => host_aliases,
}) })
file << "#{ip}\t#{host}\s#{host_aliases}\n" 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 end
end end
machines machines
end end
@ -57,11 +62,27 @@ module VagrantPlugins
end end
private 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: # Returns an array with the same structure as env.active_machines:
# [ [:machine, :virtualbox], [:foo, :virtualbox] ] # [ [:machine, :virtualbox], [:foo, :virtualbox] ]
def get_machines(env, provider) 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 env.active_machines
end end
end
end end
end end

View File

@ -2,6 +2,7 @@ en:
vagrant_hostmanager: vagrant_hostmanager:
action: action:
add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}" 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: "[%{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}'"