Skip to content

Commit efd8702

Browse files
feat(argo-workflows): Run post-enrollment setup independently
1 parent f68c90d commit efd8702

10 files changed

Lines changed: 519 additions & 194 deletions

File tree

‎python/understack-workflows/README.md‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ operation happens against the Nautobot Tenants. Operations include
1010
create, update and delete.
1111

1212

13-
## enroll-server post-inspection runbooks
13+
## Ironic runbooks
1414

15-
Optionally pass an ordered JSON array of runbook names or UUIDs:
15+
`apply-node-runbook` runs configured Ironic runbooks for a node, handling the
16+
required state transitions. Runbooks execute in order; a failure stops the list.
1617

1718
```sh
18-
enroll-server --ip-address 192.0.2.10 \
19-
--post-inspection-runbooks '["first-runbook", "second-runbook"]'
19+
apply-node-runbook --node NODE_UUID \
20+
--runbooks '["bmc-configure-snmpv3-user"]'
2021
```
2122

22-
For Argo, use the `enroll-server` WorkflowTemplate parameter
23-
`post_inspection_runbooks` with the same JSON array. It defaults to `[]`.
23+
Only idle nodes in `available` or `manageable` are supported. Active nodes,
24+
maintenance, conductor reservations, pending state transitions, and instance or
25+
allocation associations are rejected before any state change. Errors include the
26+
node state and blocking field value. An idle node with an instance UUID must have
27+
its association investigated and resolved separately; the runner never clears it.
2428

25-
Runbooks execute sequentially after agent inspection and final BIOS
26-
configuration, before RAID configuration, firmware updates, and `provide`.
27-
Each requires at least one matching node/runbook trait (Ironic API 1.112 or
28-
newer), uses the runbook's ramdisk setting, and waits for `manageable`.
29-
A failed lookup, trait check, or cleaning operation stops enrollment.
29+
For `available` nodes, the runner transitions to `manageable`, runs manual
30+
cleaning, then requests `provide` to return to `available`. Nodes starting in
31+
`manageable` remain there. Failures stop the sequence without restoring the
32+
starting state; inspect the Ironic error before retrying.
33+
34+
For automatic execution after enrollment, configure `post_enroll_workflows`
35+
with WorkflowTemplates that accept `node_uuid`.

‎python/understack-workflows/pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
]
3131

