Skip to content

Commit

Permalink
Merge pull request #4 from nagas/feature/hosts-aliases
Browse files Browse the repository at this point in the history
provide aliases for host names and configuration options validation
  • Loading branch information
smdahlen committed Apr 17, 2013
2 parents c94b1f8 + 5f38457 commit 9c984b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ 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.

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

Example configuration:

Vagrant.configure("2") do |config|
config.hostmanager.auto_update = true
config.hostmanager.ignore_private_ip = false
config.vm.define "example-box" do |node|
node.vm.hostname = "example-box-hostname"
node.vm.network :private_network, ip: "192.168.42.42"
node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
end
end

Contribute
----------
Contributions are welcome.
Expand Down
29 changes: 29 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,45 @@ module HostManager
class Config < Vagrant.plugin('2', :config)
attr_accessor :auto_update
attr_accessor :ignore_private_ip
attr_accessor :aliases

def initialize
@auto_update = UNSET_VALUE
@ignore_private_ip = UNSET_VALUE
@aliases = Array.new
end

def finalize!
@auto_update = true if @auto_update == UNSET_VALUE
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
end

def validate(machine)
errors = Array.new

# check if auto_update option is defined in Vagrantfile
# and check if is either true or false accordingly
if (machine.config.hostmanager.auto_update &&
![TrueClass, FalseClass].include?(auto_update.class))
errors << "A value for hostmanager.auto_update can be true or false."
end

# check if ignore_private_ip option is defined in Vagrantfile
# and check if is either true or false accordingly
if (machine.config.hostmanager.ignore_private_ip &&
![TrueClass, FalseClass].include?(ignore_private_ip.class))
errors << "A value for hostmanager.ignore_private_ip can be true or false."
end

# check if aliases option is defined in Vagrantfile
# and check if is an Array accordingly
if (machine.config.hostmanager.aliases &&
!machine.config.hostmanager.aliases.kind_of?(Array))
errors << "A value for hostmanager.aliases must be an Array."
end

{ "HostManager configuration" => errors }
end
end
end
end
5 changes: 3 additions & 2 deletions lib/vagrant-hostmanager/hosts_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def generate(env, provider)
machines << machine = env.machine(name, provider)
host = machine.config.vm.hostname || name
ip = get_ip_address.call(machine)
@logger.info "Adding /etc/hosts entry: #{ip} #{host}"
file << "#{ip}\t#{host}\n"
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
@logger.info "Adding /etc/hosts entry: #{ip} #{host} #{host_aliases}"
file << "#{ip}\t#{host}\s#{host_aliases}\n"
end
end
end
Expand Down

0 comments on commit 9c984b6

Please sign in to comment.