2016-05-29 03:17:40 +00:00
|
|
|
require "set"
|
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)
|
|
|
|
machine.communicate.tap do |comm|
|
|
|
|
# First, remove any previous network modifications
|
|
|
|
# from the interface file.
|
2014-03-24 15:44:09 +00:00
|
|
|
comm.sudo("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre")
|
2015-02-11 14:35:05 +00:00
|
|
|
comm.sudo("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
|
|
|
|
|
|
|
# Accumulate the configurations to add to the interfaces file as
|
|
|
|
# well as what interfaces we're actually configuring since we use that
|
|
|
|
# later.
|
|
|
|
interfaces = Set.new
|
|
|
|
entries = []
|
|
|
|
networks.each do |network|
|
|
|
|
interfaces.add(network[:interface])
|
|
|
|
entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}",
|
2014-05-22 16:35:12 +00:00
|
|
|
options: network)
|
2013-04-04 06:08:33 +00:00
|
|
|
|
|
|
|
entries << entry
|
|
|
|
end
|
|
|
|
|
2016-05-29 03:17:40 +00:00
|
|
|
# Perform the careful dance necessary to reconfigure the network
|
|
|
|
# interfaces.
|
2016-05-31 03:17:02 +00:00
|
|
|
Tempfile.open("debian-configure-networks") do |f|
|
|
|
|
f.binmode
|
2016-05-29 03:17:40 +00:00
|
|
|
f.write(entries.join("\n"))
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
comm.upload(f.path, "/tmp/vagrant-network-entry")
|
|
|
|
end
|
2013-04-04 06:08:33 +00:00
|
|
|
|
|
|
|
# Bring down all the interfaces we're reconfiguring. By bringing down
|
|
|
|
# each specifically, we avoid reconfiguring eth0 (the NAT interface) so
|
|
|
|
# SSH never dies.
|
|
|
|
interfaces.each do |interface|
|
2016-03-20 16:19:03 +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)
|
|
|
|
comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null || true")
|
2013-11-26 12:40:50 +00:00
|
|
|
comm.sudo("/sbin/ip addr flush dev eth#{interface} 2> /dev/null")
|
2013-04-04 06:08:33 +00:00
|
|
|
end
|
|
|
|
|
2014-03-24 15:44:09 +00:00
|
|
|
comm.sudo('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces')
|
|
|
|
comm.sudo('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post')
|
2013-04-04 06:08:33 +00:00
|
|
|
|
|
|
|
# Bring back up each network interface, reconfigured
|
|
|
|
interfaces.each do |interface|
|
|
|
|
comm.sudo("/sbin/ifup eth#{interface}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|