diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb new file mode 100644 index 000000000..6655898d0 --- /dev/null +++ b/plugins/guests/fedora/cap/change_host_name.rb @@ -0,0 +1,18 @@ +module VagrantPlugins + module GuestFedora + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + machine.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 +end diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb new file mode 100644 index 000000000..546d5c516 --- /dev/null +++ b/plugins/guests/fedora/cap/configure_networks.rb @@ -0,0 +1,54 @@ +require "set" +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestFedora + module Cap + class ConfigureNetworks + 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. + 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]}") + + # Render and upload the network entry file to a deterministic + # temporary location. + entry = TemplateRenderer.render("guests/fedora/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 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") + end + end + end + end + end +end diff --git a/plugins/guests/fedora/cap/network_scripts_dir.rb b/plugins/guests/fedora/cap/network_scripts_dir.rb new file mode 100644 index 000000000..3ce0e43c3 --- /dev/null +++ b/plugins/guests/fedora/cap/network_scripts_dir.rb @@ -0,0 +1,15 @@ +module VagrantPlugins + module GuestFedora + module Cap + class NetworkScriptsDir + # 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 self.network_scripts_dir(machine) + "/etc/sysconfig/network-scripts" + end + end + end + end +end diff --git a/plugins/guests/fedora/guest.rb b/plugins/guests/fedora/guest.rb index f32d8f3d2..1127dd28e 100644 --- a/plugins/guests/fedora/guest.rb +++ b/plugins/guests/fedora/guest.rb @@ -1,75 +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 GuestFedora - 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("grep 'Fedora release 1[678]' /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-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]}") - - # Render and upload the network entry file to a deterministic - # temporary location. - entry = TemplateRenderer.render("guests/fedora/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 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") - 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) - # Only do this if the hostname is not already set - if !vm.communicate.test("sudo hostname | grep '#{name}'") - vm.communicate.sudo("sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network") - vm.communicate.sudo("hostname #{name}") - vm.communicate.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts") - end - end end end end diff --git a/plugins/guests/fedora/plugin.rb b/plugins/guests/fedora/plugin.rb index 73bbcb911..b002c1963 100644 --- a/plugins/guests/fedora/plugin.rb +++ b/plugins/guests/fedora/plugin.rb @@ -10,6 +10,21 @@ module VagrantPlugins 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 + end + + guest_capability("fedora", "network_scripts_dir") do + require_relative "cap/network_scripts_dir" + Cap::NetworkScriptsDir + end end end end