Revamp behaviour of VM "--restart" options

Previously, either "--restart" was specified or a prompt was given, with
the prompt being ignored with "--unsafe" in favour of a reboot. This
failed to provide an explicit way to prevent VM restarts with these
commands, which might be desired in some non-interactive situations, and
the interaction of "--unsafe" with this option was an undesired bug.

This is now a complete binary flag with --restart and --no-restart
versions, while still defaulting to a prompt if neither is specified.
This allows full non-interactive control of this option.
This commit is contained in:
Joshua Boniface 2023-08-17 22:06:27 -04:00
parent e298d10561
commit d769071799
1 changed files with 16 additions and 18 deletions

View File

@ -166,32 +166,24 @@ def connection_req(function):
def restart_opt(function): def restart_opt(function):
""" """
Click Option Decorator: Click Option Decorator:
Wraps a Click command which requires confirm_flag or unsafe option or asks for VM restart confirmation Wraps a Click command which requires a VM domain restart, to provide options for/against restart or prompt
""" """
@click.option( @click.option(
"-r", "-r/-R",
"--restart", "--restart/--no-restart",
"restart_flag", "restart_flag",
is_flag=True, is_flag=True,
default=False, default=None,
help="Immediately restart VM to apply changes.", show_default=False,
help="Immediately restart VM to apply changes or do not restart VM, or prompt if unspecified.",
) )
@wraps(function) @wraps(function)
def confirm_action(*args, **kwargs): def confirm_action(*args, **kwargs):
confirm_action = True restart_state = kwargs.get("restart_flag", None)
if "restart_flag" in kwargs:
if not kwargs.get("restart_flag", False):
if not CLI_CONFIG.get("unsafe", False):
confirm_action = True
else:
confirm_action = False
else:
confirm_action = False
else:
confirm_action = False
if confirm_action: if restart_state is None:
# Neither "--restart" or "--no-restart" was passed: prompt for restart or restart if "--unsafe"
try: try:
click.confirm( click.confirm(
f"Restart VM {kwargs.get('domain')} to apply changes", f"Restart VM {kwargs.get('domain')} to apply changes",
@ -202,6 +194,12 @@ def restart_opt(function):
except Exception: except Exception:
echo(CLI_CONFIG, "Changes will be applied on next VM start/restart.") echo(CLI_CONFIG, "Changes will be applied on next VM start/restart.")
kwargs["restart_flag"] = False kwargs["restart_flag"] = False
elif restart_state is True:
# "--restart" was passed: allow restart without confirming
kwargs["restart_flag"] = True
elif restart_state is False:
# "--no-restart" was passed: skip confirming and skip restart
kwargs["restart_flag"] = False
return function(*args, **kwargs) return function(*args, **kwargs)
@ -5550,7 +5548,7 @@ def cli_connection_detail(
envvar="PVC_UNSAFE", envvar="PVC_UNSAFE",
is_flag=True, is_flag=True,
default=False, default=False,
help='Allow unsafe operations without confirmation/"--yes" argument.', help='Perform unsafe operations without confirmation/"--yes" argument.',
) )
@click.option( @click.option(
"--colour", "--colour",