From de10eb62ac740d410796a4ef06e3cb52417e377d Mon Sep 17 00:00:00 2001 From: John Bellone Date: Wed, 18 Dec 2013 10:33:53 -0500 Subject: [PATCH 1/2] Add support for Git proxy through system config. This adds support for overriding the HTTP proxy for Git by setting the system configuration. It uses the git command itself to write to the config file. --- lib/vagrant-proxyconf/action.rb | 2 + lib/vagrant-proxyconf/action/base.rb | 11 ++- .../action/configure_git_proxy.rb | 34 ++++++++ .../cap/linux/git_proxy_conf.rb | 14 +++ lib/vagrant-proxyconf/config/git_proxy.rb | 19 ++++ lib/vagrant-proxyconf/plugin.rb | 86 +++++++++++++++++++ locales/en.yml | 8 ++ .../action/configure_git_proxy_spec.rb | 11 +++ .../config/git_proxy_spec.rb | 7 ++ 9 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant-proxyconf/action/configure_git_proxy.rb create mode 100644 lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb create mode 100644 lib/vagrant-proxyconf/config/git_proxy.rb create mode 100644 spec/unit/vagrant-proxyconf/action/configure_git_proxy_spec.rb create mode 100644 spec/unit/vagrant-proxyconf/config/git_proxy_spec.rb diff --git a/lib/vagrant-proxyconf/action.rb b/lib/vagrant-proxyconf/action.rb index 4730b0a..27bf09f 100644 --- a/lib/vagrant-proxyconf/action.rb +++ b/lib/vagrant-proxyconf/action.rb @@ -4,6 +4,7 @@ require_relative 'action/configure_env_proxy' require_relative 'action/configure_pear_proxy' require_relative 'action/configure_yum_proxy' +require_relative 'action/configure_git_proxy' require_relative 'action/is_enabled' require_relative 'action/only_once' @@ -48,6 +49,7 @@ def self.config_actions b2.use ConfigureEnvProxy b2.use ConfigurePearProxy b2.use ConfigureYumProxy + b2.use ConfigureGitProxy end end end diff --git a/lib/vagrant-proxyconf/action/base.rb b/lib/vagrant-proxyconf/action/base.rb index 1d34384..28ef5af 100644 --- a/lib/vagrant-proxyconf/action/base.rb +++ b/lib/vagrant-proxyconf/action/base.rb @@ -84,7 +84,12 @@ def write_config(config, opts = {}) comm.sudo("chmod #{opts[:mode] || '0644'} #{tmp}") comm.sudo("chown #{opts[:owner] || 'root:root'} #{tmp}") comm.sudo("mkdir -p #{File.dirname(path)}") - comm.sudo("mv #{tmp} #{path}") + + if opts[:append] + comm.sudo("cat #{tmp} | tee -a #{path}") + else + comm.sudo("mv #{tmp} #{path}") + end end end @@ -113,6 +118,10 @@ def supported? def config_path @machine.guest.capability(cap_name) + + # @param value [String, nil] the string to escape for shell usage + def escape(value) + value.to_s.shellescape end end end diff --git a/lib/vagrant-proxyconf/action/configure_git_proxy.rb b/lib/vagrant-proxyconf/action/configure_git_proxy.rb new file mode 100644 index 0000000..2e15f8e --- /dev/null +++ b/lib/vagrant-proxyconf/action/configure_git_proxy.rb @@ -0,0 +1,34 @@ +require_relative 'base' +require_relative '../resource' +require_relative '../userinfo_uri' + +module VagrantPlugins + module ProxyConf + class Action + # Action for configuring Git on the guest + class ConfigureGitProxy < Base + def config_name + 'git_proxy' + end + + private + + def configure_machine(machine, config) + if machine.guest.capability(:git_proxy_conf) + machine.communicate.sudo("git config --system http.proxy #{config.http}") + else + write_config(machine, git_config(config), path: '/etc/gitconfig', append: true) + end + end + + def git_config(config) + (<<-CONFIG) + +[http] + proxy = "#{config.http}" + CONFIG + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb b/lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb new file mode 100644 index 0000000..7854f75 --- /dev/null +++ b/lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb @@ -0,0 +1,14 @@ +module VagrantPlugins + module ProxyConf + module Cap + module Linux + # Capability for Git command + module GitProxyConf + def self.git_proxy_conf(machine) + machine.communicate.test('sudo which git') + end + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/config/git_proxy.rb b/lib/vagrant-proxyconf/config/git_proxy.rb new file mode 100644 index 0000000..35332ab --- /dev/null +++ b/lib/vagrant-proxyconf/config/git_proxy.rb @@ -0,0 +1,19 @@ +require 'vagrant' +require_relative 'key_mixin' + +module VagrantPlugins + module ProxyConf + module Config + # Proxy configuration for Git + # + # @!parse class GitProxy < Vagrant::Plugin::V2::Config; end + class GitProxy < Vagrant.plugin('2', :config) + include KeyMixin + # @!parse extend KeyMixin::ClassMethods + + # @return [String] the HTTP proxy + key :http, env_var: 'VAGRANT_GIT_HTTP_PROXY' + end + end + end +end diff --git a/lib/vagrant-proxyconf/plugin.rb b/lib/vagrant-proxyconf/plugin.rb index a6d7a13..9f87c58 100644 --- a/lib/vagrant-proxyconf/plugin.rb +++ b/lib/vagrant-proxyconf/plugin.rb @@ -80,6 +80,92 @@ def self.load_optional_dependencies load_optional_dependencies name 'vagrant-proxyconf' + + config 'apt_proxy' do + require_relative 'config/apt_proxy' + Config::AptProxy + end + + config 'env_proxy' do + require_relative 'config/env_proxy' + Config::EnvProxy + end + + config 'git_proxy' do + require_relative 'config/git_proxy' + Config::GitProxy + end + + config 'proxy' do + require_relative 'config/proxy' + Config::Proxy + end + + config 'yum_proxy' do + require_relative 'config/yum_proxy' + Config::YumProxy + end + + guest_capability 'debian', 'apt_proxy_conf' do + require_relative 'cap/debian/apt_proxy_conf' + Cap::Debian::AptProxyConf + end + + guest_capability 'linux', 'env_proxy_conf' do + require_relative 'cap/linux/env_proxy_conf' + Cap::Linux::EnvProxyConf + end + + guest_capability 'linux', 'pear_proxy_conf' do + require_relative 'cap/linux/pear_proxy_conf' + Cap::Linux::PearProxyConf + end + + guest_capability 'linux', 'git_proxy_conf' do + require_relative 'cap/linux/git_proxy_conf' + Cap::Linux::GitProxyConf + end + + guest_capability 'coreos', 'env_proxy_conf' do + # disabled on CoreOS + end + + guest_capability 'redhat', 'yum_proxy_conf' do + require_relative 'cap/redhat/yum_proxy_conf' + Cap::Redhat::YumProxyConf + end + + action_hook 'proxyconf_configure' do |hook| + require_relative 'action' + + # the standard provision action + hook.after Vagrant::Action::Builtin::Provision, Action.configure + + # Vagrant 1.5+ can install NFS client + if check_vagrant_version('>= 1.5.0.dev') + hook.after Vagrant::Action::Builtin::SyncedFolders, Action.configure + end + + # vagrant-aws < 0.4.0 uses a non-standard provision action + if defined?(VagrantPlugins::AWS::Action::TimedProvision) + hook.after VagrantPlugins::AWS::Action::TimedProvision, Action.configure + end + + # configure the proxies before vagrant-omnibus + if defined?(VagrantPlugins::Omnibus::Action::InstallChef) + hook.after VagrantPlugins::Omnibus::Action::InstallChef, Action.configure + end + + # configure the proxies before vagrant-vbguest + if defined?(VagrantVbguest::Middleware) + hook.before VagrantVbguest::Middleware, Action.configure(before: true) + end + end + + action_hook 'proxyconf_configure', :provisioner_run do |hook| + require_relative 'action' + hook.append Action.configure_after_provisoner + end end end end diff --git a/locales/en.yml b/locales/en.yml index 4f77523..4115373 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -40,6 +40,14 @@ en: configuring: |- Configuring proxy for Yum... + git_proxy: + not_enabled: |- + git_proxy not enabled or configured + not_supported: |- + Skipping Git proxy config as the machine does not support it + configuring: |- + Configuring proxy for Git... + errors: vagrant_version: |- vagrant-proxyconf plugin requires Vagrant version %{requirement} diff --git a/spec/unit/vagrant-proxyconf/action/configure_git_proxy_spec.rb b/spec/unit/vagrant-proxyconf/action/configure_git_proxy_spec.rb new file mode 100644 index 0000000..b155434 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/action/configure_git_proxy_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +require 'vagrant-proxyconf/action/configure_git_proxy' + +describe VagrantPlugins::ProxyConf::Action::ConfigureGitProxy do + + describe '#config_name' do + subject { described_class.new(double, double).config_name } + it { should eq 'git_proxy' } + end + +end diff --git a/spec/unit/vagrant-proxyconf/config/git_proxy_spec.rb b/spec/unit/vagrant-proxyconf/config/git_proxy_spec.rb new file mode 100644 index 0000000..3999330 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/config/git_proxy_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' +require 'vagrant-proxyconf/config/git_proxy' + +describe VagrantPlugins::ProxyConf::Config::GitProxy do + let(:instance) { described_class.new } + before(:each) { ENV.delete('VAGRANT_GIT_HTTP_PROXY') } +end From 628fee76ed24af3916a05d115cd2dd3708760e8e Mon Sep 17 00:00:00 2001 From: John Bellone Date: Tue, 14 Jan 2014 17:36:57 -0500 Subject: [PATCH 2/2] Add support for Subversion HTTP proxies. I ran into this while attempting to build MRI 1.8.7 which seems to checkout from the old Subversion repository. At any rate unfortunately ti seems that Subversion itself doesn't have a `config` command so we're left with simply writing it directly to /etc/subversion/servers. --- lib/vagrant-proxyconf/action.rb | 2 + lib/vagrant-proxyconf/action/base.rb | 1 + .../action/configure_git_proxy.rb | 10 ++--- .../action/configure_svn_proxy.rb | 37 +++++++++++++++++++ .../cap/linux/svn_proxy_conf.rb | 14 +++++++ lib/vagrant-proxyconf/config/svn_proxy.rb | 19 ++++++++++ lib/vagrant-proxyconf/plugin.rb | 10 +++++ locales/en.yml | 8 ++++ .../action/configure_svn_proxy_spec.rb | 11 ++++++ .../cap/linux/git_proxy_conf.rb | 11 ++++++ .../config/svn_proxy_spec.rb | 7 ++++ 11 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 lib/vagrant-proxyconf/action/configure_svn_proxy.rb create mode 100644 lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb create mode 100644 lib/vagrant-proxyconf/config/svn_proxy.rb create mode 100644 spec/unit/vagrant-proxyconf/action/configure_svn_proxy_spec.rb create mode 100644 spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf.rb create mode 100644 spec/unit/vagrant-proxyconf/config/svn_proxy_spec.rb diff --git a/lib/vagrant-proxyconf/action.rb b/lib/vagrant-proxyconf/action.rb index 27bf09f..f93575d 100644 --- a/lib/vagrant-proxyconf/action.rb +++ b/lib/vagrant-proxyconf/action.rb @@ -5,6 +5,7 @@ require_relative 'action/configure_pear_proxy' require_relative 'action/configure_yum_proxy' require_relative 'action/configure_git_proxy' +require_relative 'action/configure_svn_proxy' require_relative 'action/is_enabled' require_relative 'action/only_once' @@ -50,6 +51,7 @@ def self.config_actions b2.use ConfigurePearProxy b2.use ConfigureYumProxy b2.use ConfigureGitProxy + b2.use ConfigureSvnProxy end end end diff --git a/lib/vagrant-proxyconf/action/base.rb b/lib/vagrant-proxyconf/action/base.rb index 28ef5af..dfa4676 100644 --- a/lib/vagrant-proxyconf/action/base.rb +++ b/lib/vagrant-proxyconf/action/base.rb @@ -118,6 +118,7 @@ def supported? def config_path @machine.guest.capability(cap_name) + end # @param value [String, nil] the string to escape for shell usage def escape(value) diff --git a/lib/vagrant-proxyconf/action/configure_git_proxy.rb b/lib/vagrant-proxyconf/action/configure_git_proxy.rb index 2e15f8e..e84fba5 100644 --- a/lib/vagrant-proxyconf/action/configure_git_proxy.rb +++ b/lib/vagrant-proxyconf/action/configure_git_proxy.rb @@ -13,15 +13,15 @@ def config_name private - def configure_machine(machine, config) - if machine.guest.capability(:git_proxy_conf) - machine.communicate.sudo("git config --system http.proxy #{config.http}") + def configure_machine + if @machine.guest.capability(:git_proxy_conf) + @machine.communicate.sudo("git config --system http.proxy #{config.http}") else - write_config(machine, git_config(config), path: '/etc/gitconfig', append: true) + write_config(git_config, path: '/etc/gitconfig', append: true) end end - def git_config(config) + def git_config (<<-CONFIG) [http] diff --git a/lib/vagrant-proxyconf/action/configure_svn_proxy.rb b/lib/vagrant-proxyconf/action/configure_svn_proxy.rb new file mode 100644 index 0000000..72abebe --- /dev/null +++ b/lib/vagrant-proxyconf/action/configure_svn_proxy.rb @@ -0,0 +1,37 @@ +require_relative 'base' +require_relative '../resource' +require 'uri' + +module VagrantPlugins + module ProxyConf + class Action + # Action for configuring Svn on the guest + class ConfigureSvnProxy < Base + def config_name + 'svn_proxy' + end + + private + + def configure_machine + write_config(svn_config, path: '/etc/subversion/servers') + end + + def svn_config + uri = URI.parse(config.http) + user = uri.user + pass = uri.password + config = (<<-CONFIG) +[global] +http-proxy-host=#{uri.host} +http-proxy-port=#{uri.port} + CONFIG + + config.concat("http-proxy-username=#{user}") if user + config.concat("http-proxy-password=#{pass}") if pass + config + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb b/lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb new file mode 100644 index 0000000..599395d --- /dev/null +++ b/lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb @@ -0,0 +1,14 @@ +module VagrantPlugins + module ProxyConf + module Cap + module Linux + # Capability for Svn command + module SvnProxyConf + def self.svn_proxy_conf(machine) + machine.communicate.test('test -d /etc/subversion') + end + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/config/svn_proxy.rb b/lib/vagrant-proxyconf/config/svn_proxy.rb new file mode 100644 index 0000000..5836478 --- /dev/null +++ b/lib/vagrant-proxyconf/config/svn_proxy.rb @@ -0,0 +1,19 @@ +require 'vagrant' +require_relative 'key_mixin' + +module VagrantPlugins + module ProxyConf + module Config + # Proxy configuration for Subversion + # + # @!parse class SvnProxy < Vagrant::Plugin::V2::Config; end + class SvnProxy < Vagrant.plugin('2', :config) + include KeyMixin + # @!parse extend KeyMixin::ClassMethods + + # @return [String] the HTTP proxy + key :http, env_var: 'VAGRANT_SVN_HTTP_PROXY' + end + end + end +end diff --git a/lib/vagrant-proxyconf/plugin.rb b/lib/vagrant-proxyconf/plugin.rb index 9f87c58..527e8cb 100644 --- a/lib/vagrant-proxyconf/plugin.rb +++ b/lib/vagrant-proxyconf/plugin.rb @@ -96,6 +96,11 @@ def self.load_optional_dependencies Config::GitProxy end + config 'svn_proxy' do + require_relative 'config/svn_proxy' + Config::SvnProxy + end + config 'proxy' do require_relative 'config/proxy' Config::Proxy @@ -126,6 +131,11 @@ def self.load_optional_dependencies Cap::Linux::GitProxyConf end + guest_capability 'linux', 'svn_proxy_conf' do + require_relative 'cap/linux/svn_proxy_conf' + Cap::Linux::SvnProxyConf + end + guest_capability 'coreos', 'env_proxy_conf' do # disabled on CoreOS end diff --git a/locales/en.yml b/locales/en.yml index 4115373..6bd5eab 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -48,6 +48,14 @@ en: configuring: |- Configuring proxy for Git... + svn_proxy: + not_enabled: |- + svn_proxy not enabled or configured + not_supported: |- + Skipping Subversion proxy config as the machine does not support it + configuring: |- + Configuring proxy for Subversion... + errors: vagrant_version: |- vagrant-proxyconf plugin requires Vagrant version %{requirement} diff --git a/spec/unit/vagrant-proxyconf/action/configure_svn_proxy_spec.rb b/spec/unit/vagrant-proxyconf/action/configure_svn_proxy_spec.rb new file mode 100644 index 0000000..df567f1 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/action/configure_svn_proxy_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +require 'vagrant-proxyconf/action/configure_svn_proxy' + +describe VagrantPlugins::ProxyConf::Action::ConfigureSvnProxy do + + describe '#config_name' do + subject { described_class.new(double, double).config_name } + it { should eq 'svn_proxy' } + end + +end diff --git a/spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf.rb b/spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf.rb new file mode 100644 index 0000000..f345a11 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +require 'vagrant-proxyconf/cap/linux/git_proxy_conf' + +describe VagrantPlugins::ProxyConf::Git::Linux::EnvProxyConf do + + describe '.git_proxy_conf' do + let(:subject) { described_class.git_proxy_conf(double) } + it { should eq 0 } + end + +end diff --git a/spec/unit/vagrant-proxyconf/config/svn_proxy_spec.rb b/spec/unit/vagrant-proxyconf/config/svn_proxy_spec.rb new file mode 100644 index 0000000..e8d73c6 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/config/svn_proxy_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' +require 'vagrant-proxyconf/config/svn_proxy' + +describe VagrantPlugins::ProxyConf::Config::SvnProxy do + let(:instance) { described_class.new } + before(:each) { ENV.delete('VAGRANT_SVN_HTTP_PROXY') } +end