Skip to content

Commit

Permalink
Merge pull request #471 from flyingcircusio/warn-if-has-update-but-no…
Browse files Browse the repository at this point in the history
…-verify

Add warning: If component has update() but no verify()
  • Loading branch information
zagy authored Oct 8, 2024
2 parents 3f10a8a + faeea24 commit 09b5eec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/batou/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,43 @@ def report(self):
output.tabular("Root", self.root_name, red=True)


class ComponentWithUpdateWithoutVerify(ConfigurationError):
"""Some components have an update method but no verify method."""

sort_key = (5, "without_verify")

@classmethod
def from_context(cls, components, roots):
self = cls()
self.components = []
for component in components:
self.components.append(repr(component.__class__.__name__))
self.roots = []
for root in roots:
self.roots.append(root.name)
return self

def __str__(self):
out_str = "Some components have an update method but no verify method:"
for idx, component in enumerate(self.components):
out_str += f"\n {component}"
out_str += f"\nRoot: {self.roots[idx]}"
out_str += f"\nThe update() method may not be called by batou if the verify() method is missing."
return out_str

def report(self):
output.error(
f"Some components have an update method but no verify method:"
)
for component in self.components:
output.line(f" {component}", red=True)
output.tabular("Root", self.root_name, red=True)
output.line(
f"The update() method may not be called by batou if the verify() method is missing.",
red=True,
)


class UnsatisfiedResources(ConfigurationError):
"""Some required resources were never provided."""

Expand Down
28 changes: 28 additions & 0 deletions src/batou/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import batou.vfs
from batou import (
ComponentLoadingError,
ComponentWithUpdateWithoutVerify,
ConfigurationError,
CycleErrorDetected,
DuplicateHostError,
Expand Down Expand Up @@ -492,6 +493,7 @@ def configure(self):

while working_set:
exceptions = []
components_without_verify = []
previous_working_sets.append(working_set.copy())
retry = set()
self.resources.dirty_dependencies.clear()
Expand All @@ -518,6 +520,8 @@ def configure(self):
)
)
else:
# warnings: does not fail the deployment
# 1. unprepared component warning
unprepared_components = []
for component in Component._instances:
if not component._prepared:
Expand All @@ -531,6 +535,21 @@ def configure(self):
)
# exceptions.append(unused_exception)
output.warn(str(unused_exception))

# 2. a component has .update() but no .verify()
def has_original_update_method(component):
return type(component).update == Component.update

def has_original_verify_method(component):
return type(component).verify == Component.verify

for component in Component._instances:
if (
not has_original_update_method(component)
and has_original_verify_method(component)
and (component not in components_without_verify)
):
components_without_verify.append((component, root))
# configured this component successfully
# we won't have to retry it later
continue
Expand Down Expand Up @@ -573,6 +592,15 @@ def configure(self):

working_set = retry

# warn if a component has .update() but no .verify()
if components_without_verify:
component_without_verify_exception = (
ComponentWithUpdateWithoutVerify.from_context(
components_without_verify, root
)
)
output.warn(str(component_without_verify_exception))

# We managed to converge on a working set. However, some resource were
# provided but never used. We're rather picky here and report this as
# an error.
Expand Down

0 comments on commit 09b5eec

Please sign in to comment.