-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from otahi/docker
Add support for docker proxy
- Loading branch information
Showing
8 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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 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 | ||
|
||
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}") | ||
comm.sudo("service #{docker} restart || /etc/init.d/#{docker} restart") | ||
end | ||
end | ||
|
||
def docker_sed_script | ||
<<-SED.gsub(/^\s+/, '') | ||
/^export HTTP_PROXY=/ d | ||
/^export NO_PROXY=/ d | ||
/^export http_proxy=/ d | ||
/^export no_proxy=/ d | ||
SED | ||
end | ||
|
||
def docker_config | ||
<<-CONFIG.gsub(/^\s+/, '') | ||
export HTTP_PROXY=#{config.http || ''} | ||
export NO_PROXY=#{config.no_proxy || ''} | ||
export http_proxy=#{config.http || ''} | ||
export no_proxy=#{config.no_proxy || ''} | ||
CONFIG | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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}" | ||
elsif machine.communicate.test('ls /var/lib/boot2docker/') | ||
"/var/lib/boot2docker/profile" | ||
else | ||
"/etc/default/#{docker_command}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
spec/unit/vagrant-proxyconf/action/configure_docker_proxy_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
78 changes: 78 additions & 0 deletions
78
spec/unit/vagrant-proxyconf/cap/linux/docker_proxy_conf_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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) 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 | ||
|
||
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 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 | ||
end | ||
end | ||
end |