3232
[project.scripts]
33+
apply-node-runbook = "understack_workflows.main.apply_node_runbook:main"
3334
bmc-kube-password = "understack_workflows.main.bmc_display_password:main"
3435
bmc-password = "understack_workflows.main.print_bmc_password:main"
3536
enroll-fw = "understack_workflows.main.enroll_fw:main"
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
import argparse
2+
from types import SimpleNamespace
3+
from typing import cast
4+
from unittest.mock import call
5+
6+
import pytest
7+
from ironicclient.v1.node import Node
8+
9+
from understack_workflows import ironic_node
10+
from understack_workflows.main import apply_node_runbook
11+
12+
# --- ironic_node.run_runbooks_standalone (state bracketing) -------------------
13+
14+
15+
def _patch_client(
16+
mocker,
17+
*,
18+
provision_state,
19+
runbook_traits=("CUSTOM_DELL",),
20+
node_traits=("CUSTOM_DELL",),
21+
):
22+
client = mocker.patch.object(ironic_node, "IronicClient").return_value
23+
client.get_runbook.return_value = SimpleNamespace(
24+
uuid="runbook-123", traits=list(runbook_traits)
25+
)
26+
client.get_node_traits.return_value = list(node_traits)
27+
mocker.patch.object(
28+
ironic_node,
29+
"refresh",
30+
side_effect=[SimpleNamespace(uuid="node-123", provision_state=provision_state)]
31+
+ [SimpleNamespace(uuid="node-123", provision_state="manageable")] * 10,
32+
)
33+
return client
34+
35+
36+
def test_standalone_available_node_is_bracketed_manage_and_provide(mocker):
37+
client = _patch_client(mocker, provision_state="available")
38+
node = cast(Node, SimpleNamespace(uuid="node-123"))
39+
40+
ironic_node.run_runbooks_standalone(node, ["bmc-configure-snmpv3-user"])
41+
42+
# manage -> clean(runbook) -> provide, in that order.
43+
assert client.set_node_provision_state.mock_calls == [
44+
call(
45+
"node-123", "manage", clean_steps=None, runbook=None, disable_ramdisk=None
46+
),
47+
call(
48+
"node-123",
49+
"clean",
50+
clean_steps=None,
51+
runbook="runbook-123",
52+
disable_ramdisk=None,
53+
),
54+
call(
55+
"node-123", "provide", clean_steps=None, runbook=None, disable_ramdisk=None
56+
),
57+
]
58+
assert client.wait_for_node_provision_state.mock_calls == [
59+
call("node-123", "manageable", timeout=ironic_node.NODE_STATE_TIMEOUT_SECS),
60+
call("node-123", "manageable", timeout=ironic_node.NODE_STATE_TIMEOUT_SECS),
61+
call("node-123", "available", timeout=ironic_node.NODE_STATE_TIMEOUT_SECS),
62+
]
63+
64+
65+
def test_standalone_manageable_node_is_not_provided(mocker):
66+
client = _patch_client(mocker, provision_state="manageable")
67+
node = cast(Node, SimpleNamespace(uuid="node-123"))
68+
69+
ironic_node.run_runbooks_standalone(node, ["bmc-configure-snmpv3-user"])
70+
71+
# No manage, no provide: only the clean runs; node stays manageable.
72+
assert client.set_node_provision_state.mock_calls == [
73+
call(
74+
"node-123",
75+
"clean",
76+
clean_steps=None,
77+
runbook="runbook-123",
78+
disable_ramdisk=None,
79+
),
80+
]
81+
82+
83+
@pytest.mark.parametrize("state", ["active", "deploying", "clean failed", "enroll"])
84+
def test_standalone_unexpected_state_is_refused(mocker, state):
85+
client = _patch_client(mocker, provision_state=state)
86+
node = cast(Node, SimpleNamespace(uuid="node-123"))
87+
88+
with pytest.raises(ValueError, match="refusing to run runbook"):
89+
ironic_node.run_runbooks_standalone(node, ["bmc-configure-snmpv3-user"])
90+
91+
client.set_node_provision_state.assert_not_called()
92+
93+
94+
@pytest.mark.parametrize("runbook_traits", [[], ["CUSTOM_OTHER"], None])
95+
def test_standalone_rejects_ineligible_runbook(mocker, runbook_traits):
96+
client = _patch_client(
97+
mocker,
98+
provision_state="manageable",
99+
node_traits=("CUSTOM_DELL",),
100+
)
101+
client.get_runbook.return_value.traits = runbook_traits
102+
node = cast(Node, SimpleNamespace(uuid="node-123"))
103+
104+
with pytest.raises(ValueError, match=r"traits metadata|matching node trait"):
105+
ironic_node.run_runbooks_standalone(node, ["bmc-configure-snmpv3-user"])
106+
107+
client.set_node_provision_state.assert_not_called()
108+
109+
110+
# --- CLI ---------------------------------------------------------------------
111+
112+
113+
@pytest.mark.parametrize(
114+
"value", ["invalid", '"first"', "{}", "null", "[null]", "[1]", '[""]', '[" "]']
115+
)
116+
def test_parse_runbooks_rejects_invalid(value):
117+
with pytest.raises(argparse.ArgumentTypeError, match="JSON array"):
118+
apply_node_runbook.parse_runbooks(value)
119+
120+
121+
def test_main_single_node(mocker):
122+
mocker.patch(
123+
"sys.argv",
124+
[
125+
"apply-node-runbook",
126+
"--node",
127+
"node-123",
128+
"--runbooks",
129+
'["first", "second"]',
130+
],
131+
)
132+
mocker.patch.object(apply_node_runbook.helpers, "setup_logger")
133+
client = mocker.patch.object(apply_node_runbook, "IronicClient").return_value
134+
run = mocker.patch.object(ironic_node, "run_runbooks_standalone")
135+
136+
apply_node_runbook.main()
137+
138+
client.get_node.assert_called_once_with("node-123")
139+
run.assert_called_once_with(client.get_node.return_value, ["first", "second"])
140+
141+
142+
def test_main_propagates_runbook_failure(mocker):
143+
mocker.patch(
144+
"sys.argv",
145+
["apply-node-runbook", "--node", "node-123", "--runbooks", '["first"]'],
146+
)
147+
mocker.patch.object(apply_node_runbook.helpers, "setup_logger")
148+
mocker.patch.object(apply_node_runbook, "IronicClient")
149+
mocker.patch.object(
150+
ironic_node, "run_runbooks_standalone", side_effect=RuntimeError("clean failed")
151+
)
152+
153+
with pytest.raises(RuntimeError, match="clean failed"):
154+
apply_node_runbook.main()
155+
156+
157+
def test_main_requires_node(mocker, capsys):
158+
mocker.patch("sys.argv", ["apply-node-runbook", "--runbooks", '["first"]'])
159+
mocker.patch.object(apply_node_runbook.helpers, "setup_logger")
160+
client = mocker.patch.object(apply_node_runbook, "IronicClient")
161+
162+
with pytest.raises(SystemExit) as exc:
163+
apply_node_runbook.main()
164+
165+
assert exc.value.code == 2
166+
assert "--node" in capsys.readouterr().err
167+
client.assert_not_called()
168+
169+
170+
@pytest.mark.parametrize(
171+
("state", "field"),
172+
[
173+
(state, field)
174+
for state in ["available", "manageable"]
175+
for field in [
176+
"maintenance",
177+
"allocation_id",
178+
"instance_uuid",
179+
"reservation",
180+
"target_provision_state",
181+
]
182+
],
183+
)
184+
def test_standalone_refuses_reserved_or_unhealthy_node(mocker, field, state):
185+
client = _patch_client(mocker, provision_state=state)
186+
mocker.patch.object(
187+
ironic_node,
188+
"refresh",
189+
return_value=SimpleNamespace(
190+
uuid="node-123", provision_state=state, **{field: True}
191+
),
192+
)
193+
with pytest.raises(ValueError, match=rf"State '{state}', {field}=True"):
194+
ironic_node.run_runbooks_standalone(
195+
cast(Node, SimpleNamespace(uuid="node-123")), ["first"]
196+
)
197+
client.set_node_provision_state.assert_not_called()
198+
199+
200+
def test_sequence_brackets_once_and_stops_without_provide_on_failure(mocker):
201+
client = _patch_client(mocker, provision_state="available")
202+
client.wait_for_node_provision_state.side_effect = [
203+
None,
204+
None,
205+
RuntimeError("clean failed"),
206+
]
207+
node = cast(Node, SimpleNamespace(uuid="node-123"))
208+
with pytest.raises(RuntimeError, match="clean failed"):
209+
ironic_node.run_runbooks_standalone(node, ["first", "second", "third"])
210+
assert [c.args[1] for c in client.set_node_provision_state.call_args_list] == [
211+
"manage",
212+
"clean",
213+
"clean",
214+
]
215+
216+
217+
def test_sequence_restores_available_only_once(mocker):
218+
client = _patch_client(mocker, provision_state="available")
219+
ironic_node.run_runbooks_standalone(
220+
cast(Node, SimpleNamespace(uuid="node-123")), ["first", "second"]
221+
)
222+
assert [c.args[1] for c in client.set_node_provision_state.call_args_list] == [
223+
"manage",
224+
"clean",
225+
"clean",
226+
"provide",
227+
]
228+
229+
230+
def test_empty_sequence_does_not_change_state(mocker):
231+
refresh = mocker.patch.object(ironic_node, "refresh")
232+
ironic_node.run_runbooks_standalone(
233+
cast(Node, SimpleNamespace(uuid="node-123")), []
234+
)
235+
refresh.assert_not_called()
236+
237+
238+
def test_sequence_preflights_later_runbooks_before_state_change(mocker):
239+
client = _patch_client(mocker, provision_state="available")
240+
client.get_runbook.side_effect = [
241+
SimpleNamespace(uuid="r-1", traits=["CUSTOM_DELL"]),
242+
SimpleNamespace(uuid="r-2", traits=["CUSTOM_OTHER"]),
243+
]
244+
with pytest.raises(ValueError, match="matching node trait"):
245+
ironic_node.run_runbooks_standalone(
246+
cast(Node, SimpleNamespace(uuid="node-123")), ["first", "second"]
247+
)
248+
client.set_node_provision_state.assert_not_called()
249+
250+
251+
def test_sequence_does_not_provide_when_manage_fails(mocker):
252+
client = _patch_client(mocker, provision_state="available")
253+
client.wait_for_node_provision_state.side_effect = RuntimeError("manage failed")
254+
with pytest.raises(RuntimeError, match="manage failed"):
255+
ironic_node.run_runbooks_standalone(
256+
cast(Node, SimpleNamespace(uuid="node-123")), ["first", "second"]
257+
)
258+
assert [c.args[1] for c in client.set_node_provision_state.call_args_list] == [
259+
"manage"
260+
]
261+
262+
263+
def test_sequence_refuses_instance_association_added_after_manage(mocker):
264+
client = _patch_client(mocker, provision_state="available")
265+
mocker.patch.object(
266+
ironic_node,
267+
"refresh",
268+
side_effect=[
269+
SimpleNamespace(uuid="node-123", provision_state="available"),
270+
SimpleNamespace(
271+
uuid="node-123",
272+
provision_state="manageable",
273+
instance_uuid="instance-123",
274+
),
275+
],
276+
)
277+
with pytest.raises(ValueError, match="instance_uuid='instance-123'"):
278+
ironic_node.run_runbooks_standalone(
279+
cast(Node, SimpleNamespace(uuid="node-123")), ["first"]
280+
)
281+
assert [c.args[1] for c in client.set_node_provision_state.call_args_list] == [
282+
"manage"
283+
]
284+
285+
286+
def test_sequence_refuses_unexpected_idle_state_before_clean(mocker):
287+
client = _patch_client(mocker, provision_state="manageable")
288+
mocker.patch.object(
289+
ironic_node,
290+
"refresh",
291+
side_effect=[
292+
SimpleNamespace(uuid="node-123", provision_state=state)
293+
for state in ["manageable", "available"]
294+
],
295+
)
296+
with pytest.raises(ValueError, match=r"Expected manageable.*got 'available'"):
297+
ironic_node.run_runbooks_standalone(
298+
cast(Node, SimpleNamespace(uuid="node-123")), ["first"]
299+
)
300+
client.set_node_provision_state.assert_not_called()

0 commit comments

Comments
 (0)