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):
"""
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(
"-r",
"--restart",
"-r/-R",
"--restart/--no-restart",
"restart_flag",
is_flag=True,
default=False,
help="Immediately restart VM to apply changes.",
default=None,
show_default=False,
help="Immediately restart VM to apply changes or do not restart VM, or prompt if unspecified.",
)
@wraps(function)
def confirm_action(*args, **kwargs):
confirm_action = True
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
restart_state = kwargs.get("restart_flag", None)
if confirm_action:
if restart_state is None:
# Neither "--restart" or "--no-restart" was passed: prompt for restart or restart if "--unsafe"
try:
click.confirm(
f"Restart VM {kwargs.get('domain')} to apply changes",
@ -202,6 +194,12 @@ def restart_opt(function):
except Exception:
echo(CLI_CONFIG, "Changes will be applied on next VM start/restart.")
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)
@ -5550,7 +5548,7 @@ def cli_connection_detail(
envvar="PVC_UNSAFE",
is_flag=True,
default=False,
help='Allow unsafe operations without confirmation/"--yes" argument.',
help='Perform unsafe operations without confirmation/"--yes" argument.',
)
@click.option(
"--colour",