Merge pull request #2529 from markpeek/markpeek-freebsd-nobashshell

guests/freebsd: Allow the FreeBSD plugin to install without bash [GH-2485]
This commit is contained in:
Mitchell Hashimoto 2013-11-23 11:04:29 -08:00
commit 33273ee561
6 changed files with 22 additions and 19 deletions

View File

@ -54,6 +54,7 @@ module VagrantPlugins
:error_class => Vagrant::Errors::VagrantError, :error_class => Vagrant::Errors::VagrantError,
:error_key => :ssh_bad_exit_status, :error_key => :ssh_bad_exit_status,
:command => command, :command => command,
:shell => nil,
:sudo => false :sudo => false
}.merge(opts || {}) }.merge(opts || {})
@ -61,7 +62,7 @@ module VagrantPlugins
stdout = "" stdout = ""
stderr = "" stderr = ""
exit_status = connect do |connection| 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 if type == :stdout
stdout += data stdout += data
elsif type == :stderr elsif type == :stderr
@ -273,18 +274,20 @@ module VagrantPlugins
end end
# Executes the command on an SSH connection within a login shell. # 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})") @logger.info("Execute: #{command} (sudo=#{sudo.inspect})")
exit_status = nil 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. # need to wrap the shell in a `sudo` call.
shell = @machine.config.ssh.shell shell_cmd = @machine.config.ssh.shell
shell = "sudo -H #{shell}" if sudo shell_cmd = shell if shell
shell_cmd = "sudo -H #{shell_cmd}" if sudo
# Open the channel so we can execute or command # Open the channel so we can execute or command
channel = connection.open_channel do |ch| 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 # Setup the channel callbacks so we can get data and exit status
ch2.on_data do |ch3, data| ch2.on_data do |ch3, data|
# Filter out the clear screen command # Filter out the clear screen command

View File

@ -3,9 +3,9 @@ module VagrantPlugins
module Cap module Cap
class ChangeHostName class ChangeHostName
def self.change_host_name(machine, name) def self.change_host_name(machine, name)
if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{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") machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf", {:shell => "sh"})
machine.communicate.sudo("hostname #{name}") machine.communicate.sudo("hostname #{name}", {:shell => "sh"})
end end
end end
end end

View File

@ -10,7 +10,7 @@ module VagrantPlugins
def self.configure_networks(machine, networks) def self.configure_networks(machine, networks)
# Remove any previous network additions to the configuration file. # 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| networks.each do |network|
entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}", entry = TemplateRenderer.render("guests/freebsd/network_#{network[:type]}",
@ -22,14 +22,14 @@ module VagrantPlugins
temp.write(entry) temp.write(entry)
temp.close temp.close
machine.communicate.upload(temp.path, "/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'") 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") machine.communicate.sudo("rm /tmp/vagrant-network-entry", {:shell => "sh"})
if network[:type].to_sym == :static 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 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 end
end end

View File

@ -4,7 +4,7 @@ module VagrantPlugins
class Halt class Halt
def self.halt(machine) def self.halt(machine)
begin begin
machine.communicate.sudo("shutdown -p now") machine.communicate.sudo("shutdown -p now", {:shell => "sh"})
rescue IOError rescue IOError
# Do nothing because SSH connection closed and it probably # Do nothing because SSH connection closed and it probably
# means the VM just shut down really fast. # means the VM just shut down really fast.

View File

@ -4,8 +4,8 @@ module VagrantPlugins
class MountNFSFolder class MountNFSFolder
def self.mount_nfs_folder(machine, ip, folders) def self.mount_nfs_folder(machine, ip, folders)
folders.each do |name, opts| folders.each do |name, opts|
machine.communicate.sudo("mkdir -p #{opts[:guestpath]}") machine.communicate.sudo("mkdir -p #{opts[:guestpath]}", {:shell => "sh"})
machine.communicate.sudo("mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") machine.communicate.sudo("mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {:shell => "sh"})
end end
end end
end end

View File

@ -7,7 +7,7 @@ module VagrantPlugins
# Contributed by Kenneth Vestergaard <kvs@binarysolutions.dk> # Contributed by Kenneth Vestergaard <kvs@binarysolutions.dk>
class Guest < Vagrant.plugin("2", :guest) class Guest < Vagrant.plugin("2", :guest)
def detect?(machine) def detect?(machine)
machine.communicate.test("uname -s | grep 'FreeBSD'") machine.communicate.test("uname -s | grep 'FreeBSD'", {:shell => "sh"})
end end
end end
end end