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()
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()
def render_task_template( cls, entity_context="", RUNBOOK_ACTION_MAP={}, CONFIG_SPEC_MAP={} ): LOG.debug("Rendering {} task template".format(cls.name)) if not isinstance(cls, TaskType): raise TypeError("{} is not of type {}".format(cls, TaskType)) # update entity_context entity_context = entity_context + "_Task_" + cls.__name__ user_attrs = cls.get_user_attrs() user_attrs["name"] = cls.name target = getattr(cls, "target_any_local_reference", None) if target: # target will be modified to have correct name(DSL name) user_attrs["target"] = render_ref_template(target) cred = cls.attrs.get("login_credential_local_reference", None) if cred: user_attrs["cred"] = "ref({})".format( get_cred_var_name(getattr(cred, "name", "") or cred.__name__) ) if cls.type == "EXEC": script_type = cls.attrs["script_type"] cls.attrs["script_file"] = create_script_file( script_type, cls.attrs["script"], entity_context ) if script_type == "sh": schema_file = "task_exec_ssh.py.jinja2" elif script_type == "static": schema_file = "task_exec_escript.py.jinja2" elif script_type == "npsscript": schema_file = "task_exec_powershell.py.jinja2" elif cls.type == "SET_VARIABLE": variables = cls.attrs.get("eval_variables", None) if variables: user_attrs["variables"] = variables script_type = cls.attrs["script_type"] cls.attrs["script_file"] = create_script_file( script_type, cls.attrs["script"], entity_context ) if script_type == "sh": schema_file = "task_setvariable_ssh.py.jinja2" elif script_type == "static": schema_file = "task_setvariable_escript.py.jinja2" elif script_type == "npsscript": schema_file = "task_setvariable_powershell.py.jinja2" elif cls.type == "DELAY": if hasattr(cls, "attrs"): user_attrs["delay_seconds"] = cls.attrs.get("interval_secs", 0) schema_file = "task_delay.py.jinja2" elif cls.type == "SCALING": scaling_count = cls.attrs.get("scaling_count", 1) if scaling_count: user_attrs["scaling_count"] = scaling_count scaling_type = cls.attrs["scaling_type"] if scaling_type == "SCALEOUT": schema_file = "task_scaling_scaleout.py.jinja2" elif scaling_type == "SCALEIN": schema_file = "task_scaling_scalein.py.jinja2" elif cls.type == "HTTP": attrs = cls.attrs user_attrs["headers"] = {} user_attrs["secret_headers"] = {} user_attrs["status_mapping"] = {} for var in attrs.get("headers", []): var_type = var["type"] if var_type == "LOCAL": user_attrs["headers"][var["name"]] = var["value"] elif var_type == "SECRET": user_attrs["secret_headers"][var["name"]] = var["value"] for status in attrs.get("expected_response_params", []): user_attrs["status_mapping"][status["code"]] = ( True if status["status"] == "SUCCESS" else False ) # Store auth objects auth_obj = attrs.get("authentication", {}) auth_type = auth_obj.get("type", "") if auth_type == "basic_with_cred": auth_cred = auth_obj.get("credential_local_reference", None) if auth_cred: user_attrs["cred"] = "ref({})".format( get_cred_var_name( getattr(auth_cred, "name", "") or auth_cred.__name__ ) ) user_attrs["response_paths"] = attrs.get("response_paths", {}) method = attrs["method"] if method == "GET": schema_file = "task_http_get.py.jinja2" elif method == "POST": schema_file = "task_http_post.py.jinja2" elif method == "PUT": schema_file = "task_http_put.py.jinja2" elif method == "DELETE": # TODO remove it from here if not cls.attrs["request_body"]: cls.attrs["request_body"] = {} schema_file = "task_http_delete.py.jinja2" elif cls.type == "CALL_RUNBOOK": runbook = cls.attrs["runbook_reference"] runbook_name = getattr(runbook, "name", "") or runbook.__name__ user_attrs = { "name": cls.name, "action": RUNBOOK_ACTION_MAP[runbook_name], "target": target.name, } schema_file = "task_call_runbook.py.jinja2" elif cls.type == "CALL_CONFIG": config_name = cls.attrs["config_spec_reference"] user_attrs = { "name": cls.name, "config": CONFIG_SPEC_MAP[config_name]["global_name"], } schema_file = "task_call_config.py.jinja2" else: LOG.error("Task type does not match any known types") sys.exit("Invalid task task") text = render_template(schema_file=schema_file, obj=user_attrs) return text.strip()
def render_task_template(cls, entity_context="", RUNBOOK_ACTION_MAP={}): LOG.debug("Rendering {} task template".format(cls.name)) if not isinstance(cls, TaskType): raise TypeError("{} is not of type {}".format(cls, TaskType)) # update entity_context entity_context = entity_context + "_Task_" + cls.__name__ user_attrs = cls.get_user_attrs() user_attrs["name"] = cls.name target = getattr(cls, "target_any_local_reference", None) if target: user_attrs["target"] = render_ref_template(target) cred = cls.attrs.get("login_credential_local_reference", None) if cred: # TODO make it as task decompile functionality cred = RefType.decompile(cred) user_attrs["cred"] = "ref({})".format(get_cred_var_name(cred.__name__)) if cls.type == "EXEC": script_type = cls.attrs["script_type"] cls.attrs["script_file"] = create_script_file( script_type, cls.attrs["script"], entity_context ) if script_type == "sh": schema_file = "task_exec_ssh.py.jinja2" elif script_type == "static": schema_file = "task_exec_escript.py.jinja2" elif script_type == "npsscript": schema_file = "task_exec_powershell.py.jinja2" elif cls.type == "SET_VARIABLE": variables = cls.attrs.get("eval_variables", None) if variables: user_attrs["variables"] = variables script_type = cls.attrs["script_type"] cls.attrs["script_file"] = create_script_file( script_type, cls.attrs["script"], entity_context ) if script_type == "sh": schema_file = "task_setvariable_ssh.py.jinja2" elif script_type == "static": schema_file = "task_setvariable_escript.py.jinja2" elif script_type == "npsscript": schema_file = "task_setvariable_powershell.py.jinja2" elif cls.type == "DELAY": if hasattr(cls, "attrs"): user_attrs["delay_seconds"] = cls.attrs.get("interval_secs", 0) schema_file = "task_delay.py.jinja2" elif cls.type == "SCALING": scaling_count = cls.attrs.get("scaling_count", 1) if scaling_count: user_attrs["scaling_count"] = scaling_count scaling_type = cls.attrs["scaling_type"] if scaling_type == "SCALEOUT": schema_file = "task_scaling_scaleout.py.jinja2" elif scaling_type == "SCALEIN": schema_file = "task_scaling_scalein.py.jinja2" elif cls.type == "HTTP": attrs = cls.attrs # TODO add basic_cred creds support. For now default cred used user_attrs["headers"] = {} user_attrs["secret_headers"] = {} user_attrs["status_mapping"] = {} for var in attrs.get("headers", []): var_type = var["type"] if var_type == "LOCAL": user_attrs["headers"][var["name"]] = var["value"] elif var_type == "SECRET": user_attrs["secret_headers"][var["name"]] = var["value"] for status in attrs.get("expected_response_params", []): user_attrs["status_mapping"][status["code"]] = ( True if status["status"] == "SUCCESS" else False ) user_attrs["response_paths"] = attrs.get("response_paths", {}) method = attrs["method"] if method == "GET": schema_file = "task_http_get.py.jinja2" elif method == "POST": schema_file = "task_http_post.py.jinja2" elif method == "PUT": schema_file = "task_http_put.py.jinja2" elif method == "DELETE": # TODO remove it from here if not cls.attrs["request_body"]: cls.attrs["request_body"] = {} schema_file = "task_http_delete.py.jinja2" elif cls.type == "CALL_RUNBOOK": # TODO shift this working to explicit method for task decompile runbook = RefType.decompile(cls.attrs["runbook_reference"]) render_ref_template(target) user_attrs = { "name": cls.name, "action": RUNBOOK_ACTION_MAP[runbook.__name__], "target": target.name, } schema_file = "task_call_runbook.py.jinja2" else: raise Exception("Invalid task type") text = render_template(schema_file=schema_file, obj=user_attrs) return text.strip()