mirror of https://github.com/docker/docker-py.git
utils: add kwargs_from_env
This commit is contained in:
parent
734da2c25c
commit
74e7a67898
|
@ -1,4 +1,4 @@
|
|||
from .utils import (
|
||||
compare_version, convert_port_bindings, convert_volume_binds,
|
||||
mkbuildcontext, ping, tar, parse_repository_tag, parse_host
|
||||
mkbuildcontext, ping, tar, parse_repository_tag, parse_host, kwargs_from_env
|
||||
) # flake8: noqa
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
import io
|
||||
import os
|
||||
import os.path
|
||||
import tarfile
|
||||
import tempfile
|
||||
from distutils.version import StrictVersion
|
||||
|
@ -23,6 +24,7 @@ import requests
|
|||
import six
|
||||
|
||||
from .. import errors
|
||||
from .. import tls
|
||||
|
||||
DEFAULT_HTTP_HOST = "127.0.0.1"
|
||||
DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock"
|
||||
|
@ -257,3 +259,23 @@ def parse_devices(devices):
|
|||
"PathInContainer": path_in_container,
|
||||
"CgroupPermissions": permissions})
|
||||
return device_list
|
||||
|
||||
|
||||
def kwargs_from_env(ssl_version=None, assert_hostname=None):
|
||||
host = os.environ.get('DOCKER_HOST')
|
||||
cert_path = os.environ.get('DOCKER_CERT_PATH')
|
||||
tls_verify = os.environ.get('DOCKER_TLS_VERIFY')
|
||||
|
||||
params = {}
|
||||
if host:
|
||||
params['base_url'] = (host.replace('tcp://', 'https://')
|
||||
if tls_verify else host)
|
||||
if tls_verify and cert_path:
|
||||
params['tls'] = tls.TLSConfig(
|
||||
client_cert=(os.path.join(cert_path, 'cert.pem'),
|
||||
os.path.join(cert_path, 'key.pem')),
|
||||
ca_cert=os.path.join(cert_path, 'ca.pem'),
|
||||
verify=True,
|
||||
ssl_version=ssl_version,
|
||||
assert_hostname=assert_hostname)
|
||||
return params
|
||||
|
|
|
@ -1,12 +1,22 @@
|
|||
import unittest
|
||||
|
||||
from docker.errors import DockerException
|
||||
from docker.utils import parse_repository_tag, parse_host
|
||||
from docker.utils import parse_repository_tag, parse_host, kwargs_from_env
|
||||
from docker.client import Client
|
||||
|
||||
import os
|
||||
import os.path
|
||||
|
||||
|
||||
class UtilsTest(unittest.TestCase):
|
||||
longMessage = True
|
||||
|
||||
def setUp(self):
|
||||
self.os_environ = os.environ.copy()
|
||||
|
||||
def tearDown(self):
|
||||
os.environ = self.os_environ
|
||||
|
||||
def test_parse_repository_tag(self):
|
||||
self.assertEqual(parse_repository_tag("root"),
|
||||
("root", None))
|
||||
|
@ -53,5 +63,25 @@ class UtilsTest(unittest.TestCase):
|
|||
for host, expected in valid_hosts.items():
|
||||
self.assertEqual(parse_host(host), expected, msg=host)
|
||||
|
||||
def test_kwargs_from_env(self):
|
||||
os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
|
||||
DOCKER_CERT_PATH=os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
'testdata/certs'),
|
||||
DOCKER_TLS_VERIFY='1')
|
||||
kwargs = kwargs_from_env(assert_hostname=False)
|
||||
self.assertEquals('https://192.168.59.103:2376', kwargs['base_url'])
|
||||
self.assertIn('ca.pem', kwargs['tls'].verify)
|
||||
self.assertIn('cert.pem', kwargs['tls'].cert[0])
|
||||
self.assertIn('key.pem', kwargs['tls'].cert[1])
|
||||
self.assertEquals(False, kwargs['tls'].assert_hostname)
|
||||
try:
|
||||
client = Client(**kwargs)
|
||||
self.assertEquals(kwargs['base_url'], client.base_url)
|
||||
self.assertEquals(kwargs['tls'].verify, client.verify)
|
||||
self.assertEquals(kwargs['tls'].cert, client.cert)
|
||||
except TypeError, e:
|
||||
self.fail(e)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue