From c92ce0f4d0bb5f9d33292630b97db3477e7b0ed7 Mon Sep 17 00:00:00 2001 From: Philipp Franke Date: Fri, 3 May 2013 12:39:26 +0200 Subject: [PATCH] Add OpenSUSE network settings --- plugins/guests/suse/cap/configure_networks.rb | 60 +++++++++++++++++++ plugins/guests/suse/plugin.rb | 5 ++ templates/guests/suse/network_dhcp.erb | 6 ++ templates/guests/suse/network_static.erb | 10 ++++ 4 files changed, 81 insertions(+) create mode 100644 plugins/guests/suse/cap/configure_networks.rb create mode 100644 templates/guests/suse/network_dhcp.erb create mode 100644 templates/guests/suse/network_static.erb diff --git a/plugins/guests/suse/cap/configure_networks.rb b/plugins/guests/suse/cap/configure_networks.rb new file mode 100644 index 000000000..a87f6d172 --- /dev/null +++ b/plugins/guests/suse/cap/configure_networks.rb @@ -0,0 +1,60 @@ +require "set" +require "tempfile" + +require "vagrant/util/retryable" +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestSuse + module Cap + class ConfigureNetworks + extend Vagrant::Util::Retryable + include Vagrant::Util + + 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]) + + # 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/suse/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| + retryable(:on => Vagrant::Errors::VagrantError, :tries => 3, :sleep => 2) do + machine.communicate.sudo("/sbin/ifdown eth#{interface} 2> /dev/null", :error_check => false) + machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}") + machine.communicate.sudo("/sbin/ifup eth#{interface} 2> /dev/null") + end + + machine.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}") + end + end + end + end + end +end diff --git a/plugins/guests/suse/plugin.rb b/plugins/guests/suse/plugin.rb index bf9fbfbb3..c5ffb0f9c 100644 --- a/plugins/guests/suse/plugin.rb +++ b/plugins/guests/suse/plugin.rb @@ -16,6 +16,11 @@ module VagrantPlugins Cap::ChangeHostName end + guest_capability("suse", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end + guest_capability("suse", "network_scripts_dir") do require_relative "cap/network_scripts_dir" Cap::NetworkScriptsDir diff --git a/templates/guests/suse/network_dhcp.erb b/templates/guests/suse/network_dhcp.erb new file mode 100644 index 000000000..8bbaa62e4 --- /dev/null +++ b/templates/guests/suse/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/suse/network_static.erb b/templates/guests/suse/network_static.erb new file mode 100644 index 000000000..7388fc355 --- /dev/null +++ b/templates/guests/suse/network_static.erb @@ -0,0 +1,10 @@ +#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] %>' +PEERDNS=no +STARTMODE='auto' +USERCONTROL='no' +#VAGRANT-END