def test_get_create_service_kwargs(self): kwargs = _get_create_service_kwargs('test', { 'image': 'foo', 'command': 'true', 'name': 'somename', 'labels': {'key': 'value'}, 'hostname': 'test_host', 'mode': 'global', 'update_config': {'update': 'config'}, 'networks': ['somenet'], 'endpoint_spec': {'blah': 'blah'}, 'container_labels': {'containerkey': 'containervalue'}, 'resources': {'foo': 'bar'}, 'restart_policy': {'restart': 'policy'}, 'log_driver': 'logdriver', 'log_driver_options': {'foo': 'bar'}, 'args': ['some', 'args'], 'env': {'FOO': 'bar'}, 'workdir': '/', 'user': '******', 'mounts': [{'some': 'mounts'}], 'stop_grace_period': 5, 'constraints': ['foo=bar'], 'preferences': ['bar=baz'], 'platforms': [('x86_64', 'linux')], 'maxreplicas': 1 }) task_template = kwargs.pop('task_template') assert kwargs == { 'name': 'somename', 'labels': {'key': 'value'}, 'mode': 'global', 'update_config': {'update': 'config'}, 'endpoint_spec': {'blah': 'blah'}, } assert set(task_template.keys()) == set([ 'ContainerSpec', 'Resources', 'RestartPolicy', 'Placement', 'LogDriver', 'Networks' ]) assert task_template['Placement'] == { 'Constraints': ['foo=bar'], 'Preferences': ['bar=baz'], 'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}], 'MaxReplicas': 1, } assert task_template['LogDriver'] == { 'Name': 'logdriver', 'Options': {'foo': 'bar'} } assert task_template['Networks'] == [{'Target': 'somenet'}] assert set(task_template['ContainerSpec'].keys()) == set([ 'Image', 'Command', 'Args', 'Hostname', 'Env', 'Dir', 'User', 'Labels', 'Mounts', 'StopGracePeriod' ])
def test_get_create_service_kwargs(self): kwargs = _get_create_service_kwargs('test', { 'image': 'foo', 'command': 'true', 'name': 'somename', 'labels': {'key': 'value'}, 'hostname': 'test_host', 'mode': 'global', 'update_config': {'update': 'config'}, 'networks': ['somenet'], 'endpoint_spec': {'blah': 'blah'}, 'container_labels': {'containerkey': 'containervalue'}, 'resources': {'foo': 'bar'}, 'restart_policy': {'restart': 'policy'}, 'log_driver': 'logdriver', 'log_driver_options': {'foo': 'bar'}, 'args': ['some', 'args'], 'env': {'FOO': 'bar'}, 'workdir': '/', 'user': '******', 'mounts': [{'some': 'mounts'}], 'stop_grace_period': 5, 'constraints': ['foo=bar'], 'preferences': ['bar=baz'], 'platforms': [('x86_64', 'linux')], }) task_template = kwargs.pop('task_template') assert kwargs == { 'name': 'somename', 'labels': {'key': 'value'}, 'mode': 'global', 'update_config': {'update': 'config'}, 'endpoint_spec': {'blah': 'blah'}, } assert set(task_template.keys()) == set([ 'ContainerSpec', 'Resources', 'RestartPolicy', 'Placement', 'LogDriver', 'Networks' ]) assert task_template['Placement'] == { 'Constraints': ['foo=bar'], 'Preferences': ['bar=baz'], 'Platforms': [{'Architecture': 'x86_64', 'OS': 'linux'}], } assert task_template['LogDriver'] == { 'Name': 'logdriver', 'Options': {'foo': 'bar'} } assert task_template['Networks'] == [{'Target': 'somenet'}] assert set(task_template['ContainerSpec'].keys()) == set([ 'Image', 'Command', 'Args', 'Hostname', 'Env', 'Dir', 'User', 'Labels', 'Mounts', 'StopGracePeriod' ])
def configure(cls, image, service, secrets=None, mounts=None, **kwargs): self = ServiceKwargs() options = service.options deploy_opts = options.get("deploy", {}) prefs = deploy_opts.get("placement", {}).get("preferences", {}) # Map compose options to service options self.image = image self.constraints = deploy_opts.get("placement", {}).get("constraints") self.preferences = [kv for pref in prefs for kv in pref.items()] self.container_labels = options.get("labels") self.endpoint_spec = EndpointSpec( deploy_opts.get("endpoint_mode"), {p.published: p.target for p in options.get("ports", [])}) self.env = options.get("environment", None) self.hostname = options.get("hostname") self.isolation = options.get("isolation") self.labels = { k: v for k, v in (kv.split('=') for kv in deploy_opts.get("labels", [])) } self.log_driver = options.get("logging", {}).get("driver") self.log_driver_options = options.get("logging", {}).get("options") self.mode = ServiceMode(deploy_opts.get("mode", "replicated"), deploy_opts.get("replicas", 1)) self.networks = [ config.SWARM_NETWORK ] # Similar to mounts. I don't see the use case but see the issues resource_opts = deploy_opts.get("resources", {}) if resource_opts: # Unpack any generic_resources defined i.e. gpus and such reservation_opts = resource_opts.get("reservations", {}) generic_resources = {} for generic_resource in reservation_opts.get( "generic_resources", {}): discrete_resource_spec = generic_resource[ "discrete_resource_spec"] generic_resources[discrete_resource_spec[ "kind"]] = discrete_resource_spec["value"] cpu_limit = sfloat(resource_opts.get("limits", {}).get("cpus"), 0) cpu_reservation = sfloat(reservation_opts.get("cpus"), 0) nano_cpu_limit = sint(cpu_limit * 1e9, 0) if cpu_limit is not None else None nano_cpu_reservation = sint( cpu_reservation * 1e9, 0) if cpu_reservation is not None else None self.resources = Resources( cpu_limit=nano_cpu_limit, mem_limit=parse_bytes( resource_opts.get("limits", {}).get("memory", '')), cpu_reservation=nano_cpu_reservation, mem_reservation=parse_bytes(reservation_opts.get("memory", '')), generic_resources=generic_resources) restart_opts = deploy_opts.get("restart_policy", {}) if restart_opts: # Parse the restart policy delay = timeparse(restart_opts.get("delay", "0s")) window = timeparse(restart_opts.get("restart_opts", "0s")) self.restart_policy = RestartPolicy( condition=restart_opts.get("condition", ), delay=delay, max_attempts=sint(restart_opts.get("max_attempts", 0), 0), window=window) self.secrets = secrets self.mounts = mounts # Grab any key word arguments that may have been given [setattr(self, k, v) for k, v in kwargs.items() if hasattr(self, k)] service_kwargs = _get_create_service_kwargs('create', copy.copy(self.__dict__)) # This is needed because aiodocker assumes the Env is a dictionary for some reason... if self.env is not None: service_kwargs["task_template"]["ContainerSpec"]["Env"] = self.env return service_kwargs