vagrant-proxyconf 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -1
  3. data/.travis.yml +15 -1
  4. data/CHANGELOG.md +25 -0
  5. data/Gemfile +6 -3
  6. data/LICENSE.txt +1 -1
  7. data/README.md +34 -8
  8. data/Rakefile +2 -0
  9. data/lib/vagrant-proxyconf/action.rb +28 -5
  10. data/lib/vagrant-proxyconf/action/base.rb +26 -19
  11. data/lib/vagrant-proxyconf/action/configure_chef_proxy.rb +15 -15
  12. data/lib/vagrant-proxyconf/action/configure_env_proxy.rb +2 -2
  13. data/lib/vagrant-proxyconf/action/configure_pear_proxy.rb +29 -0
  14. data/lib/vagrant-proxyconf/action/configure_yum_proxy.rb +5 -10
  15. data/lib/vagrant-proxyconf/action/is_enabled.rb +24 -0
  16. data/lib/vagrant-proxyconf/cap/linux/pear_proxy_conf.rb +15 -0
  17. data/lib/vagrant-proxyconf/capability.rb +31 -0
  18. data/lib/vagrant-proxyconf/config.rb +27 -0
  19. data/lib/vagrant-proxyconf/config/apt_proxy.rb +24 -24
  20. data/lib/vagrant-proxyconf/config/env_proxy.rb +8 -1
  21. data/lib/vagrant-proxyconf/config/key_mixin.rb +3 -2
  22. data/lib/vagrant-proxyconf/config/proxy.rb +3 -0
  23. data/lib/vagrant-proxyconf/hook.rb +41 -0
  24. data/lib/vagrant-proxyconf/plugin.rb +42 -70
  25. data/lib/vagrant-proxyconf/version.rb +1 -1
  26. data/locales/en.yml +9 -1
  27. data/spec/spec_helper.rb +11 -9
  28. data/spec/unit/vagrant-proxyconf/action/configure_chef_proxy_spec.rb +3 -1
  29. data/spec/unit/vagrant-proxyconf/action/configure_pear_proxy_spec.rb +10 -0
  30. data/spec/unit/vagrant-proxyconf/action/configure_yum_proxy_spec.rb +5 -1
  31. data/spec/unit/vagrant-proxyconf/action/is_enabled_spec.rb +39 -0
  32. data/spec/unit/vagrant-proxyconf/cap/linux/pear_proxy_conf_spec.rb +25 -0
  33. data/spec/unit/vagrant-proxyconf/config/env_proxy_spec.rb +15 -8
  34. data/spec/unit/vagrant-proxyconf/plugin_spec.rb +54 -15
  35. metadata +19 -15
@@ -0,0 +1,29 @@
1
+ require_relative 'base'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Action
6
+ # Action for configuring Pear on the guest
7
+ class ConfigurePearProxy < Base
8
+ def config_name
9
+ 'pear_proxy'
10
+ end
11
+
12
+ private
13
+
14
+ # @return [Vagrant::Plugin::V2::Config] the configuration
15
+ def config
16
+ # Use global proxy config
17
+ @config ||= finalize_config(@machine.config.proxy)
18
+ end
19
+
20
+ def configure_machine
21
+ proxy = config.http || ''
22
+
23
+ @machine.communicate.sudo(
24
+ "pear config-set http_proxy #{escape(proxy)} system")
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -13,15 +13,15 @@ module VagrantPlugins
13
13
 
14
14
  private
15
15
 
16
- def configure_machine(machine, config)
16
+ def configure_machine
17
17
  tmp = "/tmp/vagrant-proxyconf"
18
- path = config_path(machine)
18
+ path = config_path
19
19
 
20
- machine.communicate.tap do |comm|
20
+ @machine.communicate.tap do |comm|
21
21
  comm.sudo("rm #{tmp}", error_check: false)
22
22
  comm.upload(ProxyConf.resource("yum_config.awk"), tmp)
23
23
  comm.sudo("touch #{path}")
24
- comm.sudo("gawk -f #{tmp} #{proxy_params(config)} #{path} > #{path}.new")
24
+ comm.sudo("gawk -f #{tmp} #{proxy_params} #{path} > #{path}.new")
25
25
  comm.sudo("chmod 0644 #{path}.new")
