From 0e6716ceafee2a71ffb4b651667511df8c39b228 Mon Sep 17 00:00:00 2001 From: OTA Hiroshi Date: Sun, 25 May 2014 16:25:17 +0900 Subject: [PATCH 1/5] adds docker proxy config --- README.md | 1 + lib/vagrant-proxyconf/action.rb | 3 + .../action/configure_docker_proxy.rb | 74 +++++++++++++++++++ .../cap/linux/docker_proxy_conf.rb | 26 +++++++ lib/vagrant-proxyconf/capability.rb | 5 ++ locales/en.yml | 8 ++ .../action/configure_docker_proxy_spec.rb | 10 +++ .../cap/linux/docker_proxy_conf_spec.rb | 53 +++++++++++++ 8 files changed, 180 insertions(+) create mode 100644 lib/vagrant-proxyconf/action/configure_docker_proxy.rb create mode 100644 lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb create mode 100644 spec/unit/vagrant-proxyconf/action/configure_docker_proxy_spec.rb create mode 100644 spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb diff --git a/README.md b/README.md index ab2e594..0a11951 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The plugin can set: * generic `http_proxy` etc. environment variables that many programs support * default proxy configuration for all Chef provisioners * proxy configuration for Apt +* proxy configuration for docker * proxy configuration for npm * proxy configuration for Yum * proxy configuration for PEAR diff --git a/lib/vagrant-proxyconf/action.rb b/lib/vagrant-proxyconf/action.rb index 72ab287..632a448 100644 --- a/lib/vagrant-proxyconf/action.rb +++ b/lib/vagrant-proxyconf/action.rb @@ -1,6 +1,7 @@ require 'vagrant/action/builtin/call' require_relative 'action/configure_apt_proxy' require_relative 'action/configure_chef_proxy' +require_relative 'action/configure_docker_proxy' require_relative 'action/configure_env_proxy' require_relative 'action/configure_git_proxy' require_relative 'action/configure_npm_proxy' @@ -32,6 +33,7 @@ def self.configure_after_provisoner b.use Builtin::Call, IsEnabled do |env, b2| next if !env[:result] + b2.use ConfigureDockerProxy b2.use ConfigureGitProxy b2.use ConfigureNpmProxy b2.use ConfigurePearProxy @@ -51,6 +53,7 @@ def self.config_actions b2.use ConfigureAptProxy b2.use ConfigureChefProxy + b2.use ConfigureDockerProxy b2.use ConfigureEnvProxy b2.use ConfigureGitProxy b2.use ConfigureNpmProxy diff --git a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb new file mode 100644 index 0000000..c94166f --- /dev/null +++ b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb @@ -0,0 +1,74 @@ +require_relative 'base' +require_relative '../resource' +require_relative '../userinfo_uri' + +module VagrantPlugins + module ProxyConf + class Action + # Action for configuring Docker on the guest + class ConfigureDockerProxy < Base + def config_name + 'docker_proxy' + end + + private + + def config + # Use global proxy config + @config ||= finalize_config(@machine.config.proxy) + end + + def configure_machine + logger.info('Writing the proxy configuration to docker config') + write_docker_config + end + + def write_docker_config + tmp = "/tmp/vagrant-proxyconf" + path = config_path + + sed_script = docker_sed_script + local_tmp = tempfile(docker_config) + + @machine.communicate.tap do |comm| + comm.sudo("rm #{tmp}", error_check: false) + comm.upload(local_tmp.path, tmp) + comm.sudo("touch #{path}") + comm.sudo("sed -e '#{sed_script}' #{path} > #{path}.new") + comm.sudo("cat #{tmp} >> #{path}.new") + comm.sudo("chmod 0644 #{path}.new") + comm.sudo("chown root:root #{path}.new") + comm.sudo("mv #{path}.new #{path}") + comm.sudo("rm #{tmp}") + end + end + + def docker_sed_script + <<-SED.gsub(/^\s+/, '') + /^export HTTP_PROXY=/ d + /^export HTTPS_PROXY=/ d + /^export FTP_PROXY=/ d + /^export NO_PROXY=/ d + /^export http_proxy=/ d + /^export https_proxy=/ d + /^export ftp_proxy=/ d + /^export no_proxy=/ d + SED + end + + def docker_config + <<-CONFIG.gsub(/^\s+/, '') + export HTTP_PROXY=#{config.http} + export HTTPS_PROXY=#{config.https} + export FTP_PROXY=#{config.ftp} + export NO_PROXY=#{config.no_proxy} + export http_proxy=#{config.http} + export https_proxy=#{config.https} + export ftp_proxy=#{config.ftp} + export no_proxy=#{config.no_proxy} + CONFIG + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb b/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb new file mode 100644 index 0000000..2e2ccf8 --- /dev/null +++ b/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb @@ -0,0 +1,26 @@ +require_relative '../util' + +module VagrantPlugins + module ProxyConf + module Cap + module Linux + # Capability for docker proxy configuration + module DockerProxyConf + # @return [String, false] the path to docker or `false` if not found + def self.docker_proxy_conf(machine) + docker_command = 'docker' if Util.which(machine, 'docker') + docker_command = 'docker.io' if Util.which(machine, 'docker.io') + + return false if docker_command.nil? + + if machine.communicate.test('cat /etc/redhat-release') + "/etc/sysconfig/#{docker_command}" + else + "/etc/default/#{docker_command}" + end + end + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/capability.rb b/lib/vagrant-proxyconf/capability.rb index 31d2c0c..08d4221 100644 --- a/lib/vagrant-proxyconf/capability.rb +++ b/lib/vagrant-proxyconf/capability.rb @@ -8,6 +8,11 @@ class Plugin < Vagrant.plugin('2') Cap::Debian::AptProxyConf end + guest_capability 'linux', 'docker_proxy_conf' do + require_relative 'cap/linux/docker_proxy_conf' + Cap::Linux::DockerProxyConf + end + guest_capability 'linux', 'env_proxy_conf' do require_relative 'cap/linux/env_proxy_conf' Cap::Linux::EnvProxyConf diff --git a/locales/en.yml b/locales/en.yml index 018aed3..3a11f32 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -8,6 +8,14 @@ en: configuring: |- Configuring proxy for Apt... + docker_proxy: + not_enabled: |- + docker_proxy not enabled or configured + not_supported: |- + Skipping Docker proxy config as the machine does not support it + configuring: |- + Configuring proxy for Docker... + chef_proxy: no_provisioners: |- No Chef provisioners configured diff --git a/spec/unit/vagrant-proxyconf/action/configure_docker_proxy_spec.rb b/spec/unit/vagrant-proxyconf/action/configure_docker_proxy_spec.rb new file mode 100644 index 0000000..5983ee5 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/action/configure_docker_proxy_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'vagrant-proxyconf/action/configure_docker_proxy' + +describe VagrantPlugins::ProxyConf::Action::ConfigureDockerProxy do + + describe '#config_name' do + subject { described_class.new(double, double).config_name } + it { should eq 'docker_proxy' } + end +end diff --git a/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb b/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb new file mode 100644 index 0000000..9175912 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' +require 'vagrant-proxyconf/cap/linux/docker_proxy_conf' +require 'vagrant-proxyconf/cap/util' + +describe VagrantPlugins::ProxyConf::Cap::Linux::DockerProxyConf do + + describe '.docker_proxy_conf' do + let(:machine) { double } + it "returns the path when docker is installed on Redhat" do + VagrantPlugins::ProxyConf::Cap::Util.stub(:which) do |_m, c| + if c == 'docker' + '/path/to/docker' + else + false + end + end + machine.stub_chain(:communicate, :test).and_return(true) + + expect(described_class.docker_proxy_conf(machine)).to eq '/etc/sysconfig/docker' + end + + it "returns the path when docker is installed on Debian or Ubuntu" do + VagrantPlugins::ProxyConf::Cap::Util.stub(:which) do |_m, c| + if c == 'docker' + '/path/to/docker' + else + false + end + end + machine.stub_chain(:communicate, :test).and_return(false) + + expect(described_class.docker_proxy_conf(machine)).to eq '/etc/default/docker' + end + + it "returns the path when docker.io is installed on Ubuntu 14.04 or higher" do + VagrantPlugins::ProxyConf::Cap::Util.stub(:which) do |_m, c| + if c == 'docker.io' + '/path/to/docker.io' + else + false + end + end + machine.stub_chain(:communicate, :test).and_return(false) + + expect(described_class.docker_proxy_conf(machine)).to eq '/etc/default/docker.io' + end + + it "returns false when docker is not installed" do + VagrantPlugins::ProxyConf::Cap::Util.stub(which: false) + expect(described_class.docker_proxy_conf(machine)).to be_false + end + end +end From fc311024ffe5a4b720c469414748b7b3e7e6ebde Mon Sep 17 00:00:00 2001 From: OTA Hiroshi Date: Wed, 28 May 2014 06:55:06 +0900 Subject: [PATCH 2/5] adds docker proxy config on boot2docker --- .../cap/linux/docker_proxy_conf.rb | 2 ++ .../cap/linux/docker_proxy_conf_spec.rb | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb b/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb index 2e2ccf8..ece2c3a 100644 --- a/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb +++ b/lib/vagrant-proxyconf/cap/linux/docker_proxy_conf.rb @@ -15,6 +15,8 @@ def self.docker_proxy_conf(machine) if machine.communicate.test('cat /etc/redhat-release') "/etc/sysconfig/#{docker_command}" + elsif machine.communicate.test('ls /var/lib/boot2docker/') + "/var/lib/boot2docker/profile" else "/etc/default/#{docker_command}" end diff --git a/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb b/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb index 9175912..790a095 100644 --- a/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb +++ b/spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb @@ -14,7 +14,13 @@ false end end - machine.stub_chain(:communicate, :test).and_return(true) + machine.stub_chain(:communicate, :test) do |c| + if c == 'cat /etc/redhat-release' + true + else + false + end + end expect(described_class.docker_proxy_conf(machine)).to eq '/etc/sysconfig/docker' end @@ -45,6 +51,25 @@ expect(described_class.docker_proxy_conf(machine)).to eq '/etc/default/docker.io' end + it "returns the path when docker is installed on boot2docker" do + VagrantPlugins::ProxyConf::Cap::Util.stub(:which) do |_m, c| + if c == 'docker' + '/path/to/docker' + else + false + end + end + machine.stub_chain(:communicate, :test) do |c| + if c == 'ls /var/lib/boot2docker' + true + else + false + end + end + + expect(described_class.docker_proxy_conf(machine)).to eq '/etc/default/docker' + end + it "returns false when docker is not installed" do VagrantPlugins::ProxyConf::Cap::Util.stub(which: false) expect(described_class.docker_proxy_conf(machine)).to be_false From 69909d4f75bca80307a3ba3b782c086061f1d199 Mon Sep 17 00:00:00 2001 From: OTA Hiroshi Date: Sat, 31 May 2014 06:05:12 +0900 Subject: [PATCH 3/5] Fix docker config for empty end `false` values --- .../action/configure_docker_proxy.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb index c94166f..d044538 100644 --- a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb +++ b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb @@ -58,14 +58,14 @@ def docker_sed_script def docker_config <<-CONFIG.gsub(/^\s+/, '') - export HTTP_PROXY=#{config.http} - export HTTPS_PROXY=#{config.https} - export FTP_PROXY=#{config.ftp} - export NO_PROXY=#{config.no_proxy} - export http_proxy=#{config.http} - export https_proxy=#{config.https} - export ftp_proxy=#{config.ftp} - export no_proxy=#{config.no_proxy} + export HTTP_PROXY=#{config.http || ''} + export HTTPS_PROXY=#{config.https || ''} + export FTP_PROXY=#{config.ftp || ''} + export NO_PROXY=#{config.no_proxy || ''} + export http_proxy=#{config.http || ''} + export https_proxy=#{config.https || ''} + export ftp_proxy=#{config.ftp || ''} + export no_proxy=#{config.no_proxy || ''} CONFIG end end From 421c477c27b6a2cc2d3f5b2335873cec548a7fb8 Mon Sep 17 00:00:00 2001 From: OTA Hiroshi Date: Sat, 14 Jun 2014 07:52:06 +0900 Subject: [PATCH 4/5] adds docker daemon restarting to reflect proxy configures --- lib/vagrant-proxyconf/action/configure_docker_proxy.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb index d044538..7c95d3f 100644 --- a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb +++ b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb @@ -23,6 +23,14 @@ def configure_machine write_docker_config end + def docker + if config_path && config_path.include?('docker.io') + 'docker.io' + else + 'docker' + end + end + def write_docker_config tmp = "/tmp/vagrant-proxyconf" path = config_path @@ -40,6 +48,7 @@ def write_docker_config comm.sudo("chown root:root #{path}.new") comm.sudo("mv #{path}.new #{path}") comm.sudo("rm #{tmp}") + comm.sudo("service #{docker} restart || /etc/init.d/#{docker} restart") end end From d8755f4ffd0727543e7d40c167fc561a81bfb26b Mon Sep 17 00:00:00 2001 From: OTA Hiroshi Date: Wed, 18 Jun 2014 05:49:47 +0900 Subject: [PATCH 5/5] deletes unused environment variables by go library --- lib/vagrant-proxyconf/action/configure_docker_proxy.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb index 7c95d3f..aee9608 100644 --- a/lib/vagrant-proxyconf/action/configure_docker_proxy.rb +++ b/lib/vagrant-proxyconf/action/configure_docker_proxy.rb @@ -55,12 +55,8 @@ def write_docker_config def docker_sed_script <<-SED.gsub(/^\s+/, '') /^export HTTP_PROXY=/ d - /^export HTTPS_PROXY=/ d - /^export FTP_PROXY=/ d /^export NO_PROXY=/ d /^export http_proxy=/ d - /^export https_proxy=/ d - /^export ftp_proxy=/ d /^export no_proxy=/ d SED end @@ -68,12 +64,8 @@ def docker_sed_script def docker_config <<-CONFIG.gsub(/^\s+/, '') export HTTP_PROXY=#{config.http || ''} - export HTTPS_PROXY=#{config.https || ''} - export FTP_PROXY=#{config.ftp || ''} export NO_PROXY=#{config.no_proxy || ''} export http_proxy=#{config.http || ''} - export https_proxy=#{config.https || ''} - export ftp_proxy=#{config.ftp || ''} export no_proxy=#{config.no_proxy || ''} CONFIG end