Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use absolute paths to commands when configuring them using sudo #60

Merged
merged 5 commits into from
Apr 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/vagrant-proxyconf/action/configure_git_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ def config
end

def configure_machine
command = "#{git_path} config --system "
if config.http
command = "git config --system http.proxy #{config.http}"
command << "http.proxy #{config.http}"
else
command = "git config --system --unset-all http.proxy"
command << "--unset-all http.proxy"
end
@machine.communicate.sudo(command)
end

def git_path
@machine.guest.capability(cap_name)
end
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/vagrant-proxyconf/action/configure_npm_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ def configure_machine
end

def set_or_delete_proxy(key, value)
command = "#{npm_path} config "
if value
command = "npm config set #{key} #{escape(value)}"
command << "set #{key} #{escape(value)}"
else
command = "npm config delete #{key}"
command << "delete #{key}"
end
@machine.communicate.sudo(command)
end

def npm_path
@machine.guest.capability(cap_name)
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/vagrant-proxyconf/action/configure_pear_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def configure_machine
proxy = config.http || ''

@machine.communicate.sudo(
"pear config-set http_proxy #{escape(proxy)} system")
"#{pear_path} config-set http_proxy #{escape(proxy)} system")
end

def pear_path
@machine.guest.capability(cap_name)
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require_relative '../util'

module VagrantPlugins
module ProxyConf
module Cap
module Linux
# Capability for Git command
# Capability for git proxy configuration
module GitProxyConf
# @return [String, false] the path to git or `false` if not found
def self.git_proxy_conf(machine)
machine.communicate.test('which git')
Util.which(machine, 'git')
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require_relative '../util'

module VagrantPlugins
module ProxyConf
module Cap
module Linux
# Capability for npm proxy configuration
module NpmProxyConf
# @return [Boolean] if npm is installed
# @return [String, false] the path to npm or `false` if not found
def self.npm_proxy_conf(machine)
machine.communicate.test('which npm', sudo: true)
Util.which(machine, 'npm')
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/vagrant-proxyconf/cap/linux/pear_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require_relative '../util'

module VagrantPlugins
module ProxyConf
module Cap
module Linux
# Capability for PEAR proxy configuration
module PearProxyConf
# @return [Boolean] if PEAR is installed
# @return [String, false] the path to pear or `false` if not found
def self.pear_proxy_conf(machine)
machine.communicate.test("which pear")
Util.which(machine, 'pear')
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require_relative '../util'

module VagrantPlugins
module ProxyConf
module Cap
module Linux
# Capability for Svn command
# Capability for svn proxy configuration
module SvnProxyConf
# @return [String, false] the path to svn or `false` if not found
def self.svn_proxy_conf(machine)
machine.communicate.test('which svn')
Util.which(machine, 'svn')
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/vagrant-proxyconf/cap/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module VagrantPlugins
module ProxyConf
module Cap
# Utility methods for capabilities
module Util
# Returns path to the command on the machine, or false if it's not found
def self.which(machine, cmd)
path = false
status = machine.communicate.execute(
"which #{cmd}", error_check: false) do |type, data|
# search for the command to work around `ssh.pty = true`
path = data.chomp if type == :stdout && data =~ /#{cmd}$/
end
status == 0 && path
end
end
end
end
end
17 changes: 6 additions & 11 deletions spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
require 'spec_helper'
require 'vagrant-proxyconf/cap/linux/git_proxy_conf'
require 'vagrant-proxyconf/cap/util'

describe VagrantPlugins::ProxyConf::Cap::Linux::GitProxyConf do

describe '.git_proxy_conf' do
let(:subject) { described_class.git_proxy_conf(double) }
let(:machine) { double }
let(:communicator) { double }

before do
machine.stub(:communicate => communicator)
it "returns the path when git is installed" do
VagrantPlugins::ProxyConf::Cap::Util.stub(which: '/path/to/git')
expect(described_class.git_proxy_conf(machine)).to eq '/path/to/git'
end

it "returns true when git is installed" do
expect(communicator).to receive(:test).with('which git').and_return(true)
expect(described_class.git_proxy_conf(machine)).to be_true
end

it "returns false when pear is not installed" do
expect(communicator).to receive(:test).with('which git').and_return(false)
it "returns false when git is not installed" do
VagrantPlugins::ProxyConf::Cap::Util.stub(which: false)
expect(described_class.git_proxy_conf(machine)).to be_false
end
end
Expand Down
16 changes: 5 additions & 11 deletions spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
require 'spec_helper'
require 'vagrant-proxyconf/cap/linux/npm_proxy_conf'
require 'vagrant-proxyconf/cap/util'

