diff --git a/README.md b/README.md index f851728..59f1d13 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,26 @@ up or have a private ip configured will be added to the hosts file. In addition, the `hostmanager.aliases` configuration attribute can be used to provide aliases for your host names. +If you set the `fqdn_friendly` configuration attribute, the /etc/hosts file +will be setup so that fully qualified domain names work properly. This fixes +the `127.0.0.1` line so that the hostname isn't present, and if the +`domain_name` configuration attribute is set to your domain name, it ensures +that all the added host entries have the: + +``` + +``` + +format. This is needed so that `hostname --fqdn` and `facter -p | grep fqdn` +both work. + +If you set the `extra_hosts` configuration attribute, each hash in that list +will be used to add additional elements to each /etc/hosts file. This is useful +for defining a virtual IP, which doesn't correspond to a particular physical +host, but which requires a DNS entry. Each element in the `extra_hosts` list +should be a hash with three keys: `:ip`, `:host`, and `:aliases`. The first two +should be strings, while the third should be a list of strings. + Example configuration: ```ruby @@ -62,6 +82,15 @@ Vagrant.configure("2") do |config| config.hostmanager.manage_host = true config.hostmanager.ignore_private_ip = false config.hostmanager.include_offline = true + config.hostmanager.fqdn_friendly = true + config.hostmanager.domain_name = 'example.com' + config.hostmanager.extra_hosts = [ + { + :host => 'vip.example.com', + :ip => '192.168.42.253', + :aliases => ['vip'], + }, + ] 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 4e3d3dc..ecd5b3e 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -7,6 +7,9 @@ class Config < Vagrant.plugin('2', :config) attr_accessor :aliases attr_accessor :include_offline attr_accessor :ip_resolver + attr_accessor :fqdn_friendly + attr_accessor :domain_name + attr_accessor :extra_hosts alias_method :enabled?, :enabled alias_method :include_offline?, :include_offline @@ -19,6 +22,9 @@ def initialize @include_offline = UNSET_VALUE @aliases = UNSET_VALUE @ip_resolver = UNSET_VALUE + @fqdn_friendly = UNSET_VALUE + @domain_name = UNSET_VALUE + @extra_hosts = UNSET_VALUE end def finalize! @@ -28,6 +34,9 @@ def finalize! @include_offline = false if @include_offline == UNSET_VALUE @aliases = [] if @aliases == UNSET_VALUE @ip_resolver = nil if @ip_resolver == UNSET_VALUE + @fqdn_friendly = false if @fqdn_friendly == UNSET_VALUE + @domain_name = '' if @domain_name == UNSET_VALUE + @extra_hosts = [] if @extra_hosts == UNSET_VALUE @aliases = [ @aliases ].flatten end @@ -39,6 +48,7 @@ def validate(machine) errors << validate_bool('hostmanager.manage_host', @manage_host) errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip) errors << validate_bool('hostmanager.include_offline', @include_offline) + errors << validate_bool('hostmanager.fqdn_friendly', @fqdn_friendly) errors.compact! # check if aliases option is an Array diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 3b24260..8a82ed8 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -94,7 +94,14 @@ def get_hosts_file_entry(machine, resolving_machine) host = machine.config.vm.hostname || machine.name aliases = machine.config.hostmanager.aliases if ip != nil - "#{ip}\t#{host}\n" + aliases.map{|a| "#{ip}\t#{a}"}.join("\n") + "\n" + if @config.hostmanager.fqdn_friendly and @config.hostmanager.domain_name != '' + new_aliases = aliases + ["#{host}"] # append to a copy + # this format is needed for things like fqdn's to work properly + # test with: facter -p | grep fqdn + "#{ip}\t#{host}.#{@config.hostmanager.domain_name}\t" + new_aliases.join("\t") + "\n" + else + "#{ip}\t#{host}\n" + aliases.map{|a| "#{ip}\t#{a}"}.join("\n") + "\n" + end end end @@ -136,17 +143,37 @@ def get_machines end def get_new_content(header, footer, body, old_content) + + extras = '' + @config.hostmanager.extra_hosts.each do |x| + extras+= ("#{x[:ip]}\t#{x[:host]}\t" + x[:aliases].join("\t") + "\n") + end + if body.empty? - block = "\n" + block = "\n" + extras else - block = "\n\n" + header + body + footer + "\n" + block = "\n\n" + header + body + extras + footer + "\n" end # Pattern for finding existing block header_pattern = Regexp.quote(header) footer_pattern = Regexp.quote(footer) pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE) # Replace existing block or append - old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block + output = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block + + # remove the hostname from the 127.0.0.1 line... this breaks the fqdn + if @config.hostmanager.fqdn_friendly + output = output.split("\n") # transform into array + output.each_with_index do |x, i| + # look for a line that has 127.0.0.1 [...] + if x.start_with?("127.0.0.1") and not x.start_with?("127.0.0.1 localhost") + output[i] = "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4" + end + end + output = (output.join("\n")+"\n") + end + + return output end def read_or_create_id