From 5e09577a766487e2671ed935050b4273575e249e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Apr 2013 12:09:40 -0700 Subject: [PATCH] Lots more guest capabilities converted --- .../guests/fedora/cap/configure_networks.rb | 20 +++--- plugins/guests/fedora/plugin.rb | 7 +- plugins/guests/gentoo/cap/change_host_name.rb | 17 +++++ .../guests/gentoo/cap/configure_networks.rb | 43 ++++++++++++ plugins/guests/gentoo/guest.rb | 48 +------------ plugins/guests/gentoo/plugin.rb | 10 +++ plugins/guests/openbsd/cap/halt.rb | 16 +++++ plugins/guests/openbsd/guest.rb | 8 +-- plugins/guests/openbsd/plugin.rb | 5 ++ plugins/guests/pld/cap/network_scripts_dir.rb | 11 +++ plugins/guests/pld/guest.rb | 8 +-- plugins/guests/pld/plugin.rb | 5 ++ .../cap/change_host_name.rb | 2 +- .../guests/redhat/cap/configure_networks.rb | 55 +++++++++++++++ .../guests/redhat/cap/network_scripts_dir.rb | 11 +++ plugins/guests/redhat/guest.rb | 69 +------------------ plugins/guests/redhat/plugin.rb | 15 ++++ 17 files changed, 204 insertions(+), 146 deletions(-) create mode 100644 plugins/guests/gentoo/cap/change_host_name.rb create mode 100644 plugins/guests/gentoo/cap/configure_networks.rb create mode 100644 plugins/guests/openbsd/cap/halt.rb create mode 100644 plugins/guests/pld/cap/network_scripts_dir.rb rename plugins/guests/{fedora => redhat}/cap/change_host_name.rb (96%) create mode 100644 plugins/guests/redhat/cap/configure_networks.rb create mode 100644 plugins/guests/redhat/cap/network_scripts_dir.rb diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb index 546d5c516..5699d84b0 100644 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ b/plugins/guests/fedora/cap/configure_networks.rb @@ -7,7 +7,7 @@ module VagrantPlugins module GuestFedora module Cap class ConfigureNetworks - include Vagrant::Util + extend Vagrant::Util def self.configure_networks(machine, networks) network_scripts_dir = machine.guest.capability("network_scripts_dir") @@ -20,10 +20,10 @@ module VagrantPlugins # Remove any previous vagrant configuration in this network # interface's configuration files. - vm.communicate.sudo("touch #{network_scripts_dir}/ifcfg-p7p#{network[:interface]}") - vm.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-p7p#{network[:interface]} > /tmp/vagrant-ifcfg-p7p#{network[:interface]}") - vm.communicate.sudo("cat /tmp/vagrant-ifcfg-p7p#{network[:interface]} > #{network_scripts_dir}/ifcfg-p7p#{network[:interface]}") - vm.communicate.sudo("rm /tmp/vagrant-ifcfg-p7p#{network[:interface]}") + machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-p7p#{network[:interface]}") + machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-p7p#{network[:interface]} > /tmp/vagrant-ifcfg-p7p#{network[:interface]}") + machine.communicate.sudo("cat /tmp/vagrant-ifcfg-p7p#{network[:interface]} > #{network_scripts_dir}/ifcfg-p7p#{network[:interface]}") + machine.communicate.sudo("rm /tmp/vagrant-ifcfg-p7p#{network[:interface]}") # Render and upload the network entry file to a deterministic # temporary location. @@ -35,17 +35,17 @@ module VagrantPlugins temp.write(entry) temp.close - vm.communicate.upload(temp.path, "/tmp/vagrant-network-entry_#{network[:interface]}") + 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 p7p (the NAT interface) so # SSH never dies. interfaces.each do |interface| - vm.communicate.sudo("/sbin/ifdown p7p#{interface} 2> /dev/null", :error_check => false) - vm.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-p7p#{interface}") - vm.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}") - vm.communicate.sudo("/sbin/ifup p7p#{interface} 2> /dev/null") + machine.communicate.sudo("/sbin/ifdown p7p#{interface} 2> /dev/null", :error_check => false) + machine.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-p7p#{interface}") + machine.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}") + machine.communicate.sudo("/sbin/ifup p7p#{interface} 2> /dev/null") end end end diff --git a/plugins/guests/fedora/plugin.rb b/plugins/guests/fedora/plugin.rb index b002c1963..fdfdab807 100644 --- a/plugins/guests/fedora/plugin.rb +++ b/plugins/guests/fedora/plugin.rb @@ -6,16 +6,11 @@ module VagrantPlugins name "Fedora guest" description "Fedora guest support." - guest("fedora", "linux") do + guest("fedora", "redhat") do require File.expand_path("../guest", __FILE__) Guest end - guest_capability("fedora", "change_host_name") do - require_relative "cap/change_host_name" - Cap::ChangeHostName - end - guest_capability("fedora", "configure_networks") do require_relative "cap/configure_networks" Cap::ConfigureNetworks diff --git a/plugins/guests/gentoo/cap/change_host_name.rb b/plugins/guests/gentoo/cap/change_host_name.rb new file mode 100644 index 000000000..4c0f50d8f --- /dev/null +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -0,0 +1,17 @@ +module VagrantPlugins + module GuestFreeBSD + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + machine.communicate.tap do |comm| + if !comm.test("sudo hostname --fqdn | grep '#{name}'") + comm.sudo("echo 'hostname=#{name.split('.')[0]}' > /etc/conf.d/hostname") + comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") + comm.sudo("hostname #{name.split('.')[0]}") + end + end + end + end + end + end +end diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb new file mode 100644 index 000000000..7234f36d0 --- /dev/null +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -0,0 +1,43 @@ +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestFreeBSD + module Cap + class ChangeHostName + extend Vagrant::Util + + def self.configure_networks(machine, networks) + machine.communicate.tap do |comm| + # Remove any previous host only network additions to the interface file + comm.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net > /tmp/vagrant-network-interfaces") + comm.sudo("cat /tmp/vagrant-network-interfaces > /etc/conf.d/net") + comm.sudo("rm /tmp/vagrant-network-interfaces") + + # Configure each network interface + networks.each do |network| + entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", + :options => network) + + # Upload the entry to a temporary location + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(entry) + temp.close + + comm.upload(temp.path, "/tmp/vagrant-network-entry") + + # Configure the interface + comm.sudo("ln -fs /etc/init.d/net.lo /etc/init.d/net.eth#{network[:interface]}") + comm.sudo("/etc/init.d/net.eth#{network[:interface]} stop 2> /dev/null") + comm.sudo("cat /tmp/vagrant-network-entry >> /etc/conf.d/net") + comm.sudo("rm /tmp/vagrant-network-entry") + comm.sudo("/etc/init.d/net.eth#{network[:interface]} start") + end + end + end + end + end + end +end diff --git a/plugins/guests/gentoo/guest.rb b/plugins/guests/gentoo/guest.rb index 5aaacd573..33af4ae5d 100644 --- a/plugins/guests/gentoo/guest.rb +++ b/plugins/guests/gentoo/guest.rb @@ -1,55 +1,9 @@ -require 'tempfile' - -require "vagrant" -require 'vagrant/util/template_renderer' - -require Vagrant.source_root.join("plugins/guests/linux/guest") - module VagrantPlugins module GuestGentoo - class Guest < VagrantPlugins::GuestLinux::Guest - # Make the TemplateRenderer top-level - include Vagrant::Util - + class Guest < Vagrant.plugin("2", :guest) def detect?(machine) machine.communicate.test("cat /etc/gentoo-release") end - - def configure_networks(networks) - # Remove any previous host only network additions to the interface file - vm.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net > /tmp/vagrant-network-interfaces") - vm.communicate.sudo("cat /tmp/vagrant-network-interfaces > /etc/conf.d/net") - vm.communicate.sudo("rm /tmp/vagrant-network-interfaces") - - # Configure each network interface - networks.each do |network| - entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", - :options => network) - - # Upload the entry to a temporary location - temp = Tempfile.new("vagrant") - temp.binmode - temp.write(entry) - temp.close - - vm.communicate.upload(temp.path, "/tmp/vagrant-network-entry") - - # Configure the interface - vm.communicate.sudo("ln -fs /etc/init.d/net.lo /etc/init.d/net.eth#{network[:interface]}") - vm.communicate.sudo("/etc/init.d/net.eth#{network[:interface]} stop 2> /dev/null") - vm.communicate.sudo("cat /tmp/vagrant-network-entry >> /etc/conf.d/net") - vm.communicate.sudo("rm /tmp/vagrant-network-entry") - vm.communicate.sudo("/etc/init.d/net.eth#{network[:interface]} start") - end - end - - def change_host_name(name) - if !vm.communicate.test("sudo hostname --fqdn | grep '#{name}'") - vm.communicate.sudo("echo 'hostname=#{name.split('.')[0]}' > /etc/conf.d/hostname") - vm.communicate.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") - vm.communicate.sudo("hostname #{name.split('.')[0]}") - end - end end end end diff --git a/plugins/guests/gentoo/plugin.rb b/plugins/guests/gentoo/plugin.rb index b37b39565..2054f81cd 100644 --- a/plugins/guests/gentoo/plugin.rb +++ b/plugins/guests/gentoo/plugin.rb @@ -10,6 +10,16 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("gentoo", "change_host_name") do + require_relative "cap/change_host_name" + Cap::ChangeHostName + end + + guest_capability("gentoo", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end end end end diff --git a/plugins/guests/openbsd/cap/halt.rb b/plugins/guests/openbsd/cap/halt.rb new file mode 100644 index 000000000..3b44d9b0c --- /dev/null +++ b/plugins/guests/openbsd/cap/halt.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module GuestOpenBSD + module Cap + class Halt + def self.halt(machine) + begin + machine.communicate.sudo("shutdown -p -h now") + rescue IOError + # Do nothing, because it probably means the machine shut down + # and SSH connection was lost. + end + end + end + end + end +end diff --git a/plugins/guests/openbsd/guest.rb b/plugins/guests/openbsd/guest.rb index 8a4e6408d..180c85427 100644 --- a/plugins/guests/openbsd/guest.rb +++ b/plugins/guests/openbsd/guest.rb @@ -1,18 +1,12 @@ require "vagrant" -require Vagrant.source_root.join("plugins/guests/linux/guest") - module VagrantPlugins module GuestOpenBSD - class Guest < VagrantPlugins::GuestLinux::Guest + class Guest < Vagrant.plugin("2", :guest) def detect?(machine) # TODO: OpenBSD detection false end - - def halt - vm.communicate.sudo("shutdown -p -h now") - end end end end diff --git a/plugins/guests/openbsd/plugin.rb b/plugins/guests/openbsd/plugin.rb index dd6f38a76..b3e3312a8 100644 --- a/plugins/guests/openbsd/plugin.rb +++ b/plugins/guests/openbsd/plugin.rb @@ -10,6 +10,11 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("openbsd", "halt") do + require_relative "cap/halt" + Cap::Halt + end end end end diff --git a/plugins/guests/pld/cap/network_scripts_dir.rb b/plugins/guests/pld/cap/network_scripts_dir.rb new file mode 100644 index 000000000..3a6fbfb43 --- /dev/null +++ b/plugins/guests/pld/cap/network_scripts_dir.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module GuestPld + module Cap + class NetworkScriptsDir + def self.network_scripts_dir(machine) + "/etc/sysconfig/interfaces" + end + end + end + end +end diff --git a/plugins/guests/pld/guest.rb b/plugins/guests/pld/guest.rb index a2d7ad881..1d65ce044 100644 --- a/plugins/guests/pld/guest.rb +++ b/plugins/guests/pld/guest.rb @@ -1,17 +1,11 @@ require "vagrant" -require Vagrant.source_root.join("plugins/guests/redhat/guest") - module VagrantPlugins module GuestPld - class Guest < VagrantPlugins::GuestRedHat::Guest + class Guest < Vagrant.plugin("2", :guest) def detect?(machine) machine.communicate.test("cat /etc/pld-release") end - - def network_scripts_dir - '/etc/sysconfig/interfaces/' - end end end end diff --git a/plugins/guests/pld/plugin.rb b/plugins/guests/pld/plugin.rb index bd457b8c1..d02d76a02 100644 --- a/plugins/guests/pld/plugin.rb +++ b/plugins/guests/pld/plugin.rb @@ -10,6 +10,11 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("pld", "network_scripts_dir") do + require_relative "cap/network_scripts_dir" + Cap::NetworkScriptsDir + end end end end diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb similarity index 96% rename from plugins/guests/fedora/cap/change_host_name.rb rename to plugins/guests/redhat/cap/change_host_name.rb index 6655898d0..42d1d7d9c 100644 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ b/plugins/guests/redhat/cap/change_host_name.rb @@ -1,5 +1,5 @@ module VagrantPlugins - module GuestFedora + module GuestRedHat module Cap class ChangeHostName def self.change_host_name(machine, name) diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb new file mode 100644 index 000000000..00ab47766 --- /dev/null +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -0,0 +1,55 @@ +require "set" +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestRedHat + module Cap + class ConfigureNetworks + extend 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/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| + 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("rm /tmp/vagrant-network-entry_#{interface}") + machine.communicate.sudo("/sbin/ifup eth#{interface} 2> /dev/null") + end + end + end + end + end +end diff --git a/plugins/guests/redhat/cap/network_scripts_dir.rb b/plugins/guests/redhat/cap/network_scripts_dir.rb new file mode 100644 index 000000000..5b2a26e04 --- /dev/null +++ b/plugins/guests/redhat/cap/network_scripts_dir.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module GuestRedHat + module Cap + class NetworkScriptsDir + def self.network_scripts_dir(machine) + "/etc/sysconfig/network-scripts" + end + end + end + end +end diff --git a/plugins/guests/redhat/guest.rb b/plugins/guests/redhat/guest.rb index 0ac990ea0..197286200 100644 --- a/plugins/guests/redhat/guest.rb +++ b/plugins/guests/redhat/guest.rb @@ -1,78 +1,11 @@ -require 'set' -require 'tempfile' - require "vagrant" -require 'vagrant/util/template_renderer' - -require Vagrant.source_root.join("plugins/guests/linux/guest") module VagrantPlugins module GuestRedHat - class Guest < VagrantPlugins::GuestLinux::Guest - # Make the TemplateRenderer top-level - include Vagrant::Util - + class Guest < Vagrant.plugin("2", :guest) def detect?(machine) machine.communicate.test("cat /etc/redhat-release") end - - 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 - networks.each do |network| - interfaces.add(network[:interface]) - - # Remove any previous vagrant configuration in this network interface's - # configuration files. - vm.communicate.sudo("touch #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - vm.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-eth#{network[:interface]} > /tmp/vagrant-ifcfg-eth#{network[:interface]}") - vm.communicate.sudo("cat /tmp/vagrant-ifcfg-eth#{network[:interface]} > #{network_scripts_dir}/ifcfg-eth#{network[:interface]}") - vm.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 - - vm.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| - vm.communicate.sudo("/sbin/ifdown eth#{interface} 2> /dev/null", :error_check => false) - vm.communicate.sudo("cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-eth#{interface}") - vm.communicate.sudo("rm /tmp/vagrant-network-entry_#{interface}") - vm.communicate.sudo("/sbin/ifup eth#{interface} 2> /dev/null") - end - end - - # The path to the directory with the network configuration scripts. - # This is pulled out into its own directory since there are other - # operating systems (SuSE) which behave similarly but with a different - # path to the network scripts. - def network_scripts_dir - '/etc/sysconfig/network-scripts' - end - - def change_host_name(name) - vm.communicate.tap do |comm| - # Only do this if the hostname is not already set - if !comm.test("sudo hostname | grep '#{name}'") - comm.sudo("sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network") - comm.sudo("hostname #{name}") - comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") - end - end - end end end end diff --git a/plugins/guests/redhat/plugin.rb b/plugins/guests/redhat/plugin.rb index dd80ef78f..60ca17af6 100644 --- a/plugins/guests/redhat/plugin.rb +++ b/plugins/guests/redhat/plugin.rb @@ -10,6 +10,21 @@ module VagrantPlugins require File.expand_path("../guest", __FILE__) Guest end + + guest_capability("redhat", "change_host_name") do + require_relative "cap/change_host_name" + Cap::ChangeHostName + end + + guest_capability("redhat", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end + + guest_capability("redhat", "network_scripts_dir") do + require_relative "cap/network_scripts_dir" + Cap::NetworkScriptsDir + end end end end