def render_readiness_probe_template(cls): LOG.debug("Rendering {} readiness probe template".format(cls.__name__)) if not isinstance(cls, ReadinessProbeType): raise TypeError("{} is not of type {}".format(cls, ReadinessProbeType)) user_attrs = cls.get_user_attrs() # deal with cred cred = user_attrs["credential"] if cred: user_attrs["credential"] = "ref({})".format( get_cred_var_name(cred.__name__)) schema_file = "readiness_probe.py.jinja2" text = render_template(schema_file=schema_file, obj=user_attrs) return text.strip()
def render_blueprint_template(cls): LOG.debug("Rendering {} blueprint template".format(cls.__name__)) if not isinstance(cls, BlueprintType): raise TypeError("{} is not of type {}".format(cls, BlueprintType)) user_attrs = cls.get_user_attrs() user_attrs["name"] = cls.__name__ user_attrs["description"] = cls.__doc__ or "" credential_list = [] for cred in cls.credentials: credential_list.append( get_cred_var_name(getattr(cred, "name", "") or cred.__name__)) service_list = [] for service in cls.services: service_list.append(service.__name__) package_list = [] for package in cls.packages: package_list.append(package.__name__) substrate_list = [] for substrate in cls.substrates: substrate_list.append(substrate.__name__) profile_list = [] for profile in cls.profiles: profile_list.append(profile.__name__) user_attrs.update({ "services": ", ".join(service_list), "packages": ", ".join(package_list), "substrates": ", ".join(substrate_list), "profiles": ", ".join(profile_list), "credentials": ", ".join(credential_list), }) text = render_template("blueprint.py.jinja2", obj=user_attrs) return text.strip()
def render_ahv_vm_gc(cls, vm_name_prefix=""): schema_file = "" user_attrs = {} user_attrs = cls.get_dict() cloud_init = user_attrs.get("cloud_init", {}) sys_prep = user_attrs.get("sysprep", {}) file_name = "" spec_dir = get_specs_dir() if cloud_init: schema_file = "ahv_vm_cloud_init.py.jinja2" file_name = "{}_cloud_init_data.yaml".format(vm_name_prefix) user_attrs["filename"] = "os.path.join('{}', '{}')".format( get_specs_dir_key(), file_name) cloud_init_user_data = cloud_init.get("user_data", "") if not cloud_init_user_data: return with open(os.path.join(spec_dir, file_name), "w+") as fd: # TODO take care of macro case fd.write(yaml.dump(cloud_init_user_data, default_flow_style=False)) elif sys_prep: file_name = "{}_sysprep_unattend_xml.xml".format(vm_name_prefix) user_attrs["filename"] = "os.path.join('{}', '{}')".format( get_specs_dir_key(), file_name) sysprep_unattend_xml = sys_prep.get("unattend_xml", "") with open(os.path.join(spec_dir, file_name), "w+") as fd: fd.write(sysprep_unattend_xml) install_type = sys_prep.get("install_type", "PREPARED") is_domain = sys_prep.get("is_domain", False) if is_domain and sys_prep.get("domain_credential_reference"): cred = RefType.decompile(sys_prep["domain_credential_reference"]) user_attrs["credential"] = "ref({})".format( get_cred_var_name(cred.__name__)) if install_type == "FRESH": if is_domain: schema_file = "ahv_vm_fresh_sysprep_with_domain.py.jinja2" else: schema_file = "ahv_vm_fresh_sysprep_without_domain.py.jinja2" elif install_type == "PREPARED": if is_domain: schema_file = "ahv_vm_prepared_sysprep_with_domain.py.jinja2" else: schema_file = "ahv_vm_prepared_sysprep_without_domain.py.jinja2" else: LOG.error( "Unknown install type '{}' for sysprep guest customization". format(install_type)) sys.exit(-1) else: return None text = render_template(schema_file=schema_file, 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()