@ansible-flow/assets 1.0.3-rc.1 → 1.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/probe/afd_probe/methods.py +2 -0
- package/probe/strategy_plugins/afd_step.py +166 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Ansible Flow Designer files a host passes by path: the Python probe, the run events callback, the JSON schemas, the contract with Ansible Flow Server and the sample projects.
|
|
4
4
|
|
|
5
|
-
Built from [Ansible Flow Designer](https://github.com/ChistokhinSV/ansible-flow-designer) 1.0
|
|
5
|
+
Built from [Ansible Flow Designer](https://github.com/ChistokhinSV/ansible-flow-designer) 1.1.0-rc.1
|
|
6
6
|
and released with it. The folders are found by path: `import { probeDir, schemaDir, contractDir, samplesDir } from '@ansible-flow/assets'`.
|
|
7
7
|
|
|
8
8
|
What is in the package, which entry points are documented and what may change:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ansible-flow/assets",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0-rc.1",
|
|
4
4
|
"description": "Ansible Flow Designer files a host passes by path: the Python probe, the run events callback, the JSON schemas, the contract with Ansible Flow Server and the sample projects.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/ChistokhinSV/ansible-flow-designer/blob/main/docs/packages.md",
|
|
@@ -48,6 +48,8 @@ def run_config(root: str) -> dict[str, Any]:
|
|
|
48
48
|
"lookup_plugins": paths("DEFAULT_LOOKUP_PLUGIN_PATH"),
|
|
49
49
|
"test_plugins": paths("DEFAULT_TEST_PLUGIN_PATH"),
|
|
50
50
|
"vars_plugins": paths("DEFAULT_VARS_PLUGIN_PATH"),
|
|
51
|
+
# AFD-346: a step-by-step run sets ANSIBLE_STRATEGY_PLUGINS; these stay in it.
|
|
52
|
+
"strategy_plugins": paths("DEFAULT_STRATEGY_PLUGIN_PATH"),
|
|
51
53
|
},
|
|
52
54
|
"vars_enabled": [str(v) for v in (getattr(C, "VARIABLE_PLUGINS_ENABLED", None) or [])],
|
|
53
55
|
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Ansible Flow Designer step-by-step runs (AFD-346, ADR-0039): the linear strategy that, before the
|
|
2
|
+
# first task of a flow step, asks the run's driver whether to go on. Stdlib only; enabled for a
|
|
3
|
+
# step-by-step run only, with ANSIBLE_STRATEGY=afd_step and ANSIBLE_STRATEGY_PLUGINS naming this
|
|
4
|
+
# folder. With it off, nothing here is loaded and a run is what it always was.
|
|
5
|
+
#
|
|
6
|
+
# The channel is two files in the run folder, the same way events.jsonl already comes back from a
|
|
7
|
+
# local, WSL, SSH or container run (nothing listens on a port):
|
|
8
|
+
# - run -> driver: pause, resume and stopped events, appended to events.jsonl (AFD_EVENTS);
|
|
9
|
+
# - driver -> run: control.json (AFD_STEP_CONTROL), replaced whole (write a temporary file, then
|
|
10
|
+
# rename): {"v": 1, "command": "step"|"continue"|"pause"|"stop", "points": [node ids],
|
|
11
|
+
# "reply": "<pause id>"|null}.
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import secrets
|
|
17
|
+
import time
|
|
18
|
+
|
|
19
|
+
from ansible.plugins.strategy.linear import StrategyModule as LinearStrategy
|
|
20
|
+
from ansible.utils.display import Display
|
|
21
|
+
|
|
22
|
+
DOCUMENTATION = """
|
|
23
|
+
name: afd_step
|
|
24
|
+
short_description: Ansible Flow Designer step-by-step runs (linear, pausing before each step)
|
|
25
|
+
description:
|
|
26
|
+
- The linear strategy, pausing before the first task of a flow step (a play with
|
|
27
|
+
vars.flow_node_id) until the driver of the run says step, continue or stop in the file
|
|
28
|
+
named by AFD_STEP_CONTROL. Pause, resume and stopped events go to the file named by
|
|
29
|
+
AFD_EVENTS, beside the afd_events callback's.
|
|
30
|
+
- A step's several plays (arriving hosts, the step, its route) pause once, before the first.
|
|
31
|
+
- AFD_STEP_TIMEOUT (seconds, default 3600) ends a pause nobody answers; the run then stops.
|
|
32
|
+
- AFD_STEP_INSIDE names the step a run starts inside (an approval's later segment): no pause
|
|
33
|
+
before its first play.
|
|
34
|
+
author: Ansible Flow Designer
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
display = Display()
|
|
38
|
+
|
|
39
|
+
VERSION = 1
|
|
40
|
+
POLL_S = 0.1
|
|
41
|
+
DEFAULT_TIMEOUT_S = 3600.0
|
|
42
|
+
# Pausing again: the driver's command until it changes; step and pause stop at the next step.
|
|
43
|
+
PAUSING = ("step", "pause")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class _State:
|
|
47
|
+
"""One per ansible-playbook process (kept on the task queue manager, which outlives plays)."""
|
|
48
|
+
|
|
49
|
+
def __init__(self) -> None:
|
|
50
|
+
self.control = os.environ.get("AFD_STEP_CONTROL") or ""
|
|
51
|
+
events = os.environ.get("AFD_EVENTS")
|
|
52
|
+
self.events = events or ""
|
|
53
|
+
try:
|
|
54
|
+
self.timeout = float(os.environ.get("AFD_STEP_TIMEOUT") or DEFAULT_TIMEOUT_S)
|
|
55
|
+
except ValueError:
|
|
56
|
+
self.timeout = DEFAULT_TIMEOUT_S
|
|
57
|
+
# Pause ids name this process too: a run of approval segments is several processes.
|
|
58
|
+
self.token = secrets.token_hex(4)
|
|
59
|
+
self.count = 0
|
|
60
|
+
# An approval's later segment starts inside the approval step (its route play): that step
|
|
61
|
+
# paused already, in the segment before (AFD_STEP_INSIDE names it).
|
|
62
|
+
self.last_node: str | None = os.environ.get("AFD_STEP_INSIDE") or None
|
|
63
|
+
self.stopped = False
|
|
64
|
+
self.good: dict = {}
|
|
65
|
+
|
|
66
|
+
def read(self) -> dict:
|
|
67
|
+
"""The driver's last control.json. While it is being replaced (or a half-written one is
|
|
68
|
+
read over SSH) the last whole one stands: a step boundary never runs on by mistake."""
|
|
69
|
+
try:
|
|
70
|
+
with open(self.control, encoding="utf-8") as f:
|
|
71
|
+
data = json.load(f)
|
|
72
|
+
except (OSError, ValueError):
|
|
73
|
+
return self.good
|
|
74
|
+
if isinstance(data, dict) and data.get("v") == VERSION:
|
|
75
|
+
self.good = data
|
|
76
|
+
return self.good
|
|
77
|
+
|
|
78
|
+
def emit(self, **event: object) -> None:
|
|
79
|
+
if not self.events:
|
|
80
|
+
return
|
|
81
|
+
event["ts"] = round(time.time(), 3)
|
|
82
|
+
# Appended as one line, as the afd_events callback does (same process, same thread).
|
|
83
|
+
with open(self.events, "a", encoding="utf-8") as f:
|
|
84
|
+
f.write(json.dumps(event, default=str) + "\n")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _wants_pause(control: dict, node: str) -> bool:
|
|
88
|
+
if control.get("command") in PAUSING:
|
|
89
|
+
return True
|
|
90
|
+
points = control.get("points")
|
|
91
|
+
return isinstance(points, list) and node in points
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class StrategyModule(LinearStrategy):
|
|
95
|
+
def _afd_state(self) -> _State:
|
|
96
|
+
state = getattr(self._tqm, "_afd_step", None)
|
|
97
|
+
if state is None:
|
|
98
|
+
state = _State()
|
|
99
|
+
self._tqm._afd_step = state
|
|
100
|
+
return state
|
|
101
|
+
|
|
102
|
+
def run(self, iterator, play_context): # type: ignore[override]
|
|
103
|
+
state = self._afd_state()
|
|
104
|
+
play = iterator._play
|
|
105
|
+
node = (play.vars or {}).get("flow_node_id")
|
|
106
|
+
node = str(node) if node else None
|
|
107
|
+
if state.stopped:
|
|
108
|
+
return self._tqm.RUN_FAILED_BREAK_PLAY
|
|
109
|
+
# The first play of a step only: a step's arriving and route plays go on with it.
|
|
110
|
+
if node and node != state.last_node and state.control:
|
|
111
|
+
self._set_hosts_cache(play)
|
|
112
|
+
hosts = [h.get_name() for h in self.get_hosts_left(iterator)]
|
|
113
|
+
# A play no host reaches is not where a step starts for anyone.
|
|
114
|
+
if hosts:
|
|
115
|
+
state.last_node = node
|
|
116
|
+
if self._afd_gate(state, node, play.get_name(), hosts) == "stop":
|
|
117
|
+
state.stopped = True
|
|
118
|
+
return self._tqm.RUN_FAILED_BREAK_PLAY
|
|
119
|
+
return super().run(iterator, play_context)
|
|
120
|
+
|
|
121
|
+
def _afd_gate(self, state: _State, node: str, name: str, hosts: list[str]) -> str:
|
|
122
|
+
"""Pauses before `node` when the driver wants it; returns step, continue or stop."""
|
|
123
|
+
control = state.read()
|
|
124
|
+
if control.get("command") == "stop":
|
|
125
|
+
state.emit(event="stopped", node=node, play=name, hosts=hosts, reason="driver")
|
|
126
|
+
return "stop"
|
|
127
|
+
if not _wants_pause(control, node):
|
|
128
|
+
return "continue"
|
|
129
|
+
state.count += 1
|
|
130
|
+
pause = f"{state.token}-{state.count}"
|
|
131
|
+
state.emit(
|
|
132
|
+
event="pause",
|
|
133
|
+
id=pause,
|
|
134
|
+
node=node,
|
|
135
|
+
play=name,
|
|
136
|
+
hosts=hosts,
|
|
137
|
+
timeout=state.timeout,
|
|
138
|
+
)
|
|
139
|
+
display.display(f"AFD: paused before {name} ({len(hosts)} host(s)); waiting for the driver")
|
|
140
|
+
deadline = time.monotonic() + state.timeout
|
|
141
|
+
while True:
|
|
142
|
+
control = state.read()
|
|
143
|
+
command = control.get("command")
|
|
144
|
+
if control.get("reply") == pause and command in ("step", "continue", "stop"):
|
|
145
|
+
break
|
|
146
|
+
# A stop needs no reply: it may come from a driver that missed this pause.
|
|
147
|
+
if command == "stop":
|
|
148
|
+
break
|
|
149
|
+
# AFD-347 seam: while paused, requests about this pause (variables, evaluate, set) are
|
|
150
|
+
# served here, with the play, hosts and variable manager at hand. Not built (AFD-346).
|
|
151
|
+
if time.monotonic() > deadline:
|
|
152
|
+
state.emit(
|
|
153
|
+
event="stopped", id=pause, node=node, play=name, hosts=hosts, reason="timeout"
|
|
154
|
+
)
|
|
155
|
+
display.warning(
|
|
156
|
+
f"AFD: nobody answered within {int(state.timeout)} s; the run stops"
|
|
157
|
+
)
|
|
158
|
+
return "stop"
|
|
159
|
+
time.sleep(POLL_S)
|
|
160
|
+
if command == "stop":
|
|
161
|
+
state.emit(
|
|
162
|
+
event="stopped", id=pause, node=node, play=name, hosts=hosts, reason="driver"
|
|
163
|
+
)
|
|
164
|
+
return "stop"
|
|
165
|
+
state.emit(event="resume", id=pause, node=node, command=command)
|
|
166
|
+
return command
|