def schedule_tasks(self, args): """Schedules one or more Tasks. Required Arguments: * `tasks` - a JSON document containing a list of Tasks and schedule details. Schedule definition format: > All lists are _zero based_ integers. > The [Task Schedule Object](restapi/api-response-objects.html#TaskSchedule){:target="_blank"} response from the `get_task_schedules` command in JSON format can provide schedule definition examples for this command. [ { "Task" : *task name*, "Version" : *optional*, "Months": "*" or [list of months], "DaysOrWeekdays": "Days" = days of the month, "Weekdays" = days of the week (default), "Days": "*" or [list of days], "Hours": "*" or [list of hours], "Minutes": "*" or [list of minutes] }, { ... } ] Returns: Nothing if successful, error messages on failure. """ # 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 = ["tasks"] has_required, resp = api.check_required_params(required_params, args) if not has_required: return resp # so, we'll loop through each task and try to schedule it # keeping track of errors, etc. tasks = [] if args.get("tasks"): try: tasks = json.loads(args["tasks"]) except Exception as ex: return R(err_code=R.Codes.Exception, err_detail="Schedule definition is not valid JSON. %s" % ex) out = [] for t in tasks: if not t.get("Task"): return R(err_code=R.Codes.CreateError, err_detail="Each item in the schedule definition requires a 'task'.") obj = task.Task() obj.FromNameVersion(t["Task"], t.get("Version"), 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.") sched_def = { "Months": t.get("Months"), "Days": t.get("Days"), "Hours": t.get("Hours"), "Minutes": t.get("Minutes"), "DaysOrWeekdays": t.get("DaysOrWeekdays") } # parameters coming from the command line are defined as json OR xml, we need xml parameters = t.get("Parameters") pxml = "" if parameters: # this will convert json OR xml params into the xml format we need pxml = catocommon.params2xml(parameters) obj.RunRepeatedly(sched_def, pxml, t.get("Debug"), t.get("AccountID")) out.append("[%s] successfully scheduled." % obj.Name) return R(response="\n".join(out))
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 ""))