Skip to content

Commit

Permalink
Merge pull request #2529 from markpeek/markpeek-freebsd-nobashshell
Browse files Browse the repository at this point in the history
guests/freebsd: Allow the FreeBSD plugin to install without bash [GH-2485]
  • Loading branch information
mitchellh committed Nov 23, 2013
2 parents b02d0d5 + d5dcf84 commit 33273ee
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
15 changes: 9 additions & 6 deletions plugins/communicators/ssh/communicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ def execute(command, opts=nil, &block)
:error_class => Vagrant::Errors::VagrantError,
:error_key => :ssh_bad_exit_status,
:command => command,
:shell => nil,
:sudo => false
}.merge(opts || {})

# Connect via SSH and execute the command in the shell.
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
Expand Down Expand Up @@ -273,18 +274,20 @@ def connect
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
Expand Down
6 changes: 3 additions & 3 deletions plugins/guests/freebsd/cap/change_host_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module GuestFreeBSD
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
Expand Down
12 changes: 6 additions & 6 deletions plugins/guests/freebsd/cap/configure_networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ConfigureNetworks

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]}",
Expand All @@ -22,14 +22,14 @@ def self.configure_networks(machine, networks)
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
Expand Down
2 changes: 1 addition & 1 deletion plugins/guests/freebsd/cap/halt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Cap
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.
Expand Down
4 changes: 2 additions & 2 deletions plugins/guests/freebsd/cap/mount_nfs_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Cap
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
Expand Down
2 changes: 1 addition & 1 deletion plugins/guests/freebsd/guest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module GuestFreeBSD
# Contributed by Kenneth Vestergaard <[email protected]>
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
Expand Down

0 comments on commit 33273ee

Please sign in to comment.