User Management CLI Enhancements
Added generic capability in the fault manager CLI Framework for supporting user confirmation prompt to all system CLI commands that could impact service. Story: 2011240 Task: 51196 Change-Id: I25a544e4df8fd1ac217c023a87bc4f6977cdaea6 Signed-off-by: Sabyasachi Nayak <sabyasachi.nayak@windriver.com>
This commit is contained in:
parent
448bb06c3b
commit
5c2e05d78b
@ -32,6 +32,8 @@ import argparse
|
||||
import dateutil
|
||||
import prettytable
|
||||
import textwrap
|
||||
import signal
|
||||
import sys
|
||||
|
||||
from datetime import datetime
|
||||
from dateutil import parser
|
||||
@ -228,7 +230,9 @@ def define_command(subparsers, command, callback, cmd_mapper):
|
||||
formatter_class=HelpFormatter)
|
||||
subparser.add_argument('-h', '--help', action='help',
|
||||
help=argparse.SUPPRESS)
|
||||
|
||||
if _is_service_impacting_command(command):
|
||||
subparser.add_argument('--yes', action='store_true', help=f"Automatically confirm the action: {command}")
|
||||
callback = require_confirmation(callback, command.split("-")[0])
|
||||
# Are we a list command?
|
||||
if _does_command_need_no_wrap(callback):
|
||||
# then decorate it with wrapping data formatter functionality
|
||||
@ -578,3 +582,55 @@ def print_list(objs, fields, field_labels, formatters={}, sortby=0,
|
||||
return print_long_list(objs, fields, field_labels, formatters=formatters, sortby=sortby,
|
||||
reversesort=reversesort, no_wrap_fields=no_wrap_fields,
|
||||
no_paging=True, printer=printer)
|
||||
|
||||
|
||||
def input_with_timeout(prompt, timeout):
|
||||
def timeout_handler(signum, frame):
|
||||
raise TimeoutError
|
||||
|
||||
# Set the timeout handler
|
||||
signal.signal(signal.SIGALRM, timeout_handler)
|
||||
signal.alarm(timeout) # Set the alarm for the timeout
|
||||
|
||||
try:
|
||||
# Try to get input from the user
|
||||
result = input(prompt)
|
||||
signal.alarm(0) # Cancel the alarm if input is received in time
|
||||
return result
|
||||
except TimeoutError:
|
||||
print("\nError: No response received within the time limit.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def require_confirmation(func, target_object, timeout=10):
|
||||
"""Decorator that asks for user confirmation before running the function."""
|
||||
def wrapper(*args, **kwargs):
|
||||
YELLOW = '\033[93m'
|
||||
RESET = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
if hasattr(args[1], 'yes') and args[1].yes:
|
||||
# Skip confirmation if --yes was passed
|
||||
return func(*args, **kwargs)
|
||||
|
||||
confirmation = input_with_timeout(
|
||||
f"{BOLD}{YELLOW}WARNING: This is a high-risk operation that may cause a service interruption{RESET}\n"
|
||||
f"{BOLD}{YELLOW}Do you want to continue? (yes/No): {RESET}", timeout
|
||||
)
|
||||
if confirmation is None:
|
||||
print("\nError: No response received within the time limit.")
|
||||
return
|
||||
elif confirmation.lower() != 'yes':
|
||||
print("Operation cancelled by the user")
|
||||
sys.exit(1)
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
def _is_service_impacting_command(command):
|
||||
service_impacting_fm_commands = [
|
||||
"event-suppress",
|
||||
"event-unsuppress",
|
||||
"event-unsuppress-all"
|
||||
]
|
||||
|
||||
return command in service_impacting_fm_commands or 'delete' in command or 'remove' in command
|
||||
|
Loading…
x
Reference in New Issue
Block a user