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
class ConfigServiceTemplateError(Exception):
pass
@dataclass
class ShadowDir:
path: str
@ -316,7 +320,13 @@ class ConfigService(abc.ABC):
elif self.templates.has_template(template_path):
template = self.templates.get_template(template_path).source
else:
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)
templates[file] = template
return templates
@ -340,7 +350,13 @@ class ConfigService(abc.ABC):
elif self.templates.has_template(template_path):
rendered = self.render_template(template_path, data)
else:
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)
self.node.create_file(file_path, rendered)
@ -429,20 +445,20 @@ class ConfigService(abc.ABC):
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.
:param basename: base name for file to render
:param template_path: path of file to render
:param data: service specific defined data for template
:return: rendered template
"""
try:
template = self.templates.get_template(basename)
template = self.templates.get_template(template_path)
return self._render(template, data)
except Exception:
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()}"
)

View file

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

View file

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