Class: VagrantPlugins::Hostentries::Action::UpdateHostsEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-hostentries/action/update_hosts_entry.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ UpdateHostsEntry

Returns a new instance of UpdateHostsEntry.



7
8
9
10
# File 'lib/vagrant-hostentries/action/update_hosts_entry.rb', line 7

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new("VagrantPlugins::Hosts")
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-hostentries/action/update_hosts_entry.rb', line 12

def call(env)
  # Update the host system's hostfile entry for the booting guest
  env[:host].update_hosts_entry(env[:machine].guest.capability(:read_ip_address), env[:machine].config.vm.hostname)

  # Update each guest machines' host entry with all the other guest machines' entries
  env[:machine].env.active_machines.each do |machine|
    m = env[:machine].env.machine(machine[0], machine[1])
    next unless m.methods.member? :guest # Machine is not actually active...
    if !m.guest.capability?(:update_hosts_entry)
      @logger.warn "Unsupported machine #{machine.config.name}"
      next
    end
    env[:machine].env.active_machines.each do |machine2|
      m2 = env[:machine].env.machine(machine2[0], machine2[1])
      next unless m2.methods.member? :guest # skip machines not in service
      m2_ip = m2.guest.capability(:read_ip_address)
      m2_hostname = m2.config.vm.hostname
      env[:ui].info "Adding IP: #{m2_ip} -> Hostname: #{m2_hostname}"
      m.guest.capability(:update_hosts_entry, m2_ip, m2_hostname)
    end
  end
  return @app.call(env)
end