Commit 25bd815c authored by René Nussbaumer's avatar René Nussbaumer
Browse files

Refactor _ConfirmOperation from gnt_instance.py to cli.py



This will allow us to use this functionality also in other cli-tools
like gnt-node power where we've to operate on multiple nodes.
Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent a167575e
......@@ -161,6 +161,7 @@ __all__ = [
"VG_NAME_OPT",
"YES_DOIT_OPT",
# Generic functions for CLI programs
"ConfirmOperation",
"GenericMain",
"GenericInstanceCreate",
"GenericList",
......@@ -2974,3 +2975,42 @@ def FormatParameterDict(buf, param_dict, actual, level=1):
for key in sorted(actual):
val = param_dict.get(key, "default (%s)" % actual[key])
buf.write("%s- %s: %s\n" % (indent, key, val))
def ConfirmOperation(names, list_type, text, extra=""):
"""Ask the user to confirm an operation on a list of list_type.
This function is used to request confirmation for doing an operation
on a given list of list_type.
@type names: list
@param names: the list of names that we display when
we ask for confirmation
@type list_type: str
@param list_type: Human readable name for elements in the list (e.g. nodes)
@type text: str
@param text: the operation that the user should confirm
@rtype: boolean
@return: True or False depending on user's confirmation.
"""
count = len(names)
msg = ("The %s will operate on %d %s.\n%s"
"Do you want to continue?" % (text, count, list_type, extra))
affected = (("\nAffected %s:\n" % list_type) +
"\n".join([" %s" % name for name in names]))
choices = [("y", True, "Yes, execute the %s" % text),
("n", False, "No, abort the %s" % text)]
if count > 20:
choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
question = msg
else:
question = msg + affected
choice = AskUser(question, choices)
if choice == "v":
choices.pop(1)
choice = AskUser(msg + affected, choices)
return choice
......@@ -142,44 +142,6 @@ def _ExpandMultiNames(mode, names, client=None):
return inames
def _ConfirmOperation(inames, text, extra=""):
"""Ask the user to confirm an operation on a list of instances.
This function is used to request confirmation for doing an operation
on a given list of instances.
@type inames: list
@param inames: the list of names that we display when
we ask for confirmation
@type text: str
@param text: the operation that the user should confirm
(e.g. I{shutdown} or I{startup})
@rtype: boolean
@return: True or False depending on user's confirmation.
"""
count = len(inames)
msg = ("The %s will operate on %d instances.\n%s"
"Do you want to continue?" % (text, count, extra))
affected = ("\nAffected instances:\n" +
"\n".join([" %s" % name for name in inames]))
choices = [('y', True, 'Yes, execute the %s' % text),
('n', False, 'No, abort the %s' % text)]
if count > 20:
choices.insert(1, ('v', 'v', 'View the list of affected instances'))
ask = msg
else:
ask = msg + affected
choice = AskUser(ask, choices)
if choice == 'v':
choices.pop(1)
choice = AskUser(msg + affected, choices)
return choice
def _EnsureInstancesExist(client, names):
"""Check for and ensure the given instance names exist.
......@@ -221,7 +183,7 @@ def GenericManyOps(operation, fn):
" any instances", errors.ECODE_INVAL)
multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
if not (opts.force_multi or not multi_on
or _ConfirmOperation(inames, operation)):
or ConfirmOperation(inames, "instances", operation)):
return 1
jex = JobExecutor(verbose=multi_on, cl=cl, opts=opts)
for name in inames:
......@@ -491,7 +453,7 @@ def ReinstallInstance(opts, args):
if multi_on:
warn_msg = "Note: this will remove *all* data for the below instances!\n"
if not (opts.force_multi or
_ConfirmOperation(inames, "reinstall", extra=warn_msg)):
ConfirmOperation(inames, "instances", "reinstall", extra=warn_msg)):
return 1
else:
if not (opts.force or opts.force_multi):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment