Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 2c0af7da

History | View | Annotate | Download (109.2 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Module dealing with command line parsing"""
23

    
24

    
25
import sys
26
import textwrap
27
import os.path
28
import time
29
import logging
30
import errno
31
import itertools
32
import shlex
33
from cStringIO import StringIO
34

    
35
from ganeti import utils
36
from ganeti import errors
37
from ganeti import constants
38
from ganeti import opcodes
39
from ganeti import luxi
40
from ganeti import ssconf
41
from ganeti import rpc
42
from ganeti import ssh
43
from ganeti import compat
44
from ganeti import netutils
45
from ganeti import qlang
46

    
47
from optparse import (OptionParser, TitledHelpFormatter,
48
                      Option, OptionValueError)
49

    
50

    
51
__all__ = [
52
  # Command line options
53
  "ADD_UIDS_OPT",
54
  "ALLOCATABLE_OPT",
55
  "ALLOC_POLICY_OPT",
56
  "ALL_OPT",
57
  "ALLOW_FAILOVER_OPT",
58
  "AUTO_PROMOTE_OPT",
59
  "AUTO_REPLACE_OPT",
60
  "BACKEND_OPT",
61
  "BLK_OS_OPT",
62
  "CAPAB_MASTER_OPT",
63
  "CAPAB_VM_OPT",
64
  "CLEANUP_OPT",
65
  "CLUSTER_DOMAIN_SECRET_OPT",
66
  "CONFIRM_OPT",
67
  "CP_SIZE_OPT",
68
  "DEBUG_OPT",
69
  "DEBUG_SIMERR_OPT",
70
  "DISKIDX_OPT",
71
  "DISK_OPT",
72
  "DISK_PARAMS_OPT",
73
  "DISK_TEMPLATE_OPT",
74
  "DRAINED_OPT",
75
  "DRY_RUN_OPT",
76
  "DRBD_HELPER_OPT",
77
  "DST_NODE_OPT",
78
  "EARLY_RELEASE_OPT",
79
  "ENABLED_HV_OPT",
80
  "ERROR_CODES_OPT",
81
  "FIELDS_OPT",
82
  "FILESTORE_DIR_OPT",
83
  "FILESTORE_DRIVER_OPT",
84
  "FORCE_FILTER_OPT",
85
  "FORCE_OPT",
86
  "FORCE_VARIANT_OPT",
87
  "GLOBAL_FILEDIR_OPT",
88
  "HID_OS_OPT",
89
  "GLOBAL_SHARED_FILEDIR_OPT",
90
  "HVLIST_OPT",
91
  "HVOPTS_OPT",
92
  "HYPERVISOR_OPT",
93
  "IALLOCATOR_OPT",
94
  "DEFAULT_IALLOCATOR_OPT",
95
  "IDENTIFY_DEFAULTS_OPT",
96
  "IGNORE_CONSIST_OPT",
97
  "IGNORE_ERRORS_OPT",
98
  "IGNORE_FAILURES_OPT",
99
  "IGNORE_OFFLINE_OPT",
100
  "IGNORE_REMOVE_FAILURES_OPT",
101
  "IGNORE_SECONDARIES_OPT",
102
  "IGNORE_SIZE_OPT",
103
  "INTERVAL_OPT",
104
  "MAC_PREFIX_OPT",
105
  "MAINTAIN_NODE_HEALTH_OPT",
106
  "MASTER_NETDEV_OPT",
107
  "MASTER_NETMASK_OPT",
108
  "MC_OPT",
109
  "MIGRATION_MODE_OPT",
110
  "NET_OPT",
111
  "NEW_CLUSTER_CERT_OPT",
112
  "NEW_CLUSTER_DOMAIN_SECRET_OPT",
113
  "NEW_CONFD_HMAC_KEY_OPT",
114
  "NEW_RAPI_CERT_OPT",
115
  "NEW_SECONDARY_OPT",
116
  "NEW_SPICE_CERT_OPT",
117
  "NIC_PARAMS_OPT",
118
  "NODE_FORCE_JOIN_OPT",
119
  "NODE_LIST_OPT",
120
  "NODE_PLACEMENT_OPT",
121
  "NODEGROUP_OPT",
122
  "NODE_PARAMS_OPT",
123
  "NODE_POWERED_OPT",
124
  "NODRBD_STORAGE_OPT",
125
  "NOHDR_OPT",
126
  "NOIPCHECK_OPT",
127
  "NO_INSTALL_OPT",
128
  "NONAMECHECK_OPT",
129
  "NOLVM_STORAGE_OPT",
130
  "NOMODIFY_ETCHOSTS_OPT",
131
  "NOMODIFY_SSH_SETUP_OPT",
132
  "NONICS_OPT",
133
  "NONLIVE_OPT",
134
  "NONPLUS1_OPT",
135
  "NOSHUTDOWN_OPT",
136
  "NOSTART_OPT",
137
  "NOSSH_KEYCHECK_OPT",
138
  "NOVOTING_OPT",
139
  "NO_REMEMBER_OPT",
140
  "NWSYNC_OPT",
141
  "OFFLINE_INST_OPT",
142
  "ONLINE_INST_OPT",
143
  "ON_PRIMARY_OPT",
144
  "ON_SECONDARY_OPT",
145
  "OFFLINE_OPT",
146
  "OSPARAMS_OPT",
147
  "OS_OPT",
148
  "OS_SIZE_OPT",
149
  "OOB_TIMEOUT_OPT",
150
  "POWER_DELAY_OPT",
151
  "PREALLOC_WIPE_DISKS_OPT",
152
  "PRIMARY_IP_VERSION_OPT",
153
  "PRIMARY_ONLY_OPT",
154
  "PRIORITY_OPT",
155
  "RAPI_CERT_OPT",
156
  "READD_OPT",
157
  "REBOOT_TYPE_OPT",
158
  "REMOVE_INSTANCE_OPT",
159
  "REMOVE_UIDS_OPT",
160
  "RESERVED_LVS_OPT",
161
  "RUNTIME_MEM_OPT",
162
  "ROMAN_OPT",
163
  "SECONDARY_IP_OPT",
164
  "SECONDARY_ONLY_OPT",
165
  "SELECT_OS_OPT",
166
  "SEP_OPT",
167
  "SHOWCMD_OPT",
168
  "SHUTDOWN_TIMEOUT_OPT",
169
  "SINGLE_NODE_OPT",
170
  "SPECS_CPU_COUNT_OPT",
171
  "SPECS_DISK_COUNT_OPT",
172
  "SPECS_DISK_SIZE_OPT",
173
  "SPECS_MEM_SIZE_OPT",
174
  "SPECS_NIC_COUNT_OPT",
175
  "SPECS_DISK_TEMPLATES",
176
  "SPICE_CACERT_OPT",
177
  "SPICE_CERT_OPT",
178
  "SRC_DIR_OPT",
179
  "SRC_NODE_OPT",
180
  "SUBMIT_OPT",
181
  "STARTUP_PAUSED_OPT",
182
  "STATIC_OPT",
183
  "SYNC_OPT",
184
  "TAG_ADD_OPT",
185
  "TAG_SRC_OPT",
186
  "TIMEOUT_OPT",
187
  "TO_GROUP_OPT",
188
  "UIDPOOL_OPT",
189
  "USEUNITS_OPT",
190
  "USE_EXTERNAL_MIP_SCRIPT",
191
  "USE_REPL_NET_OPT",
192
  "VERBOSE_OPT",
193
  "VG_NAME_OPT",
194
  "YES_DOIT_OPT",
195
  "DISK_STATE_OPT",
196
  "HV_STATE_OPT",
197
  "IGNORE_IPOLICY_OPT",
198
  "INSTANCE_POLICY_OPTS",
199
  # Generic functions for CLI programs
200
  "ConfirmOperation",
201
  "GenericMain",
202
  "GenericInstanceCreate",
203
  "GenericList",
204
  "GenericListFields",
205
  "GetClient",
206
  "GetOnlineNodes",
207
  "JobExecutor",
208
  "JobSubmittedException",
209
  "ParseTimespec",
210
  "RunWhileClusterStopped",
211
  "SubmitOpCode",
212
  "SubmitOrSend",
213
  "UsesRPC",
214
  # Formatting functions
215
  "ToStderr", "ToStdout",
216
  "FormatError",
217
  "FormatQueryResult",
218
  "FormatParameterDict",
219
  "GenerateTable",
220
  "AskUser",
221
  "FormatTimestamp",
222
  "FormatLogMessage",
223
  # Tags functions
224
  "ListTags",
225
  "AddTags",
226
  "RemoveTags",
227
  # command line options support infrastructure
228
  "ARGS_MANY_INSTANCES",
229
  "ARGS_MANY_NODES",
230
  "ARGS_MANY_GROUPS",
231
  "ARGS_NONE",
232
  "ARGS_ONE_INSTANCE",
233
  "ARGS_ONE_NODE",
234
  "ARGS_ONE_GROUP",
235
  "ARGS_ONE_OS",
236
  "ArgChoice",
237
  "ArgCommand",
238
  "ArgFile",
239
  "ArgGroup",
240
  "ArgHost",
241
  "ArgInstance",
242
  "ArgJobId",
243
  "ArgNode",
244
  "ArgOs",
245
  "ArgSuggest",
246
  "ArgUnknown",
247
  "OPT_COMPL_INST_ADD_NODES",
248
  "OPT_COMPL_MANY_NODES",
249
  "OPT_COMPL_ONE_IALLOCATOR",
250
  "OPT_COMPL_ONE_INSTANCE",
251
  "OPT_COMPL_ONE_NODE",
252
  "OPT_COMPL_ONE_NODEGROUP",
253
  "OPT_COMPL_ONE_OS",
254
  "cli_option",
255
  "SplitNodeOption",
256
  "CalculateOSNames",
257
  "ParseFields",
258
  "COMMON_CREATE_OPTS",
259
  ]
260

    
261
NO_PREFIX = "no_"
262
UN_PREFIX = "-"
263

    
264
#: Priorities (sorted)
265
_PRIORITY_NAMES = [
266
  ("low", constants.OP_PRIO_LOW),
267
  ("normal", constants.OP_PRIO_NORMAL),
268
  ("high", constants.OP_PRIO_HIGH),
269
  ]
270

    
271
#: Priority dictionary for easier lookup
272
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
273
# we migrate to Python 2.6
274
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
275

    
276
# Query result status for clients
277
(QR_NORMAL,
278
 QR_UNKNOWN,
279
 QR_INCOMPLETE) = range(3)
280

    
281
#: Maximum batch size for ChooseJob
282
_CHOOSE_BATCH = 25
283

    
284

    
285
class _Argument:
286
  def __init__(self, min=0, max=None): # pylint: disable=W0622
287
    self.min = min
288
    self.max = max
289

    
290
  def __repr__(self):
291
    return ("<%s min=%s max=%s>" %
292
            (self.__class__.__name__, self.min, self.max))
293

    
294

    
295
class ArgSuggest(_Argument):
296
  """Suggesting argument.
297

298
  Value can be any of the ones passed to the constructor.
299

300
  """
301
  # pylint: disable=W0622
302
  def __init__(self, min=0, max=None, choices=None):
303
    _Argument.__init__(self, min=min, max=max)
304
    self.choices = choices
305

    
306
  def __repr__(self):
307
    return ("<%s min=%s max=%s choices=%r>" %
308
            (self.__class__.__name__, self.min, self.max, self.choices))
309

    
310

    
311
class ArgChoice(ArgSuggest):
312
  """Choice argument.
313

314
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
315
  but value must be one of the choices.
316

317
  """
318

    
319

    
320
class ArgUnknown(_Argument):
321
  """Unknown argument to program (e.g. determined at runtime).
322

323
  """
324

    
325

    
326
class ArgInstance(_Argument):
327
  """Instances argument.
328

329
  """
330

    
331

    
332
class ArgNode(_Argument):
333
  """Node argument.
334

335
  """
336

    
337

    
338
class ArgGroup(_Argument):
339
  """Node group argument.
340

341
  """
342

    
343

    
344
class ArgJobId(_Argument):
345
  """Job ID argument.
346

347
  """
348

    
349

    
350
class ArgFile(_Argument):
351
  """File path argument.
352

353
  """
354

    
355

    
356
class ArgCommand(_Argument):
357
  """Command argument.
358

359
  """
360

    
361

    
362
class ArgHost(_Argument):
363
  """Host argument.
364

365
  """
366

    
367

    
368
class ArgOs(_Argument):
369
  """OS argument.
370

371
  """
372

    
373

    
374
ARGS_NONE = []
375
ARGS_MANY_INSTANCES = [ArgInstance()]
376
ARGS_MANY_NODES = [ArgNode()]
377
ARGS_MANY_GROUPS = [ArgGroup()]
378
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
379
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
380
# TODO
381
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
382
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
383

    
384

    
385
def _ExtractTagsObject(opts, args):
386
  """Extract the tag type object.
387

388
  Note that this function will modify its args parameter.
389

390
  """
391
  if not hasattr(opts, "tag_type"):
392
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
393
  kind = opts.tag_type
394
  if kind == constants.TAG_CLUSTER:
395
    retval = kind, kind
396
  elif kind in (constants.TAG_NODEGROUP,
397
                constants.TAG_NODE,
398
                constants.TAG_INSTANCE):
399
    if not args:
400
      raise errors.OpPrereqError("no arguments passed to the command")
401
    name = args.pop(0)
402
    retval = kind, name
403
  else:
404
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
405
  return retval
406

    
407

    
408
def _ExtendTags(opts, args):
409
  """Extend the args if a source file has been given.
410

411
  This function will extend the tags with the contents of the file
412
  passed in the 'tags_source' attribute of the opts parameter. A file
413
  named '-' will be replaced by stdin.
414

415
  """
416
  fname = opts.tags_source
417
  if fname is None:
418
    return
419
  if fname == "-":
420
    new_fh = sys.stdin
421
  else:
422
    new_fh = open(fname, "r")
423
  new_data = []
424
  try:
425
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
426
    # because of python bug 1633941
427
    while True:
428
      line = new_fh.readline()
429
      if not line:
430
        break
431
      new_data.append(line.strip())
432
  finally:
433
    new_fh.close()
434
  args.extend(new_data)
435

    
436

    
437
def ListTags(opts, args):
438
  """List the tags on a given object.
439

440
  This is a generic implementation that knows how to deal with all
441
  three cases of tag objects (cluster, node, instance). The opts
442
  argument is expected to contain a tag_type field denoting what
443
  object type we work on.
444

445
  """
446
  kind, name = _ExtractTagsObject(opts, args)
447
  cl = GetClient()
448
  result = cl.QueryTags(kind, name)
449
  result = list(result)
450
  result.sort()
451
  for tag in result:
452
    ToStdout(tag)
453

    
454

    
455
def AddTags(opts, args):
456
  """Add tags on a given object.
457

458
  This is a generic implementation that knows how to deal with all
459
  three cases of tag objects (cluster, node, instance). The opts
460
  argument is expected to contain a tag_type field denoting what
461
  object type we work on.
462

463
  """
464
  kind, name = _ExtractTagsObject(opts, args)
465
  _ExtendTags(opts, args)
466
  if not args:
467
    raise errors.OpPrereqError("No tags to be added")
468
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
469
  SubmitOpCode(op, opts=opts)
470

    
471

    
472
def RemoveTags(opts, args):
473
  """Remove tags from a given object.
474

475
  This is a generic implementation that knows how to deal with all
476
  three cases of tag objects (cluster, node, instance). The opts
477
  argument is expected to contain a tag_type field denoting what
478
  object type we work on.
479

480
  """
481
  kind, name = _ExtractTagsObject(opts, args)
482
  _ExtendTags(opts, args)
483
  if not args:
484
    raise errors.OpPrereqError("No tags to be removed")
485
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
486
  SubmitOpCode(op, opts=opts)
487

    
488

    
489
def check_unit(option, opt, value): # pylint: disable=W0613
490
  """OptParsers custom converter for units.
491

492
  """
493
  try:
494
    return utils.ParseUnit(value)
495
  except errors.UnitParseError, err:
496
    raise OptionValueError("option %s: %s" % (opt, err))
497

    
498

    
499
def _SplitKeyVal(opt, data):
500
  """Convert a KeyVal string into a dict.
501

502
  This function will convert a key=val[,...] string into a dict. Empty
503
  values will be converted specially: keys which have the prefix 'no_'
504
  will have the value=False and the prefix stripped, the others will
505
  have value=True.
506

507
  @type opt: string
508
  @param opt: a string holding the option name for which we process the
509
      data, used in building error messages
510
  @type data: string
511
  @param data: a string of the format key=val,key=val,...
512
  @rtype: dict
513
  @return: {key=val, key=val}
514
  @raises errors.ParameterError: if there are duplicate keys
515

516
  """
517
  kv_dict = {}
518
  if data:
519
    for elem in utils.UnescapeAndSplit(data, sep=","):
520
      if "=" in elem:
521
        key, val = elem.split("=", 1)
522
      else:
523
        if elem.startswith(NO_PREFIX):
524
          key, val = elem[len(NO_PREFIX):], False
525
        elif elem.startswith(UN_PREFIX):
526
          key, val = elem[len(UN_PREFIX):], None
527
        else:
528
          key, val = elem, True
529
      if key in kv_dict:
530
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
531
                                    (key, opt))
532
      kv_dict[key] = val
533
  return kv_dict
534

    
535

    
536
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
537
  """Custom parser for ident:key=val,key=val options.
538

539
  This will store the parsed values as a tuple (ident, {key: val}). As such,
540
  multiple uses of this option via action=append is possible.
541

542
  """
543
  if ":" not in value:
544
    ident, rest = value, ""
545
  else:
546
    ident, rest = value.split(":", 1)
547

    
548
  if ident.startswith(NO_PREFIX):
549
    if rest:
550
      msg = "Cannot pass options when removing parameter groups: %s" % value
551
      raise errors.ParameterError(msg)
552
    retval = (ident[len(NO_PREFIX):], False)
553
  elif ident.startswith(UN_PREFIX):
554
    if rest:
555
      msg = "Cannot pass options when removing parameter groups: %s" % value
556
      raise errors.ParameterError(msg)
557
    retval = (ident[len(UN_PREFIX):], None)
558
  else:
559
    kv_dict = _SplitKeyVal(opt, rest)
560
    retval = (ident, kv_dict)
561
  return retval
562

    
563

    
564
def check_key_val(option, opt, value):  # pylint: disable=W0613
565
  """Custom parser class for key=val,key=val options.
566

567
  This will store the parsed values as a dict {key: val}.
568

569
  """
570
  return _SplitKeyVal(opt, value)
571

    
572

    
573
def check_bool(option, opt, value): # pylint: disable=W0613
574
  """Custom parser for yes/no options.
575

576
  This will store the parsed value as either True or False.
577

578
  """
579
  value = value.lower()
580
  if value == constants.VALUE_FALSE or value == "no":
581
    return False
582
  elif value == constants.VALUE_TRUE or value == "yes":
583
    return True
584
  else:
585
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
586

    
587

    
588
def check_list(option, opt, value): # pylint: disable=W0613
589
  """Custom parser for comma-separated lists.
590

591
  """
592
  # we have to make this explicit check since "".split(",") is [""],
593
  # not an empty list :(
594
  if not value:
595
    return []
596
  else:
597
    return utils.UnescapeAndSplit(value)
598

    
599

    
600
# completion_suggestion is normally a list. Using numeric values not evaluating
601
# to False for dynamic completion.
602
(OPT_COMPL_MANY_NODES,
603
 OPT_COMPL_ONE_NODE,
604
 OPT_COMPL_ONE_INSTANCE,
605
 OPT_COMPL_ONE_OS,
606
 OPT_COMPL_ONE_IALLOCATOR,
607
 OPT_COMPL_INST_ADD_NODES,
608
 OPT_COMPL_ONE_NODEGROUP) = range(100, 107)
609

    
610
OPT_COMPL_ALL = frozenset([
611
  OPT_COMPL_MANY_NODES,
612
  OPT_COMPL_ONE_NODE,
613
  OPT_COMPL_ONE_INSTANCE,
614
  OPT_COMPL_ONE_OS,
615
  OPT_COMPL_ONE_IALLOCATOR,
616
  OPT_COMPL_INST_ADD_NODES,
617
  OPT_COMPL_ONE_NODEGROUP,
618
  ])
619

    
620

    
621
class CliOption(Option):
622
  """Custom option class for optparse.
623

624
  """
625
  ATTRS = Option.ATTRS + [
626
    "completion_suggest",
627
    ]
628
  TYPES = Option.TYPES + (
629
    "identkeyval",
630
    "keyval",
631
    "unit",
632
    "bool",
633
    "list",
634
    )
635
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
636
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
637
  TYPE_CHECKER["keyval"] = check_key_val
638
  TYPE_CHECKER["unit"] = check_unit
639
  TYPE_CHECKER["bool"] = check_bool
640
  TYPE_CHECKER["list"] = check_list
641

    
642

    
643
# optparse.py sets make_option, so we do it for our own option class, too
644
cli_option = CliOption
645

    
646

    
647
_YORNO = "yes|no"
648

    
649
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
650
                       help="Increase debugging level")
651

    
652
NOHDR_OPT = cli_option("--no-headers", default=False,
653
                       action="store_true", dest="no_headers",
654
                       help="Don't display column headers")
655

    
656
SEP_OPT = cli_option("--separator", default=None,
657
                     action="store", dest="separator",
658
                     help=("Separator between output fields"
659
                           " (defaults to one space)"))
660

    
661
USEUNITS_OPT = cli_option("--units", default=None,
662
                          dest="units", choices=("h", "m", "g", "t"),
663
                          help="Specify units for output (one of h/m/g/t)")
664

    
665
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
666
                        type="string", metavar="FIELDS",
667
                        help="Comma separated list of output fields")
668

    
669
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
670
                       default=False, help="Force the operation")
671

    
672
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
673
                         default=False, help="Do not require confirmation")
674

    
675
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
676
                                  action="store_true", default=False,
677
                                  help=("Ignore offline nodes and do as much"
678
                                        " as possible"))
679

    
680
TAG_ADD_OPT = cli_option("--tags", dest="tags",
681
                         default=None, help="Comma-separated list of instance"
682
                                            " tags")
683

    
684
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
685
                         default=None, help="File with tag names")
686

    
687
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
688
                        default=False, action="store_true",
689
                        help=("Submit the job and return the job ID, but"
690
                              " don't wait for the job to finish"))
691

    
692
SYNC_OPT = cli_option("--sync", dest="do_locking",
693
                      default=False, action="store_true",
694
                      help=("Grab locks while doing the queries"
695
                            " in order to ensure more consistent results"))
696

    
697
DRY_RUN_OPT = cli_option("--dry-run", default=False,
698
                         action="store_true",
699
                         help=("Do not execute the operation, just run the"
700
                               " check steps and verify it it could be"
701
                               " executed"))
702

    
703
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
704
                         action="store_true",
705
                         help="Increase the verbosity of the operation")
706

    
707
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
708
                              action="store_true", dest="simulate_errors",
709
                              help="Debugging option that makes the operation"
710
                              " treat most runtime checks as failed")
711

    
712
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
713
                        default=True, action="store_false",
714
                        help="Don't wait for sync (DANGEROUS!)")
715

    
716
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
717
                             action="store_true", default=False,
718
                             help="Enable offline instance")
719

    
720
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
721
                              action="store_true", default=False,
722
                              help="Disable down instance")
723

    
724
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
725
                               help=("Custom disk setup (%s)" %
726
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
727
                               default=None, metavar="TEMPL",
728
                               choices=list(constants.DISK_TEMPLATES))
729

    
730
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
731
                        help="Do not create any network cards for"
732
                        " the instance")
733

    
734
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
735
                               help="Relative path under default cluster-wide"
736
                               " file storage dir to store file-based disks",
737
                               default=None, metavar="<DIR>")
738

    
739
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
740
                                  help="Driver to use for image files",
741
                                  default="loop", metavar="<DRIVER>",
742
                                  choices=list(constants.FILE_DRIVER))
743

    
744
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
745
                            help="Select nodes for the instance automatically"
746
                            " using the <NAME> iallocator plugin",
747
                            default=None, type="string",
748
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
749

    
750
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
751
                            metavar="<NAME>",
752
                            help="Set the default instance allocator plugin",
753
                            default=None, type="string",
754
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
755

    
756
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
757
                    metavar="<os>",
758
                    completion_suggest=OPT_COMPL_ONE_OS)
759

    
760
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
761
                         type="keyval", default={},
762
                         help="OS parameters")
763

    
764
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
765
                               action="store_true", default=False,
766
                               help="Force an unknown variant")
767

    
768
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
769
                            action="store_true", default=False,
770
                            help="Do not install the OS (will"
771
                            " enable no-start)")
772

    
773
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
774
                         type="keyval", default={},
775
                         help="Backend parameters")
776

    
777
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
778
                        default={}, dest="hvparams",
779
                        help="Hypervisor parameters")
780

    
781
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
782
                             help="Disk template parameters, in the format"
783
                             " template:option=value,option=value,...",
784
                             type="identkeyval", action="append", default=[])
785

    
786
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
787
                                 type="keyval", default={},
788
                                 help="Memory count specs: min, max, std"
789
                                 " (in MB)")
790

    
791
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
792
                                 type="keyval", default={},
793
                                 help="CPU count specs: min, max, std")
794

    
795
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
796
                                  dest="ispecs_disk_count",
797
                                  type="keyval", default={},
798
                                  help="Disk count specs: min, max, std")
799

    
800
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
801
                                 type="keyval", default={},
802
                                 help="Disk size specs: min, max, std (in MB)")
803

    
804
SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
805
                                 type="keyval", default={},
806
                                 help="NIC count specs: min, max, std")
807

    
808
SPECS_DISK_TEMPLATES = cli_option("--specs-disk-templates",
809
                                  dest="ispecs_disk_templates",
810
                                  type="list", default=None,
811
                                  help="Comma-separated list of"
812
                                  " enabled disk templates")
813

    
814
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
815
                            help="Hypervisor and hypervisor options, in the"
816
                            " format hypervisor:option=value,option=value,...",
817
                            default=None, type="identkeyval")
818

    
819
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
820
                        help="Hypervisor and hypervisor options, in the"
821
                        " format hypervisor:option=value,option=value,...",
822
                        default=[], action="append", type="identkeyval")
823

    
824
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
825
                           action="store_false",
826
                           help="Don't check that the instance's IP"
827
                           " is alive")
828

    
829
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
830
                             default=True, action="store_false",
831
                             help="Don't check that the instance's name"
832
                             " is resolvable")
833

    
834
NET_OPT = cli_option("--net",
835
                     help="NIC parameters", default=[],
836
                     dest="nics", action="append", type="identkeyval")
837

    
838
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
839
                      dest="disks", action="append", type="identkeyval")
840

    
841
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
842
                         help="Comma-separated list of disks"
843
                         " indices to act on (e.g. 0,2) (optional,"
844
                         " defaults to all disks)")
845

    
846
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
847
                         help="Enforces a single-disk configuration using the"
848
                         " given disk size, in MiB unless a suffix is used",
849
                         default=None, type="unit", metavar="<size>")
850

    
851
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
852
                                dest="ignore_consistency",
853
                                action="store_true", default=False,
854
                                help="Ignore the consistency of the disks on"
855
                                " the secondary")
856

    
857
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
858
                                dest="allow_failover",
859
                                action="store_true", default=False,
860
                                help="If migration is not possible fallback to"
861
                                     " failover")
862

    
863
NONLIVE_OPT = cli_option("--non-live", dest="live",
864
                         default=True, action="store_false",
865
                         help="Do a non-live migration (this usually means"
866
                         " freeze the instance, save the state, transfer and"
867
                         " only then resume running on the secondary node)")
868

    
869
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
870
                                default=None,
871
                                choices=list(constants.HT_MIGRATION_MODES),
872
                                help="Override default migration mode (choose"
873
                                " either live or non-live")
874

    
875
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
876
                                help="Target node and optional secondary node",
877
                                metavar="<pnode>[:<snode>]",
878
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
879

    
880
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
881
                           action="append", metavar="<node>",
882
                           help="Use only this node (can be used multiple"
883
                           " times, if not given defaults to all nodes)",
884
                           completion_suggest=OPT_COMPL_ONE_NODE)
885

    
886
NODEGROUP_OPT_NAME = "--node-group"
887
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
888
                           dest="nodegroup",
889
                           help="Node group (name or uuid)",
890
                           metavar="<nodegroup>",
891
                           default=None, type="string",
892
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
893

    
894
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
895
                             metavar="<node>",
896
                             completion_suggest=OPT_COMPL_ONE_NODE)
897

    
898
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
899
                         action="store_false",
900
                         help="Don't start the instance after creation")
901

    
902
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
903
                         action="store_true", default=False,
904
                         help="Show command instead of executing it")
905

    
906
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
907
                         default=False, action="store_true",
908
                         help="Instead of performing the migration, try to"
909
                         " recover from a failed cleanup. This is safe"
910
                         " to run even if the instance is healthy, but it"
911
                         " will create extra replication traffic and "
912
                         " disrupt briefly the replication (like during the"
913
                         " migration")
914

    
915
STATIC_OPT = cli_option("-s", "--static", dest="static",
916
                        action="store_true", default=False,
917
                        help="Only show configuration data, not runtime data")
918

    
919
ALL_OPT = cli_option("--all", dest="show_all",
920
                     default=False, action="store_true",
921
                     help="Show info on all instances on the cluster."
922
                     " This can take a long time to run, use wisely")
923

    
924
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
925
                           action="store_true", default=False,
926
                           help="Interactive OS reinstall, lists available"
927
                           " OS templates for selection")
928

    
929
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
930
                                 action="store_true", default=False,
931
                                 help="Remove the instance from the cluster"
932
                                 " configuration even if there are failures"
933
                                 " during the removal process")
934

    
935
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
936
                                        dest="ignore_remove_failures",
937
                                        action="store_true", default=False,
938
                                        help="Remove the instance from the"
939
                                        " cluster configuration even if there"
940
                                        " are failures during the removal"
941
                                        " process")
942

    
943
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
944
                                 action="store_true", default=False,
945
                                 help="Remove the instance from the cluster")
946

    
947
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
948
                               help="Specifies the new node for the instance",
949
                               metavar="NODE", default=None,
950
                               completion_suggest=OPT_COMPL_ONE_NODE)
951

    
952
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
953
                               help="Specifies the new secondary node",
954
                               metavar="NODE", default=None,
955
                               completion_suggest=OPT_COMPL_ONE_NODE)
956

    
957
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
958
                            default=False, action="store_true",
959
                            help="Replace the disk(s) on the primary"
960
                                 " node (applies only to internally mirrored"
961
                                 " disk templates, e.g. %s)" %
962
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
963

    
964
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
965
                              default=False, action="store_true",
966
                              help="Replace the disk(s) on the secondary"
967
                                   " node (applies only to internally mirrored"
968
                                   " disk templates, e.g. %s)" %
969
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
970

    
971
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
972
                              default=False, action="store_true",
973
                              help="Lock all nodes and auto-promote as needed"
974
                              " to MC status")
975

    
976
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
977
                              default=False, action="store_true",
978
                              help="Automatically replace faulty disks"
979
                                   " (applies only to internally mirrored"
980
                                   " disk templates, e.g. %s)" %
981
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
982

    
983
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
984
                             default=False, action="store_true",
985
                             help="Ignore current recorded size"
986
                             " (useful for forcing activation when"
987
                             " the recorded size is wrong)")
988

    
989
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
990
                          metavar="<node>",
991
                          completion_suggest=OPT_COMPL_ONE_NODE)
992

    
993
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
994
                         metavar="<dir>")
995

    
996
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
997
                              help="Specify the secondary ip for the node",
998
                              metavar="ADDRESS", default=None)
999

    
1000
READD_OPT = cli_option("--readd", dest="readd",
1001
                       default=False, action="store_true",
1002
                       help="Readd old node after replacing it")
1003

    
1004
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1005
                                default=True, action="store_false",
1006
                                help="Disable SSH key fingerprint checking")
1007

    
1008
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1009
                                 default=False, action="store_true",
1010
                                 help="Force the joining of a node")
1011

    
1012
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1013
                    type="bool", default=None, metavar=_YORNO,
1014
                    help="Set the master_candidate flag on the node")
1015

    
1016
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1017
                         type="bool", default=None,
1018
                         help=("Set the offline flag on the node"
1019
                               " (cluster does not communicate with offline"
1020
                               " nodes)"))
1021

    
1022
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1023
                         type="bool", default=None,
1024
                         help=("Set the drained flag on the node"
1025
                               " (excluded from allocation operations)"))
1026

    
1027
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1028
                    type="bool", default=None, metavar=_YORNO,
1029
                    help="Set the master_capable flag on the node")
1030

    
1031
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1032
                    type="bool", default=None, metavar=_YORNO,
1033
                    help="Set the vm_capable flag on the node")
1034

    
1035
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1036
                             type="bool", default=None, metavar=_YORNO,
1037
                             help="Set the allocatable flag on a volume")
1038

    
1039
NOLVM_STORAGE_OPT = cli_option("--no-lvm-storage", dest="lvm_storage",
1040
                               help="Disable support for lvm based instances"
1041
                               " (cluster-wide)",
1042
                               action="store_false", default=True)
1043

    
1044
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1045
                            dest="enabled_hypervisors",
1046
                            help="Comma-separated list of hypervisors",
1047
                            type="string", default=None)
1048

    
1049
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1050
                            type="keyval", default={},
1051
                            help="NIC parameters")
1052

    
1053
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1054
                         dest="candidate_pool_size", type="int",
1055
                         help="Set the candidate pool size")
1056

    
1057
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1058
                         help=("Enables LVM and specifies the volume group"
1059
                               " name (cluster-wide) for disk allocation"
1060
                               " [%s]" % constants.DEFAULT_VG),
1061
                         metavar="VG", default=None)
1062

    
1063
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1064
                          help="Destroy cluster", action="store_true")
1065

    
1066
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1067
                          help="Skip node agreement check (dangerous)",
1068
                          action="store_true", default=False)
1069

    
1070
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1071
                            help="Specify the mac prefix for the instance IP"
1072
                            " addresses, in the format XX:XX:XX",
1073
                            metavar="PREFIX",
1074
                            default=None)
1075

    
1076
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1077
                               help="Specify the node interface (cluster-wide)"
1078
                               " on which the master IP address will be added"
1079
                               " (cluster init default: %s)" %
1080
                               constants.DEFAULT_BRIDGE,
1081
                               metavar="NETDEV",
1082
                               default=None)
1083

    
1084
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1085
                                help="Specify the netmask of the master IP",
1086
                                metavar="NETMASK",
1087
                                default=None)
1088

    
1089
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1090
                                dest="use_external_mip_script",
1091
                                help="Specify whether to run a user-provided"
1092
                                " script for the master IP address turnup and"
1093
                                " turndown operations",
1094
                                type="bool", metavar=_YORNO, default=None)
1095

    
1096
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1097
                                help="Specify the default directory (cluster-"
1098
                                "wide) for storing the file-based disks [%s]" %
1099
                                constants.DEFAULT_FILE_STORAGE_DIR,
1100
                                metavar="DIR",
1101
                                default=constants.DEFAULT_FILE_STORAGE_DIR)
1102

    
1103
GLOBAL_SHARED_FILEDIR_OPT = cli_option("--shared-file-storage-dir",
1104
                            dest="shared_file_storage_dir",
1105
                            help="Specify the default directory (cluster-"
1106
                            "wide) for storing the shared file-based"
1107
                            " disks [%s]" %
1108
                            constants.DEFAULT_SHARED_FILE_STORAGE_DIR,
1109
                            metavar="SHAREDDIR",
1110
                            default=constants.DEFAULT_SHARED_FILE_STORAGE_DIR)
1111

    
1112
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1113
                                   help="Don't modify /etc/hosts",
1114
                                   action="store_false", default=True)
1115

    
1116
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1117
                                    help="Don't initialize SSH keys",
1118
                                    action="store_false", default=True)
1119

    
1120
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1121
                             help="Enable parseable error messages",
1122
                             action="store_true", default=False)
1123

    
1124
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1125
                          help="Skip N+1 memory redundancy tests",
1126
                          action="store_true", default=False)
1127

    
1128
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1129
                             help="Type of reboot: soft/hard/full",
1130
                             default=constants.INSTANCE_REBOOT_HARD,
1131
                             metavar="<REBOOT>",
1132
                             choices=list(constants.REBOOT_TYPES))
1133

    
1134
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1135
                                    dest="ignore_secondaries",
1136
                                    default=False, action="store_true",
1137
                                    help="Ignore errors from secondaries")
1138

    
1139
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1140
                            action="store_false", default=True,
1141
                            help="Don't shutdown the instance (unsafe)")
1142

    
1143
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1144
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1145
                         help="Maximum time to wait")
1146

    
1147
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1148
                         dest="shutdown_timeout", type="int",
1149
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1150
                         help="Maximum time to wait for instance shutdown")
1151

    
1152
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1153
                          default=None,
1154
                          help=("Number of seconds between repetions of the"
1155
                                " command"))
1156

    
1157
EARLY_RELEASE_OPT = cli_option("--early-release",
1158
                               dest="early_release", default=False,
1159
                               action="store_true",
1160
                               help="Release the locks on the secondary"
1161
                               " node(s) early")
1162

    
1163
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1164
                                  dest="new_cluster_cert",
1165
                                  default=False, action="store_true",
1166
                                  help="Generate a new cluster certificate")
1167

    
1168
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1169
                           default=None,
1170
                           help="File containing new RAPI certificate")
1171

    
1172
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1173
                               default=None, action="store_true",
1174
                               help=("Generate a new self-signed RAPI"
1175
                                     " certificate"))
1176

    
1177
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1178
                           default=None,
1179
                           help="File containing new SPICE certificate")
1180

    
1181
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1182
                           default=None,
1183
                           help="File containing the certificate of the CA"
1184
                                " which signed the SPICE certificate")
1185

    
1186
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1187
                               dest="new_spice_cert", default=None,
1188
                               action="store_true",
1189
                               help=("Generate a new self-signed SPICE"
1190
                                     " certificate"))
1191

    
1192
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1193
                                    dest="new_confd_hmac_key",
1194
                                    default=False, action="store_true",
1195
                                    help=("Create a new HMAC key for %s" %
1196
                                          constants.CONFD))
1197

    
1198
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1199
                                       dest="cluster_domain_secret",
1200
                                       default=None,
1201
                                       help=("Load new new cluster domain"
1202
                                             " secret from file"))
1203

    
1204
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1205
                                           dest="new_cluster_domain_secret",
1206
                                           default=False, action="store_true",
1207
                                           help=("Create a new cluster domain"
1208
                                                 " secret"))
1209

    
1210
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1211
                              dest="use_replication_network",
1212
                              help="Whether to use the replication network"
1213
                              " for talking to the nodes",
1214
                              action="store_true", default=False)
1215

    
1216
MAINTAIN_NODE_HEALTH_OPT = \
1217
    cli_option("--maintain-node-health", dest="maintain_node_health",
1218
               metavar=_YORNO, default=None, type="bool",
1219
               help="Configure the cluster to automatically maintain node"
1220
               " health, by shutting down unknown instances, shutting down"
1221
               " unknown DRBD devices, etc.")
1222

    
1223
IDENTIFY_DEFAULTS_OPT = \
1224
    cli_option("--identify-defaults", dest="identify_defaults",
1225
               default=False, action="store_true",
1226
               help="Identify which saved instance parameters are equal to"
1227
               " the current cluster defaults and set them as such, instead"
1228
               " of marking them as overridden")
1229

    
1230
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1231
                         action="store", dest="uid_pool",
1232
                         help=("A list of user-ids or user-id"
1233
                               " ranges separated by commas"))
1234

    
1235
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1236
                          action="store", dest="add_uids",
1237
                          help=("A list of user-ids or user-id"
1238
                                " ranges separated by commas, to be"
1239
                                " added to the user-id pool"))
1240

    
1241
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1242
                             action="store", dest="remove_uids",
1243
                             help=("A list of user-ids or user-id"
1244
                                   " ranges separated by commas, to be"
1245
                                   " removed from the user-id pool"))
1246

    
1247
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1248
                             action="store", dest="reserved_lvs",
1249
                             help=("A comma-separated list of reserved"
1250
                                   " logical volumes names, that will be"
1251
                                   " ignored by cluster verify"))
1252

    
1253
ROMAN_OPT = cli_option("--roman",
1254
                       dest="roman_integers", default=False,
1255
                       action="store_true",
1256
                       help="Use roman numbers for positive integers")
1257

    
1258
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1259
                             action="store", default=None,
1260
                             help="Specifies usermode helper for DRBD")
1261

    
1262
NODRBD_STORAGE_OPT = cli_option("--no-drbd-storage", dest="drbd_storage",
1263
                                action="store_false", default=True,
1264
                                help="Disable support for DRBD")
1265

    
1266
PRIMARY_IP_VERSION_OPT = \
1267
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1268
               action="store", dest="primary_ip_version",
1269
               metavar="%d|%d" % (constants.IP4_VERSION,
1270
                                  constants.IP6_VERSION),
1271
               help="Cluster-wide IP version for primary IP")
1272

    
1273
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1274
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1275
                          choices=_PRIONAME_TO_VALUE.keys(),
1276
                          help="Priority for opcode processing")
1277

    
1278
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1279
                        type="bool", default=None, metavar=_YORNO,
1280
                        help="Sets the hidden flag on the OS")
1281

    
1282
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1283
                        type="bool", default=None, metavar=_YORNO,
1284
                        help="Sets the blacklisted flag on the OS")
1285

    
1286
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1287
                                     type="bool", metavar=_YORNO,
1288
                                     dest="prealloc_wipe_disks",
1289
                                     help=("Wipe disks prior to instance"
1290
                                           " creation"))
1291

    
1292
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1293
                             type="keyval", default=None,
1294
                             help="Node parameters")
1295

    
1296
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1297
                              action="store", metavar="POLICY", default=None,
1298
                              help="Allocation policy for the node group")
1299

    
1300
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1301
                              type="bool", metavar=_YORNO,
1302
                              dest="node_powered",
1303
                              help="Specify if the SoR for node is powered")
1304

    
1305
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1306
                         default=constants.OOB_TIMEOUT,
1307
                         help="Maximum time to wait for out-of-band helper")
1308

    
1309
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1310
                             default=constants.OOB_POWER_DELAY,
1311
                             help="Time in seconds to wait between power-ons")
1312

    
1313
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1314
                              action="store_true", default=False,
1315
                              help=("Whether command argument should be treated"
1316
                                    " as filter"))
1317

    
1318
NO_REMEMBER_OPT = cli_option("--no-remember",
1319
                             dest="no_remember",
1320
                             action="store_true", default=False,
1321
                             help="Perform but do not record the change"
1322
                             " in the configuration")
1323

    
1324
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1325
                              default=False, action="store_true",
1326
                              help="Evacuate primary instances only")
1327

    
1328
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1329
                                default=False, action="store_true",
1330
                                help="Evacuate secondary instances only"
1331
                                     " (applies only to internally mirrored"
1332
                                     " disk templates, e.g. %s)" %
1333
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1334

    
1335
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1336
                                action="store_true", default=False,
1337
                                help="Pause instance at startup")
1338

    
1339
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1340
                          help="Destination node group (name or uuid)",
1341
                          default=None, action="append",
1342
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1343

    
1344
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1345
                               action="append", dest="ignore_errors",
1346
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1347
                               help="Error code to be ignored")
1348

    
1349
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1350
                            action="append",
1351
                            help=("Specify disk state information in the format"
1352
                                  " storage_type/identifier:option=value,..."),
1353
                            type="identkeyval")
1354

    
1355
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1356
                          action="append",
1357
                          help=("Specify hypervisor state information in the"
1358
                                " format hypervisor:option=value,..."),
1359
                          type="identkeyval")
1360

    
1361
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1362
                                action="store_true", default=False,
1363
                                help="Ignore instance policy violations")
1364

    
1365
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1366
                             help="Sets the instance's runtime memory,"
1367
                             " ballooning it up or down to the new value",
1368
                             default=None, type="unit", metavar="<size>")
1369

    
1370
#: Options provided by all commands
1371
COMMON_OPTS = [DEBUG_OPT]
1372

    
1373
# common options for creating instances. add and import then add their own
1374
# specific ones.
1375
COMMON_CREATE_OPTS = [
1376
  BACKEND_OPT,
1377
  DISK_OPT,
1378
  DISK_TEMPLATE_OPT,
1379
  FILESTORE_DIR_OPT,
1380
  FILESTORE_DRIVER_OPT,
1381
  HYPERVISOR_OPT,
1382
  IALLOCATOR_OPT,
1383
  NET_OPT,
1384
  NODE_PLACEMENT_OPT,
1385
  NOIPCHECK_OPT,
1386
  NONAMECHECK_OPT,
1387
  NONICS_OPT,
1388
  NWSYNC_OPT,
1389
  OSPARAMS_OPT,
1390
  OS_SIZE_OPT,
1391
  SUBMIT_OPT,
1392
  TAG_ADD_OPT,
1393
  DRY_RUN_OPT,
1394
  PRIORITY_OPT,
1395
  ]
1396

    
1397
# common instance policy options
1398
INSTANCE_POLICY_OPTS = [
1399
  SPECS_CPU_COUNT_OPT,
1400
  SPECS_DISK_COUNT_OPT,
1401
  SPECS_DISK_SIZE_OPT,
1402
  SPECS_MEM_SIZE_OPT,
1403
  SPECS_NIC_COUNT_OPT,
1404
  SPECS_DISK_TEMPLATES,
1405
  ]
1406

    
1407

    
1408
def _ParseArgs(argv, commands, aliases, env_override):
1409
  """Parser for the command line arguments.
1410

1411
  This function parses the arguments and returns the function which
1412
  must be executed together with its (modified) arguments.
1413

1414
  @param argv: the command line
1415
  @param commands: dictionary with special contents, see the design
1416
      doc for cmdline handling
1417
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
1418
  @param env_override: list of env variables allowed for default args
1419

1420
  """
1421
  assert not (env_override - set(commands))
1422

    
1423
  if len(argv) == 0:
1424
    binary = "<command>"
1425
  else:
1426
    binary = argv[0].split("/")[-1]
1427

    
1428
  if len(argv) > 1 and argv[1] == "--version":
1429
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
1430
             constants.RELEASE_VERSION)
1431
    # Quit right away. That way we don't have to care about this special
1432
    # argument. optparse.py does it the same.
1433
    sys.exit(0)
1434

    
1435
  if len(argv) < 2 or not (argv[1] in commands or
1436
                           argv[1] in aliases):
1437
    # let's do a nice thing
1438
    sortedcmds = commands.keys()
1439
    sortedcmds.sort()
1440

    
1441
    ToStdout("Usage: %s {command} [options...] [argument...]", binary)
1442
    ToStdout("%s <command> --help to see details, or man %s", binary, binary)
1443
    ToStdout("")
1444

    
1445
    # compute the max line length for cmd + usage
1446
    mlen = max([len(" %s" % cmd) for cmd in commands])
1447
    mlen = min(60, mlen) # should not get here...
1448

    
1449
    # and format a nice command list
1450
    ToStdout("Commands:")
1451
    for cmd in sortedcmds:
1452
      cmdstr = " %s" % (cmd,)
1453
      help_text = commands[cmd][4]
1454
      help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1455
      ToStdout("%-*s - %s", mlen, cmdstr, help_lines.pop(0))
1456
      for line in help_lines:
1457
        ToStdout("%-*s   %s", mlen, "", line)
1458

    
1459
    ToStdout("")
1460

    
1461
    return None, None, None
1462

    
1463
  # get command, unalias it, and look it up in commands
1464
  cmd = argv.pop(1)
1465
  if cmd in aliases:
1466
    if cmd in commands:
1467
      raise errors.ProgrammerError("Alias '%s' overrides an existing"
1468
                                   " command" % cmd)
1469

    
1470
    if aliases[cmd] not in commands:
1471
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1472
                                   " command '%s'" % (cmd, aliases[cmd]))
1473

    
1474
    cmd = aliases[cmd]
1475

    
1476
  if cmd in env_override:
1477
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1478
    env_args = os.environ.get(args_env_name)
1479
    if env_args:
1480
      argv = utils.InsertAtPos(argv, 1, shlex.split(env_args))
1481

    
1482
  func, args_def, parser_opts, usage, description = commands[cmd]
1483
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1484
                        description=description,
1485
                        formatter=TitledHelpFormatter(),
1486
                        usage="%%prog %s %s" % (cmd, usage))
1487
  parser.disable_interspersed_args()
1488
  options, args = parser.parse_args(args=argv[1:])
1489

    
1490
  if not _CheckArguments(cmd, args_def, args):
1491
    return None, None, None
1492

    
1493
  return func, options, args
1494

    
1495

    
1496
def _CheckArguments(cmd, args_def, args):
1497
  """Verifies the arguments using the argument definition.
1498

1499
  Algorithm:
1500

1501
    1. Abort with error if values specified by user but none expected.
1502

1503
    1. For each argument in definition
1504

1505
      1. Keep running count of minimum number of values (min_count)
1506
      1. Keep running count of maximum number of values (max_count)
1507
      1. If it has an unlimited number of values
1508

1509
        1. Abort with error if it's not the last argument in the definition
1510

1511
    1. If last argument has limited number of values
1512

1513
      1. Abort with error if number of values doesn't match or is too large
1514

1515
    1. Abort with error if user didn't pass enough values (min_count)
1516

1517
  """
1518
  if args and not args_def:
1519
    ToStderr("Error: Command %s expects no arguments", cmd)
1520
    return False
1521

    
1522
  min_count = None
1523
  max_count = None
1524
  check_max = None
1525

    
1526
  last_idx = len(args_def) - 1
1527

    
1528
  for idx, arg in enumerate(args_def):
1529
    if min_count is None:
1530
      min_count = arg.min
1531
    elif arg.min is not None:
1532
      min_count += arg.min
1533

    
1534
    if max_count is None:
1535
      max_count = arg.max
1536
    elif arg.max is not None:
1537
      max_count += arg.max
1538

    
1539
    if idx == last_idx:
1540
      check_max = (arg.max is not None)
1541

    
1542
    elif arg.max is None:
1543
      raise errors.ProgrammerError("Only the last argument can have max=None")
1544

    
1545
  if check_max:
1546
    # Command with exact number of arguments
1547
    if (min_count is not None and max_count is not None and
1548
        min_count == max_count and len(args) != min_count):
1549
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1550
      return False
1551

    
1552
    # Command with limited number of arguments
1553
    if max_count is not None and len(args) > max_count:
1554
      ToStderr("Error: Command %s expects only %d argument(s)",
1555
               cmd, max_count)
1556
      return False
1557

    
1558
  # Command with some required arguments
1559
  if min_count is not None and len(args) < min_count:
1560
    ToStderr("Error: Command %s expects at least %d argument(s)",
1561
             cmd, min_count)
1562
    return False
1563

    
1564
  return True
1565

    
1566

    
1567
def SplitNodeOption(value):
1568
  """Splits the value of a --node option.
1569

1570
  """
1571
  if value and ":" in value:
1572
    return value.split(":", 1)
1573
  else:
1574
    return (value, None)
1575

    
1576

    
1577
def CalculateOSNames(os_name, os_variants):
1578
  """Calculates all the names an OS can be called, according to its variants.
1579

1580
  @type os_name: string
1581
  @param os_name: base name of the os
1582
  @type os_variants: list or None
1583
  @param os_variants: list of supported variants
1584
  @rtype: list
1585
  @return: list of valid names
1586

1587
  """
1588
  if os_variants:
1589
    return ["%s+%s" % (os_name, v) for v in os_variants]
1590
  else:
1591
    return [os_name]
1592

    
1593

    
1594
def ParseFields(selected, default):
1595
  """Parses the values of "--field"-like options.
1596

1597
  @type selected: string or None
1598
  @param selected: User-selected options
1599
  @type default: list
1600
  @param default: Default fields
1601

1602
  """
1603
  if selected is None:
1604
    return default
1605

    
1606
  if selected.startswith("+"):
1607
    return default + selected[1:].split(",")
1608

    
1609
  return selected.split(",")
1610

    
1611

    
1612
UsesRPC = rpc.RunWithRPC
1613

    
1614

    
1615
def AskUser(text, choices=None):
1616
  """Ask the user a question.
1617

1618
  @param text: the question to ask
1619

1620
  @param choices: list with elements tuples (input_char, return_value,
1621
      description); if not given, it will default to: [('y', True,
1622
      'Perform the operation'), ('n', False, 'Do no do the operation')];
1623
      note that the '?' char is reserved for help
1624

1625
  @return: one of the return values from the choices list; if input is
1626
      not possible (i.e. not running with a tty, we return the last
1627
      entry from the list
1628

1629
  """
1630
  if choices is None:
1631
    choices = [("y", True, "Perform the operation"),
1632
               ("n", False, "Do not perform the operation")]
1633
  if not choices or not isinstance(choices, list):
1634
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1635
  for entry in choices:
1636
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1637
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1638

    
1639
  answer = choices[-1][1]
1640
  new_text = []
1641
  for line in text.splitlines():
1642
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1643
  text = "\n".join(new_text)
1644
  try:
1645
    f = file("/dev/tty", "a+")
1646
  except IOError:
1647
    return answer
1648
  try:
1649
    chars = [entry[0] for entry in choices]
1650
    chars[-1] = "[%s]" % chars[-1]
1651
    chars.append("?")
1652
    maps = dict([(entry[0], entry[1]) for entry in choices])
1653
    while True:
1654
      f.write(text)
1655
      f.write("\n")
1656
      f.write("/".join(chars))
1657
      f.write(": ")
1658
      line = f.readline(2).strip().lower()
1659
      if line in maps:
1660
        answer = maps[line]
1661
        break
1662
      elif line == "?":
1663
        for entry in choices:
1664
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1665
        f.write("\n")
1666
        continue
1667
  finally:
1668
    f.close()
1669
  return answer
1670

    
1671

    
1672
class JobSubmittedException(Exception):
1673
  """Job was submitted, client should exit.
1674

1675
  This exception has one argument, the ID of the job that was
1676
  submitted. The handler should print this ID.
1677

1678
  This is not an error, just a structured way to exit from clients.
1679

1680
  """
1681

    
1682

    
1683
def SendJob(ops, cl=None):
1684
  """Function to submit an opcode without waiting for the results.
1685

1686
  @type ops: list
1687
  @param ops: list of opcodes
1688
  @type cl: luxi.Client
1689
  @param cl: the luxi client to use for communicating with the master;
1690
             if None, a new client will be created
1691

1692
  """
1693
  if cl is None:
1694
    cl = GetClient()
1695

    
1696
  job_id = cl.SubmitJob(ops)
1697

    
1698
  return job_id
1699

    
1700

    
1701
def GenericPollJob(job_id, cbs, report_cbs):
1702
  """Generic job-polling function.
1703

1704
  @type job_id: number
1705
  @param job_id: Job ID
1706
  @type cbs: Instance of L{JobPollCbBase}
1707
  @param cbs: Data callbacks
1708
  @type report_cbs: Instance of L{JobPollReportCbBase}
1709
  @param report_cbs: Reporting callbacks
1710

1711
  """
1712
  prev_job_info = None
1713
  prev_logmsg_serial = None
1714

    
1715
  status = None
1716

    
1717
  while True:
1718
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1719
                                      prev_logmsg_serial)
1720
    if not result:
1721
      # job not found, go away!
1722
      raise errors.JobLost("Job with id %s lost" % job_id)
1723

    
1724
    if result == constants.JOB_NOTCHANGED:
1725
      report_cbs.ReportNotChanged(job_id, status)
1726

    
1727
      # Wait again
1728
      continue
1729

    
1730
    # Split result, a tuple of (field values, log entries)
1731
    (job_info, log_entries) = result
1732
    (status, ) = job_info
1733

    
1734
    if log_entries:
1735
      for log_entry in log_entries:
1736
        (serial, timestamp, log_type, message) = log_entry
1737
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1738
                                    log_type, message)
1739
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
1740

    
1741
    # TODO: Handle canceled and archived jobs
1742
    elif status in (constants.JOB_STATUS_SUCCESS,
1743
                    constants.JOB_STATUS_ERROR,
1744
                    constants.JOB_STATUS_CANCELING,
1745
                    constants.JOB_STATUS_CANCELED):
1746
      break
1747

    
1748
    prev_job_info = job_info
1749

    
1750
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
1751
  if not jobs:
1752
    raise errors.JobLost("Job with id %s lost" % job_id)
1753

    
1754
  status, opstatus, result = jobs[0]
1755

    
1756
  if status == constants.JOB_STATUS_SUCCESS:
1757
    return result
1758

    
1759
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
1760
    raise errors.OpExecError("Job was canceled")
1761

    
1762
  has_ok = False
1763
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
1764
    if status == constants.OP_STATUS_SUCCESS:
1765
      has_ok = True
1766
    elif status == constants.OP_STATUS_ERROR:
1767
      errors.MaybeRaise(msg)
1768

    
1769
      if has_ok:
1770
        raise errors.OpExecError("partial failure (opcode %d): %s" %
1771
                                 (idx, msg))
1772

    
1773
      raise errors.OpExecError(str(msg))
1774

    
1775
  # default failure mode
1776
  raise errors.OpExecError(result)
1777

    
1778

    
1779
class JobPollCbBase:
1780
  """Base class for L{GenericPollJob} callbacks.
1781

1782
  """
1783
  def __init__(self):
1784
    """Initializes this class.
1785

1786
    """
1787

    
1788
  def WaitForJobChangeOnce(self, job_id, fields,
1789
                           prev_job_info, prev_log_serial):
1790
    """Waits for changes on a job.
1791

1792
    """
1793
    raise NotImplementedError()
1794

    
1795
  def QueryJobs(self, job_ids, fields):
1796
    """Returns the selected fields for the selected job IDs.
1797

1798
    @type job_ids: list of numbers
1799
    @param job_ids: Job IDs
1800
    @type fields: list of strings
1801
    @param fields: Fields
1802

1803
    """
1804
    raise NotImplementedError()
1805

    
1806

    
1807
class JobPollReportCbBase:
1808
  """Base class for L{GenericPollJob} reporting callbacks.
1809

1810
  """
1811
  def __init__(self):
1812
    """Initializes this class.
1813

1814
    """
1815

    
1816
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1817
    """Handles a log message.
1818

1819
    """
1820
    raise NotImplementedError()
1821

    
1822
  def ReportNotChanged(self, job_id, status):
1823
    """Called for if a job hasn't changed in a while.
1824

1825
    @type job_id: number
1826
    @param job_id: Job ID
1827
    @type status: string or None
1828
    @param status: Job status if available
1829

1830
    """
1831
    raise NotImplementedError()
1832

    
1833

    
1834
class _LuxiJobPollCb(JobPollCbBase):
1835
  def __init__(self, cl):
1836
    """Initializes this class.
1837

1838
    """
1839
    JobPollCbBase.__init__(self)
1840
    self.cl = cl
1841

    
1842
  def WaitForJobChangeOnce(self, job_id, fields,
1843
                           prev_job_info, prev_log_serial):
1844
    """Waits for changes on a job.
1845

1846
    """
1847
    return self.cl.WaitForJobChangeOnce(job_id, fields,
1848
                                        prev_job_info, prev_log_serial)
1849

    
1850
  def QueryJobs(self, job_ids, fields):
1851
    """Returns the selected fields for the selected job IDs.
1852

1853
    """
1854
    return self.cl.QueryJobs(job_ids, fields)
1855

    
1856

    
1857
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
1858
  def __init__(self, feedback_fn):
1859
    """Initializes this class.
1860

1861
    """
1862
    JobPollReportCbBase.__init__(self)
1863

    
1864
    self.feedback_fn = feedback_fn
1865

    
1866
    assert callable(feedback_fn)
1867

    
1868
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1869
    """Handles a log message.
1870

1871
    """
1872
    self.feedback_fn((timestamp, log_type, log_msg))
1873

    
1874
  def ReportNotChanged(self, job_id, status):
1875
    """Called if a job hasn't changed in a while.
1876

1877
    """
1878
    # Ignore
1879

    
1880

    
1881
class StdioJobPollReportCb(JobPollReportCbBase):
1882
  def __init__(self):
1883
    """Initializes this class.
1884

1885
    """
1886
    JobPollReportCbBase.__init__(self)
1887

    
1888
    self.notified_queued = False
1889
    self.notified_waitlock = False
1890

    
1891
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1892
    """Handles a log message.
1893

1894
    """
1895
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
1896
             FormatLogMessage(log_type, log_msg))
1897

    
1898
  def ReportNotChanged(self, job_id, status):
1899
    """Called if a job hasn't changed in a while.
1900

1901
    """
1902
    if status is None:
1903
      return
1904

    
1905
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
1906
      ToStderr("Job %s is waiting in queue", job_id)
1907
      self.notified_queued = True
1908

    
1909
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
1910
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
1911
      self.notified_waitlock = True
1912

    
1913

    
1914
def FormatLogMessage(log_type, log_msg):
1915
  """Formats a job message according to its type.
1916

1917
  """
1918
  if log_type != constants.ELOG_MESSAGE:
1919
    log_msg = str(log_msg)
1920

    
1921
  return utils.SafeEncode(log_msg)
1922

    
1923

    
1924
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
1925
  """Function to poll for the result of a job.
1926

1927
  @type job_id: job identified
1928
  @param job_id: the job to poll for results
1929
  @type cl: luxi.Client
1930
  @param cl: the luxi client to use for communicating with the master;
1931
             if None, a new client will be created
1932

1933
  """
1934
  if cl is None:
1935
    cl = GetClient()
1936

    
1937
  if reporter is None:
1938
    if feedback_fn:
1939
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
1940
    else:
1941
      reporter = StdioJobPollReportCb()
1942
  elif feedback_fn:
1943
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
1944

    
1945
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
1946

    
1947

    
1948
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
1949
  """Legacy function to submit an opcode.
1950

1951
  This is just a simple wrapper over the construction of the processor
1952
  instance. It should be extended to better handle feedback and
1953
  interaction functions.
1954

1955
  """
1956
  if cl is None:
1957
    cl = GetClient()
1958

    
1959
  SetGenericOpcodeOpts([op], opts)
1960

    
1961
  job_id = SendJob([op], cl=cl)
1962

    
1963
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
1964
                       reporter=reporter)
1965

    
1966
  return op_results[0]
1967

    
1968

    
1969
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
1970
  """Wrapper around SubmitOpCode or SendJob.
1971

1972
  This function will decide, based on the 'opts' parameter, whether to
1973
  submit and wait for the result of the opcode (and return it), or
1974
  whether to just send the job and print its identifier. It is used in
1975
  order to simplify the implementation of the '--submit' option.
1976

1977
  It will also process the opcodes if we're sending the via SendJob
1978
  (otherwise SubmitOpCode does it).
1979

1980
  """
1981
  if opts and opts.submit_only:
1982
    job = [op]
1983
    SetGenericOpcodeOpts(job, opts)
1984
    job_id = SendJob(job, cl=cl)
1985
    raise JobSubmittedException(job_id)
1986
  else:
1987
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
1988

    
1989

    
1990
def SetGenericOpcodeOpts(opcode_list, options):
1991
  """Processor for generic options.
1992

1993
  This function updates the given opcodes based on generic command
1994
  line options (like debug, dry-run, etc.).
1995

1996
  @param opcode_list: list of opcodes
1997
  @param options: command line options or None
1998
  @return: None (in-place modification)
1999

2000
  """
2001
  if not options:
2002
    return
2003
  for op in opcode_list:
2004
    op.debug_level = options.debug
2005
    if hasattr(options, "dry_run"):
2006
      op.dry_run = options.dry_run
2007
    if getattr(options, "priority", None) is not None:
2008
      op.priority = _PRIONAME_TO_VALUE[options.priority]
2009

    
2010

    
2011
def GetClient():
2012
  # TODO: Cache object?
2013
  try:
2014
    client = luxi.Client()
2015
  except luxi.NoMasterError:
2016
    ss = ssconf.SimpleStore()
2017

    
2018
    # Try to read ssconf file
2019
    try:
2020
      ss.GetMasterNode()
2021
    except errors.ConfigurationError:
2022
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
2023
                                 " not part of a cluster")
2024

    
2025
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
2026
    if master != myself:
2027
      raise errors.OpPrereqError("This is not the master node, please connect"
2028
                                 " to node '%s' and rerun the command" %
2029
                                 master)
2030
    raise
2031
  return client
2032

    
2033

    
2034
def FormatError(err):
2035
  """Return a formatted error message for a given error.
2036

2037
  This function takes an exception instance and returns a tuple
2038
  consisting of two values: first, the recommended exit code, and
2039
  second, a string describing the error message (not
2040
  newline-terminated).
2041

2042
  """
2043
  retcode = 1
2044
  obuf = StringIO()
2045
  msg = str(err)
2046
  if isinstance(err, errors.ConfigurationError):
2047
    txt = "Corrupt configuration file: %s" % msg
2048
    logging.error(txt)
2049
    obuf.write(txt + "\n")
2050
    obuf.write("Aborting.")
2051
    retcode = 2
2052
  elif isinstance(err, errors.HooksAbort):
2053
    obuf.write("Failure: hooks execution failed:\n")
2054
    for node, script, out in err.args[0]:
2055
      if out:
2056
        obuf.write("  node: %s, script: %s, output: %s\n" %
2057
                   (node, script, out))
2058
      else:
2059
        obuf.write("  node: %s, script: %s (no output)\n" %
2060
                   (node, script))
2061
  elif isinstance(err, errors.HooksFailure):
2062
    obuf.write("Failure: hooks general failure: %s" % msg)
2063
  elif isinstance(err, errors.ResolverError):
2064
    this_host = netutils.Hostname.GetSysName()
2065
    if err.args[0] == this_host:
2066
      msg = "Failure: can't resolve my own hostname ('%s')"
2067
    else:
2068
      msg = "Failure: can't resolve hostname '%s'"
2069
    obuf.write(msg % err.args[0])
2070
  elif isinstance(err, errors.OpPrereqError):
2071
    if len(err.args) == 2:
2072
      obuf.write("Failure: prerequisites not met for this"
2073
               " operation:\nerror type: %s, error details:\n%s" %
2074
                 (err.args[1], err.args[0]))
2075
    else:
2076
      obuf.write("Failure: prerequisites not met for this"
2077
                 " operation:\n%s" % msg)
2078
  elif isinstance(err, errors.OpExecError):
2079
    obuf.write("Failure: command execution error:\n%s" % msg)
2080
  elif isinstance(err, errors.TagError):
2081
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2082
  elif isinstance(err, errors.JobQueueDrainError):
2083
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2084
               " accept new requests\n")
2085
  elif isinstance(err, errors.JobQueueFull):
2086
    obuf.write("Failure: the job queue is full and doesn't accept new"
2087
               " job submissions until old jobs are archived\n")
2088
  elif isinstance(err, errors.TypeEnforcementError):
2089
    obuf.write("Parameter Error: %s" % msg)
2090
  elif isinstance(err, errors.ParameterError):
2091
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2092
  elif isinstance(err, luxi.NoMasterError):
2093
    obuf.write("Cannot communicate with the master daemon.\nIs it running"
2094
               " and listening for connections?")
2095
  elif isinstance(err, luxi.TimeoutError):
2096
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2097
               " been submitted and will continue to run even if the call"
2098
               " timed out. Useful commands in this situation are \"gnt-job"
2099
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2100
    obuf.write(msg)
2101
  elif isinstance(err, luxi.PermissionError):
2102
    obuf.write("It seems you don't have permissions to connect to the"
2103
               " master daemon.\nPlease retry as a different user.")
2104
  elif isinstance(err, luxi.ProtocolError):
2105
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2106
               "%s" % msg)
2107
  elif isinstance(err, errors.JobLost):
2108
    obuf.write("Error checking job status: %s" % msg)
2109
  elif isinstance(err, errors.QueryFilterParseError):
2110
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2111
    obuf.write("\n".join(err.GetDetails()))
2112
  elif isinstance(err, errors.GenericError):
2113
    obuf.write("Unhandled Ganeti error: %s" % msg)
2114
  elif isinstance(err, JobSubmittedException):
2115
    obuf.write("JobID: %s\n" % err.args[0])
2116
    retcode = 0
2117
  else:
2118
    obuf.write("Unhandled exception: %s" % msg)
2119
  return retcode, obuf.getvalue().rstrip("\n")
2120

    
2121

    
2122
def GenericMain(commands, override=None, aliases=None,
2123
                env_override=frozenset()):
2124
  """Generic main function for all the gnt-* commands.
2125

2126
  @param commands: a dictionary with a special structure, see the design doc
2127
                   for command line handling.
2128
  @param override: if not None, we expect a dictionary with keys that will
2129
                   override command line options; this can be used to pass
2130
                   options from the scripts to generic functions
2131
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2132
  @param env_override: list of environment names which are allowed to submit
2133
                       default args for commands
2134

2135
  """
2136
  # save the program name and the entire command line for later logging
2137
  if sys.argv:
2138
    binary = os.path.basename(sys.argv[0]) or sys.argv[0]
2139
    if len(sys.argv) >= 2:
2140
      binary += " " + sys.argv[1]
2141
      old_cmdline = " ".join(sys.argv[2:])
2142
    else:
2143
      old_cmdline = ""
2144
  else:
2145
    binary = "<unknown program>"
2146
    old_cmdline = ""
2147

    
2148
  if aliases is None:
2149
    aliases = {}
2150

    
2151
  try:
2152
    func, options, args = _ParseArgs(sys.argv, commands, aliases, env_override)
2153
  except errors.ParameterError, err:
2154
    result, err_msg = FormatError(err)
2155
    ToStderr(err_msg)
2156
    return 1
2157

    
2158
  if func is None: # parse error
2159
    return 1
2160

    
2161
  if override is not None:
2162
    for key, val in override.iteritems():
2163
      setattr(options, key, val)
2164

    
2165
  utils.SetupLogging(constants.LOG_COMMANDS, binary, debug=options.debug,
2166
                     stderr_logging=True)
2167

    
2168
  if old_cmdline:
2169
    logging.info("run with arguments '%s'", old_cmdline)
2170
  else:
2171
    logging.info("run with no arguments")
2172

    
2173
  try:
2174
    result = func(options, args)
2175
  except (errors.GenericError, luxi.ProtocolError,
2176
          JobSubmittedException), err:
2177
    result, err_msg = FormatError(err)
2178
    logging.exception("Error during command processing")
2179
    ToStderr(err_msg)
2180
  except KeyboardInterrupt:
2181
    result = constants.EXIT_FAILURE
2182
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2183
             " might have been submitted and"
2184
             " will continue to run in the background.")
2185
  except IOError, err:
2186
    if err.errno == errno.EPIPE:
2187
      # our terminal went away, we'll exit
2188
      sys.exit(constants.EXIT_FAILURE)
2189
    else:
2190
      raise
2191

    
2192
  return result
2193

    
2194

    
2195
def ParseNicOption(optvalue):
2196
  """Parses the value of the --net option(s).
2197

2198
  """
2199
  try:
2200
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2201
  except (TypeError, ValueError), err:
2202
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
2203

    
2204
  nics = [{}] * nic_max
2205
  for nidx, ndict in optvalue:
2206
    nidx = int(nidx)
2207

    
2208
    if not isinstance(ndict, dict):
2209
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2210
                                 " got %s" % (nidx, ndict))
2211

    
2212
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2213

    
2214
    nics[nidx] = ndict
2215

    
2216
  return nics
2217

    
2218

    
2219
def GenericInstanceCreate(mode, opts, args):
2220
  """Add an instance to the cluster via either creation or import.
2221

2222
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2223
  @param opts: the command line options selected by the user
2224
  @type args: list
2225
  @param args: should contain only one element, the new instance name
2226
  @rtype: int
2227
  @return: the desired exit code
2228

2229
  """
2230
  instance = args[0]
2231

    
2232
  (pnode, snode) = SplitNodeOption(opts.node)
2233

    
2234
  hypervisor = None
2235
  hvparams = {}
2236
  if opts.hypervisor:
2237
    hypervisor, hvparams = opts.hypervisor
2238

    
2239
  if opts.nics:
2240
    nics = ParseNicOption(opts.nics)
2241
  elif opts.no_nics:
2242
    # no nics
2243
    nics = []
2244
  elif mode == constants.INSTANCE_CREATE:
2245
    # default of one nic, all auto
2246
    nics = [{}]
2247
  else:
2248
    # mode == import
2249
    nics = []
2250

    
2251
  if opts.disk_template == constants.DT_DISKLESS:
2252
    if opts.disks or opts.sd_size is not None:
2253
      raise errors.OpPrereqError("Diskless instance but disk"
2254
                                 " information passed")
2255
    disks = []
2256
  else:
2257
    if (not opts.disks and not opts.sd_size
2258
        and mode == constants.INSTANCE_CREATE):
2259
      raise errors.OpPrereqError("No disk information specified")
2260
    if opts.disks and opts.sd_size is not None:
2261
      raise errors.OpPrereqError("Please use either the '--disk' or"
2262
                                 " '-s' option")
2263
    if opts.sd_size is not None:
2264
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2265

    
2266
    if opts.disks:
2267
      try:
2268
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2269
      except ValueError, err:
2270
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
2271
      disks = [{}] * disk_max
2272
    else:
2273
      disks = []
2274
    for didx, ddict in opts.disks:
2275
      didx = int(didx)
2276
      if not isinstance(ddict, dict):
2277
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2278
        raise errors.OpPrereqError(msg)
2279
      elif constants.IDISK_SIZE in ddict:
2280
        if constants.IDISK_ADOPT in ddict:
2281
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2282
                                     " (disk %d)" % didx)
2283
        try:
2284
          ddict[constants.IDISK_SIZE] = \
2285
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2286
        except ValueError, err:
2287
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2288
                                     (didx, err))
2289
      elif constants.IDISK_ADOPT in ddict:
2290
        if mode == constants.INSTANCE_IMPORT:
2291
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2292
                                     " import")
2293
        ddict[constants.IDISK_SIZE] = 0
2294
      else:
2295
        raise errors.OpPrereqError("Missing size or adoption source for"
2296
                                   " disk %d" % didx)
2297
      disks[didx] = ddict
2298

    
2299
  if opts.tags is not None:
2300
    tags = opts.tags.split(",")
2301
  else:
2302
    tags = []
2303

    
2304
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2305
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2306

    
2307
  if mode == constants.INSTANCE_CREATE:
2308
    start = opts.start
2309
    os_type = opts.os
2310
    force_variant = opts.force_variant
2311
    src_node = None
2312
    src_path = None
2313
    no_install = opts.no_install
2314
    identify_defaults = False
2315
  elif mode == constants.INSTANCE_IMPORT:
2316
    start = False
2317
    os_type = None
2318
    force_variant = False
2319
    src_node = opts.src_node
2320
    src_path = opts.src_dir
2321
    no_install = None
2322
    identify_defaults = opts.identify_defaults
2323
  else:
2324
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2325

    
2326
  op = opcodes.OpInstanceCreate(instance_name=instance,
2327
                                disks=disks,
2328
                                disk_template=opts.disk_template,
2329
                                nics=nics,
2330
                                pnode=pnode, snode=snode,
2331
                                ip_check=opts.ip_check,
2332
                                name_check=opts.name_check,
2333
                                wait_for_sync=opts.wait_for_sync,
2334
                                file_storage_dir=opts.file_storage_dir,
2335
                                file_driver=opts.file_driver,
2336
                                iallocator=opts.iallocator,
2337
                                hypervisor=hypervisor,
2338
                                hvparams=hvparams,
2339
                                beparams=opts.beparams,
2340
                                osparams=opts.osparams,
2341
                                mode=mode,
2342
                                start=start,
2343
                                os_type=os_type,
2344
                                force_variant=force_variant,
2345
                                src_node=src_node,
2346
                                src_path=src_path,
2347
                                tags=tags,
2348
                                no_install=no_install,
2349
                                identify_defaults=identify_defaults,
2350
                                ignore_ipolicy=opts.ignore_ipolicy)
2351

    
2352
  SubmitOrSend(op, opts)
2353
  return 0
2354

    
2355

    
2356
class _RunWhileClusterStoppedHelper:
2357
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2358

2359
  """
2360
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
2361
    """Initializes this class.
2362

2363
    @type feedback_fn: callable
2364
    @param feedback_fn: Feedback function
2365
    @type cluster_name: string
2366
    @param cluster_name: Cluster name
2367
    @type master_node: string
2368
    @param master_node Master node name
2369
    @type online_nodes: list
2370
    @param online_nodes: List of names of online nodes
2371

2372
    """
2373
    self.feedback_fn = feedback_fn
2374
    self.cluster_name = cluster_name
2375
    self.master_node = master_node
2376
    self.online_nodes = online_nodes
2377

    
2378
    self.ssh = ssh.SshRunner(self.cluster_name)
2379

    
2380
    self.nonmaster_nodes = [name for name in online_nodes
2381
                            if name != master_node]
2382

    
2383
    assert self.master_node not in self.nonmaster_nodes
2384

    
2385
  def _RunCmd(self, node_name, cmd):
2386
    """Runs a command on the local or a remote machine.
2387

2388
    @type node_name: string
2389
    @param node_name: Machine name
2390
    @type cmd: list
2391
    @param cmd: Command
2392

2393
    """
2394
    if node_name is None or node_name == self.master_node:
2395
      # No need to use SSH
2396
      result = utils.RunCmd(cmd)
2397
    else:
2398
      result = self.ssh.Run(node_name, "root", utils.ShellQuoteArgs(cmd))
2399

    
2400
    if result.failed:
2401
      errmsg = ["Failed to run command %s" % result.cmd]
2402
      if node_name:
2403
        errmsg.append("on node %s" % node_name)
2404
      errmsg.append(": exitcode %s and error %s" %
2405
                    (result.exit_code, result.output))
2406
      raise errors.OpExecError(" ".join(errmsg))
2407

    
2408
  def Call(self, fn, *args):
2409
    """Call function while all daemons are stopped.
2410

2411
    @type fn: callable
2412
    @param fn: Function to be called
2413

2414
    """
2415
    # Pause watcher by acquiring an exclusive lock on watcher state file
2416
    self.feedback_fn("Blocking watcher")
2417
    watcher_block = utils.FileLock.Open(constants.WATCHER_LOCK_FILE)
2418
    try:
2419
      # TODO: Currently, this just blocks. There's no timeout.
2420
      # TODO: Should it be a shared lock?
2421
      watcher_block.Exclusive(blocking=True)
2422

    
2423
      # Stop master daemons, so that no new jobs can come in and all running
2424
      # ones are finished
2425
      self.feedback_fn("Stopping master daemons")
2426
      self._RunCmd(None, [constants.DAEMON_UTIL, "stop-master"])
2427
      try:
2428
        # Stop daemons on all nodes
2429
        for node_name in self.online_nodes:
2430
          self.feedback_fn("Stopping daemons on %s" % node_name)
2431
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "stop-all"])
2432

    
2433
        # All daemons are shut down now
