diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 944bdd5f8..a557f88d8 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -780,6 +780,14 @@ module Vagrant error_key(:virtualbox_no_name) end + class VirtualBoxMountFailed < VagrantError + 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/arch/cap/change_host_name.rb b/plugins/guests/arch/cap/change_host_name.rb index fc3ec4e3e..0b10112e1 100644 --- a/plugins/guests/arch/cap/change_host_name.rb +++ b/plugins/guests/arch/cap/change_host_name.rb @@ -5,19 +5,22 @@ 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 -hostnamectl set-hostname '#{name}' + comm.sudo <<-EOH.gsub(/^ {14}/, "") + set -e -# Remove comments and blank lines from /etc/hosts -sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts + # Set hostname + hostnamectl set-hostname '#{basename}' -# Prepend ourselves to /etc/hosts -grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts -} -EOH + # 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 diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 9915b4a32..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" @@ -11,18 +13,19 @@ module VagrantPlugins def self.configure_networks(machine, networks) 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 + commands = ["set -e"] + interfaces = machine.guest.capability(:network_interfaces) 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/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/guests/atomic/cap/change_host_name.rb b/plugins/guests/atomic/cap/change_host_name.rb index 70e936321..2c501c8dd 100644 --- a/plugins/guests/atomic/cap/change_host_name.rb +++ b/plugins/guests/atomic/cap/change_host_name.rb @@ -5,19 +5,22 @@ 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 -hostnamectl set-hostname '#{name}' + comm.sudo <<-EOH.gsub(/^ {14}/, "") + set -e -# Remove comments and blank lines from /etc/hosts -sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts + # Set hostname + hostnamectl set-hostname '#{basename}' -# Prepend ourselves to /etc/hosts -grep -w '#{name}' /etc/hosts || { - sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts -} -EOH + # 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 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. 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/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..5d5902d9e 100644 --- a/plugins/guests/darwin/cap/change_host_name.rb +++ b/plugins/guests/darwin/cap/change_host_name.rb @@ -5,10 +5,13 @@ 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}/, '') + set -e + + # Set hostname scutil --set ComputerName '#{name}' scutil --set HostName '#{name}' 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 diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index a80d05516..a1ce9195a 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -2,20 +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 -w '#{name}'") + 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 '#{name}' > /etc/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 @@ -27,17 +30,13 @@ module VagrantPlugins # Update mailname 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 + # Restart hostname services + if test -f /etc/init.d/hostname; then + /etc/init.d/hostname start || true fi - if [ test -f /etc/init.d/networking ]; then - invoke-rc.d networking force-reload - fi - - if [ test -f /etc/init.d/network-manager ]; then - invoke-rc.d network-manager force-reload + if test -f /etc/init.d/hostname.sh; then + /etc/init.d/hostname.sh start || true fi EOH end diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index e7fabcb68..2553516a8 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -11,13 +11,9 @@ module VagrantPlugins def self.configure_networks(machine, networks) comm = machine.communicate - commands = [] + commands = ["set -e"] 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/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/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 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 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 82e1da99a..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 | 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}' - -# 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 e4a7e1f19..000000000 --- a/plugins/guests/fedora/cap/configure_networks.rb +++ /dev/null @@ -1,135 +0,0 @@ -require "set" -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) - network_scripts_dir = machine.guest.capability("network_scripts_dir") - - 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 - end - - if virtual - machine.communicate.sudo("ls -v /sys/class/net | egrep -v lo\\|docker") do |_, result| - interface_names = result.split("\n") - end - - 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. - entry = TemplateRenderer.render("guests/fedora/network_#{network[:type]}", - options: network) - - 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}") - end - 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 - 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/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index fc194d6d9..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 -w '#{name}' || hostname -s | grep -w '#{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/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 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..7d8ca3ac5 100644 --- a/plugins/guests/gentoo/cap/change_host_name.rb +++ b/plugins/guests/gentoo/cap/change_host_name.rb @@ -3,12 +3,27 @@ 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}$'", 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 + + # 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/plugins/guests/gentoo/cap/configure_networks.rb b/plugins/guests/gentoo/cap/configure_networks.rb index 1d4a26b56..38b18501f 100644 --- a/plugins/guests/gentoo/cap/configure_networks.rb +++ b/plugins/guests/gentoo/cap/configure_networks.rb @@ -9,34 +9,43 @@ 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 = machine.guest.capability(:network_interfaces, "/bin/ip") - # 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") + 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/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 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/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/plugins/guests/linux/cap/network_interfaces.rb b/plugins/guests/linux/cap/network_interfaces.rb new file mode 100644 index 000000000..3360fde78 --- /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, path = "/sbin/ip") + s = "" + 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") + end + end + end + end +end 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/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/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/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index 285c3ffdb..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 @@ -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 @@ -46,9 +46,14 @@ 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 + require_relative "cap/nfs" + Cap::NFS end # For the Docker provider @@ -63,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 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/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 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..afe461d4f 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 @@ -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 8a117f3d7..6cbc9ec17 100644 --- a/plugins/guests/redhat/cap/configure_networks.rb +++ b/plugins/guests/redhat/cap/configure_networks.rb @@ -1,43 +1,20 @@ 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) - 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/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/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/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/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/plugins/guests/ubuntu/cap/change_host_name.rb b/plugins/guests/ubuntu/cap/change_host_name.rb deleted file mode 100644 index bbcada3e7..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 -w '#{name}'") - basename = name.split(".", 2)[0] - comm.sudo <<-EOH.gsub(/^ {14}/, '') - # Set the hostname - echo '#{name}' > /etc/hostname - hostname -F /etc/hostname - - if command -v hostnamectl; then - hostnamectl set-hostname '#{name}' - 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 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) 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) 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 new file mode 100644 index 000000000..e64ffd5ca --- /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 +Address6=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> +Gateway6='<%= options[:gateway] %>' +<% end -%> 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/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/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 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 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..000ea6150 100644 --- a/templates/guests/redhat/network_static.erb +++ b/templates/guests/redhat/network_static.erb @@ -5,9 +5,12 @@ BOOTPROTO=none ONBOOT=yes IPADDR=<%= options[:ip] %> NETMASK=<%= options[:netmask] %> -DEVICE=eth<%= options[:interface] %> +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/redhat/network_static6.erb similarity index 59% rename from templates/guests/fedora/network_static6.erb rename to templates/guests/redhat/network_static6.erb index 9ca7821d8..275c97508 100644 --- a/templates/guests/fedora/network_static6.erb +++ b/templates/guests/redhat/network_static6.erb @@ -3,7 +3,10 @@ NM_CONTROLLED=no BOOTPROTO=static ONBOOT=yes -IPV6INIT=yes -IPV6ADDR=<%= options[:ip] %> DEVICE=<%= options[:device] %> +IPV6INIT=yes +IPV6ADDR=<%= options[:ip] %>/<%= options[:netmask] %> +<% if options[:gateway] -%> +IPV6_DEFAULTGW=<%= options[:gateway] %> +<% end %> #VAGRANT-END diff --git a/templates/locales/en.yml b/templates/locales/en.yml index d3fa33e13..61cee6032 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1342,6 +1342,38 @@ 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_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 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..bab7a7cb3 100644 --- a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb @@ -1,20 +1,18 @@ 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") } + 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 @@ -22,6 +20,13 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do end 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, @@ -40,16 +45,16 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do end it "creates and starts the networks" do - described_class.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'/) + cap.configure_networks(machine, [network_1, network_2]) + 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/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/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 726b2926b..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 @@ -24,16 +24,14 @@ 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/) - 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 - 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/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/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/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 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 56050a893..1da032e88 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,41 +49,16 @@ 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) - .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(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/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/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/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/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 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