26
26
  comm.sudo("chown root:root #{path}.new")
27
27
  comm.sudo("mv #{path}.new #{path}")
@@ -29,15 +29,10 @@ module VagrantPlugins
29
29
  end
30
30
  end
31
31
 
32
- def proxy_params(config)
32
+ def proxy_params
33
33
  u = UserinfoURI.new(config.http)
34
34
  "-v proxy=#{escape(u.uri)} -v user=#{escape(u.user)} -v pass=#{escape(u.pass)}"
35
35
  end
36
-
37
- # @param value [String, nil] the string to escape for shell usage
38
- def escape(value)
39
- value.to_s.shellescape
40
- end
41
36
  end
42
37
  end
43
38
  end
@@ -0,0 +1,24 @@
1
+ module VagrantPlugins
2
+ module ProxyConf
3
+ class Action
4
+ # Action which checks if the plugin should be enabled
5
+ class IsEnabled
6
+ def initialize(app, env)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env[:result] = plugin_enabled?(env[:machine].config.proxy)
12
+
13
+ @app.call(env)
14
+ end
15
+
16
+ private
17
+
18
+ def plugin_enabled?(config)
19
+ config.enabled != false && config.enabled != ''
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module VagrantPlugins
2
+ module ProxyConf
3
+ module Cap
4
+ module Linux
5
+ # Capability for PEAR proxy configuration
6
+ module PearProxyConf
7
+ # @return [Boolean] if PEAR is installed
8
+ def self.pear_proxy_conf(machine)
9
+ machine.communicate.test("which pear")
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Plugin < Vagrant.plugin('2')
6
+ guest_capability 'debian', 'apt_proxy_conf' do
7
+ require_relative 'cap/debian/apt_proxy_conf'
8
+ Cap::Debian::AptProxyConf
9
+ end
10
+
11
+ guest_capability 'coreos', 'env_proxy_conf' do
12
+ # disabled on CoreOS
13
+ end
14
+
15
+ guest_capability 'linux', 'env_proxy_conf' do
16
+ require_relative 'cap/linux/env_proxy_conf'
17
+ Cap::Linux::EnvProxyConf
18
+ end
19
+
20
+ guest_capability 'linux', 'pear_proxy_conf' do
21
+ require_relative 'cap/linux/pear_proxy_conf'
22
+ Cap::Linux::PearProxyConf
23
+ end
24
+
25
+ guest_capability 'redhat', 'yum_proxy_conf' do
26
+ require_relative 'cap/redhat/yum_proxy_conf'
27
+ Cap::Redhat::YumProxyConf
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Plugin < Vagrant.plugin('2')
6
+ config 'apt_proxy' do
7
+ require_relative 'config/apt_proxy'
8
+ Config::AptProxy
9
+ end
10
+
11
+ config 'env_proxy' do
12
+ require_relative 'config/env_proxy'
13
+ Config::EnvProxy
14
+ end
15
+
16
+ config 'proxy' do
17
+ require_relative 'config/proxy'
18
+ Config::Proxy
19
+ end
20
+
21
+ config 'yum_proxy' do
22
+ require_relative 'config/yum_proxy'
23
+ Config::YumProxy
24
+ end
25
+ end
26
+ end
27
+ end
@@ -20,56 +20,56 @@ module VagrantPlugins
20
20
  # @return [String] the FTP proxy
21
21
  key :ftp, env_var: 'VAGRANT_APT_FTP_PROXY'
22
22
 
23
+ def finalize!
24
+ super
25
+
26
+ keys.each do |key|
27
+ value = get(key)
28
+ set(key, finalize_uri(key, value)) if value
29
+ end
30
+ end
31
+
23
32
  private
24
33
 
25
34
  # (see KeyMixin#config_for)
26
35
  def config_for(key, value)
