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

Inherits:
Object
  • Object
show all
Extended by:
Vagrant::Util::GuestInspection::Linux
Includes:
Vagrant::Util
Defined in:
lib/vagrant-guests-clearlinux/cap/configure_networks.rb

Constant Summary collapse

NETWORKD_DIRECTORY =
'/etc/systemd/network'.freeze
@@logger =
Log4r::Logger.new('vagrant::guest::clearlinux::configure_networks')

Class Method Summary collapse

Class Method Details

.configure_networks(machine, networks) ⇒ Object



35
36
37
38
39
40
41
42
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
# File 'lib/vagrant-guests-clearlinux/cap/configure_networks.rb', line 35

def self.configure_networks(machine, networks)
  comm = machine.communicate
  interfaces = machine.guest.capability(:network_interfaces)
  networks.each do |network|
    interface = network[:interface].to_i
    device = interfaces[interface]
    if device.nil?
      @@logger.warn("Could not find match rule for network #{network.inspect}")
      next
    end
    unit_name = format('50-vagrant-%<device>s.network', device: device)

    if network[:type] == :static
      leased = 'no'
      cidr = IPAddr.new(network[:netmask]).to_cidr
      body = "Address=#{network[:ip]}/#{cidr}"
      body += "\nGateway=#{network[:gateway]}" if network[:gateway]
    elsif network[:type] == :dhcp
      leased = 'yes'
      body = ''
    end

    unit_file = format(VAGRANT_NETWORK, device, leased, body)
    temp = Tempfile.new('vagrant')
    temp.binmode
    temp.write(unit_file)
    temp.close

    comm.upload(temp.path, "/tmp/#{unit_name}")
    comm.sudo([
      "mkdir -p #{NETWORKD_DIRECTORY}",
      "rm -f #{NETWORKD_DIRECTORY}/#{unit_name}",
      "mv /tmp/#{unit_name} #{NETWORKD_DIRECTORY}",
      "chown root:root #{NETWORKD_DIRECTORY}/#{unit_name}",
      "chmod a+r #{NETWORKD_DIRECTORY}/#{unit_name}",
      'systemctl restart systemd-networkd'
    ].join(' && '))
    comm.wait_for_ready(30)
  end
end