From d5dcf84d6270fb36b0fb4e8f3059a759ab7e20d6 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Fri, 22 Nov 2013 15:30:12 -0800 Subject: [PATCH] Allow the FreeBSD plugin to install without bash [GH-2485] The default shell is "bash -l" which does not get installed by default on FreeBSD. This change allows the plugin to override the default shell and use a known installed shell (sh). --- plugins/communicators/ssh/communicator.rb | 15 +++++++++------ plugins/guests/freebsd/cap/change_host_name.rb | 6 +++--- plugins/guests/freebsd/cap/configure_networks.rb | 12 ++++++------ plugins/guests/freebsd/cap/halt.rb | 2 +- plugins/guests/freebsd/cap/mount_nfs_folder.rb | 4 ++-- plugins/guests/freebsd/guest.rb | 2 +- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 6b12ebfa5..ac6589591 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -54,6 +54,7 @@ module VagrantPlugins :error_class => Vagrant::Errors::VagrantError, :error_key => :ssh_bad_exit_status, :command => command, + :shell => nil, :sudo => false }.merge(opts || {}) @@ -61,7 +62,7 @@ module VagrantPlugins stdout = "" stderr = "" exit_status = connect do |connection| - shell_execute(connection, command, opts[:sudo]) do |type, data| + shell_execute(connection, command, opts[:sudo], opts[:shell]) do |type, data| if type == :stdout stdout += data elsif type == :stderr @@ -273,18 +274,20 @@ module VagrantPlugins end # Executes the command on an SSH connection within a login shell. - def shell_execute(connection, command, sudo=false) + def shell_execute(connection, command, sudo=false, shell=nil) @logger.info("Execute: #{command} (sudo=#{sudo.inspect})") exit_status = nil - # Determine the shell to execute. If we are using `sudo` then we + # Determine the shell to execute. Prefer the explicitly passed in shell + # over the default configured shell. If we are using `sudo` then we # need to wrap the shell in a `sudo` call. - shell = @machine.config.ssh.shell - shell = "sudo -H #{shell}" if sudo + shell_cmd = @machine.config.ssh.shell + shell_cmd = shell if shell + shell_cmd = "sudo -H #{shell_cmd}" if sudo # Open the channel so we can execute or command channel = connection.open_channel do |ch| - ch.exec(shell) do |ch2, _| + ch.exec(shell_cmd) do |ch2, _| # Setup the channel callbacks so we can get data and exit status ch2.on_data do |ch3, data| # Filter out the clear screen command diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index 513a450bd..1f4423a22 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -3,9 +3,9 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'") - machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf") - machine.communicate.sudo("hostname #{name}") + if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'", {:shell => "sh"}) + machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf", {:shell => "sh"}) + machine.communicate.sudo("hostname #{name}", {:shell => "sh"}) end end end diff --git a/plugins/guests/freebsd/cap/configure_networks.rb b/plugins/guests/freebsd/cap/configure_networks.rb index a04266146..94ac9817e 100644 --- a/plugins/guests/freebsd/cap/configure_networks.rb +++ b/plugins/guests/freebsd/cap/configure_networks.rb @@ -10,7 +10,7 @@ module VagrantPlugins def self.configure_networks(machine, networks) # Remove any previous network additions to the configuration file. - machine.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf") + machine.communicate.sudo("sed -i '' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf", {:shell => "sh"}) networks.each do |network| entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", @@ -22,14 +22,14 @@ module VagrantPlugins temp.write(entry) temp.close - machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry") - machine.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'") - machine.communicate.sudo("rm /tmp/vagrant-network-entry") + machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry", {:shell => "sh"}) + machine.communicate.sudo("su -m root -c 'cat /tmp/vagrant-network-entry >> /etc/rc.conf'", {:shell => "sh"}) + machine.communicate.sudo("rm /tmp/vagrant-network-entry", {:shell => "sh"}) if network[:type].to_sym == :static - machine.communicate.sudo("ifconfig em#{network[:interface]} inet #{network[:ip]} netmask #{network[:netmask]}") + machine.communicate.sudo("ifconfig em#{network[:interface]} inet #{network[:ip]} netmask #{network[:netmask]}", {:shell => "sh"}) elsif network[:type].to_sym == :dhcp - machine.communicate.sudo("dhclient em#{network[:interface]}") + machine.communicate.sudo("dhclient em#{network[:interface]}", {:shell => "sh"}) end end end diff --git a/plugins/guests/freebsd/cap/halt.rb b/plugins/guests/freebsd/cap/halt.rb index 938add189..05437ed8d 100644 --- a/plugins/guests/freebsd/cap/halt.rb +++ b/plugins/guests/freebsd/cap/halt.rb @@ -4,7 +4,7 @@ module VagrantPlugins class Halt def self.halt(machine) begin - machine.communicate.sudo("shutdown -p now") + machine.communicate.sudo("shutdown -p now", {:shell => "sh"}) rescue IOError # Do nothing because SSH connection closed and it probably # means the VM just shut down really fast. diff --git a/plugins/guests/freebsd/cap/mount_nfs_folder.rb b/plugins/guests/freebsd/cap/mount_nfs_folder.rb index 6287d4a1e..8b314a684 100644 --- a/plugins/guests/freebsd/cap/mount_nfs_folder.rb +++ b/plugins/guests/freebsd/cap/mount_nfs_folder.rb @@ -4,8 +4,8 @@ module VagrantPlugins class MountNFSFolder def self.mount_nfs_folder(machine, ip, folders) folders.each do |name, opts| - machine.communicate.sudo("mkdir -p #{opts[:guestpath]}") - machine.communicate.sudo("mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") + machine.communicate.sudo("mkdir -p #{opts[:guestpath]}", {:shell => "sh"}) + machine.communicate.sudo("mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {:shell => "sh"}) end end end diff --git a/plugins/guests/freebsd/guest.rb b/plugins/guests/freebsd/guest.rb index fca8e9c27..3f8de663b 100644 --- a/plugins/guests/freebsd/guest.rb +++ b/plugins/guests/freebsd/guest.rb @@ -7,7 +7,7 @@ module VagrantPlugins # Contributed by Kenneth Vestergaard class Guest < Vagrant.plugin("2", :guest) def detect?(machine) - machine.communicate.test("uname -s | grep 'FreeBSD'") + machine.communicate.test("uname -s | grep 'FreeBSD'", {:shell => "sh"}) end end end