27
- ConfigLine.new(key.name, value)
36
+ %Q{Acquire::#{key.name}::Proxy #{value.inspect};\n} if value
37
+ end
38
+
39
+ def finalize_uri(key, value)
40
+ AptProxyURI.new(key.name, value).to_s
28
41
  end
29
42
 
30
- # Helper for constructing a configuration line for apt.conf
43
+ # Helper for constructing configuration values for apt.conf
31
44
  #
32
45
  # @api private
33
- class ConfigLine
46
+ class AptProxyURI
34
47
 
35
- attr_reader :proto, :value
48
+ attr_reader :scheme, :value
36
49
 
37
- # @param proto [String] the protocol ("http", "https", ...)
50
+ # @param scheme [String] the protocol ("http", "https", ...)
38
51
  # @param value [Object] the configuration value
39
- def initialize(proto, value)
40
- @proto = proto
52
+ def initialize(scheme, value)
53
+ @scheme = scheme
41
54
  @value = value
42
55
  end
43
56
 
44
- # @return [String] the full Apt configuration line
45
57
  def to_s
46
- %Q{Acquire::#{proto}::Proxy "#{direct || proxy_uri}";\n} if set?
58
+ direct || "#{prefix}#{value}#{suffix}"
47
59
  end
48
60
 
49
61
  private
50
62
 
51
- def set?
52
- value && !value.empty?
53
- end
54
-
55
63
  def direct
56
- "DIRECT" if value.upcase == "DIRECT"
57
- end
58
-
59
- def proxy_uri
60
- "#{prefix}#{value}#{suffix}"
64
+ 'DIRECT' if value.upcase == 'DIRECT'
61
65
  end
62
66
 
63
67
  def prefix
64
- "#{proto}://" if value !~ %r{^.*://}
68
+ "#{scheme}://" if value !~ %r{^.*://}
65
69
  end
66
70
 
67
71
  def suffix
68
- ":#{default_port}" if value !~ %r{:\d+$} && value !~ %r{/}
69
- end
70
-
71
- def default_port
72
- 3142
72
+ ':3142' if value !~ %r{:\d+$} && value !~ %r{/}
73
73
  end
74
74
  end
75
75
  end
@@ -27,8 +27,15 @@ module VagrantPlugins
27
27
 
28
28
  # (see KeyMixin#config_for)
29
29
  def config_for(key, value)
30
- if value && !value.empty?
30
+ if value
31
31
  var = env_variable_name(key)
32
+
33
+ # Quote the `no_proxy` value in case there are spaces and special
34
+ # characters. Unfortunately we can't escape other values before
35
+ # v2.0 as even the README had an example of using shell variables
36
+ # still in v1.0.x.
37
+ value = value.inspect if key.name == :no_proxy
38
+
32
39
  [var.upcase, var.downcase].map { |v| "export #{v}=#{value}\n" }.join
33
40
  end
34
41
  end
@@ -70,7 +70,7 @@ module VagrantPlugins
70
70
  # @param value [String, nil] the configuration value
71
71
  # @return [#to_s] the configuration line(s)
72
72
  def config_for(key, value)
73
- "#{key}=#{value}\n"
73
+ "#{key}=#{value && value}\n"
74
74
  end
75
75
 
76
76
  # Returns a new instance of this class where all nil keys are
@@ -113,10 +113,11 @@ module VagrantPlugins
113
113
  private
114
114
 
115
115
  def resolve_value(key)
116
- key.value_from_env_var do |default|
116
+ ret = key.value_from_env_var do |default|
117
117
  value = get(key)
118
118
  value == self.class::UNSET_VALUE ? default : value
119
119
  end
120
+ ret == '' ? false : ret
120
121
  end
121
122
  end
122
123
  end
@@ -11,6 +11,9 @@ module VagrantPlugins
11
11
  include KeyMixin
12
12
  # @!parse extend KeyMixin::ClassMethods
13
13
 
14
+ # Defines the mode of the plugin
15
+ key :enabled, env_var: 'VAGRANT_PROXY'
16
+
14
17
  # @return [String] the HTTP proxy
15
18
  key :http, env_var: 'VAGRANT_HTTP_PROXY'
16
19
 
@@ -0,0 +1,41 @@
1
+ require 'vagrant'
2
+
3
+ module VagrantPlugins
4
+ module ProxyConf
5
+ class Plugin < Vagrant.plugin('2')
6
+ # Actions to run before any provisioner or other plugin
7
+ action_hook 'proxyconf_configure' do |hook|
8
+ require_relative 'action'
9
+
10
+ # the standard provision action
11
+ hook.after Vagrant::Action::Builtin::Provision, Action.configure
12
+
13
+ # Vagrant 1.5+ can install NFS client
14
+ if check_vagrant_version('>= 1.5.0.dev')
15
+ hook.after Vagrant::Action::Builtin::SyncedFolders, Action.configure
16
+ end
17
+
18
+ # vagrant-aws < 0.4.0 uses a non-standard provision action
19
+ if defined?(VagrantPlugins::AWS::Action::TimedProvision)
20
+ hook.after VagrantPlugins::AWS::Action::TimedProvision, Action.configure
21
+ end
22
+
23
+ # configure the proxies before vagrant-omnibus
24
+ if defined?(VagrantPlugins::Omnibus::Action::InstallChef)
25
+ hook.after VagrantPlugins::Omnibus::Action::InstallChef, Action.configure
26
+ end
27
+
28
+ # configure the proxies before vagrant-vbguest
29
+ if defined?(VagrantVbguest::Middleware)
30
+ hook.before VagrantVbguest::Middleware, Action.configure(before: true)
31
+ end
32
+ end
33
+
34
+ # Actions to run after each provisioner run
35
+ action_hook 'proxyconf_configure', :provisioner_run do |hook|
36
+ require_relative 'action'
37
+ hook.after :run_provisioner, Action.configure_after_provisoner
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,4 +1,5 @@
1
1
  require 'vagrant'
2
+ require_relative 'logger'
2
3
 
3
4
  module VagrantPlugins
4
5
  module ProxyConf
@@ -6,19 +7,30 @@ module VagrantPlugins
6
7
  #
7
8
  # @!parse class Plugin < Vagrant::Plugin::V2::Plugin; end
8
9
  class Plugin < Vagrant.plugin('2')
9
- # The minimum compatible Vagrant version
10
- MIN_VAGRANT_VERSION = '1.2.0'
10
+ # Compatible Vagrant versions
11
+ VAGRANT_VERSION_REQUIREMENT = '>= 1.2.0'
11
12
 
12
13
  # A list of plugins whose action classes we hook to if installed
13
14
  OPTIONAL_PLUGIN_DEPENDENCIES = %w[vagrant-aws vagrant-omnibus vagrant-vbguest]
14
15
 
16
+ # Returns true if the Vagrant version fulfills the requirements
17
+ #
18
+ # @param requirements [String, Array<String>] the version requirement
19
+ # @return [Boolean]
20
+ def self.check_vagrant_version(*requirements)
21
+ Gem::Requirement.new(*requirements).satisfied_by?(
22
+ Gem::Version.new(Vagrant::VERSION))
23
+ end
24
+
15
25
  # Verifies that the Vagrant version fulfills the requirements
16
26
  #
17
27
  # @raise [VagrantPlugins::ProxyConf::VagrantVersionError] if this plugin
18
28
  # is incompatible with the Vagrant version
19
29
  def self.check_vagrant_version!
20
- if Gem::Version.new(Vagrant::VERSION) < Gem::Version.new(MIN_VAGRANT_VERSION)
21
- msg = I18n.t('vagrant_proxyconf.errors.vagrant_version', min_version: MIN_VAGRANT_VERSION)
30
+ if !check_vagrant_version(VAGRANT_VERSION_REQUIREMENT)
31
+ msg = I18n.t(
32
+ 'vagrant_proxyconf.errors.vagrant_version',
33
+ requirement: VAGRANT_VERSION_REQUIREMENT.inspect)
22
34
  $stderr.puts msg
23
35
  raise msg
24
36
  end
@@ -31,19 +43,36 @@ module VagrantPlugins
31
43
  end
32
44
 
33
45
  # Ensures a dependent plugin is loaded before us if it is installed.
34
- # Ignores {Vagrant::Errors::PluginLoadError} but passes other exceptions.
46
+ # Ignores Errors while loading, as Vagrant itself anyway shows them to
47
+ # used when *it* tries to load the plugin.
35
48
  #
36
49
  # @param plugin [String] the plugin name
37
50
  def self.load_optional_dependency(plugin)
38
- begin
39
- Vagrant.require_plugin plugin
40
- rescue Vagrant::Errors::PluginLoadError; end
51
+ logger = ProxyConf.logger
52
+ logger.info "Trying to load #{plugin}"
53
+
54
+ if check_vagrant_version('< 1.5.0.dev')
55
+ begin
56
+ Vagrant.require_plugin plugin
57
+ rescue Vagrant::Errors::PluginLoadError
58
+ logger.info "Ignoring the load error of #{plugin}"
59
+ end
60
+ else
61
+ begin
62
+ require plugin
63
+ rescue Exception => e
64
+ logger.info "Failed to load #{plugin}: #{e.inspect}"
65
+ logger.info "Ignoring the error"
66
+ end
67
+ end
41
68
  end
42
69
 
43
70
  # Loads the plugins to ensure their action hooks are registered before us.
44
71
  # Uses alphabetical order to not change the default behaviour otherwise.
45
72
  def self.load_optional_dependencies
46
- OPTIONAL_PLUGIN_DEPENDENCIES.sort.each { |plugin| load_optional_dependency plugin }
73
+ OPTIONAL_PLUGIN_DEPENDENCIES.sort.each do |plugin|
74
+ load_optional_dependency plugin
75
+ end
47
76
  end
48
77
 
49
78
  setup_i18n
@@ -51,67 +80,10 @@ module VagrantPlugins
51
80
  load_optional_dependencies
52
81
 
53
82
  name 'vagrant-proxyconf'
54
-
55
- config 'apt_proxy' do
56
- require_relative 'config/apt_proxy'
57
- Config::AptProxy
58
- end
59
-
60
- config 'env_proxy' do
61
- require_relative 'config/env_proxy'
62
- Config::EnvProxy
63
- end
64
-
65
- config 'proxy' do
66
- require_relative 'config/proxy'
67
- Config::Proxy
68
- end
69
-
70
- config 'yum_proxy' do
71
- require_relative 'config/yum_proxy'
72
- Config::YumProxy
73
- end
74
-
75
- guest_capability 'debian', 'apt_proxy_conf' do
76
- require_relative 'cap/debian/apt_proxy_conf'
77
- Cap::Debian::AptProxyConf
78
- end
79
-
80
- guest_capability 'linux', 'env_proxy_conf' do
81
- require_relative 'cap/linux/env_proxy_conf'
82
- Cap::Linux::EnvProxyConf
83
- end
84
-
85
- guest_capability 'coreos', 'env_proxy_conf' do
86
- # disabled on CoreOS
87
- end
88
-
89
- guest_capability 'redhat', 'yum_proxy_conf' do
90
- require_relative 'cap/redhat/yum_proxy_conf'
91
- Cap::Redhat::YumProxyConf
92
- end
93
-
94
- action_hook 'proxyconf_configure' do |hook|
95
- require_relative 'action'
96
-
97
- # the standard provision action
98
- hook.after Vagrant::Action::Builtin::Provision, Action.configure
99
-
100
- # vagrant-aws < 0.4.0 uses a non-standard provision action
101
- if defined?(VagrantPlugins::AWS::Action::TimedProvision)
102
- hook.after VagrantPlugins::AWS::Action::TimedProvision, Action.configure
103
- end
104
-
105
- # configure the proxies before vagrant-omnibus
106
- if defined?(VagrantPlugins::Omnibus::Action::InstallChef)
107
- hook.after VagrantPlugins::Omnibus::Action::InstallChef, Action.configure
108
- end
109
-
110
- # configure the proxies before vagrant-vbguest
111
- if defined?(VagrantVbguest::Middleware)
112
- hook.before VagrantVbguest::Middleware, Action.configure(before: true)
113
- end
114
- end
115
83
  end
116
84
  end
117
85
  end
86
+
87
+ require_relative "capability"
88
+ require_relative "config"
89
+ require_relative "hook"