Class: VagrantPlugins::ProviderLibvirt::Action::ReadMacAddresses

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/read_mac_addresses.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ReadMacAddresses

Returns a new instance of ReadMacAddresses.



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

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_libvirt::action::read_mac_addresses")
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
# File 'lib/vagrant-libvirt/action/read_mac_addresses.rb', line 12

def call(env)
  env[:machine_mac_addresses] = read_mac_addresses(env[:libvirt_compute], env[:machine])
end

#read_mac_addresses(libvirt, machine) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-libvirt/action/read_mac_addresses.rb', line 16

def read_mac_addresses(libvirt, machine)
  return nil if machine.id.nil?

  domain = libvirt.client.lookup_domain_by_uuid(machine.id)

  if domain.nil?
    @logger.info("Machine could not be found, assuming it got destroyed")
    machine.id = nil
    return nil
  end

  xml = Nokogiri::XML(domain.xml_desc)
  mac = xml.xpath("/domain/devices/interface/mac/@address")

  if mac.length == 0
    return {}
  end

  Hash[mac.each_with_index.map do |x,i|
    @logger.debug("interface[#{i}] = #{x.value}")
    [i,x.value]
  end]
end