Class: VagrantPlugins::ProviderLibvirt::Action::PrepareNFSSettings

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Action::Builtin::MixinSyncedFolders
Defined in:
lib/vagrant-libvirt/action/prepare_nfs_settings.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ PrepareNFSSettings

Returns a new instance of PrepareNFSSettings.



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

def initialize(app,env)
  @app = app
  @logger = Log4r::Logger.new("vagrant::action::vm::nfs")
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 13

def call(env)
  @machine = env[:machine]
  @app.call(env)

  if using_nfs?
    @logger.info("Using NFS, preparing NFS settings by reading host IP and machine IP")
    env[:nfs_host_ip]    = read_host_ip(env[:machine],env)
    env[:nfs_machine_ip] = env[:machine].ssh_info[:host]

    @logger.info("host IP: #{env[:nfs_host_ip]} machine IP: #{env[:nfs_machine_ip]}")

    raise Vagrant::Errors::NFSNoHostonlyNetwork if !env[:nfs_machine_ip] || !env[:nfs_host_ip]
  end
end

#read_host_ip(machine, env) ⇒ String

Returns the IP address of the first host only network adapter

Parameters:

  • machine (Machine)

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 39

def read_host_ip(machine,env)
  nets = env[:libvirt_compute].list_networks
  if nets.size == 1
    net = nets.first
  else
    domain = env[:libvirt_compute].servers.get(machine.id.to_s)
    xml=Nokogiri::XML(domain.to_xml)
    networkname = xml.xpath('/domain/devices/interface/source').first.attributes['network'].value.to_s
    @logger.info("Using network named #{networkname}")
    net = env[:libvirt_compute].list_networks.find {|netw| netw[:name] == networkname}
  end
  # FIXME better implement by libvirt xml parsing
  `ip addr show | grep -A 2 #{net[:bridge_name]} | grep -i 'inet ' | tr -s ' ' | cut -d' ' -f3 | cut -d'/' -f 1`.chomp
end

#read_machine_ip(machine) ⇒ String

Returns the IP address of the guest by looking at the first enabled host only network.

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 58

def read_machine_ip(machine)
  machine.config.vm.networks.each do |type, options|
    if type == :private_network && options[:ip].is_a?(String)
      return options[:ip]
    end
  end

  nil
end

#using_nfs?Boolean

We’re using NFS if we have any synced folder with NFS configured. If we are not using NFS we don’t need to do the extra work to populate these fields in the environment.

Returns:

  • (Boolean)


31
32
33
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 31

def using_nfs?
  !!synced_folders(@machine)[:nfs]
end