diff --git a/docker/utils/__init__.py b/docker/utils/__init__.py index 411d278c..3594b9cd 100644 --- a/docker/utils/__init__.py +++ b/docker/utils/__init__.py @@ -5,4 +5,4 @@ from .utils import ( create_container_config, parse_bytes, ping_registry ) # flake8: noqa -from .types import Ulimit # flake8: noqa \ No newline at end of file +from .types import Ulimit, LogConfig # flake8: noqa \ No newline at end of file diff --git a/docker/utils/types.py b/docker/utils/types.py index f960afb0..d742fd0a 100644 --- a/docker/utils/types.py +++ b/docker/utils/types.py @@ -1,12 +1,63 @@ import six +class LogConfigTypesEnum(object): + _values = ( + 'json-file', + 'syslog', + 'none' + ) + JSON, SYSLOG, NONE = _values + + 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): + type_ = kwargs.get('type', kwargs.get('Type')) + config = kwargs.get('config', kwargs.get('Config')) + if type_ not in self.types._values: + raise ValueError("LogConfig.type must be one of ({0})".format( + ', '.join(self.types._values) + )) + if config and not isinstance(config, dict): + raise ValueError("LogConfig.config must be a dictionary") + + super(LogConfig, self).__init__({ + 'Type': type_, + 'Config': config or {} + }) + + @property + def type(self): + return self['Type'] + + @type.setter + def type(self, value): + if value not in self.types._values: + raise ValueError("LogConfig.type must be one of {0}".format( + ', '.join(self.types._values) + )) + 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')) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index fb32904b..975bb4d8 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -28,7 +28,7 @@ import six from .. import errors from .. import tls -from .types import Ulimit +from .types import Ulimit, LogConfig DEFAULT_HTTP_HOST = "127.0.0.1" @@ -359,7 +359,7 @@ def create_host_config( dns=None, dns_search=None, volumes_from=None, network_mode=None, restart_policy=None, cap_add=None, cap_drop=None, devices=None, extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None, - security_opt=None, ulimits=None + security_opt=None, ulimits=None, log_config=None ): host_config = {} @@ -456,13 +456,24 @@ def create_host_config( if not isinstance(ulimits, list): raise errors.DockerException( 'Invalid type for ulimits param: expected list but found' - ' {0}'.format(type(ulimits))) + ' {0}'.format(type(ulimits)) + ) host_config['Ulimits'] = [] for l in ulimits: if not isinstance(l, Ulimit): l = Ulimit(**l) host_config['Ulimits'].append(l) + if log_config is not None: + if not isinstance(log_config, LogConfig): + if not isinstance(log_config, dict): + raise errors.DockerException( + 'Invalid type for log_config param: expected LogConfig but' + ' found {0}'.format(type(log_config)) + ) + log_config = LogConfig(**log_config) + host_config['LogConfig'] = log_config + return host_config diff --git a/docs/hostconfig.md b/docs/hostconfig.md index 1d82c830..99ffe1c6 100644 --- a/docs/hostconfig.md +++ b/docs/hostconfig.md @@ -85,8 +85,11 @@ for example: * read_only (bool): mount the container's root filesystem as read only * pid_mode (str): if set to "host", use the host PID namespace inside the container -* security_opt (list): A list of string values to customize labels for MLS systems, such as SELinux. -* ulimits (list): A list of dicts or `docker.utils.Ulimit` objects. +* security_opt (list): A list of string values to customize labels for MLS + systems, such as SELinux. +* ulimits (list): A list of dicts or `docker.utils.Ulimit` objects. A list + of ulimits to be set in the container. +* log_config (`docker.utils.LogConfig` or dict): Logging configuration to container **Returns** (dict) HostConfig dictionary