def update_task(name, context=None, **kwargs): """ Updates the specified task. An exception is raised when the action does not exist. :param name: Name of the task. This name overwrites the name in kwargs if it is used there :param kwargs: Task parameters dictionary, see create_task for details. :param context: Lambda context :return: Updated task item """ with _get_logger(context=context) as logger: logger.info("update_task") config = TaskConfiguration(context=context, logger=logger) if name is None or len(name) == 0: raise ValueError(ERR_NO_TASK_NAME) item = config.get_config_item(name) if item is None: raise ValueError(ERR_TASK_DOES_NOT_EXIST.format(name)) # copy to avoid side effects when modifying arguments args = copy.deepcopy(kwargs) args[configuration.CONFIG_TASK_NAME] = name stack_id = item.get(configuration.CONFIG_STACK_ID) if stack_id is not None: args[configuration.CONFIG_STACK_ID] = stack_id item = config.put_config_item(**args) return safe_json(item)
def run_scheduler_task(task_name, stack_name): configuration = TaskConfiguration() task_item = configuration.get_config_item(task_name) if task_item is None: raise ValueError( "Task \"{}\" is not configured in stack \"{}\"".format( task_name, stack_name)) print_verbose("Configuration item is\n{}", safe_json(task_item, indent=3)) task = TaskConfiguration( context=used_context, logger=ConsoleLogger()).configuration_item_to_task(task_item) event = { handlers.HANDLER_EVENT_ACTION: handlers.HANDLER_ACTION_SELECT_RESOURCES, handlers.HANDLER_EVENT_TASK: task, handlers.HANDLER_EVENT_SOURCE: sys.argv[0], handlers.HANDLER_EVENT_TASK_DT: datetime.now().isoformat() } for sub_task in ScheduleHandler.task_account_region_sub_tasks(task): event[handlers.HANDLER_EVENT_SUB_TASK] = sub_task print_verbose("Event is \n{}", safe_json(event, indent=3)) handler = handlers.create_handler("SelectResourcesHandler", event, used_context) result = handler.handle_request() print_verbose("(Sub) Task result is\n{}", safe_json(result, indent=3))
def delete_task(name, exception_if_not_exists=False, context=None): """ Deletes the specified task :param name: Name of the task to be deleted, if the task does not exist an exception is raised :param exception_if_not_exists: if set to True raises an exception if the item does not exist :param context: Lambda context :return: Deleted task item """ with _get_logger(context=context) as logger: logger.info("delete_task") config = TaskConfiguration(context=context, logger=logger) if exception_if_not_exists: item = config.get_config_item(name) if item is None: raise ValueError(ERR_TASK_DOES_NOT_EXIST.format(name)) else: item = {"Name": name} config.delete_config_item(name) return safe_json(item)
def create_task(context=None, **kwargs): """ Creates a new task :param kwargs: Task parameters :param context: Lambda context Constants can be found in configuration/__init__.py -CONFIG_ACTION_NAME: Name of the action executed by the task, exception is raised if not specified or action does not exist (mandatory, string) -CONFIG_DEBUG: Set to True to log additional debug information for this task (optional, default False, boolean) -CONFIG_DESCRIPTION: Task description(optional, default None, string) -CONFIG_CROSS_ACCOUNT_ROLES: List of cross accounts for cross account processing. Note that roles if the account of a role has already been found in another role, or if the account of a role is the processed account of the scheduler a warning is generated when executing the task and the role is skipped (optional, default [], List<string>) -CONFIG_ENABLED: Set to True to enable execution of task, False to suspend executions (optional, default True, boolean) -CONFIG_INTERNAL: Flag to indicate task is used for internal tats of the scheduler (optional, default False, boolean) -CONFIG_INTERVAL: Cron expression to schedule time/date based execution of task (optional, default "", string) -CONFIG_TASK_TIMEOUT: Timeout in minutes for task to complete (optional, default is action's value or global timeout, number) -CONFIG_TASK_NAME: Name of the task, exception is raised if not specified or name does already exist (mandatory, string) -CONFIG_PARAMETERS: dictionary with names and values passed to the executed action of this task(optional,default {}, dictionary) -CONFIG_THIS_ACCOUNT: Set to True to run tasks for resources in the account of the (optional, default True, boolean) -CONFIG_TIMEZONE: Timezone for time/date based tasks for this task (optional, default UTC, string) -CONFIG_TAG_FILTER: Tag filter used to select resources for the task instead of name of task in the list of values for the automation tag. Only allowed if selected resources support tags (optional, default "", string) -CONFIG_REGIONS: Regions in which to run the task. Use "*" for all regions in which the service for this tasks action is available. If no regions are specified the region in which the scheduler is installed is used as default. Specifying one or more regions for services tha are not region specific will generate a warning when processing the task. (optional, default current region, List<string>) -CONFIG_STACK_ID: Id of the stack if the task is created as part of a cloudformation template (optional, default None, string) -CONFIG_DRYRUN: Dryrun parameter passed to the executed action (optional, default False, boolean) -CONFIG_EVENTS: List of resource events that trigger the task to be executed (optional, default, List<string>) -CONFIG_DRYRUN: Dryrun parameter passed to the executed action (optional, default False, boolean) -CONFIG_EVENTS: List of resource events that trigger the task to be executed (optional, default, List<string>) :return: Item created in the task configuration """ with _get_logger(context=context) as logger: logger.info("create_task") config = TaskConfiguration(context=context, logger=logger) name = kwargs.get(configuration.CONFIG_TASK_NAME) if name is None or len(name) == 0: raise ValueError(ERR_NO_TASK_NAME) item = config.get_config_item(name) if item is not None: raise ValueError(ERR_TASK_DOES_ALREADY_EXIST.format(name)) new_item = config.put_config_item(**kwargs) return safe_json(new_item)