docker_image: allow proxy config (#53905)

* Simplify checks.

* Add use_config_proxy option.

* Add changelog.

* Avoid docker-py crash.
This commit is contained in:
Felix Fontein 2019-03-19 18:58:16 +01:00 committed by ansibot
parent e23b116157
commit 847a86beef
2 changed files with 23 additions and 2 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "docker_image - add option ``build.use_proxy_config`` to pass proxy config from the docker client configuration to the container while building."

View file

@ -118,6 +118,13 @@ options:
description: description:
- CPUs in which to allow execution, e.g., "0-3", "0,1". - CPUs in which to allow execution, e.g., "0-3", "0,1".
type: str type: str
use_config_proxy:
description:
- If set to `yes` and a proxy configuration is specified in the docker client configuration
(by default C($HOME/.docker/config.json)), the corresponding environment variables will
be set in the container being built.
- Needs Docker SDK for Python >= 3.7.0.
type: bool
version_added: "2.8" version_added: "2.8"
archive_path: archive_path:
description: description:
@ -454,6 +461,7 @@ class ImageManager(DockerBaseClass):
self.http_timeout = build.get('http_timeout') self.http_timeout = build.get('http_timeout')
self.push = parameters.get('push') self.push = parameters.get('push')
self.buildargs = build.get('args') self.buildargs = build.get('args')
self.use_config_proxy = build.get('use_config_proxy')
# If name contains a tag, it takes precedence over tag parameter. # If name contains a tag, it takes precedence over tag parameter.
if not is_image_name_id(self.name): if not is_image_name_id(self.name):
@ -711,6 +719,12 @@ class ImageManager(DockerBaseClass):
params['cache_from'] = self.cache_from params['cache_from'] = self.cache_from
if self.network: if self.network:
params['network_mode'] = self.network params['network_mode'] = self.network
if self.use_config_proxy:
params['use_config_proxy'] = self.use_config_proxy
# Due to a bug in docker-py, it will crash if
# use_config_proxy is True and buildargs is None
if 'buildargs' not in params:
params['buildargs'] = {}
for line in self.client.build(**params): for line in self.client.build(**params):
# line = json.loads(line) # line = json.loads(line)
@ -776,6 +790,7 @@ def main():
pull=dict(type='bool'), pull=dict(type='bool'),
rm=dict(type='bool', default=True), rm=dict(type='bool', default=True),
args=dict(type='dict'), args=dict(type='dict'),
use_config_proxy=dict(type='bool'),
)), )),
archive_path=dict(type='path'), archive_path=dict(type='path'),
container_limits=dict(type='dict', options=dict( container_limits=dict(type='dict', options=dict(
@ -811,14 +826,18 @@ def main():
] ]
def detect_build_cache_from(client): def detect_build_cache_from(client):
return bool(client.params['build'] and client.params['build']['cache_from'] is not None) return client.params['build'] and client.params['build']['cache_from'] is not None
def detect_build_network(client): def detect_build_network(client):
return bool(client.params['build'] and client.params['build']['network'] is not None) return client.params['build'] and client.params['build']['network'] is not None
def detect_use_config_proxy(client):
return client.params['build'] and client.params['build']['use_config_proxy'] is not None
option_minimal_versions = dict() option_minimal_versions = dict()
option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from) option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from)
option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network) option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network)
option_minimal_versions["build.use_config_proxy"] = dict(docker_py_version='3.7.0', detect_usage=detect_use_config_proxy)
client = AnsibleDockerClient( client = AnsibleDockerClient(
argument_spec=argument_spec, argument_spec=argument_spec,