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 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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

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
Expand All @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand All @@ -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
Expand All @@ -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
Expand Down
35 changes: 31 additions & 4 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 @@ -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 <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