def service_create(self, command, image=None, **kwopts):
     if ('service_create_image_constraint' in self._conf
             or 'service_create_cpus_constraint'
             in self._conf) and 'constraint' not in kwopts:
         kwopts['constraint'] = []
     image = self._get_image(image)
     if self._conf.service_create_image_constraint:
         kwopts['constraint'].append((IMAGE_CONSTRAINT, '==', image))
     if self._conf.service_create_cpus_constraint:
         cpus = kwopts.get('reserve_cpus', kwopts.get('limit_cpus', '1'))
         kwopts['constraint'].append((CPUS_CONSTRAINT, '==', cpus))
     if self._conf.cpus:
         kwopts['cpu_limit'] = self._conf.cpus
         kwopts['cpu_reservation'] = self._conf.cpus
     if self._conf.memory:
         kwopts['mem_limit'] = self._conf.memory
         kwopts['mem_reservation'] = self._conf.memory
     self.set_kwopts_name(kwopts)
     args = '{kwopts} {image} {command}'.format(
         kwopts=self._stringify_kwopts(kwopts),
         image=image if image else '',
         command=command if command else '',
     ).strip()
     service_id = self._run_docker(subcommand='service create',
                                   args=args,
                                   verbose=True)
     return DockerService.from_id(self, service_id)
 def service_create(self, command, image=None, **kwopts):
     # TODO: some of this should probably move to run_in_container when the CLI interface is removed
     log.debug("Creating docker service with image '%s' for command: %s",
               image, command)
     # insert run kwopts from config
     for opt in self.conf_run_kwopts:
         if self._conf[opt]:
             kwopts[opt] = self._conf[opt]
     # image is part of the container spec
     kwopts['image'] = self._get_image(image)
     # service constraints
     kwopts['constraint'] = kwopts.get('constraint', [])
     if self._conf.service_create_image_constraint:
         kwopts['constraint'].append((IMAGE_CONSTRAINT + '==' + image))
     if self._conf.service_create_cpus_constraint:
         cpus = kwopts.get('reserve_cpus', kwopts.get('limit_cpus', '1'))
         kwopts['constraint'].append((CPUS_CONSTRAINT + '==' + cpus))
     # ports
     if 'publish_port_random' in kwopts:
         kwopts['ports'] = [
             DockerSwarmAPIInterface.create_random_port_spec(
                 kwopts.pop('publish_port_random'))
         ]
     # create specs
     service_mode = self._create_docker_api_spec('service_mode',
                                                 docker.types.ServiceMode,
                                                 kwopts)
     endpoint_spec = self._create_docker_api_spec('endpoint_spec',
                                                  docker.types.EndpointSpec,
                                                  kwopts)
     task_template = self._create_docker_api_spec('task_template',
                                                  docker.types.TaskTemplate,
                                                  kwopts)
     self.set_kwopts_name(kwopts)
     log.debug("Docker service task template:\n%s",
               safe_dumps_formatted(task_template))
     log.debug("Docker service endpoint specification:\n%s",
               safe_dumps_formatted(endpoint_spec))
     log.debug("Docker service mode:\n%s",
               safe_dumps_formatted(service_mode))
     log.debug("Docker service creation parameters:\n%s",
               safe_dumps_formatted(kwopts))
     success_test = partial(self._first,
                            self.service_ls,
                            name=kwopts['name'])
     # this can raise exceptions, if necessary we could wrap them in a more generic "creation failed" exception class
     service = self._client.create_service(task_template,
                                           mode=service_mode,
                                           endpoint_spec=endpoint_spec,
                                           success_test=success_test,
                                           max_tries=5,
                                           **kwopts)
     service_id = service.get('ID')
     log.debug('Created service: %s (%s)', kwopts['name'], service_id)
     return DockerService.from_id(self, service_id)
 def service_create(self, command, image=None, **kwopts):
     # TODO: some of this should probably move to run_in_container when the CLI interface is removed
     log.debug("Creating docker service with image '%s' for command: %s", image, command)
     # insert run kwopts from config
     for opt in self.conf_run_kwopts:
         if self._conf[opt]:
             kwopts[opt] = self._conf[opt]
     # image is part of the container spec
     kwopts['image'] = self._get_image(image)
     # service constraints
     kwopts['constraint'] = kwopts.get('constraint', [])
     if self._conf.service_create_image_constraint:
         kwopts['constraint'].append((IMAGE_CONSTRAINT + '==' + image))
     if self._conf.service_create_cpus_constraint:
         cpus = kwopts.get('reserve_cpus', kwopts.get('limit_cpus', '1'))
         kwopts['constraint'].append((CPUS_CONSTRAINT + '==' + cpus))
     # ports
     if 'publish_port_random' in kwopts:
         kwopts['ports'] = [DockerSwarmAPIInterface.create_random_port_spec(kwopts.pop('publish_port_random'))]
     # create specs
     service_mode = self._create_docker_api_spec('service_mode', docker.types.ServiceMode, kwopts)
     endpoint_spec = self._create_docker_api_spec('endpoint_spec', docker.types.EndpointSpec, kwopts)
     task_template = self._create_docker_api_spec('task_template', docker.types.TaskTemplate, kwopts)
     self.set_kwopts_name(kwopts)
     log.debug("Docker service task template:\n%s", safe_dumps_formatted(task_template))
     log.debug("Docker service endpoint specification:\n%s", safe_dumps_formatted(endpoint_spec))
     log.debug("Docker service mode:\n%s", safe_dumps_formatted(service_mode))
     log.debug("Docker service creation parameters:\n%s", safe_dumps_formatted(kwopts))
     success_test = partial(self._first, self.service_ls, name=kwopts['name'])
     # this can raise exceptions, if necessary we could wrap them in a more generic "creation failed" exception class
     service = self._client.create_service(
         task_template,
         mode=service_mode,
         endpoint_spec=endpoint_spec,
         success_test=success_test,
         max_tries=5,
         **kwopts)
     service_id = service.get('ID')
     log.debug('Created service: %s (%s)', kwopts['name'], service_id)
     return DockerService.from_id(self, service_id)
Exemple #4
0
 def service_create(self, command, image=None, **kwopts):
     if ('service_create_image_constraint' in self._conf or 'service_create_cpus_constraint' in self._conf) and 'constraint' not in kwopts:
         kwopts['constraint'] = []
     image = self._get_image(image)
     if self._conf.service_create_image_constraint:
         kwopts['constraint'].append((IMAGE_CONSTRAINT, '==', image))
     if self._conf.service_create_cpus_constraint:
         cpus = kwopts.get('reserve_cpus', kwopts.get('limit_cpus', '1'))
         kwopts['constraint'].append((CPUS_CONSTRAINT, '==', cpus))
     if self._conf.cpus:
         kwopts['cpu_limit'] = self._conf.cpus
         kwopts['cpu_reservation'] = self._conf.cpus
     if self._conf.memory:
         kwopts['mem_limit'] = self._conf.memory
         kwopts['mem_reservation'] = self._conf.memory
     self.set_kwopts_name(kwopts)
     args = '{kwopts} {image} {command}'.format(
         kwopts=self._stringify_kwopts(kwopts),
         image=image if image else '',
         command=command if command else '',
     ).strip()
     service_id = self._run_docker(subcommand='service create', args=args, verbose=True)
     return DockerService.from_id(self, service_id)