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
|
2013-04-04 06:08:33 +00:00
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
2016-06-05 01:31:41 +00:00
|
|
|
comm = machine.communicate
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2016-06-25 01:18:16 +00:00
|
|
|
commands = ["set -e"]
|
2016-06-05 01:31:41 +00:00
|
|
|
entries = []
|
2016-06-23 02:08:02 +00:00
|
|
|
interfaces = machine.guest.capability(:network_interfaces)
|
2013-04-04 06:08:33 +00:00
|
|
|
|
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]}",
|
|
|
|
options: network,
|
|
|
|
)
|
|
|
|
entries << entry
|
|
|
|
end
|
|
|
|
|
|
|
|
Tempfile.open("vagrant-debian-configure-networks") do |f|
|
|
|
|
f.binmode
|
|
|
|
f.write(entries.join("\n"))
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
comm.upload(f.path, "/tmp/vagrant-network-entry")
|
|
|
|
end
|
|
|
|
|
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
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
cat \\
|
|
|
|
/tmp/vagrant-network-interfaces.pre \\
|
|
|
|
/tmp/vagrant-network-entry \\
|
|
|
|
/tmp/vagrant-network-interfaces.post \\
|
|
|
|
> /etc/network/interfaces
|
2013-04-04 06:08:33 +00:00
|
|
|
|
2016-06-05 01:31:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
# 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
|