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

Feat/extras #111

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow the possibility to generate fqdn friendly /etc/hosts files.
This is needed on some systems so that:

hostname --fqdn

and

facter -p | grep fqdn

both return something useful.

This patch adds the fqdn_friendly and domain_name configuration options.
  • Loading branch information
purpleidea committed Apr 17, 2015
commit f087051398d567592535496f811a9b0447190d0b
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ 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:

```
<ip> <fqdn> <hostname>
```

format. This is needed so that `hostname --fqdn` and `facter -p | grep fqdn`
both work.

Example configuration:

```ruby
Expand All @@ -62,6 +75,8 @@ 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.vm.define 'example-box' do |node|
node.vm.hostname = 'example-box-hostname'
node.vm.network :private_network, ip: '192.168.42.42'
Expand Down
7 changes: 7 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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

alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
Expand All @@ -19,6 +21,8 @@ def initialize
@include_offline = UNSET_VALUE
@aliases = UNSET_VALUE
@ip_resolver = UNSET_VALUE
@fqdn_friendly = UNSET_VALUE
@domain_name = UNSET_VALUE
end

def finalize!
Expand All @@ -28,6 +32,8 @@ 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

@aliases = [ @aliases ].flatten
end
Expand All @@ -39,6 +45,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
Expand Down
25 changes: 23 additions & 2 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -146,7 +153,21 @@ def get_new_content(header, footer, body, old_content)
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 <hostname> [...]
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
Expand Down