daemon: small improvements to command error reporting, as well as catching and reporting config service get text dynamic exceptions

This commit is contained in:
Blake Harnden 2022-03-04 09:55:40 -08:00
parent 0536747d9a
commit f545726ed5
3 changed files with 26 additions and 9 deletions

View file

@ -43,6 +43,10 @@ class ConfigServiceBootError(Exception):
pass pass
class ConfigServiceTemplateError(Exception):
pass
@dataclass @dataclass
class ShadowDir: class ShadowDir:
path: str path: str
@ -316,7 +320,13 @@ class ConfigService(abc.ABC):
elif self.templates.has_template(template_path): elif self.templates.has_template(template_path):
template = self.templates.get_template(template_path).source template = self.templates.get_template(template_path).source
else: else:
template = self.get_text_template(file) try:
template = self.get_text_template(file)
except Exception as e:
raise ConfigServiceTemplateError(
f"node({self.node.name}) service({self.name}) file({file}) "
f"failure getting template: {e}"
)
template = self.clean_text(template) template = self.clean_text(template)
templates[file] = template templates[file] = template
return templates return templates
@ -340,7 +350,13 @@ class ConfigService(abc.ABC):
elif self.templates.has_template(template_path): elif self.templates.has_template(template_path):
rendered = self.render_template(template_path, data) rendered = self.render_template(template_path, data)
else: else:
text = self.get_text_template(file) try:
text = self.get_text_template(file)
except Exception as e:
raise ConfigServiceTemplateError(
f"node({self.node.name}) service({self.name}) file({file}) "
f"failure getting template: {e}"
)
rendered = self.render_text(text, data) rendered = self.render_text(text, data)
self.node.create_file(file_path, rendered) self.node.create_file(file_path, rendered)
@ -429,20 +445,20 @@ class ConfigService(abc.ABC):
f"{exceptions.text_error_template().render_unicode()}" f"{exceptions.text_error_template().render_unicode()}"
) )
def render_template(self, basename: str, data: Dict[str, Any] = None) -> str: def render_template(self, template_path: str, data: Dict[str, Any] = None) -> str:
""" """
Renders file based template providing all associated data to template. Renders file based template providing all associated data to template.
:param basename: base name for file to render :param template_path: path of file to render
:param data: service specific defined data for template :param data: service specific defined data for template
:return: rendered template :return: rendered template
""" """
try: try:
template = self.templates.get_template(basename) template = self.templates.get_template(template_path)
return self._render(template, data) return self._render(template, data)
except Exception: except Exception:
raise CoreError( raise CoreError(
f"node({self.node.name}) service({self.name}) " f"node({self.node.name}) service({self.name}) file({template_path})"
f"{exceptions.text_error_template().render_template()}" f"{exceptions.text_error_template().render_template()}"
) )

View file

@ -11,7 +11,7 @@ class CoreCommandError(subprocess.CalledProcessError):
def __str__(self) -> str: def __str__(self) -> str:
return ( return (
f"Command({self.cmd}), Status({self.returncode}):\n" f"command({self.cmd}), status({self.returncode}):\n"
f"stdout: {self.output}\nstderr: {self.stderr}" f"stdout: {self.output}\nstderr: {self.stderr}"
) )

View file

@ -227,6 +227,7 @@ def cmd(
execute is not found execute is not found
""" """
logger.debug("command cwd(%s) wait(%s): %s", cwd, wait, args) logger.debug("command cwd(%s) wait(%s): %s", cwd, wait, args)
input_args = args
if shell is False: if shell is False:
args = shlex.split(args) args = shlex.split(args)
try: try:
@ -238,13 +239,13 @@ def cmd(
stderr = stderr.decode("utf-8").strip() stderr = stderr.decode("utf-8").strip()
status = p.wait() status = p.wait()
if status != 0: if status != 0:
raise CoreCommandError(status, args, stdout, stderr) raise CoreCommandError(status, input_args, stdout, stderr)
return stdout return stdout
else: else:
return "" return ""
except OSError as e: except OSError as e:
logger.error("cmd error: %s", e.strerror) logger.error("cmd error: %s", e.strerror)
raise CoreCommandError(1, args, "", e.strerror) raise CoreCommandError(1, input_args, "", e.strerror)
def file_munge(pathname: str, header: str, text: str) -> None: def file_munge(pathname: str, header: str, text: str) -> None: