Skip to content

Commit 6f9c62b

Browse files
authored
Add --all parameter to baremetal clean command with safety confirmation (#1820)
AI-assisted: Claude Code Signed-off-by: Christian Berendt <[email protected]>
1 parent 6f82a60 commit 6f9c62b

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

osism/commands/baremetal.py

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -873,52 +873,86 @@ def get_parser(self, prog_name):
873873

874874
parser.add_argument(
875875
"name",
876+
nargs="?",
876877
type=str,
877878
help="Clean given baremetal node when in provision state available",
878879
)
880+
parser.add_argument(
881+
"--all",
882+
default=False,
883+
help="Clean all baremetal nodes in provision state available",
884+
action="store_true",
885+
)
886+
parser.add_argument(
887+
"--yes-i-really-really-mean-it",
888+
default=False,
889+
help="Specify this to actually clean all nodes",
890+
action="store_true",
891+
)
879892
return parser
880893

881894
def take_action(self, parsed_args):
895+
all_nodes = parsed_args.all
882896
name = parsed_args.name
897+
yes_i_really_really_mean_it = parsed_args.yes_i_really_really_mean_it
898+
899+
if not all_nodes and not name:
900+
logger.error("Please specify a node name or use --all")
901+
return
902+
903+
if all_nodes and not yes_i_really_really_mean_it:
904+
logger.error(
905+
"Please confirm that you wish to clean all nodes by specifying '--yes-i-really-really-mean-it'"
906+
)
907+
return
883908

884909
clean_steps = [{"interface": "deploy", "step": "erase_devices"}]
885910

886911
conn = get_cloud_connection()
887912

888-
node = conn.baremetal.find_node(name, ignore_missing=True, details=True)
889-
if not node:
890-
logger.warning(f"Could not find node {name}")
891-
return
892-
893-
if node.provision_state in ["available"]:
894-
# NOTE: Clean is available in the "manageable" provision state, so we move the node into this state
895-
try:
896-
node = conn.baremetal.set_node_provision_state(node.id, "manage")
897-
node = conn.baremetal.wait_for_nodes_provision_state(
898-
[node.id], "manageable"
899-
)[0]
900-
except Exception as exc:
901-
logger.warning(
902-
f"Node {node.name} ({node.id}) could not be moved to manageable state: {exc}"
903-
)
913+
if all_nodes:
914+
clean_nodes = list(conn.baremetal.nodes(details=True))
915+
else:
916+
node = conn.baremetal.find_node(name, ignore_missing=True, details=True)
917+
if not node:
918+
logger.warning(f"Could not find node {name}")
904919
return
920+
clean_nodes = [node]
905921

906-
if node.provision_state in ["manageable"]:
907-
try:
908-
conn.baremetal.set_node_provision_state(
909-
node.id, "clean", clean_steps=clean_steps
910-
)
911-
logger.info(
912-
f"Successfully initiated clean for node {node.name} ({node.id})"
922+
for node in clean_nodes:
923+
if not node:
924+
continue
925+
926+
if node.provision_state in ["available"]:
927+
# NOTE: Clean is available in the "manageable" provision state, so we move the node into this state
928+
try:
929+
node = conn.baremetal.set_node_provision_state(node.id, "manage")
930+
node = conn.baremetal.wait_for_nodes_provision_state(
931+
[node.id], "manageable"
932+
)[0]
933+
except Exception as exc:
934+
logger.warning(
935+
f"Node {node.name} ({node.id}) could not be moved to manageable state: {exc}"
936+
)
937+
continue
938+
939+
if node.provision_state in ["manageable"]:
940+
try:
941+
conn.baremetal.set_node_provision_state(
942+
node.id, "clean", clean_steps=clean_steps
943+
)
944+
logger.info(
945+
f"Successfully initiated clean for node {node.name} ({node.id})"
946+
)
947+
except Exception as exc:
948+
logger.warning(
949+
f"Clean of node {node.name} ({node.id}) failed: {exc}"
950+
)
951+
continue
952+
else:
953+
logger.warning(
954+
f"Node {node.name} ({node.id}) not in supported state! Provision state: {node.provision_state}, maintenance mode: {node['maintenance']}"
913955
)
914-
except Exception as exc:
915-
logger.warning(f"Clean of node {node.name} ({node.id}) failed: {exc}")
916-
return
917-
else:
918-
logger.warning(
919-
f"Node {node.name} ({node.id}) not in supported state! Provision state: {node.provision_state}, maintenance mode: {node['maintenance']}"
920-
)
921-
return
922956

923957

924958
class BaremetalProvide(Command):

0 commit comments

Comments
 (0)