class VagrantHosts::Cap::Facts::POSIX

Base class for retrieving network facts from POSIX

@since 2.8.0

Public Instance Methods

load_facts() click to toggle source
# File lib/vagrant-hosts/cap/facts/posix.rb, line 8
def load_facts
  facts = {}
  facts['networking'] = {}
  facts['networking']['interfaces'] = parse_ifconfig

  iface = get_default_iface
  facts['networking']['ip'] = facts['networking']['interfaces'][iface]['ip']

  facts
end

Private Instance Methods

get_default_iface() click to toggle source
# File lib/vagrant-hosts/cap/facts/posix.rb, line 38
def get_default_iface
  route_table = sudo('netstat -rn')[:stdout]
  default = route_table.lines.find {|e| e.start_with?('default') || e.start_with?('0.0.0.0')}

  default.split.last.chomp
end
ifconfig() click to toggle source
# File lib/vagrant-hosts/cap/facts/posix.rb, line 21
def ifconfig
  ifconfig_output = sudo('ifconfig -a')[:stdout]
  # Group output by interface.
  ifconfig_output.split(/^([[:alnum:]]+[:]?\s)/).drop(1).each_slice(2).map(&:join)
end
parse_ifconfig() click to toggle source
# File lib/vagrant-hosts/cap/facts/posix.rb, line 27
def parse_ifconfig
  results = ifconfig.map do |i|
    i.match(/^([[:alnum:]]+)[:]?\s.*inet (?:addr:)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/m)
  end.compact.map do |r|
    name, ip = r.captures
    [name, {'ip' => ip}]
  end

  Hash[results]
end
sudo(cmd) click to toggle source
# File lib/vagrant-hosts/cap/facts/posix.rb, line 47
def sudo(cmd)
  stdout = ''
  stderr = ''

  # FIXME: The chomp operations below smell like cargo culting. I have no
  #        idea why we do it and it breaks on WinRM 2.x which uses PSRP
  #        instead of routing though the CMD shell.
  retval = machine.communicate.sudo(cmd) do |type, data|
    if type == :stderr
      stderr << data.chomp
    else
      stdout << data.chomp
    end
  end

  {:stdout => stdout, :stderr => stderr, :retval => retval}
end