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

Add support for Git/Svn proxy through system config. #40

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions lib/vagrant-proxyconf/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please maintain alphabetic order.

require_relative 'action/is_enabled'
require_relative 'action/only_once'

Expand Down Expand Up @@ -48,6 +50,8 @@ def self.config_actions
b2.use ConfigureEnvProxy
b2.use ConfigurePearProxy
b2.use ConfigureYumProxy
b2.use ConfigureGitProxy
b2.use ConfigureSvnProxy
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please maintain alphabetic order. And add these also to configure_after_provisoner method so they are supposed to be triggered also after each provisioner run. (Although I haven't got it actually working; assuming bug in Vagrant.)

end
end
end
Expand Down
12 changes: 11 additions & 1 deletion lib/vagrant-proxyconf/action/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, "cat #{tmp} >> #{path}" would be simpler. But this ignores :mode and :owner options as those are set only for the tmp file.

else
comm.sudo("mv #{tmp} #{path}")
end
end
end

Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was already moved here in another PR.

end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/vagrant-proxyconf/action/configure_git_proxy.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions lib/vagrant-proxyconf/action/configure_svn_proxy.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a helper class/module for this. IIRC the yum action uses it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall there was an issue with how the helper stripped information. I originally tried to use it. I can take a look at it again.

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
14 changes: 14 additions & 0 deletions lib/vagrant-proxyconf/cap/linux/git_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/vagrant-proxyconf/cap/linux/svn_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/vagrant-proxyconf/config/git_proxy.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/vagrant-proxyconf/config/svn_proxy.rb
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions lib/vagrant-proxyconf/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
11 changes: 11 additions & 0 deletions spec/unit/vagrant-proxyconf/action/configure_git_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/unit/vagrant-proxyconf/action/configure_svn_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/unit/vagrant-proxyconf/cap/linux/git_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/unit/vagrant-proxyconf/config/git_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/unit/vagrant-proxyconf/config/svn_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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