Skip to content

Commit

Permalink
fix: check operation availability on host before execution
Browse files Browse the repository at this point in the history
If a host is provided for an operation, check if the operation is
available for this host before executing it.
Log a custom error message in the database.

This is required to avoid edge case with the `--limit <host>` option
on the `ansible-playbook` command.
  • Loading branch information
PaulFarault committed Oct 31, 2023
1 parent 4791a94 commit f8dacce
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tdp/core/deployment/deployment_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ def _run_operation(self, operation_log: OperationLog) -> None:
operation_log.start_time = datetime.utcnow()

operation = self._collections.get_operation(operation_log.operation)

# Check if the operation is available for the given host
if operation_log.host and operation_log.host not in operation.host_names:
logs = (
f"Operation '{operation_log.operation}' not available for host "
+ f"'{operation_log.host}'"
)
logger.error(logs)
operation_log.state = OperationStateEnum.FAILURE
operation_log.logs = logs.encode("utf-8")
operation_log.end_time = datetime.utcnow()
return

# Execute the operation
playbook_file = self._collections[operation.collection_name].playbooks[
operation.name
]
Expand All @@ -62,6 +76,7 @@ def _run_operation(self, operation_log: OperationLog) -> None:
)
operation_log.end_time = datetime.utcnow()

# ? This case shouldn't happen as the executor should return a valid state
if state not in OperationStateEnum:
logger.error(
f"Invalid state ({state}) returned by {self._executor.__class__.__name__}.run('{playbook_file}'))"
Expand Down

0 comments on commit f8dacce

Please sign in to comment.