Merge pull request #190 from purpleidea/feat/sudo-forward

Forward SSH ports <= 1024 using sudo.
This commit is contained in:
Dmitry Vasilets 2014-06-25 08:24:59 +02:00
commit 0f0ce68ba5

View File

@ -3,6 +3,8 @@ module VagrantPlugins
module Action module Action
# Adds support for vagrant's `forward_ports` configuration directive. # Adds support for vagrant's `forward_ports` configuration directive.
class ForwardPorts class ForwardPorts
@@lock = Mutex.new
def initialize(app, env) def initialize(app, env)
@app = app @app = app
@logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports') @logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports')
@ -95,7 +97,20 @@ module VagrantPlugins
"IdentityFile=#{pk}" "IdentityFile=#{pk}"
end).map { |s| s.prepend('-o ') }.join(' ') end).map { |s| s.prepend('-o ') }.join(' ')
ssh_cmd = "ssh #{options} #{params}" # TODO: instead of this, try and lock and get the stdin from spawn...
ssh_cmd = ''
if host_port <= 1024
@@lock.synchronize do
# TODO: add i18n
@env[:ui].info 'Requesting sudo for host port(s) <= 1024'
r = system('sudo -v')
if r
ssh_cmd << 'sudo ' # add sudo prefix
end
end
end
ssh_cmd << "ssh #{options} #{params}"
@logger.debug "Forwarding port with `#{ssh_cmd}`" @logger.debug "Forwarding port with `#{ssh_cmd}`"
spawn(ssh_cmd, [:out, :err] => '/dev/null') spawn(ssh_cmd, [:out, :err] => '/dev/null')
@ -119,6 +134,8 @@ module VagrantPlugins
module Action module Action
# Cleans up ssh-forwarded ports on VM halt/destroy. # Cleans up ssh-forwarded ports on VM halt/destroy.
class ClearForwardedPorts class ClearForwardedPorts
@@lock = Mutex.new
def initialize(app, env) def initialize(app, env)
@app = app @app = app
@logger = Log4r::Logger.new( @logger = Log4r::Logger.new(
@ -133,10 +150,19 @@ module VagrantPlugins
env[:ui].info I18n.t( env[:ui].info I18n.t(
'vagrant.actions.vm.clear_forward_ports.deleting' 'vagrant.actions.vm.clear_forward_ports.deleting'
) )
ssh_pids.each do |pid| ssh_pids.each do |tag|
next unless ssh_pid?(pid) next unless ssh_pid?(tag[:pid])
@logger.debug "Killing pid #{pid}" @logger.debug "Killing pid #{tag[:pid]}"
system "kill #{pid}" kill_cmd = ''
if tag[:port] <= 1024
kill_cmd << 'sudo ' # add sudo prefix
end
kill_cmd << "kill #{tag[:pid]}"
@@lock.synchronize do
system(kill_cmd)
end
end end
@logger.info 'Removing ssh pid files' @logger.info 'Removing ssh pid files'
@ -153,7 +179,10 @@ module VagrantPlugins
def ssh_pids def ssh_pids
glob = @env[:machine].data_dir.join('pids').to_s + '/ssh_*.pid' glob = @env[:machine].data_dir.join('pids').to_s + '/ssh_*.pid'
@ssh_pids = Dir[glob].map do |file| @ssh_pids = Dir[glob].map do |file|
File.read(file).strip.chomp {
:pid => File.read(file).strip.chomp,
:port => File.basename(file)['ssh_'.length..-1*('.pid'.length+1)].to_i
}
end end
end end