Use configfile-provided proxy values to populate buildargs and env values

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2018-02-22 16:18:44 -08:00
parent 551b4b4b31
commit d4106679a6
5 changed files with 61 additions and 14 deletions

View File

@ -117,4 +117,7 @@ def docker_client(environment, version=None, tls_config=None, host=None,
kwargs['user_agent'] = generate_user_agent()
return APIClient(**kwargs)
client = APIClient(**kwargs)
client._original_base_url = kwargs.get('base_url')
return client

View File

@ -793,8 +793,12 @@ class Service(object):
))
container_options['environment'] = merge_environment(
self.options.get('environment'),
override_options.get('environment'))
self._parse_proxy_config(),
merge_environment(
self.options.get('environment'),
override_options.get('environment')
)
)
container_options['labels'] = merge_labels(
self.options.get('labels'),
@ -963,6 +967,9 @@ class Service(object):
if build_args_override:
build_args.update(build_args_override)
for k, v in self._parse_proxy_config().items():
build_args.setdefault(k, v)
# python2 os.stat() doesn't support unicode on some UNIX, so we
# encode it to a bytestring to be safe
path = build_opts.get('context')
@ -1142,6 +1149,31 @@ class Service(object):
raise HealthCheckFailed(ctnr.short_id)
return result
def _parse_proxy_config(self):
client = self.client
if 'proxies' not in client._general_configs:
return {}
docker_host = getattr(client, '_original_base_url', client.base_url)
proxy_config = client._general_configs['proxies'].get(
docker_host, client._general_configs['proxies'].get('default')
) or {}
permitted = {
'ftpProxy': 'FTP_PROXY',
'httpProxy': 'HTTP_PROXY',
'httpsProxy': 'HTTPS_PROXY',
'noProxy': 'NO_PROXY',
}
result = {}
for k, v in proxy_config.items():
if k not in permitted:
continue
result[permitted[k]] = result[permitted[k].lower()] = v
return result
def short_id_alias_exists(container, network):
aliases = container.get(

View File

@ -102,6 +102,7 @@ class CLITestCase(unittest.TestCase):
os.environ['COMPOSE_INTERACTIVE_NO_CLI'] = 'true'
mock_client = mock.create_autospec(docker.APIClient)
mock_client.api_version = DEFAULT_DOCKER_API_VERSION
mock_client._general_configs = {}
project = Project.from_config(
name='composetest',
client=mock_client,
@ -136,6 +137,7 @@ class CLITestCase(unittest.TestCase):
def test_run_service_with_restart_always(self):
mock_client = mock.create_autospec(docker.APIClient)
mock_client.api_version = DEFAULT_DOCKER_API_VERSION
mock_client._general_configs = {}
project = Project.from_config(
name='composetest',

View File

@ -24,6 +24,7 @@ from compose.service import Service
class ProjectTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client._general_configs = {}
def test_from_config_v1(self):
config = Config(

View File

@ -43,6 +43,7 @@ class ServiceTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client.api_version = DEFAULT_DOCKER_API_VERSION
self.mock_client._general_configs = {}
def test_containers(self):
service = Service('db', self.mock_client, 'myproject', image='foo')
@ -743,13 +744,16 @@ class ServiceTest(unittest.TestCase):
'for this service are created on a single host, the port will clash.'.format(name))
class TestServiceNetwork(object):
class TestServiceNetwork(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client.api_version = DEFAULT_DOCKER_API_VERSION
self.mock_client._general_configs = {}
def test_connect_container_to_networks_short_aliase_exists(self):
mock_client = mock.create_autospec(docker.APIClient)
service = Service(
'db',
mock_client,
self.mock_client,
'myproject',
image='foo',
networks={'project_default': {}})
@ -768,8 +772,8 @@ class TestServiceNetwork(object):
True)
service.connect_container_to_networks(container)
assert not mock_client.disconnect_container_from_network.call_count
assert not mock_client.connect_container_to_network.call_count
assert not self.mock_client.disconnect_container_from_network.call_count
assert not self.mock_client.connect_container_to_network.call_count
def sort_by_name(dictionary_list):
@ -814,6 +818,10 @@ class BuildUlimitsTestCase(unittest.TestCase):
class NetTestCase(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client.api_version = DEFAULT_DOCKER_API_VERSION
self.mock_client._general_configs = {}
def test_network_mode(self):
network_mode = NetworkMode('host')
@ -831,12 +839,11 @@ class NetTestCase(unittest.TestCase):
def test_network_mode_service(self):
container_id = 'bbbb'
service_name = 'web'
mock_client = mock.create_autospec(docker.APIClient)
mock_client.containers.return_value = [
self.mock_client.containers.return_value = [
{'Id': container_id, 'Name': container_id, 'Image': 'abcd'},
]
service = Service(name=service_name, client=mock_client)
service = Service(name=service_name, client=self.mock_client)
network_mode = ServiceNetworkMode(service)
assert network_mode.id == service_name
@ -845,10 +852,9 @@ class NetTestCase(unittest.TestCase):
def test_network_mode_service_no_containers(self):
service_name = 'web'
mock_client = mock.create_autospec(docker.APIClient)
mock_client.containers.return_value = []
self.mock_client.containers.return_value = []
service = Service(name=service_name, client=mock_client)
service = Service(name=service_name, client=self.mock_client)
network_mode = ServiceNetworkMode(service)
assert network_mode.id == service_name
@ -884,6 +890,7 @@ class ServiceVolumesTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client.api_version = DEFAULT_DOCKER_API_VERSION
self.mock_client._general_configs = {}
def test_build_volume_binding(self):
binding = build_volume_binding(VolumeSpec.parse('/outside:/inside', True))
@ -1118,6 +1125,8 @@ class ServiceVolumesTest(unittest.TestCase):
class ServiceSecretTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.APIClient)
self.mock_client.api_version = DEFAULT_DOCKER_API_VERSION
self.mock_client._general_configs = {}
def test_get_secret_volumes(self):
secret1 = {