Skip to content

Commit

Permalink
Merge pull request #14 from vStone/feature/include_offline_boxes
Browse files Browse the repository at this point in the history
Added option to include offline boxes in the hosts file.
  • Loading branch information
smdahlen committed May 7, 2013
2 parents 1715eca + 00b8e75 commit ad1febb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class Config < Vagrant.plugin('2', :config)
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!
Expand All @@ -24,6 +27,9 @@ def validate(machine)
# 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)
Expand Down
51 changes: 39 additions & 12 deletions lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@ def generate(env, provider)
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

Expand All @@ -57,6 +60,30 @@ def update(machine)
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
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}'"
Expand Down

0 comments on commit ad1febb

Please sign in to comment.