Switch configure_networks to capabilities system

This commit is contained in:
Mitchell Hashimoto 2013-04-03 23:08:33 -07:00
parent 28d3f274d8
commit 2a542dab02
5 changed files with 65 additions and 47 deletions

View File

@ -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

View File

@ -16,51 +16,6 @@ module VagrantPlugins
machine.communicate.test("cat /proc/version | grep 'Debian'") machine.communicate.test("cat /proc/version | grep 'Debian'")
end 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) def change_host_name(name)
vm.communicate.tap do |comm| vm.communicate.tap do |comm|
if !comm.test("hostname --fqdn | grep '^#{name}$' || hostname --short | grep '^#{name}$'") if !comm.test("hostname --fqdn | grep '^#{name}$' || hostname --short | grep '^#{name}$'")

View File

@ -10,6 +10,11 @@ module VagrantPlugins
require File.expand_path("../guest", __FILE__) require File.expand_path("../guest", __FILE__)
Guest Guest
end end
guest_capability("debian", "configure_networks") do
require_relative "cap/configure_networks"
Cap::ConfigureNetworks
end
end end
end end
end end

View File

@ -4,7 +4,7 @@ module VagrantPlugins
class ShellExpandGuestPath class ShellExpandGuestPath
def self.shell_expand_guest_path(machine, path) def self.shell_expand_guest_path(machine, path)
real_path = nil real_path = nil
machine.communicate.execute("printf #{guestpath}") do |type, data| machine.communicate.execute("printf #{path}") do |type, data|
if type == :stdout if type == :stdout
real_path ||= "" real_path ||= ""
real_path += data real_path += data

View File

@ -112,7 +112,7 @@ module VagrantPlugins
# Only configure the networks the user requested us to configure # Only configure the networks the user requested us to configure
networks_to_configure = networks.select { |n| n[:auto_config] } networks_to_configure = networks.select { |n| n[:auto_config] }
env[:ui].info I18n.t("vagrant.actions.vm.network.configuring") 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
end end