From df28094b75c2b95fb70553e123e7debff32d3ffa Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 22 Jun 2013 19:05:54 -0300 Subject: [PATCH] Add configuration for Apt proxy --- .../action/configure_apt_proxy.rb | 53 +++++++++++ lib/vagrant-cachier/apt_proxy_config.rb | 88 +++++++++++++++++++ .../cap/debian/apt_proxy_conf.rb | 13 +++ lib/vagrant-cachier/plugin.rb | 12 +++ 4 files changed, 166 insertions(+) create mode 100644 lib/vagrant-cachier/action/configure_apt_proxy.rb create mode 100644 lib/vagrant-cachier/apt_proxy_config.rb create mode 100644 lib/vagrant-cachier/cap/debian/apt_proxy_conf.rb diff --git a/lib/vagrant-cachier/action/configure_apt_proxy.rb b/lib/vagrant-cachier/action/configure_apt_proxy.rb new file mode 100644 index 0000000..4461566 --- /dev/null +++ b/lib/vagrant-cachier/action/configure_apt_proxy.rb @@ -0,0 +1,53 @@ +require 'tempfile' + +module VagrantPlugins + module Cachier + class Action + class ConfigureAptProxy + attr_reader :logger + + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::cachier::action::configure_apt_proxy") + end + + def call(env) + @app.call env + + proxy_config = env[:machine].config.apt_proxy + if !proxy_config.enabled? + logger.debug "apt_proxy not enabled or configured" + elsif !proxy_conf_capability?(env[:machine]) + env[:ui].info "Skipping Apt proxy config as the machine does not support it" + else + env[:ui].info "Configuring proxy for Apt..." + write_apt_proxy_conf(env[:machine], proxy_config) + end + end + + def write_apt_proxy_conf(machine, config) + logger.debug "Configuration:\n#{config}" + + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(config) + temp.close + + machine.communicate.tap do |comm| + comm.upload(temp.path, "/tmp/vagrant-apt-proxy-conf") + comm.sudo("cat /tmp/vagrant-apt-proxy-conf > #{proxy_conf_path(machine)}") + comm.sudo("rm /tmp/vagrant-apt-proxy-conf") + end + end + + def proxy_conf_capability?(machine) + machine.guest.capability?(:apt_proxy_conf) + end + + def proxy_conf_path(machine) + machine.guest.capability(:apt_proxy_conf) + end + end + end + end +end diff --git a/lib/vagrant-cachier/apt_proxy_config.rb b/lib/vagrant-cachier/apt_proxy_config.rb new file mode 100644 index 0000000..bdb77a0 --- /dev/null +++ b/lib/vagrant-cachier/apt_proxy_config.rb @@ -0,0 +1,88 @@ +require 'vagrant' + +module VagrantPlugins + module Cachier + class AptProxyConfig < Vagrant.plugin("2", :config) + # HTTP proxy for Apt + attr_accessor :http + + # HTTPS proxy for Apt + attr_accessor :https + + def initialize + @http = UNSET_VALUE + @https = UNSET_VALUE + end + + def finalize! + @http = override_from_env_var('http', @http) + @http = nil if @http == UNSET_VALUE + + @https = override_from_env_var('https', @https) + @https = nil if @https == UNSET_VALUE + end + + def enabled? + !http.nil? || !https.nil? + end + + # @return [String] the full configuration stanza + def to_s + "#{config_for('http')}#{config_for('https')}" + end + + private + + def override_from_env_var(proto, default) + ENV.fetch("APT_PROXY_#{proto.upcase}", default) + end + + def config_for(proto) + ConfigValue.new(proto, send(proto.to_sym)) + end + + class ConfigValue + + attr_reader :proto, :value + + # @param proto [String] the protocol ("http", "https") + # @param value [Object] the configuration value + def initialize(proto, value) + @proto = proto + @value = value + end + + # @return [String] the full Apt configuration line + def to_s + set? ? %Q{Acquire::#{proto}::Proxy "#{proxy_uri}";\n} : "" + end + + private + + def set? + value && !value.empty? + end + + def direct? + value.upcase == "DIRECT" + end + + def proxy_uri + direct? ? "DIRECT" : "#{prefix}#{value}#{suffix}" + end + + def prefix + "#{proto}://" if value !~ %r{^.*://} + end + + def suffix + ":#{default_port}" if value !~ %r{:\d+$} + end + + def default_port + 3142 + end + end + end + end +end diff --git a/lib/vagrant-cachier/cap/debian/apt_proxy_conf.rb b/lib/vagrant-cachier/cap/debian/apt_proxy_conf.rb new file mode 100644 index 0000000..0329a59 --- /dev/null +++ b/lib/vagrant-cachier/cap/debian/apt_proxy_conf.rb @@ -0,0 +1,13 @@ +module VagrantPlugins + module Cachier + module Cap + module Debian + module AptProxyConf + def self.apt_proxy_conf(machine) + '/etc/apt/apt.conf.d/01proxy' + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 31a2870..e5d94c4 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -8,6 +8,11 @@ module VagrantPlugins Config end + config 'apt_proxy' do + require_relative 'apt_proxy_config' + AptProxyConfig + end + guest_capability 'linux', 'gemdir' do require_relative 'cap/linux/gemdir' Cap::Linux::Gemdir @@ -18,6 +23,11 @@ module VagrantPlugins Cap::Debian::AptCacheDir end + guest_capability 'debian', 'apt_proxy_conf' do + require_relative 'cap/debian/apt_proxy_conf' + Cap::Debian::AptProxyConf + end + guest_capability 'redhat', 'yum_cache_dir' do require_relative 'cap/redhat/yum_cache_dir' Cap::RedHat::YumCacheDir @@ -30,7 +40,9 @@ module VagrantPlugins install_action_hook = lambda do |hook| require_relative 'action' + require_relative 'action/configure_apt_proxy' hook.after Vagrant::Action::Builtin::Provision, VagrantPlugins::Cachier::Action::Install + hook.after Vagrant::Action::Builtin::Provision, VagrantPlugins::Cachier::Action::ConfigureAptProxy end action_hook 'set-shared-cache-on-machine-up', :machine_action_up, &install_action_hook action_hook 'set-shared-cache-on-machine-reload', :machine_action_reload, &install_action_hook