Kaydet (Commit) bfdd0a88 authored tarafından Corentin Henry's avatar Corentin Henry Kaydeden (comit) Joffrey F

add support for proxies

Signed-off-by: 's avatarCorentin Henry <corentinhenry@gmail.com>
üst 4ca4e94e
......@@ -168,8 +168,11 @@ class BuildApiMixin(object):
}
params.update(container_limits)
final_buildargs = self._proxy_configs.get_environment()
if buildargs:
params.update({'buildargs': json.dumps(buildargs)})
final_buildargs.update(buildargs)
if final_buildargs:
params.update({'buildargs': json.dumps(final_buildargs)})
if shmsize:
if utils.version_gte(self._version, '1.22'):
......
......@@ -34,6 +34,7 @@ from ..transport import SSLAdapter, UnixAdapter
from ..utils import utils, check_resource, update_headers, config
from ..utils.socket import frames_iter, consume_socket_output, demux_adaptor
from ..utils.json_stream import json_stream
from ..utils.proxy import ProxyConfig
try:
from ..transport import NpipeAdapter
except ImportError:
......@@ -114,6 +115,12 @@ class APIClient(
self.headers['User-Agent'] = user_agent
self._general_configs = config.load_general_config()
try:
proxies = self._general_configs['proxies']['default']
except KeyError:
proxies = {}
self._proxy_configs = ProxyConfig.from_dict(proxies)
self._auth_configs = auth.load_config(
config_dict=self._general_configs, credstore_env=credstore_env,
)
......
......@@ -403,6 +403,10 @@ class ContainerApiMixin(object):
if isinstance(volumes, six.string_types):
volumes = [volumes, ]
if isinstance(environment, dict):
environment = utils.utils.format_environment(environment)
environment = self._proxy_configs.inject_proxy_environment(environment)
config = self.create_container_config(
image, command, hostname, user, detach, stdin_open, tty,
ports, environment, volumes,
......
......@@ -50,6 +50,7 @@ class ExecApiMixin(object):
if isinstance(environment, dict):
environment = utils.utils.format_environment(environment)
environment = self._proxy_configs.inject_proxy_environment(environment)
data = {
'Container': container,
......
from .utils import format_environment
class ProxyConfig():
'''
Hold the client's proxy configuration
'''
def __init__(self, http=None, https=None, ftp=None, no_proxy=None):
self.http = http
self.https = https
self.ftp = ftp
self.no_proxy = no_proxy
@staticmethod
def from_dict(config):
'''
Instantiate a new ProxyConfig from a dictionary that represents a
client configuration, as described in `the documentation`_.
.. _the documentation:
https://docs.docker.com/network/proxy/#configure-the-docker-client
'''
return ProxyConfig(
http=config.get('httpProxy', None),
https=config.get('httpsProxy', None),
ftp=config.get('ftpProxy', None),
no_proxy=config.get('noProxy', None))
def get_environment(self):
'''
Return a dictionary representing the environment variables used to
set the proxy settings.
'''
env = {}
if self.http:
env['http_proxy'] = env['HTTP_PROXY'] = self.http
if self.https:
env['https_proxy'] = env['HTTPS_PROXY'] = self.https
if self.ftp:
env['ftp_proxy'] = env['FTP_PROXY'] = self.ftp
if self.no_proxy:
env['no_proxy'] = env['NO_PROXY'] = self.no_proxy
return env
def inject_proxy_environment(self, environment):
'''
Given a list of strings representing environment variables, prepend the
environemt variables corresponding to the proxy settings.
'''
if not self:
return environment
proxy_env = format_environment(self.get_environment())
if not environment:
return proxy_env
# It is important to prepend our variables, because we want the
# variables defined in "environment" to take precedence.
return proxy_env + environment
def __bool__(self):
return bool(self.http or self.https or self.ftp or self.no_proxy)
def __nonzero__(self):
return self.__bool__()
def __str__(self):
return 'ProxyConfig(http={}, https={}, ftp={}, no_proxy={})'.format(
self.http, self.https, self.ftp, self.no_proxy)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment