summaryrefslogtreecommitdiff
path: root/docker
diff options
context:
space:
mode:
authorOndřej Nový <onovy@debian.org>2016-10-07 22:56:59 +0200
committerOndřej Nový <onovy@debian.org>2016-10-07 22:56:59 +0200
commit5d8d4569fb7b4e869d9c88fa0f7f703c38f8131e (patch)
treecaa16dd2da7ca381a4cfe7d34bb69b9936422cdc /docker
parent49556cb01423a89a6d2ce7a58b1f5cb64dde0ff3 (diff)
Import python-docker_1.10.3.orig.tar.gz
Diffstat (limited to 'docker')
-rw-r--r--docker/__init__.py14
-rw-r--r--docker/api/__init__.py4
-rw-r--r--docker/api/build.py6
-rw-r--r--docker/api/container.py25
-rw-r--r--docker/api/exec_api.py14
-rw-r--r--docker/api/image.py216
-rw-r--r--docker/api/network.py35
-rw-r--r--docker/api/service.py105
-rw-r--r--docker/api/swarm.py78
-rw-r--r--docker/api/volume.py13
-rw-r--r--docker/auth/auth.py98
-rw-r--r--docker/client.py70
-rw-r--r--docker/constants.py3
-rw-r--r--docker/errors.py13
-rw-r--r--docker/transport/npipeconn.py10
-rw-r--r--docker/transport/npipesocket.py2
-rw-r--r--docker/transport/unixconn.py33
-rw-r--r--docker/types/__init__.py7
-rw-r--r--docker/types/base.py7
-rw-r--r--docker/types/containers.py92
-rw-r--r--docker/types/services.py181
-rw-r--r--docker/types/swarm.py40
-rw-r--r--docker/utils/__init__.py14
-rw-r--r--docker/utils/decorators.py4
-rw-r--r--docker/utils/socket.py68
-rw-r--r--docker/utils/types.py97
-rw-r--r--docker/utils/utils.py145
-rw-r--r--docker/version.py2
28 files changed, 1028 insertions, 368 deletions
diff --git a/docker/__init__.py b/docker/__init__.py
index 84d0734..ad53805 100644
--- a/docker/__init__.py
+++ b/docker/__init__.py
@@ -1,17 +1,3 @@
-# Copyright 2013 dotCloud inc.
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-# http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
from .version import version, version_info
__version__ = version
diff --git a/docker/api/__init__.py b/docker/api/__init__.py
index 9e74428..bc7e93c 100644
--- a/docker/api/__init__.py
+++ b/docker/api/__init__.py
@@ -4,5 +4,7 @@ from .container import ContainerApiMixin
from .daemon import DaemonApiMixin
from .exec_api import ExecApiMixin
from .image import ImageApiMixin
-from .volume import VolumeApiMixin
from .network import NetworkApiMixin
+from .service import ServiceApiMixin
+from .swarm import SwarmApiMixin
+from .volume import VolumeApiMixin
diff --git a/docker/api/build.py b/docker/api/build.py
index 971a50e..7403716 100644
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -18,7 +18,8 @@ class BuildApiMixin(object):
custom_context=False, encoding=None, pull=False,
forcerm=False, dockerfile=None, container_limits=None,
decode=False, buildargs=None, gzip=False):
- remote = context = headers = None
+ remote = context = None
+ headers = {}
container_limits = container_limits or {}
if path is None and fileobj is None:
raise TypeError("Either path or fileobj needs to be provided.")
@@ -134,8 +135,7 @@ class BuildApiMixin(object):
', '.join(repr(k) for k in self._auth_configs.keys())
)
)
- if headers is None:
- headers = {}
+
if utils.compare_version('1.19', self._version) >= 0:
headers['X-Registry-Config'] = auth.encode_header(
self._auth_configs
diff --git a/docker/api/container.py b/docker/api/container.py
index 9cc14db..b8507d8 100644
--- a/docker/api/container.py
+++ b/docker/api/container.py
@@ -15,12 +15,18 @@ class ContainerApiMixin(object):
'logs': logs and 1 or 0,
'stdout': stdout and 1 or 0,
'stderr': stderr and 1 or 0,
- 'stream': stream and 1 or 0,
+ 'stream': stream and 1 or 0
}
+
+ headers = {
+ 'Connection': 'Upgrade',
+ 'Upgrade': 'tcp'
+ }
+
u = self._url("/containers/{0}/attach", container)
- response = self._post(u, params=params, stream=stream)
+ response = self._post(u, headers=headers, params=params, stream=stream)
- return self._get_result(container, stream, response)
+ return self._read_from_socket(response, stream)
@utils.check_resource
def attach_socket(self, container, params=None, ws=False):
@@ -34,9 +40,18 @@ class ContainerApiMixin(object):
if ws:
return self._attach_websocket(container, params)
+ headers = {
+ 'Connection': 'Upgrade',
+ 'Upgrade': 'tcp'
+ }
+
u = self._url("/containers/{0}/attach", container)
- return self._get_raw_response_socket(self.post(
- u, None, params=self._attach_params(params), stream=True))
+ return self._get_raw_response_socket(
+ self.post(
+ u, None, params=self._attach_params(params), stream=True,
+ headers=headers
+ )
+ )
@utils.check_resource
def commit(self, container, repository=None, tag=None, message=None,
diff --git a/docker/api/exec_api.py b/docker/api/exec_api.py
index f0e4afa..6e49996 100644
--- a/docker/api/exec_api.py
+++ b/docker/api/exec_api.py
@@ -56,8 +56,6 @@ class ExecApiMixin(object):
def exec_start(self, exec_id, detach=False, tty=False, stream=False,
socket=False):
# we want opened socket if socket == True
- if socket:
- stream = True
if isinstance(exec_id, dict):
exec_id = exec_id.get('Id')
@@ -66,10 +64,18 @@ class ExecApiMixin(object):
'Detach': detach
}
+ headers = {} if detach else {
+ 'Connection': 'Upgrade',
+ 'Upgrade': 'tcp'
+ }
+
res = self._post_json(
- self._url('/exec/{0}/start', exec_id), data=data, stream=stream
+ self._url('/exec/{0}/start', exec_id),
+ headers=headers,
+ data=data,
+ stream=True
)
if socket:
return self._get_raw_response_socket(res)
- return self._get_result_tty(stream, res, tty)
+ return self._read_from_socket(res, stream)
diff --git a/docker/api/image.py b/docker/api/image.py
index 3e66347..7f25f9d 100644
--- a/docker/api/image.py
+++ b/docker/api/image.py
@@ -1,4 +1,5 @@
import logging
+import os
import six
import warnings
@@ -42,87 +43,79 @@ class ImageApiMixin(object):
return [x['Id'] for x in res]
return res
- def import_image(self, src=None, repository=None, tag=None, image=None):
- if src:
- if isinstance(src, six.string_types):
- try:
- result = self.import_image_from_file(
- src, repository=repository, tag=tag)
- except IOError:
- result = self.import_image_from_url(
- src, repository=repository, tag=tag)
- else:
- result = self.import_image_from_data(
- src, repository=repository, tag=tag)
- elif image:
- result = self.import_image_from_image(
- image, repository=repository, tag=tag)
- else:
- raise Exception("Must specify a src or image")
-
- return result
-
- def import_image_from_data(self, data, repository=None, tag=None):
- u = self._url("/images/create")
- params = {
- 'fromSrc': '-',
- 'repo': repository,
- 'tag': tag
- }
- headers = {
- 'Content-Type': 'application/tar',
- }
- return self._result(
- self._post(u, data=data, params=params, headers=headers))
+ def import_image(self, src=None, repository=None, tag=None, image=None,
+ changes=None, stream_src=False):
+ if not (src or image):
+ raise errors.DockerException(
+ 'Must specify src or image to import from'
+ )
+ u = self._url('/images/create')
- def import_image_from_file(self, filename, repository=None, tag=None):
- u = self._url("/images/create")
- params = {
- 'fromSrc': '-',
- 'repo': repository,
- 'tag': tag
- }
- headers = {
- 'Content-Type': 'application/tar',
- }
- with open(filename, 'rb') as f:
+ params = _import_image_params(
+ repository, tag, image,
+ src=(src if isinstance(src, six.string_types) else None),
+ changes=changes
+ )
+ headers = {'Content-Type': 'application/tar'}
+
+ if image or params.get('fromSrc') != '-': # from image or URL
+ return self._result(
+ self._post(u, data=None, params=params)
+ )
+ elif isinstance(src, six.string_types): # from file path
+ with open(src, 'rb') as f:
+ return self._result(
+ self._post(
+ u, data=f, params=params, headers=headers, timeout=None
+ )
+ )
+ else: # from raw data
+ if stream_src:
+ headers['Transfer-Encoding'] = 'chunked'
return self._result(
- self._post(u, data=f, params=params, headers=headers,
- timeout=None))
+ self._post(u, data=src, params=params, headers=headers)
+ )
- def import_image_from_stream(self, stream, repository=None, tag=None):
- u = self._url("/images/create")
- params = {
- 'fromSrc': '-',
- 'repo': repository,
- 'tag': tag
- }
- headers = {
- 'Content-Type': 'application/tar',
- 'Transfer-Encoding': 'chunked',
- }
+ def import_image_from_data(self, data, repository=None, tag=None,
+ changes=None):
+ u = self._url('/images/create')
+ params = _import_image_params(
+ repository, tag, src='-', changes=changes
+ )
+ headers = {'Content-Type': 'application/tar'}
return self._result(
- self._post(u, data=stream, params=params, headers=headers))
+ self._post(
+ u, data=data, params=params, headers=headers, timeout=None
+ )
+ )
+ return self.import_image(
+ src=data, repository=repository, tag=tag, changes=changes
+ )
- def import_image_from_url(self, url, repository=None, tag=None):
- u = self._url("/images/create")
- params = {
- 'fromSrc': url,
- 'repo': repository,
- 'tag': tag
- }
- return self._result(
- self._post(u, data=None, params=params))
+ def import_image_from_file(self, filename, repository=None, tag=None,
+ changes=None):
+ return self.import_image(
+ src=filename, repository=repository, tag=tag, changes=changes
+ )
- def import_image_from_image(self, image, repository=None, tag=None):
- u = self._url("/images/create")
- params = {
- 'fromImage': image,
- 'repo': repository,
- 'tag': tag
- }
- return self._result(
- self._post(u, data=None, params=params))
+ def import_image_from_stream(self, stream, repository=None, tag=None,
+ changes=None):
+ return self.import_image(
+ src=stream, stream_src=True, repository=repository, tag=tag,
+ changes=changes
+ )
+
+ def import_image_from_url(self, url, repository=None, tag=None,
+ changes=None):
+ return self.import_image(
+ src=url, repository=repository, tag=tag, changes=changes
+ )
+
+ def import_image_from_image(self, image, repository=None, tag=None,
+ changes=None):
+ return self.import_image(
+ image=image, repository=repository, tag=tag, changes=changes
+ )
@utils.check_resource
def insert(self, image, url, path):
@@ -166,28 +159,10 @@ class ImageApiMixin(object):
headers = {}
if utils.compare_version('1.5', self._version) >= 0:
- # If we don't have any auth data so far, try reloading the config
- # file one more time in case anything showed up in there.
if auth_config is None:
- log.debug('Looking for auth config')
- if not self._auth_configs:
- log.debug(
- "No auth config in memory - loading from filesystem"
- )
- self._auth_configs = auth.load_config()
- authcfg = auth.resolve_authconfig(self._auth_configs, registry)
- # Do not fail here if no authentication exists for this
- # specific registry as we can have a readonly pull. Just
- # put the header if we can.
- if authcfg:
- log.debug('Found auth config')
- # auth_config needs to be a dict in the format used by
- # auth.py username , password, serveraddress, email
- headers['X-Registry-Auth'] = auth.encode_header(
- authcfg
- )
- else:
- log.debug('No auth config found')
+ header = auth.get_config_header(self, registry)
+ if header:
+ headers['X-Registry-Auth'] = header
else:
log.debug('Sending supplied auth config')
headers['X-Registry-Auth'] = auth.encode_header(auth_config)
@@ -205,7 +180,7 @@ class ImageApiMixin(object):
return self._result(response)
def push(self, repository, tag=None, stream=False,
- insecure_registry=False, decode=False):
+ insecure_registry=False, auth_config=None, decode=False):
if insecure_registry:
warnings.warn(
INSECURE_REGISTRY_DEPRECATION_WARNING.format('push()'),
@@ -222,17 +197,13 @@ class ImageApiMixin(object):
headers = {}
if utils.compare_version('1.5', self._version) >= 0:
- # If we don't have any auth data so far, try reloading the config
- # file one more time in case anything showed up in there.
- if not self._auth_configs:
- self._auth_configs = auth.load_config()
- authcfg = auth.resolve_authconfig(self._auth_configs, registry)
-
- # Do not fail here if no authentication exists for this specific
- # registry as we can have a readonly pull. Just put the header if
- # we can.
- if authcfg:
- headers['X-Registry-Auth'] = auth.encode_header(authcfg)
+ if auth_config is None:
+ header = auth.get_config_header(self, registry)
+ if header:
+ headers['X-Registry-Auth'] = header
+ else:
+ log.debug('Sending supplied auth config')
+ headers['X-Registry-Auth'] = auth.encode_header(auth_config)
response = self._post_json(
u, None, headers=headers, stream=stream, params=params
@@ -268,3 +239,32 @@ class ImageApiMixin(object):
res = self._post(url, params=params)
self._raise_for_status(res)
return res.status_code == 201
+
+
+def is_file(src):
+ try:
+ return (
+ isinstance(src, six.string_types) and
+ os.path.isfile(src)
+ )
+ except TypeError: # a data string will make isfile() raise a TypeError
+ return False
+
+
+def _import_image_params(repo, tag, image=None, src=None,
+ changes=None):
+ params = {
+ 'repo': repo,
+ 'tag': tag,
+ }
+ if image:
+ params['fromImage'] = image
+ elif src and not is_file(src):
+ params['fromSrc'] = src
+ else:
+ params['fromSrc'] = '-'
+
+ if changes:
+ params['changes'] = changes
+
+ return params
diff --git a/docker/api/network.py b/docker/api/network.py
index a35f0a4..0ee0dab 100644
--- a/docker/api/network.py
+++ b/docker/api/network.py
@@ -22,7 +22,8 @@ class NetworkApiMixin(object):
@minimum_version('1.21')
def create_network(self, name, driver=None, options=None, ipam=None,
- check_duplicate=None, internal=False):
+ check_duplicate=None, internal=False, labels=None,
+ enable_ipv6=False):
if options is not None and not isinstance(options, dict):
raise TypeError('options must be a dictionary')
@@ -34,6 +35,22 @@ class NetworkApiMixin(object):
'CheckDuplicate': check_duplicate
}
+ if labels is not None:
+ if version_lt(self._version, '1.23'):
+ raise InvalidVersion(
+ 'network labels were introduced in API 1.23'
+ )
+ if not isinstance(labels, dict):
+ raise TypeError('labels must be a dictionary')
+ data["Labels"] = labels
+
+ if enable_ipv6:
+ if version_lt(self._version, '1.23'):
+ raise InvalidVersion(
+ 'enable_ipv6 was introduced in API 1.23'
+ )
+ data['EnableIPv6'] = True
+
if internal:
if version_lt(self._version, '1.22'):
raise InvalidVersion('Internal networks are not '
@@ -60,12 +77,13 @@ class NetworkApiMixin(object):
@minimum_version('1.21')
def connect_container_to_network(self, container, net_id,
ipv4_address=None, ipv6_address=None,
- aliases=None, links=None):
+ aliases=None, links=None,
+ link_local_ips=None):
data = {
"Container": container,
"EndpointConfig": self.create_endpoint_config(
aliases=aliases, links=links, ipv4_address=ipv4_address,
- ipv6_address=ipv6_address
+ ipv6_address=ipv6_address, link_local_ips=link_local_ips
),
}
@@ -75,8 +93,15 @@ class NetworkApiMixin(object):
@check_resource
@minimum_version('1.21')
- def disconnect_container_from_network(self, container, net_id):
- data = {"container": container}
+ def disconnect_container_from_network(self, container, net_id,
+ force=False):
+ data = {"Container": container}
+ if force:
+ if version_lt(self._version, '1.22'):
+ raise InvalidVersion(
+ 'Forced disconnect was introduced in API 1.22'
+ )
+ data['Force'] = force
url = self._url("/networks/{0}/disconnect", net_id)
res = self._post_json(url, data=data)
self._raise_for_status(res)
diff --git a/docker/api/service.py b/docker/api/service.py
new file mode 100644
index 0000000..baebbad
--- /dev/null
+++ b/docker/api/service.py
@@ -0,0 +1,105 @@
+from .. import errors
+from .. import utils
+from ..auth import auth
+
+
+class ServiceApiMixin(object):
+ @utils.minimum_version('1.24')
+ def create_service(
+ self, task_template, name=None, labels=None, mode=None,
+ update_config=None, networks=None, endpoint_config=None
+ ):
+ url = self._url('/services/create')
+ headers = {}
+ image = task_template.get('ContainerSpec', {}).get('Image', None)
+ if image is None:
+ raise errors.DockerException(
+ 'Missing mandatory Image key in ContainerSpec'
+ )
+ registry, repo_name = auth.resolve_repository_name(image)
+ auth_header = auth.get_config_header(self, registry)
+ if auth_header:
+ headers['X-Registry-Auth'] = auth_header
+ data = {
+ 'Name': name,
+ 'Labels': labels,
+ 'TaskTemplate': task_template,
+ 'Mode': mode,
+ 'UpdateConfig': update_config,
+ 'Networks': networks,
+ 'Endpoint': endpoint_config
+ }
+ return self._result(
+ self._post_json(url, data=data, headers=headers), True
+ )
+
+ @utils.minimum_version('1.24')
+ @utils.check_resource
+ def inspect_service(self, service):
+ url = self._url('/services/{0}', service)
+ return self._result(self._get(url), True)
+
+ @utils.minimum_version('1.24')
+ @utils.check_resource
+ def inspect_task(self, task):
+ url = self._url('/tasks/{0}', task)
+ return self._result(self._get(url), True)
+
+ @utils.minimum_version('1.24')
+ @utils.check_resource
+ def remove_service(self, service):
+ url = self._url('/services/{0}', service)
+ resp = self._delete(url)
+ self._raise_for_status(resp)
+ return True
+
+ @utils.minimum_version('1.24')
+ def services(self, filters=None):
+ params = {
+ 'filters': utils.convert_filters(filters) if filters else None
+ }
+ url = self._url('/services')
+ return self._result(self._get(url, params=params), True)
+
+ @utils.minimum_version('1.24')
+ def tasks(self, filters=None):
+ params = {
+ 'filters': utils.convert_filters(filters) if filters else None
+ }
+ url = self._url('/tasks')
+ return self._result(self._get(url, params=params), True)
+
+ @utils.minimum_version('1.24')
+ @utils.check_resource
+ def update_service(self, service, version, task_template=None, name=None,
+ labels=None, mode=None, update_config=None,
+ networks=None, endpoint_config=None):
+ url = self._url('/services/{0}/update', service)
+ data = {}
+ headers = {}
+ if name is not None:
+ data['Name'] = name
+ if labels is not None:
+ data['Labels'] = labels
+ if mode is not None:
+ data['Mode'] = mode
+ if task_template is not None:
+ image = task_template.get('ContainerSpec', {}).get('Image', None)
+ if image is not None:
+ registry, repo_name = auth.resolve_repository_name(image)
+ auth_header = auth.get_config_header(self, registry)
+ if auth_header:
+ headers['X-Registry-Auth'] = auth_header
+ data['TaskTemplate'] = task_template
+ if update_config is not None:
+ data['UpdateConfig'] = update_config
+ if networks is not None:
+ data['Networks'] = networks
+ if endpoint_config is not None:
+ data['Endpoint'] = endpoint_config
+
+ resp = self._post_json(
+ url, data=data, params={'version': version}, headers=headers
+ )
+ self._raise_for_status(resp)
+ return True
diff --git a/docker/api/swarm.py b/docker/api/swarm.py
new file mode 100644
index 0000000..d099364
--- /dev/null
+++ b/docker/api/swarm.py
@@ -0,0 +1,78 @@
+from .. import utils
+import logging
+log = logging.getLogger(__name__)
+
+
+class SwarmApiMixin(object):
+
+ def create_swarm_spec(self, *args, **kwargs):
+ return utils.SwarmSpec(*args, **kwargs)
+
+ @utils.minimum_version('1.24')
+ def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
+ force_new_cluster=False, swarm_spec=None):
+ url = self._url('/swarm/init')
+ if swarm_spec is not None and not isinstance(swarm_spec, dict):
+ raise TypeError('swarm_spec must be a dictionary')
+ data = {
+ 'AdvertiseAddr': advertise_addr,
+ 'ListenAddr': listen_addr,
+ 'ForceNewCluster': force_new_cluster,
+ 'Spec': swarm_spec,
+ }
+ response = self._post_json(url, data=data)
+ self._raise_for_status(response)
+ return True
+
+ @utils.minimum_version('1.24')
+ def inspect_swarm(self):
+ url = self._url('/swarm')
+ return self._result(self._get(url), True)
+
+ @utils.check_resource
+ @utils.minimum_version('1.24')
+ def inspect_node(self, node_id):
+ url = self._url('/nodes/{0}', node_id)
+ return self._result(self._get(url), True)
+
+ @utils.minimum_version('1.24')
+ def join_swarm(self, remote_addrs, join_token, listen_addr=None,
+ advertise_addr=None):
+ data = {
+ "RemoteAddrs": remote_addrs,
+ "ListenAddr": listen_addr,
+ "JoinToken": join_token,
+ "AdvertiseAddr": advertise_addr,
+ }
+ url = self._url('/swarm/join')
+ response = self._post_json(url, data=data)
+ self._raise_for_status(response)
+ return True
+
+ @utils.minimum_version('1.24')
+ def leave_swarm(self, force=False):
+ url = self._url('/swarm/leave')
+ response = self._post(url, params={'force': force})
+ self._raise_for_status(response)
+ return True
+
+ @utils.minimum_version('1.24')
+ def nodes(self, filters=None):
+ url = self._url('/nodes')
+ params = {}
+ if filters:
+ params['filters'] = utils.convert_filters(filters)
+
+ return self._result(self._get(url, params=params), True)
+
+ @utils.minimum_version('1.24')
+ def update_swarm(self, version, swarm_spec=None, rotate_worker_token=False,
+ rotate_manager_token=False):
+ url = self._url('/swarm/update')
+ response = self._post_json(url, data=swarm_spec, params={
+ 'rotateWorkerToken': rotate_worker_token,
+ 'rotateManagerToken': rotate_manager_token,
+ 'version': version
+ })
+ self._raise_for_status(response)
+ return True
diff --git a/docker/api/volume.py b/docker/api/volume.py
index bb8b39b..afc72cb 100644
--- a/docker/api/volume.py
+++ b/docker/api/volume.py
@@ -1,3 +1,4 @@
+from .. import errors
from .. import utils
@@ -11,7 +12,7 @@ class VolumeApiMixin(object):
return self._result(self._get(url, params=params), True)
@utils.minimum_version('1.21')
- def create_volume(self, name, driver=None, driver_opts=None):
+ def create_volume(self, name, driver=None, driver_opts=None, labels=None):
url = self._url('/volumes/create')
if driver_opts is not None and not isinstance(driver_opts, dict):
raise TypeError('driver_opts must be a dictionary')
@@ -21,6 +22,16 @@ class VolumeApiMixin(object):
'Driver': driver,
'DriverOpts': driver_opts,
}
+
+ if labels is not None:
+ if utils.compare_version('1.23', self._version) < 0:
+ raise errors.InvalidVersion(
+ 'volume labels were introduced in API 1.23'
+ )
+ if not isinstance(labels, dict):
+ raise TypeError('labels must be a dictionary')
+ data["Labels"] = labels
+
return self._result(self._post_json(url, data=data), True)
@utils.minimum_version('1.21')
diff --git a/docker/auth/auth.py b/docker/auth/auth.py
index d23e6f3..dc0baea 100644
--- a/docker/auth/auth.py
+++ b/docker/auth/auth.py
@@ -1,22 +1,9 @@
-# Copyright 2013 dotCloud inc.
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-# http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
import base64
import json
import logging
import os
+import dockerpycreds
import six
from .. import errors
@@ -25,6 +12,7 @@ INDEX_NAME = 'docker.io'
INDEX_URL = 'https://{0}/v1/'.format(INDEX_NAME)
DOCKER_CONFIG_FILENAME = os.path.join('.docker', 'config.json')
LEGACY_DOCKER_CONFIG_FILENAME = '.dockercfg'
+TOKEN_USERNAME = '<token>'
log = logging.getLogger(__name__)
@@ -51,6 +39,26 @@ def resolve_index_name(index_name):
return index_name
+def get_config_header(client, registry):
+ log.debug('Looking for auth config')
+ if not client._auth_configs:
+ log.debug(
+ "No auth config in memory - loading from filesystem"
+ )
+ client._auth_configs = load_config()
+ authcfg = resolve_authconfig(client._auth_configs, registry)
+ # Do not fail here if no authentication exists for this
+ # specific registry as we can have a readonly pull. Just
+ # put the header if we can.
+ if authcfg:
+ log.debug('Found auth config')
+ # auth_config needs to be a dict in the format used by
+ # auth.py username , password, serveraddress, email
+ return encode_header(authcfg)
+ log.debug('No auth config found')
+ return None
+
+
def split_repo_name(repo_name):
parts = repo_name.split('/', 1)
if len(parts) == 1 or (
@@ -68,6 +76,13 @@ def resolve_authconfig(authconfig, registry=None):
with full URLs are stripped down to hostnames before checking for a match.
Returns None if no match was found.
"""
+ if 'credsStore' in authconfig:
+ log.debug(
+ 'Using credentials store "{0}"'.format(authconfig['credsStore'])
+ )
+ return _resolve_authconfig_credstore(
+ authconfig, registry, authconfig['credsStore']
+ )
# Default to the public index server
registry = resolve_index_name(registry) if registry else INDEX_NAME
log.debug("Looking for auth entry for {0}".format(repr(registry)))
@@ -85,6 +100,35 @@ def resolve_authconfig(authconfig, registry=None):
return None
+def _resolve_authconfig_credstore(authconfig, registry, credstore_name):
+ if not registry or registry == INDEX_NAME:
+ # The ecosystem is a little schizophrenic with index.docker.io VS
+ # docker.io - in that case, it seems the full URL is necessary.
+ registry = 'https://index.docker.io/v1/'
+ log.debug("Looking for auth entry for {0}".format(repr(registry)))
+ store = dockerpycreds.Store(credstore_name)
+ try:
+ data = store.get(registry)
+ res = {
+ 'ServerAddress': registry,
+ }
+ if data['Username'] == TOKEN_USERNAME:
+ res['IdentityToken'] = data['Secret']
+ else:
+ res.update({
+ 'Username': data['Username'],
+ 'Password': data['Secret'],
+ })
+ return res
+ except dockerpycreds.CredentialsNotFound as e:
+ log.debug('No entry found')
+ return None
+ except dockerpycreds.StoreError as e:
+ raise errors.DockerException(
+ 'Credentials store error: {0}'.format(repr(e))
+ )
+
+
def convert_to_hostname(url):
return url.replace('http://', '').replace('https://', '').split('/', 1)[0]
@@ -130,6 +174,15 @@ def parse_auth(entries, raise_on_error=False):
'Invalid configuration for registry {0}'.format(registry)
)
return {}
+ if 'identitytoken' in entry:
+ log.debug('Found an IdentityToken entry for registry {0}'.format(
+ registry
+ ))
+ conf[registry] = {
+ 'IdentityToken': entry['identitytoken']
+ }
+ continue # Other values are irrelevant if we have a token, skip.
+
if 'auth' not in entry:
# Starting with engine v1.11 (API 1.23), an empty dictionary is
# a valid value in the auths config.
@@ -138,13 +191,15 @@ def parse_auth(entries, raise_on_error=False):
'Auth data for {0} is absent. Client might be using a '
'credentials store instead.'
)
- return {}
+ conf[registry] = {}
+ continue
username, password = decode_auth(entry['auth'])
log.debug(
'Found entry (registry={0}, username={1})'
.format(repr(registry), repr(username))
)
+
conf[registry] = {
'username': username,
'password': password,
@@ -160,18 +215,24 @@ def find_config_file(config_path=None):
os.path.basename(DOCKER_CONFIG_FILENAME)
) if os.environ.get('DOCKER_CONFIG') else None
- paths = [
+ paths = filter(None, [
config_path, # 1
environment_path, # 2
os.path.join(os.path.expanduser('~'), DOCKER_CONFIG_FILENAME), # 3
os.path.join(
os.path.expanduser('~'), LEGACY_DOCKER_CONFIG_FILENAME
) # 4
- ]
+ ])
+
+ log.debug("Trying paths: {0}".format(repr(paths)))
for path in paths:
- if path and os.path.exists(path):
+ if os.path.exists(path):
+ log.debug("Found file at path: {0}".format(path))
return path
+
+ log.debug("No config file found")
+
return None
@@ -186,7 +247,6 @@ def load_config(config_path=None):
config_file = find_config_file(config_path)
if not config_file:
- log.debug("File doesn't exist")
return {}
try:
diff --git a/docker/client.py b/docker/client.py
index 81e9de9..47ad09e 100644
--- a/docker/client.py
+++ b/docker/client.py
@@ -1,19 +1,6 @@
-# Copyright 2013 dotCloud inc.
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-# http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
import json
import struct
+from functools import partial
import requests
import requests.exceptions
@@ -29,6 +16,7 @@ from .ssladapter import ssladapter
from .tls import TLSConfig
from .transport import UnixAdapter
from .utils import utils, check_resource, update_headers, kwargs_from_env
+from .utils.socket import frames_iter
try:
from .transport import NpipeAdapter
except ImportError:
@@ -46,11 +34,14 @@ class Client(
api.DaemonApiMixin,
api.ExecApiMixin,
api.ImageApiMixin,
- api.VolumeApiMixin,
- api.NetworkApiMixin):
+ api.NetworkApiMixin,
+ api.ServiceApiMixin,
+ api.SwarmApiMixin,
+ api.VolumeApiMixin):
def __init__(self, base_url=None, version=None,
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False,
- user_agent=constants.DEFAULT_USER_AGENT):
+ user_agent=constants.DEFAULT_USER_AGENT,
+ num_pools=constants.DEFAULT_NUM_POOLS):
super(Client, self).__init__()
if tls and not base_url:
@@ -68,8 +59,11 @@ class Client(
base_url, constants.IS_WINDOWS_PLATFORM, tls=bool(tls)
)
if base_url.startswith('http+unix://'):
- self._custom_adapter = UnixAdapter(base_url, timeout)
+ self._custom_adapter = UnixAdapter(
+ base_url, timeout, num_pools=num_pools
+ )
self.mount('http+docker://', self._custom_adapter)
+ self._unmount('http://', 'https://')
self.base_url = 'http+docker://localunixsocket'
elif base_url.startswith('npipe://'):
if not constants.IS_WINDOWS_PLATFORM:
@@ -77,7 +71,9 @@ class Client(
'The npipe:// protocol is only supported on Windows'
)
try:
- self._custom_adapter = NpipeAdapter(base_url, timeout)
+ self._custom_adapter = NpipeAdapter(
+ base_url, timeout, num_pools=num_pools
+ )
except NameError:
raise errors.DockerException(
'Install pypiwin32 package to enable npipe:// support'
@@ -89,7 +85,9 @@ class Client(
if isinstance(tls, TLSConfig):
tls.configure_client(self)
elif tls:
- self._custom_adapter = ssladapter.SSLAdapter()
+ self._custom_adapter = ssladapter.SSLAdapter(
+ num_pools=num_pools
+ )
self.mount('https://', self._custom_adapter)
self.base_url = base_url
@@ -110,7 +108,8 @@ class Client(
@classmethod
def from_env(cls, **kwargs):
- return cls(**kwargs_from_env(**kwargs))
+ version = kwargs.pop('version', None)
+ return cls(version=version, **kwargs_from_env(**kwargs))
def _retrieve_server_version(self):
try:
@@ -155,7 +154,8 @@ class Client(
'instead'.format(arg, type(arg))
)
- args = map(six.moves.urllib.parse.quote_plus, args)
+ quote_f = partial(six.moves.urllib.parse.quote_plus, safe="/:")
+ args = map(quote_f, args)
if kwargs.get('versioned_api', True):
return '{0}/v{1}{2}'.format(
@@ -250,12 +250,20 @@ class Client(
if decode:
if six.PY3:
data = data.decode('utf-8')
- data = json.loads(data)
- yield data
+ # remove the trailing newline
+ data = data.strip()
+ # split the data at any newlines
+ data_list = data.split("\r\n")
+ # load and yield each line seperately
+ for data in data_list:
+ data = json.loads(data)
+ yield data
+ else:
+ yield data
else:
# Response isn't chunked, meaning we probably
# encountered an error immediately
- yield self._result(response)
+ yield self._result(response, json=decode)
def _multiplexed_buffer_helper(self, response):
"""A generator of multiplexed data blocks read from a buffered
@@ -307,6 +315,14 @@ class Client(
for out in response.iter_content(chunk_size=1, decode_unicode=True):
yield out
+ def _read_from_socket(self, response, stream):
+ socket = self._get_raw_response_socket(response)
+
+ if stream:
+ return frames_iter(socket)
+ else:
+ return six.binary_type().join(frames_iter(socket))
+
def _disable_socket_timeout(self, socket):
""" Depending on the combination of python version and whether we're
connecting over http or https, we might need to access _sock, which
@@ -360,6 +376,10 @@ class Client(
[x for x in self._multiplexed_buffer_helper(res)]
)
+ def _unmount(self, *args):
+ for proto in args:
+ self.adapters.pop(proto)
+
def get_adapter(self, url):
try:
return super(Client, self).get_adapter(url)
diff --git a/docker/constants.py b/docker/constants.py
index 904d50e..0c9a020 100644
--- a/docker/constants.py
+++ b/docker/constants.py
@@ -1,7 +1,7 @@
import sys
from .version import version
-DEFAULT_DOCKER_API_VERSION = '1.22'
+DEFAULT_DOCKER_API_VERSION = '1.24'
DEFAULT_TIMEOUT_SECONDS = 60
STREAM_HEADER_SIZE_BYTES = 8
CONTAINER_LIMITS_KEYS = [
@@ -15,3 +15,4 @@ INSECURE_REGISTRY_DEPRECATION_WARNING = \
IS_WINDOWS_PLATFORM = (sys.platform == 'win32')
DEFAULT_USER_AGENT = "docker-py/{0}".format(version)
+DEFAULT_NUM_POOLS = 25
diff --git a/docker/errors.py b/docker/errors.py
index e85910c..97be802 100644
--- a/docker/errors.py
+++ b/docker/errors.py
@@ -1,16 +1,3 @@
-# Copyright 2014 dotCloud inc.
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
import requests
diff --git a/docker/transport/npipeconn.py b/docker/transport/npipeconn.py
index 736ddf6..917fa8b 100644
--- a/docker/transport/npipeconn.py
+++ b/docker/transport/npipeconn.py
@@ -1,6 +1,7 @@
import six
import requests.adapters
+from .. import constants
from .npipesocket import NpipeSocket
if six.PY3:
@@ -33,9 +34,9 @@ class NpipeHTTPConnection(httplib.HTTPConnection, object):
class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
- def __init__(self, npipe_path, timeout=60):
+ def __init__(self, npipe_path, timeout=60, maxsize=10):
super(NpipeHTTPConnectionPool, self).__init__(
- 'localhost', timeout=timeout
+ 'localhost', timeout=timeout, maxsize=maxsize
)
self.npipe_path = npipe_path
self.timeout = timeout
@@ -47,11 +48,12 @@ class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
class NpipeAdapter(requests.adapters.HTTPAdapter):
- def __init__(self, base_url, timeout=60):
+ def __init__(self, base_url, timeout=60,
+ num_pools=constants.DEFAULT_NUM_POOLS):
self.npipe_path = base_url.replace('npipe://', '')
self.timeout = timeout
self.pools = RecentlyUsedContainer(
- 10, dispose_func=lambda p: p.close()
+ num_pools, dispose_func=lambda p: p.close()
)
super(NpipeAdapter, self).__init__()
diff --git a/docker/transport/npipesocket.py b/docker/transport/npipesocket.py
index 35418ef..9010ceb 100644
--- a/docker/transport/npipesocket.py
+++ b/docker/transport/npipesocket.py
@@ -94,7 +94,7 @@ class NpipeSocket(object):
if mode.strip('b') != 'r':
raise NotImplementedError()
rawio = NpipeFileIOBase(self)
- if bufsize is None:
+ if bufsize is None or bufsize < 0:
bufsize = io.DEFAULT_BUFFER_SIZE
return io.BufferedReader(rawio, buffer_size=bufsize)
diff --git a/docker/transport/unixconn.py b/docker/transport/unixconn.py
index f4d83ef..b7905a0 100644
--- a/docker/transport/unixconn.py
+++ b/docker/transport/unixconn.py
@@ -1,20 +1,9 @@
-# Copyright 2013 dotCloud inc.
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-# http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
import six
import requests.adapters
import socket
+from .. import constants
+
if six.PY3:
import http.client as httplib
else:
@@ -25,6 +14,7 @@ try:
except ImportError:
import urllib3
+
RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer
@@ -45,28 +35,31 @@ class UnixHTTPConnection(httplib.HTTPConnection, object):
class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
- def __init__(self, base_url, socket_path, timeout=60):
+ def __init__(self, base_url, socket_path, timeout=60, maxsize=10):
super(UnixHTTPConnectionPool, self).__init__(
- 'localhost', timeout=timeout
+ 'localhost', timeout=timeout, maxsize=maxsize
)
self.base_url = base_url
self.socket_path = socket_path
self.timeout = timeout
def _new_conn(self):
- return UnixHTTPConnection(self.base_url, self.socket_path,
- self.timeout)
+ return UnixHTTPConnection(
+ self.base_url, self.socket_path, self.timeout
+ )
class UnixAdapter(requests.adapters.HTTPAdapter):
- def __init__(self, socket_url, timeout=60):
+ def __init__(self, socket_url, timeout=60,
+ num_pools=constants.DEFAULT_NUM_POOLS):
socket_path = socket_url.replace('http+unix://', '')
if not socket_path.startswith('/'):
socket_path = '/' + socket_path
self.socket_path = socket_path
self.timeout = timeout
- self.pools = RecentlyUsedContainer(10,
- dispose_func=lambda p: p.close())
+ self.pools = RecentlyUsedContainer(
+ num_pools, dispose_func=lambda p: p.close()
+ )
super(UnixAdapter, self).__init__()
def get_connection(self, url, proxies=None):
diff --git a/docker/types/__init__.py b/docker/types/__init__.py
new file mode 100644
index 0000000..3609581
--- /dev/null
+++ b/docker/types/__init__.py
@@ -0,0 +1,7 @@
+# flake8: noqa
+from .containers import LogConfig, Ulimit
+from .services import (
+ ContainerSpec, DriverConfig, Mount, Resources, RestartPolicy, TaskTemplate,
+ UpdateConfig
+)
+from .swarm import SwarmSpec, SwarmExternalCA
diff --git a/docker/types/base.py b/docker/types/base.py
new file mode 100644
index 0000000..6891062
--- /dev/null
+++ b/docker/types/base.py
@@ -0,0 +1,7 @@
+import six
+
+
+class DictType(dict):
+ def __init__(self, init):
+ for k, v in six.iteritems(init):
+ self[k] = v
diff --git a/docker/types/containers.py b/docker/types/containers.py
new file mode 100644
index 0000000..40a44ca
--- /dev/null
+++ b/docker/types/containers.py
@@ -0,0 +1,92 @@
+import six
+
+from .base import DictType
+
+
+class LogConfigTypesEnum(object):
+ _values = (
+ 'json-file',
+ 'syslog',
+ 'journald',
+ 'gelf',
+ 'fluentd',
+ 'none'
+ )
+ JSON, SYSLOG, JOURNALD, GELF, FLUENTD, NONE = _values
+
+
+class LogConfig(DictType):
+ types = LogConfigTypesEnum
+
+ def __init__(self, **kwargs):
+ log_driver_type = kwargs.get('type', kwargs.get('Type'))
+ config = kwargs.get('config', kwargs.get('Config')) or {}
+
+ if config and not isinstance(config, dict):
+ raise ValueError("LogConfig.config must be a dictionary")
+
+ super(LogConfig, self).__init__({
+ 'Type': log_driver_type,
+ 'Config': config
+ })
+
+ @property
+ def type(self):
+ return self['Type']
+
+ @type.setter
+ def type(self, value):
+ self['Type'] = value
+
+ @property
+ def config(self):
+ return self['Config']
+
+ def set_config_value(self, key, value):
+ self.config[key] = value
+
+ def unset_config(self, key):
+ if key in self.config:
+ del self.config[key]
+
+
+class Ulimit(DictType):
+ def __init__(self, **kwargs):
+ name = kwargs.get('name', kwargs.get('Name'))
+ soft = kwargs.get('soft', kwargs.get('Soft'))
+ hard = kwargs.get('hard', kwargs.get('Hard'))
+ if not isinstance(name, six.string_types):
+ raise ValueError("Ulimit.name must be a string")
+ if soft and not isinstance(soft, int):
+ raise ValueError("Ulimit.soft must be an integer")
+ if hard and not isinstance(hard, int):
+ raise ValueError("Ulimit.hard must be an integer")
+ super(Ulimit, self).__init__({
+ 'Name': name,
+ 'Soft': soft,
+ 'Hard': hard
+ })
+
+ @property
+ def name(self):
+ return self['Name']
+
+ @name.setter
+ def name(self, value):
+ self['Name'] = value
+
+ @property
+ def soft(self):
+ return self.get('Soft')
+
+ @soft.setter
+ def soft(self, value):
+ self['Soft'] = value
+
+ @property
+ def hard(self):
+ return self.get('Hard')
+
+ @hard.setter
+ def hard(self, value):
+ self['Hard'] = value
diff --git a/docker/types/services.py b/docker/types/services.py
new file mode 100644
index 0000000..8488d6e
--- /dev/null
+++ b/docker/types/services.py
@@ -0,0 +1,181 @@
+import six
+
+from .. import errors
+
+
+class TaskTemplate(dict):
+ def __init__(self, container_spec, resources=None, restart_policy=None,
+ placement=None, log_driver=None):
+ self['ContainerSpec'] = container_spec
+ if resources:
+ self['Resources'] = resources
+ if restart_policy:
+ self['RestartPolicy'] = restart_policy
+ if placement:
+ self['Placement'] = placement
+ if log_driver:
+ self['LogDriver'] = log_driver
+
+ @property
+ def container_spec(self):
+ return self.get('ContainerSpec')
+
+ @property
+ def resources(self):
+ return self.get('Resources')
+
+ @property
+ def restart_policy(self):
+ return self.get('RestartPolicy')
+
+ @property
+ def placement(self):
+ return self.get('Placement')
+
+
+class ContainerSpec(dict):
+ def __init__(self, image, command=None, args=None, env=None, workdir=None,
+ user=None, labels=None, mounts=None, stop_grace_period=None):
+ from ..utils import split_command # FIXME: circular import
+
+ self['Image'] = image
+
+ if isinstance(command, six.string_types):
+ command = split_command(command)
+ self['Command'] = command
+ self['Args'] = args
+
+ if env is not None:
+ self['Env'] = env
+ if workdir is not None:
+ self['Dir'] = workdir
+ if user is not None:
+ self['User'] = user
+ if labels is not None:
+ self['Labels'] = labels
+ if mounts is not None:
+ for mount in mounts:
+ if isinstance(mount, six.string_types):
+ mounts.append(Mount.parse_mount_string(mount))
+ mounts.remove(mount)
+ self['Mounts'] = mounts
+ if stop_grace_period is not None:
+ self['StopGracePeriod'] = stop_grace_period
+
+
+class Mount(dict):
+ def __init__(self, target, source, type='volume', read_only=False,
+ propagation=None, no_copy=False, labels=None,
+ driver_config=None):
+ self['Target'] = target
+ self['Source'] = source
+ if type not in ('bind', 'volume'):
+ raise errors.DockerError(
+ 'Only acceptable mount types are `bind` and `volume`.'
+ )
+ self['Type'] = type
+
+ if type == 'bind':
+ if propagation is not None:
+ self['BindOptions'] = {
+ 'Propagation': propagation
+ }
+ if any([labels, driver_config, no_copy]):
+ raise errors.DockerError(
+ 'Mount type is binding but volume options have been '
+ 'provided.'
+ )
+ else:
+ volume_opts = {}
+ if no_copy:
+ volume_opts['NoCopy'] = True
+ if labels:
+ volume_opts['Labels'] = labels
+ if driver_config:
+ volume_opts['driver_config'] = driver_config
+ if volume_opts:
+ self['VolumeOptions'] = volume_opts
+ if propagation:
+ raise errors.DockerError(
+ 'Mount type is volume but `propagation` argument has been '
+ 'provided.'
+ )
+
+ @classmethod
+ def parse_mount_string(cls, string):
+ parts = string.split(':')
+ if len(parts) > 3:
+ raise errors.DockerError(
+ 'Invalid mount format "{0}"'.format(string)
+ )
+ if len(parts) == 1:
+ return cls(target=parts[0])
+ else:
+ target = parts[1]
+ source = parts[0]
+ read_only = not (len(parts) == 3 or parts[2] == 'ro')
+ return cls(target, source, read_only=read_only)
+
+
+class Resources(dict):
+ def __init__(self, cpu_limit=None, mem_limit=None, cpu_reservation=None,
+ mem_reservation=None):
+ limits = {}
+ reservation = {}
+ if cpu_limit is not None:
+ limits['NanoCPUs'] = cpu_limit
+ if mem_limit is not None:
+ limits['MemoryBytes'] = mem_limit
+ if cpu_reservation is not None:
+ reservation['NanoCPUs'] = cpu_reservation
+ if mem_reservation is not None:
+ reservation['MemoryBytes'] = mem_reservation
+
+ if limits:
+ self['Limits'] = limits
+ if reservation:
+ self['Reservations'] = reservation
+
+
+class UpdateConfig(dict):
+ def __init__(self, parallelism=0, delay=None, failure_action='continue'):
+ self['Parallelism'] = parallelism
+ if delay is not None:
+ self['Delay'] = delay
+ if failure_action not in ('pause', 'continue'):
+ raise errors.DockerError(
+ 'failure_action must be either `pause` or `continue`.'
+ )
+ self['FailureAction'] = failure_action
+
+
+class RestartConditionTypesEnum(object):
+ _values = (
+ 'none',
+ 'on_failure',
+ 'any',
+ )
+ NONE, ON_FAILURE, ANY = _values
+
+
+class RestartPolicy(dict):
+ condition_types = RestartConditionTypesEnum
+
+ def __init__(self, condition=RestartConditionTypesEnum.NONE, delay=0,
+ max_attempts=0, window=0):
+ if condition not in self.condition_types._values:
+ raise TypeError(
+ 'Invalid RestartPolicy condition {0}'.format(condition)
+ )
+
+ self['Condition'] = condition
+ self['Delay'] = delay
+ self['MaxAttempts'] = max_attempts
+ self['Window'] = window
+
+
+class DriverConfig(dict):
+ def __init__(self, name, options=None):
+ self['Name'] = name
+ if options:
+ self['Options'] = options
diff --git a/docker/types/swarm.py b/docker/types/swarm.py
new file mode 100644
index 0000000..865fde6
--- /dev/null
+++ b/docker/types/swarm.py
@@ -0,0 +1,40 @@
+class SwarmSpec(dict):
+ def __init__(self, task_history_retention_limit=None,
+ snapshot_interval=None, keep_old_snapshots=None,
+ log_entries_for_slow_followers=None, heartbeat_tick=None,
+ election_tick=None, dispatcher_heartbeat_period=None,
+ node_cert_expiry=None, external_ca=None, name=None):
+ if task_history_retention_limit is not None:
+ self['Orchestration'] = {
+ 'TaskHistoryRetentionLimit': task_history_retention_limit
+ }
+ if any([snapshot_interval, keep_old_snapshots,
+ log_entries_for_slow_followers, heartbeat_tick, election_tick]):
+ self['Raft'] = {
+ 'SnapshotInterval': snapshot_interval,
+ 'KeepOldSnapshots': keep_old_snapshots,
+ 'LogEntriesForSlowFollowers': log_entries_for_slow_followers,
+ 'HeartbeatTick': heartbeat_tick,
+ 'ElectionTick': election_tick
+ }
+
+ if dispatcher_heartbeat_period:
+ self['Dispatcher'] = {
+ 'HeartbeatPeriod': dispatcher_heartbeat_period
+ }
+
+ if node_cert_expiry or external_ca:
+ self['CAConfig'] = {
+ 'NodeCertExpiry': node_cert_expiry,
+ 'ExternalCA': external_ca
+ }
+
+ if name is not None:
+ self['Name'] = name
+
+
+class SwarmExternalCA(dict):
+ def __init__(self, url, protocol=None, options=None):
+ self['URL'] = url
+ self['Protocol'] = protocol
+ self['Options'] = options
diff --git a/docker/utils/__init__.py b/docker/utils/__init__.py
index ccc3819..4bb3876 100644
--- a/docker/utils/__init__.py
+++ b/docker/utils/__init__.py
@@ -1,11 +1,13 @@
+# flake8: noqa
from .utils import (
compare_version, convert_port_bindings, convert_volume_binds,
mkbuildcontext, tar, exclude_paths, parse_repository_tag, parse_host,
- kwargs_from_env, convert_filters, datetime_to_timestamp, create_host_config,
- create_container_config, parse_bytes, ping_registry, parse_env_file,
- version_lt, version_gte, decode_json_header, split_command,
+ kwargs_from_env, convert_filters, datetime_to_timestamp,
+ create_host_config, create_container_config, parse_bytes, ping_registry,
+ parse_env_file, version_lt, version_gte, decode_json_header, split_command,
create_ipam_config, create_ipam_pool, parse_devices, normalize_links,
-) # flake8: noqa
+)
-from .types import Ulimit, LogConfig # flake8: noqa
-from .decorators import check_resource, minimum_version, update_headers #flake8: noqa
+from ..types import LogConfig, Ulimit
+from ..types import SwarmExternalCA, SwarmSpec
+from .decorators import check_resource, minimum_version, update_headers
diff --git a/docker/utils/decorators.py b/docker/utils/decorators.py
index 7c41a5f..2fe880c 100644
--- a/docker/utils/decorators.py
+++ b/docker/utils/decorators.py
@@ -13,7 +13,7 @@ def check_resource(f):
elif kwargs.get('image'):
resource_id = kwargs.pop('image')
if isinstance(resource_id, dict):
- resource_id = resource_id.get('Id')
+ resource_id = resource_id.get('Id', resource_id.get('ID'))
if not resource_id:
raise errors.NullResource(
'image or container param is undefined'
@@ -40,7 +40,7 @@ def minimum_version(version):
def update_headers(f):
def inner(self, *args, **kwargs):
if 'HttpHeaders' in self._auth_configs:
- if 'headers' not in kwargs:
+ if not kwargs.get('headers'):
kwargs['headers'] = self._auth_configs['HttpHeaders']
else:
kwargs['headers'].update(self._auth_configs['HttpHeaders'])
diff --git a/docker/utils/socket.py b/docker/utils/socket.py
new file mode 100644
index 0000000..ed34350
--- /dev/null
+++ b/docker/utils/socket.py
@@ -0,0 +1,68 @@
+import errno
+import os
+import select
+import struct
+
+import six
+
+
+class SocketError(Exception):
+ pass
+
+
+def read(socket, n=4096):
+ """
+ Reads at most n bytes from socket
+ """
+ recoverable_errors = (errno.EINTR, errno.EDEADLK, errno.EWOULDBLOCK)
+
+ # wait for data to become available
+ select.select([socket], [], [])
+
+ try:
+ if hasattr(socket, 'recv'):
+ return socket.recv(n)
+ return os.read(socket.fileno(), n)
+ except EnvironmentError as e:
+ if e.errno not in recoverable_errors:
+ raise
+
+
+def read_exactly(socket, n):
+ """
+ Reads exactly n bytes from socket
+ Raises SocketError if there isn't enough data
+ """
+ data = six.binary_type()
+ while len(data) < n:
+ next_data = read(socket, n - len(data))
+ if not next_data:
+ raise SocketError("Unexpected EOF")
+ data += next_data
+ return data
+
+
+def next_frame_size(socket):
+ """
+ Returns the size of the next frame of data waiting to be read from socket,
+ according to the protocol defined here:
+
+ https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/attach-to-a-container
+ """
+ try:
+ data = read_exactly(socket, 8)
+ except SocketError:
+ return 0
+
+ _, actual = struct.unpack('>BxxxL', data)
+ return actual
+
+
+def frames_iter(socket):
+ """
+ Returns a generator of frames read from socket
+ """
+ n = next_frame_size(socket)
+ while n > 0:
+ yield read(socket, n)
+ n = next_frame_size(socket)
diff --git a/docker/utils/types.py b/docker/utils/types.py
index ea9f06d..8098c47 100644
--- a/docker/utils/types.py
+++ b/docker/utils/types.py
@@ -1,96 +1,7 @@
-import six
+# Compatibility module. See https://github.com/docker/docker-py/issues/1196
+import warnings
-class LogConfigTypesEnum(object):
- _values = (
- 'json-file',
- 'syslog',
- 'journald',
- 'gelf',
- 'fluentd',
- 'none'
- )
- JSON, SYSLOG, JOURNALD, GELF, FLUENTD, NONE = _values
+from ..types import Ulimit, LogConfig # flake8: noqa
-
-class DictType(dict):
- def __init__(self, init):
- for k, v in six.iteritems(init):
- self[k] = v
-
-
-class LogConfig(DictType):
- types = LogConfigTypesEnum
-
- def __init__(self, **kwargs):
- log_driver_type = kwargs.get('type', kwargs.get('Type'))
- config = kwargs.get('config', kwargs.get('Config')) or {}
-
- if config and not isinstance(config, dict):
- raise ValueError("LogConfig.config must be a dictionary")
-
- super(LogConfig, self).__init__({
- 'Type': log_driver_type,
- 'Config': config
- })
-
- @property
- def type(self):
- return self['Type']
-
- @type.setter
- def type(self, value):
- self['Type'] = value
-
- @property
- def config(self):
- return self['Config']
-
- def set_config_value(self, key, value):
- self.config[key] = value
-
- def unset_config(self, key):
- if key in self.config:
- del self.config[key]
-
-
-class Ulimit(DictType):
- def __init__(self, **kwargs):
- name = kwargs.get('name', kwargs.get('Name'))
- soft = kwargs.get('soft', kwargs.get('Soft'))
- hard = kwargs.get('hard', kwargs.get('Hard'))
- if not isinstance(name, six.string_types):
- raise ValueError("Ulimit.name must be a string")
- if soft and not isinstance(soft, int):
- raise ValueError("Ulimit.soft must be an integer")
- if hard and not isinstance(hard, int):
- raise ValueError("Ulimit.hard must be an integer")
- super(Ulimit, self).__init__({
- 'Name': name,
- 'Soft': soft,
- 'Hard': hard
- })
-
- @property
- def name(self):
- return self['Name']
-
- @name.setter
- def name(self, value):
- self['Name'] = value
-
- @property
- def soft(self):
- return self.get('Soft')
-
- @soft.setter
- def soft(self, value):
- self['Soft'] = value
-
- @property
- def hard(self):
- return self.get('Hard')
-
- @hard.setter
- def hard(self, value):
- self['Hard'] = value
+warnings.warn('docker.utils.types is now docker.types', ImportWarning)
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index 2ef8ef0..d46f8fc 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -1,17 +1,3 @@
-# Copyright 2013 dotCloud inc.
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-# http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
import base64
import io
import os
@@ -22,8 +8,8 @@ import tarfile
import tempfile
import warnings
from distutils.version import StrictVersion
-from fnmatch import fnmatch
from datetime import datetime
+from fnmatch import fnmatch
import requests
import six
@@ -31,11 +17,17 @@ import six
from .. import constants
from .. import errors
from .. import tls
-from .types import Ulimit, LogConfig
+from ..types import Ulimit, LogConfig
+if six.PY2:
+ from urllib import splitnport
+else:
+ from urllib.parse import splitnport
DEFAULT_HTTP_HOST = "127.0.0.1"
DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock"
+DEFAULT_NPIPE = 'npipe:////./pipe/docker_engine'
+
BYTE_UNITS = {
'b': 1,
'k': 1024,
@@ -385,12 +377,11 @@ def parse_repository_tag(repo_name):
# Protocol translation: tcp -> http, unix -> http+unix
def parse_host(addr, is_win32=False, tls=False):
proto = "http+unix"
- host = DEFAULT_HTTP_HOST
port = None
path = ''
if not addr and is_win32:
- addr = '{0}:{1}'.format(DEFAULT_HTTP_HOST, 2375)
+ addr = DEFAULT_NPIPE
if not addr or addr.strip() == 'unix://':
return DEFAULT_UNIX_SOCKET
@@ -425,32 +416,27 @@ def parse_host(addr, is_win32=False, tls=False):
)
proto = "https" if tls else "http"
- if proto != "http+unix" and ":" in addr:
- host_parts = addr.split(':')
- if len(host_parts) != 2:
- raise errors.DockerException(
- "Invalid bind address format: {0}".format(addr)
- )
- if host_parts[0]:
- host = host_parts[0]
+ if proto in ("http", "https"):
+ address_parts = addr.split('/', 1)
+ host = address_parts[0]
+ if len(address_parts) == 2:
+ path = '/' + address_parts[1]
+ host, port = splitnport(host)
- port = host_parts[1]
- if '/' in port:
- port, path = port.split('/', 1)
- path = '/{0}'.format(path)
- try:
- port = int(port)
- except Exception:
+ if port is None:
raise errors.DockerException(
"Invalid port: {0}".format(addr)
)
- elif proto in ("http", "https") and ':' not in addr:
- raise errors.DockerException(
- "Bind address needs a port: {0}".format(addr))
+ if not host:
+ host = DEFAULT_HTTP_HOST
else:
host = addr
+ if proto in ("http", "https") and port == -1:
+ raise errors.DockerException(
+ "Bind address needs a port: {0}".format(addr))
+
if proto == "http+unix" or proto == 'npipe':
return "{0}://{1}".format(proto, host)
return "{0}://{1}:{2}{3}".format(proto, host, port, path)
@@ -613,14 +599,17 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
cap_drop=None, devices=None, extra_hosts=None,
read_only=None, pid_mode=None, ipc_mode=None,
security_opt=None, ulimits=None, log_config=None,
- mem_limit=None, memswap_limit=None, mem_swappiness=None,
- cgroup_parent=None, group_add=None, cpu_quota=None,
+ mem_limit=None, memswap_limit=None,
+ mem_reservation=None, kernel_memory=None,
+ mem_swappiness=None, cgroup_parent=None,
+ group_add=None, cpu_quota=None,
cpu_period=None, blkio_weight=None,
blkio_weight_device=None, device_read_bps=None,
device_write_bps=None, device_read_iops=None,
device_write_iops=None, oom_kill_disable=False,
- shm_size=None, version=None, tmpfs=None,
- oom_score_adj=None):
+ shm_size=None, sysctls=None, version=None, tmpfs=None,
+ oom_score_adj=None, dns_opt=None, cpu_shares=None,
+ cpuset_cpus=None, userns_mode=None, pids_limit=None):
host_config = {}
@@ -637,6 +626,18 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
if memswap_limit is not None:
host_config['MemorySwap'] = parse_bytes(memswap_limit)
+ if mem_reservation:
+ if version_lt(version, '1.21'):
+ raise host_config_version_error('mem_reservation', '1.21')
+
+ host_config['MemoryReservation'] = parse_bytes(mem_reservation)
+
+ if kernel_memory:
+ if version_lt(version, '1.21'):
+ raise host_config_version_error('kernel_memory', '1.21')
+
+ host_config['KernelMemory'] = parse_bytes(kernel_memory)
+
if mem_swappiness is not None:
if version_lt(version, '1.20'):
raise host_config_version_error('mem_swappiness', '1.20')
@@ -719,12 +720,25 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
if dns is not None:
host_config['Dns'] = dns
+ if dns_opt is not None:
+ if version_lt(version, '1.21'):
+ raise host_config_version_error('dns_opt', '1.21')
+
+ host_config['DnsOptions'] = dns_opt
+
if security_opt is not None:
if not isinstance(security_opt, list):
raise host_config_type_error('security_opt', security_opt, 'list')
host_config['SecurityOpt'] = security_opt
+ if sysctls:
+ if not isinstance(sysctls, dict):
+ raise host_config_type_error('sysctls', sysctls, 'dict')
+ host_config['Sysctls'] = {}
+ for k, v in six.iteritems(sysctls):
+ host_config['Sysctls'][k] = six.text_type(v)
+
if volumes_from is not None:
if isinstance(volumes_from, six.string_types):
volumes_from = volumes_from.split(',')
@@ -796,6 +810,21 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
host_config['CpuPeriod'] = cpu_period
+ if cpu_shares:
+ if version_lt(version, '1.18'):
+ raise host_config_version_error('cpu_shares', '1.18')
+
+ if not isinstance(cpu_shares, int):
+ raise host_config_type_error('cpu_shares', cpu_shares, 'int')
+
+ host_config['CpuShares'] = cpu_shares
+
+ if cpuset_cpus:
+ if version_lt(version, '1.18'):
+ raise host_config_version_error('cpuset_cpus', '1.18')
+
+ host_config['CpuSetCpus'] = cpuset_cpus
+
if blkio_weight:
if not isinstance(blkio_weight, int):
raise host_config_type_error('blkio_weight', blkio_weight, 'int')
@@ -853,6 +882,21 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
raise host_config_version_error('tmpfs', '1.22')
host_config["Tmpfs"] = convert_tmpfs_mounts(tmpfs)
+ if userns_mode:
+ if version_lt(version, '1.23'):
+ raise host_config_version_error('userns_mode', '1.23')
+
+ if userns_mode != "host":
+ raise host_config_value_error("userns_mode", userns_mode)
+ host_config['UsernsMode'] = userns_mode
+
+ if pids_limit:
+ if not isinstance(pids_limit, int):
+ raise host_config_type_error('pids_limit', pids_limit, 'int')
+ if version_lt(version, '1.23'):
+ raise host_config_version_error('pids_limit', '1.23')
+ host_config["PidsLimit"] = pids_limit
+
return host_config
@@ -873,7 +917,8 @@ def create_networking_config(endpoints_config=None):
def create_endpoint_config(version, aliases=None, links=None,
- ipv4_address=None, ipv6_address=None):
+ ipv4_address=None, ipv6_address=None,
+ link_local_ips=None):
if version_lt(version, '1.22'):
raise errors.InvalidVersion(
'Endpoint config is not supported for API version < 1.22'
@@ -893,6 +938,13 @@ def create_endpoint_config(version, aliases=None, links=None,
if ipv6_address:
ipam_config['IPv6Address'] = ipv6_address
+ if link_local_ips is not None:
+ if version_lt(version, '1.24'):
+ raise errors.InvalidVersion(
+ 'link_local_ips is not supported for API version < 1.24'
+ )
+ ipam_config['LinkLocalIPs'] = link_local_ips
+
if ipam_config:
endpoint_config['IPAMConfig'] = ipam_config
@@ -934,7 +986,7 @@ def format_environment(environment):
def format_env(key, value):
if value is None:
return key
- return '{key}={value}'.format(key=key, value=value)
+ return u'{key}={value}'.format(key=key, value=value)
return [format_env(*var) for var in six.iteritems(environment)]
@@ -960,6 +1012,14 @@ def create_container_config(
'labels were only introduced in API version 1.18'
)
+ if cpuset is not None or cpu_shares is not None:
+ if version_gte(version, '1.18'):
+ warnings.warn(
+ 'The cpuset_cpus and cpu_shares options have been moved to '
+ 'host_config in API version 1.18, and will be removed',
+ DeprecationWarning
+ )
+
if stop_signal is not None and compare_version('1.21', version) < 0:
raise errors.InvalidVersion(
'stop_signal was only introduced in API version 1.21'
@@ -989,6 +1049,7 @@ def create_container_config(
if mem_limit is not None:
mem_limit = parse_bytes(mem_limit)
+
if memswap_limit is not None:
memswap_limit = parse_bytes(memswap_limit)
diff --git a/docker/version.py b/docker/version.py
index 95405c7..2bf8436 100644
--- a/docker/version.py
+++ b/docker/version.py
@@ -1,2 +1,2 @@
-version = "1.9.0"
+version = "1.10.3"
version_info = tuple([int(d) for d in version.split("-")[0].split(".")])