2016-05-31 03:17:02 +00:00
|
|
|
require "tempfile"
|
2013-04-04 06:18:12 +00:00
|
|
|
|
2016-05-29 03:17:40 +00:00
|
|
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
2013-04-04 06:08:33 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestDebian
|
|
|
|
module Cap
|
|
|
|
class ConfigureNetworks
|
2013-04-08 17:47:19 +00:00
|
|
|
include Vagrant::Util
|
2017-12-14 00:44:13 +00:00
|
|
|
extend Vagrant::Util::GuestInspection::Linux
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
NETPLAN_DEFAULT_VERSION = 2
|
|
|
|
NETPLAN_DEFAULT_RENDERER = "networkd".freeze
|
|
|
|
NETPLAN_DIRECTORY = "/etc/netplan".freeze
|
|
|
|
NETWORKD_DIRECTORY = "/etc/systemd/network".freeze
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
def self.configure_networks(machine, networks)
|
|
|
|
comm = machine.communicate
|
|
|
|
interfaces = machine.guest.capability(:network_interfaces)
|
2017-12-14 00:44:13 +00:00
|
|
|
|
2018-01-04 00:31:49 +00:00
|
|
|
if systemd?(comm)
|
|
|
|
if netplan?(comm)
|
|
|
|
configure_netplan(machine, interfaces, comm, networks)
|
|
|
|
elsif systemd_networkd?(comm)
|
2018-01-03 15:17:41 +00:00
|
|
|
configure_networkd(machine, interfaces, comm, networks)
|
|
|
|
else
|
2018-01-04 00:31:49 +00:00
|
|
|
configure_nettools(machine, interfaces, comm, networks)
|
2018-01-03 15:17:41 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
configure_nettools(machine, interfaces, comm, networks)
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
# Configure networking using netplan
|
|
|
|
def self.configure_netplan(machine, interfaces, comm, networks)
|
|
|
|
ethernets = {}.tap do |e_nets|
|
|
|
|
networks.each do |network|
|
|
|
|
e_config = {}.tap do |entry|
|
|
|
|
if network[:ip]
|
2018-01-04 01:26:08 +00:00
|
|
|
mask = network[:netmask]
|
|
|
|
if mask && IPAddr.new(network[:ip]).ipv4?
|
|
|
|
begin
|
|
|
|
mask = IPAddr.new(mask).to_i.to_s(2).count("1")
|
|
|
|
rescue IPAddr::Error
|
|
|
|
# ignore and use given value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
entry["addresses"] = [[network[:ip], mask].compact.join("/")]
|
2018-01-03 15:17:41 +00:00
|
|
|
else
|
|
|
|
entry["dhcp4"] = true
|
|
|
|
end
|
|
|
|
if network[:gateway]
|
|
|
|
entry["gateway4"] = network[:gateway]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
e_nets[interfaces[network[:interface]]] = e_config
|
|
|
|
end
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
2018-05-10 20:02:05 +00:00
|
|
|
|
|
|
|
# By default, netplan expects the renderer to be systemd-networkd,
|
|
|
|
# but if any device is managed by NetworkManager, then we use that renderer
|
|
|
|
# ref: https://netplan.io/reference
|
|
|
|
renderer = NETPLAN_DEFAULT_RENDERER
|
|
|
|
ethernets.keys.each do |k|
|
|
|
|
if nm_controlled?(comm, k)
|
|
|
|
renderer = "NetworkManager"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
np_config = {"network" => {"version" => NETPLAN_DEFAULT_VERSION,
|
2018-05-10 20:02:05 +00:00
|
|
|
"renderer" => renderer, "ethernets" => ethernets}}
|
2018-01-03 15:17:41 +00:00
|
|
|
|
|
|
|
remote_path = upload_tmp_file(comm, np_config.to_yaml)
|
2018-04-02 19:08:06 +00:00
|
|
|
dest_path = "#{NETPLAN_DIRECTORY}/50-vagrant.yaml"
|
2018-01-03 15:17:41 +00:00
|
|
|
comm.sudo(["mv -f '#{remote_path}' '#{dest_path}'",
|
|
|
|
"chown root:root '#{dest_path}'",
|
|
|
|
"chmod 0644 '#{dest_path}'",
|
|
|
|
"netplan apply"].join("\n"))
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
# Configure guest networking using networkd
|
|
|
|
def self.configure_networkd(machine, interfaces, comm, networks)
|
|
|
|
net_conf = []
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
root_device = interfaces.first
|
|
|
|
networks.each do |network|
|
2018-01-03 15:17:41 +00:00
|
|
|
dev_name = interfaces[network[:interface]]
|
|
|
|
net_conf << "[Match]"
|
|
|
|
net_conf << "Name=#{dev_name}"
|
|
|
|
net_conf << "[Network]"
|
|
|
|
if network[:ip]
|
2018-01-04 01:26:08 +00:00
|
|
|
mask = network[:netmask]
|
|
|
|
if mask && IPAddr.new(network[:ip]).ipv4?
|
|
|
|
begin
|
|
|
|
mask = IPAddr.new(mask).to_i.to_s(2).count("1")
|
|
|
|
rescue IPAddr::Error
|
|
|
|
# ignore and use given value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
address = [network[:ip], mask].compact.join("/")
|
2018-01-03 15:17:41 +00:00
|
|
|
net_conf << "DHCP=no"
|
2018-01-04 01:26:08 +00:00
|
|
|
net_conf << "Address=#{address}"
|
2018-01-03 15:17:41 +00:00
|
|
|
net_conf << "Gateway=#{network[:gateway]}" if network[:gateway]
|
|
|
|
else
|
|
|
|
net_conf << "DHCP=yes"
|
|
|
|
end
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
remote_path = upload_tmp_file(comm, net_conf.join("\n"))
|
|
|
|
dest_path = "#{NETWORKD_DIRECTORY}/99-vagrant.network"
|
|
|
|
comm.sudo(["mkdir -p #{NETWORKD_DIRECTORY}",
|
|
|
|
"mv -f '#{remote_path}' '#{dest_path}'",
|
|
|
|
"chown root:root '#{dest_path}'",
|
|
|
|
"chmod 0644 '#{dest_path}'",
|
|
|
|
"systemctl restart systemd-networkd.service"].join("\n"))
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
# Configure guest networking using net-tools
|
|
|
|
def self.configure_nettools(machine, interfaces, comm, networks)
|
2017-12-14 00:44:13 +00:00
|
|
|
commands = []
|
|
|
|
entries = []
|
2017-05-10 19:26:57 +00:00
|
|
|
root_device = interfaces.first
|
2016-06-05 01:31:41 +00:00
|
|
|
networks.each do |network|
|
2016-06-06 15:36:59 +00:00
|
|
|
network[:device] = interfaces[network[:interface]]
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}",
|
2017-05-10 19:26:57 +00:00
|
|
|
options: network.merge(:root_device => root_device),
|
2016-06-05 01:31:41 +00:00
|
|
|
)
|
|
|
|
entries << entry
|
|
|
|
end
|
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
content = entries.join("\n")
|
2018-01-03 15:17:41 +00:00
|
|
|
remote_path = "/tmp/vagrant-network-entry"
|
2017-12-14 00:44:13 +00:00
|
|
|
upload_tmp_file(comm, content, remote_path)
|
2016-06-05 01:31:41 +00:00
|
|
|
|
2016-06-06 15:36:59 +00:00
|
|
|
networks.each do |network|
|
2016-06-05 01:31:41 +00:00
|
|
|
# Ubuntu 16.04+ returns an error when downing an interface that
|
|
|
|
# does not exist. The `|| true` preserves the behavior that older
|
|
|
|
# Ubuntu versions exhibit and Vagrant expects (GH-7155)
|
2016-06-06 15:36:59 +00:00
|
|
|
commands << "/sbin/ifdown '#{network[:device]}' || true"
|
|
|
|
commands << "/sbin/ip addr flush dev '#{network[:device]}'"
|
2016-06-05 01:31:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Reconfigure /etc/network/interfaces.
|
|
|
|
commands << <<-EOH.gsub(/^ {12}/, "")
|
|
|
|
# Remove any previous network modifications from the interfaces file
|
|
|
|
sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre
|
|
|
|
sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post
|
|
|
|
cat \\
|
|
|
|
/tmp/vagrant-network-interfaces.pre \\
|
|
|
|
/tmp/vagrant-network-entry \\
|
|
|
|
/tmp/vagrant-network-interfaces.post \\
|
|
|
|
> /etc/network/interfaces
|
|
|
|
rm -f /tmp/vagrant-network-interfaces.pre
|
|
|
|
rm -f /tmp/vagrant-network-entry
|
|
|
|
rm -f /tmp/vagrant-network-interfaces.post
|
|
|
|
EOH
|
|
|
|
|
|
|
|
# Bring back up each network interface, reconfigured.
|
2016-06-06 15:36:59 +00:00
|
|
|
networks.each do |network|
|
|
|
|
commands << "/sbin/ifup '#{network[:device]}'"
|
2013-04-04 06:08:33 +00:00
|
|
|
end
|
2018-01-03 15:17:41 +00:00
|
|
|
comm.sudo(commands.join("\n"))
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 15:17:41 +00:00
|
|
|
# Simple helper to upload content to guest temporary file
|
|
|
|
#
|
|
|
|
# @param [Vagrant::Plugin::Communicator] comm
|
|
|
|
# @param [String] content
|
|
|
|
# @return [String] remote path
|
|
|
|
def self.upload_tmp_file(comm, content, remote_path=nil)
|
|
|
|
if remote_path.nil?
|
|
|
|
remote_path = "/tmp/vagrant-network-entry-#{Time.now.to_i}"
|
2017-12-14 00:44:13 +00:00
|
|
|
end
|
2018-01-03 15:17:41 +00:00
|
|
|
Tempfile.open("vagrant-debian-configure-networks") do |f|
|
|
|
|
f.binmode
|
|
|
|
f.write(content)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
comm.upload(f.path, remote_path)
|
|
|
|
end
|
|
|
|
remote_path
|
2013-04-04 06:08:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|