def create_task(self, task_body): """ Given the parsed body of a create task request, create the task and start it in a thread :param dict task_body: Dict representing the JSON body of a create task request action - The action the task will execute design_id - The design context the task will execute in node_filter - A filter on which nodes will be affected by the task. The result is an intersection of applying all filters node_names - A list of node hostnames rack_names - A list of rack names that contain the nodes node_tags - A list of tags applied to the nodes :return: The Task object created """ design_id = task_body.get('design_id', None) node_filter = task_body.get('node_filter', None) action = task_body.get('action', None) if design_id is None or action is None: raise errors.InvalidFormat( 'Task creation requires fields design_id, action') task = self.orchestrator.create_task(obj_task.OrchestratorTask, design_id=design_id, action=action, node_filter=node_filter) task_thread = threading.Thread(target=self.orchestrator.execute_task, args=[task.get_id()]) task_thread.start() return task
def create_task(self, task_body, req_context): """General task creation. Given the parsed ``task_body`` of a create task request, create the task and queue it in the database. :param dict task_body: Dict representing the JSON body of a create task request action - The action the task will execute design_ref - A URI reference to the design document set the task should operate on node_filter - A filter on which nodes will be affected by the task. :return: The Task object created """ design_ref = task_body.get('design_ref', None) node_filter = task_body.get('node_filter', None) action = task_body.get('action', None) if design_ref is None or action is None: raise errors.InvalidFormat( 'Task creation requires fields design_ref, action') task = self.orchestrator.create_task(design_ref=design_ref, action=action, node_filter=node_filter, context=req_context) task.set_status(hd_fields.TaskStatus.Queued) task.save() return task
def req_json(self, req): if req.content_length is None or req.content_length == 0: return None if req.content_type is not None and req.content_type.lower() == 'application/json': raw_body = req.stream.read(req.content_length or 0) if raw_body is None: return None try: json_body = json.loads(raw_body.decode('utf-8')) return json_body except json.JSONDecodeError as jex: raise errors.InvalidFormat("%s: Invalid JSON in body: %s" % (req.path, jex)) else: raise errors.InvalidFormat("Requires application/json payload")