Skip to content

Commit

Permalink
CASMCMS-8557: Create faster way to clear out BOS components
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed Sep 18, 2024
1 parent b328ef1 commit ba82916
Show file tree
Hide file tree
Showing 7 changed files with 5,139 additions and 5,994 deletions.
97 changes: 89 additions & 8 deletions cray/modules/bos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ def updatemany_data_handler(args):
return "PATCH", path, data


def create_patch_shim(func):
def create_patch_shim_updatemany_components(func):
""" Callback function to custom create our own payload """

def _decorator(filter_ids, filter_session, patch, enabled, retry_policy, **kwargs):
def _decorator(filter_ids, filter_session, patch, enabled, retry_policy, clear_desired_state,
clear_staged_state, clear_pending_state, **kwargs):
filter_ids = filter_ids["value"]
filter_session = filter_session["value"]
if not (filter_ids or filter_session):
Expand All @@ -123,6 +124,23 @@ def _decorator(filter_ids, filter_session, patch, enabled, retry_policy, **kwarg
payload["patch"]["enabled"] = enabled["value"]
if retry_policy["value"] is not None:
payload["patch"]["retry_policy"] = retry_policy["value"]
empty_boot_artifacts = {
"initrd": "",
"kernel_parameters": "",
"kernel": "" }
if clear_pending_state["value"]:
clear_staged_state["value"] = True
clear_desired_state["value"] = True
if clear_staged_state["value"]:
payload["patch"]["staged_state"] = {
"session": "",
"configuration": "",
"boot_artifacts": empty_boot_artifacts }
if clear_desired_state["value"]:
payload["patch"]["desired_state"] = {
"bss_token": "",
"configuration": "",
"boot_artifacts": empty_boot_artifacts }
# Hack to tell the CLI we are passing our own payload; don't generate
kwargs[FROM_FILE_TAG] = {"value": payload, "name": FROM_FILE_TAG}
return func(data_handler=updatemany_data_handler, **kwargs)
Expand All @@ -132,13 +150,12 @@ def _decorator(filter_ids, filter_session, patch, enabled, retry_policy, **kwarg

def setup_components_patch():
""" Sets up an updatemany command for components """
source_command = cli.commands['v2'].commands['components'].commands['list']
source_command = cli.commands['v2'].commands['components'].commands.pop('create')
command_type = type(source_command)
new_command = command_type("updatemany")
for key, value in source_command.__dict__.items():
setattr(new_command, key, value)
cli.commands['v2'].commands['components'].commands[
'updatemany'] = new_command
cli.commands['v2'].commands['components'].commands['updatemany'] = new_command
new_command.params = []
default_params = [param for param in source_command.params if
not param.expose_value]
Expand Down Expand Up @@ -172,18 +189,80 @@ def setup_components_patch():
type=bool,
default=None,
metavar='BOOLEAN',
help="Shortcut for --patch '{\"enabled\":True/False}'"
help="Enable or disable the components"
)(new_command)
option(
'--retry-policy',
callback=_opt_callback,
type=int,
default=None,
metavar='INT',
help="Shortcut for --patch '{\"retry_policy\":<int>}'"
help="Set the retry policy for the components"
)(new_command)
option(
'--clear-desired-state',
is_flag=True,
callback=_opt_callback,
help='Clear all desired state fields for the components'
)(new_command)
option(
'--clear-staged-state',
is_flag=True,
callback=_opt_callback,
help='Clear all the staged state fields for the components'
)(new_command)
option(
'--clear-pending-state',
is_flag=True,
callback=_opt_callback,
help='Shortcut for --clear-desired-state --clear-staged-state'
)(new_command)
new_command.params += default_params
new_command.callback = create_patch_shim(new_command.callback)
new_command.callback = create_patch_shim_updatemany_components(new_command.callback)

def create_patch_shim_update_components(func):
""" Callback function to custom create our own payload """

def _decorator(clear_desired_state, clear_staged_state, clear_pending_state, **kwargs):
if clear_pending_state["value"]:
clear_staged_state["value"] = True
clear_desired_state["value"] = True
if clear_staged_state["value"]:
for arg in kwargs.values():
if arg["name"].find('staged_state') == 0:
arg["value"] = ""
if clear_desired_state["value"]:
for arg in kwargs.values():
if arg["name"].find('desired_state') == 0:
arg["value"] = ""
return func(**kwargs)

return _decorator


def modify_component_patch():
""" Modifies update command for components """
source_command = cli.commands['v2'].commands['components'].commands['update']
option(
'--clear-desired-state',
is_flag=True,
callback=_opt_callback,
help='Clear all desired state fields for the component'
)(source_command)
option(
'--clear-staged-state',
is_flag=True,
callback=_opt_callback,
help='Clear all staged state fields for the component'
)(source_command)
option(
'--clear-pending-state',
is_flag=True,
callback=_opt_callback,
help='Shortcut for --clear-desired-state --clear-staged-state'
)(source_command)
source_command.params = source_command.params[-3:] + source_command.params[:-3]
source_command.callback = create_patch_shim_update_components(source_command.callback)


def setup_v2_template_create():
Expand All @@ -209,3 +288,5 @@ def setup_v2_template_create():
)

setup_components_patch()

modify_component_patch()
7 changes: 2 additions & 5 deletions cray/modules/bos/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,7 @@ components:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/V2ComponentsUpdate'
- $ref: '#/components/schemas/V2ComponentArrayWithIds'
type: object
V2optionsUpdateRequest:
description: Service-wide options
required: true
Expand Down Expand Up @@ -1698,13 +1696,12 @@ paths:
$ref: '#/components/responses/V2componentDetailsArray'
400:
$ref: '#/components/responses/BadRequest'
patch:
post:
summary: Update a collection of Components
description: Update the state for a collection of Components in the BOS database
tags:
- v2
- components
- cli_ignore
x-openapi-router-controller: bos.server.controllers.v2.components
operationId: patch_v2_components
requestBody:
Expand Down
Loading

0 comments on commit ba82916

Please sign in to comment.