7.1.9. cobbler.templates package
7.1.9.1. Submodules
7.1.9.2. cobbler.templates.cheetah module
Cobbler provides builtin methods for use in Cheetah templates. $SNIPPET is one such function and is now used to implement Cobbler’s SNIPPET:: syntax.
The CobblerCheetahTemplate class is defined using the Cheetah language. Using the ‘compile’ function we can compile the source directly into a Python class. This class will allow us to define the cheetah builtins.
- class cobbler.templates.cheetah.CheetahTemplateProvider(api: CobblerAPI)[source]
Bases:
BaseTemplateProviderProvides support for the Cheetah template language to Cobbler.
See: https://cheetahtemplate.org/
- check_for_invalid_imports(data: str)[source]
Ensure that Cheetah code is not importing Python modules that may allow for advanced privileges by ensuring we whitelist the imports that we allow.
- Parameters:
data – The Cheetah code to check.
- Raises:
CX – Raised in case there could be a pontentially insecure import in the template.
- render(raw_data: str, search_table: Dict[str, Any]) str[source]
Render data_input back into a file.
- Parameters:
raw_data – Is the template code which is not rendered into the result.
search_table – is a dict of metadata keys and values (though results are always returned)
- Returns:
The rendered Template.
- property template_file_extension: str
Returns the file-extension that is recommended by the template engine.
- Getter:
The string with the file-extensions without the dot.
- template_language = 'cheetah'
Identifier for the template type. Must be identical to the module name.
- class cobbler.templates.cheetah.CobblerCheetahTemplate(**kwargs: Any)[source]
Bases:
TemplateThis class will allow us to include any pure python builtin functions. It derives from the cheetah-compiled class above. This way, we can include both types (cheetah and pure python) of builtins in the same base template.
This part (see ‘Template’ below for the other part) handles the actual inclusion of the file contents. We still need to make the snippet’s namespace (searchList) available to the template calling SNIPPET (done in the other part).
This function can be used in two ways: Cheetah syntax: - $SNIPPET(‘my_snippet’) - SNIPPET syntax: - SNIPPET::my_snippet
This follows all of the rules of snippets and advanced snippets. First it searches for a per-system snippet, then a per-profile snippet, then a general snippet. If none is found, a comment explaining the error is substituted.
- SNIPPET(file: str) Any[source]
Include the contents of the named snippet here. This is equivalent to the #include directive in Cheetah, except that it searches for system and profile specific snippets, and it includes the snippet’s namespace.
This may be a little frobby, but it’s really cool. This is a pure python portion of SNIPPET that appends the snippet’s searchList to the caller’s searchList. This makes any #defs within a given snippet available to the template that included the snippet.
- Parameters:
file – The snippet file to read and include in the template.
- Returns:
The updated template.
- cobbler_api: CobblerAPI
- classmethod compile(*args: Any, **kwargs: Any) bytes[source]
Compile a cheetah template with Cobbler modifications. Modifications include
SNIPPET::syntax replacement and inclusion of Cobbler builtin methods. Please be aware that you cannot use thebaseclassattribute of Cheetah anymore due to the fact that we are using it in our implementation to enable the Cheetah Macros.This function gets called by Cheetah.Template.Template.__init__ to compile the template into a class. This is probably a kludge, but it add a baseclass argument to the standard compile (see Cheetah’s compile docstring) and returns the resulting class. This argument, of course, points to this class. Now any methods entered here (or in the base class above) will be accessible to all cheetah templates compiled by Cobbler.
- Parameters:
args – These just get passed right to Cheetah.
kwargs – We just execute our own preprocessors and remove them and let afterwards handle Cheetah the rest.
- Returns:
The compiled template.
- read_snippet(template_identifier: str) str | None[source]
Locate the appropriate snippet for the current system and profile and read its contents.
This will first check for a per-system snippet, a per-profile snippet, a distro snippet, and a general snippet.
- Parameters:
template_identifier – The name or uid of the template.
- Returns:
None (if the snippet file was not found) or the string with the read snippet.
- sedesc(value: str) str[source]
Escape a string for use in sed.
Example: Replace all instances of
/etc/bannerwith a value stored in$new_banner..code:
sed 's/$sedesc("/etc/banner")/$sedesc($new_banner)/'
- Parameters:
value – The phrase to escape.
- Returns:
The escaped phrase.
- validate_uuid(possible_uuid: str) bool[source]
Copied from
validate_uuid()to prevent issues with module availablity during template processing.- Parameters:
possible_uuid – The str with the UUID.
- Returns:
True in case it is one, False otherwise.
7.1.9.3. cobbler.templates.jinja module
Template functionality that is tied to Jinja2.
- class cobbler.templates.jinja.CobblerJinjaLoader(cobbler_api: CobblerAPI)[source]
Bases:
BaseLoaderCustom Jinja template loader that allows loading templates from the CobblerAPI.
- get_source(environment: Environment, template: str) Tuple[str, str | None, Callable[[], bool] | None][source]
Get the template source, filename and reload helper for a template. It’s passed the environment and template name and has to return a tuple in the form
(source, filename, uptodate)or raise a TemplateNotFound error if it can’t locate the template.The source part of the returned tuple must be the source of the template as a string. The filename should be the name of the file on the filesystem if it was loaded from there, otherwise
None. The filename is used by Python for the tracebacks if no loader extension is used.The last item in the tuple is the uptodate function. If auto reloading is enabled it’s always called to check if the template changed. No arguments are passed so the function must store the old state somewhere (for example in a closure). If it returns False the template will be reloaded.
- class cobbler.templates.jinja.JinjaTemplateProvider(api: CobblerAPI)[source]
Bases:
BaseTemplateProviderProvides support for the Jinja2 template language to Cobbler.
See: https://jinja.palletsprojects.com/en/3.1.x/
- render(raw_data: str, search_table: Dict[str, Any]) str[source]
Render data_input back into a file.
- Parameters:
raw_data – Is the template code which is not rendered into the result.
search_table – is a dict of metadata keys and values (though results are always returned)
- Returns:
The rendered Template.
- property template_file_extension: str
Returns the file-extension that is recommended by the template engine.
- Getter:
The string with the file-extensions without the dot.
- template_language = 'jinja'
Identifier for the template type. Must be identical to the module name.
7.1.9.4. Module contents
Package that contains built-in templates and rendering logic from Cheetah and Jinja, as well as their abstraction layer.
Cobbler uses Cheetah templates for lots of stuff, but there’s some additional magic around that to deal with snippets/etc. (And it’s not spelled wrong!)
- class cobbler.templates.BaseTemplateProvider(api: CobblerAPI)[source]
Bases:
objectAbstract base template provider that allows custom providers to be implemented with a common set of methods.
- property built_in_templates: List[Template]
Collects the list of built-in read only templates.
- Returns:
The list of templates that are built-in.
- render(raw_data: str, search_table: Dict[str, Any]) str[source]
Render data_input back into a file.
- Parameters:
raw_data – Is the template code which is not rendered into the result.
search_table – is a dict of metadata keys and values (though results are always returned)
- Returns:
The rendered Template.
- property template_file_extension: str
Returns the file-extension that is recommended by the template engine.
- Getter:
The string with the file-extensions without the dot.
- template_language = 'generic'
Identifier for the template type. Must be identical to the module name.
- cobbler.templates.TEMPLATE_TAG_MAPPING: Dict[str, Set[ConvertableEnum]] = {'answerfile.template': {AutoinstallerType.WINDOWS, TemplateTag.WINDOWS_ANSWERFILE}, 'answerfile.xml.jinja': {AutoinstallerType.XEN}, 'autoinst.json.jinja': {AutoinstallerType.AGAMA}, 'bootcfg.template': {TemplateTag.BOOTCFG}, 'bootinfo.template': {TemplateTag.ISO_BOOTINFO}, 'build_report_email.template': {TemplateTag.REPORTING_BUILD_EMAIL}, 'buildiso.template': {TemplateTag.ISO_BUILDISO}, 'default.ks.template': {AutoinstallerType.KICKSTART}, 'dhcp.template': {TemplateTag.DHCPV4}, 'dhcp6.template': {TemplateTag.DHCPV6}, 'dnsmasq.template': {TemplateTag.DNSMASQ}, 'genders.template': {TemplateTag.GENDERS}, 'grub.template': {TemplateTag.GRUB}, 'grub_menu.template': {TemplateTag.GRUB_MENU}, 'grub_menuentry.template': {TemplateTag.ISO_GRUB_MENUENTRY}, 'grub_submenu.template': {TemplateTag.GRUB_SUBMENU}, 'ipxe.template': {TemplateTag.IPXE}, 'ipxe_menu.template': {TemplateTag.IPXE_MENU}, 'ipxe_submenu.template': {TemplateTag.IPXE_SUBMENU}, 'isolinux_menuentry.jinja': {TemplateTag.ISO_ISOLINUX_MENUENTRY}, 'legacy.ks.template': {AutoinstallerType.KICKSTART}, 'meta-data.jinja': {AutoinstallerType.CLOUDINIT, CloudInitFileTypes.META_DATA}, 'named.template': {TemplateTag.NAMED_PRIMARY}, 'ndjbdns.template': {TemplateTag.NDJBDNS}, 'network-config.jinja': {AutoinstallerType.CLOUDINIT, CloudInitFileTypes.NETWORK_CONFIG}, 'post_inst_cmd.template': {AutoinstallerType.WINDOWS, TemplateTag.WINDOWS_POST_INST_CMD}, 'powerkvm.ks.template': {AutoinstallerType.KICKSTART}, 'pxe.template': {TemplateTag.PXE}, 'pxe_menu.template': {TemplateTag.PXE_MENU}, 'pxe_submenu.template': {TemplateTag.PXE_SUBMENU}, 'pxerescue.ks.template': {AutoinstallerType.KICKSTART}, 'rsync.template': {TemplateTag.RSYNC}, 'sample.ks.template': {AutoinstallerType.KICKSTART}, 'sample.seed.template': {AutoinstallerType.PRESEED}, 'sample_autoyast.xml.template': {AutoinstallerType.AUTOYAST}, 'sample_esxi4.ks.template': {AutoinstallerType.KICKSTART}, 'sample_esxi5.ks.template': {AutoinstallerType.KICKSTART}, 'sample_esxi6.ks.template': {AutoinstallerType.KICKSTART}, 'sample_esxi7.ks.template': {AutoinstallerType.KICKSTART}, 'sample_legacy.ks.template': {AutoinstallerType.KICKSTART}, 'sample_old.seed.template': {AutoinstallerType.PRESEED}, 'secondary.template': {TemplateTag.NAMED_SECONDARY}, 'startnet.template': {AutoinstallerType.WINDOWS, TemplateTag.WINDOWS_STARTNET}, 'user-data.jinja': {AutoinstallerType.CLOUDINIT, CloudInitFileTypes.USER_DATA}, 'vendor-data.jinja': {AutoinstallerType.CLOUDINIT, CloudInitFileTypes.VENDOR_DATA}, 'win.xml.template': {AutoinstallerType.WINDOWS}, 'zone.template': {TemplateTag.NAMED_ZONE_DEFAULT}}
This static mapping is adding the function tags to all built-in templates that are well-known to the application. If new templates are added or old ones are removed, this mapping has to be adjusted.
- class cobbler.templates.Templar(api: CobblerAPI)[source]
Bases:
objectWrapper to encapsulate all logic of the template providers.
- property available_template_providers: List[str]
A property to return the currently available template providers.
- Getter:
A list of valid template providers. Each individual element of the list can be handed to “render”.
- load_built_in_templates() None[source]
Load all built-in templates for all providers and add them to the API. The added templates will not be modifiable.
- load_template_providers() None[source]
Load all template providers that Cobbler is offering. This is dynamic to prevent the need to adjust for hardcoding the available types and discourage the assumption that a given provider is available.
- render(data_input: TextIO | str, search_table: Dict[str, Any], out_path: str | None, template_type: str = 'default') str[source]
Render data_input back into a file.
- Parameters:
data_input – is either a str or a TextIO object.
search_table – is a dict of metadata keys and values.
out_path – Optional parameter which (if present), represents the target path to write the result into.
template_type – May currently be “cheetah” or “jinja2”. “default” looks in the settings.
- Returns:
The rendered template.