diff --git a/lib/vagrant-proxyconf/action.rb b/lib/vagrant-proxyconf/action.rb index 4730b0a..f93575d 100644 --- a/lib/vagrant-proxyconf/action.rb +++ b/lib/vagrant-proxyconf/action.rb @@ -4,6 +4,8 @@ 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/configure_svn_proxy' require_relative 'action/is_enabled' require_relative 'action/only_once' @@ -48,6 +50,8 @@ def self.config_actions b2.use ConfigureEnvProxy 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 1d34384..dfa4676 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 @@ -114,6 +119,11 @@ 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) + value.to_s.shellescape + end 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..e84fba5 --- /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 + if @machine.guest.capability(:git_proxy_conf) + @machine.communicate.sudo("git config --system http.proxy #{config.http}") + else + write_config(git_config, path: '/etc/gitconfig', append: true) + end + end + + def git_config + (<<-CONFIG) + +[http] + proxy = "#{config.http}" + CONFIG + end + end + end + end +end 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/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/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/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/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 a6d7a13..527e8cb 100644 --- a/lib/vagrant-proxyconf/plugin.rb +++ b/lib/vagrant-proxyconf/plugin.rb @@ -80,6 +80,102 @@ 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 'svn_proxy' do + require_relative 'config/svn_proxy' + Config::SvnProxy + 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 '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 + + 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..6bd5eab 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -40,6 +40,22 @@ 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... + + 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_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/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/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 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