Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the hostmanager.extra_hosts attribute, attempt to fix issue #223 #248

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ set, it falls back to the symbol defining the machine in the Vagrantfile.
If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
up or have a private ip configured will be added to the hosts file.

If the `hostmanager.extra_hosts` attribute is set, boxes defined will be added
to the hosts file

In addition, the `hostmanager.aliases` configuration attribute can be used
to provide aliases for your host names.

Expand All @@ -56,6 +59,14 @@ Vagrant.configure("2") do |config|
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.hostmanager.extra_hosts = [
[
'192.168.42.41', [
'external-box.localdomain',
'subdomain.external-box.localdomain',
]
],
]
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
1 change: 1 addition & 0 deletions lib/vagrant-hostmanager-ext.rb
12 changes: 11 additions & 1 deletion lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :aliases
attr_accessor :include_offline
attr_accessor :ip_resolver
attr_accessor :extra_hosts

alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
Expand All @@ -22,6 +23,7 @@ def initialize
@include_offline = UNSET_VALUE
@aliases = UNSET_VALUE
@ip_resolver = UNSET_VALUE
@extra_hosts = UNSET_VALUE
end

def finalize!
Expand All @@ -32,6 +34,7 @@ def finalize!
@include_offline = false if @include_offline == UNSET_VALUE
@aliases = [] if @aliases == UNSET_VALUE
@ip_resolver = nil if @ip_resolver == UNSET_VALUE
@extra_hosts = [] if @extra_hosts == UNSET_VALUE

@aliases = [ @aliases ].flatten
end
Expand All @@ -46,7 +49,7 @@ def validate(machine)
errors << validate_bool('hostmanager.include_offline', @include_offline)
errors.compact!

# check if aliases option is an Array
# check if aliases option is an Array or String
if !machine.config.hostmanager.aliases.kind_of?(Array) &&
!machine.config.hostmanager.aliases.kind_of?(String)
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
Expand All @@ -55,6 +58,13 @@ def validate(machine)
})
end

if !machine.config.hostmanager.extra_hosts.kind_of?(Array)
errors << I18n.t('vagrant_hostmanager.config.not_an_array', {
:config_key => 'hostmanager.extra_hosts',
:is_class => extra_hosts.class.to_s,
})
end

if !machine.config.hostmanager.ip_resolver.nil? &&
!machine.config.hostmanager.ip_resolver.kind_of?(Proc)
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
Expand Down
19 changes: 19 additions & 0 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,28 @@ def update_content(file_content, resolving_machine, include_id, line_endings)
body = get_machines
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
.join
unless body.empty? #only when there is at least one active machine
extra_hosts = @config.hostmanager.extra_hosts
case depth(extra_hosts)
#allow single defs: config.hostmanager.extra_hosts = ['172.28.128.100', ['db.dev']]
when 1
body << extra_hosts
.map { |ip, aliases| "#{ip}\t#{aliases}"}.join(" ") + "\n\n"
#and multiple ones, config.hostmanager.extra_hosts = [['172.28.128.100', ['db.dev']], ['0.0.0.0', ['db.io', 'db.dev']] ]
when 2
body << extra_hosts
.map { |ip, aliases| aliases.map{|a| "#{ip}\t#{a}"}.join("\n") + "\n\n" }
.join
end
end
get_new_content(header, footer, body, file_content, line_endings)
end

def depth(array)
return 0 unless array.is_a?(Array)
return 1 + depth(array[0])
end

def get_hosts_file_entry(machine, resolving_machine)
ip = get_ip_address(machine, resolving_machine)
host = machine.config.vm.hostname || machine.name
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-hostmanager/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module VagrantPlugins
module HostManager
VERSION = '1.8.7'
VERSION = '1.8.8'
end
end
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ en:
update_host: "[vagrant-hostmanager:host] Updating hosts file on your workstation (password may be required)..."
config:
not_a_bool: "[vagrant-hostmanager:config:error] A value for %{config_key} can only be true or false, not type '%{value}'"
not_an_array: "[vagrant-hostmanager:config:error] A value for %{config_key} must be an Array, not type '%{is_class}'"
not_an_array_or_string: "[vagrant-hostmanager:config:error] A value for %{config_key} must be an Array or String, not type '%{is_class}'"
not_a_proc: "[vagrant-hostmanager:config:error] A value for %{config_key} must be a Proc, not type '%{is_class}'"
2 changes: 1 addition & 1 deletion vagrant-hostmanager.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'vagrant-hostmanager/version'

Gem::Specification.new do |gem|
gem.name = 'vagrant-hostmanager'
gem.name = 'vagrant-hostmanager-ext'
gem.version = VagrantPlugins::HostManager::VERSION
gem.authors = ['Shawn Dahlen','Seth Reeser']
gem.email = ['[email protected]','[email protected]']
Expand Down