def run_schedule_instance(self, instance_row): try: schedule_id = instance_row[0] plan_id = instance_row[1] task_id = instance_row[2] action_id = instance_row[3] parameter_xml = instance_row[4] debug_level = instance_row[5] run_on_dt = instance_row[6] account_id = instance_row[7] cloud_id = instance_row[8] ti = catocommon.add_task_instance(task_id, "", debug_level, parameter_xml, account_id, plan_id, schedule_id, cloud_id=cloud_id) self.logger.info("Started task instance %s for schedule id %s and plan id %s" % (ti, schedule_id, plan_id)) sql = """insert into action_plan_history (plan_id, task_id, run_on_dt, action_id, parameter_xml, debug_level, source, schedule_id, task_instance, account_id, cloud_id) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) on duplicate key update task_id=%s, run_on_dt=%s, action_id=%s, parameter_xml=%s, debug_level=%s, source=%s, schedule_id=%s, task_instance=%s, account_id=%s, cloud_id=%s""" self.db.exec_db(sql, (plan_id, task_id, run_on_dt, action_id, parameter_xml, debug_level, 'schedule', schedule_id, ti, account_id, cloud_id, task_id, run_on_dt, action_id, parameter_xml, debug_level, 'schedule', schedule_id, ti, account_id, cloud_id)) sql = """delete from action_plan where plan_id = %s""" self.db.exec_db(sql, (plan_id)) except Exception as ex: self.logger.error("Unable to run schedule instance. Probable error inserting action_plan_history.\n" + ex.__str__())
def run_task(self, args): """Runs a Task. Required Arguments: * `task` - Either the Task ID or Name. * `version` - The Task Version. (Unnecessary if 'task' is an ID.) Optional Arguments: * `log_level` - an integer (0-4) where 0 is none, 2 is normal and 4 is verbose. Default is 2. * `account` - the ID or Name of a Cloud Account. Certain Task commands require a Cloud Account. * `parameters` - A JSON or XML document defining parameters for the Task. * `options` - a JSON object defining certain options for this Task. Typically used to provide scope for extensions to the Task Engine. * `run_later` - if provided, the Task will be scheduled to run at the specified date/time. ex. "7/4/1776 15:30" Returns: A [Task Instance Object](restapi/api-response-objects.html#TaskInstance){:target="_blank"}. * If 'output_format' is set to 'text', returns only a Task Instance ID. * If 'run_later' was specified, will return a success or error message. """ # this is a developer function if not api._DEVELOPER: return R(err_code=R.Codes.Forbidden, err_msg="Only Developers or Administrators can perform this function.") required_params = ["task"] has_required, resp = api.check_required_params(required_params, args) if not has_required: return resp ver = args["version"] if "version" in args else "" # find the task obj = task.Task() obj.FromNameVersion(args["task"], ver, False) if not api.is_object_allowed(obj.ID, catocommon.CatoObjectTypes.Task): return R(err_code=R.Codes.Forbidden, err_msg="You do not have access to the details of this Task.") task_id = obj.ID debug = args.get("log_level", "20") # not verifying this optional value because that would require importing a maestro lib # just use it as-is options = args.get("options", "") # same for account account_id = "" account = args.get("account", "") if account: ca = cloud.CloudAccount() ca.FromName(args["account"]) if ca: account_id = ca.ID parameters = args.get("parameters", "") pxml = "" if parameters: # this will convert json OR xml params into the xml format we need pxml = catocommon.params2xml(parameters) if args.get("run_later"): obj.RunLater(args.get("run_later"), pxml, debug, account_id) return R(response="[%s] successfully scheduled." % obj.Name) else: # try to launch it ti = catocommon.add_task_instance(task_id, api._USER_ID, debug, pxml, account_id=account_id, options=options) if ti: if args.get("output_format") == "text": return R(response=ti) else: instance = task.TaskInstance(ti) if args.get("output_format") == "json": return R(response=instance.__dict__) else: return R(response=instance.AsXML()) # uh oh, something went wrong but we don't know what. return R(err_code=R.Codes.GetError, err_detail="Unable to run Task [%s%s]. Check the log for details." % (args["task"], " %s" % (ver) if ver else ""))