2434
        try:
2435
          return fn(self, *args)
2436
        except Exception, err:
2437
          _, errmsg = FormatError(err)
2438
          logging.exception("Caught exception")
2439
          self.feedback_fn(errmsg)
2440
          raise
2441
      finally:
2442
        # Start cluster again, master node last
2443
        for node_name in self.nonmaster_nodes + [self.master_node]:
2444
          self.feedback_fn("Starting daemons on %s" % node_name)
2445
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "start-all"])
2446
    finally:
2447
      # Resume watcher
2448
      watcher_block.Close()
2449

    
2450

    
2451
def RunWhileClusterStopped(feedback_fn, fn, *args):
2452
  """Calls a function while all cluster daemons are stopped.
2453

2454
  @type feedback_fn: callable
2455
  @param feedback_fn: Feedback function
2456
  @type fn: callable
2457
  @param fn: Function to be called when daemons are stopped
2458

2459
  """
2460
  feedback_fn("Gathering cluster information")
2461

    
2462
  # This ensures we're running on the master daemon
2463
  cl = GetClient()
2464

    
2465
  (cluster_name, master_node) = \
2466
    cl.QueryConfigValues(["cluster_name", "master_node"])
2467

    
2468
  online_nodes = GetOnlineNodes([], cl=cl)
