Merge branch 'sudo-for-redir' of https://github.com/guerremdq/vagrant-lxc
This commit is contained in:
commit
ee63d2b2ae
4 changed files with 94 additions and 8 deletions
|
@ -13,9 +13,13 @@ module Vagrant
|
||||||
if redir_pids.any?
|
if redir_pids.any?
|
||||||
env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
|
env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
|
||||||
redir_pids.each do |pid|
|
redir_pids.each do |pid|
|
||||||
next unless is_redir_pid?(pid)
|
next unless is_redir_pid?(pid[0])
|
||||||
@logger.debug "Killing pid #{pid}"
|
@logger.debug "Killing pid #{pid[0]}"
|
||||||
system "pkill -TERM -P #{pid}"
|
if pid[1]
|
||||||
|
system "sudo pkill -TERM -P #{pid[0]}"
|
||||||
|
else
|
||||||
|
system "pkill -TERM -P #{pid[0]}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@logger.info "Removing redir pids files"
|
@logger.info "Removing redir pids files"
|
||||||
|
@ -31,7 +35,9 @@ module Vagrant
|
||||||
|
|
||||||
def redir_pids
|
def redir_pids
|
||||||
@redir_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].map do |file|
|
@redir_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].map do |file|
|
||||||
File.read(file).strip.chomp
|
port_number = file.split(/[^\d]/).join
|
||||||
|
privileged_port = true if Integer(port_number) > 1024
|
||||||
|
a = [ File.read(file).strip.chomp , privileged_port ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -79,8 +79,11 @@ module Vagrant
|
||||||
params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} )
|
params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} )
|
||||||
params.unshift "--laddr=#{host_ip}" if host_ip
|
params.unshift "--laddr=#{host_ip}" if host_ip
|
||||||
params << '--syslog' if ENV['REDIR_LOG']
|
params << '--syslog' if ENV['REDIR_LOG']
|
||||||
redir_cmd = "redir #{params.join(' ')} 2>/dev/null"
|
if host_port < 1024
|
||||||
|
redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null"
|
||||||
|
else
|
||||||
|
redir_cmd = "redir #{params.join(' ')} 2>/dev/null"
|
||||||
|
end
|
||||||
@logger.debug "Forwarding port with `#{redir_cmd}`"
|
@logger.debug "Forwarding port with `#{redir_cmd}`"
|
||||||
spawn redir_cmd
|
spawn redir_cmd
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,7 +29,7 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do
|
||||||
|
|
||||||
context 'with a valid redir pid' do
|
context 'with a valid redir pid' do
|
||||||
it 'kills known processes' do
|
it 'kills known processes' do
|
||||||
expect(subject).to have_received(:system).with("pkill -TERM -P #{pid}")
|
expect(subject).to have_received(:system).with("sudo pkill -TERM -P #{pid}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ describe Vagrant::LXC::Action::ClearForwardedPorts do
|
||||||
let(:pid_cmd) { 'sudo ls' }
|
let(:pid_cmd) { 'sudo ls' }
|
||||||
|
|
||||||
it 'does not kill the process' do
|
it 'does not kill the process' do
|
||||||
expect(subject).not_to have_received(:system).with("pkill -TERM -P #{pid}")
|
expect(subject).not_to have_received(:system).with("sudo pkill -TERM -P #{pid}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
77
spec/unit/action/sudo_forward_ports_spec.rb
Normal file
77
spec/unit/action/sudo_forward_ports_spec.rb
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
require 'unit_helper'
|
||||||
|
|
||||||
|
require 'tmpdir'
|
||||||
|
require 'vagrant-lxc/provider'
|
||||||
|
require 'vagrant-lxc/action/forward_ports'
|
||||||
|
|
||||||
|
describe Vagrant::LXC::Action::ForwardPorts do
|
||||||
|
let(:app) { double(:app, call: true) }
|
||||||
|
let(:env) { {machine: machine, ui: double(info: true, warn: true)} }
|
||||||
|
let(:machine) { double(:machine) }
|
||||||
|
let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
|
||||||
|
let(:provider) { double(Vagrant::LXC::Provider, ssh_info: {host: container_ip}) }
|
||||||
|
let(:host_ip) { '127.0.0.1' }
|
||||||
|
let(:host_port) { 80 }
|
||||||
|
let(:guest_port) { 80 }
|
||||||
|
let(:container_ip) { '10.0.1.234' }
|
||||||
|
let(:pid) { 'a-pid' }
|
||||||
|
let(:forward_conf) { {guest: guest_port, host: host_port, host_ip: host_ip} }
|
||||||
|
let(:networks) { [[:other_config, {}], [:forwarded_port, forward_conf]] }
|
||||||
|
|
||||||
|
subject { described_class.new(app, env) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
machine.stub_chain(:config, :vm, :networks).and_return(networks)
|
||||||
|
machine.stub(provider: provider, data_dir: data_dir)
|
||||||
|
|
||||||
|
subject.stub(exec: true)
|
||||||
|
subject.stub(spawn: pid)
|
||||||
|
end
|
||||||
|
|
||||||
|
after { FileUtils.rm_rf data_dir.to_s }
|
||||||
|
|
||||||
|
it 'forwards ports using redir' do
|
||||||
|
subject.stub(system: true)
|
||||||
|
subject.call(env)
|
||||||
|
expect(subject).to have_received(:spawn).with(
|
||||||
|
"sudo redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'skips --laddr parameter if host_ip is nil' do
|
||||||
|
forward_conf.delete(:host_ip)
|
||||||
|
subject.stub(system: true)
|
||||||
|
subject.call(env)
|
||||||
|
expect(subject).to have_received(:spawn).with(
|
||||||
|
"sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'skips --laddr parameter if host_ip is a blank string' do
|
||||||
|
forward_conf[:host_ip] = ' '
|
||||||
|
subject.stub(system: true)
|
||||||
|
subject.call(env)
|
||||||
|
expect(subject).to have_received(:spawn).with(
|
||||||
|
"sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "stores redir pids on machine's data dir" do
|
||||||
|
subject.stub(system: true)
|
||||||
|
subject.call(env)
|
||||||
|
pid_file = data_dir.join('pids', "redir_#{host_port}.pid").read
|
||||||
|
expect(pid_file).to eq(pid)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'allows disabling a previously forwarded port' do
|
||||||
|
forward_conf[:disabled] = true
|
||||||
|
subject.stub(system: true)
|
||||||
|
subject.call(env)
|
||||||
|
expect(subject).not_to have_received(:spawn)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises RedirNotInstalled error if `redir` is not installed' do
|
||||||
|
subject.stub(system: false)
|
||||||
|
expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue