Example #1
0
def render_action_template(cls, entity_context=""):

    global RUNBOOK_ACTION_MAP
    LOG.debug("Rendering {} action template".format(cls.__name__))
    if not isinstance(cls, ActionType):
        raise TypeError("{} is not of type {}".format(cls, action))

    # Update entity context
    # TODO for now, not adding runbook to context as current mapping -is 1:1
    entity_context = entity_context + "_Action_" + cls.__name__

    runbook = cls.runbook
    runbook_name = getattr(runbook, "name", "") or runbook.__name__
    # Note cls.__name__ should be used for call_runbook tasks
    RUNBOOK_ACTION_MAP[runbook_name] = cls.__name__

    # NOTE Not using main_task_local_reference for now,
    # bcz type of main task is "DAG"
    levelled_tasks = get_task_order(runbook.tasks)
    tasks = []
    for task_list in levelled_tasks:
        if len(task_list) != 1:
            tasks.append(
                render_parallel_task_template(
                    task_list, entity_context, RUNBOOK_ACTION_MAP
                )
            )
        else:
            tasks.append(
                render_task_template(task_list[0], entity_context, RUNBOOK_ACTION_MAP)
            )

    variables = []
    for variable in runbook.variables:
        variables.append(render_variable_template(variable, entity_context))

    if not (variables or tasks):
        return ""

    user_attrs = {
        "name": cls.__name__,
        "description": cls.__doc__ or "",
        "tasks": tasks,
        "variables": variables,
    }

    gui_display_name = getattr(cls, "name", "") or cls.__name__
    if gui_display_name != cls.__name__:
        user_attrs["gui_display_name"] = gui_display_name

    text = render_template(schema_file="action.py.jinja2", obj=user_attrs)
    return text.strip()
Example #2
0
def render_service_template(cls):

    LOG.debug("Rendering {} service template".format(cls.__name__))
    if not isinstance(cls, ServiceType):
        raise TypeError("{} is not of type {}".format(cls, ServiceType))

    # Entity context
    entity_context = "Service_" + cls.__name__

    user_attrs = cls.get_user_attrs()
    user_attrs["name"] = cls.__name__
    user_attrs["description"] = cls.__doc__ or ""

    # Update service name map and gui name
    gui_display_name = getattr(cls, "name", "") or cls.__name__
    if gui_display_name != cls.__name__:
        user_attrs["gui_display_name"] = gui_display_name

    # updating ui and dsl name mapping
    update_service_name(gui_display_name, cls.__name__)

    depends_on_list = []
    for entity in user_attrs.get("dependencies", []):
        depends_on_list.append(render_ref_template(entity))

    variable_list = []
    for entity in user_attrs.get("variables", []):
        variable_list.append(render_variable_template(entity, entity_context))

    action_list = []
    system_actions = {
        v: k
        for k, v in ServiceType.ALLOWED_SYSTEM_ACTIONS.items()
    }
    for entity in user_attrs.get("actions", []):
        if entity.__name__ in list(system_actions.keys()):
            entity.name = system_actions[entity.__name__]
            entity.__name__ = system_actions[entity.__name__]
        rendered_txt = render_action_template(entity, entity_context)
        if rendered_txt:
            action_list.append(rendered_txt)

    user_attrs["dependencies"] = ",".join(depends_on_list)
    user_attrs["variables"] = variable_list
    user_attrs["actions"] = action_list

    # TODO add ports, ..etc.

    text = render_template("service.py.jinja2", obj=user_attrs)
    return text.strip()
Example #3
0
def render_package_template(cls):

    LOG.debug("Rendering {} package template".format(cls.__name__))
    if not isinstance(cls, PackageType):
        raise TypeError("{} is not of type {}".format(cls, PackageType))

    # Entity context
    entity_context = "Package_" + cls.__name__

    user_attrs = cls.get_user_attrs()
    user_attrs["name"] = cls.__name__
    user_attrs["description"] = cls.__doc__ or ""

    # Update package name map
    gui_display_name = getattr(cls, "name", "") or cls.__name__
    if gui_display_name != cls.__name__:
        user_attrs["gui_display_name"] = gui_display_name

    # updating ui and dsl name mapping
    update_package_name(gui_display_name, cls.__name__)

    service_list = []
    for entity in user_attrs.get("services", []):
        service_list.append(render_ref_template(entity))

    variable_list = []
    for entity in user_attrs.get("variables", []):
        variable_list.append(render_variable_template(entity, entity_context))

    action_list = []
    if hasattr(cls, "__install__"):
        cls.__install__.__name__ = "__install__"
        cls.__install__.name = "__install__"
        action_list.append(render_action_template(cls.__install__, entity_context))

    if hasattr(cls, "__uninstall__"):
        cls.__uninstall__.__name__ = "__uninstall__"
        cls.__uninstall__.name = "__uninstall__"
        action_list.append(render_action_template(cls.__uninstall__, entity_context))

    user_attrs["services"] = ",".join(service_list)
    user_attrs["variables"] = variable_list
    user_attrs["actions"] = action_list

    text = render_template("package.py.jinja2", obj=user_attrs)
    return text.strip()