2469

    
2470
  # Don't keep a reference to the client. The master daemon will go away.
2471
  del cl
2472

    
2473
  assert master_node in online_nodes
2474

    
2475
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2476
                                       online_nodes).Call(fn, *args)
2477

    
2478

    
2479
def GenerateTable(headers, fields, separator, data,
2480
                  numfields=None, unitfields=None,
2481
                  units=None):
2482
  """Prints a table with headers and different fields.
2483

2484
  @type headers: dict
2485
  @param headers: dictionary mapping field names to headers for
2486
      the table
2487
  @type fields: list
2488
  @param fields: the field names corresponding to each row in
2489
      the data field
2490
  @param separator: the separator to be used; if this is None,
2491
      the default 'smart' algorithm is used which computes optimal
2492
      field width, otherwise just the separator is used between
2493
      each field
2494
  @type data: list
2495
  @param data: a list of lists, each sublist being one row to be output
2496
  @type numfields: list
2497
  @param numfields: a list with the fields that hold numeric
2498
      values and thus should be right-aligned
2499
  @type unitfields: list
2500
  @param unitfields: a list with the fields that hold numeric
2501
      values that should be formatted with the units field
2502
  @type units: string or None
2503
  @param units: the units we should use for formatting, or None for
2504
      automatic choice (human-readable for non-separator usage, otherwise
2505
      megabytes); this is a one-letter string
2506

2507
  """
