diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb new file mode 100644 index 000000000..dc949df5e --- /dev/null +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -0,0 +1,58 @@ +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestDebian + module Cap + class ConfigureNetworks + include Vagrant::Util + + def self.configure_networks(machine, networks) + machine.communicate.tap do |comm| + # First, remove any previous network modifications + # from the interface file. + comm.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces") + comm.sudo("su -c 'cat /tmp/vagrant-network-interfaces > /etc/network/interfaces'") + comm.sudo("rm /tmp/vagrant-network-interfaces") + + # 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]}", + :options => network) + + entries << entry + end + + # Perform the careful dance necessary to reconfigure + # the network interfaces + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(entries.join("\n")) + temp.close + + comm.upload(temp.path, "/tmp/vagrant-network-entry") + + # 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| + comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null") + end + + comm.sudo("cat /tmp/vagrant-network-entry >> /etc/network/interfaces") + comm.sudo("rm /tmp/vagrant-network-entry") + + # Bring back up each network interface, reconfigured + interfaces.each do |interface| + comm.sudo("/sbin/ifup eth#{interface}") + end + end + end + end + end + end +end diff --git a/plugins/guests/debian/guest.rb b/plugins/guests/debian/guest.rb index 2712b32c7..4dfb38a38 100644 --- a/plugins/guests/debian/guest.rb +++ b/plugins/guests/debian/guest.rb @@ -16,51 +16,6 @@ module VagrantPlugins machine.communicate.test("cat /proc/version | grep 'Debian'") end - def configure_networks(networks) - # First, remove any previous network modifications - # from the interface file. - vm.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces") - vm.communicate.sudo("su -c 'cat /tmp/vagrant-network-interfaces > /etc/network/interfaces'") - vm.communicate.sudo("rm /tmp/vagrant-network-interfaces") - - # 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]}", - :options => network) - - entries << entry - end - - # Perform the careful dance necessary to reconfigure - # the network interfaces - temp = Tempfile.new("vagrant") - temp.binmode - temp.write(entries.join("\n")) - temp.close - - vm.communicate.upload(temp.path, "/tmp/vagrant-network-entry") - - # 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| - vm.communicate.sudo("/sbin/ifdown eth#{interface} 2> /dev/null") - end - - vm.communicate.sudo("cat /tmp/vagrant-network-entry >> /etc/network/interfaces") - vm.communicate.sudo("rm /tmp/vagrant-network-entry") - - # Bring back up each network interface, reconfigured - interfaces.each do |interface| - vm.communicate.sudo("/sbin/ifup eth#{interface}") - end - end - def change_host_name(name) vm.communicate.tap do |comm| if !comm.test("hostname --fqdn | grep '^#{name}$' || hostname --short | grep '^#{name}$'") diff --git a/plugins/guests/debian/plugin.rb b/plugins/guests/debian/plugin.rb index b9190d4bf..f0c9ccceb 100644 --- a/plugins/guests/debian/plugin.rb +++ b/plugins/guests/debian/plugin.rb @@ -10,6 +10,11 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("debian", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end end end end diff --git a/plugins/guests/linux/cap/shell_expand_guest_path.rb b/plugins/guests/linux/cap/shell_expand_guest_path.rb index 1869ccfb3..23ee6174f 100644 --- a/plugins/guests/linux/cap/shell_expand_guest_path.rb +++ b/plugins/guests/linux/cap/shell_expand_guest_path.rb @@ -4,7 +4,7 @@ module VagrantPlugins class ShellExpandGuestPath def self.shell_expand_guest_path(machine, path) real_path = nil - machine.communicate.execute("printf #{guestpath}") do |type, data| + machine.communicate.execute("printf #{path}") do |type, data| if type == :stdout real_path ||= "" real_path += data diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index dca3000c9..736524eaa 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -112,7 +112,7 @@ module VagrantPlugins # Only configure the networks the user requested us to configure networks_to_configure = networks.select { |n| n[:auto_config] } env[:ui].info I18n.t("vagrant.actions.vm.network.configuring") - env[:machine].guest.configure_networks(networks_to_configure) + env[:machine].guest.capability(:configure_networks, networks_to_configure) end end