Example #4
0
def render_profile_template(cls):

    LOG.debug("Rendering {} profile template".format(cls.__name__))
    if not isinstance(cls, ProfileType):
        raise TypeError("{} is not of type {}".format(cls, ProfileType))

    # Entity context
    entity_context = "Profile_" + cls.__name__

    user_attrs = cls.get_user_attrs()
    user_attrs["name"] = cls.__name__
    user_attrs["description"] = cls.__doc__ or ""

    # Update profile name map and gui name
    gui_display_name = getattr(cls, "name", "") or cls.__name__
    if gui_display_name != cls.__name__:
        user_attrs["gui_display_name"] = gui_display_name

    # updating ui and dsl name mapping
    update_profile_name(gui_display_name, cls.__name__)

    action_list = []
    for action in user_attrs.get("actions", []):
        action_list.append(render_action_template(action, entity_context))

    deployment_list = []
    for deployment in user_attrs.get("deployments", []):
        deployment_list.append(deployment.__name__)

    variable_list = []
    for entity in user_attrs.get("variables", []):
        variable_list.append(render_variable_template(entity, entity_context))

    user_attrs["variables"] = variable_list
    user_attrs["deployments"] = ", ".join(deployment_list)
    user_attrs["actions"] = action_list

    text = render_template("profile.py.jinja2", obj=user_attrs)
    return text.strip()
Example #5
0
def render_profile_template(cls):

    LOG.debug("Rendering {} profile template".format(cls.__name__))
    if not isinstance(cls, ProfileType):
        raise TypeError("{} is not of type {}".format(cls, ProfileType))

    # Entity context
    entity_context = "Profile_" + cls.__name__

    user_attrs = cls.get_user_attrs()
    user_attrs["name"] = cls.__name__
    user_attrs["description"] = cls.__doc__ or ""

    # Update profile name map and gui name
    gui_display_name = getattr(cls, "name", "") or cls.__name__
    if gui_display_name != cls.__name__:
        user_attrs["gui_display_name"] = gui_display_name

    # updating ui and dsl name mapping
    update_profile_name(gui_display_name, cls.__name__)

    restore_config_list = []
    for idx, entity in enumerate(user_attrs.get("restore_configs", [])):
        CONFIG_SPEC_MAP[entity.name] = {
            "global_name": "{}.restore_configs[{}]".format(cls.__name__, idx),
            "local_name": "restore_configs[{}]".format(idx),
        }
        restore_config_list.append(
            render_restore_config_template(entity, entity_context))

    snapshot_config_list = []
    for idx, entity in enumerate(user_attrs.get("snapshot_configs", [])):
        CONFIG_SPEC_MAP[entity.name] = {
            "global_name": "{}.snapshot_configs[{}]".format(cls.__name__, idx),
            "local_name": "snapshot_configs[{}]".format(idx),
        }
        snapshot_config_list.append(
            render_snapshot_config_template(entity, entity_context,
                                            CONFIG_SPEC_MAP))
    update_config_list = []
    for idx, entity in enumerate(user_attrs.get("update_configs", [])):
        CONFIG_SPEC_MAP[entity.name] = {
            "global_name": "{}.update_configs[{}]".format(cls.__name__, idx),
            "local_name": "update_configs[{}]".format(idx),
        }
        update_config_list.append(
            render_update_config_template(entity, entity_context))

    action_list = []
    for action in user_attrs.get("actions", []):
        action_list.append(
            render_action_template(action, entity_context, CONFIG_SPEC_MAP))

    deployment_list = []
    for deployment in user_attrs.get("deployments", []):
        deployment_list.append(deployment.__name__)

    variable_list = []
    for entity in user_attrs.get("variables", []):
        variable_list.append(render_variable_template(entity, entity_context))

    user_attrs["variables"] = variable_list
    user_attrs["deployments"] = ", ".join(deployment_list)
    user_attrs["actions"] = action_list
    user_attrs["restore_configs"] = ", ".join(restore_config_list)
    user_attrs["snapshot_configs"] = ", ".join(snapshot_config_list)

    text = render_template("profile.py.jinja2", obj=user_attrs)
    return text.strip()