2508
  if units is None:
2509
    if separator:
2510
      units = "m"
2511
    else:
2512
      units = "h"
2513

    
2514
  if numfields is None:
2515
    numfields = []
2516
  if unitfields is None:
2517
    unitfields = []
2518

    
2519
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2520
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2521

    
2522
  format_fields = []
2523
  for field in fields:
2524
    if headers and field not in headers:
2525
      # TODO: handle better unknown fields (either revert to old
2526
      # style of raising exception, or deal more intelligently with
2527
      # variable fields)
2528
      headers[field] = field
2529
    if separator is not None:
2530
      format_fields.append("%s")
2531
    elif numfields.Matches(field):
2532
      format_fields.append("%*s")
2533
    else:
2534
      format_fields.append("%-*s")
2535

    
2536
  if separator is None:
2537
    mlens = [0 for name in fields]
2538
    format_str = " ".join(format_fields)
2539
  else:
2540
    format_str = separator.replace("%", "%%").join(format_fields)
2541

    
2542
  for row in data:
2543
    if row is None:
2544
      continue
2545
    for idx, val in enumerate(row):
2546
      if unitfields.Matches(fields[idx]):
2547
        try:
2548
          val = int(val)
2549
        except (TypeError, ValueError):
2550
          pass
2551
        else:
