diff options
| author | Joffrey F <joffrey@docker.com> | 2014-07-10 17:19:22 +0200 |
|---|---|---|
| committer | Joffrey F <joffrey@docker.com> | 2014-07-10 17:19:22 +0200 |
| commit | 7b8e0cd78363630d7f7a803233d69ee26434aa88 (patch) | |
| tree | 1336a32be6f222977ce8bd36a00227485f59e6d4 /docker | |
| parent | 72cb3882b076d4dc569fe1e82779b7862f2ae5b6 (diff) | |
| parent | ed2b4581d76f893086d0808b1dece070742c99ae (diff) | |
| download | docker-py-7b8e0cd78363630d7f7a803233d69ee26434aa88.tar.gz | |
Merge branch 'master' into momer-tls
Conflicts:
docker/client.py
Diffstat (limited to 'docker')
| -rw-r--r-- | docker/client.py | 16 | ||||
| -rw-r--r-- | docker/utils/__init__.py | 2 | ||||
| -rw-r--r-- | docker/utils/utils.py | 69 | ||||
| -rw-r--r-- | docker/version.py | 2 |
4 files changed, 74 insertions, 15 deletions
diff --git a/docker/client.py b/docker/client.py index 4cba6a9..0a12b66 100644 --- a/docker/client.py +++ b/docker/client.py @@ -41,21 +41,13 @@ class Client(requests.Session): def __init__(self, base_url=None, version=DEFAULT_DOCKER_API_VERSION, timeout=DEFAULT_TIMEOUT_SECONDS, tls=False): super(Client, self).__init__() - - if base_url is None: - base_url = "http+unix://var/run/docker.sock" + base_url = utils.parse_host(base_url) + if 'http+unix:///' in base_url: + base_url = base_url.replace('unix:/', 'unix:') if tls and not base_url.startswith('https://'): raise errors.TLSParameterError( 'If using TLS, the base_url argument must begin with ' '"https://".') - if 'unix:///' in base_url: - base_url = base_url.replace('unix:/', 'unix:') - if base_url.startswith('unix:'): - base_url = "http+" + base_url - if base_url.startswith('tcp:'): - base_url = base_url.replace('tcp:', 'http:') - if base_url.endswith('/'): - base_url = base_url[:-1] self.base_url = base_url self._version = version self._timeout = timeout @@ -240,7 +232,7 @@ class Client(requests.Session): # Because Docker introduced newlines at the end of chunks in v0.9, # and only on some API endpoints, we have to cater for both cases. size_line = socket.readline() - if size_line == '\r\n': + if size_line == '\r\n' or size_line == '\n': size_line = socket.readline() size = int(size_line, 16) diff --git a/docker/utils/__init__.py b/docker/utils/__init__.py index 86ddd35..5d2c1b8 100644 --- a/docker/utils/__init__.py +++ b/docker/utils/__init__.py @@ -1,4 +1,4 @@ from .utils import ( compare_version, convert_port_bindings, convert_volume_binds, - mkbuildcontext, ping, tar, parse_repository_tag + mkbuildcontext, ping, tar, parse_repository_tag, parse_host ) # flake8: noqa diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 036a504..f741ee2 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -20,6 +20,11 @@ from distutils.version import StrictVersion import requests import six +from .. import errors + +DEFAULT_HTTP_HOST = "127.0.0.1" +DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock" + def mkbuildcontext(dockerfile): f = tempfile.NamedTemporaryFile() @@ -139,9 +144,71 @@ def parse_repository_tag(repo): column_index = repo.rfind(':') if column_index < 0: return repo, None - tag = repo[column_index+1:] + tag = repo[column_index + 1:] slash_index = tag.find('/') if slash_index < 0: return repo[:column_index], tag return repo, None + + +# Based on utils.go:ParseHost http://tinyurl.com/nkahcfh +# fd:// protocol unsupported (for obvious reasons) +# Added support for http and https +# Protocol translation: tcp -> http, unix -> http+unix +def parse_host(addr): + proto = "http+unix" + host = DEFAULT_HTTP_HOST + port = None + if not addr or addr.strip() == 'unix://': + return DEFAULT_UNIX_SOCKET + + addr = addr.strip() + if addr.startswith('http://'): + addr = addr.replace('http://', 'tcp://') + if addr.startswith('http+unix://'): + addr = addr.replace('http+unix://', 'unix://') + + if addr == 'tcp://': + raise errors.DockerException("Invalid bind address format: %s" % addr) + elif addr.startswith('unix://'): + addr = addr[7:] + elif addr.startswith('tcp://'): + proto = "http" + addr = addr[6:] + elif addr.startswith('https://'): + proto = "https" + addr = addr[8:] + elif addr.startswith('fd://'): + raise errors.DockerException("fd protocol is not implemented") + else: + if "://" in addr: + raise errors.DockerException( + "Invalid bind address protocol: %s" % addr + ) + proto = "http" + + if proto != "http+unix" and ":" in addr: + host_parts = addr.split(':') + if len(host_parts) != 2: + raise errors.DockerException( + "Invalid bind address format: %s" % addr + ) + if host_parts[0]: + host = host_parts[0] + + try: + port = int(host_parts[1]) + except Exception: + raise errors.DockerException( + "Invalid port: %s", addr + ) + + elif proto in ("http", "https") and ':' not in addr: + raise errors.DockerException("Bind address needs a port: %s" % addr) + else: + host = addr + + if proto == "http+unix": + return "%s://%s" % (proto, host) + return "%s://%s:%d" % (proto, host, port) diff --git a/docker/version.py b/docker/version.py index 77035f1..c55028d 100644 --- a/docker/version.py +++ b/docker/version.py @@ -1 +1 @@ -version = "0.3.2-dev" +version = "0.3.3-dev" |
