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
85
# File 'lib/vagrant-guests-clearlinux/cap/configure_networks.rb', line 43

def self.configure_networks(machine, networks)
  machine.communicate.tap do |comm|

    # 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/")
      comm.sudo("chown root:root /etc/systemd/network/#{unit_name}")
      comm.sudo("chmod a+r /etc/systemd/network/#{unit_name}")
    end
    comm.sudo('systemctl daemon-reload && systemctl restart systemd-networkd')
  end
end

.find_network_file(comm, iface) ⇒ Object



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

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