diff --git a/lib/vagrant/guest/redhat.rb b/lib/vagrant/guest/redhat.rb index 49cfac2d0..d4bfbba80 100644 --- a/lib/vagrant/guest/redhat.rb +++ b/lib/vagrant/guest/redhat.rb @@ -1,27 +1,80 @@ +require 'set' + +require 'vagrant/util/template_renderer' + module Vagrant module Guest class Redhat < Linux - def prepare_host_only_network(net_options) - # Remove any previous host only network additions to the - # interface file. + # Make the TemplateRenderer top-level + include Vagrant::Util + + def configure_networks(networks) + # 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 + + # Since redhat/centos uses a single file for each interface, + # we must loop through for each network assigned + networks.each do |network| + interfaces.add(network[:interface]) + + # First, remove any previous network modifications + # from the interface file. + vm.ssh.execute do |ssh| + ssh.exec!("sudo touch #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") + ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}") + ssh.exec!("sudo su -c 'cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}'") + end + + entry = "" + entry << TemplateRenderer.render("guests/redhat/network_#{network[:type]}", + :options => network) + + # Perform the careful dance necessary to to reconfigure + # the network interfaces + vm.ssh.upload!(StringIO.new(entry), "/tmp/vagrant-network-entry_#{network[:interface]}") + end + vm.ssh.execute do |ssh| - # Clear out any previous entries - ssh.exec!("sudo touch #{network_scripts_dir}/ifcfg-eth#{net_options[:adapter]}") - ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN-HOSTONLY/,/^#VAGRANT-END-HOSTONLY/ d' #{network_scripts_dir}/ifcfg-eth#{net_options[:adapter]} > /tmp/vagrant-ifcfg-eth#{net_options[:adapter]}") - ssh.exec!("sudo su -c 'cat /tmp/vagrant-ifcfg-eth#{net_options[:adapter]} > #{network_scripts_dir}/ifcfg-eth#{net_options[:adapter]}'") + # 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| + ssh.exec!("sudo /sbin/ifdown eth#{interface} 2> /dev/null") + ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}'") + # Bring back up each network interface, reconfigured + ssh.exec!("sudo /sbin/ifup eth#{interface}") + end end end - def enable_host_only_network(net_options) - entry = TemplateRenderer.render('guests/redhat/network_hostonly', - :net_options => net_options) + def prepare_bridged_networks(networks) + # Remove any previous bridged network additions from the + # interface file. + vm.ssh.execute do |ssh| + networks.each do |network| + # Clear out any previous entries + ssh.exec!("sudo touch #{network_scripts_dir}/ifcfg-eth#{network[:adapter]}") + ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN-BRIDGED/,/^#VAGRANT-END-BRIDGED/ d' #{network_scripts_dir}/ifcfg-eth#{network[:adapter]} > /tmp/vagrant-ifcfg-eth#{network[:adapter]}") + ssh.exec!("sudo su -c 'cat /tmp/vagrant-ifcfg-eth#{network[:adapter]} > #{network_scripts_dir}/ifcfg-eth#{network[:adapter]}'") + end + end + end + + def enable_bridged_networks(networks) + entry = TemplateRenderer.render('guests/redhat/network_bridged', + :networks => networks) + vm.ssh.upload!(StringIO.new(entry), "/tmp/vagrant-network-entry") vm.ssh.execute do |ssh| - interface_up = ssh.test?("/sbin/ifconfig eth#{net_options[:adapter]} | grep 'inet addr:'") - ssh.exec!("sudo /sbin/ifdown eth#{net_options[:adapter]} 2> /dev/null") if interface_up - ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-entry >> #{network_scripts_dir}/ifcfg-eth#{net_options[:adapter]}'") - ssh.exec!("sudo /sbin/ifup eth#{net_options[:adapter]}") + networks.each do |network| + interface_up = ssh.test?("/sbin/ifconfig eth#{network[:adapter]} | grep 'inet addr:'") + ssh.exec!("sudo /sbin/ifdown eth#{network[:adapter]} 2> /dev/null") if interface_up + ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-entry >> #{network_scripts_dir}/ifcfg-eth#{network[:adapter]}'") + ssh.exec!("sudo /sbin/ifup eth#{network[:adapter]}") + end end end @@ -30,7 +83,7 @@ module Vagrant # operationg systems (SuSE) which behave similarly but with a different # path to the network scripts. def network_scripts_dir - '/etc/sysconfig/network-scripts/' + '/etc/sysconfig/network-scripts' end def change_host_name(name) diff --git a/templates/guests/redhat/network_dhcp.erb b/templates/guests/redhat/network_dhcp.erb new file mode 100644 index 000000000..8bbaa62e4 --- /dev/null +++ b/templates/guests/redhat/network_dhcp.erb @@ -0,0 +1,6 @@ +#VAGRANT-BEGIN +# The contents below are automatically generated by Vagrant. Do not modify. +BOOTPROTO=dhcp +ONBOOT=yes +DEVICE=eth<%= options[:interface] %> +#VAGRANT-END diff --git a/templates/guests/redhat/network_hostonly.erb b/templates/guests/redhat/network_hostonly.erb deleted file mode 100644 index 1ca1a8c98..000000000 --- a/templates/guests/redhat/network_hostonly.erb +++ /dev/null @@ -1,8 +0,0 @@ -#VAGRANT-BEGIN-HOSTONLY -# The contents below are automatically generated by Vagrant. Do not modify. -BOOTPROTO=static -DHCPCLASS= -IPADDR=<%= net_options[:ip] %> -NETMASK=<%= net_options[:netmask] %> -DEVICE=eth<%= net_options[:adapter] %> -#VAGRANT-END-HOSTONLY diff --git a/templates/guests/redhat/network_static.erb b/templates/guests/redhat/network_static.erb new file mode 100644 index 000000000..acff23abc --- /dev/null +++ b/templates/guests/redhat/network_static.erb @@ -0,0 +1,7 @@ +#VAGRANT-BEGIN +# The contents below are automatically generated by Vagrant. Do not modify. +BOOTPROTO=static +IPADDR=<%= options[:ip] %> +NETMASK=<%= options[:netmask] %> +DEVICE=eth<%= options[:interface] %> +#VAGRANT-END