Module: Vagrant::Util::GuestHosts::Unix

Included in:
BSD, Linux
Defined in:
lib/vagrant/util/guest_hosts.rb

Constant Summary collapse

DEAFAULT_LOOPBACK_CHECK_LIMIT =
5.freeze

Instance Method Summary collapse

Instance Method Details

#add_hostname_to_loopback_interface(comm, name, loop_bound = DEAFAULT_LOOPBACK_CHECK_LIMIT) ⇒ Object

Add hostname to a loopback address on /etc/hosts if not already there Will insert name at the first free address of the form 127.0.X.1, up to the loop_bound

Parameters:

  • (Communicator)
  • full (String)

    hostanme

  • (option) (int)

    defines the upper bound for searching for an available loopback address


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant/util/guest_hosts.rb', line 16

def add_hostname_to_loopback_interface(comm, name, loop_bound=DEAFAULT_LOOPBACK_CHECK_LIMIT)
  basename = name.split(".", 2)[0]
  comm.sudo <<-EOH.gsub(/^ {14}/, '')
  grep -w '#{name}' /etc/hosts || {
    for i in #{[*1..loop_bound].join(' ')}; do
      grep -w "127.0.${i}.1" /etc/hosts || {
        echo "127.0.${i}.1 #{name} #{basename}" >> /etc/hosts
        break
      }
    done
  }
  EOH
end