From 3eebff5bf3b588b7e016f8d64b1251515d43e617 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:31:17 -0700 Subject: [PATCH 01/39] guests/funtoo: Behave like a gentoo guest --- plugins/guests/funtoo/cap/change_host_name.rb | 17 ------------- plugins/guests/funtoo/plugin.rb | 7 +----- plugins/guests/gentoo/cap/change_host_name.rb | 25 ++++++++++++++----- templates/guests/funtoo/network_static6.erb | 9 +++++++ 4 files changed, 29 insertions(+), 29 deletions(-) delete mode 100644 plugins/guests/funtoo/cap/change_host_name.rb create mode 100644 templates/guests/funtoo/network_static6.erb diff --git a/plugins/guests/funtoo/cap/change_host_name.rb b/plugins/guests/funtoo/cap/change_host_name.rb deleted file mode 100644 index 298ff5b12..000000000 --- a/plugins/guests/funtoo/cap/change_host_name.rb +++ /dev/null @@ -1,17 +0,0 @@ -module VagrantPlugins - module GuestFuntoo - 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/funtoo/plugin.rb b/plugins/guests/funtoo/plugin.rb index 59f76da98..2f768d669 100644 --- a/plugins/guests/funtoo/plugin.rb +++ b/plugins/guests/funtoo/plugin.rb @@ -6,16 +6,11 @@ module VagrantPlugins name "Funtoo guest" description "Funtoo guest support." - guest(:funtoo, :linux) do + guest(:funtoo, :gentoo) do require_relative "guest" Guest end - guest_capability(:funtoo, :change_host_name) do - require_relative "cap/change_host_name" - Cap::ChangeHostName - end - guest_capability(:funtoo, :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 index a8112b7d5..c36274e5c 100644 --- a/plugins/guests/gentoo/cap/change_host_name.rb +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -3,12 +3,25 @@ module VagrantPlugins 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 + comm = machine.communicate + + if !comm.test("hostname -f | grep '^#{name}$'") + basename = name.split(".", 2)[0] + comm.sudo <<-EOH.gsub(/^ {14}/, "") + # Set the hostname + hostname '#{basename}' + echo "hostname=#{basename}" > /etc/conf.d/hostname + + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' /etc/hosts + sed -i'' -e '/^$/d' /etc/hosts + + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts + mv /tmp/tmp-hosts /etc/hosts + } + EOH end end end diff --git a/templates/guests/funtoo/network_static6.erb b/templates/guests/funtoo/network_static6.erb new file mode 100644 index 000000000..d74408959 --- /dev/null +++ b/templates/guests/funtoo/network_static6.erb @@ -0,0 +1,9 @@ +#VAGRANT-BEGIN +template='interface' +ipaddr='<%= options[:ip] %>/<%= options[:netmask] %>' +<% [:gateway, :nameservers, :domain, :route, :gateway6, :route6, :mtu].each do |key| %> +<% if options[key] %> +<%= key %>='<%= options[key] %>' +<% end %> +<% end %> +#VAGRANT-END From 10b12ce393fa3aef06f027b4532d6a61c18a94bb Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:31:36 -0700 Subject: [PATCH 02/39] guests/arch: Search for FQDN hostname --- plugins/guests/arch/cap/change_host_name.rb | 20 ++++++++++---------- templates/guests/arch/network_static6.erb | 8 ++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 templates/guests/arch/network_static6.erb diff --git a/plugins/guests/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index fc3ec4e3e..f349d444b 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -5,19 +5,19 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'") basename = name.split(".", 2)[0] - comm.sudo <<-EOH -hostnamectl set-hostname '#{name}' + comm.sudo <<-EOH.gsub(/^ {14}/, "") + hostnamectl set-hostname '#{basename}' -# Remove comments and blank lines from /etc/hosts -sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts -# Prepend ourselves to /etc/hosts -grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts -} -EOH + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts + } + EOH end end end diff --git a/templates/guests/arch/network_static6.erb b/templates/guests/arch/network_static6.erb new file mode 100644 index 000000000..40e61c596 --- /dev/null +++ b/templates/guests/arch/network_static6.erb @@ -0,0 +1,8 @@ +Connection=ethernet +Description='A basic IPv6 ethernet connection' +Interface=<%= options[:device] %> +IP6=static +Address=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] %> +Gateway='<%= options[:gateway] %>' +<% end %> From bbcddb64990b037b7a3c8c18d3f65d3b2071679c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:31:44 -0700 Subject: [PATCH 03/39] guests/atomic: Search for FQDN hostname --- plugins/guests/atomic/cap/change_host_name.rb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index 70e936321..9f4b7c44f 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -5,19 +5,19 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'") basename = name.split(".", 2)[0] - comm.sudo <<-EOH -hostnamectl set-hostname '#{name}' + comm.sudo <<-EOH.gsub(/^ {14}/, "") + hostnamectl set-hostname '#{basename}' -# Remove comments and blank lines from /etc/hosts -sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts -# Prepend ourselves to /etc/hosts -grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts -} -EOH + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts + } + EOH end end end From 5a372a9942f8c3e40a4987d54679bbd439727b82 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:31:57 -0700 Subject: [PATCH 04/39] guests/bsd: Do not use -h to shutdown --- plugins/guests/bsd/cap/halt.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/guests/bsd/cap/halt.rb b/plugins/guests/bsd/cap/halt.rb index 004bf222b..f13379839 100644 --- a/plugins/guests/bsd/cap/halt.rb +++ b/plugins/guests/bsd/cap/halt.rb @@ -4,7 +4,7 @@ module VagrantPlugins class Halt def self.halt(machine) begin - machine.communicate.sudo("/sbin/shutdown -p -h now", shell: "sh") + machine.communicate.sudo("/sbin/shutdown -p now", shell: "sh") rescue IOError # Do nothing, because it probably means the machine shut down # and SSH connection was lost. From 87d2b7fec8c14706d23f64045f2f23332a0b2018 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:32:33 -0700 Subject: [PATCH 05/39] guests/debian: Set hostname to short value Refs GH-7488 --- plugins/guests/debian/cap/change_host_name.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index a80d05516..fa17ae2b0 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -9,11 +9,11 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'") basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname - echo '#{name}' > /etc/hostname + echo '#{basename}' > /etc/hostname hostname -F /etc/hostname # Remove comments and blank lines from /etc/hosts From 57774601d9e36cdc3fee27840b9eff85fe9721bc Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:32:52 -0700 Subject: [PATCH 06/39] guests/fedora: Fix indentation on configure hostname --- plugins/guests/fedora/cap/change_host_name.rb | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb index 82e1da99a..f8ceabd11 100644 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ b/plugins/guests/fedora/cap/change_host_name.rb @@ -7,20 +7,20 @@ module VagrantPlugins if !comm.test("hostname | grep -w '#{name}'") basename = name.split(".", 2)[0] - comm.sudo <<-EOH -echo '#{name}' > /etc/hostname -hostname -F /etc/hostname -hostnamectl set-hostname --static '#{name}' -hostnamectl set-hostname --transient '#{name}' + comm.sudo <<-EOH.gsub(/^ {14}/, "") + echo '#{basename}' > /etc/hostname + hostname -F /etc/hostname + hostnamectl set-hostname --static '#{basename}' + hostnamectl set-hostname --transient '#{basename}' -# Remove comments and blank lines from /etc/hosts -sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts -# Prepend ourselves to /etc/hosts -grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts -} -EOH + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts + } + EOH end end end From ea9b28f9b6239176dfc4a472355dee9ef5638dab Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:33:39 -0700 Subject: [PATCH 07/39] guests/freebsd: Check FQDN for hostname --- plugins/guests/freebsd/cap/change_host_name.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index fc194d6d9..09b517e9b 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -6,7 +6,7 @@ module VagrantPlugins options = { shell: "sh" } comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", options) + if !comm.test("hostname -f | grep '^#{name}$'", options) basename = name.split(".", 2)[0] command = <<-EOH.gsub(/^ {14}/, '') # Set the hostname From caafb936f00d6deb83d392b4c20203f6a61d1a47 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:33:59 -0700 Subject: [PATCH 08/39] guests/gentoo: Use predictable network interface names --- .../guests/gentoo/cap/configure_networks.rb | 61 +++++++++++-------- templates/guests/gentoo/network_dhcp.erb | 2 +- templates/guests/gentoo/network_static.erb | 8 +-- templates/guests/gentoo/network_static6.erb | 7 +++ 4 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 templates/guests/gentoo/network_static6.erb diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index 1d4a26b56..ab27a58e3 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -9,34 +9,47 @@ module VagrantPlugins include 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 -f /tmp/vagrant-network-interfaces") + comm = machine.communicate - # Configure each network interface - networks.each do |network| - entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", - options: network) + commands = [] + interfaces = [] - # Upload the entry to a temporary location - Tempfile.open("vagrant-gentoo-configure-networks") do |f| - f.binmode - f.write(entry) - f.fsync - f.close - comm.upload(f.path, "/tmp/vagrant-network-entry") - end + # Remove any previous network additions to the configuration file. + commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net" - # 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") - comm.sudo("cat /tmp/vagrant-network-entry >> /etc/conf.d/net") - comm.sudo("rm -f /tmp/vagrant-network-entry") - comm.sudo("/etc/init.d/net.eth#{network[:interface]} start") - end + comm.sudo("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'") do |_, stdout| + interfaces = stdout.split("\n") end + + networks.each_with_index do |network, i| + network[:device] = interfaces[network[:interface]] + + entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}", + options: network, + ) + + remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" + + Tempfile.open("vagrant-gentoo-configure-networks") do |f| + f.binmode + f.write(entry) + f.fsync + f.close + comm.upload(f.path, remote_path) + end + + commands << <<-EOH.gsub(/^ {14}/, '') + ln -sf /etc/init.d/net.lo /etc/init.d/net.#{network[:device]} + /etc/init.d/net.#{network[:device]} stop || true + + cat '#{remote_path}' >> /etc/conf.d/net + rm -f '#{remote_path}' + + /etc/init.d/net.#{network[:device]} start + EOH + end + + comm.sudo(commands.join("\n")) end end end diff --git a/templates/guests/gentoo/network_dhcp.erb b/templates/guests/gentoo/network_dhcp.erb index df1092987..18cea064e 100644 --- a/templates/guests/gentoo/network_dhcp.erb +++ b/templates/guests/gentoo/network_dhcp.erb @@ -1,4 +1,4 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -config_eth<%= options[:interface] %>="dhcp" +config_<%= options[:device] %>="dhcp" #VAGRANT-END diff --git a/templates/guests/gentoo/network_static.erb b/templates/guests/gentoo/network_static.erb index fe6c77194..ed68c911d 100644 --- a/templates/guests/gentoo/network_static.erb +++ b/templates/guests/gentoo/network_static.erb @@ -1,7 +1,7 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -config_eth<%= options[:interface] %>=("<%= options[:ip] %> netmask <%= options[:netmask] %>") -<% if options[:gateway] %> -gateways_eth<%= options[:interface] %>="<%= options[:gateway] %>" -<% end %> +config_<%= options[:device] %>=("<%= options[:ip] %> netmask <%= options[:netmask] %>") +<% if options[:gateway] -%> +gateways_<%= options[:device] %>="<%= options[:gateway] %>" +<% end -%> #VAGRANT-END diff --git a/templates/guests/gentoo/network_static6.erb b/templates/guests/gentoo/network_static6.erb new file mode 100644 index 000000000..6467a57cd --- /dev/null +++ b/templates/guests/gentoo/network_static6.erb @@ -0,0 +1,7 @@ +#VAGRANT-BEGIN +# The contents below are automatically generated by Vagrant. Do not modify. +config_<%= options[:device] %>="<%= options[:ip] %>/<%= options[:netmask] %>" +<% if options[:gateway] -%> +gateways_<%= options[:device] %>="<%= options[:gateway] %>" +<% end -%> +#VAGRANT-END From 4664930e061975f33acfdef84fd978a9a704395b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:34:37 -0700 Subject: [PATCH 09/39] guests/ubuntu: Use short hostname Fixes GH-7488 --- plugins/guests/ubuntu/cap/change_host_name.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb index bbcada3e7..64dcd4151 100644 --- a/plugins/guests/ubuntu/cap/change_host_name.rb +++ b/plugins/guests/ubuntu/cap/change_host_name.rb @@ -9,11 +9,11 @@ module VagrantPlugins basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname - echo '#{name}' > /etc/hostname + echo '#{basename}' > /etc/hostname hostname -F /etc/hostname if command -v hostnamectl; then - hostnamectl set-hostname '#{name}' + hostnamectl set-hostname '#{basename}' fi # Remove comments and blank lines from /etc/hosts From b621cc44fb63d93143915d2e744556ab36d80b17 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:37:01 -0700 Subject: [PATCH 10/39] guests/rhel: Switch to predicable network interface names --- .../guests/fedora/cap/configure_networks.rb | 135 +++++------------- templates/guests/freebsd/network_static6.erb | 6 + templates/guests/redhat/network_dhcp.erb | 2 +- templates/guests/redhat/network_static.erb | 2 +- templates/guests/redhat/network_static6.erb | 10 ++ 5 files changed, 50 insertions(+), 105 deletions(-) create mode 100644 templates/guests/freebsd/network_static6.erb create mode 100644 templates/guests/redhat/network_static6.erb diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb index e4a7e1f19..9305b34b0 100644 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ b/plugins/guests/fedora/cap/configure_networks.rb @@ -1,4 +1,3 @@ -require "set" require "tempfile" require_relative "../../../../lib/vagrant/util/retryable" @@ -12,122 +11,52 @@ module VagrantPlugins include Vagrant::Util def self.configure_networks(machine, networks) - network_scripts_dir = machine.guest.capability("network_scripts_dir") + comm = machine.communicate - virtual = false - interface_names = Array.new - interface_names_by_slot = Array.new - machine.communicate.sudo("/usr/sbin/biosdevname &>/dev/null; echo $?") do |_, result| - # The above command returns: - # - '4' if /usr/sbin/biosdevname detects it is running in a virtual machine - # - '127' if /usr/sbin/biosdevname doesn't exist - virtual = true if ['4', '127'].include? result.chomp + network_scripts_dir = machine.guest.capability(:network_scripts_dir) + + interfaces = [] + commands = [] + + comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| + interfaces = stdout.split("\n") end - if virtual - machine.communicate.sudo("ls -v /sys/class/net | egrep -v lo\\|docker") do |_, result| - interface_names = result.split("\n") - end + networks.each.with_index do |network, i| + network[:device] = interfaces[network[:interface]] - interface_names_by_slot = networks.map do |network| - "#{interface_names[network[:interface]]}" - end - else - machine.communicate.sudo("/usr/sbin/biosdevname -d | grep Kernel | cut -f2 -d: | sed -e 's/ //;'") do |_, result| - interface_names = result.split("\n") - end - - interface_name_pairs = Array.new - interface_names.each do |interface_name| - machine.communicate.sudo("/usr/sbin/biosdevname --policy=all_ethN -i #{interface_name}") do |_, result| - interface_name_pairs.push([interface_name, result.gsub("\n", "")]) - end - end - - setting_interface_names = networks.map do |network| - "eth#{network[:interface]}" - end - - interface_names_by_slot = interface_names.dup - interface_name_pairs.each do |interface_name, previous_interface_name| - if setting_interface_names.index(previous_interface_name) == nil - interface_names_by_slot.delete(interface_name) - end - end - end - - # Read interface MAC addresses for later matching - mac_addresses = Array.new(interface_names.length) - interface_names.each_with_index do |ifname, index| - machine.communicate.sudo("cat /sys/class/net/#{ifname}/address") do |_, result| - mac_addresses[index] = result.strip - end - end - - # 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| - interface = nil - if network[:mac_address] - found_idx = mac_addresses.find_index(network[:mac_address]) - # Ignore network if requested MAC address could not be found - next if found_idx.nil? - interface = interface_names[found_idx] - else - ifname_by_slot = interface_names_by_slot[network[:interface]-1] - # Don't overwrite if interface was already matched via MAC address - next if interfaces.include?(ifname_by_slot) - interface = ifname_by_slot - end - - interfaces.add(interface) - network[:device] = interface - - # Remove any previous vagrant configuration in this network - # interface's configuration files. - machine.communicate.sudo("touch #{network_scripts_dir}/ifcfg-#{interface}") - machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' #{network_scripts_dir}/ifcfg-#{interface} > /tmp/vagrant-ifcfg-#{interface}") - machine.communicate.sudo("cat /tmp/vagrant-ifcfg-#{interface} > #{network_scripts_dir}/ifcfg-#{interface}") - machine.communicate.sudo("rm -f /tmp/vagrant-ifcfg-#{interface}") - - # Render and upload the network entry file to a deterministic - # temporary location. + # Render a new configuration entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}", - options: network) + options: network, + ) + # Upload the new configuration + remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}" Tempfile.open("vagrant-fedora-configure-networks") do |f| f.binmode f.write(entry) f.fsync f.close - machine.communicate.upload(f.path, "/tmp/vagrant-network-entry_#{interface}") + machine.communicate.upload(f.path, remote_path) end + + # Add the new interface and bring it back up + final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}" + commands << <<-EOH.gsub(/^ {14}/, '') + # Down the interface before munging the config file. This might + # fail if the interface is not actually set up yet so ignore + # errors. + /sbin/ifdown '#{network[:device]}' || true + + # Move new config into place + mv '#{remote_path}' '#{final_path}' + + # Bring the interface up + ARPCHECK=no /sbin/ifup '#{network[:device]}' + EOH 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| - retryable(on: Vagrant::Errors::VagrantError, tries: 3, sleep: 2) do - machine.communicate.sudo(<<-SCRIPT, error_check: true) -cat /tmp/vagrant-network-entry_#{interface} >> #{network_scripts_dir}/ifcfg-#{interface} - -if command -v nmcli &>/dev/null; then - if command -v systemctl &>/dev/null && systemctl -q is-enabled NetworkManager &>/dev/null; then - nmcli c reload #{interface} - elif command -v service &>/dev/null && service NetworkManager status &>/dev/null; then - nmcli c reload #{interface} - fi -fi - -/sbin/ifdown #{interface} -/sbin/ifup #{interface} - -rm -f /tmp/vagrant-network-entry_#{interface} -SCRIPT - end - end + comm.sudo(commands.join("\n")) end end end diff --git a/templates/guests/freebsd/network_static6.erb b/templates/guests/freebsd/network_static6.erb new file mode 100644 index 000000000..094584da6 --- /dev/null +++ b/templates/guests/freebsd/network_static6.erb @@ -0,0 +1,6 @@ +#VAGRANT-BEGIN +ifconfig_<%= options[:device] %>_ipv6="inet6 <%= options[:ip] %> prefixlen <%= options[:netmask] %>" +<% if options[:gateway] %> +ipv6_default_router="<%= options[:gateway] %>" +<% end %> +#VAGRANT-END diff --git a/templates/guests/redhat/network_dhcp.erb b/templates/guests/redhat/network_dhcp.erb index 8bbaa62e4..b15250cc2 100644 --- a/templates/guests/redhat/network_dhcp.erb +++ b/templates/guests/redhat/network_dhcp.erb @@ -2,5 +2,5 @@ # The contents below are automatically generated by Vagrant. Do not modify. BOOTPROTO=dhcp ONBOOT=yes -DEVICE=eth<%= options[:interface] %> +DEVICE=<%= options[:device] %> #VAGRANT-END diff --git a/templates/guests/redhat/network_static.erb b/templates/guests/redhat/network_static.erb index 53ef59571..b144c6cf4 100644 --- a/templates/guests/redhat/network_static.erb +++ b/templates/guests/redhat/network_static.erb @@ -5,7 +5,7 @@ BOOTPROTO=none ONBOOT=yes IPADDR=<%= options[:ip] %> NETMASK=<%= options[:netmask] %> -DEVICE=eth<%= options[:interface] %> +DEVICE=<%= options[:device] %> <% if options[:gateway] %> GATEWAY=<%= options[:gateway] %> <% end %> diff --git a/templates/guests/redhat/network_static6.erb b/templates/guests/redhat/network_static6.erb new file mode 100644 index 000000000..288da4396 --- /dev/null +++ b/templates/guests/redhat/network_static6.erb @@ -0,0 +1,10 @@ +#VAGRANT-BEGIN +# The contents below are automatically generated by Vagrant. Do not modify. +auto <%= options[:device] %> +iface <%= options[:device] %> inet6 static + address <%= options[:ip] %> + netmask <%= options[:netmask] %> +<% if options[:gateway] %> + gateway <%= options[:gateway] %> +<% end %> +#VAGRANT-END From 66cbe7b41efa8378cc5bc2f4942705fcb4207553 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 18:35:19 -0700 Subject: [PATCH 11/39] Fix failing tests --- .../guests/arch/cap/change_host_name_test.rb | 12 ++++++------ .../guests/arch/cap/configure_networks_test.rb | 7 ++++--- .../guests/atomic/cap/change_host_name_test.rb | 17 +++++++++-------- test/unit/plugins/guests/bsd/cap/halt_test.rb | 4 ++-- .../guests/debian/cap/change_host_name_test.rb | 4 ++-- .../guests/freebsd/cap/change_host_name_test.rb | 4 ++-- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb index e296caa0a..99a1ef852 100644 --- a/test/unit/plugins/guests/arch/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/arch/cap/change_host_name_test.rb @@ -20,18 +20,18 @@ describe "VagrantPlugins::GuestArch::Cap::ChangeHostName" do end describe ".change_host_name" do - let(:hostname) { "banana-rama.example.com" } + let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - described_class.change_host_name(machine, hostname) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/) + described_class.change_host_name(machine, name) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname 'banana-rama'/) end it "does not change the hostname if already set" do - comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0) - described_class.change_host_name(machine, hostname) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) + described_class.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end end diff --git a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb index 24c89a2ed..82c50329d 100644 --- a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb @@ -1,11 +1,10 @@ require_relative "../../../../base" describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do - let(:described_class) do + let(:caps) do VagrantPlugins::GuestArch::Plugin .components .guest_capabilities[:arch] - .get(:configure_networks) end let(:machine) { double("machine") } @@ -22,6 +21,8 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do end describe ".configure_networks" do + let(:cap) { caps.get(:configure_networks) } + let(:network_1) do { interface: 0, @@ -40,7 +41,7 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - described_class.configure_networks(machine, [network_1, network_2]) + cap.configure_networks(machine, [network_1, network_2]) expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth1'/) expect(comm.received_commands[1]).to match(/ip link set 'eth1' down/) expect(comm.received_commands[1]).to match(/netctl restart 'eth1'/) diff --git a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb index 72d0dc75b..2aba69943 100644 --- a/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/atomic/cap/change_host_name_test.rb @@ -1,11 +1,10 @@ require_relative "../../../../base" describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do - let(:described_class) do + let(:caps) do VagrantPlugins::GuestAtomic::Plugin .components .guest_capabilities[:atomic] - .get(:change_host_name) end let(:machine) { double("machine") } @@ -20,18 +19,20 @@ describe "VagrantPlugins::GuestAtomic::Cap::ChangeHostName" do end describe ".change_host_name" do - let(:hostname) { "banana-rama.example.com" } + let(:cap) { caps.get(:change_host_name) } + + let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) - described_class.change_host_name(machine, hostname) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/) + cap.change_host_name(machine, name) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname 'banana-rama'/) end it "does not change the hostname if already set" do - comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0) - described_class.change_host_name(machine, hostname) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) + cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end end diff --git a/test/unit/plugins/guests/bsd/cap/halt_test.rb b/test/unit/plugins/guests/bsd/cap/halt_test.rb index 3ec6ddbc0..7ab3322b6 100644 --- a/test/unit/plugins/guests/bsd/cap/halt_test.rb +++ b/test/unit/plugins/guests/bsd/cap/halt_test.rb @@ -22,12 +22,12 @@ describe "VagrantPlugins::GuestBSD::Cap::Halt" do let(:cap) { caps.get(:halt) } it "runs the shutdown command" do - comm.expect_command("/sbin/shutdown -p -h now") + comm.expect_command("/sbin/shutdown -p now") cap.halt(machine) end it "ignores an IOError" do - comm.stub_command("/sbin/shutdown -p -h now", raise: IOError) + comm.stub_command("/sbin/shutdown -p now", raise: IOError) expect { cap.halt(machine) }.to_not raise_error diff --git a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb index 726b2926b..faa3b6935 100644 --- a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb @@ -24,7 +24,7 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do let(:name) { 'banana-rama.example.com' } it "sets the hostname if not set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/hostname -F \/etc\/hostname/) expect(comm.received_commands[1]).to match(/invoke-rc.d hostname.sh start/) @@ -33,7 +33,7 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do end it "does not set the hostname if unset" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb index fc5c061e9..bdd9b4226 100644 --- a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb @@ -23,7 +23,7 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname and /etc/hosts" do - comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) described_class.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/hostname '#{name}'/) @@ -32,7 +32,7 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do end it "does nothing if the hostname is already set" do - comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) described_class.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end From 8f3b6511f280cd6211d83dd46c9558623fe0d1f7 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 19:08:02 -0700 Subject: [PATCH 12/39] guests/linux: Add shared cap for listing network interfaces --- plugins/guests/arch/cap/configure_networks.rb | 8 +--- .../guests/debian/cap/configure_networks.rb | 6 +-- .../guests/fedora/cap/configure_networks.rb | 6 +-- .../guests/linux/cap/network_interfaces.rb | 20 ++++++++++ plugins/guests/linux/plugin.rb | 5 +++ .../guests/redhat/cap/configure_networks.rb | 6 +-- .../slackware/cap/configure_networks.rb | 6 +-- plugins/guests/suse/cap/configure_networks.rb | 6 +-- .../arch/cap/configure_networks_test.rb | 26 +++++++------ .../debian/cap/configure_networks_test.rb | 22 ++++++----- .../redhat/cap/configure_networks_test.rb | 38 +++++++++---------- .../slackware/cap/configure_networks_test.rb | 12 ++++-- .../suse/cap/configure_networks_test.rb | 29 +++++++------- 13 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 plugins/guests/linux/cap/network_interfaces.rb diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 9915b4a32..74989d984 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -12,13 +12,7 @@ module VagrantPlugins comm = machine.communicate commands = [] - interfaces = [] - - # The result will be something like: - # eth0\nenp0s8\nenp0s9 - comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| - interfaces = stdout.split("\n") - end + interfaces = machine.guest.capability(:network_interfaces) networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index e7fabcb68..263fe924a 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -13,11 +13,7 @@ module VagrantPlugins commands = [] entries = [] - interfaces = [] - - comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| - interfaces = stdout.split("\n") - end + interfaces = machine.guest.capability(:network_interfaces) networks.each do |network| network[:device] = interfaces[network[:interface]] diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb index 9305b34b0..cb54306fe 100644 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ b/plugins/guests/fedora/cap/configure_networks.rb @@ -14,14 +14,10 @@ module VagrantPlugins comm = machine.communicate network_scripts_dir = machine.guest.capability(:network_scripts_dir) + interfaces = machine.guest.capability(:network_interface) - interfaces = [] commands = [] - comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| - interfaces = stdout.split("\n") - end - networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] diff --git a/plugins/guests/linux/cap/network_interfaces.rb b/plugins/guests/linux/cap/network_interfaces.rb new file mode 100644 index 000000000..fd1480aa6 --- /dev/null +++ b/plugins/guests/linux/cap/network_interfaces.rb @@ -0,0 +1,20 @@ +module VagrantPlugins + module GuestLinux + module Cap + class NetworkInterfaces + # Get network interfaces as a list. The result will be something like: + # + # eth0\nenp0s8\nenp0s9 + # + # @return [Array] + def self.network_interfaces(machine) + s = "" + machine.communicate.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |type, data| + s << data if type == :stdout + end + s.split("\n") + end + end + end + end +end diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index 285c3ffdb..146915520 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -46,6 +46,11 @@ module VagrantPlugins Cap::MountVirtualBoxSharedFolder end + guest_capability(:linux, :network_interfaces) do + require_relative "cap/network_interfaces" + Cap::NetworkInterfaces + end + guest_capability(:linux, :nfs_client_installed) do require_relative "cap/nfs_client" Cap::NFSClient diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index 8a117f3d7..fa817594f 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -32,12 +32,8 @@ module VagrantPlugins network_scripts_dir = machine.guest.capability(:network_scripts_dir) - interfaces = [] commands = [] - - comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| - interfaces = stdout.split("\n") - end + interfaces = machine.guest.capability(:network_interfaces) networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] diff --git a/plugins/guests/slackware/cap/configure_networks.rb b/plugins/guests/slackware/cap/configure_networks.rb index 1d3011a10..b11b93e98 100644 --- a/plugins/guests/slackware/cap/configure_networks.rb +++ b/plugins/guests/slackware/cap/configure_networks.rb @@ -12,11 +12,7 @@ module VagrantPlugins comm = machine.communicate commands = [] - interfaces = [] - - comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result| - interfaces = result.split("\n") - end + interfaces = machine.guest.capability(:network_interfaces) # Remove any previous configuration commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.d/rc.inet1.conf" diff --git a/plugins/guests/suse/cap/configure_networks.rb b/plugins/guests/suse/cap/configure_networks.rb index cd1b5d697..2dd140230 100644 --- a/plugins/guests/suse/cap/configure_networks.rb +++ b/plugins/guests/suse/cap/configure_networks.rb @@ -15,11 +15,7 @@ module VagrantPlugins network_scripts_dir = machine.guest.capability(:network_scripts_dir) commands = [] - interfaces = [] - - comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout| - interfaces = stdout.split("\n") - end + interfaces = machine.guest.capability(:network_interfaces) networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] diff --git a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb index 82c50329d..bab7a7cb3 100644 --- a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb @@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do .guest_capabilities[:arch] end - let(:machine) { double("machine") } + let(:guest) { double("guest") } + let(:machine) { double("machine", guest: guest) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do allow(machine).to receive(:communicate).and_return(comm) - comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", - stdout: "eth1\neth2") end after do @@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do describe ".configure_networks" do let(:cap) { caps.get(:configure_networks) } + before do + allow(guest).to receive(:capability).with(:network_interfaces) + .and_return(["eth1", "eth2"]) + end + let(:network_1) do { interface: 0, @@ -42,15 +46,15 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do it "creates and starts the networks" do cap.configure_networks(machine, [network_1, network_2]) - expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth1'/) - expect(comm.received_commands[1]).to match(/ip link set 'eth1' down/) - expect(comm.received_commands[1]).to match(/netctl restart 'eth1'/) - expect(comm.received_commands[1]).to match(/netctl enable 'eth1'/) + expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth1'/) + expect(comm.received_commands[0]).to match(/ip link set 'eth1' down/) + expect(comm.received_commands[0]).to match(/netctl restart 'eth1'/) + expect(comm.received_commands[0]).to match(/netctl enable 'eth1'/) - expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth2'/) - expect(comm.received_commands[1]).to match(/ip link set 'eth2' down/) - expect(comm.received_commands[1]).to match(/netctl restart 'eth2'/) - expect(comm.received_commands[1]).to match(/netctl enable 'eth2'/) + expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth2'/) + expect(comm.received_commands[0]).to match(/ip link set 'eth2' down/) + expect(comm.received_commands[0]).to match(/netctl restart 'eth2'/) + expect(comm.received_commands[0]).to match(/netctl enable 'eth2'/) end end end diff --git a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb index bde388906..13a742cae 100644 --- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb @@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do .guest_capabilities[:debian] end - let(:machine) { double("machine") } + let(:guest) { double("guest") } + let(:machine) { double("machine", guest: guest) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do allow(machine).to receive(:communicate).and_return(comm) - comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", - stdout: "eth1\neth2") end after do @@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do describe ".configure_networks" do let(:cap) { caps.get(:configure_networks) } + before do + allow(guest).to receive(:capability).with(:network_interfaces) + .and_return(["eth1", "eth2"]) + end + let(:network_0) do { interface: 0, @@ -43,12 +47,12 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do it "creates and starts the networks" do cap.configure_networks(machine, [network_0, network_1]) - expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth1' || true") - expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth1'") - expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth2' || true") - expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth2'") - expect(comm.received_commands[1]).to match("/sbin/ifup 'eth1'") - expect(comm.received_commands[1]).to match("/sbin/ifup 'eth2'") + expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth1' || true") + expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth1'") + expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth2' || true") + expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth2'") + expect(comm.received_commands[0]).to match("/sbin/ifup 'eth1'") + expect(comm.received_commands[0]).to match("/sbin/ifup 'eth2'") end end end diff --git a/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb index 56050a893..284fe3580 100644 --- a/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb @@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do .guest_capabilities[:redhat] end - let(:machine) { double("machine") } + let(:guest) { double("guest") } + let(:machine) { double("machine", guest: guest) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do allow(machine).to receive(:communicate).and_return(comm) - comm.stub_command("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", - stdout: "eth1\neth2") end after do @@ -23,6 +22,16 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do describe ".configure_networks" do let(:cap) { caps.get(:configure_networks) } + before do + allow(guest).to receive(:capability) + .with(:network_scripts_dir) + .and_return("/scripts") + + allow(guest).to receive(:capability) + .with(:network_interfaces) + .and_return(["eth1", "eth2"]) + end + let(:network_1) do { interface: 0, @@ -40,21 +49,10 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do } end - let(:network_scripts_dir) { "/" } - - let(:capability) { double("capability") } - - before do - allow(machine).to receive(:guest).and_return(capability) - allow(capability).to receive(:capability) - .with(:network_scripts_dir) - .and_return(network_scripts_dir) - end - it "uses fedora for rhel7 configuration" do require_relative "../../../../../../plugins/guests/fedora/cap/configure_networks" - allow(capability).to receive(:capability) + allow(guest).to receive(:capability) .with(:flavor) .and_return(:rhel_7) allow(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks) @@ -66,15 +64,15 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - allow(capability).to receive(:capability) + allow(guest).to receive(:capability) .with(:flavor) .and_return(:rhel) cap.configure_networks(machine, [network_1, network_2]) - expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/) end end end diff --git a/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb b/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb index 10cc8493b..a03ea9242 100644 --- a/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/slackware/cap/configure_networks_test.rb @@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do .guest_capabilities[:slackware] end - let(:machine) { double("machine") } + let(:guest) { double("guest") } + let(:machine) { double("machine", guest: guest) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do allow(machine).to receive(:communicate).and_return(comm) - comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", - stdout: "eth1\neth2") end after do @@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do describe ".configure_networks" do let(:cap) { caps.get(:configure_networks) } + before do + allow(guest).to receive(:capability).with(:network_interfaces) + .and_return(["eth1", "eth2"]) + end + let(:network_1) do { interface: 0, @@ -42,7 +46,7 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do it "creates and starts the networks" do cap.configure_networks(machine, [network_1, network_2]) - expect(comm.received_commands[1]).to match(/\/etc\/rc.d\/rc.inet1/) + expect(comm.received_commands[0]).to match(/\/etc\/rc.d\/rc.inet1/) end end end diff --git a/test/unit/plugins/guests/suse/cap/configure_networks_test.rb b/test/unit/plugins/guests/suse/cap/configure_networks_test.rb index b31f4e3e7..944cf5cb5 100644 --- a/test/unit/plugins/guests/suse/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/suse/cap/configure_networks_test.rb @@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do .guest_capabilities[:suse] end - let(:machine) { double("machine") } + let(:guest) { double("guest") } + let(:machine) { double("machine", guest: guest) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } before do allow(machine).to receive(:communicate).and_return(comm) - comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'", - stdout: "eth1\neth2") end after do @@ -23,6 +22,13 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do describe ".configure_networks" do let(:cap) { caps.get(:configure_networks) } + before do + allow(guest).to receive(:capability).with(:network_scripts_dir) + .and_return("/scripts") + allow(guest).to receive(:capability).with(:network_interfaces) + .and_return(["eth1", "eth2"]) + end + let(:network_1) do { interface: 0, @@ -40,21 +46,12 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do } end - let(:guest) { double("guest") } - - before do - allow(machine).to receive(:guest).and_return(guest) - allow(guest).to receive(:capability) - .with(:network_scripts_dir) - .and_return("/scripts") - end - it "creates and starts the networks" do cap.configure_networks(machine, [network_1, network_2]) - expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/) - expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/) + expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/) end end end From cf91bcf029e8c3a95da19631ffc294fec8d6a543 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 19:29:46 -0700 Subject: [PATCH 13/39] guests: Always search for FQDN without sudo --- plugins/guests/arch/cap/change_host_name.rb | 2 +- plugins/guests/atomic/cap/change_host_name.rb | 2 +- plugins/guests/coreos/cap/change_host_name.rb | 2 +- plugins/guests/darwin/cap/change_host_name.rb | 2 +- plugins/guests/debian/cap/change_host_name.rb | 2 +- plugins/guests/fedora/cap/change_host_name.rb | 2 +- plugins/guests/freebsd/cap/change_host_name.rb | 5 ++--- plugins/guests/gentoo/cap/change_host_name.rb | 2 +- plugins/guests/omnios/cap/change_host_name.rb | 2 +- plugins/guests/photon/cap/change_host_name.rb | 2 +- plugins/guests/pld/cap/change_host_name.rb | 2 +- plugins/guests/redhat/cap/change_host_name.rb | 2 +- plugins/guests/slackware/cap/change_host_name.rb | 2 +- plugins/guests/suse/cap/change_host_name.rb | 2 +- plugins/guests/ubuntu/cap/change_host_name.rb | 2 +- 15 files changed, 16 insertions(+), 17 deletions(-) diff --git a/plugins/guests/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index f349d444b..d604cf9bf 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep '^#{name}$'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") hostnamectl set-hostname '#{basename}' diff --git a/plugins/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index 9f4b7c44f..74fd081ab 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep '^#{name}$'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") hostnamectl set-hostname '#{basename}' diff --git a/plugins/guests/coreos/cap/change_host_name.rb b/plugins/guests/coreos/cap/change_host_name.rb index f1aeb5bc4..fe9b21865 100644 --- a/plugins/guests/coreos/cap/change_host_name.rb +++ b/plugins/guests/coreos/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname --fqdn | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo("hostname '#{basename}'") diff --git a/plugins/guests/darwin/cap/change_host_name.rb b/plugins/guests/darwin/cap/change_host_name.rb index b7b69faa7..5ef6d9c08 100644 --- a/plugins/guests/darwin/cap/change_host_name.rb +++ b/plugins/guests/darwin/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index fa17ae2b0..b2e972cac 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -9,7 +9,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep '^#{name}$'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb index f8ceabd11..1add7fd5b 100644 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ b/plugins/guests/fedora/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") echo '#{basename}' > /etc/hostname diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index 09b517e9b..35b924af3 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -3,10 +3,9 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - options = { shell: "sh" } comm = machine.communicate - if !comm.test("hostname -f | grep '^#{name}$'", options) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false, shell: "sh") basename = name.split(".", 2)[0] command = <<-EOH.gsub(/^ {14}/, '') # Set the hostname @@ -23,7 +22,7 @@ module VagrantPlugins mv /tmp/tmp-hosts /etc/hosts } EOH - comm.sudo(command, options) + comm.sudo(command, shell: "sh") end end end diff --git a/plugins/guests/gentoo/cap/change_host_name.rb b/plugins/guests/gentoo/cap/change_host_name.rb index c36274e5c..f1f08a55c 100644 --- a/plugins/guests/gentoo/cap/change_host_name.rb +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep '^#{name}$'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") # Set the hostname diff --git a/plugins/guests/omnios/cap/change_host_name.rb b/plugins/guests/omnios/cap/change_host_name.rb index 4d238d350..9cf9d5b2f 100644 --- a/plugins/guests/omnios/cap/change_host_name.rb +++ b/plugins/guests/omnios/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set hostname diff --git a/plugins/guests/photon/cap/change_host_name.rb b/plugins/guests/photon/cap/change_host_name.rb index bb103d24c..49fe25c5b 100644 --- a/plugins/guests/photon/cap/change_host_name.rb +++ b/plugins/guests/photon/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname diff --git a/plugins/guests/pld/cap/change_host_name.rb b/plugins/guests/pld/cap/change_host_name.rb index 09dc5eaee..7697c4a20 100644 --- a/plugins/guests/pld/cap/change_host_name.rb +++ b/plugins/guests/pld/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') hostname '#{name}' diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb index 64691b8f5..816ecda21 100644 --- a/plugins/guests/redhat/cap/change_host_name.rb +++ b/plugins/guests/redhat/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split('.', 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Update sysconfig diff --git a/plugins/guests/slackware/cap/change_host_name.rb b/plugins/guests/slackware/cap/change_host_name.rb index d16bfabd0..c8f51e0d6 100644 --- a/plugins/guests/slackware/cap/change_host_name.rb +++ b/plugins/guests/slackware/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname diff --git a/plugins/guests/suse/cap/change_host_name.rb b/plugins/guests/suse/cap/change_host_name.rb index a6f291a9c..2e8cc1940 100644 --- a/plugins/guests/suse/cap/change_host_name.rb +++ b/plugins/guests/suse/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'", sudo: false) + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') echo '#{name}' > /etc/HOSTNAME diff --git a/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb index 64dcd4151..ffaec34f4 100644 --- a/plugins/guests/ubuntu/cap/change_host_name.rb +++ b/plugins/guests/ubuntu/cap/change_host_name.rb @@ -5,7 +5,7 @@ module VagrantPlugins def self.change_host_name(machine, name) comm = machine.communicate - if !comm.test("hostname -f | grep -w '#{name}'") + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') # Set the hostname From 336cb3319c1c4d919d4716e4be08955d42e6f1cf Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 19:31:12 -0700 Subject: [PATCH 14/39] guests/rhel: Use FQDN for hostname Refs: https://lukas.zapletalovi.com/2011/12/setting-hostname-properly-in-fedora-and.html --- plugins/guests/fedora/cap/change_host_name.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb index 1add7fd5b..0914dfa46 100644 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ b/plugins/guests/fedora/cap/change_host_name.rb @@ -8,10 +8,10 @@ module VagrantPlugins if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") - echo '#{basename}' > /etc/hostname + echo '#{name}' > /etc/hostname hostname -F /etc/hostname - hostnamectl set-hostname --static '#{basename}' - hostnamectl set-hostname --transient '#{basename}' + hostnamectl set-hostname --static '#{name}' + hostnamectl set-hostname --transient '#{name}' # Remove comments and blank lines from /etc/hosts sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts From 084d62b5a676671a8e40fc14781b290d0f3151f3 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 22 Jun 2016 19:49:33 -0700 Subject: [PATCH 15/39] guests/fedora: Move as a child of redhat Fedora should have been a child of redhat for awhile now... --- plugins/guests/fedora/cap/change_host_name.rb | 29 --------- .../guests/fedora/cap/configure_networks.rb | 60 ------------------- plugins/guests/fedora/cap/flavor.rb | 2 +- .../guests/fedora/cap/network_scripts_dir.rb | 15 ----- plugins/guests/fedora/plugin.rb | 15 ----- plugins/guests/redhat/cap/change_host_name.rb | 5 +- .../guests/redhat/cap/configure_networks.rb | 19 ------ templates/guests/fedora/network_dhcp.erb | 6 -- templates/guests/fedora/network_static.erb | 16 ----- templates/guests/fedora/network_static6.erb | 9 --- templates/guests/redhat/network_static.erb | 3 + templates/guests/redhat/network_static6.erb | 14 +++-- 12 files changed, 15 insertions(+), 178 deletions(-) delete mode 100644 plugins/guests/fedora/cap/change_host_name.rb delete mode 100644 plugins/guests/fedora/cap/configure_networks.rb delete mode 100644 plugins/guests/fedora/cap/network_scripts_dir.rb delete mode 100644 templates/guests/fedora/network_dhcp.erb delete mode 100644 templates/guests/fedora/network_static.erb delete mode 100644 templates/guests/fedora/network_static6.erb diff --git a/plugins/guests/fedora/cap/change_host_name.rb b/plugins/guests/fedora/cap/change_host_name.rb deleted file mode 100644 index 0914dfa46..000000000 --- a/plugins/guests/fedora/cap/change_host_name.rb +++ /dev/null @@ -1,29 +0,0 @@ -module VagrantPlugins - module GuestFedora - module Cap - class ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, "") - echo '#{name}' > /etc/hostname - hostname -F /etc/hostname - hostnamectl set-hostname --static '#{name}' - hostnamectl set-hostname --transient '#{name}' - - # Remove comments and blank lines from /etc/hosts - sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts - - # Prepend ourselves to /etc/hosts - grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts - } - EOH - end - end - end - end - end -end diff --git a/plugins/guests/fedora/cap/configure_networks.rb b/plugins/guests/fedora/cap/configure_networks.rb deleted file mode 100644 index cb54306fe..000000000 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ /dev/null @@ -1,60 +0,0 @@ -require "tempfile" - -require_relative "../../../../lib/vagrant/util/retryable" -require_relative "../../../../lib/vagrant/util/template_renderer" - -module VagrantPlugins - module GuestFedora - module Cap - class ConfigureNetworks - extend Vagrant::Util::Retryable - include Vagrant::Util - - def self.configure_networks(machine, networks) - comm = machine.communicate - - network_scripts_dir = machine.guest.capability(:network_scripts_dir) - interfaces = machine.guest.capability(:network_interface) - - commands = [] - - networks.each.with_index do |network, i| - network[:device] = interfaces[network[:interface]] - - # Render a new configuration - entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}", - options: network, - ) - - # Upload the new configuration - remote_path = "/tmp/vagrant-network-entry-#{network[:device]}-#{Time.now.to_i}-#{i}" - Tempfile.open("vagrant-fedora-configure-networks") do |f| - f.binmode - f.write(entry) - f.fsync - f.close - machine.communicate.upload(f.path, remote_path) - end - - # Add the new interface and bring it back up - final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}" - commands << <<-EOH.gsub(/^ {14}/, '') - # Down the interface before munging the config file. This might - # fail if the interface is not actually set up yet so ignore - # errors. - /sbin/ifdown '#{network[:device]}' || true - - # Move new config into place - mv '#{remote_path}' '#{final_path}' - - # Bring the interface up - ARPCHECK=no /sbin/ifup '#{network[:device]}' - EOH - end - - comm.sudo(commands.join("\n")) - end - end - end - end -end diff --git a/plugins/guests/fedora/cap/flavor.rb b/plugins/guests/fedora/cap/flavor.rb index 2a29f50b5..fa5bf2463 100644 --- a/plugins/guests/fedora/cap/flavor.rb +++ b/plugins/guests/fedora/cap/flavor.rb @@ -14,7 +14,7 @@ module VagrantPlugins if version.nil? return :fedora else - return "fedora_#{version}".to_sym + return :"fedora_#{version}" end end end diff --git a/plugins/guests/fedora/cap/network_scripts_dir.rb b/plugins/guests/fedora/cap/network_scripts_dir.rb deleted file mode 100644 index 82a4abad7..000000000 --- a/plugins/guests/fedora/cap/network_scripts_dir.rb +++ /dev/null @@ -1,15 +0,0 @@ -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/plugin.rb b/plugins/guests/fedora/plugin.rb index acb39548d..9a9241b0c 100644 --- a/plugins/guests/fedora/plugin.rb +++ b/plugins/guests/fedora/plugin.rb @@ -11,21 +11,6 @@ module VagrantPlugins 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 - guest_capability(:fedora, :flavor) do require_relative "cap/flavor" Cap::Flavor diff --git a/plugins/guests/redhat/cap/change_host_name.rb b/plugins/guests/redhat/cap/change_host_name.rb index 816ecda21..afe461d4f 100644 --- a/plugins/guests/redhat/cap/change_host_name.rb +++ b/plugins/guests/redhat/cap/change_host_name.rb @@ -17,9 +17,10 @@ module VagrantPlugins # Set the hostname - use hostnamectl if available echo '#{name}' > /etc/hostname if command -v hostnamectl; then - hostnamectl set-hostname '#{name}' + hostnamectl set-hostname --static '#{name}' + hostnamectl set-hostname --transient '#{name}' else - hostname '#{name}' + hostname -F /etc/hostname fi # Remove comments and blank lines from /etc/hosts diff --git a/plugins/guests/redhat/cap/configure_networks.rb b/plugins/guests/redhat/cap/configure_networks.rb index fa817594f..6cbc9ec17 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -1,33 +1,14 @@ require "tempfile" -require_relative "../../../../lib/vagrant/util/retryable" require_relative "../../../../lib/vagrant/util/template_renderer" module VagrantPlugins module GuestRedHat module Cap class ConfigureNetworks - extend Vagrant::Util::Retryable include Vagrant::Util def self.configure_networks(machine, networks) - case machine.guest.capability(:flavor) - when :rhel_7 - configure_networks_rhel7(machine, networks) - else - configure_networks_default(machine, networks) - end - end - - def self.configure_networks_rhel7(machine, networks) - # This is kind of jank but the configure networks is the same as - # Fedora at this point. - require_relative "../../fedora/cap/configure_networks" - ::VagrantPlugins::GuestFedora::Cap::ConfigureNetworks - .configure_networks(machine, networks) - end - - def self.configure_networks_default(machine, networks) comm = machine.communicate network_scripts_dir = machine.guest.capability(:network_scripts_dir) diff --git a/templates/guests/fedora/network_dhcp.erb b/templates/guests/fedora/network_dhcp.erb deleted file mode 100644 index b15250cc2..000000000 --- a/templates/guests/fedora/network_dhcp.erb +++ /dev/null @@ -1,6 +0,0 @@ -#VAGRANT-BEGIN -# The contents below are automatically generated by Vagrant. Do not modify. -BOOTPROTO=dhcp -ONBOOT=yes -DEVICE=<%= options[:device] %> -#VAGRANT-END diff --git a/templates/guests/fedora/network_static.erb b/templates/guests/fedora/network_static.erb deleted file mode 100644 index 000ea6150..000000000 --- a/templates/guests/fedora/network_static.erb +++ /dev/null @@ -1,16 +0,0 @@ -#VAGRANT-BEGIN -# The contents below are automatically generated by Vagrant. Do not modify. -NM_CONTROLLED=no -BOOTPROTO=none -ONBOOT=yes -IPADDR=<%= options[:ip] %> -NETMASK=<%= options[:netmask] %> -DEVICE=<%= options[:device] %> -<% if options[:gateway] %> -GATEWAY=<%= options[:gateway] %> -<% end %> -<% if options[:mac_address] %> -HWADDR=<%= options[:mac_address] %> -<% end %> -PEERDNS=no -#VAGRANT-END diff --git a/templates/guests/fedora/network_static6.erb b/templates/guests/fedora/network_static6.erb deleted file mode 100644 index 9ca7821d8..000000000 --- a/templates/guests/fedora/network_static6.erb +++ /dev/null @@ -1,9 +0,0 @@ -#VAGRANT-BEGIN -# The contents below are automatically generated by Vagrant. Do not modify. -NM_CONTROLLED=no -BOOTPROTO=static -ONBOOT=yes -IPV6INIT=yes -IPV6ADDR=<%= options[:ip] %> -DEVICE=<%= options[:device] %> -#VAGRANT-END diff --git a/templates/guests/redhat/network_static.erb b/templates/guests/redhat/network_static.erb index b144c6cf4..000ea6150 100644 --- a/templates/guests/redhat/network_static.erb +++ b/templates/guests/redhat/network_static.erb @@ -9,5 +9,8 @@ DEVICE=<%= options[:device] %> <% if options[:gateway] %> GATEWAY=<%= options[:gateway] %> <% end %> +<% if options[:mac_address] %> +HWADDR=<%= options[:mac_address] %> +<% end %> PEERDNS=no #VAGRANT-END diff --git a/templates/guests/redhat/network_static6.erb b/templates/guests/redhat/network_static6.erb index 288da4396..275c97508 100644 --- a/templates/guests/redhat/network_static6.erb +++ b/templates/guests/redhat/network_static6.erb @@ -1,10 +1,12 @@ #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. -auto <%= options[:device] %> -iface <%= options[:device] %> inet6 static - address <%= options[:ip] %> - netmask <%= options[:netmask] %> -<% if options[:gateway] %> - gateway <%= options[:gateway] %> +NM_CONTROLLED=no +BOOTPROTO=static +ONBOOT=yes +DEVICE=<%= options[:device] %> +IPV6INIT=yes +IPV6ADDR=<%= options[:ip] %>/<%= options[:netmask] %> +<% if options[:gateway] -%> +IPV6_DEFAULTGW=<%= options[:gateway] %> <% end %> #VAGRANT-END From cf9a8e3c0a4e42f0d4618cab84c17b26607e336c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:06:02 -0400 Subject: [PATCH 16/39] guests/arch: Ensure exit on error setting hostname --- plugins/guests/arch/cap/change_host_name.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/guests/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index d604cf9bf..0b10112e1 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -8,6 +8,9 @@ module VagrantPlugins if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") + set -e + + # Set hostname hostnamectl set-hostname '#{basename}' # Remove comments and blank lines from /etc/hosts From e69211ab22fd734aacf0ec8054eec21809d89da0 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:07:25 -0400 Subject: [PATCH 17/39] guests/arch: Add NFS capabilities --- plugins/guests/arch/cap/nfs.rb | 35 +++++++++++++++++++++ plugins/guests/arch/plugin.rb | 15 +++++++++ plugins/synced_folders/nfs/synced_folder.rb | 12 +++++-- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 plugins/guests/arch/cap/nfs.rb diff --git a/plugins/guests/arch/cap/nfs.rb b/plugins/guests/arch/cap/nfs.rb new file mode 100644 index 000000000..116256ed2 --- /dev/null +++ b/plugins/guests/arch/cap/nfs.rb @@ -0,0 +1,35 @@ +module VagrantPlugins + module GuestArch + module Cap + class NFS + def self.nfs_client_installed(machine) + machine.communicate.test("pacman -Q nfs-utils") + end + + def self.nfs_pre(machine) + comm = machine.communicate + + # There is a bug in NFS where the rpcbind functionality is not started + # and it's not a dependency of nfs-utils. Read more here: + # + # https://bbs.archlinux.org/viewtopic.php?id=193410 + # + comm.sudo <<-EOH.gsub(/^ {12}/, "") + set -e + systemctl enable rpcbind + systemctl start rpcbind + EOH + end + + def self.nfs_client_install(machine) + comm = machine.communicate + comm.sudo <<-EOH.gsub(/^ {12}/, "") + set -e + pacman --noconfirm -Syy + pacman --noconfirm -S nfs-utils ntp + EOH + end + end + end + end +end diff --git a/plugins/guests/arch/plugin.rb b/plugins/guests/arch/plugin.rb index 6b678d763..440917754 100644 --- a/plugins/guests/arch/plugin.rb +++ b/plugins/guests/arch/plugin.rb @@ -20,6 +20,21 @@ module VagrantPlugins require_relative "cap/configure_networks" Cap::ConfigureNetworks end + + guest_capability(:arch, :nfs_client_install) do + require_relative "cap/nfs" + Cap::NFS + end + + guest_capability(:arch, :nfs_client_installed) do + require_relative "cap/nfs" + Cap::NFS + end + + guest_capability(:arch, :nfs_pre) do + require_relative "cap/nfs" + Cap::NFS + end end end end diff --git a/plugins/synced_folders/nfs/synced_folder.rb b/plugins/synced_folders/nfs/synced_folder.rb index 8fe0f69c3..f75cc8f4a 100644 --- a/plugins/synced_folders/nfs/synced_folder.rb +++ b/plugins/synced_folders/nfs/synced_folder.rb @@ -101,8 +101,16 @@ module VagrantPlugins end # Mount them! - machine.guest.capability( - :mount_nfs_folder, nfsopts[:nfs_host_ip], mount_folders) + if machine.guest.capability?(:nfs_pre) + machine.guest.capability(:nfs_pre) + end + + machine.guest.capability(:mount_nfs_folder, + nfsopts[:nfs_host_ip], mount_folders) + + if machine.guest.capability?(:nfs_post) + machine.guest.capability(:nfs_post) + end end def cleanup(machine, opts) From 9ea13105c0c3c0abdd88cbe0bf7806720741b3aa Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:08:35 -0400 Subject: [PATCH 18/39] guests/arch: Ensure exit on configure networks fail --- plugins/guests/arch/cap/configure_networks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 74989d984..8f75548f0 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -11,7 +11,7 @@ module VagrantPlugins def self.configure_networks(machine, networks) comm = machine.communicate - commands = [] + commands = ["set -e"] interfaces = machine.guest.capability(:network_interfaces) networks.each.with_index do |network, i| From 627091de35f258fa3841e049fc4dd0390523aae7 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:08:58 -0400 Subject: [PATCH 19/39] guests/linux: reorg --- plugins/guests/linux/cap/{mount_nfs.rb => nfs.rb} | 6 +++++- plugins/guests/linux/cap/nfs_client.rb | 11 ----------- plugins/guests/linux/plugin.rb | 8 ++++---- 3 files changed, 9 insertions(+), 16 deletions(-) rename plugins/guests/linux/cap/{mount_nfs.rb => nfs.rb} (92%) delete mode 100644 plugins/guests/linux/cap/nfs_client.rb diff --git a/plugins/guests/linux/cap/mount_nfs.rb b/plugins/guests/linux/cap/nfs.rb similarity index 92% rename from plugins/guests/linux/cap/mount_nfs.rb rename to plugins/guests/linux/cap/nfs.rb index e220fd379..d47d53b98 100644 --- a/plugins/guests/linux/cap/mount_nfs.rb +++ b/plugins/guests/linux/cap/nfs.rb @@ -3,9 +3,13 @@ require "vagrant/util/retryable" module VagrantPlugins module GuestLinux module Cap - class MountNFS + class NFS extend Vagrant::Util::Retryable + def self.nfs_client_installed(machine) + machine.communicate.test("test -x /sbin/mount.nfs") + end + def self.mount_nfs_folder(machine, ip, folders) comm = machine.communicate diff --git a/plugins/guests/linux/cap/nfs_client.rb b/plugins/guests/linux/cap/nfs_client.rb deleted file mode 100644 index 8e51da37c..000000000 --- a/plugins/guests/linux/cap/nfs_client.rb +++ /dev/null @@ -1,11 +0,0 @@ -module VagrantPlugins - module GuestLinux - module Cap - class NFSClient - def self.nfs_client_installed(machine) - machine.communicate.test("test -x /sbin/mount.nfs") - end - end - end - end -end diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index 146915520..966f56353 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -32,8 +32,8 @@ module VagrantPlugins end guest_capability(:linux, :mount_nfs_folder) do - require_relative "cap/mount_nfs" - Cap::MountNFS + require_relative "cap/nfs" + Cap::NFS end guest_capability(:linux, :mount_smb_shared_folder) do @@ -52,8 +52,8 @@ module VagrantPlugins end guest_capability(:linux, :nfs_client_installed) do - require_relative "cap/nfs_client" - Cap::NFSClient + require_relative "cap/nfs" + Cap::NFS end # For the Docker provider From ccc40eb19d8bc7a425c34daec160f5b782498b5c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:16:44 -0400 Subject: [PATCH 20/39] guests/arch: compute netmask Arch expects /24 for netmask, but users usually specify as a netmask block like 255.255.255.0. This automatically converts it. --- plugins/guests/arch/cap/configure_networks.rb | 9 +++++++++ templates/guests/arch/network_static.erb | 6 +++--- templates/guests/arch/network_static6.erb | 8 ++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 8f75548f0..2fa9c8017 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -1,3 +1,5 @@ +require "ipaddr" +require "socket" require "tempfile" require_relative "../../../../lib/vagrant/util/template_renderer" @@ -17,6 +19,13 @@ module VagrantPlugins networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] + # Arch expects netmasks to be in the "24" or "64", but users may + # specify IPV4 netmasks like "255.255.255.0". This magic converts + # the netmask to the proper value. + if network[:netmask] && network[:netmask].to_s.include?(".") + network[:netmask] = (32-Math.log2((IPAddr.new(network[:netmask], Socket::AF_INET).to_i^0xffffffff)+1)).to_i + end + entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}", options: network, ) diff --git a/templates/guests/arch/network_static.erb b/templates/guests/arch/network_static.erb index f8cb06f1b..52b2f2d77 100644 --- a/templates/guests/arch/network_static.erb +++ b/templates/guests/arch/network_static.erb @@ -2,7 +2,7 @@ Connection=ethernet Description='A basic static ethernet connection' Interface=<%= options[:device] %> IP=static -Address=('<%= options[:ip]%>/24') -<% if options[:gateway] %> +Address=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> Gateway='<%= options[:gateway] %>' -<% end %> +<% end -%> diff --git a/templates/guests/arch/network_static6.erb b/templates/guests/arch/network_static6.erb index 40e61c596..e64ffd5ca 100644 --- a/templates/guests/arch/network_static6.erb +++ b/templates/guests/arch/network_static6.erb @@ -2,7 +2,7 @@ Connection=ethernet Description='A basic IPv6 ethernet connection' Interface=<%= options[:device] %> IP6=static -Address=('<%= options[:ip]%>/<%= options[:netmask] %>') -<% if options[:gateway] %> -Gateway='<%= options[:gateway] %>' -<% end %> +Address6=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> +Gateway6='<%= options[:gateway] %>' +<% end -%> From 0af02fc78c119af7d4621ebc0b5bc7adadc8554c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:28:46 -0400 Subject: [PATCH 21/39] guests/atomic: Ensure error exit when setting hostname --- plugins/guests/atomic/cap/change_host_name.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index 74fd081ab..2c501c8dd 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -8,6 +8,9 @@ module VagrantPlugins if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") + set -e + + # Set hostname hostnamectl set-hostname '#{basename}' # Remove comments and blank lines from /etc/hosts From 9134172ce6d6bc6a93cf3fc1d045e1fe29c0ad1a Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:17:45 -0400 Subject: [PATCH 22/39] guests/debian: Move NFS into same file --- plugins/guests/debian/cap/{nfs_client.rb => nfs.rb} | 3 ++- plugins/guests/debian/plugin.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) rename plugins/guests/debian/cap/{nfs_client.rb => nfs.rb} (90%) diff --git a/plugins/guests/debian/cap/nfs_client.rb b/plugins/guests/debian/cap/nfs.rb similarity index 90% rename from plugins/guests/debian/cap/nfs_client.rb rename to plugins/guests/debian/cap/nfs.rb index d8107565d..881469906 100644 --- a/plugins/guests/debian/cap/nfs_client.rb +++ b/plugins/guests/debian/cap/nfs.rb @@ -1,10 +1,11 @@ module VagrantPlugins module GuestDebian module Cap - class NFSClient + class NFS def self.nfs_client_install(machine) comm = machine.communicate comm.sudo <<-EOH.gsub(/^ {12}/, '') + set -e apt-get -yqq update apt-get -yqq install nfs-common portmap EOH diff --git a/plugins/guests/debian/plugin.rb b/plugins/guests/debian/plugin.rb index 84be3c975..705cb6557 100644 --- a/plugins/guests/debian/plugin.rb +++ b/plugins/guests/debian/plugin.rb @@ -22,8 +22,8 @@ module VagrantPlugins end guest_capability(:debian, :nfs_client_install) do - require_relative "cap/nfs_client" - Cap::NFSClient + require_relative "cap/nfs" + Cap::NFS end guest_capability(:debian, :rsync_install) do From 7a7256b3abb022f0da57b2945989e7f0dd20c733 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:18:04 -0400 Subject: [PATCH 23/39] guests/debian: Exit on error when configuring hostname --- plugins/guests/debian/cap/change_host_name.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index b2e972cac..7d34c7b90 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -12,6 +12,8 @@ module VagrantPlugins if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') + set -e + # Set the hostname echo '#{basename}' > /etc/hostname hostname -F /etc/hostname @@ -28,16 +30,16 @@ module VagrantPlugins echo '#{name}' > /etc/mailname # Restart networking and force new DHCP - if [ test -f /etc/init.d/hostname.sh ]; then - invoke-rc.d hostname.sh start + if test -f /etc/init.d/hostname.sh; then + invoke-rc.d hostname.sh start || true fi - if [ test -f /etc/init.d/networking ]; then - invoke-rc.d networking force-reload + if test -f /etc/init.d/networking; then + invoke-rc.d networking force-reload || true fi - if [ test -f /etc/init.d/network-manager ]; then - invoke-rc.d network-manager force-reload + if test -f /etc/init.d/network-manager; then + invoke-rc.d network-manager force-reload || true fi EOH end From 76bab1932ea5d03371dd611be5c760f2752698f5 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:18:16 -0400 Subject: [PATCH 24/39] guests/debian: Use set -e when configuring networks --- plugins/guests/debian/cap/configure_networks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index 263fe924a..2553516a8 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -11,7 +11,7 @@ module VagrantPlugins def self.configure_networks(machine, networks) comm = machine.communicate - commands = [] + commands = ["set -e"] entries = [] interfaces = machine.guest.capability(:network_interfaces) From cb2f3a697f66d86fd177622b342294ef6fb087f0 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:18:40 -0400 Subject: [PATCH 25/39] guests/debian: Do not check if rsync is installed before installing This is already done via the rsync_installed capability. --- plugins/guests/debian/cap/rsync.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/plugins/guests/debian/cap/rsync.rb b/plugins/guests/debian/cap/rsync.rb index d8371bf86..b503f8cf3 100644 --- a/plugins/guests/debian/cap/rsync.rb +++ b/plugins/guests/debian/cap/rsync.rb @@ -4,12 +4,11 @@ module VagrantPlugins class RSync def self.rsync_install(machine) comm = machine.communicate - if !comm.test("command -v rsync") - comm.sudo <<-EOH.gsub(/^ {14}/, '') - apt-get -yqq update - apt-get -yqq install rsync - EOH - end + comm.sudo <<-EOH.gsub(/^ {14}/, '') + set -e + apt-get -yqq update + apt-get -yqq install rsync + EOH end end end From 4aaa600bd6c677b5476ab47ed1cd1d8a04b35a95 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:19:08 -0400 Subject: [PATCH 26/39] guests/linux: Centralize public_key logic --- plugins/guests/linux/cap/insert_public_key.rb | 31 --------- plugins/guests/linux/cap/public_key.rb | 65 +++++++++++++++++++ plugins/guests/linux/cap/remove_public_key.rb | 24 ------- plugins/guests/linux/plugin.rb | 8 +-- 4 files changed, 69 insertions(+), 59 deletions(-) delete mode 100644 plugins/guests/linux/cap/insert_public_key.rb create mode 100644 plugins/guests/linux/cap/public_key.rb delete mode 100644 plugins/guests/linux/cap/remove_public_key.rb diff --git a/plugins/guests/linux/cap/insert_public_key.rb b/plugins/guests/linux/cap/insert_public_key.rb deleted file mode 100644 index 653720587..000000000 --- a/plugins/guests/linux/cap/insert_public_key.rb +++ /dev/null @@ -1,31 +0,0 @@ -module VagrantPlugins - module GuestLinux - module Cap - class InsertPublicKey - def self.insert_public_key(machine, contents) - comm = machine.communicate - contents = contents.strip << "\n" - - remote_path = "/tmp/vagrant-authorized-keys-#{Time.now.to_i}" - Tempfile.open("vagrant-linux-insert-public-key") do |f| - f.binmode - f.write(contents) - f.fsync - f.close - comm.upload(f.path, remote_path) - end - - comm.execute <<-EOH.gsub(/^ {12}/, '') - mkdir -p ~/.ssh - chmod 0700 ~/.ssh - cat '#{remote_path}' >> ~/.ssh/authorized_keys - chmod 0600 ~/.ssh/authorized_keys - - # Remove the temporary file - rm -f '#{remote_path}' - EOH - end - end - end - end -end diff --git a/plugins/guests/linux/cap/public_key.rb b/plugins/guests/linux/cap/public_key.rb new file mode 100644 index 000000000..92a718281 --- /dev/null +++ b/plugins/guests/linux/cap/public_key.rb @@ -0,0 +1,65 @@ +require "tempfile" + +require "vagrant/util/shell_quote" + +module VagrantPlugins + module GuestLinux + module Cap + class PublicKey + def self.insert_public_key(machine, contents) + comm = machine.communicate + contents = contents.strip << "\n" + + remote_path = "/tmp/vagrant-insert-pubkey-#{Time.now.to_i}" + Tempfile.open("vagrant-linux-insert-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) + end + + # Use execute (not sudo) because we want to execute this as the SSH + # user (which is "vagrant" by default). + comm.execute <<-EOH.gsub(/^ {12}/, "") + set -e + + mkdir -p ~/.ssh + chmod 0700 ~/.ssh + cat '#{remote_path}' >> ~/.ssh/authorized_keys + chmod 0600 ~/.ssh/authorized_keys + + rm -f '#{remote_path}' + EOH + end + + def self.remove_public_key(machine, contents) + comm = machine.communicate + contents = contents.strip << "\n" + + remote_path = "/tmp/vagrant-remove-pubkey-#{Time.now.to_i}" + Tempfile.open("vagrant-bsd-remove-public-key") do |f| + f.binmode + f.write(contents) + f.fsync + f.close + comm.upload(f.path, remote_path) + end + + # Use execute (not sudo) because we want to execute this as the SSH + # user (which is "vagrant" by default). + comm.execute <<-EOH.sub(/^ {12}/, "") + set -e + + if test -f ~/.ssh/authorized_keys; then + grep -v -x -f '#{remote_path}' ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp + mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys + fi + + rm -f '#{remote_path}' + EOH + end + end + end + end +end diff --git a/plugins/guests/linux/cap/remove_public_key.rb b/plugins/guests/linux/cap/remove_public_key.rb deleted file mode 100644 index dfd9daae5..000000000 --- a/plugins/guests/linux/cap/remove_public_key.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestLinux - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute(< ~/.ssh/authorized_keys.new -mv ~/.ssh/authorized_keys.new ~/.ssh/authorized_keys -chmod 600 ~/.ssh/authorized_keys -SCRIPT - end - end - end - end - end - end -end diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index 966f56353..cb0a53c19 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -22,8 +22,8 @@ module VagrantPlugins end guest_capability(:linux, :insert_public_key) do - require_relative "cap/insert_public_key" - Cap::InsertPublicKey + require_relative "cap/public_key" + Cap::PublicKey end guest_capability(:linux, :shell_expand_guest_path) do @@ -68,8 +68,8 @@ module VagrantPlugins end guest_capability(:linux, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey + require_relative "cap/public_key" + Cap::PublicKey end guest_capability(:linux, :rsync_installed) do From 7e88266999e0eaf86299421e99a4fb038b4f2b1c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 21:19:58 -0400 Subject: [PATCH 27/39] guests/linux: Properly escape and retry vbox shared folder mounting --- lib/vagrant/errors.rb | 4 + .../cap/mount_virtualbox_shared_folder.rb | 78 ++++++++----------- templates/locales/en.yml | 13 ++++ 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 944bdd5f8..786e52f33 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -780,6 +780,10 @@ module Vagrant error_key(:virtualbox_no_name) end + class VirtualBoxMountFailed < VagrantError + error_key(:virtualbox_mount_failed) + end + class VirtualBoxNameExists < VagrantError error_key(:virtualbox_name_exists) end diff --git a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb index 1d67c86e3..6e87e42e8 100644 --- a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb +++ b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb @@ -1,12 +1,17 @@ +require "shellwords" + +require "vagrant/util/retryable" + module VagrantPlugins module GuestLinux module Cap class MountVirtualBoxSharedFolder - def self.mount_virtualbox_shared_folder(machine, name, guestpath, options) - expanded_guest_path = machine.guest.capability( - :shell_expand_guest_path, guestpath) + extend Vagrant::Util::Retryable - mount_commands = [] + def self.mount_virtualbox_shared_folder(machine, name, guestpath, options) + guest_path = Shellwords.escape(guestpath) + + mount_commands = ["set -e"] if options[:owner].is_a? Integer mount_uid = options[:owner] @@ -25,73 +30,54 @@ module VagrantPlugins # First mount command uses getent to get the group mount_options = "-o uid=#{mount_uid},gid=#{mount_gid}" mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options] - mount_commands << "mount -t vboxsf #{mount_options} #{name} #{expanded_guest_path}" + mount_commands << "mount -t vboxsf #{mount_options} #{name} #{guest_path}" # Second mount command uses the old style `id -g` mount_options = "-o uid=#{mount_uid},gid=#{mount_gid_old}" mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options] - mount_commands << "mount -t vboxsf #{mount_options} #{name} #{expanded_guest_path}" + mount_commands << "mount -t vboxsf #{mount_options} #{name} #{guest_path}" # Create the guest path if it doesn't exist - machine.communicate.sudo("mkdir -p #{expanded_guest_path}") + machine.communicate.sudo("mkdir -p #{guest_path}") # Attempt to mount the folder. We retry here a few times because # it can fail early on. - attempts = 0 - while true - success = true - - stderr = "" - mount_commands.each do |command| - no_such_device = false - stderr = "" - status = machine.communicate.sudo(command, error_check: false) do |type, data| - if type == :stderr - no_such_device = true if data =~ /No such device/i - stderr += data.to_s - end - end - - success = status == 0 && !no_such_device - break if success - end - - break if success - - attempts += 1 - if attempts > 10 - raise Vagrant::Errors::LinuxMountFailed, - command: mount_commands.join("\n"), - output: stderr - end - - sleep(2*attempts) + command = mount_commands.join("\n") + stderr = "" + retryable(on: Vagrant::Errors::VirtualBoxMountFailed, tries: 3, sleep: 5) do + machine.communicate.sudo(command, + error_class: Vagrant::Errors::VirtualBoxMountFailed, + error_key: :virtualbox_mount_failed, + command: command, + output: stderr, + ) { |type, data| stderr = data if type == :stderr } end # Chown the directory to the proper user. We skip this if the # mount options contained a readonly flag, because it won't work. if !options[:mount_options] || !options[:mount_options].include?("ro") chown_commands = [] - chown_commands << "chown #{mount_uid}:#{mount_gid} #{expanded_guest_path}" - chown_commands << "chown #{mount_uid}:#{mount_gid_old} #{expanded_guest_path}" + chown_commands << "chown #{mount_uid}:#{mount_gid} #{guest_path}" + chown_commands << "chown #{mount_uid}:#{mount_gid_old} #{guest_path}" exit_status = machine.communicate.sudo(chown_commands[0], error_check: false) machine.communicate.sudo(chown_commands[1]) if exit_status != 0 end # Emit an upstart event if we can - machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init && /sbin/init --version | grep upstart; then - /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' -fi -SCRIPT + machine.communicate.sudo <<-EOH.gsub(/^ {12}/, "") + if command -v /sbin/init && /sbin/init --version | grep upstart; then + /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path} + fi + EOH end def self.unmount_virtualbox_shared_folder(machine, guestpath, options) - result = machine.communicate.sudo( - "umount #{guestpath}", error_check: false) + guest_path = Shellwords.escape(guestpath) + + result = machine.communicate.sudo("umount #{guest_path}", error_check: false) if result == 0 - machine.communicate.sudo("rmdir #{guestpath}", error_check: false) + machine.communicate.sudo("rmdir #{guest_path}", error_check: false) end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index d3fa33e13..249206838 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1342,6 +1342,19 @@ en: VirtualBox is complaining that the installation is incomplete. Please run `VBoxManage --version` to see the error message which should contain instructions on how to fix this error. + virtualbox_mount_failed: |- + Vagrant was unable to mount VirtualBox shared folders. This is usually + because the filesystem "vboxsf" is not available. This filesystem is + made available via the VirtualBox Guest Additions and kernel module. + Please verify that these guest additions are properly installed in the + guest. This is not a bug in Vagrant and is usually caused by a faulty + Vagrant box. For context, the command attemped was: + + %{command} + + The error output from the command was: + + %{output} virtualbox_name_exists: |- The name of your virtual machine couldn't be set because VirtualBox is reporting another VM with that name already exists. Most of the From 619c7a5b26ade0e8671c30ced09d36746bcd2c27 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 25 Jun 2016 15:22:58 -0400 Subject: [PATCH 28/39] guests/darwin: Exit on error setting hostname --- plugins/guests/darwin/cap/change_host_name.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/guests/darwin/cap/change_host_name.rb b/plugins/guests/darwin/cap/change_host_name.rb index 5ef6d9c08..5d5902d9e 100644 --- a/plugins/guests/darwin/cap/change_host_name.rb +++ b/plugins/guests/darwin/cap/change_host_name.rb @@ -9,6 +9,9 @@ module VagrantPlugins basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') + set -e + + # Set hostname scutil --set ComputerName '#{name}' scutil --set HostName '#{name}' From d1a071d403660d7cdaf0db0569ec72b690c76181 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 25 Jun 2016 15:23:28 -0400 Subject: [PATCH 29/39] guests/darwin: Shellescape guest path for rsync --- plugins/guests/darwin/cap/rsync.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/guests/darwin/cap/rsync.rb b/plugins/guests/darwin/cap/rsync.rb index 7e5d6d9a5..e1c594a19 100644 --- a/plugins/guests/darwin/cap/rsync.rb +++ b/plugins/guests/darwin/cap/rsync.rb @@ -1,3 +1,5 @@ +require "shellwords" + module VagrantPlugins module GuestDarwin module Cap @@ -11,9 +13,8 @@ module VagrantPlugins end def self.rsync_pre(machine, opts) - machine.communicate.tap do |comm| - comm.sudo("mkdir -p '#{opts[:guestpath]}'") - end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo("mkdir -p #{guest_path}") end def self.rsync_post(machine, opts) @@ -21,8 +22,10 @@ module VagrantPlugins return end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo( - "find '#{opts[:guestpath]}' '(' ! -user #{opts[:owner]} -or ! -group #{opts[:group]} ')' -print0 | " + + "find #{guest_path} '(' ! -user #{opts[:owner]} -or ! -group #{opts[:group]} ')' -print0 | " + "xargs -0 chown #{opts[:owner]}:#{opts[:group]}") end end From c4a0a86ee0511f9f466105855420073095aeda78 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 25 Jun 2016 15:51:58 -0400 Subject: [PATCH 30/39] guests/debian: Do not restart networking Restarting networking causes Vagrant to disconnect and fail. --- plugins/guests/debian/cap/change_host_name.rb | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index 7d34c7b90..a1ce9195a 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -2,22 +2,23 @@ module VagrantPlugins module GuestDebian module Cap class ChangeHostName - # For more information, please see: - # - # https://wiki.debian.org/HowTo/ChangeHostname - # def self.change_host_name(machine, name) comm = machine.communicate if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, '') + # Ensure exit on command error set -e # Set the hostname echo '#{basename}' > /etc/hostname hostname -F /etc/hostname + if command -v hostnamectl; then + hostnamectl set-hostname '#{basename}' + fi + # Remove comments and blank lines from /etc/hosts sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts @@ -29,17 +30,13 @@ module VagrantPlugins # Update mailname echo '#{name}' > /etc/mailname - # Restart networking and force new DHCP + # Restart hostname services + if test -f /etc/init.d/hostname; then + /etc/init.d/hostname start || true + fi + if test -f /etc/init.d/hostname.sh; then - invoke-rc.d hostname.sh start || true - fi - - if test -f /etc/init.d/networking; then - invoke-rc.d networking force-reload || true - fi - - if test -f /etc/init.d/network-manager; then - invoke-rc.d network-manager force-reload || true + /etc/init.d/hostname.sh start || true fi EOH end From 3b2ffae400700c8046a554d7bf1539ef5f546db5 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 25 Jun 2016 17:18:32 -0400 Subject: [PATCH 31/39] guests/bsd: Print a very descriptive error when mounting vbox folders BSD-based guests do not support VirtualBox shared folders. This is a common source of confusion in Vagrant. This new error clearly explains that this is not a bug in Vagrant and provides instructions on how to disable them. --- lib/vagrant/errors.rb | 4 ++++ plugins/guests/bsd/cap/virtualbox.rb | 16 ++++++++++++++++ plugins/guests/bsd/plugin.rb | 5 +++++ templates/locales/en.yml | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 plugins/guests/bsd/cap/virtualbox.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 786e52f33..a557f88d8 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -784,6 +784,10 @@ module Vagrant error_key(:virtualbox_mount_failed) end + class VirtualBoxMountNotSupportedBSD < VagrantError + error_key(:virtualbox_mount_not_supported_bsd) + end + class VirtualBoxNameExists < VagrantError error_key(:virtualbox_name_exists) end diff --git a/plugins/guests/bsd/cap/virtualbox.rb b/plugins/guests/bsd/cap/virtualbox.rb new file mode 100644 index 000000000..14406c534 --- /dev/null +++ b/plugins/guests/bsd/cap/virtualbox.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module GuestBSD + module Cap + class VirtualBox + # BSD-based guests do not currently support VirtualBox synced folders. + # Instead of raising an error about a missing capability, this defines + # the capability and then provides a more detailed error message, + # linking to sources on the Internet where the problem is + # better-described. + def self.mount_virtualbox_shared_folder(machine, name, guestpath, options) + raise Vagrant::Errors::VirtualBoxMountNotSupportedBSD + end + end + end + end +end diff --git a/plugins/guests/bsd/plugin.rb b/plugins/guests/bsd/plugin.rb index c83a3a454..368765cdf 100644 --- a/plugins/guests/bsd/plugin.rb +++ b/plugins/guests/bsd/plugin.rb @@ -26,6 +26,11 @@ module VagrantPlugins Cap::NFS end + guest_capability(:bsd, :mount_virtualbox_shared_folder) do + require_relative "cap/virtualbox" + Cap::VirtualBox + end + guest_capability(:bsd, :remove_public_key) do require_relative "cap/public_key" Cap::PublicKey diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 249206838..61cee6032 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1355,6 +1355,25 @@ en: The error output from the command was: %{output} + virtualbox_mount_not_supported_bsd: |- + Vagrant is not able to mount VirtualBox shared folders on BSD-based + guests. BSD-based guests do not support the VirtualBox filesystem at + this time. + + To change the type of the default synced folder, specify the type as + rsync or nfs: + + config.vm.synced_folder ".", "/vagrant", type: "nfs" # or "rsync" + + Alternatively, if you do not need to mount the default synced folder, + you can also disable it entirely: + + config.vm.synced_folder ".", "/vagrant", disabled: true + + You can read more about Vagrant's synced folder types and the various + configuration options on the Vagrant website. + + This is not a bug in Vagrant. virtualbox_name_exists: |- The name of your virtual machine couldn't be set because VirtualBox is reporting another VM with that name already exists. Most of the From 72dcf610f7b27de859b426909f74abd205b3589c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sat, 25 Jun 2016 17:18:53 -0400 Subject: [PATCH 32/39] guests/freebsd: Escape guest path in rsync folders --- plugins/guests/freebsd/cap/rsync.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/guests/freebsd/cap/rsync.rb b/plugins/guests/freebsd/cap/rsync.rb index 5bcffc347..6064a8a1d 100644 --- a/plugins/guests/freebsd/cap/rsync.rb +++ b/plugins/guests/freebsd/cap/rsync.rb @@ -1,3 +1,5 @@ +require "shellwords" + module VagrantPlugins module GuestFreeBSD module Cap @@ -15,9 +17,8 @@ module VagrantPlugins end def self.rsync_pre(machine, opts) - machine.communicate.tap do |comm| - comm.sudo("mkdir -p '#{opts[:guestpath]}'") - end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo("mkdir -p #{guest_path}") end def self.rsync_post(machine, opts) @@ -25,8 +26,10 @@ module VagrantPlugins return end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo( - "find '#{opts[:guestpath]}' '(' ! -user #{opts[:owner]} -or ! -group #{opts[:group]} ')' -print0 | " + + "find #{guest_path} '(' ! -user #{opts[:owner]} -or ! -group #{opts[:group]} ')' -print0 | " + "xargs -0 -r chown #{opts[:owner]}:#{opts[:group]}") end end From 8fe47b79c249e26e73b4f5fdbb9e13daf2613637 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 26 Jun 2016 11:40:41 -0400 Subject: [PATCH 33/39] guests/gentoo: Exit on hostname error --- plugins/guests/gentoo/cap/change_host_name.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/guests/gentoo/cap/change_host_name.rb b/plugins/guests/gentoo/cap/change_host_name.rb index f1f08a55c..7d8ca3ac5 100644 --- a/plugins/guests/gentoo/cap/change_host_name.rb +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -8,6 +8,8 @@ module VagrantPlugins if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) basename = name.split(".", 2)[0] comm.sudo <<-EOH.gsub(/^ {14}/, "") + set -e + # Set the hostname hostname '#{basename}' echo "hostname=#{basename}" > /etc/conf.d/hostname From e2becda89d391f6b88b5a267ccff29f4752c3ecb Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 26 Jun 2016 11:41:16 -0400 Subject: [PATCH 34/39] guests/gentoo: Use shared cap for listing network interfaces --- plugins/guests/gentoo/cap/configure_networks.rb | 6 +----- plugins/guests/linux/cap/network_interfaces.rb | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index ab27a58e3..38b18501f 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -12,15 +12,11 @@ module VagrantPlugins comm = machine.communicate commands = [] - interfaces = [] + interfaces = machine.guest.capability(:network_interfaces, "/bin/ip") # Remove any previous network additions to the configuration file. commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net" - comm.sudo("ifconfig -a | grep -o ^[0-9a-z]* | grep -v '^lo'") do |_, stdout| - interfaces = stdout.split("\n") - end - networks.each_with_index do |network, i| network[:device] = interfaces[network[:interface]] diff --git a/plugins/guests/linux/cap/network_interfaces.rb b/plugins/guests/linux/cap/network_interfaces.rb index fd1480aa6..3360fde78 100644 --- a/plugins/guests/linux/cap/network_interfaces.rb +++ b/plugins/guests/linux/cap/network_interfaces.rb @@ -7,9 +7,9 @@ module VagrantPlugins # eth0\nenp0s8\nenp0s9 # # @return [Array] - def self.network_interfaces(machine) + def self.network_interfaces(machine, path = "/sbin/ip") s = "" - machine.communicate.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |type, data| + machine.communicate.sudo("#{path} -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |type, data| s << data if type == :stdout end s.split("\n") From 1c0d0b8b9dc7c53dd13524eddf1c6cf4301b71c9 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 13 Jul 2016 10:01:17 -0600 Subject: [PATCH 35/39] guests/linux: Style update for choose_addressable_ip --- plugins/guests/linux/cap/choose_addressable_ip_addr.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/guests/linux/cap/choose_addressable_ip_addr.rb b/plugins/guests/linux/cap/choose_addressable_ip_addr.rb index 2ae94185d..2d889ee54 100644 --- a/plugins/guests/linux/cap/choose_addressable_ip_addr.rb +++ b/plugins/guests/linux/cap/choose_addressable_ip_addr.rb @@ -6,13 +6,12 @@ module VagrantPlugins comm = machine.communicate possible.each do |ip| - command = "ping -c1 -w1 -W1 #{ip}" - if comm.test(command) + if comm.test("ping -c1 -w1 -W1 #{ip}") return ip end end - nil + return nil end end end From 7f1947fec26400792ac7860e079d2e9e7d5e98ff Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 13 Jul 2016 10:02:09 -0600 Subject: [PATCH 36/39] folders/rsync: Shellescape guest paths This allows users to have spaces or other special characters in destination paths on the guest. --- plugins/guests/linux/cap/rsync.rb | 11 +++++++---- plugins/synced_folders/rsync/helper.rb | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/guests/linux/cap/rsync.rb b/plugins/guests/linux/cap/rsync.rb index 424d20f46..bd3d6f9b1 100644 --- a/plugins/guests/linux/cap/rsync.rb +++ b/plugins/guests/linux/cap/rsync.rb @@ -1,3 +1,5 @@ +require "shellwords" + module VagrantPlugins module GuestLinux module Cap @@ -11,9 +13,8 @@ module VagrantPlugins end def self.rsync_pre(machine, opts) - machine.communicate.tap do |comm| - comm.sudo("mkdir -p '#{opts[:guestpath]}'") - end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo("mkdir -p #{guest_path}") end def self.rsync_post(machine, opts) @@ -21,8 +22,10 @@ module VagrantPlugins return end + guest_path = Shellwords.escape(opts[:guestpath]) + machine.communicate.sudo( - "find '#{opts[:guestpath]}' " + + "find #{guest_path} " + "'!' -type l -a " + "'(' ! -user #{opts[:owner]} -or ! -group #{opts[:group]} ')' -print0 | " + "xargs -0 -r chown #{opts[:owner]}:#{opts[:group]}") diff --git a/plugins/synced_folders/rsync/helper.rb b/plugins/synced_folders/rsync/helper.rb index aa6cb710b..46d7643c5 100644 --- a/plugins/synced_folders/rsync/helper.rb +++ b/plugins/synced_folders/rsync/helper.rb @@ -1,3 +1,5 @@ +require "shellwords" + require "vagrant/util/platform" require "vagrant/util/subprocess" @@ -43,6 +45,9 @@ module VagrantPlugins guestpath = machine.guest.capability(:rsync_scrub_guestpath, opts) end + # Shellescape + guestpath = Shellwords.escape(guestpath) + if Vagrant::Util::Platform.windows? # rsync for Windows expects cygwin style paths, always. hostpath = Vagrant::Util::Platform.cygwin_path(hostpath) From 1e0d7bb652d5de73a3ee8b2aa1fe3b19bd5269e0 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Wed, 13 Jul 2016 10:02:41 -0600 Subject: [PATCH 37/39] guests/ubuntu: Use debians change_host_name functionality --- plugins/guests/ubuntu/cap/change_host_name.rb | 52 ------------------- plugins/guests/ubuntu/plugin.rb | 5 -- 2 files changed, 57 deletions(-) delete mode 100644 plugins/guests/ubuntu/cap/change_host_name.rb diff --git a/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb deleted file mode 100644 index ffaec34f4..000000000 --- a/plugins/guests/ubuntu/cap/change_host_name.rb +++ /dev/null @@ -1,52 +0,0 @@ -module VagrantPlugins - module GuestUbuntu - module Cap - class ChangeHostName - def self.change_host_name(machine, name) - comm = machine.communicate - - if !comm.test("hostname -f | grep '^#{name}$'", sudo: false) - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Set the hostname - echo '#{basename}' > /etc/hostname - hostname -F /etc/hostname - - if command -v hostnamectl; then - hostnamectl set-hostname '#{basename}' - fi - - # Remove comments and blank lines from /etc/hosts - sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts - - # Prepend ourselves to /etc/hosts - grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts - } - - # Update mailname - echo '#{name}' > /etc/mailname - - # Restart networking and force new DHCP - if [ test -f /etc/init.d/hostname ]; then - /etc/init.d/hostname start || true - fi - - if [ test -f /etc/init.d/hostname.sh ]; then - /etc/init.d/hostname.sh start || true - fi - - if [ test -f /etc/init.d/networking ]; then - /etc/init.d/networking force-reload - fi - - if [ test -f /etc/init.d/network-manager ]; then - /etc/init.d/network-manager force-reload - fi - EOH - end - end - end - end - end -end diff --git a/plugins/guests/ubuntu/plugin.rb b/plugins/guests/ubuntu/plugin.rb index d83e9ed58..3147bdd8f 100644 --- a/plugins/guests/ubuntu/plugin.rb +++ b/plugins/guests/ubuntu/plugin.rb @@ -10,11 +10,6 @@ module VagrantPlugins require_relative "guest" Guest end - - guest_capability(:ubuntu, :change_host_name) do - require_relative "cap/change_host_name" - Cap::ChangeHostName - end end end end From 7868421a2157f3bfd220f72452d6762df15803a0 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 18 Jul 2016 21:12:37 -0400 Subject: [PATCH 38/39] guests/openbsd: Fix hostname setting --- .../guests/openbsd/cap/change_host_name.rb | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/plugins/guests/openbsd/cap/change_host_name.rb b/plugins/guests/openbsd/cap/change_host_name.rb index d47b659c8..20f879d81 100644 --- a/plugins/guests/openbsd/cap/change_host_name.rb +++ b/plugins/guests/openbsd/cap/change_host_name.rb @@ -3,9 +3,27 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - if !machine.communicate.test("hostname | grep '^#{name}$'") - machine.communicate.sudo("sh -c \"echo '#{name}' > /etc/myname\"") - machine.communicate.sudo("hostname #{name}") + comm = machine.communicate + + if !comm.test("hostname -f | grep '^#{name}$'", sudo: false, shell: "sh") + basename = name.split(".", 2)[0] + command = <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + hostname '#{name}' + sed -i'' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf + echo '#{name}' > /etc/myname + + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' /etc/hosts + sed -i'' -e '/^$/d' /etc/hosts + + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts + mv /tmp/tmp-hosts /etc/hosts + } + EOH + comm.sudo(command, shell: "sh") end end end From 184999051798cafda685d23268e4deb7ecef8890 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 18 Jul 2016 21:31:25 -0400 Subject: [PATCH 39/39] Fix tests --- .../coreos/cap/change_host_name_test.rb | 10 +-- .../darwin/cap/change_host_name_test.rb | 10 +-- .../debian/cap/change_host_name_test.rb | 4 +- .../plugins/guests/debian/cap/rsync_test.rb | 14 +--- .../omnios/cap/change_host_name_test.rb | 4 +- .../photon/cap/change_host_name_test.rb | 4 +- .../guests/pld/cap/change_host_name_test.rb | 4 +- .../redhat/cap/change_host_name_test.rb | 7 +- .../redhat/cap/configure_networks_test.rb | 14 ---- .../slackware/cap/change_host_name_test.rb | 4 +- .../guests/suse/cap/change_host_name_test.rb | 4 +- .../ubuntu/cap/change_host_name_test.rb | 42 ----------- .../guests/arch/network_static_test.rb | 6 +- .../guests/fedora/network_dhcp_test.rb | 21 ------ .../guests/fedora/network_static6_test.rb | 25 ------- .../guests/fedora/network_static_test.rb | 71 ------------------- .../guests/gentoo/network_dhcp_test.rb | 4 +- .../guests/gentoo/network_static_test.rb | 20 +++--- .../guests/redhat/network_dhcp_test.rb | 4 +- .../guests/redhat/network_static_test.rb | 18 ++--- 20 files changed, 55 insertions(+), 235 deletions(-) delete mode 100644 test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb delete mode 100644 test/unit/templates/guests/fedora/network_dhcp_test.rb delete mode 100644 test/unit/templates/guests/fedora/network_static6_test.rb delete mode 100644 test/unit/templates/guests/fedora/network_static_test.rb diff --git a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb index e29d52aa5..8d9603c2d 100644 --- a/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/coreos/cap/change_host_name_test.rb @@ -20,17 +20,17 @@ describe "VagrantPlugins::GuestCoreOS::Cap::ChangeHostName" do end describe ".change_host_name" do - let(:hostname) { "banana-rama.example.com" } + let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) comm.expect_command("hostname 'banana-rama'") - described_class.change_host_name(machine, hostname) + described_class.change_host_name(machine, name) end it "does not change the hostname if already set" do - comm.stub_command("hostname --fqdn | grep -w '#{hostname}'", exit_code: 0) - described_class.change_host_name(machine, hostname) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) + described_class.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end end diff --git a/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb index 8852817fa..e92479eaf 100644 --- a/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/darwin/cap/change_host_name_test.rb @@ -20,11 +20,11 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do end describe ".change_host_name" do - let(:hostname) { "banana-rama.example.com" } + let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname -f | grep -w '#{hostname}' || hostname -s | grep -w '#{hostname}'", exit_code: 1) - described_class.change_host_name(machine, hostname) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) + described_class.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/scutil --set ComputerName 'banana-rama.example.com'/) expect(comm.received_commands[1]).to match(/scutil --set HostName 'banana-rama.example.com'/) expect(comm.received_commands[1]).to match(/scutil --set LocalHostName 'banana-rama'/) @@ -32,8 +32,8 @@ describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do end it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep -w '#{hostname}' || hostname -s | grep -w '#{hostname}'", exit_code: 0) - described_class.change_host_name(machine, hostname) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) + described_class.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end end diff --git a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb index faa3b6935..dee94a67c 100644 --- a/test/unit/plugins/guests/debian/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/debian/cap/change_host_name_test.rb @@ -27,9 +27,7 @@ describe "VagrantPlugins::GuestDebian::Cap::ChangeHostName" do comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/hostname -F \/etc\/hostname/) - expect(comm.received_commands[1]).to match(/invoke-rc.d hostname.sh start/) - expect(comm.received_commands[1]).to match(/invoke-rc.d networking force-reload/) - expect(comm.received_commands[1]).to match(/invoke-rc.d network-manager force-reload/) + expect(comm.received_commands[1]).to match(/hostname.sh start/) end it "does not set the hostname if unset" do diff --git a/test/unit/plugins/guests/debian/cap/rsync_test.rb b/test/unit/plugins/guests/debian/cap/rsync_test.rb index 753a9ab16..9cf9dccab 100644 --- a/test/unit/plugins/guests/debian/cap/rsync_test.rb +++ b/test/unit/plugins/guests/debian/cap/rsync_test.rb @@ -20,19 +20,11 @@ describe "VagrantPlugins::GuestDebian::Cap:RSync" do end describe ".rsync_install" do - it "installs rsync when not installed" do - comm.stub_command("command -v rsync", exit_code: 1) + it "installs rsync" do described_class.rsync_install(machine) - expect(comm.received_commands[1]).to match(/apt-get -yqq update/) - expect(comm.received_commands[1]).to match(/apt-get -yqq install rsync/) - end - - it "does not install rsync when installed" do - comm.stub_command("command -v rsync", exit_code: 0) - described_class.rsync_install(machine) - - expect(comm.received_commands.join("")).to_not match(/update/) + expect(comm.received_commands[0]).to match(/apt-get -yqq update/) + expect(comm.received_commands[0]).to match(/apt-get -yqq install rsync/) end end end diff --git a/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb b/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb index 1ad9fb4c9..c4afed849 100644 --- a/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/omnios/cap/change_host_name_test.rb @@ -24,7 +24,7 @@ describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do let(:name) { "banana-rama.example.com" } it "sets the hostname if unset" do - comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/nodename/) @@ -32,7 +32,7 @@ describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do end it "does not set the hostname if set" do - comm.stub_command("hostname | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) diff --git a/test/unit/plugins/guests/photon/cap/change_host_name_test.rb b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb index 39634aec2..581c9bd77 100644 --- a/test/unit/plugins/guests/photon/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb @@ -26,7 +26,7 @@ describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/hostname/) @@ -34,7 +34,7 @@ describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do end it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/pld/cap/change_host_name_test.rb b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb index 75af68d6f..beb46bae8 100644 --- a/test/unit/plugins/guests/pld/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/pld/cap/change_host_name_test.rb @@ -24,14 +24,14 @@ describe "VagrantPlugins::GuestPld::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/hostname '#{name}'/) end it "does not change the hostname if already set" do - comm.stub_command("hostname | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb index 84da01b20..766293545 100644 --- a/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/redhat/cap/change_host_name_test.rb @@ -24,17 +24,18 @@ describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/) expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/) - expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{name}'/) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/) + expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/) expect(comm.received_commands[1]).to match(/service network restart/) end it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb index 284fe3580..1da032e88 100644 --- a/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/redhat/cap/configure_networks_test.rb @@ -49,20 +49,6 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do } end - it "uses fedora for rhel7 configuration" do - require_relative "../../../../../../plugins/guests/fedora/cap/configure_networks" - - allow(guest).to receive(:capability) - .with(:flavor) - .and_return(:rhel_7) - allow(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks) - .to receive(:configure_networks) - - expect(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks) - .to receive(:configure_networks).once - cap.configure_networks(machine, [network_1, network_2]) - end - it "creates and starts the networks" do allow(guest).to receive(:capability) .with(:flavor) diff --git a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb index 2c128fc61..e4c20ca0b 100644 --- a/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/slackware/cap/change_host_name_test.rb @@ -24,7 +24,7 @@ describe "VagrantPlugins::GuestSlackware::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/hostname/) @@ -32,7 +32,7 @@ describe "VagrantPlugins::GuestSlackware::Cap::ChangeHostName" do end it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb index 9001614d1..1ff565006 100644 --- a/test/unit/plugins/guests/suse/cap/change_host_name_test.rb +++ b/test/unit/plugins/guests/suse/cap/change_host_name_test.rb @@ -24,7 +24,7 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do let(:name) { "banana-rama.example.com" } it "sets the hostname" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1) cap.change_host_name(machine, name) expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/HOSTNAME/) @@ -32,7 +32,7 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do end it "does not change the hostname if already set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) + comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0) cap.change_host_name(machine, name) expect(comm.received_commands.size).to eq(1) end diff --git a/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb b/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb deleted file mode 100644 index d184ad9cb..000000000 --- a/test/unit/plugins/guests/ubuntu/cap/change_host_name_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -require_relative "../../../../base" - -describe "VagrantPlugins::GuestUbuntu::Cap::ChangeHostName" do - let(:caps) do - VagrantPlugins::GuestUbuntu::Plugin - .components - .guest_capabilities[:ubuntu] - end - - let(:machine) { double("machine") } - let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - - before do - allow(machine).to receive(:communicate).and_return(comm) - end - - after do - comm.verify_expectations! - end - - describe ".change_host_name" do - let(:cap) { caps.get(:change_host_name) } - - let(:name) { 'banana-rama.example.com' } - - it "sets the hostname if not set" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1) - cap.change_host_name(machine, name) - expect(comm.received_commands[1]).to match(/hostname -F \/etc\/hostname/) - expect(comm.received_commands[1]).to match(/\/etc\/init.d\/hostname.sh start/) - expect(comm.received_commands[1]).to match(/\/etc\/init.d\/hostname start/) - expect(comm.received_commands[1]).to match(/\/etc\/init.d\/networking force-reload/) - expect(comm.received_commands[1]).to match(/\/etc\/init.d\/network-manager force-reload/) - end - - it "does not set the hostname if unset" do - comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0) - cap.change_host_name(machine, name) - expect(comm.received_commands.size).to eq(1) - end - end -end diff --git a/test/unit/templates/guests/arch/network_static_test.rb b/test/unit/templates/guests/arch/network_static_test.rb index fb3251d3b..d6f156ebc 100644 --- a/test/unit/templates/guests/arch/network_static_test.rb +++ b/test/unit/templates/guests/arch/network_static_test.rb @@ -7,8 +7,9 @@ describe "templates/guests/arch/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "eth1", - ip: "1.1.1.1", + device: "eth1", + ip: "1.1.1.1", + netmask: "24", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") Connection=ethernet @@ -24,6 +25,7 @@ describe "templates/guests/arch/network_static" do device: "eth1", ip: "1.1.1.1", gateway: "1.2.3.4", + netmask: "24", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") Connection=ethernet diff --git a/test/unit/templates/guests/fedora/network_dhcp_test.rb b/test/unit/templates/guests/fedora/network_dhcp_test.rb deleted file mode 100644 index b257aa76e..000000000 --- a/test/unit/templates/guests/fedora/network_dhcp_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -require_relative "../../../base" - -require "vagrant/util/template_renderer" - -describe "templates/guests/fedora/network_dhcp" do - let(:template) { "guests/fedora/network_dhcp" } - - it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - }) - expect(result).to eq <<-EOH.gsub(/^ {6}/, "") - #VAGRANT-BEGIN - # The contents below are automatically generated by Vagrant. Do not modify. - BOOTPROTO=dhcp - ONBOOT=yes - DEVICE=en0 - #VAGRANT-END - EOH - end -end diff --git a/test/unit/templates/guests/fedora/network_static6_test.rb b/test/unit/templates/guests/fedora/network_static6_test.rb deleted file mode 100644 index bcd4076eb..000000000 --- a/test/unit/templates/guests/fedora/network_static6_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -require_relative "../../../base" - -require "vagrant/util/template_renderer" - -describe "templates/guests/fedora/network_static6" do - let(:template) { "guests/fedora/network_static6" } - - it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "fc00::10/64" - }) - expect(result).to eq <<-EOH.gsub(/^ {6}/, "") - #VAGRANT-BEGIN - # The contents below are automatically generated by Vagrant. Do not modify. - NM_CONTROLLED=no - BOOTPROTO=static - ONBOOT=yes - IPV6INIT=yes - IPV6ADDR=fc00::10/64 - DEVICE=en0 - #VAGRANT-END - EOH - end -end diff --git a/test/unit/templates/guests/fedora/network_static_test.rb b/test/unit/templates/guests/fedora/network_static_test.rb deleted file mode 100644 index f6e879fa2..000000000 --- a/test/unit/templates/guests/fedora/network_static_test.rb +++ /dev/null @@ -1,71 +0,0 @@ -require_relative "../../../base" - -require "vagrant/util/template_renderer" - -describe "templates/guests/fedora/network_static" do - let(:template) { "guests/fedora/network_static" } - - it "renders the template" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", - }) - expect(result).to eq <<-EOH.gsub(/^ {6}/, "") - #VAGRANT-BEGIN - # The contents below are automatically generated by Vagrant. Do not modify. - NM_CONTROLLED=no - BOOTPROTO=none - ONBOOT=yes - IPADDR=1.1.1.1 - NETMASK=255.255.0.0 - DEVICE=en0 - PEERDNS=no - #VAGRANT-END - EOH - end - - it "includes the gateway" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", - gateway: "1.2.3.4", - }) - expect(result).to eq <<-EOH.gsub(/^ {6}/, "") - #VAGRANT-BEGIN - # The contents below are automatically generated by Vagrant. Do not modify. - NM_CONTROLLED=no - BOOTPROTO=none - ONBOOT=yes - IPADDR=1.1.1.1 - NETMASK=255.255.0.0 - DEVICE=en0 - GATEWAY=1.2.3.4 - PEERDNS=no - #VAGRANT-END - EOH - end - - it "includes the mac_address" do - result = Vagrant::Util::TemplateRenderer.render(template, options: { - device: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", - mac_address: "abcd1234", - }) - expect(result).to eq <<-EOH.gsub(/^ {6}/, "") - #VAGRANT-BEGIN - # The contents below are automatically generated by Vagrant. Do not modify. - NM_CONTROLLED=no - BOOTPROTO=none - ONBOOT=yes - IPADDR=1.1.1.1 - NETMASK=255.255.0.0 - DEVICE=en0 - HWADDR=abcd1234 - PEERDNS=no - #VAGRANT-END - EOH - end -end diff --git a/test/unit/templates/guests/gentoo/network_dhcp_test.rb b/test/unit/templates/guests/gentoo/network_dhcp_test.rb index 7885c2acc..8126c6d58 100644 --- a/test/unit/templates/guests/gentoo/network_dhcp_test.rb +++ b/test/unit/templates/guests/gentoo/network_dhcp_test.rb @@ -7,12 +7,12 @@ describe "templates/guests/gentoo/network_dhcp" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", + device: "en0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - config_ethen0="dhcp" + config_en0="dhcp" #VAGRANT-END EOH end diff --git a/test/unit/templates/guests/gentoo/network_static_test.rb b/test/unit/templates/guests/gentoo/network_static_test.rb index e1eb8ce1a..74c394480 100644 --- a/test/unit/templates/guests/gentoo/network_static_test.rb +++ b/test/unit/templates/guests/gentoo/network_static_test.rb @@ -7,30 +7,30 @@ describe "templates/guests/gentoo/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", + device: "en0", + ip: "1.1.1.1", + netmask: "255.255.0.0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - config_ethen0=("1.1.1.1 netmask 255.255.0.0") + config_en0=("1.1.1.1 netmask 255.255.0.0") #VAGRANT-END EOH end it "includes the gateway" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", - gateway: "1.2.3.4", + device: "en0", + ip: "1.1.1.1", + netmask: "255.255.0.0", + gateway: "1.2.3.4", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. - config_ethen0=("1.1.1.1 netmask 255.255.0.0") - gateways_ethen0="1.2.3.4" + config_en0=("1.1.1.1 netmask 255.255.0.0") + gateways_en0="1.2.3.4" #VAGRANT-END EOH end diff --git a/test/unit/templates/guests/redhat/network_dhcp_test.rb b/test/unit/templates/guests/redhat/network_dhcp_test.rb index 280678a2f..505869c28 100644 --- a/test/unit/templates/guests/redhat/network_dhcp_test.rb +++ b/test/unit/templates/guests/redhat/network_dhcp_test.rb @@ -7,14 +7,14 @@ describe "templates/guests/redhat/network_dhcp" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", + device: "en0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN # The contents below are automatically generated by Vagrant. Do not modify. BOOTPROTO=dhcp ONBOOT=yes - DEVICE=ethen0 + DEVICE=en0 #VAGRANT-END EOH end diff --git a/test/unit/templates/guests/redhat/network_static_test.rb b/test/unit/templates/guests/redhat/network_static_test.rb index 443ec2d56..6260878ea 100644 --- a/test/unit/templates/guests/redhat/network_static_test.rb +++ b/test/unit/templates/guests/redhat/network_static_test.rb @@ -7,9 +7,9 @@ describe "templates/guests/redhat/network_static" do it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", - ip: "1.1.1.1", - netmask: "255.255.0.0", + device: "en0", + ip: "1.1.1.1", + netmask: "255.255.0.0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN @@ -19,7 +19,7 @@ describe "templates/guests/redhat/network_static" do ONBOOT=yes IPADDR=1.1.1.1 NETMASK=255.255.0.0 - DEVICE=ethen0 + DEVICE=en0 PEERDNS=no #VAGRANT-END EOH @@ -27,10 +27,10 @@ describe "templates/guests/redhat/network_static" do it "includes the gateway" do result = Vagrant::Util::TemplateRenderer.render(template, options: { - interface: "en0", - ip: "1.1.1.1", - gateway: "1.2.3.4", - netmask: "255.255.0.0", + device: "en0", + ip: "1.1.1.1", + gateway: "1.2.3.4", + netmask: "255.255.0.0", }) expect(result).to eq <<-EOH.gsub(/^ {6}/, "") #VAGRANT-BEGIN @@ -40,7 +40,7 @@ describe "templates/guests/redhat/network_static" do ONBOOT=yes IPADDR=1.1.1.1 NETMASK=255.255.0.0 - DEVICE=ethen0 + DEVICE=en0 GATEWAY=1.2.3.4 PEERDNS=no #VAGRANT-END