2013-04-04 19:09:40 +00:00
|
|
|
require "set"
|
2016-05-31 03:17:02 +00:00
|
|
|
require "tempfile"
|
2013-04-04 19:09:40 +00:00
|
|
|
|
2016-05-29 03:17:40 +00:00
|
|
|
require_relative "../../../../lib/vagrant/util/retryable"
|
|
|
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
2013-04-04 19:09:40 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module GuestRedHat
|
|
|
|
module Cap
|
|
|
|
class ConfigureNetworks
|
2013-04-08 18:02:03 +00:00
|
|
|
extend Vagrant::Util::Retryable
|
2013-04-08 17:47:19 +00:00
|
|
|
include Vagrant::Util
|
2013-04-04 19:09:40 +00:00
|
|
|
|
|
|
|
def self.configure_networks(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
case machine.guest.capability("flavor")
|
|
|
|
when :rhel_7
|
2014-09-03 15:07:55 +00:00
|
|
|
configure_networks_rhel7(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
else
|
2014-09-03 15:07:55 +00:00
|
|
|
configure_networks_default(machine, networks)
|
2014-08-31 05:49:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networks_rhel7(machine, networks)
|
|
|
|
# This is kind of jank but the configure networks is the same
|
|
|
|
# as Fedora at this point.
|
2014-09-04 19:51:10 +00:00
|
|
|
require File.expand_path("../../../fedora/cap/configure_networks", __FILE__)
|
2014-08-31 05:49:13 +00:00
|
|
|
::VagrantPlugins::GuestFedora::Cap::ConfigureNetworks.
|
|
|
|
configure_networks(machine, networks)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.configure_networks_default(machine, networks)
|
2013-04-04 19:09:40 +00:00
|
|
|
network_scripts_dir = machine.guest.capability("network_scripts_dir")
|
2014-05-06 20:26:44 +00:00
|
|
|
|
2013-04-04 19:09:40 +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|
|
2014-05-06 20:26:44 +00:00
|
|
|
interfaces.add(network[:interface])
|
|
|
|
|
|
|
|
# Down the interface before munging the config file. This might fail
|
|
|
|
# if the interface is not actually set up yet so ignore errors.
|
|
|
|
machine.communicate.sudo(
|
|
|
|
"/sbin/ifdown eth#{network[:interface]} 2> /dev/null", error_check: false)
|
|
|
|
|
2013-04-04 19:09:40 +00:00
|
|
|
# Remove any previous vagrant configuration in this network interface's
|
|
|
|
# configuration files.
|
2014-05-06 20:26:44 +00:00
|
|
|
machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-eth#{network[:interface]}")
|
|
|
|
machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}")
|
|
|
|
machine.communicate.sudo("cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}")
|
|
|
|
machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-eth#{network[:interface]}")
|
|
|
|
|
2013-04-04 19:09:40 +00:00
|
|
|
# Render and upload the network entry file to a deterministic
|
|
|
|
# temporary location.
|
2014-05-06 12:48:01 +00:00
|
|
|
entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}",
|
2014-05-22 16:35:12 +00:00
|
|
|
options: network)
|
2013-04-04 19:09:40 +00:00
|
|
|
|
2016-05-31 04:18:16 +00:00
|
|
|
Tempfile.open("vagrant-red-hat-configure-networks") do |f|
|
2016-05-31 03:17:02 +00:00
|
|
|
f.binmode
|
2016-05-29 03:17:40 +00:00
|
|
|
f.write(entry)
|
|
|
|
f.fsync
|
|
|
|
f.close
|
|
|
|
machine.communicate.upload(f.path, "/tmp/vagrant-network-entry_#{network[:interface]}")
|
|
|
|
end
|
2013-04-04 19:09:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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|
|
2014-05-22 16:35:12 +00:00
|
|
|
retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do
|
2014-05-06 20:26:44 +00:00
|
|
|
# The interface should already be down so this probably
|
|
|
|
# won't do anything, so we run it with error_check false.
|
|
|
|
machine.communicate.sudo(
|
|
|
|
"/sbin/ifdown eth#{interface} 2> /dev/null", error_check: false)
|
|
|
|
|
|
|
|
# Add the new interface and bring it up
|
|
|
|
machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}")
|
|
|
|
machine.communicate.sudo("ARPCHECK=no /sbin/ifup eth#{interface} 2> /dev/null")
|
2013-04-08 18:02:03 +00:00
|
|
|
end
|
2014-05-06 20:26:44 +00:00
|
|
|
|
|
|
|
machine.communicate.sudo("rm -f /tmp/vagrant-network-entry_#{interface}")
|
2013-04-04 19:09:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|