Class: VagrantPlugins::GuestClearLinux::Cap::ConfigureNetworks

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-guests-clearlinux/cap/configure_networks.rb

Constant Summary collapse

@@logger =
Log4r::Logger.new('vagrant::guest::clearlinux::configure_networks')

Class Method Summary collapse

Class Method Details

.configure_networks(machine, networks) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-guests-clearlinux/cap/configure_networks.rb', line 43

def self.configure_networks(machine, networks)
  comm = machine.communicate
  # Read network interface names
  interfaces = []
  comm.sudo("ifconfig -a | grep -E '^en|^eth' | cut -f1 -d' '") do |_, result|
    interfaces = result.split("\n")
  end

  # Configure interfaces
  networks.each do |network|
    interface = network[:interface].to_i

    iface = interfaces[interface]
    if iface.nil?
      @@logger.warn("Could not find match rule for network #{network.inspect}")
      next
    end
    comm.sudo("mkdir -p /etc/systemd/network/")
    unit_name = find_network_file comm, iface
    comm.sudo("rm -f /etc/systemd/network/#{unit_name}")

    if network[:type] == :static
      cidr = IPAddr.new(network[:netmask]).to_cidr
      address = format('%s/%s', network[:ip], cidr)
      unit_file = format(STATIC_NETWORK, iface, address)
    elsif network[:type] == :dhcp
      unit_file = format(DHCP_NETWORK, iface)
    end

    temp = Tempfile.new('vagrant')
    temp.binmode
    temp.write(unit_file)
    temp.close

    comm.upload(temp.path, "/tmp/#{unit_name}")
    comm.sudo(["mv /tmp/#{unit_name} /etc/systemd/network/",
      "chown root:root /etc/systemd/network/#{unit_name}",
      "chmod a+r /etc/systemd/network/#{unit_name}"].join("\n"))
  end
  comm.sudo("systemctl restart systemd-networkd")
  comm.wait_for_ready(30)
end

.find_network_file(comm, iface) ⇒ Object



86
87
88
# File 'lib/vagrant-guests-clearlinux/cap/configure_networks.rb', line 86

def self.find_network_file(comm, iface)
  format('50-vagrant-%s.network', iface)
end