2552
          val = row[idx] = utils.FormatUnit(val, units)
2553
      val = row[idx] = str(val)
2554
      if separator is None:
2555
        mlens[idx] = max(mlens[idx], len(val))
2556

    
2557
  result = []
2558
  if headers:
2559
    args = []
2560
    for idx, name in enumerate(fields):
2561
      hdr = headers[name]
2562
      if separator is None:
2563
        mlens[idx] = max(mlens[idx], len(hdr))
2564
        args.append(mlens[idx])
2565
      args.append(hdr)
2566
    result.append(format_str % tuple(args))
2567

    
2568
  if separator is None:
2569
    assert len(mlens) == len(fields)
2570

    
2571
    if fields and not numfields.Matches(fields[-1]):
2572
      mlens[-1] = 0
2573

    
2574
  for line in data:
2575
    args = []
2576
    if line is None:
2577
      line = ["-" for _ in fields]
2578
    for idx in range(len(fields)):
2579
      if separator is None:
2580
        args.append(mlens[idx])
2581
      args.append(line[idx])
2582
    result.append(format_str % tuple(args))
2583

    
2584
  return result
2585

    
2586

    
2587
def _FormatBool(value):
2588
  """Formats a boolean value as a string.
2589

2590
  """
2591
  if value:
2592
    return "Y"
