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
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
def self.generate_netplan_cfg(options)
|
|
|
|
cfg = {"network" => {"version" => 2,
|
|
|
|
"renderer" => "networkd",
|
|
|
|
"ethernets" => {}}}
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
options.each do |option|
|
|
|
|
cfg["network"]["ethernets"].merge!(option)
|
|
|
|
end
|
|
|
|
return cfg
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_interface_entries(interface)
|
|
|
|
entry = {interface[:device] => {"dhcp4" => true}}
|
|
|
|
if interface[:ip]
|
|
|
|
# is this always the right prefix length to pick??
|
|
|
|
entry[interface[:device]].merge!({"addresses" => ["#{interface[:ip]}/24"]})
|
|
|
|
entry[interface[:device]]["dhcp4"] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if interface[:gateway]
|
|
|
|
entry[interface[:device]].merge!({"gateway4" => interface[:gateway]})
|
|
|
|
end
|
|
|
|
return entry
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.determine_systemd_networkd(comm)
|
|
|
|
return systemd?(comm) && systemd_networkd?(comm)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.upload_tmp_file(comm, content, remote_path)
|
|
|
|
Tempfile.open("vagrant-debian-configure-networks") do |f|
|
|
|
|
f.binmode
|
|
|
|
f.write(content)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
comm.upload(f.path, remote_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_netplan_networks(machine, interfaces, comm, networks)
|
|
|
|
commands = []
|
|
|
|
entries = []
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
root_device = interfaces.first
|
|
|
|
networks.each do |network|
|
|
|
|
network[:device] = interfaces[network[:interface]]
|
|
|
|
|
|
|
|
options = network.merge(:root_device => root_device)
|
|
|
|
entry = build_interface_entries(options)
|
|
|
|
entries << entry
|
|
|
|
end
|
|
|
|
|
|
|
|
remote_path = "/tmp/vagrant-network-entry"
|
|
|
|
|
|
|
|
netplan_cfg = generate_netplan_cfg(entries)
|
|
|
|
content = netplan_cfg.to_yaml
|
|
|
|
upload_tmp_file(comm, content, remote_path)
|
|
|
|
|
|
|
|
commands << <<-EOH.gsub(/^ {12}/, "")
|
|
|
|
mv '#{remote_path}' /etc/netplan/99-vagrant.yaml
|
|
|
|
sudo netplan apply
|
|
|
|
EOH
|
|
|
|
|
|
|
|
return commands
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networkd_networks(machine, interfaces, comm, networks)
|
|
|
|
commands = []
|
|
|
|
entries = []
|
|
|
|
|
|
|
|
root_device = interfaces.first
|
|
|
|
networks.each.with_index do |network,i|
|
|
|
|
network[:device] = interfaces[network[:interface]]
|
|
|
|
# generic systemd-networkd config file
|
|
|
|
# update for debian
|
|
|
|
entry = TemplateRenderer.render("guests/debian/networkd/network_#{network[:type]}",
|
|
|
|
options: network,
|
|
|
|
)
|
|
|
|
|
|
|
|
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
|
|
|
upload_tmp_file(comm, entry, remote_path)
|
|
|
|
|
|
|
|
commands << <<-EOH.gsub(/^ {14}/, '').rstrip
|
|
|
|
# Configure #{network[:device]}
|
|
|
|
mv '#{remote_path}' '/etc/systemd/network/#{network[:device]}.network' &&
|
|
|
|
sudo chown root:root '/etc/systemd/network/#{network[:device]}.network' &&
|
|
|
|
sudo chmod 644 '/etc/systemd/network/#{network[:device]}.network' &&
|
|
|
|
ip link set '#{network[:device]}' down &&
|
|
|
|
sudo rm /etc/resolv.conf &&
|
|
|
|
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf &&
|
|
|
|
sudo systemctl enable systemd-resolved.service &&
|
|
|
|
sudo systemctl start systemd-resolved.service &&
|
|
|
|
sudo systemctl enable systemd-networkd.service
|
|
|
|
sudo systemctl start systemd-networkd.service
|
|
|
|
EOH
|
|
|
|
end
|
|
|
|
|
|
|
|
return commands
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_other_networks(machine, interfaces, comm, networks)
|
|
|
|
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
|
|
|
remote_path = "/tmp/vagrant-network-entry"
|
|
|
|
content = entries.join("\n")
|
|
|
|
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
|
2016-06-05 01:31:41 +00:00
|
|
|
|
2017-12-14 00:44:13 +00:00
|
|
|
return commands
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
|
|
|
comm = machine.communicate
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
interfaces = machine.guest.capability(:network_interfaces)
|
|
|
|
|
|
|
|
systemd_controlled = determine_systemd_networkd(comm)
|
|
|
|
netplan_cli = netplan?(comm)
|
|
|
|
|
|
|
|
if systemd_controlled
|
|
|
|
if netplan_cli
|
|
|
|
commands = configure_netplan_networks(machine, interfaces, comm, networks)
|
|
|
|
else
|
|
|
|
commands = configure_networkd_networks(machine, interfaces, comm, networks)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
commands = configure_other_networks(machine, interfaces, comm, networks)
|
|
|
|
end
|
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
# Run all the commands in one session to prevent partial configuration
|
|
|
|
# due to a severed network.
|
|
|
|
comm.sudo(commands.join("\n"))
|
2013-04-04 06:08:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|