Skip to content

Commit

Permalink
Add setting for disabling the plugin
Browse files Browse the repository at this point in the history
The plugin can be totally skipped by setting `config.proxy.enabled` to
`false` or empty string (`""`). This can be useful to for example
disable it for some provider.
  • Loading branch information
tmatilai committed Dec 2, 2013
1 parent a670053 commit 3025558
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -107,6 +107,23 @@ For example to spin up a VM, run:
VAGRANT_HTTP_PROXY="http://proxy.example.com:8080" vagrant up
```

### Disabling the plugin

The plugin can be totally skipped by setting `config.proxy.enabled` to `false` or empty string (`""`). This can be useful to for example disable it for some provider.

#### Example Vagrantfile

```ruby
Vagrant.configure("2") do |config|
config.proxy.http = "http://192.168.0.2:3128/"

config.vm.provider :my_cloud do |cloud, override|
override.proxy.enabled = false
end
# ... other stuff
end
```

### Global `*_proxy` environment variables

Many programs (wget, curl, yum, etc.) can be configured to use proxies with `http_proxy` or `HTTP_PROXY` etc. environment variables. This configuration will be written to _/etc/profile.d/proxy.sh_ on the guest.
Expand Down
14 changes: 10 additions & 4 deletions lib/vagrant-proxyconf/action.rb
@@ -1,7 +1,9 @@
require 'vagrant/action/builtin/call'
require_relative 'action/configure_apt_proxy'
require_relative 'action/configure_chef_proxy'
require_relative 'action/configure_env_proxy'
require_relative 'action/configure_yum_proxy'
require_relative 'action/is_enabled'
require_relative 'action/only_once'

module VagrantPlugins
Expand All @@ -22,10 +24,14 @@ def self.configure(opts = {})
# middleware builder
def self.config_actions
@actions ||= Proc.new do |builder|
builder.use ConfigureAptProxy
builder.use ConfigureChefProxy
builder.use ConfigureEnvProxy
builder.use ConfigureYumProxy
builder.use Vagrant::Action::Builtin::Call, IsEnabled do |env, b2|
next if !env[:result]

b2.use ConfigureAptProxy
b2.use ConfigureChefProxy
b2.use ConfigureEnvProxy
b2.use ConfigureYumProxy
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/vagrant-proxyconf/action/is_enabled.rb
@@ -0,0 +1,24 @@
module VagrantPlugins
module ProxyConf
class Action
# Action which checks if the plugin should be enabled
class IsEnabled
def initialize(app, env)
@app = app
end

def call(env)
env[:result] = plugin_enabled?(env[:machine].config.proxy)

@app.call(env)
end

private

def plugin_enabled?(config)
config.enabled != false && config.enabled != ''
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/vagrant-proxyconf/config/proxy.rb
Expand Up @@ -11,6 +11,9 @@ class Proxy < Vagrant.plugin('2', :config)
include KeyMixin
# @!parse extend KeyMixin::ClassMethods

# Defines the mode of the plugin
key :enabled, env_var: 'VAGRANT_PROXY'

# @return [String] the HTTP proxy
key :http, env_var: 'VAGRANT_HTTP_PROXY'

Expand Down
39 changes: 39 additions & 0 deletions spec/unit/vagrant-proxyconf/action/is_enabled_spec.rb
@@ -0,0 +1,39 @@
require 'spec_helper'
require 'vagrant-proxyconf/action/is_enabled'

describe VagrantPlugins::ProxyConf::Action::IsEnabled do
let(:app) { lambda { |env| } }
let(:env) { { :machine => machine } }
let(:machine) do
double('machine').tap { |machine| machine.stub(:config).and_return(config) }
end
let(:config) do
double('config').tap { |config| config.stub(:proxy => proxy_config) }
end
let(:proxy_config) do
double('proxy_config').tap { |config| config.stub(:enabled => enabled) }
end

[false, ''].each do |value|
context "with `config.proxy.enabled=#{value.inspect}`" do
let(:enabled) { value }

it "results to falsy" do
described_class.new(app, {}).call(env)
expect(env[:result]).to be_false
end
end
end

[nil, true, :auto, 'yes please'].each do |value|
context "with `config.proxy.enabled=#{value.inspect}`" do
let(:enabled) { value }

it "results to truthy" do
described_class.new(app, {}).call(env)
expect(env[:result]).to be_true
end
end
end

end

0 comments on commit 3025558

Please sign in to comment.