2593
  return "N"
2594

    
2595

    
2596
#: Default formatting for query results; (callback, align right)
2597
_DEFAULT_FORMAT_QUERY = {
2598
  constants.QFT_TEXT: (str, False),
2599
  constants.QFT_BOOL: (_FormatBool, False),
2600
  constants.QFT_NUMBER: (str, True),
2601
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
2602
  constants.QFT_OTHER: (str, False),
2603
  constants.QFT_UNKNOWN: (str, False),
2604
  }
2605

    
2606

    
2607
def _GetColumnFormatter(fdef, override, unit):
2608
  """Returns formatting function for a field.
2609

2610
  @type fdef: L{objects.QueryFieldDefinition}
2611
  @type override: dict
2612
  @param override: Dictionary for overriding field formatting functions,
2613
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2614
  @type unit: string
2615
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
2616
  @rtype: tuple; (callable, bool)
2617
  @return: Returns the function to format a value (takes one parameter) and a
2618
    boolean for aligning the value on the right-hand side
2619

2620
  """
2621
  fmt = override.get(fdef.name, None)
2622
  if fmt is not None:
2623
    return fmt
2624

    
2625
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
2626

    
2627
  if fdef.kind == constants.QFT_UNIT:
2628
    # Can't keep this information in the static dictionary
