Repository navigation
Expand file tree
/
Copy pathrun_workflow
More file actions
executable file
·223 lines (181 loc) · 7.63 KB
/
Copy pathrun_workflow
File metadata and controls
executable file
·223 lines (181 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
"""Trigger a neops workflow execution and wait for it to reach a terminal state.
Examples
--------
# Inline parameters
./run_workflow wf.lab.neops.io/simple_lab_discovery:1.4.0 '{"subnets": []}'
# Parameters from a file (curl-style "@file")
./run_workflow wf.lab.neops.io/simple_lab_discovery:1.4.0 @scenarios/<name>/workflow-execution-parameters/discover-params.json
# Parameters from stdin
echo '{"foo": "bar"}' | ./run_workflow wf.example/something:1.0.0 -
# With executeOnParameters.ids
./run_workflow wf.example/configure:1.0.0 '{}' --ids 12,34
Exit codes
----------
0 workflow reached `completed_ack`
1 workflow reached `failed_safe_ack` or `failed_unsafe_ack`
2 could not start the workflow, or stopped polling before a terminal
state (timeout, or the engine refused the token)
"""
from __future__ import annotations
import argparse
import http.client
import json
import pathlib
import sys
import textwrap
import time
from typing import NoReturn
from urllib import error, request
import engine_http
# From neops-workflow-engine/src/model/execution/workflow-execution-status.ts:
# only three terminal states exist, all end in `_ack`.
TERMINAL_SUCCESS = {"completed_ack"}
TERMINAL_FAILURE = {"failed_safe_ack", "failed_unsafe_ack"}
# Where a started execution can be followed after this script stops polling.
MONITOR_URL = "http://localhost:3031"
# Seconds one engine round trip may take; without it a half-open connection
# blocks past `--timeout`.
REQUEST_TIMEOUT = 10.0
def load_parameters(arg: str) -> dict:
"""Resolve the `params` argument: inline JSON, `@file`, or `-` for stdin."""
if arg == "-":
return json.load(sys.stdin)
if arg.startswith("@"):
with pathlib.Path(arg[1:]).open() as fh:
return json.load(fh)
return json.loads(arg)
def parse_ids(arg: str) -> list[int]:
return [int(x) for x in arg.split(",") if x.strip()]
def build_request(url: str, *, payload: dict | None = None) -> request.Request:
data = json.dumps(payload).encode() if payload is not None else None
headers = engine_http.auth_headers()
if payload is not None:
headers["Content-Type"] = "application/json"
return request.Request(url, data=data, headers=headers)
def http_json(req: request.Request, *, timeout: float = REQUEST_TIMEOUT) -> dict:
with request.urlopen(req, timeout=timeout) as resp:
return json.load(resp)
def abort(message: str) -> NoReturn:
"""Exit 2, the code for a run that ended without seeing a terminal state."""
print(message, file=sys.stderr)
raise SystemExit(2)
def start_execution(engine_url: str, workflow: str, parameters: dict, ids: list[int]) -> str:
"""POST /workflow-execution and return the execution UUID."""
payload = {
"workflow": workflow,
"executeOnParameters": {"ids": ids},
"parameters": parameters,
}
req = build_request(f"{engine_url}/workflow-execution", payload=payload)
try:
resp = http_json(req)
except error.HTTPError as e:
if e.code in engine_http.UNAUTHORIZED:
abort(f"failed to start workflow {engine_http.refusal(e)}")
abort(f"failed to start workflow ({e.code} {e.reason}): {engine_http.error_body(e)}")
except error.URLError as e:
abort(f"failed to reach engine at {engine_url}: {e.reason}")
except http.client.InvalidURL as e:
abort(f"invalid engine URL {engine_url!r}: {e}")
except engine_http.REQUEST_ERRORS as e:
# Not retried: the engine may have accepted the POST before the
# answer was lost, and a second one would start a second execution.
abort(
f"lost the engine's answer to the start request ({e}); the execution may have started — "
f"check {MONITOR_URL} before running it again"
)
return resp["uuid"]
def fetch_execution(engine_url: str, uuid: str, timeout: float) -> dict | None:
"""GET one execution; None after a failure that polling again can outlast.
A 401 or 403 stops instead: the token will not recover by itself, and the
execution keeps running without this script.
"""
req = build_request(f"{engine_url}/workflow-execution/id/{uuid}")
try:
return http_json(req, timeout=timeout)
except engine_http.REQUEST_ERRORS as e:
if isinstance(e, error.HTTPError) and e.code in engine_http.UNAUTHORIZED:
abort(
f"polling stopped {engine_http.refusal(e)}. Execution {uuid} keeps running; follow it at {MONITOR_URL}"
)
print(f" poll failed, retrying: {e}", file=sys.stderr)
return None
def poll_until_terminal(engine_url: str, uuid: str, timeout: int, interval: float) -> int:
"""Poll the execution until terminal state. Return shell-style exit code."""
deadline = time.monotonic() + timeout
started = time.monotonic()
last_line = None
while (remaining := deadline - time.monotonic()) > 0:
data = fetch_execution(engine_url, uuid, timeout=min(REQUEST_TIMEOUT, remaining))
if data is None:
time.sleep(interval)
continue
status = data.get("status", "?")
reason = (data.get("reason") or "").strip()
# retryNote (engine >= 0.42.2-beta.3): why an execution is being
# retried, e.g. a CMS lock that could not be taken yet.
retry_note = (data.get("retryNote") or "").strip()
line = f"{status} — {reason}" if reason else status
if retry_note:
line = f"{line} (retry: {retry_note})"
if line != last_line:
elapsed = int(time.monotonic() - started)
print(f" [{elapsed:3d}s] {line}")
last_line = line
if status in TERMINAL_SUCCESS:
return 0
if status in TERMINAL_FAILURE:
print(
f"execution finished in non-success terminal state: {status}" + (f" — {reason}" if reason else ""),
file=sys.stderr,
)
return 1
time.sleep(interval)
print(f"timeout: workflow did not reach terminal state within {timeout}s", file=sys.stderr)
return 2
def build_arg_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
prog="run_workflow",
description="Trigger a neops workflow execution and wait for it to finish.",
epilog=textwrap.dedent(__doc__.split("Examples\n--------\n", 1)[1]),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
p.add_argument(
"workflow",
help="SemVer workflow id, e.g. wf.lab.neops.io/simple_lab_discovery:1.0.0",
)
p.add_argument(
"parameters",
nargs="?",
default="{}",
help='JSON parameters: inline ({"k":"v"}), "@path/to/file", or "-" for stdin. Default: {}',
)
p.add_argument(
"--ids",
default="",
help="Comma-separated entity ids for executeOnParameters.ids (default: empty / global execution)",
)
engine_http.add_engine_url_argument(p)
p.add_argument(
"--timeout",
type=int,
default=300,
help="Total polling timeout in seconds (default: 300)",
)
p.add_argument(
"--interval",
type=float,
default=2.0,
help="Polling interval in seconds (default: 2.0)",
)
return p
def main() -> None:
args = build_arg_parser().parse_args()
parameters = load_parameters(args.parameters)
ids = parse_ids(args.ids)
uuid = start_execution(args.engine_url, args.workflow, parameters, ids)
print(f"execution {uuid}")
sys.exit(poll_until_terminal(args.engine_url, uuid, args.timeout, args.interval))
if __name__ == "__main__":
main()