Repository navigation
Expand file tree
/
Copy pathironic_node.py
More file actions
477 lines (392 loc) · 15.3 KB
/
Copy pathironic_node.py
File metadata and controls
477 lines (392 loc) · 15.3 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import logging
from collections.abc import Sequence
from dataclasses import dataclass
import ironicclient.common.apiclient.exceptions
from ironicclient.common.utils import args_array_to_patch
from ironicclient.v1.node import Node
from understack_workflows.bmc import Bmc
from understack_workflows.ironic.client import IronicClient
FIRMWARE_UPDATE_TRAIT_PREFIX = "CUSTOM_FIRMWARE_UPDATE_"
STEADY_STATE_BOOT_INTERFACE = "http-ipxe"
_VIRTUAL_MEDIA_BOOT_INTERFACE = {
"idrac": "idrac-redfish-virtual-media",
"redfish": "redfish-virtual-media",
"ilo": "ilo-virtual-media",
"ilo5": "ilo-virtual-media",
"fake-hardware": "fake",
}
# When performing a "clean" or "provide" operation, how long do we wait for
# Ironic to move the node its final state:
NODE_STATE_TIMEOUT_SECS = 90 * 60
@dataclass
class NodeInterface:
name: str
mac_address: str
speed_mbps: int
ENROLLABLE_STATES = {"clean failed", "inspect failed", "enroll", "manageable"}
logger = logging.getLogger(__name__)
# Substrings of patch/update paths whose values must never be logged.
_SENSITIVE_PATH_FRAGMENTS = ("password", "secret", "token")
_REDACTED = "***REDACTED***"
def _is_sensitive(path: str) -> bool:
path = path.lower()
return any(fragment in path for fragment in _SENSITIVE_PATH_FRAGMENTS)
def _redact_patches(patches: Sequence[dict]) -> list[dict]:
"""Return a copy of JSON-patch operations with sensitive values redacted."""
redacted = []
for patch_op in patches:
if isinstance(patch_op, dict) and _is_sensitive(str(patch_op.get("path", ""))):
redacted.append({**patch_op, "value": _REDACTED})
else:
redacted.append(patch_op)
return redacted
def _redact_updates(updates: Sequence[str]) -> list[str]:
"""Return a copy of "path=value" update strings with sensitive values redacted."""
redacted = []
for update in updates:
path, sep, _ = str(update).partition("=")
if sep and _is_sensitive(path):
redacted.append(f"{path}={_REDACTED}")
else:
redacted.append(update)
return redacted
def create_or_update(
bmc: Bmc,
name: str,
manufacturer: str,
external_cmdb_id: int | str | None = None,
) -> Node:
"""Find-or-create Node by name, update attributes, set state to Manageable.
If the node exists in a quiescent state like "clean failed" then we will set
it to "manageable" state before proceeding to update the node attributes.
If the node exists in a "busy" state then we raise an exception.
If the node does not already exist it is created and then transitioned from
enroll->manageable state.
Note interfaces/ports are not synced here, that happens elsewhere.
"""
client = IronicClient()
driver, inspect_interface = _driver_for(manufacturer)
try:
node = client.get_node(name)
logger.debug(
"Baremetal node %s already exists with name %s in provision_state %s",
node.uuid,
name,
node.provision_state,
)
if node.provision_state not in ENROLLABLE_STATES:
raise Exception(
f"Re-enroll cannot proceed unless node is in one of the states "
f"{sorted(ENROLLABLE_STATES)} but node {node.uuid} "
f"is in '{node.provision_state}'."
)
if node.provision_state != "manageable":
transition(node, target_state="manage", expected_state="manageable")
update_ironic_node(
client,
bmc,
node,
name,
driver,
inspect_interface,
external_cmdb_id,
)
except ironicclient.common.apiclient.exceptions.NotFound:
logger.debug("Baremetal Node with name %s not found in Ironic, creating.", name)
node = create_ironic_node(
client,
bmc,
name,
driver,
inspect_interface,
external_cmdb_id,
)
# All newly-created nodes start out with "enroll" state:
transition(node, target_state="manage", expected_state="manageable")
return node
def update_ironic_node(
client,
bmc,
ironic_node,
name,
driver,
inspect_interface,
external_cmdb_id: int | str | None = None,
):
updates = [
f"name={name}",
f"driver={driver}",
f"driver_info/redfish_address={bmc.url()}",
"driver_info/redfish_verify_ca=false",
f"driver_info/redfish_username={bmc.username}",
f"driver_info/redfish_password={bmc.password}",
f"boot_interface={STEADY_STATE_BOOT_INTERFACE}",
f"inspect_interface={inspect_interface}",
]
if external_cmdb_id:
updates.append(f"extra/external_cmdb_id={external_cmdb_id}")
patches = args_array_to_patch("add", updates)
logger.info(
"Updating Ironic node %s patches=%s",
ironic_node.uuid,
_redact_patches(patches),
)
client.update_node(ironic_node.uuid, patches)
logger.debug("Ironic node %s Updated.", ironic_node.uuid)
def create_ironic_node(
client: IronicClient,
bmc: Bmc,
name: str,
driver: str,
inspect_interface: str,
external_cmdb_id: int | str | None = None,
) -> Node:
node_data = {
"name": name,
"driver": driver,
"driver_info": {
"redfish_address": bmc.url(),
"redfish_verify_ca": False,
"redfish_username": bmc.username,
"redfish_password": bmc.password,
},
"boot_interface": STEADY_STATE_BOOT_INTERFACE,
"deploy_interface": "direct",
"inspect_interface": inspect_interface,
}
if external_cmdb_id:
node_data["extra"] = {"external_cmdb_id": external_cmdb_id}
if driver == "fake-hardware":
node_data["resource_class"] = "fakehw"
return client.create_node(node_data)
def _driver_for(manufacturer: str) -> tuple[str, str]:
"""Answer the (driver, inspect_interface) for this server."""
if manufacturer.startswith("Dell"):
return ("idrac", "idrac-redfish")
else:
return ("redfish", "redfish")
def virtual_media_boot_interface_for(driver: str) -> str:
"""Return the virtual-media boot_interface name for this driver."""
try:
return _VIRTUAL_MEDIA_BOOT_INTERFACE[driver]
except KeyError as exc:
raise ValueError(
f"No virtual-media boot_interface configured for driver {driver!r}"
) from exc
def transition(
node: Node,
target_state: str,
expected_state: str | None = None,
clean_steps: list[dict] | None = None,
runbook: str | None = None,
disable_ramdisk: bool | None = None,
) -> None:
client = IronicClient()
logger.info("%s requesting provision state %s", node.uuid, target_state)
client.set_node_provision_state(
node.uuid,
target_state,
clean_steps=clean_steps,
runbook=runbook,
disable_ramdisk=disable_ramdisk,
)
if expected_state:
logger.info(
"Waiting for node %s to enter provision state %s",
node.uuid,
expected_state,
)
client.wait_for_node_provision_state(
node.uuid, expected_state, timeout=NODE_STATE_TIMEOUT_SECS
)
def patch(node: Node, updates: Sequence[str]) -> None:
if not updates:
return
logger.info("%s updating node %s", node.uuid, _redact_updates(updates))
IronicClient().update_node(node.uuid, args_array_to_patch("add", list(updates)))
def set_target_raid_config(node: Node, raid_config: dict) -> None:
IronicClient().set_node_target_raid_config(node.uuid, raid_config)
def inspect_out_of_band(node: Node):
intf_reset = args_array_to_patch("remove", ["inspect_interface"])
logger.info("[node:%s] Resetting to redfish interface", node.uuid)
IronicClient().update_node(node.uuid, intf_reset)
logger.info("[node:%s] Performing out-of-band inspection", node.uuid)
transition(node, target_state="inspect", expected_state="manageable")
def get_node_inventory(node: Node) -> dict:
"""Return the raw inventory dict populated by OOB inspection."""
return IronicClient().get_node_inventory(node.uuid)
def get_node_interfaces(node: Node) -> list[NodeInterface]:
"""Return interfaces from the inventory populated by OOB inspection."""
inventory_data = get_node_inventory(node)
interfaces_raw = inventory_data.get("inventory", {}).get("interfaces", [])
return [
NodeInterface(
name=iface["name"],
mac_address=iface["mac_address"],
speed_mbps=iface.get("speed_mbps", 0),
)
for iface in interfaces_raw
]
def list_node_ports(node: Node) -> list:
"""Return ironic ports for this node."""
return list(IronicClient().list_ports(node.uuid))
def pxe_enabled_bios_name(node: Node) -> str | None:
"""BIOS-reported name of port currently flagged pxe_enabled.
We don't count a port whose physical_network has the placeholder "enrol"
value.
extra.bios_name is populated by the port-bios-name inspection hook during
out-of-band redfish inspection.
pxe_enabled is populated by the port-enroll-config hook during agent
inspection.
"""
for port in list_node_ports(node):
if (
port.pxe_enabled
and port.extra.get("bios_name")
and port.physical_network != "enrol"
):
return port.extra["bios_name"]
def get_lldp_connected_interfaces(
interfaces: list[NodeInterface], parsed_lldp: dict
) -> list[NodeInterface]:
"""Return interfaces that have a confirmed LLDP switch connection.
An interface is considered connected when it appears in ``parsed_lldp``
with a ``switch_port_id`` that is not ``"Not Available"``.
"""
return [
iface
for iface in interfaces
if parsed_lldp.get(iface.name, {}).get("switch_port_id")
not in (None, "Not Available")
]
def inspect(node: Node, inspect_interface: str = "agent"):
"""Set the node inspect interface, inspect and wait for success.
We raise an error if anything failed. If we return then the everything has
succeeded and the node has returned to the manageable state.
"""
refreshed_node = refresh(node, fields=["inspect_interface"])
if refreshed_node.inspect_interface != inspect_interface:
patch(node, [f"inspect_interface={inspect_interface}"])
transition(node, target_state="inspect", expected_state="manageable")
def run_post_inspection_runbook(node: Node, name_or_id: str) -> None:
"""Run a selected runbook while the inspected node is manageable.
Require at least one matching runbook trait before starting cleaning.
Let the runbook determine whether cleaning needs an agent ramdisk.
"""
client = IronicClient()
runbook = client.get_runbook(name_or_id)
traits = client.get_node_traits(node.uuid)
runbook_traits = getattr(runbook, "traits", None)
if runbook_traits is None:
raise ValueError(
f"[node:{node.uuid}] Runbook {name_or_id!r} has no traits metadata; "
"Ironic API 1.112 or newer is required"
)
if not set(traits).intersection(runbook_traits):
raise ValueError(
f"[node:{node.uuid}] Runbook {name_or_id!r} requires at least one "
f"matching node trait from {sorted(runbook_traits)}"
)
logger.info("[node:%s] Running post-inspection runbook %s", node.uuid, name_or_id)
transition(
node,
target_state="clean",
expected_state="manageable",
runbook=runbook.uuid,
)
def validate_node_runbook(node: Node, name_or_id: str):
"""Resolve a runbook and check its trait requirements without mutation."""
client = IronicClient()
runbook = client.get_runbook(name_or_id)
traits = client.get_node_traits(node.uuid)
runbook_traits = getattr(runbook, "traits", None)
if runbook_traits is None:
raise ValueError(
f"[node:{node.uuid}] Runbook {name_or_id!r} has no traits metadata; "
"Ironic API 1.112 or newer is required"
)
if not set(traits).intersection(runbook_traits):
raise ValueError(
f"[node:{node.uuid}] Runbook {name_or_id!r} requires at least one "
f"matching node trait from {sorted(runbook_traits)}"
)
return runbook
def validate_runbook_node(node: Node) -> None:
"""Require an idle, healthy node with no instance or allocation."""
state = node.provision_state
if state not in {"available", "manageable"}:
raise ValueError(
f"[node:{node.uuid}] State {state!r}: refusing to run runbooks"
)
for field in (
"maintenance",
"reservation",
"target_provision_state",
"allocation_id",
"instance_uuid",
):
value = getattr(node, field, None)
if value:
raise ValueError(
f"[node:{node.uuid}] State {state!r}, {field}={value!r}: "
"refusing to run runbooks"
)
def run_runbooks_standalone(node: Node, runbooks: list[str]) -> None:
"""Run idle-node runbooks in order; restore availability only on success."""
if not runbooks:
return
current = refresh(node)
validate_runbook_node(current)
# Validate the entire sequence before removing the node from availability.
resolved = [(name, validate_node_runbook(node, name)) for name in runbooks]
restore_available = current.provision_state == "available"
if restore_available:
transition(node, target_state="manage", expected_state="manageable")
for name, runbook in resolved:
current = refresh(node)
validate_runbook_node(current)
if current.provision_state != "manageable":
raise ValueError(
f"[node:{node.uuid}] Expected manageable before {name!r}, "
f"got {current.provision_state!r}"
)
logger.info("[node:%s] Running runbook %s", node.uuid, name)
try:
transition(
node,
target_state="clean",
expected_state="manageable",
runbook=runbook.uuid,
)
except Exception:
logger.exception("[node:%s] Runbook %s failed", node.uuid, name)
raise
if restore_available:
transition(node, target_state="provide", expected_state="available")
def apply_firmware_updates(node: Node) -> None:
client = IronicClient()
traits = client.get_node_traits(node.uuid)
update_traits = sorted(
(trait for trait in traits if trait.startswith(FIRMWARE_UPDATE_TRAIT_PREFIX)),
key=firmware_trait_sort_key,
)
if not update_traits:
logger.info("%s No firmware update traits found", node.uuid)
return
for trait in update_traits:
runbook = client.get_runbook(trait)
logger.info("%s Running firmware update trait %s", node.uuid, trait)
transition(
node,
"clean",
expected_state="manageable",
runbook=runbook.uuid,
)
def firmware_trait_sort_key(trait: str) -> tuple[int, str]:
parts = trait.split("_")
order = parts[3] if len(parts) > 3 else ""
if order.isdigit():
return (int(order), trait)
return (10**9, trait)
def refresh(node: Node, fields: list[str] | None = None) -> Node:
return IronicClient().get_node(node.uuid, fields=fields)