From 7d48e8aa259266dcdc06f11a4ad67a764108b6b1 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 20 Nov 2023 16:43:52 -0500 Subject: [PATCH] cephadm: add a custom template not found exception with diagnostic info Add a new exception based on jinja2's template not found exception for the case where the template was not found in the zip(app). We've been having sporadic failures with this in CI & testing and hopefully the additional information will help pinpoint the cause. Signed-off-by: John Mulligan --- src/cephadm/cephadmlib/templating.py | 37 +++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/cephadm/cephadmlib/templating.py b/src/cephadm/cephadmlib/templating.py index e6e8d5e0ea2c..ceef32ff9fed 100644 --- a/src/cephadm/cephadmlib/templating.py +++ b/src/cephadm/cephadmlib/templating.py @@ -29,6 +29,34 @@ class Templates(str, enum.Enum): return repr(self.value) +class TemplateNotFoundInZipApp(jinja2.TemplateNotFound): + def __init__( + self, + template: str, + *, + path: str = '', + relative_path: str = '', + archive_norm_path: str = '', + archive_path: str = '' + ) -> None: + super().__init__(template) + self.path = path + self.relative_path = relative_path + self.archive_norm_path = archive_norm_path + self.archive_path = archive_path + + def __str__(self) -> str: + msg = self.message + msg2 = '' + if self.path or self.relative_path: + msg2 += f' path [{self.path!r}, rel={self.relative_path!r}] not found' + if self.archive_norm_path or self.archive_path: + msg2 += f' in [{self.archive_norm_path!r}, orig={self.archive_path!r}]' + if msg2: + msg2 = ':' + msg2 + return f'{msg}{msg2}' + + class _PackageLoader(jinja2.PackageLoader): """Workaround for PackageLoader when using cephadm with relative paths. @@ -71,7 +99,14 @@ class _PackageLoader(jinja2.PackageLoader): try: source = cast(bytes, self._loader.get_data(arelpath)) except OSError as e: - raise jinja2.TemplateNotFound(template) from e + not_found = TemplateNotFoundInZipApp( + template, + path=path, + relative_path=arelpath, + archive_norm_path=archive_path, + archive_path=self._loader.archive, + ) + raise not_found from e return source.decode(self.encoding), path, None -- 2.47.3