Sha256: 75f0e8ab79fb9511e5b135148dadd106c267615e9b2d54318f906b03c1d57438

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

module VagrantHosts::Addresses

  private

  def all_hosts(config)
    all_hosts = []
    all_hosts += local_hosts(@machine, config)

    if config.autoconfigure
      all_hosts += vagrant_hosts(@env)
    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, config)
    entries = [['127.0.0.1', ['localhost']]]

    if config.add_localhost_hostnames
      entries << ['127.0.1.1', hostnames_for_machine(machine)]
    end

    entries
  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 do |vm_id|
      begin
        env.machine(*vm_id)
      rescue Vagrant::Errors::MachineNotFound
        nil
      end
    end.compact
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vagrant-hosts-2.6.2 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.6.1 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.6.0 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.5.0 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.4.0 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.3.0 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.2.4 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.2.3 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.2.2 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.2.1 lib/vagrant-hosts/addresses.rb
vagrant-hosts-2.2.0 lib/vagrant-hosts/addresses.rb