2629
    return (lambda value: utils.FormatUnit(value, unit), True)
2630

    
2631
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
2632
  if fmt is not None:
2633
    return fmt
2634

    
2635
  raise NotImplementedError("Can't format column type '%s'" % fdef.kind)
2636

    
2637

    
2638
class _QueryColumnFormatter:
2639
  """Callable class for formatting fields of a query.
2640

2641
  """
2642
  def __init__(self, fn, status_fn, verbose):
2643
    """Initializes this class.
2644

2645
    @type fn: callable
2646
    @param fn: Formatting function
2647
    @type status_fn: callable
2648
    @param status_fn: Function to report fields' status
2649
    @type verbose: boolean
2650
    @param verbose: whether to use verbose field descriptions or not
2651

2652
    """
2653
    self._fn = fn
2654
    self._status_fn = status_fn
2655
    self._verbose = verbose
2656

    
2657
  def __call__(self, data):
2658
    """Returns a field's string representation.
2659

2660
    """
2661
    (status, value) = data
2662

    
2663
    # Report status
2664
    self._status_fn(status)
2665

    
2666
    if status == constants.RS_NORMAL:
2667
      return self._fn(value)
2668

    
2669
    assert value is None, \
2670
           "Found value %r for abnormal status %s" % (value, status)
2671

    
2672
    return FormatResultError(status, self._verbose)
2673

    
2674

    
2675
def FormatResultError(status, verbose):
2676
  """Formats result status other than L{constants.RS_NORMAL}.
2677

2678
  @param status: The result status
2679
  @type verbose: boolean
2680
  @param verbose: Whether to return the verbose text
2681
  @return: Text of result status
2682

2683
  """
2684
  assert status != constants.RS_NORMAL, \
2685
         "FormatResultError called with status equal to constants.RS_NORMAL"
2686
  try:
2687
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
2688
  except KeyError:
2689
    raise NotImplementedError("Unknown status %s" % status)
2690
  else:
2691
    if verbose:
2692
      return verbose_text
2693
    return normal_text
2694

    
2695

    
2696
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
2697
                      header=False, verbose=False):
2698
  """Formats data in L{objects.QueryResponse}.
2699

2700
  @type result: L{objects.QueryResponse}
2701
  @param result: result of query operation
2702
  @type unit: string
2703
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
2704
    see L{utils.text.FormatUnit}
2705
  @type format_override: dict
2706
  @param format_override: Dictionary for overriding field formatting functions,
2707
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2708
  @type separator: string or None
2709
  @param separator: String used to separate fields
2710
  @type header: bool
2711
  @param header: Whether to output header row
2712
  @type verbose: boolean
2713
  @param verbose: whether to use verbose field descriptions or not
2714

2715
  """
2716
  if unit is None:
2717
    if separator:
2718
      unit = "m"
2719
    else:
2720
      unit = "h"
2721

    
2722
  if format_override is None:
2723
    format_override = {}
2724

    
2725
  stats = dict.fromkeys(constants.RS_ALL, 0)
2726

    
2727
  def _RecordStatus(status):
2728
    if status in stats:
2729
      stats[status] += 1
2730

    
2731
  columns = []
2732
  for fdef in result.fields:
2733
    assert fdef.title and fdef.name
2734
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
2735
    columns.append(TableColumn(fdef.title,
2736
                               _QueryColumnFormatter(fn, _RecordStatus,
2737
                                                     verbose),
2738
                               align_right))
2739

    
2740
  table = FormatTable(result.data, columns, header, separator)
2741

    
2742
  # Collect statistics
2743
  assert len(stats) == len(constants.RS_ALL)
2744
  assert compat.all(count >= 0 for count in stats.values())
2745

    
2746
  # Determine overall status. If there was no data, unknown fields must be
2747
  # detected via the field definitions.
2748
  if (stats[constants.RS_UNKNOWN] or
2749
      (not result.data and _GetUnknownFields(result.fields))):
2750
    status = QR_UNKNOWN
2751
  elif compat.any(count > 0 for key, count in stats.items()
2752
                  if key != constants.RS_NORMAL):
2753
    status = QR_INCOMPLETE
2754
  else:
2755
    status = QR_NORMAL
2756

    
2757
  return (status, table)
2758

    
2759

    
2760
def _GetUnknownFields(fdefs):
2761
  """Returns list of unknown fields included in C{fdefs}.
2762

2763
  @type fdefs: list of L{objects.QueryFieldDefinition}
2764

2765
  """
2766
  return [fdef for fdef in fdefs
2767
          if fdef.kind == constants.QFT_UNKNOWN]
2768

    
2769

    
2770
def _WarnUnknownFields(fdefs):
2771
  """Prints a warning to stderr if a query included unknown fields.
2772

2773
  @type fdefs: list of L{objects.QueryFieldDefinition}
2774

2775
  """
2776
  unknown = _GetUnknownFields(fdefs)
2777
  if unknown:
2778
    ToStderr("Warning: Queried for unknown fields %s",
2779
             utils.CommaJoin(fdef.name for fdef in unknown))
2780
    return True
2781

    
2782
  return False
2783

    
2784

    
2785
def GenericList(resource, fields, names, unit, separator, header, cl=None,
2786
                format_override=None, verbose=False, force_filter=False):
2787
  """Generic implementation for listing all items of a resource.
2788

2789
  @param resource: One of L{constants.QR_VIA_LUXI}
2790
  @type fields: list of strings
2791
  @param fields: List of fields to query for
2792
  @type names: list of strings
2793
  @param names: Names of items to query for
2794
  @type unit: string or None
2795
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
2796
    None for automatic choice (human-readable for non-separator usage,
2797
    otherwise megabytes); this is a one-letter string
2798
  @type separator: string or None
2799
  @param separator: String used to separate fields
2800
  @type header: bool
2801
  @param header: Whether to show header row
2802
  @type force_filter: bool
2803
  @param force_filter: Whether to always treat names as filter
2804
  @type format_override: dict
2805
  @param format_override: Dictionary for overriding field formatting functions,
2806
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2807
  @type verbose: boolean
2808
  @param verbose: whether to use verbose field descriptions or not
2809

2810
  """
2811
  if not names:
2812
    names = None
2813

    
2814
  qfilter = qlang.MakeFilter(names, force_filter)
2815

    
2816
  if cl is None:
2817
    cl = GetClient()
2818

    
2819
  response = cl.Query(resource, fields, qfilter)
2820

    
2821
  found_unknown = _WarnUnknownFields(response.fields)
2822

    
2823
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
2824
                                     header=header,
2825
                                     format_override=format_override,
2826
                                     verbose=verbose)
2827

    
2828
  for line in data:
2829
    ToStdout(line)
2830

    
2831
  assert ((found_unknown and status == QR_UNKNOWN) or
2832
          (not found_unknown and status != QR_UNKNOWN))
2833

    
2834
  if status == QR_UNKNOWN:
2835
    return constants.EXIT_UNKNOWN_FIELD
2836

    
2837
  # TODO: Should the list command fail if not all data could be collected?
2838
  return constants.EXIT_SUCCESS
2839

    
2840

    
2841
def GenericListFields(resource, fields, separator, header, cl=None):
2842
  """Generic implementation for listing fields for a resource.
2843

2844
  @param resource: One of L{constants.QR_VIA_LUXI}
2845
  @type fields: list of strings
2846
  @param fields: List of fields to query for
2847
  @type separator: string or None
2848
  @param separator: String used to separate fields
2849
  @type header: bool
2850
  @param header: Whether to show header row
2851

2852
  """
2853
  if cl is None:
2854
    cl = GetClient()
2855

    
2856
  if not fields:
2857
    fields = None
2858

    
2859
  response = cl.QueryFields(resource, fields)
2860

    
2861
  found_unknown = _WarnUnknownFields(response.fields)
2862

    
2863
  columns = [
2864
    TableColumn("Name", str, False),
2865
    TableColumn("Title", str, False),
2866
    TableColumn("Description", str, False),
2867
    ]
2868

    
2869
  rows = [[fdef.name, fdef.title, fdef.doc] for fdef in response.fields]
2870

    
2871
  for line in FormatTable(rows, columns, header, separator):
2872
    ToStdout(line)
2873

    
2874
  if found_unknown:
2875
    return constants.EXIT_UNKNOWN_FIELD
2876

    
2877
  return constants.EXIT_SUCCESS
2878

    
2879

    
2880
class TableColumn:
2881
  """Describes a column for L{FormatTable}.
2882

2883
  """
2884
  def __init__(self, title, fn, align_right):
2885
    """Initializes this class.
2886

2887
    @type title: string
2888
    @param title: Column title
2889
    @type fn: callable
2890
    @param fn: Formatting function
2891
    @type align_right: bool
2892
    @param align_right: Whether to align values on the right-hand side
2893

2894
    """
2895
    self.title = title
2896
    self.format = fn
2897
    self.align_right = align_right
2898

    
2899

    
2900
def _GetColFormatString(width, align_right):
2901
  """Returns the format string for a field.
2902

2903
  """
2904
  if align_right:
2905
    sign = ""
2906
  else:
2907
    sign = "-"
2908

    
2909
  return "%%%s%ss" % (sign, width)
