vagrant-proxyconf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ /.bundle
2
+ /Gemfile.lock
3
+ /pkg
4
+ /tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # 0.1.0 / 2013-06-27
2
+
3
+ - Initial release
4
+ - Support for Apt proxy configuration
5
+ - Based heavily on [vagrant-cachier](https://github.com/fgrehm/vagrant-cachier) plugin
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'rspec', '~> 2.11'
7
+ gem 'tailor', '~> 1.2'
8
+ gem 'vagrant', github: 'mitchellh/vagrant'
9
+
10
+ group :development do
11
+ gem 'guard-rspec'
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/unit/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013 Teemu Matilainen <teemu.matilainen@iki.fi>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Vagrant Proxy Configuration Plugin
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/vagrant-proxyconf.png)][gem]
4
+ [![Build Status](https://travis-ci.org/tmatilai/vagrant-proxyconf.png?branch=master)][travis]
5
+ [![Dependency Status](https://gemnasium.com/tmatilai/vagrant-proxyconf.png)][gemnasium]
6
+ [![Code Climate](https://codeclimate.com/github/tmatilai/vagrant-proxyconf.png)][codeclimate]
7
+
8
+ [gem]: https://rubygems.org/gems/vagrant-proxyconf
9
+ [travis]: https://travis-ci.org/tmatilai/vagrant-proxyconf
10
+ [gemnasium]: https://gemnasium.com/tmatilai/vagrant-proxyconf
11
+ [codeclimate]: https://codeclimate.com/github/tmatilai/vagrant-proxyconf
12
+
13
+ A [Vagrant](http://www.vagrantup.com/) 1.1+ plugin that configures the virtual machine to use specified proxies for package managers etc.
14
+
15
+ At this state we support:
16
+
17
+ - [APT](http://en.wikipedia.org/wiki/Advanced_Packaging_Tool) proxy/cacher
18
+
19
+ Support is planned for other package managers (at least yum).
20
+
21
+ ## Usage
22
+
23
+ Install using standard Vagrant 1.1+ plugin installation method:
24
+ ```sh
25
+ vagrant plugin install vagrant-proxyconf
26
+ ```
27
+
28
+ ### Apt
29
+
30
+ The proxy for Apt can be specified in the Vagrantfile:
31
+ ```ruby
32
+ Vagrant.configure("2") do |config|
33
+
34
+ config.apt_proxy.http = "192.168.33.1:3142"
35
+
36
+ # ... other stuff
37
+ end
38
+ ```
39
+
40
+ The proxy can be specified as an IP address, name or full URL, with optional port (defaults to 3142).
41
+
42
+ You can also use `APT_PROXY_HTTP` and `APT_PROXY_HTTPS` environment variables. These override the Vagrantfile configuration. To disable or remove the proxy use "DIRECT" or an empty value.
43
+
44
+ Proxy settings will be written to _/etc/apt/apt.conf.d/01proxy_ on the guest.
45
+
46
+ ## Related plugins and projects
47
+
48
+ - [vagrant-cachier](https://github.com/fgrehm/vagrant-cachier)
49
+ - [vagrant-proxy](https://github.com/clintoncwolfe/vagrant-proxy)
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'tailor/rake_task'
4
+
5
+ task :default => [:test, :tailor]
6
+
7
+ # Remove 'install' task as the gem is installed to Vagrant, not to system
8
+ Rake::Task[:install].clear
9
+
10
+ namespace :test do
11
+ RSpec::Core::RakeTask.new('unit') do |task|
12
+ task.pattern = 'spec/unit/**/*_spec.rb'
13
+ end
14
+ end
15
+ desc "Run all tests"
16
+ task :test => ['test:unit']
17
+
18
+ Tailor::RakeTask.new do |task|
19
+ task.file_set('lib/**/*.rb', 'code') do |style|
20
+ style.max_line_length 100, level: :warn
21
+ style.max_line_length 140, level: :error
22
+ end
23
+ task.file_set('spec/**/*.rb', 'tests') do |style|
24
+ style.max_line_length 120, level: :warn
25
+ # allow vertical alignment of `let(:foo) { block }` blocks
26
+ style.spaces_before_lbrace 1, level: :off
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ require 'vagrant'
2
+
3
+ require_relative 'vagrant-proxyconf/version'
4
+ require_relative 'vagrant-proxyconf/plugin'
@@ -0,0 +1 @@
1
+ require_relative 'action/configure_apt_proxy'
@@ -0,0 +1,53 @@
1
+ require 'tempfile'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Action
6
+ class ConfigureAptProxy
7
+ attr_reader :logger
8
+
9
+ def initialize(app, env)
10
+ @app = app
11
+ @logger = Log4r::Logger.new('vagrant::proxyconf::action::configure_apt_proxy')
12
+ end
13
+
14
+ def call(env)
15
+ @app.call env
16
+
17
+ proxy_config = env[:machine].config.apt_proxy
18
+ if !proxy_config.enabled?
19
+ logger.debug "apt_proxy not enabled or configured"
20
+ elsif !proxy_conf_capability?(env[:machine])
21
+ env[:ui].info "Skipping Apt proxy config as the machine does not support it"
22
+ else
23
+ env[:ui].info "Configuring proxy for Apt..."
24
+ write_apt_proxy_conf(env[:machine], proxy_config)
25
+ end
26
+ end
27
+
28
+ def write_apt_proxy_conf(machine, config)
29
+ logger.debug "Configuration:\n#{config}"
30
+
31
+ temp = Tempfile.new("vagrant")
32
+ temp.binmode
33
+ temp.write(config)
34
+ temp.close
35
+
36
+ machine.communicate.tap do |comm|
37
+ comm.upload(temp.path, "/tmp/vagrant-apt-proxy-conf")
38
+ comm.sudo("cat /tmp/vagrant-apt-proxy-conf > #{proxy_conf_path(machine)}")
39
+ comm.sudo("rm /tmp/vagrant-apt-proxy-conf")
40
+ end
41
+ end
42
+
43
+ def proxy_conf_capability?(machine)
44
+ machine.guest.capability?(:apt_proxy_conf)
45
+ end
46
+
47
+ def proxy_conf_path(machine)
48
+ machine.guest.capability(:apt_proxy_conf)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,88 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class AptProxyConfig < Vagrant.plugin('2', :config)
6
+ # HTTP proxy for Apt
7
+ attr_accessor :http
8
+
9
+ # HTTPS proxy for Apt
10
+ attr_accessor :https
11
+
12
+ def initialize
13
+ @http = UNSET_VALUE
14
+ @https = UNSET_VALUE
15
+ end
16
+
17
+ def finalize!
18
+ @http = override_from_env_var('http', @http)
19
+ @http = nil if @http == UNSET_VALUE
20
+
21
+ @https = override_from_env_var('https', @https)
22
+ @https = nil if @https == UNSET_VALUE
23
+ end
24
+
25
+ def enabled?
26
+ !http.nil? || !https.nil?
27
+ end
28
+
29
+ # @return [String] the full configuration stanza
30
+ def to_s
31
+ "#{config_for('http')}#{config_for('https')}"
32
+ end
33
+
34
+ private
35
+
36
+ def override_from_env_var(proto, default)
37
+ ENV.fetch("APT_PROXY_#{proto.upcase}", default)
38
+ end
39
+
40
+ def config_for(proto)
41
+ ConfigValue.new(proto, send(proto.to_sym))
42
+ end
43
+
44
+ class ConfigValue
45
+
46
+ attr_reader :proto, :value
47
+
48
+ # @param proto [String] the protocol ("http", "https")
49
+ # @param value [Object] the configuration value
50
+ def initialize(proto, value)
51
+ @proto = proto
52
+ @value = value
53
+ end
54
+
55
+ # @return [String] the full Apt configuration line
56
+ def to_s
57
+ set? ? %Q{Acquire::#{proto}::Proxy "#{proxy_uri}";\n} : ""
58
+ end
59
+
60
+ private
61
+
62
+ def set?
63
+ value && !value.empty?
64
+ end
65
+
66
+ def direct?
67
+ value.upcase == "DIRECT"
68
+ end
69
+
70
+ def proxy_uri
71
+ direct? ? "DIRECT" : "#{prefix}#{value}#{suffix}"
72
+ end
73
+
74
+ def prefix
75
+ "#{proto}://" if value !~ %r{^.*://}
76
+ end
77
+
78
+ def suffix
79
+ ":#{default_port}" if value !~ %r{:\d+$}
80
+ end
81
+
82
+ def default_port
83
+ 3142
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,13 @@
1
+ module VagrantPlugins
2
+ module ProxyConf
3
+ module Cap
4
+ module Debian
5
+ module AptProxyConf
6
+ def self.apt_proxy_conf(machine)
7
+ '/etc/apt/apt.conf.d/01proxy'
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Plugin < Vagrant.plugin('2')
6
+ name 'vagrant-proxyconf'
7
+
8
+ config('apt_proxy') do
9
+ require_relative 'apt_proxy_config'
10
+ AptProxyConfig
11
+ end
12
+
13
+ guest_capability 'debian', 'apt_proxy_conf' do
14
+ require_relative 'cap/debian/apt_proxy_conf'
15
+ Cap::Debian::AptProxyConf
16
+ end
17
+
18
+ proxyconf_action_hook = lambda do |hook|
19
+ require_relative 'action'
20
+ hook.after Vagrant::Action::Builtin::Provision, Action::ConfigureAptProxy
21
+ end
22
+ action_hook 'proxyconf-machine-up', :machine_action_up, &proxyconf_action_hook
23
+ action_hook 'proxyconf-machine-reload', :machine_action_reload, &proxyconf_action_hook
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module ProxyConf
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = :expect
4
+ end
5
+ config.color = true
6
+ config.tty = true
7
+ end
8
+
9
+ require 'tempfile'
10
+ require 'vagrant-proxyconf'
@@ -0,0 +1,103 @@
1
+ def config_with(options)
2
+ instance.tap do |c|
3
+ options.each_pair { |k, v| c.send("#{k}=".to_sym, v) }
4
+ c.finalize!
5
+ end
6
+ end
7
+
8
+ def conf_line(proto, name, port = 3142)
9
+ if name == :direct
10
+ %Q{Acquire::#{proto}::Proxy "DIRECT";\n}
11
+ else
12
+ %Q{Acquire::#{proto}::Proxy "#{proto}://#{name}:#{port}";\n}
13
+ end
14
+ end
15
+
16
+ def conf_line_pattern(proto, name, port = 3142)
17
+ "^#{Regexp.escape(conf_line(proto, name, port))}"
18
+ end
19
+
20
+ shared_examples "apt proxy config" do |proto|
21
+ context "#{proto} proxy" do
22
+
23
+ context "with ip" do
24
+ subject { config_with(proto => "10.1.2.3") }
25
+ its(:enabled?) { should be_true }
26
+ its(:to_s) { should eq conf_line(proto, "10.1.2.3") }
27
+ end
28
+
29
+ context "with name" do
30
+ subject { config_with(proto => "proxy.example.com") }
31
+ its(:enabled?) { should be_true }
32
+ its(:to_s) { should eq conf_line(proto, "proxy.example.com") }
33
+ end
34
+
35
+ context "with name and port" do
36
+ subject { config_with(proto => "acng:8080") }
37
+ its(:enabled?) { should be_true }
38
+ its(:to_s) { should eq conf_line(proto, "acng", 8080) }
39
+ end
40
+
41
+ context "with protocol and name" do
42
+ subject { config_with(proto => "#{proto}://proxy.foo.tld") }
43
+ its(:enabled?) { should be_true }
44
+ its(:to_s) { should eq conf_line(proto, "proxy.foo.tld") }
45
+ end
46
+
47
+ context "with protocol and name and port" do
48
+ subject { config_with(proto => "#{proto}://prism.nsa.gov:666") }
49
+ its(:enabled?) { should be_true }
50
+ its(:to_s) { should eq conf_line(proto, "prism.nsa.gov", 666) }
51
+ end
52
+
53
+ ["DIRECT", "direct"].each do |direct|
54
+ context "with #{direct.inspect}" do
55
+ subject { config_with(proto => direct) }
56
+ its(:enabled?) { should be_true }
57
+ its(:to_s) { should eq conf_line(proto, :direct) }
58
+ end
59
+ end
60
+
61
+ [false, ""].each do |unset|
62
+ context "with #{unset.inspect}" do
63
+ subject { config_with(proto => unset) }
64
+ its(:enabled?) { should be_true }
65
+ its(:to_s) { should eq "" }
66
+ end
67
+ end
68
+
69
+ context "with nil" do
70
+ subject { config_with(proto => nil) }
71
+ its(:enabled?) { should be_false }
72
+ its(:to_s) { should eq "" }
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ shared_examples "apt proxy env var" do |var, proto|
79
+ context var do
80
+ let(:conf) { config_with(http: "acng:8080", https: "ssl-proxy:8443") }
81
+
82
+ it "sets #{proto} proxy" do
83
+ ENV[var] = "myproxy"
84
+ expect(conf.to_s).to match conf_line_pattern(proto, "myproxy")
85
+ end
86
+
87
+ it "does not set other proxies" do
88
+ ENV[var] = "myproxy:2345"
89
+ conf = config_with({})
90
+ expect(conf.to_s).to eq conf_line(proto, "myproxy", 2345)
91
+ end
92
+
93
+ it "sets empty configuration" do
94
+ ENV[var] = ""
95
+ expect(conf.to_s).to_not match %r{#{proto}://}
96
+ end
97
+
98
+ it "sets direct configuration" do
99
+ ENV[var] = "direct"
100
+ expect(conf.to_s).to match conf_line_pattern(proto, :direct)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'unit/support/shared/apt_proxy_config'
3
+ require 'vagrant-proxyconf/apt_proxy_config'
4
+
5
+ describe VagrantPlugins::ProxyConf::AptProxyConfig do
6
+ let(:instance) { described_class.new }
7
+
8
+ before :each do
9
+ # Ensure tests are not affected by environment variables
10
+ %w[APT_PROXY_HTTP APT_PROXY_HTTPS].each { |k| ENV.delete(k) }
11
+ end
12
+
13
+ context "defaults" do
14
+ subject { config_with({}) }
15
+ its(:enabled?) { should be_false }
16
+ its(:to_s) { should eq "" }
17
+ end
18
+
19
+ include_examples "apt proxy config", "http"
20
+ include_examples "apt proxy config", "https"
21
+
22
+ context "with both http and https proxies" do
23
+ subject { config_with(http: "10.2.3.4", https: "ssl-proxy:8443") }
24
+ its(:enabled?) { should be_true }
25
+ its(:to_s) { should match conf_line_pattern("http", "10.2.3.4") }
26
+ its(:to_s) { should match conf_line_pattern("https", "ssl-proxy", 8443) }
27
+ end
28
+
29
+ context "with env var" do
30
+ include_examples "apt proxy env var", "APT_PROXY_HTTP", "http"
31
+ include_examples "apt proxy env var", "APT_PROXY_HTTPS", "https"
32
+ end
33
+
34
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-proxyconf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-proxyconf"
8
+ spec.version = VagrantPlugins::ProxyConf::VERSION
9
+ spec.authors = ["Teemu Matilainen"]
10
+ spec.email = ["teemu.matilainen@iki.fi"]
11
+ spec.description = "A Vagrant Plugin that configures the virtual machine to use proxies"
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/tmatilai/vagrant-proxyconf"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-proxyconf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Teemu Matilainen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Vagrant Plugin that configures the virtual machine to use proxies
15
+ email:
16
+ - teemu.matilainen@iki.fi
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .travis.yml
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Guardfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - lib/vagrant-proxyconf.rb
30
+ - lib/vagrant-proxyconf/action.rb
31
+ - lib/vagrant-proxyconf/action/configure_apt_proxy.rb
32
+ - lib/vagrant-proxyconf/apt_proxy_config.rb
33
+ - lib/vagrant-proxyconf/cap/debian/apt_proxy_conf.rb
34
+ - lib/vagrant-proxyconf/plugin.rb
35
+ - lib/vagrant-proxyconf/version.rb
36
+ - spec/spec_helper.rb
37
+ - spec/unit/support/shared/apt_proxy_config.rb
38
+ - spec/unit/vagrant-proxyconf/apt_proxy_config_spec.rb
39
+ - vagrant-proxyconf.gemspec
40
+ homepage: https://github.com/tmatilai/vagrant-proxyconf
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: -4265757349911692365
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: -4265757349911692365
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A Vagrant Plugin that configures the virtual machine to use proxies
71
+ test_files:
72
+ - spec/spec_helper.rb
73
+ - spec/unit/support/shared/apt_proxy_config.rb
74
+ - spec/unit/vagrant-proxyconf/apt_proxy_config_spec.rb