Sha256: 3cbd1c4b86625827db28dc86d09639ae0dd017df67c5db64f0e1d245cb4228c2

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module VagrantHosts::Addresses

  private

  def all_hosts(config)

    all_hosts = []
    all_hosts += local_hosts

    if config.autoconfigure
      all_hosts += vagrant_hosts
    end
    all_hosts += config.hosts

    all_hosts
  end

  # Builds an array containing hostname and aliases for a given machine.
  def hostnames_for_machine(machine)
    # Cast any Symbols to Strings
    machine_name = machine.name.to_s
    hostname = machine.config.vm.hostname || machine_name

    hostnames = [hostname]
    # If the hostname is a fqdn, add the first component as an alias.
    hostnames << hostname.split('.').first if hostname.index('.')
    # Also add the machine name as an alias.
    hostnames << machine_name unless hostnames.include? machine_name

    hostnames
  end

  def local_hosts(machine)
    [
      ['127.0.0.1', ['localhost']],
      ['127.0.1.1', hostnames_for_machine(machine)],
    ]
  end

  def vagrant_hosts(env)
    hosts = []

    all_machines(env).each do |m|
      m.config.vm.networks.each do |(net_type, opts)|
        next unless net_type == :private_network
        addr = opts[:ip]
        hosts << [addr, hostnames_for_machine(m)]
      end
    end

    hosts
  end

  # @return [Array<Vagrant::Machine>]
  def all_machines(env)
    env.active_machines.map { |vm_id| env.machine(*vm_id) }
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-hosts-2.1.0 lib/vagrant-hosts/addresses.rb