Skip to content

Commit

Permalink
Add changed_operations parameter to state runner
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 committed Dec 5, 2024
1 parent 9de1135 commit 0169699
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is the changelog for RedPepper.
### Added

- Disable connection keep-alive pings if `ping_interval` is set to `0`.
- Add `changed_operations` parameter to state runner to allow forcing certain operations to run.

## [0.3.1]

Expand Down
1 change: 1 addition & 0 deletions config/data/state/webservers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Code synced:
type: command.Run
command: echo "git.UpToDate(target=/home/webserver/src, [email protected]/repo)"
if: false
- Virtual environment:
type: command.Run
command: echo "python3 -m venv /home/webserver/src/.venv"
Expand Down
18 changes: 12 additions & 6 deletions src/agent/redpepper/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ async def _run_command(
if not isinstance(state_data, list):
raise ValueError(f"State {state_name} is not a list")
result = await self.run_state(
state_name, state_data, commandID=commandID
state_name,
state_data,
commandID=commandID,
**kwargs,
)
else:
await self.send_command_progress(
Expand Down Expand Up @@ -276,6 +279,7 @@ async def run_state(
state_name: str,
state_data: list,
commandID: str | None = None,
changed_operations: list[str] = [],
) -> Result:
# For now we can raise errors because we don't have any previous output to return.
# Arrange the state entries into a list of OperationSpec objects
Expand All @@ -297,7 +301,9 @@ async def run_state(
commandID, 0, len(tasks), f"Starting {state_name}..."
)
# Keep track of what changed
changed = OrderedDict()
changed: OrderedDict[str, bool] = OrderedDict(
(n, True) for n in changed_operations
)
# Run the tasks
for task in tasks:
# Give other tasks a chance to run
Expand Down Expand Up @@ -331,9 +337,9 @@ async def run_state(
break
changed[task.name] = cmd_result.changed
if cmd_result.changed:
for item in changed:
if task.name.startswith(item + ":"):
changed[item] = True
parts = task.name.split(":")
for pi in range(1, len(parts)):
changed[":".join(parts[:pi])] = True
# Send the progress message
if commandID is not None:
await self.send_command_progress(
Expand Down Expand Up @@ -423,7 +429,7 @@ def evaluate_condition(self, condition: Any, changed: dict = {}) -> bool:
if not isinstance(v, str):
raise ValueError("Value for changed condition must be a string")
for item in reversed(changed):
if changed[item]:
if (item == v or item.endswith(":" + v)) and changed[item]:
return not negate
return negate
if ctype == "py":
Expand Down

0 comments on commit 0169699

Please sign in to comment.