describe VagrantPlugins::ProxyConf::Cap::Linux::NpmProxyConf do

describe '.npm_proxy_conf' do
let(:machine) { double }
let(:communicator) { double }

before do
machine.stub(:communicate => communicator)
end

it "returns true when npm is installed" do
expect(communicator).
to receive(:test).with('which npm', sudo: true).and_return(true)
expect(described_class.npm_proxy_conf(machine)).to be_true
it "returns the path when npm is installed" do
VagrantPlugins::ProxyConf::Cap::Util.stub(which: '/path/to/npm')
expect(described_class.npm_proxy_conf(machine)).to eq '/path/to/npm'
end

it "returns false when npm is not installed" do
expect(communicator).
to receive(:test).with('which npm', sudo: true).and_return(false)
VagrantPlugins::ProxyConf::Cap::Util.stub(which: false)
expect(described_class.npm_proxy_conf(machine)).to be_false
end
end
Expand Down
14 changes: 5 additions & 9 deletions spec/unit/vagrant-proxyconf/cap/linux/pear_proxy_conf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
require 'spec_helper'
require 'vagrant-proxyconf/cap/linux/pear_proxy_conf'
require 'vagrant-proxyconf/cap/util'

describe VagrantPlugins::ProxyConf::Cap::Linux::PearProxyConf do

describe '.pear_proxy_conf' do
let(:machine) { double }
let(:communicator) { double }

before do
machine.stub(:communicate => communicator)
end

it "returns true when pear is installed" do
expect(communicator).to receive(:test).with("which pear").and_return(true)
expect(described_class.pear_proxy_conf(machine)).to be_true
it "returns the path when pear is installed" do
VagrantPlugins::ProxyConf::Cap::Util.stub(which: '/path/to/pear')
expect(described_class.pear_proxy_conf(machine)).to eq '/path/to/pear'
end

it "returns false when pear is not installed" do
expect(communicator).to receive(:test).with("which pear").and_return(false)
VagrantPlugins::ProxyConf::Cap::Util.stub(which: false)
expect(described_class.pear_proxy_conf(machine)).to be_false
end
end
Expand Down
13 changes: 4 additions & 9 deletions spec/unit/vagrant-proxyconf/cap/linux/svn_proxy_conf_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
require 'spec_helper'
require 'vagrant-proxyconf/cap/linux/svn_proxy_conf'
require 'vagrant-proxyconf/cap/util'

describe VagrantPlugins::ProxyConf::Cap::Linux::SvnProxyConf do

describe '.svn_proxy_conf' do
let(:subject) { described_class.svn_proxy_conf(double) }
let(:machine) { double }
let(:communicator) { double }

before do
machine.stub(:communicate => communicator)
end

it "returns true when svn is installed" do
expect(communicator).to receive(:test).with('which svn').and_return(true)
expect(described_class.svn_proxy_conf(machine)).to be_true
VagrantPlugins::ProxyConf::Cap::Util.stub(which: '/path/to/svn')
expect(described_class.svn_proxy_conf(machine)).to eq '/path/to/svn'
end

it "returns false when pear is not installed" do
expect(communicator).to receive(:test).with('which svn').and_return(false)
VagrantPlugins::ProxyConf::Cap::Util.stub(which: false)
expect(described_class.svn_proxy_conf(machine)).to be_false
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/unit/vagrant-proxyconf/cap/util_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'vagrant-proxyconf/cap/util'

describe VagrantPlugins::ProxyConf::Cap::Util do

describe '.which' do
let(:machine) { double }
let(:communicator) { double }

before do
machine.stub(:communicate => communicator)
end

it "returns the path when the command is installed" do
expect(communicator).to receive(:execute).
with('which foo', error_check: false).
and_yield(:stdout, "/path/to/foo\n").
and_yield(:stdout, "PROMPT\n").
and_yield(:stderr, "not foo\n").
and_return(0)
expect(described_class.which(machine, 'foo')).to eq '/path/to/foo'
end

it "returns false when the command is not installed" do
expect(communicator).to receive(:execute).
with('which bar', error_check: false).
and_return(1)
expect(described_class.which(machine, 'bar')).to be_false
end
end

end