diff options
Diffstat (limited to 'docker/models')
| -rw-r--r-- | docker/models/containers.py | 31 | ||||
| -rw-r--r-- | docker/models/networks.py | 21 | ||||
| -rw-r--r-- | docker/models/services.py | 11 | ||||
| -rw-r--r-- | docker/models/swarm.py | 8 |
4 files changed, 58 insertions, 13 deletions
diff --git a/docker/models/containers.py b/docker/models/containers.py index 97a08b9..6ba308e 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -629,6 +629,9 @@ class ContainerCollection(Collection): (e.g. ``SIGINT``). storage_opt (dict): Storage driver options per container as a key-value mapping. + stream (bool): If true and ``detach`` is false, return a log + generator instead of a string. Ignored if ``detach`` is true. + Default: ``False``. sysctls (dict): Kernel parameters to set in the container. tmpfs (dict): Temporary filesystems to mount, as a dictionary mapping a path inside the container to options for that path. @@ -696,6 +699,7 @@ class ContainerCollection(Collection): """ if isinstance(image, Image): image = image.id + stream = kwargs.pop('stream', False) detach = kwargs.pop("detach", False) if detach and remove: if version_gte(self.client.api._version, '1.25'): @@ -723,23 +727,30 @@ class ContainerCollection(Collection): if detach: return container - exit_status = container.wait() - if exit_status != 0: - stdout = False - stderr = True - logging_driver = container.attrs['HostConfig']['LogConfig']['Type'] + out = None if logging_driver == 'json-file' or logging_driver == 'journald': - out = container.logs(stdout=stdout, stderr=stderr) - else: + out = container.logs( + stdout=stdout, stderr=stderr, stream=True, follow=True + ) + + exit_status = container.wait() + if exit_status != 0: out = None + if not kwargs.get('auto_remove'): + out = container.logs(stdout=False, stderr=True) if remove: container.remove() if exit_status != 0: - raise ContainerError(container, exit_status, command, image, out) - return out + raise ContainerError( + container, exit_status, command, image, out + ) + + return out if stream or out is None else b''.join( + [line for line in out] + ) def create(self, image, command=None, **kwargs): """ @@ -873,6 +884,8 @@ RUN_HOST_CONFIG_KWARGS = [ 'cpu_shares', 'cpuset_cpus', 'cpuset_mems', + 'cpu_rt_period', + 'cpu_rt_runtime', 'device_read_bps', 'device_read_iops', 'device_write_bps', diff --git a/docker/models/networks.py b/docker/models/networks.py index 158af99..1c2fbf2 100644 --- a/docker/models/networks.py +++ b/docker/models/networks.py @@ -1,4 +1,5 @@ from ..api import APIClient +from ..utils import version_gte from .containers import Container from .resource import Model, Collection @@ -153,7 +154,7 @@ class NetworkCollection(Collection): resp = self.client.api.create_network(name, *args, **kwargs) return self.get(resp['Id']) - def get(self, network_id): + def get(self, network_id, *args, **kwargs): """ Get a network by its ID. @@ -175,7 +176,9 @@ class NetworkCollection(Collection): If the server returns an error. """ - return self.prepare_model(self.client.api.inspect_network(network_id)) + return self.prepare_model( + self.client.api.inspect_network(network_id, *args, **kwargs) + ) def list(self, *args, **kwargs): """ @@ -184,6 +187,13 @@ class NetworkCollection(Collection): Args: names (:py:class:`list`): List of names to filter by. ids (:py:class:`list`): List of ids to filter by. + filters (dict): Filters to be processed on the network list. + Available filters: + - ``driver=[<driver-name>]`` Matches a network's driver. + - ``label=[<key>]`` or ``label=[<key>=<value>]``. + - ``type=["custom"|"builtin"]`` Filters networks by type. + greedy (bool): Fetch more details for each network individually. + You might want this to get the containers attached to them. Returns: (list of :py:class:`Network`) The networks on the server. @@ -192,8 +202,13 @@ class NetworkCollection(Collection): :py:class:`docker.errors.APIError` If the server returns an error. """ + greedy = kwargs.pop('greedy', False) resp = self.client.api.networks(*args, **kwargs) - return [self.prepare_model(item) for item in resp] + networks = [self.prepare_model(item) for item in resp] + if greedy and version_gte(self.client.api._version, '1.28'): + for net in networks: + net.reload() + return networks def prune(self, filters=None): self.client.api.prune_networks(filters=filters) diff --git a/docker/models/services.py b/docker/models/services.py index 6fc5c2a..009e455 100644 --- a/docker/models/services.py +++ b/docker/models/services.py @@ -251,6 +251,7 @@ CONTAINER_SPEC_KWARGS = [ # kwargs to copy straight over to TaskTemplate TASK_TEMPLATE_KWARGS = [ + 'networks', 'resources', 'restart_policy', ] @@ -261,7 +262,6 @@ CREATE_SERVICE_KWARGS = [ 'labels', 'mode', 'update_config', - 'networks', 'endpoint_spec', ] @@ -295,6 +295,15 @@ def _get_create_service_kwargs(func_name, kwargs): 'Options': kwargs.pop('log_driver_options', {}) } + if func_name == 'update': + if 'force_update' in kwargs: + task_template_kwargs['force_update'] = kwargs.pop('force_update') + + # fetch the current spec by default if updating the service + # through the model + fetch_current_spec = kwargs.pop('fetch_current_spec', True) + create_kwargs['fetch_current_spec'] = fetch_current_spec + # All kwargs should have been consumed by this point, so raise # error if any are left if kwargs: diff --git a/docker/models/swarm.py b/docker/models/swarm.py index 5a253c5..7396e73 100644 --- a/docker/models/swarm.py +++ b/docker/models/swarm.py @@ -29,6 +29,10 @@ class Swarm(Model): """ return self.attrs.get('Version').get('Index') + def get_unlock_key(self): + return self.client.api.get_unlock_key() + get_unlock_key.__doc__ = APIClient.get_unlock_key.__doc__ + def init(self, advertise_addr=None, listen_addr='0.0.0.0:2377', force_new_cluster=False, **kwargs): """ @@ -128,6 +132,10 @@ class Swarm(Model): """ self.attrs = self.client.api.inspect_swarm() + def unlock(self, key): + return self.client.api.unlock_swarm(key) + unlock.__doc__ = APIClient.unlock_swarm.__doc__ + def update(self, rotate_worker_token=False, rotate_manager_token=False, **kwargs): """ |
