2013-04-04 19:09:40 +00:00
|
|
|
require "set"
|
|
|
|
require "tempfile"
|
|
|
|
|
2013-04-08 18:02:03 +00:00
|
|
|
require "vagrant/util/retryable"
|
2013-04-04 19:09:40 +00:00
|
|
|
require "vagrant/util/template_renderer"
|
|
|
|
|
|
|
|
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)
|
|
|
|
network_scripts_dir = machine.guest.capability("network_scripts_dir")
|
|
|
|
|
|
|
|
# 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|
|
|
|
|
interfaces.add(network[:interface])
|
|
|
|
|
2013-12-10 20:04:36 +00:00
|
|
|
# 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-11-02 19:15:58 +00:00
|
|
|
|
2013-04-04 19:09:40 +00:00
|
|
|
# Remove any previous vagrant configuration in this network interface's
|
|
|
|
# configuration files.
|
|
|
|
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 /tmp/vagrant-ifcfg-eth#{network[:interface]}")
|
|
|
|
|
|
|
|
# Render and upload the network entry file to a deterministic
|
|
|
|
# temporary location.
|
|
|
|
entry = TemplateRenderer.render("guests/redhat/network_#{network[:type]}",
|
|
|
|
:options => network)
|
|
|
|
|
|
|
|
temp = Tempfile.new("vagrant")
|
|
|
|
temp.binmode
|
|
|
|
temp.write(entry)
|
|
|
|
temp.close
|
|
|
|
|
|
|
|
machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry_#{network[:interface]}")
|
|
|
|
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|
|
2013-04-08 18:02:03 +00:00
|
|
|
retryable(:on => Vagrant::Errors::VagrantError, :tries => 3, :sleep => 2) do
|
2013-12-10 17:04:20 +00:00
|
|
|
# The interface should already be down so this probably
|
2013-12-10 20:04:36 +00:00
|
|
|
# won't do anything, so we run it with error_check false.
|
2013-12-10 17:04:20 +00:00
|
|
|
machine.communicate.sudo(
|
|
|
|
"/sbin/ifdown eth#{interface} 2> /dev/null", error_check: false)
|
|
|
|
|
|
|
|
# Add the new interface and bring it up
|
2013-04-08 18:02:03 +00:00
|
|
|
machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}")
|
2013-06-09 18:14:33 +00:00
|
|
|
machine.communicate.sudo("ARPCHECK=no /sbin/ifup eth#{interface} 2> /dev/null")
|
2013-04-08 18:02:03 +00:00
|
|
|
end
|
|
|
|
|
2013-04-04 19:09:40 +00:00
|
|
|
machine.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|