2013-03-27 00:45:20 +00:00
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
module Action
|
|
|
|
class ClearForwardedPorts
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::lxc::action::clear_forwarded_ports")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
|
|
|
|
if redir_pids.any?
|
|
|
|
env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
|
|
|
|
redir_pids.each do |pid|
|
2014-04-29 03:02:16 +00:00
|
|
|
next unless is_redir_pid?(pid[0])
|
|
|
|
@logger.debug "Killing pid #{pid[0]}"
|
2014-05-04 02:28:04 +00:00
|
|
|
if pid[1]
|
|
|
|
system "sudo pkill -TERM -P #{pid[0]}"
|
|
|
|
else
|
|
|
|
system "pkill -TERM -P #{pid[0]}"
|
|
|
|
end
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
|
2013-09-21 04:32:15 +00:00
|
|
|
@logger.info "Removing redir pids files"
|
2013-03-27 00:45:20 +00:00
|
|
|
remove_redir_pids
|
2013-09-21 04:32:15 +00:00
|
|
|
else
|
|
|
|
@logger.info "No redir pids found"
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@app.call env
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def redir_pids
|
|
|
|
@redir_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].map do |file|
|
2014-04-29 03:02:16 +00:00
|
|
|
port_number = file.split(/[^\d]/).join
|
|
|
|
privileged_port = true if Integer(port_number) > 1024
|
|
|
|
a = [ File.read(file).strip.chomp , privileged_port ]
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_redir_pid?(pid)
|
2013-09-21 04:32:15 +00:00
|
|
|
@logger.debug "Checking if #{pid} is a redir process with `ps -o cmd= #{pid}`"
|
2013-03-27 00:45:20 +00:00
|
|
|
`ps -o cmd= #{pid}`.strip.chomp =~ /redir/
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_redir_pids
|
|
|
|
Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].each do |file|
|
|
|
|
File.delete file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|