2910

    
2911

    
2912
def FormatTable(rows, columns, header, separator):
2913
  """Formats data as a table.
2914

2915
  @type rows: list of lists
2916
  @param rows: Row data, one list per row
2917
  @type columns: list of L{TableColumn}
2918
  @param columns: Column descriptions
2919
  @type header: bool
2920
  @param header: Whether to show header row
2921
  @type separator: string or None
2922
  @param separator: String used to separate columns
2923

2924
  """
2925
  if header:
2926
    data = [[col.title for col in columns]]
2927
    colwidth = [len(col.title) for col in columns]
2928
  else:
2929
    data = []
2930
    colwidth = [0 for _ in columns]
2931

    
2932
  # Format row data
2933
  for row in rows:
2934
    assert len(row) == len(columns)
2935

    
2936
    formatted = [col.format(value) for value, col in zip(row, columns)]
2937

    
2938
    if separator is None:
2939
      # Update column widths
2940
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
2941
        # Modifying a list's items while iterating is fine
2942
        colwidth[idx] = max(oldwidth, len(value))
2943

    
2944
    data.append(formatted)
2945

    
2946
  if separator is not None:
2947
    # Return early if a separator is used
2948
    return [separator.join(row) for row in data]
2949

    
2950
  if columns and not columns[-1].align_right:
2951
    # Avoid unnecessary spaces at end of line
2952
    colwidth[-1] = 0
2953

    
2954
  # Build format string
2955
  fmt = " ".join([_GetColFormatString(width, col.align_right)
2956
                  for col, width in zip(columns, colwidth)])
2957

    
2958
  return [fmt % tuple(row) for row in data]
2959

    
2960

    
2961
def FormatTimestamp(ts):
2962
  """Formats a given timestamp.
2963

2964
  @type ts: timestamp
2965
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
2966

2967
  @rtype: string
2968
  @return: a string with the formatted timestamp
2969

2970
  """
2971
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
2972
    return "?"
2973
  sec, usec = ts
2974
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
2975

    
2976

    
2977
def ParseTimespec(value):
2978
  """Parse a time specification.
2979

2980
  The following suffixed will be recognized:
2981

2982
    - s: seconds
2983
    - m: minutes
2984
    - h: hours
2985
    - d: day
2986
    - w: weeks
2987

2988
  Without any suffix, the value will be taken to be in seconds.
2989

2990
  """
2991
  value = str(value)
2992
  if not value:
2993
    raise errors.OpPrereqError("Empty time specification passed")
2994
  suffix_map = {
2995
    "s": 1,
2996
    "m": 60,
2997
    "h": 3600,
2998
    "d": 86400,
2999
    "w": 604800,
3000
    }
3001
  if value[-1] not in suffix_map:
3002
    try:
3003
      value = int(value)
3004
    except (TypeError, ValueError):
3005
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
3006
  else:
3007
    multiplier = suffix_map[value[-1]]
3008
    value = value[:-1]
3009
    if not value: # no data left after stripping the suffix
3010
      raise errors.OpPrereqError("Invalid time specification (only"
3011
                                 " suffix passed)")
3012
    try:
3013
      value = int(value) * multiplier
3014
    except (TypeError, ValueError):
3015
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
3016
  return value
3017

    
3018

    
3019
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3020
                   filter_master=False, nodegroup=None):
3021
  """Returns the names of online nodes.
3022

3023
  This function will also log a warning on stderr with the names of
3024
  the online nodes.
3025

3026
  @param nodes: if not empty, use only this subset of nodes (minus the
3027
      offline ones)
3028
  @param cl: if not None, luxi client to use
3029
  @type nowarn: boolean
3030
  @param nowarn: by default, this function will output a note with the
3031
      offline nodes that are skipped; if this parameter is True the
3032
      note is not displayed
3033
  @type secondary_ips: boolean
3034
  @param secondary_ips: if True, return the secondary IPs instead of the
3035
      names, useful for doing network traffic over the replication interface
3036
      (if any)
3037
  @type filter_master: boolean
3038
  @param filter_master: if True, do not return the master node in the list
3039
      (useful in coordination with secondary_ips where we cannot check our
3040
      node name against the list)
3041
  @type nodegroup: string
3042
  @param nodegroup: If set, only return nodes in this node group
3043

3044
  """
3045
  if cl is None:
3046
    cl = GetClient()
3047

    
3048
  qfilter = []
3049

    
3050
  if nodes:
3051
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3052

    
3053
  if nodegroup is not None:
3054
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3055
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3056

    
3057
  if filter_master:
3058
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3059

    
3060
  if qfilter:
3061
    if len(qfilter) > 1:
3062
      final_filter = [qlang.OP_AND] + qfilter
3063
    else:
3064
      assert len(qfilter) == 1
3065
      final_filter = qfilter[0]
3066
  else:
3067
    final_filter = None
3068

    
3069
  result = cl.Query(constants.QR_NODE, ["name", "offline", "sip"], final_filter)
3070

    
3071
  def _IsOffline(row):
3072
    (_, (_, offline), _) = row
3073
    return offline
3074

    
3075
  def _GetName(row):
3076
    ((_, name), _, _) = row
3077
    return name
3078

    
3079
  def _GetSip(row):
3080
    (_, _, (_, sip)) = row
3081
    return sip
3082

    
3083
  (offline, online) = compat.partition(result.data, _IsOffline)
3084

    
3085
  if offline and not nowarn:
3086
    ToStderr("Note: skipping offline node(s): %s" %
3087
             utils.CommaJoin(map(_GetName, offline)))
3088

    
3089
  if secondary_ips:
3090
    fn = _GetSip
3091
  else:
3092
    fn = _GetName
3093

    
3094
  return map(fn, online)
3095

    
3096

    
3097
def _ToStream(stream, txt, *args):
3098
  """Write a message to a stream, bypassing the logging system
3099

3100
  @type stream: file object
3101
  @param stream: the file to which we should write
3102
  @type txt: str
3103
  @param txt: the message
3104

3105
  """
3106
  try:
3107
    if args:
3108
      args = tuple(args)
3109
      stream.write(txt % args)
3110
    else:
3111
      stream.write(txt)
3112
    stream.write("\n")
3113
    stream.flush()
3114
  except IOError, err:
3115
    if err.errno == errno.EPIPE:
3116
      # our terminal went away, we'll exit
3117
      sys.exit(constants.EXIT_FAILURE)
3118
    else:
3119
      raise
3120

    
3121

    
3122
def ToStdout(txt, *args):
3123
  """Write a message to stdout only, bypassing the logging system
3124

3125
  This is just a wrapper over _ToStream.
3126

3127
  @type txt: str
3128
  @param txt: the message
3129

3130
  """
3131
  _ToStream(sys.stdout, txt, *args)
3132

    
3133

    
3134
def ToStderr(txt, *args):
3135
  """Write a message to stderr only, bypassing the logging system
3136

3137
  This is just a wrapper over _ToStream.
3138

3139
  @type txt: str
3140
  @param txt: the message
3141

3142
  """
3143
  _ToStream(sys.stderr, txt, *args)
3144

    
3145

    
3146
class JobExecutor(object):
3147
  """Class which manages the submission and execution of multiple jobs.
3148

3149
  Note that instances of this class should not be reused between
3150
  GetResults() calls.
3151

3152
  """
3153
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3154
    self.queue = []
3155
    if cl is None:
3156
      cl = GetClient()
3157
    self.cl = cl
3158
    self.verbose = verbose
3159
    self.jobs = []
3160
    self.opts = opts
3161
    self.feedback_fn = feedback_fn
3162
    self._counter = itertools.count()
3163

    
3164
  @staticmethod
3165
  def _IfName(name, fmt):
3166
    """Helper function for formatting name.
3167

3168
    """
3169
    if name:
3170
      return fmt % name
3171

    
3172
    return ""
3173

    
3174
  def QueueJob(self, name, *ops):
3175
    """Record a job for later submit.
3176

3177
    @type name: string
3178
    @param name: a description of the job, will be used in WaitJobSet
3179

3180
    """
3181
    SetGenericOpcodeOpts(ops, self.opts)
3182
    self.queue.append((self._counter.next(), name, ops))
3183

    
3184
  def AddJobId(self, name, status, job_id):
3185
    """Adds a job ID to the internal queue.
3186

3187
    """
3188
    self.jobs.append((self._counter.next(), status, job_id, name))
3189

    
3190
  def SubmitPending(self, each=False):
3191
    """Submit all pending jobs.
3192

3193
    """
3194
    if each:
3195
      results = []
3196
      for (_, _, ops) in self.queue:
3197
        # SubmitJob will remove the success status, but raise an exception if
3198
        # the submission fails, so we'll notice that anyway.
3199
        results.append([True, self.cl.SubmitJob(ops)[0]])
3200
    else:
3201
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3202
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3203
      self.jobs.append((idx, status, data, name))
3204

    
3205
  def _ChooseJob(self):
3206
    """Choose a non-waiting/queued job to poll next.
3207

3208
    """
3209
    assert self.jobs, "_ChooseJob called with empty job list"
3210

    
3211
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3212
                               ["status"])
3213
    assert result
3214

    
3215
    for job_data, status in zip(self.jobs, result):
3216
      if (isinstance(status, list) and status and
3217
          status[0] in (constants.JOB_STATUS_QUEUED,
3218
                        constants.JOB_STATUS_WAITING,
3219
                        constants.JOB_STATUS_CANCELING)):
3220
        # job is still present and waiting
3221
        continue
3222
      # good candidate found (either running job or lost job)
3223
      self.jobs.remove(job_data)
3224
      return job_data
3225

    
3226
    # no job found
3227
    return self.jobs.pop(0)
3228

    
3229
  def GetResults(self):
3230
    """Wait for and return the results of all jobs.
3231

3232
    @rtype: list
3233
    @return: list of tuples (success, job results), in the same order
3234
        as the submitted jobs; if a job has failed, instead of the result
3235
        there will be the error message
3236

3237
    """
3238
    if not self.jobs:
3239
      self.SubmitPending()
3240
    results = []
3241
    if self.verbose:
3242
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3243
      if ok_jobs:
3244
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3245

    
3246
    # first, remove any non-submitted jobs
3247
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3248
    for idx, _, jid, name in failures:
3249
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3250
      results.append((idx, False, jid))
3251

    
3252
    while self.jobs:
3253
      (idx, _, jid, name) = self._ChooseJob()
3254
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3255
      try:
3256
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3257
        success = True
3258
      except errors.JobLost, err:
3259
        _, job_result = FormatError(err)
3260
        ToStderr("Job %s%s has been archived, cannot check its result",
3261
                 jid, self._IfName(name, " for %s"))
3262
        success = False
3263
      except (errors.GenericError, luxi.ProtocolError), err:
3264
        _, job_result = FormatError(err)
3265
        success = False
3266
        # the error message will always be shown, verbose or not
3267
        ToStderr("Job %s%s has failed: %s",
3268
                 jid, self._IfName(name, " for %s"), job_result)
3269

    
3270
      results.append((idx, success, job_result))
3271

    
3272
    # sort based on the index, then drop it
3273
    results.sort()
3274
    results = [i[1:] for i in results]
3275

    
3276
    return results
3277

    
3278
  def WaitOrShow(self, wait):
3279
    """Wait for job results or only print the job IDs.
3280

3281
    @type wait: boolean
3282
    @param wait: whether to wait or not
3283

3284
    """
3285
    if wait:
3286
      return self.GetResults()
3287
    else:
3288
      if not self.jobs:
3289
        self.SubmitPending()
3290
      for _, status, result, name in self.jobs:
3291
        if status:
3292
          ToStdout("%s: %s", result, name)
3293
        else:
3294
          ToStderr("Failure for %s: %s", name, result)
3295
      return [row[1:3] for row in self.jobs]
3296

    
3297

    
3298
def FormatParameterDict(buf, param_dict, actual, level=1):
3299
  """Formats a parameter dictionary.
3300

3301
  @type buf: L{StringIO}
3302
  @param buf: the buffer into which to write
3303
  @type param_dict: dict
3304
  @param param_dict: the own parameters
3305
  @type actual: dict
3306
  @param actual: the current parameter set (including defaults)
3307
  @param level: Level of indent
3308

3309
  """
3310
  indent = "  " * level
3311
  for key in sorted(actual):
3312
    val = param_dict.get(key, "default (%s)" % actual[key])
3313
    buf.write("%s- %s: %s\n" % (indent, key, val))
3314

    
3315

    
3316
def ConfirmOperation(names, list_type, text, extra=""):
3317
  """Ask the user to confirm an operation on a list of list_type.
3318

3319
  This function is used to request confirmation for doing an operation
3320
  on a given list of list_type.
3321

3322
  @type names: list
3323
  @param names: the list of names that we display when
3324
      we ask for confirmation
3325
  @type list_type: str
3326
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3327
  @type text: str
3328
  @param text: the operation that the user should confirm
3329
  @rtype: boolean
3330
  @return: True or False depending on user's confirmation.
3331

3332
  """
3333
  count = len(names)
3334
  msg = ("The %s will operate on %d %s.\n%s"
3335
         "Do you want to continue?" % (text, count, list_type, extra))
3336
  affected = (("\nAffected %s:\n" % list_type) +
3337
              "\n".join(["  %s" % name for name in names]))
3338

    
3339
  choices = [("y", True, "Yes, execute the %s" % text),
3340
             ("n", False, "No, abort the %s" % text)]
3341

    
3342
  if count > 20:
3343
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3344
    question = msg
3345
  else:
3346
    question = msg + affected
3347

    
3348
  choice = AskUser(question, choices)
3349
  if choice == "v":
3350
    choices.pop(1)
3351
    choice = AskUser(msg + affected, choices)
3352
  return choice