From 6e4a2ae96761c930a3c0997a9e7b076b803888f0 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Mon, 6 May 2013 08:29:25 +0200 Subject: [PATCH 1/3] Abstract the get_machines part. Prepare for include offline. --- lib/vagrant-hostmanager/hosts_file.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index d45d770..1a175d1 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -24,9 +24,7 @@ module VagrantPlugins 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 @@ -57,6 +55,14 @@ module VagrantPlugins machine.communicate.sudo("mv /tmp/hosts /etc/hosts") end end + + private + # Returns an array with the same structure as env.active_machines: + # [ [:machine, :virtualbox], [:foo, :virtualbox] ] + def get_machines(env, provider) + env.active_machines + end + end end end From 0b3e60cb5db2789885dfdb5b7eb9d09cfa1b4b82 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Mon, 6 May 2013 08:31:31 +0200 Subject: [PATCH 2/3] Added new include_offline configuration item --- README.md | 5 +++++ lib/vagrant-hostmanager/config.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 16ae137..0f11188 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 20be3c8..5244db3 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -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) From 00b8e75a7d6fad55dcae9614b11fbfa7661f81b8 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Mon, 6 May 2013 08:32:02 +0200 Subject: [PATCH 3/3] 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. --- lib/vagrant-hostmanager/hosts_file.rb | 41 ++++++++++++++++++++------- locales/en.yml | 1 + 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 1a175d1..505b82e 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -17,7 +17,7 @@ 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 @@ -29,17 +29,22 @@ module VagrantPlugins 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,10 +62,26 @@ module VagrantPlugins 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) - env.active_machines + 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 diff --git a/locales/en.yml b/locales/en.yml index 337d096..758b7d3 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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}'"