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

Inherits:
Object
  • Object
show all
Includes:
Util::Nfs
Defined in:
lib/vagrant-libvirt/action/prepare_nfs_settings.rb

Instance Method Summary collapse

Methods included from Util::Nfs

#using_nfs?

Constructor Details

#initialize(app, _env) ⇒ PrepareNFSSettings

Returns a new instance of PrepareNFSSettings.



14
15
16
17
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 14

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

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 19

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_machine_ip] = read_machine_ip(env[:machine])
    env[:nfs_host_ip]    = read_host_ip(env[:nfs_machine_ip])

    @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(ip) ⇒ String

Returns the IP address of the host

Parameters:

  • machine (Machine)

Returns:



38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 38

def read_host_ip(ip)
  UDPSocket.open do |s|
    if ip.is_a?(Array)
      s.connect(ip.last, 1)
    else
      s.connect(ip, 1)
    end
    s.addr.last
  end
end

#read_machine_ip(machine) ⇒ String

Returns the IP address of the guest

Parameters:

  • machine (Machine)

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vagrant-libvirt/action/prepare_nfs_settings.rb', line 53

def read_machine_ip(machine)
  # check host only ip
  ssh_info = machine.ssh_info
  return ssh_info[:host] if ping(ssh_info[:host], ssh_info[:port])

  # check other ips
  command = "ip=$(which ip); ${ip:-/sbin/ip} addr show | grep -i 'inet ' | grep -v '127.0.0.1' | tr -s ' ' | cut -d' ' -f3 | cut -d'/' -f 1"
  result  = ''
  machine.communicate.execute(command) do |type, data|
    result += data if type == :stdout
  end

  ips = result.chomp.split("\n").uniq
  @logger.info("guest IPs: #{ips.join(', ')}")
  ips.each do |ip|
    next if ip == ssh_info[:host]
    return ip if ping(ip)
  end

  nil
end