2013-04-04 18:49:26 +00:00
|
|
|
require "set"
|
|
|
|
require "tempfile"
|
|
|
|
|
2013-04-08 18:08:14 +00:00
|
|
|
require "vagrant/util/retryable"
|
2013-04-04 18:49:26 +00:00
|
|
|
require "vagrant/util/template_renderer"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestFedora
|
|
|
|
module Cap
|
|
|
|
class ConfigureNetworks
|
2013-04-08 18:08:14 +00:00
|
|
|
extend Vagrant::Util::Retryable
|
2013-04-08 17:47:19 +00:00
|
|
|
include Vagrant::Util
|
2013-04-04 18:49:26 +00:00
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
|
|
|
network_scripts_dir = machine.guest.capability("network_scripts_dir")
|
|
|
|
|
2014-08-05 03:02:28 +00:00
|
|
|
virtual = false
|
2014-03-13 11:32:00 +00:00
|
|
|
interface_names = Array.new
|
2015-06-30 21:43:36 +00:00
|
|
|
interface_names_by_slot = Array.new
|
2015-10-11 21:09:10 +00:00
|
|
|
machine.communicate.sudo("/usr/sbin/biosdevname &>/dev/null; echo $?") do |_, result|
|
|
|
|
# The above command returns:
|
|
|
|
# - '4' if /usr/sbin/biosdevname detects it is running in a virtual machine
|
|
|
|
# - '127' if /usr/sbin/biosdevname doesn't exist
|
2015-05-12 20:43:18 +00:00
|
|
|
virtual = true if ['4', '127'].include? result.chomp
|
2014-03-13 11:32:00 +00:00
|
|
|
end
|
|
|
|
|
2014-08-05 03:02:28 +00:00
|
|
|
if virtual
|
2015-05-12 20:18:31 +00:00
|
|
|
machine.communicate.sudo("ls /sys/class/net | egrep -v lo\\|docker") do |_, result|
|
2014-08-05 03:02:28 +00:00
|
|
|
interface_names = result.split("\n")
|
2014-03-13 11:32:00 +00:00
|
|
|
end
|
|
|
|
|
2015-06-30 21:43:36 +00:00
|
|
|
interface_names_by_slot = networks.map do |network|
|
2014-12-21 07:25:11 +00:00
|
|
|
"#{interface_names[network[:interface]]}"
|
2014-08-05 03:02:28 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
machine.communicate.sudo("/usr/sbin/biosdevname -d | grep Kernel | cut -f2 -d: | sed -e 's/ //;'") do |_, result|
|
|
|
|
interface_names = result.split("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
interface_name_pairs = Array.new
|
|
|
|
interface_names.each do |interface_name|
|
|
|
|
machine.communicate.sudo("/usr/sbin/biosdevname --policy=all_ethN -i #{interface_name}") do |_, result|
|
|
|
|
interface_name_pairs.push([interface_name, result.gsub("\n", "")])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setting_interface_names = networks.map do |network|
|
|
|
|
"eth#{network[:interface]}"
|
|
|
|
end
|
2014-03-13 11:32:00 +00:00
|
|
|
|
2015-06-30 21:43:36 +00:00
|
|
|
interface_names_by_slot = interface_names.dup
|
2014-08-05 03:02:28 +00:00
|
|
|
interface_name_pairs.each do |interface_name, previous_interface_name|
|
|
|
|
if setting_interface_names.index(previous_interface_name) == nil
|
2015-06-30 21:43:36 +00:00
|
|
|
interface_names_by_slot.delete(interface_name)
|
2014-08-05 03:02:28 +00:00
|
|
|
end
|
2014-03-13 11:32:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-30 21:43:36 +00:00
|
|
|
# Read interface MAC addresses for later matching
|
|
|
|
mac_addresses = Array.new(interface_names.length)
|
|
|
|
interface_names.each_with_index do |ifname, index|
|
|
|
|
machine.communicate.sudo("cat /sys/class/net/#{ifname}/address") do |_, result|
|
|
|
|
mac_addresses[index] = result.strip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-04 18:49:26 +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
|
|
|
|
networks.each do |network|
|
2015-06-30 21:43:36 +00:00
|
|
|
interface = nil
|
|
|
|
if network[:mac_address]
|
|
|
|
found_idx = mac_addresses.find_index(network[:mac_address])
|
|
|
|
# Ignore network if requested MAC address could not be found
|
|
|
|
next if found_idx.nil?
|
|
|
|
interface = interface_names[found_idx]
|
|
|
|
else
|
|
|
|
ifname_by_slot = interface_names_by_slot[network[:interface]-1]
|
|
|
|
# Don't overwrite if interface was already matched via MAC address
|
|
|
|
next if interfaces.include?(ifname_by_slot)
|
|
|
|
interface = ifname_by_slot
|
|
|
|
end
|
|
|
|
|
2014-03-13 11:32:00 +00:00
|
|
|
interfaces.add(interface)
|
|
|
|
network[:device] = interface
|
2013-04-04 18:49:26 +00:00
|
|
|
|
|
|
|
# Remove any previous vagrant configuration in this network
|
|
|
|
# interface's configuration files.
|
2014-03-13 11:32:00 +00:00
|
|
|
machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-#{interface}")
|
|
|
|
machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-#{interface} > /tmp/vagrant-ifcfg-#{interface}")
|
|
|
|
machine.communicate.sudo("cat /tmp/vagrant-ifcfg-#{interface} > #{network_scripts_dir}/ifcfg-#{interface}")
|
2014-08-29 08:51:31 +00:00
|
|
|
machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-#{interface}")
|
2013-04-04 18:49:26 +00:00
|
|
|
|
|
|
|
# Render and upload the network entry file to a deterministic
|
|
|
|
# temporary location.
|
|
|
|
entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}",
|
2014-05-22 16:35:12 +00:00
|
|
|
options: network)
|
2013-04-04 18:49:26 +00:00
|
|
|
|
|
|
|
temp = Tempfile.new("vagrant")
|
|
|
|
temp.binmode
|
|
|
|
temp.write(entry)
|
|
|
|
temp.close
|
|
|
|
|
2014-03-13 11:32:00 +00:00
|
|
|
machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry_#{interface}")
|
2013-04-04 18:49:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Bring down all the interfaces we're reconfiguring. By bringing down
|
|
|
|
# each specifically, we avoid reconfiguring p7p (the NAT interface) so
|
|
|
|
# SSH never dies.
|
|
|
|
interfaces.each do |interface|
|
2014-05-22 16:35:12 +00:00
|
|
|
retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do
|
2015-11-18 23:55:11 +00:00
|
|
|
machine.communicate.sudo(<<-SCRIPT, error_check: true)
|
|
|
|
cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-#{interface}
|
|
|
|
|
|
|
|
if command -v nmcli &>/dev/null; then
|
|
|
|
if command -v systemctl &>/dev/null && systemctl -q is-enabled NetworkManager &>/dev/null; then
|
|
|
|
nmcli c reload #{interface}
|
|
|
|
elif command -v service &>/dev/null && service NetworkManager status &>/dev/null; then
|
|
|
|
nmcli c reload #{interface}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
/sbin/ifdown #{interface}
|
|
|
|
/sbin/ifup #{interface}
|
|
|
|
|
|
|
|
rm -f /tmp/vagrant-network-entry_#{interface}
|
|
|
|
SCRIPT
|
2013-04-08 18:08:14 +00:00
|
|
|
end
|
2013-04-04 18:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|