Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 31d3b918

History | View | Annotate | Download (140.7 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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
import ganeti.rpc.errors as rpcerr
40
import ganeti.rpc.node as rpc
41
from ganeti import ssh
42
from ganeti import compat
43
from ganeti import netutils
44
from ganeti import qlang
45
from ganeti import objects
46
from ganeti import pathutils
47
from ganeti import serializer
48

    
49
from ganeti.runtime import (GetClient)
50

    
51
from optparse import (OptionParser, TitledHelpFormatter,
52
                      Option, OptionValueError)
53

    
54

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

    
308
NO_PREFIX = "no_"
309
UN_PREFIX = "-"
310

    
311
#: Priorities (sorted)
312
_PRIORITY_NAMES = [
313
  ("low", constants.OP_PRIO_LOW),
314
  ("normal", constants.OP_PRIO_NORMAL),
315
  ("high", constants.OP_PRIO_HIGH),
316
  ]
317

    
318
#: Priority dictionary for easier lookup
319
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
320
# we migrate to Python 2.6
321
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
322

    
323
# Query result status for clients
324
(QR_NORMAL,
325
 QR_UNKNOWN,
326
 QR_INCOMPLETE) = range(3)
327

    
328
#: Maximum batch size for ChooseJob
329
_CHOOSE_BATCH = 25
330

    
331

    
332
# constants used to create InstancePolicy dictionary
333
TISPECS_GROUP_TYPES = {
334
  constants.ISPECS_MIN: constants.VTYPE_INT,
335
  constants.ISPECS_MAX: constants.VTYPE_INT,
336
  }
337

    
338
TISPECS_CLUSTER_TYPES = {
339
  constants.ISPECS_MIN: constants.VTYPE_INT,
340
  constants.ISPECS_MAX: constants.VTYPE_INT,
341
  constants.ISPECS_STD: constants.VTYPE_INT,
342
  }
343

    
344
#: User-friendly names for query2 field types
345
_QFT_NAMES = {
346
  constants.QFT_UNKNOWN: "Unknown",
347
  constants.QFT_TEXT: "Text",
348
  constants.QFT_BOOL: "Boolean",
349
  constants.QFT_NUMBER: "Number",
350
  constants.QFT_UNIT: "Storage size",
351
  constants.QFT_TIMESTAMP: "Timestamp",
352
  constants.QFT_OTHER: "Custom",
353
  }
354

    
355

    
356
class _Argument:
357
  def __init__(self, min=0, max=None): # pylint: disable=W0622
358
    self.min = min
359
    self.max = max
360

    
361
  def __repr__(self):
362
    return ("<%s min=%s max=%s>" %
363
            (self.__class__.__name__, self.min, self.max))
364

    
365

    
366
class ArgSuggest(_Argument):
367
  """Suggesting argument.
368

369
  Value can be any of the ones passed to the constructor.
370

371
  """
372
  # pylint: disable=W0622
373
  def __init__(self, min=0, max=None, choices=None):
374
    _Argument.__init__(self, min=min, max=max)
375
    self.choices = choices
376

    
377
  def __repr__(self):
378
    return ("<%s min=%s max=%s choices=%r>" %
379
            (self.__class__.__name__, self.min, self.max, self.choices))
380

    
381

    
382
class ArgChoice(ArgSuggest):
383
  """Choice argument.
384

385
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
386
  but value must be one of the choices.
387

388
  """
389

    
390

    
391
class ArgUnknown(_Argument):
392
  """Unknown argument to program (e.g. determined at runtime).
393

394
  """
395

    
396

    
397
class ArgInstance(_Argument):
398
  """Instances argument.
399

400
  """
401

    
402

    
403
class ArgNode(_Argument):
404
  """Node argument.
405

406
  """
407

    
408

    
409
class ArgNetwork(_Argument):
410
  """Network argument.
411

412
  """
413

    
414

    
415
class ArgGroup(_Argument):
416
  """Node group argument.
417

418
  """
419

    
420

    
421
class ArgJobId(_Argument):
422
  """Job ID argument.
423

424
  """
425

    
426

    
427
class ArgFile(_Argument):
428
  """File path argument.
429

430
  """
431

    
432

    
433
class ArgCommand(_Argument):
434
  """Command argument.
435

436
  """
437

    
438

    
439
class ArgHost(_Argument):
440
  """Host argument.
441

442
  """
443

    
444

    
445
class ArgOs(_Argument):
446
  """OS argument.
447

448
  """
449

    
450

    
451
class ArgExtStorage(_Argument):
452
  """ExtStorage argument.
453

454
  """
455

    
456

    
457
ARGS_NONE = []
458
ARGS_MANY_INSTANCES = [ArgInstance()]
459
ARGS_MANY_NETWORKS = [ArgNetwork()]
460
ARGS_MANY_NODES = [ArgNode()]
461
ARGS_MANY_GROUPS = [ArgGroup()]
462
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
463
ARGS_ONE_NETWORK = [ArgNetwork(min=1, max=1)]
464
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
465
# TODO
466
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
467
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
468

    
469

    
470
def _ExtractTagsObject(opts, args):
471
  """Extract the tag type object.
472

473
  Note that this function will modify its args parameter.
474

475
  """
476
  if not hasattr(opts, "tag_type"):
477
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
478
  kind = opts.tag_type
479
  if kind == constants.TAG_CLUSTER:
480
    retval = kind, ""
481
  elif kind in (constants.TAG_NODEGROUP,
482
                constants.TAG_NODE,
483
                constants.TAG_NETWORK,
484
                constants.TAG_INSTANCE):
485
    if not args:
486
      raise errors.OpPrereqError("no arguments passed to the command",
487
                                 errors.ECODE_INVAL)
488
    name = args.pop(0)
489
    retval = kind, name
490
  else:
491
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
492
  return retval
493

    
494

    
495
def _ExtendTags(opts, args):
496
  """Extend the args if a source file has been given.
497

498
  This function will extend the tags with the contents of the file
499
  passed in the 'tags_source' attribute of the opts parameter. A file
500
  named '-' will be replaced by stdin.
501

502
  """
503
  fname = opts.tags_source
504
  if fname is None:
505
    return
506
  if fname == "-":
507
    new_fh = sys.stdin
508
  else:
509
    new_fh = open(fname, "r")
510
  new_data = []
511
  try:
512
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
513
    # because of python bug 1633941
514
    while True:
515
      line = new_fh.readline()
516
      if not line:
517
        break
518
      new_data.append(line.strip())
519
  finally:
520
    new_fh.close()
521
  args.extend(new_data)
522

    
523

    
524
def ListTags(opts, args):
525
  """List the tags on a given object.
526

527
  This is a generic implementation that knows how to deal with all
528
  three cases of tag objects (cluster, node, instance). The opts
529
  argument is expected to contain a tag_type field denoting what
530
  object type we work on.
531

532
  """
533
  kind, name = _ExtractTagsObject(opts, args)
534
  cl = GetClient(query=True)
535
  result = cl.QueryTags(kind, name)
536
  result = list(result)
537
  result.sort()
538
  for tag in result:
539
    ToStdout(tag)
540

    
541

    
542
def AddTags(opts, args):
543
  """Add tags on a given object.
544

545
  This is a generic implementation that knows how to deal with all
546
  three cases of tag objects (cluster, node, instance). The opts
547
  argument is expected to contain a tag_type field denoting what
548
  object type we work on.
549

550
  """
551
  kind, name = _ExtractTagsObject(opts, args)
552
  _ExtendTags(opts, args)
553
  if not args:
554
    raise errors.OpPrereqError("No tags to be added", errors.ECODE_INVAL)
555
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
556
  SubmitOrSend(op, opts)
557

    
558

    
559
def RemoveTags(opts, args):
560
  """Remove tags from a given object.
561

562
  This is a generic implementation that knows how to deal with all
563
  three cases of tag objects (cluster, node, instance). The opts
564
  argument is expected to contain a tag_type field denoting what
565
  object type we work on.
566

567
  """
568
  kind, name = _ExtractTagsObject(opts, args)
569
  _ExtendTags(opts, args)
570
  if not args:
571
    raise errors.OpPrereqError("No tags to be removed", errors.ECODE_INVAL)
572
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
573
  SubmitOrSend(op, opts)
574

    
575

    
576
def check_unit(option, opt, value): # pylint: disable=W0613
577
  """OptParsers custom converter for units.
578

579
  """
580
  try:
581
    return utils.ParseUnit(value)
582
  except errors.UnitParseError, err:
583
    raise OptionValueError("option %s: %s" % (opt, err))
584

    
585

    
586
def _SplitKeyVal(opt, data, parse_prefixes):
587
  """Convert a KeyVal string into a dict.
588

589
  This function will convert a key=val[,...] string into a dict. Empty
590
  values will be converted specially: keys which have the prefix 'no_'
591
  will have the value=False and the prefix stripped, keys with the prefix
592
  "-" will have value=None and the prefix stripped, and the others will
593
  have value=True.
594

595
  @type opt: string
596
  @param opt: a string holding the option name for which we process the
597
      data, used in building error messages
598
  @type data: string
599
  @param data: a string of the format key=val,key=val,...
600
  @type parse_prefixes: bool
601
  @param parse_prefixes: whether to handle prefixes specially
602
  @rtype: dict
603
  @return: {key=val, key=val}
604
  @raises errors.ParameterError: if there are duplicate keys
605

606
  """
607
  kv_dict = {}
608
  if data:
609
    for elem in utils.UnescapeAndSplit(data, sep=","):
610
      if "=" in elem:
611
        key, val = elem.split("=", 1)
612
      elif parse_prefixes:
613
        if elem.startswith(NO_PREFIX):
614
          key, val = elem[len(NO_PREFIX):], False
615
        elif elem.startswith(UN_PREFIX):
616
          key, val = elem[len(UN_PREFIX):], None
617
        else:
618
          key, val = elem, True
619
      else:
620
        raise errors.ParameterError("Missing value for key '%s' in option %s" %
621
                                    (elem, opt))
622
      if key in kv_dict:
623
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
624
                                    (key, opt))
625
      kv_dict[key] = val
626
  return kv_dict
627

    
628

    
629
def _SplitIdentKeyVal(opt, value, parse_prefixes):
630
  """Helper function to parse "ident:key=val,key=val" options.
631

632
  @type opt: string
633
  @param opt: option name, used in error messages
634
  @type value: string
635
  @param value: expected to be in the format "ident:key=val,key=val,..."
636
  @type parse_prefixes: bool
637
  @param parse_prefixes: whether to handle prefixes specially (see
638
      L{_SplitKeyVal})
639
  @rtype: tuple
640
  @return: (ident, {key=val, key=val})
641
  @raises errors.ParameterError: in case of duplicates or other parsing errors
642

643
  """
644
  if ":" not in value:
645
    ident, rest = value, ""
646
  else:
647
    ident, rest = value.split(":", 1)
648

    
649
  if parse_prefixes and ident.startswith(NO_PREFIX):
650
    if rest:
651
      msg = "Cannot pass options when removing parameter groups: %s" % value
652
      raise errors.ParameterError(msg)
653
    retval = (ident[len(NO_PREFIX):], False)
654
  elif (parse_prefixes and ident.startswith(UN_PREFIX) and
655
        (len(ident) <= len(UN_PREFIX) or not ident[len(UN_PREFIX)].isdigit())):
656
    if rest:
657
      msg = "Cannot pass options when removing parameter groups: %s" % value
658
      raise errors.ParameterError(msg)
659
    retval = (ident[len(UN_PREFIX):], None)
660
  else:
661
    kv_dict = _SplitKeyVal(opt, rest, parse_prefixes)
662
    retval = (ident, kv_dict)
663
  return retval
664

    
665

    
666
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
667
  """Custom parser for ident:key=val,key=val options.
668

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

672
  """
673
  return _SplitIdentKeyVal(opt, value, True)
674

    
675

    
676
def check_key_val(option, opt, value):  # pylint: disable=W0613
677
  """Custom parser class for key=val,key=val options.
678

679
  This will store the parsed values as a dict {key: val}.
680

681
  """
682
  return _SplitKeyVal(opt, value, True)
683

    
684

    
685
def check_key_private_val(option, opt, value):  # pylint: disable=W0613
686
  """Custom parser class for private and secret key=val,key=val options.
687

688
  This will store the parsed values as a dict {key: val}.
689

690
  """
691
  return serializer.PrivateDict(_SplitKeyVal(opt, value, True))
692

    
693

    
694
def _SplitListKeyVal(opt, value):
695
  retval = {}
696
  for elem in value.split("/"):
697
    if not elem:
698
      raise errors.ParameterError("Empty section in option '%s'" % opt)
699
    (ident, valdict) = _SplitIdentKeyVal(opt, elem, False)
700
    if ident in retval:
701
      msg = ("Duplicated parameter '%s' in parsing %s: %s" %
702
             (ident, opt, elem))
703
      raise errors.ParameterError(msg)
704
    retval[ident] = valdict
705
  return retval
706

    
707

    
708
def check_multilist_ident_key_val(_, opt, value):
709
  """Custom parser for "ident:key=val,key=val/ident:key=val//ident:.." options.
710

711
  @rtype: list of dictionary
712
  @return: [{ident: {key: val, key: val}, ident: {key: val}}, {ident:..}]
713

714
  """
715
  retval = []
716
  for line in value.split("//"):
717
    retval.append(_SplitListKeyVal(opt, line))
718
  return retval
719

    
720

    
721
def check_bool(option, opt, value): # pylint: disable=W0613
722
  """Custom parser for yes/no options.
723

724
  This will store the parsed value as either True or False.
725

726
  """
727
  value = value.lower()
728
  if value == constants.VALUE_FALSE or value == "no":
729
    return False
730
  elif value == constants.VALUE_TRUE or value == "yes":
731
    return True
732
  else:
733
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
734

    
735

    
736
def check_list(option, opt, value): # pylint: disable=W0613
737
  """Custom parser for comma-separated lists.
738

739
  """
740
  # we have to make this explicit check since "".split(",") is [""],
741
  # not an empty list :(
742
  if not value:
743
    return []
744
  else:
745
    return utils.UnescapeAndSplit(value)
746

    
747

    
748
def check_maybefloat(option, opt, value): # pylint: disable=W0613
749
  """Custom parser for float numbers which might be also defaults.
750

751
  """
752
  value = value.lower()
753

    
754
  if value == constants.VALUE_DEFAULT:
755
    return value
756
  else:
757
    return float(value)
758

    
759

    
760
# completion_suggestion is normally a list. Using numeric values not evaluating
761
# to False for dynamic completion.
762
(OPT_COMPL_MANY_NODES,
763
 OPT_COMPL_ONE_NODE,
764
 OPT_COMPL_ONE_INSTANCE,
765
 OPT_COMPL_ONE_OS,
766
 OPT_COMPL_ONE_EXTSTORAGE,
767
 OPT_COMPL_ONE_IALLOCATOR,
768
 OPT_COMPL_ONE_NETWORK,
769
 OPT_COMPL_INST_ADD_NODES,
770
 OPT_COMPL_ONE_NODEGROUP) = range(100, 109)
771

    
772
OPT_COMPL_ALL = compat.UniqueFrozenset([
773
  OPT_COMPL_MANY_NODES,
774
  OPT_COMPL_ONE_NODE,
775
  OPT_COMPL_ONE_INSTANCE,
776
  OPT_COMPL_ONE_OS,
777
  OPT_COMPL_ONE_EXTSTORAGE,
778
  OPT_COMPL_ONE_IALLOCATOR,
779
  OPT_COMPL_ONE_NETWORK,
780
  OPT_COMPL_INST_ADD_NODES,
781
  OPT_COMPL_ONE_NODEGROUP,
782
  ])
783

    
784

    
785
class CliOption(Option):
786
  """Custom option class for optparse.
787

788
  """
789
  ATTRS = Option.ATTRS + [
790
    "completion_suggest",
791
    ]
792
  TYPES = Option.TYPES + (
793
    "multilistidentkeyval",
794
    "identkeyval",
795
    "keyval",
796
    "keyprivateval",
797
    "unit",
798
    "bool",
799
    "list",
800
    "maybefloat",
801
    )
802
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
803
  TYPE_CHECKER["multilistidentkeyval"] = check_multilist_ident_key_val
804
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
805
  TYPE_CHECKER["keyval"] = check_key_val
806
  TYPE_CHECKER["keyprivateval"] = check_key_private_val
807
  TYPE_CHECKER["unit"] = check_unit
808
  TYPE_CHECKER["bool"] = check_bool
809
  TYPE_CHECKER["list"] = check_list
810
  TYPE_CHECKER["maybefloat"] = check_maybefloat
811

    
812

    
813
# optparse.py sets make_option, so we do it for our own option class, too
814
cli_option = CliOption
815

    
816

    
817
_YORNO = "yes|no"
818

    
819
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
820
                       help="Increase debugging level")
821

    
822
NOHDR_OPT = cli_option("--no-headers", default=False,
823
                       action="store_true", dest="no_headers",
824
                       help="Don't display column headers")
825

    
826
SEP_OPT = cli_option("--separator", default=None,
827
                     action="store", dest="separator",
828
                     help=("Separator between output fields"
829
                           " (defaults to one space)"))
830

    
831
USEUNITS_OPT = cli_option("--units", default=None,
832
                          dest="units", choices=("h", "m", "g", "t"),
833
                          help="Specify units for output (one of h/m/g/t)")
834

    
835
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
836
                        type="string", metavar="FIELDS",
837
                        help="Comma separated list of output fields")
838

    
839
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
840
                       default=False, help="Force the operation")
841

    
842
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
843
                         default=False, help="Do not require confirmation")
844

    
845
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
846
                                  action="store_true", default=False,
847
                                  help=("Ignore offline nodes and do as much"
848
                                        " as possible"))
849

    
850
TAG_ADD_OPT = cli_option("--tags", dest="tags",
851
                         default=None, help="Comma-separated list of instance"
852
                                            " tags")
853

    
854
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
855
                         default=None, help="File with tag names")
856

    
857
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
858
                        default=False, action="store_true",
859
                        help=("Submit the job and return the job ID, but"
860
                              " don't wait for the job to finish"))
861

    
862
PRINT_JOBID_OPT = cli_option("--print-jobid", dest="print_jobid",
863
                             default=False, action="store_true",
864
                             help=("Additionally print the job as first line"
865
                                   " on stdout (for scripting)."))
866

    
867
SYNC_OPT = cli_option("--sync", dest="do_locking",
868
                      default=False, action="store_true",
869
                      help=("Grab locks while doing the queries"
870
                            " in order to ensure more consistent results"))
871

    
872
DRY_RUN_OPT = cli_option("--dry-run", default=False,
873
                         action="store_true",
874
                         help=("Do not execute the operation, just run the"
875
                               " check steps and verify if it could be"
876
                               " executed"))
877

    
878
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
879
                         action="store_true",
880
                         help="Increase the verbosity of the operation")
881

    
882
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
883
                              action="store_true", dest="simulate_errors",
884
                              help="Debugging option that makes the operation"
885
                              " treat most runtime checks as failed")
886

    
887
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
888
                        default=True, action="store_false",
889
                        help="Don't wait for sync (DANGEROUS!)")
890

    
891
WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync",
892
                        default=False, action="store_true",
893
                        help="Wait for disks to sync")
894

    
895
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
896
                             action="store_true", default=False,
897
                             help="Enable offline instance")
898

    
899
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
900
                              action="store_true", default=False,
901
                              help="Disable down instance")
902

    
903
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
904
                               help=("Custom disk setup (%s)" %
905
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
906
                               default=None, metavar="TEMPL",
907
                               choices=list(constants.DISK_TEMPLATES))
908

    
909
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
910
                        help="Do not create any network cards for"
911
                        " the instance")
912

    
913
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
914
                               help="Relative path under default cluster-wide"
915
                               " file storage dir to store file-based disks",
916
                               default=None, metavar="<DIR>")
917

    
918
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
919
                                  help="Driver to use for image files",
920
                                  default=None, metavar="<DRIVER>",
921
                                  choices=list(constants.FILE_DRIVER))
922

    
923
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
924
                            help="Select nodes for the instance automatically"
925
                            " using the <NAME> iallocator plugin",
926
                            default=None, type="string",
927
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
928

    
929
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
930
                                    metavar="<NAME>",
931
                                    help="Set the default instance"
932
                                    " allocator plugin",
933
                                    default=None, type="string",
934
                                    completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
935

    
936
DEFAULT_IALLOCATOR_PARAMS_OPT = cli_option("--default-iallocator-params",
937
                                           dest="default_iallocator_params",
938
                                           help="iallocator template"
939
                                           " parameters, in the format"
940
                                           " template:option=value,"
941
                                           " option=value,...",
942
                                           type="keyval",
943
                                           default={})
944

    
945
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
946
                    metavar="<os>",
947
                    completion_suggest=OPT_COMPL_ONE_OS)
948

    
949
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
950
                          type="keyval", default={},
951
                          help="OS parameters")
952

    
953
OSPARAMS_PRIVATE_OPT = cli_option("--os-parameters-private",
954
                                  dest="osparams_private",
955
                                  type="keyprivateval",
956
                                  default=serializer.PrivateDict(),
957
                                  help="Private OS parameters"
958
                                       " (won't be logged)")
959

    
960
OSPARAMS_SECRET_OPT = cli_option("--os-parameters-secret",
961
                                 dest="osparams_secret",
962
                                 type="keyprivateval",
963
                                 default=serializer.PrivateDict(),
964
                                 help="Secret OS parameters (won't be logged or"
965
                                      " saved; you must supply these for every"
966
                                      " operation.)")
967

    
968
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
969
                               action="store_true", default=False,
970
                               help="Force an unknown variant")
971

    
972
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
973
                            action="store_true", default=False,
974
                            help="Do not install the OS (will"
975
                            " enable no-start)")
976

    
977
NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes",
978
                                dest="allow_runtime_chgs",
979
                                default=True, action="store_false",
980
                                help="Don't allow runtime changes")
981

    
982
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
983
                         type="keyval", default={},
984
                         help="Backend parameters")
985

    
986
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
987
                        default={}, dest="hvparams",
988
                        help="Hypervisor parameters")
989

    
990
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
991
                             help="Disk template parameters, in the format"
992
                             " template:option=value,option=value,...",
993
                             type="identkeyval", action="append", default=[])
994

    
995
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
996
                                 type="keyval", default={},
997
                                 help="Memory size specs: list of key=value,"
998
                                " where key is one of min, max, std"
999
                                 " (in MB or using a unit)")
1000

    
1001
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
1002
                                 type="keyval", default={},
1003
                                 help="CPU count specs: list of key=value,"
1004
                                 " where key is one of min, max, std")
1005

    
1006
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
1007
                                  dest="ispecs_disk_count",
1008
                                  type="keyval", default={},
1009
                                  help="Disk count specs: list of key=value,"
1010
                                  " where key is one of min, max, std")
1011

    
1012
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
1013
                                 type="keyval", default={},
1014
                                 help="Disk size specs: list of key=value,"
1015
                                 " where key is one of min, max, std"
1016
                                 " (in MB or using a unit)")
1017

    
1018
SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
1019
                                 type="keyval", default={},
1020
                                 help="NIC count specs: list of key=value,"
1021
                                 " where key is one of min, max, std")
1022

    
1023
IPOLICY_BOUNDS_SPECS_STR = "--ipolicy-bounds-specs"
1024
IPOLICY_BOUNDS_SPECS_OPT = cli_option(IPOLICY_BOUNDS_SPECS_STR,
1025
                                      dest="ipolicy_bounds_specs",
1026
                                      type="multilistidentkeyval", default=None,
1027
                                      help="Complete instance specs limits")
1028

    
1029
IPOLICY_STD_SPECS_STR = "--ipolicy-std-specs"
1030
IPOLICY_STD_SPECS_OPT = cli_option(IPOLICY_STD_SPECS_STR,
1031
                                   dest="ipolicy_std_specs",
1032
                                   type="keyval", default=None,
1033
                                   help="Complte standard instance specs")
1034

    
1035
IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
1036
                                    dest="ipolicy_disk_templates",
1037
                                    type="list", default=None,
1038
                                    help="Comma-separated list of"
1039
                                    " enabled disk templates")
1040

    
1041
IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio",
1042
                                 dest="ipolicy_vcpu_ratio",
1043
                                 type="maybefloat", default=None,
1044
                                 help="The maximum allowed vcpu-to-cpu ratio")
1045

    
1046
IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio",
1047
                                   dest="ipolicy_spindle_ratio",
1048
                                   type="maybefloat", default=None,
1049
                                   help=("The maximum allowed instances to"
1050
                                         " spindle ratio"))
1051

    
1052
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
1053
                            help="Hypervisor and hypervisor options, in the"
1054
                            " format hypervisor:option=value,option=value,...",
1055
                            default=None, type="identkeyval")
1056

    
1057
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
1058
                        help="Hypervisor and hypervisor options, in the"
1059
                        " format hypervisor:option=value,option=value,...",
1060
                        default=[], action="append", type="identkeyval")
1061

    
1062
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
1063
                           action="store_false",
1064
                           help="Don't check that the instance's IP"
1065
                           " is alive")
1066

    
1067
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
1068
                             default=True, action="store_false",
1069
                             help="Don't check that the instance's name"
1070
                             " is resolvable")
1071

    
1072
NET_OPT = cli_option("--net",
1073
                     help="NIC parameters", default=[],
1074
                     dest="nics", action="append", type="identkeyval")
1075

    
1076
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
1077
                      dest="disks", action="append", type="identkeyval")
1078

    
1079
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
1080
                         help="Comma-separated list of disks"
1081
                         " indices to act on (e.g. 0,2) (optional,"
1082
                         " defaults to all disks)")
1083

    
1084
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
1085
                         help="Enforces a single-disk configuration using the"
1086
                         " given disk size, in MiB unless a suffix is used",
1087
                         default=None, type="unit", metavar="<size>")
1088

    
1089
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
1090
                                dest="ignore_consistency",
1091
                                action="store_true", default=False,
1092
                                help="Ignore the consistency of the disks on"
1093
                                " the secondary")
1094

    
1095
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
1096
                                dest="allow_failover",
1097
                                action="store_true", default=False,
1098
                                help="If migration is not possible fallback to"
1099
                                     " failover")
1100

    
1101
NONLIVE_OPT = cli_option("--non-live", dest="live",
1102
                         default=True, action="store_false",
1103
                         help="Do a non-live migration (this usually means"
1104
                         " freeze the instance, save the state, transfer and"
1105
                         " only then resume running on the secondary node)")
1106

    
1107
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
1108
                                default=None,
1109
                                choices=list(constants.HT_MIGRATION_MODES),
1110
                                help="Override default migration mode (choose"
1111
                                " either live or non-live")
1112

    
1113
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
1114
                                help="Target node and optional secondary node",
1115
                                metavar="<pnode>[:<snode>]",
1116
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
1117

    
1118
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
1119
                           action="append", metavar="<node>",
1120
                           help="Use only this node (can be used multiple"
1121
                           " times, if not given defaults to all nodes)",
1122
                           completion_suggest=OPT_COMPL_ONE_NODE)
1123

    
1124
NODEGROUP_OPT_NAME = "--node-group"
1125
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
1126
                           dest="nodegroup",
1127
                           help="Node group (name or uuid)",
1128
                           metavar="<nodegroup>",
1129
                           default=None, type="string",
1130
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1131

    
1132
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
1133
                             metavar="<node>",
1134
                             completion_suggest=OPT_COMPL_ONE_NODE)
1135

    
1136
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
1137
                         action="store_false",
1138
                         help="Don't start the instance after creation")
1139

    
1140
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
1141
                         action="store_true", default=False,
1142
                         help="Show command instead of executing it")
1143

    
1144
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
1145
                         default=False, action="store_true",
1146
                         help="Instead of performing the migration/failover,"
1147
                         " try to recover from a failed cleanup. This is safe"
1148
                         " to run even if the instance is healthy, but it"
1149
                         " will create extra replication traffic and "
1150
                         " disrupt briefly the replication (like during the"
1151
                         " migration/failover")
1152

    
1153
STATIC_OPT = cli_option("-s", "--static", dest="static",
1154
                        action="store_true", default=False,
1155
                        help="Only show configuration data, not runtime data")
1156

    
1157
ALL_OPT = cli_option("--all", dest="show_all",
1158
                     default=False, action="store_true",
1159
                     help="Show info on all instances on the cluster."
1160
                     " This can take a long time to run, use wisely")
1161

    
1162
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
1163
                           action="store_true", default=False,
1164
                           help="Interactive OS reinstall, lists available"
1165
                           " OS templates for selection")
1166

    
1167
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
1168
                                 action="store_true", default=False,
1169
                                 help="Remove the instance from the cluster"
1170
                                 " configuration even if there are failures"
1171
                                 " during the removal process")
1172

    
1173
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
1174
                                        dest="ignore_remove_failures",
1175
                                        action="store_true", default=False,
1176
                                        help="Remove the instance from the"
1177
                                        " cluster configuration even if there"
1178
                                        " are failures during the removal"
1179
                                        " process")
1180

    
1181
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
1182
                                 action="store_true", default=False,
1183
                                 help="Remove the instance from the cluster")
1184

    
1185
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
1186
                               help="Specifies the new node for the instance",
1187
                               metavar="NODE", default=None,
1188
                               completion_suggest=OPT_COMPL_ONE_NODE)
1189

    
1190
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
1191
                               help="Specifies the new secondary node",
1192
                               metavar="NODE", default=None,
1193
                               completion_suggest=OPT_COMPL_ONE_NODE)
1194

    
1195
NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node",
1196
                             help="Specifies the new primary node",
1197
                             metavar="<node>", default=None,
1198
                             completion_suggest=OPT_COMPL_ONE_NODE)
1199

    
1200
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
1201
                            default=False, action="store_true",
1202
                            help="Replace the disk(s) on the primary"
1203
                                 " node (applies only to internally mirrored"
1204
                                 " disk templates, e.g. %s)" %
1205
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
1206

    
1207
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
1208
                              default=False, action="store_true",
1209
                              help="Replace the disk(s) on the secondary"
1210
                                   " node (applies only to internally mirrored"
1211
                                   " disk templates, e.g. %s)" %
1212
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1213

    
1214
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
1215
                              default=False, action="store_true",
1216
                              help="Lock all nodes and auto-promote as needed"
1217
                              " to MC status")
1218

    
1219
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
1220
                              default=False, action="store_true",
1221
                              help="Automatically replace faulty disks"
1222
                                   " (applies only to internally mirrored"
1223
                                   " disk templates, e.g. %s)" %
1224
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1225

    
1226
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
1227
                             default=False, action="store_true",
1228
                             help="Ignore current recorded size"
1229
                             " (useful for forcing activation when"
1230
                             " the recorded size is wrong)")
1231

    
1232
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
1233
                          metavar="<node>",
1234
                          completion_suggest=OPT_COMPL_ONE_NODE)
1235

    
1236
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
1237
                         metavar="<dir>")
1238

    
1239
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
1240
                              help="Specify the secondary ip for the node",
1241
                              metavar="ADDRESS", default=None)
1242

    
1243
READD_OPT = cli_option("--readd", dest="readd",
1244
                       default=False, action="store_true",
1245
                       help="Readd old node after replacing it")
1246

    
1247
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1248
                                default=True, action="store_false",
1249
                                help="Disable SSH key fingerprint checking")
1250

    
1251
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1252
                                 default=False, action="store_true",
1253
                                 help="Force the joining of a node")
1254

    
1255
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1256
                    type="bool", default=None, metavar=_YORNO,
1257
                    help="Set the master_candidate flag on the node")
1258

    
1259
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1260
                         type="bool", default=None,
1261
                         help=("Set the offline flag on the node"
1262
                               " (cluster does not communicate with offline"
1263
                               " nodes)"))
1264

    
1265
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1266
                         type="bool", default=None,
1267
                         help=("Set the drained flag on the node"
1268
                               " (excluded from allocation operations)"))
1269

    
1270
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1271
                              type="bool", default=None, metavar=_YORNO,
1272
                              help="Set the master_capable flag on the node")
1273

    
1274
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1275
                          type="bool", default=None, metavar=_YORNO,
1276
                          help="Set the vm_capable flag on the node")
1277

    
1278
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1279
                             type="bool", default=None, metavar=_YORNO,
1280
                             help="Set the allocatable flag on a volume")
1281

    
1282
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1283
                            dest="enabled_hypervisors",
1284
                            help="Comma-separated list of hypervisors",
1285
                            type="string", default=None)
1286

    
1287
ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates",
1288
                                        dest="enabled_disk_templates",
1289
                                        help="Comma-separated list of "
1290
                                             "disk templates",
1291
                                        type="string", default=None)
1292

    
1293
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1294
                            type="keyval", default={},
1295
                            help="NIC parameters")
1296

    
1297
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1298
                         dest="candidate_pool_size", type="int",
1299
                         help="Set the candidate pool size")
1300

    
1301
RQL_OPT = cli_option("--max-running-jobs", dest="max_running_jobs",
1302
                     type="int", help="Set the maximal number of jobs to "
1303
                                      "run simultaneously")
1304

    
1305
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1306
                         help=("Enables LVM and specifies the volume group"
1307
                               " name (cluster-wide) for disk allocation"
1308
                               " [%s]" % constants.DEFAULT_VG),
1309
                         metavar="VG", default=None)
1310

    
1311
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1312
                          help="Destroy cluster", action="store_true")
1313

    
1314
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1315
                          help="Skip node agreement check (dangerous)",
1316
                          action="store_true", default=False)
1317

    
1318
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1319
                            help="Specify the mac prefix for the instance IP"
1320
                            " addresses, in the format XX:XX:XX",
1321
                            metavar="PREFIX",
1322
                            default=None)
1323

    
1324
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1325
                               help="Specify the node interface (cluster-wide)"
1326
                               " on which the master IP address will be added"
1327
                               " (cluster init default: %s)" %
1328
                               constants.DEFAULT_BRIDGE,
1329
                               metavar="NETDEV",
1330
                               default=None)
1331

    
1332
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1333
                                help="Specify the netmask of the master IP",
1334
                                metavar="NETMASK",
1335
                                default=None)
1336

    
1337
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1338
                                     dest="use_external_mip_script",
1339
                                     help="Specify whether to run a"
1340
                                     " user-provided script for the master"
1341
                                     " IP address turnup and"
1342
                                     " turndown operations",
1343
                                     type="bool", metavar=_YORNO, default=None)
1344

    
1345
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1346
                                help="Specify the default directory (cluster-"
1347
                                "wide) for storing the file-based disks [%s]" %
1348
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1349
                                metavar="DIR",
1350
                                default=None)
1351

    
1352
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1353
  "--shared-file-storage-dir",
1354
  dest="shared_file_storage_dir",
1355
  help="Specify the default directory (cluster-wide) for storing the"
1356
  " shared file-based disks [%s]" %
1357
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1358
  metavar="SHAREDDIR", default=None)
1359

    
1360
GLOBAL_GLUSTER_FILEDIR_OPT = cli_option(
1361
  "--gluster-storage-dir",
1362
  dest="gluster_storage_dir",
1363
  help="Specify the default directory (cluster-wide) for mounting Gluster"
1364
  " file systems [%s]" %
1365
  pathutils.DEFAULT_GLUSTER_STORAGE_DIR,
1366
  metavar="GLUSTERDIR",
1367
  default=pathutils.DEFAULT_GLUSTER_STORAGE_DIR)
1368

    
1369
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1370
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1371
                                   action="store_false", default=True)
1372

    
1373
MODIFY_ETCHOSTS_OPT = \
1374
 cli_option("--modify-etc-hosts", dest="modify_etc_hosts", metavar=_YORNO,
1375
            default=None, type="bool",
1376
            help="Defines whether the cluster should autonomously modify"
1377
            " and keep in sync the /etc/hosts file of the nodes")
1378

    
1379
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1380
                                    help="Don't initialize SSH keys",
1381
                                    action="store_false", default=True)
1382

    
1383
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1384
                             help="Enable parseable error messages",
1385
                             action="store_true", default=False)
1386

    
1387
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1388
                          help="Skip N+1 memory redundancy tests",
1389
                          action="store_true", default=False)
1390

    
1391
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1392
                             help="Type of reboot: soft/hard/full",
1393
                             default=constants.INSTANCE_REBOOT_HARD,
1394
                             metavar="<REBOOT>",
1395
                             choices=list(constants.REBOOT_TYPES))
1396

    
1397
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1398
                                    dest="ignore_secondaries",
1399
                                    default=False, action="store_true",
1400
                                    help="Ignore errors from secondaries")
1401

    
1402
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1403
                            action="store_false", default=True,
1404
                            help="Don't shutdown the instance (unsafe)")
1405

    
1406
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1407
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1408
                         help="Maximum time to wait")
1409

    
1410
COMPRESS_OPT = cli_option("--compress", dest="compress",
1411
                          default=constants.IEC_NONE,
1412
                          help="The compression mode to use",
1413
                          choices=list(constants.IEC_ALL))
1414

    
1415
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1416
                                  dest="shutdown_timeout", type="int",
1417
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1418
                                  help="Maximum time to wait for instance"
1419
                                  " shutdown")
1420

    
1421
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1422
                          default=None,
1423
                          help=("Number of seconds between repetions of the"
1424
                                " command"))
1425

    
1426
EARLY_RELEASE_OPT = cli_option("--early-release",
1427
                               dest="early_release", default=False,
1428
                               action="store_true",
1429
                               help="Release the locks on the secondary"
1430
                               " node(s) early")
1431

    
1432
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1433
                                  dest="new_cluster_cert",
1434
                                  default=False, action="store_true",
1435
                                  help="Generate a new cluster certificate")
1436

    
1437
NEW_NODE_CERT_OPT = cli_option(
1438
  "--new-node-certificates", dest="new_node_cert", default=False,
1439
  action="store_true", help="Generate new node certificates (for all nodes)")
1440

    
1441
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1442
                           default=None,
1443
                           help="File containing new RAPI certificate")
1444

    
1445
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1446
                               default=None, action="store_true",
1447
                               help=("Generate a new self-signed RAPI"
1448
                                     " certificate"))
1449

    
1450
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1451
                            default=None,
1452
                            help="File containing new SPICE certificate")
1453

    
1454
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1455
                              default=None,
1456
                              help="File containing the certificate of the CA"
1457
                              " which signed the SPICE certificate")
1458

    
1459
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1460
                                dest="new_spice_cert", default=None,
1461
                                action="store_true",
1462
                                help=("Generate a new self-signed SPICE"
1463
                                      " certificate"))
1464

    
1465
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1466
                                    dest="new_confd_hmac_key",
1467
                                    default=False, action="store_true",
1468
                                    help=("Create a new HMAC key for %s" %
1469
                                          constants.CONFD))
1470

    
1471
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1472
                                       dest="cluster_domain_secret",
1473
                                       default=None,
1474
                                       help=("Load new new cluster domain"
1475
                                             " secret from file"))
1476

    
1477
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1478
                                           dest="new_cluster_domain_secret",
1479
                                           default=False, action="store_true",
1480
                                           help=("Create a new cluster domain"
1481
                                                 " secret"))
1482

    
1483
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1484
                              dest="use_replication_network",
1485
                              help="Whether to use the replication network"
1486
                              " for talking to the nodes",
1487
                              action="store_true", default=False)
1488

    
1489
MAINTAIN_NODE_HEALTH_OPT = \
1490
    cli_option("--maintain-node-health", dest="maintain_node_health",
1491
               metavar=_YORNO, default=None, type="bool",
1492
               help="Configure the cluster to automatically maintain node"
1493
               " health, by shutting down unknown instances, shutting down"
1494
               " unknown DRBD devices, etc.")
1495

    
1496
IDENTIFY_DEFAULTS_OPT = \
1497
    cli_option("--identify-defaults", dest="identify_defaults",
1498
               default=False, action="store_true",
1499
               help="Identify which saved instance parameters are equal to"
1500
               " the current cluster defaults and set them as such, instead"
1501
               " of marking them as overridden")
1502

    
1503
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1504
                         action="store", dest="uid_pool",
1505
                         help=("A list of user-ids or user-id"
1506
                               " ranges separated by commas"))
1507

    
1508
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1509
                          action="store", dest="add_uids",
1510
                          help=("A list of user-ids or user-id"
1511
                                " ranges separated by commas, to be"
1512
                                " added to the user-id pool"))
1513

    
1514
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1515
                             action="store", dest="remove_uids",
1516
                             help=("A list of user-ids or user-id"
1517
                                   " ranges separated by commas, to be"
1518
                                   " removed from the user-id pool"))
1519

    
1520
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1521
                              action="store", dest="reserved_lvs",
1522
                              help=("A comma-separated list of reserved"
1523
                                    " logical volumes names, that will be"
1524
                                    " ignored by cluster verify"))
1525

    
1526
ROMAN_OPT = cli_option("--roman",
1527
                       dest="roman_integers", default=False,
1528
                       action="store_true",
1529
                       help="Use roman numbers for positive integers")
1530

    
1531
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1532
                             action="store", default=None,
1533
                             help="Specifies usermode helper for DRBD")
1534

    
1535
PRIMARY_IP_VERSION_OPT = \
1536
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1537
               action="store", dest="primary_ip_version",
1538
               metavar="%d|%d" % (constants.IP4_VERSION,
1539
                                  constants.IP6_VERSION),
1540
               help="Cluster-wide IP version for primary IP")
1541

    
1542
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1543
                              action="store_true",
1544
                              help="Show machine name for every line in output")
1545

    
1546
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1547
                              action="store_true",
1548
                              help=("Hide successful results and show failures"
1549
                                    " only (determined by the exit code)"))
1550

    
1551
REASON_OPT = cli_option("--reason", default=None,
1552
                        help="The reason for executing the command")
1553

    
1554

    
1555
def _PriorityOptionCb(option, _, value, parser):
1556
  """Callback for processing C{--priority} option.
1557

1558
  """
1559
  value = _PRIONAME_TO_VALUE[value]
1560

    
1561
  setattr(parser.values, option.dest, value)
1562

    
1563

    
1564
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1565
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1566
                          choices=_PRIONAME_TO_VALUE.keys(),
1567
                          action="callback", type="choice",
1568
                          callback=_PriorityOptionCb,
1569
                          help="Priority for opcode processing")
1570

    
1571
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1572
                        type="bool", default=None, metavar=_YORNO,
1573
                        help="Sets the hidden flag on the OS")
1574

    
1575
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1576
                        type="bool", default=None, metavar=_YORNO,
1577
                        help="Sets the blacklisted flag on the OS")
1578

    
1579
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1580
                                     type="bool", metavar=_YORNO,
1581
                                     dest="prealloc_wipe_disks",
1582
                                     help=("Wipe disks prior to instance"
1583
                                           " creation"))
1584

    
1585
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1586
                             type="keyval", default=None,
1587
                             help="Node parameters")
1588

    
1589
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1590
                              action="store", metavar="POLICY", default=None,
1591
                              help="Allocation policy for the node group")
1592

    
1593
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1594
                              type="bool", metavar=_YORNO,
1595
                              dest="node_powered",
1596
                              help="Specify if the SoR for node is powered")
1597

    
1598
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1599
                             default=constants.OOB_TIMEOUT,
1600
                             help="Maximum time to wait for out-of-band helper")
1601

    
1602
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1603
                             default=constants.OOB_POWER_DELAY,
1604
                             help="Time in seconds to wait between power-ons")
1605

    
1606
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1607
                              action="store_true", default=False,
1608
                              help=("Whether command argument should be treated"
1609
                                    " as filter"))
1610

    
1611
NO_REMEMBER_OPT = cli_option("--no-remember",
1612
                             dest="no_remember",
1613
                             action="store_true", default=False,
1614
                             help="Perform but do not record the change"
1615
                             " in the configuration")
1616

    
1617
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1618
                              default=False, action="store_true",
1619
                              help="Evacuate primary instances only")
1620

    
1621
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1622
                                default=False, action="store_true",
1623
                                help="Evacuate secondary instances only"
1624
                                     " (applies only to internally mirrored"
1625
                                     " disk templates, e.g. %s)" %
1626
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1627

    
1628
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1629
                                action="store_true", default=False,
1630
                                help="Pause instance at startup")
1631

    
1632
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1633
                          help="Destination node group (name or uuid)",
1634
                          default=None, action="append",
1635
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1636

    
1637
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1638
                               action="append", dest="ignore_errors",
1639
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1640
                               help="Error code to be ignored")
1641

    
1642
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1643
                            action="append",
1644
                            help=("Specify disk state information in the"
1645
                                  " format"
1646
                                  " storage_type/identifier:option=value,...;"
1647
                                  " note this is unused for now"),
1648
                            type="identkeyval")
1649

    
1650
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1651
                          action="append",
1652
                          help=("Specify hypervisor state information in the"
1653
                                " format hypervisor:option=value,...;"
1654
                                " note this is unused for now"),
1655
                          type="identkeyval")
1656

    
1657
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1658
                                action="store_true", default=False,
1659
                                help="Ignore instance policy violations")
1660

    
1661
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1662
                             help="Sets the instance's runtime memory,"
1663
                             " ballooning it up or down to the new value",
1664
                             default=None, type="unit", metavar="<size>")
1665

    
1666
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1667
                          action="store_true", default=False,
1668
                          help="Marks the grow as absolute instead of the"
1669
                          " (default) relative mode")
1670

    
1671
NETWORK_OPT = cli_option("--network",
1672
                         action="store", default=None, dest="network",
1673
                         help="IP network in CIDR notation")
1674

    
1675
GATEWAY_OPT = cli_option("--gateway",
1676
                         action="store", default=None, dest="gateway",
1677
                         help="IP address of the router (gateway)")
1678

    
1679
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1680
                                  action="store", default=None,
1681
                                  dest="add_reserved_ips",
1682
                                  help="Comma-separated list of"
1683
                                  " reserved IPs to add")
1684

    
1685
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1686
                                     action="store", default=None,
1687
                                     dest="remove_reserved_ips",
1688
                                     help="Comma-delimited list of"
1689
                                     " reserved IPs to remove")
1690

    
1691
NETWORK6_OPT = cli_option("--network6",
1692
                          action="store", default=None, dest="network6",
1693
                          help="IP network in CIDR notation")
1694

    
1695
GATEWAY6_OPT = cli_option("--gateway6",
1696
                          action="store", default=None, dest="gateway6",
1697
                          help="IP6 address of the router (gateway)")
1698

    
1699
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1700
                                  dest="conflicts_check",
1701
                                  default=True,
1702
                                  action="store_false",
1703
                                  help="Don't check for conflicting IPs")
1704

    
1705
INCLUDEDEFAULTS_OPT = cli_option("--include-defaults", dest="include_defaults",
1706
                                 default=False, action="store_true",
1707
                                 help="Include default values")
1708

    
1709
HOTPLUG_OPT = cli_option("--hotplug", dest="hotplug",
1710
                         action="store_true", default=False,
1711
                         help="Hotplug supported devices (NICs and Disks)")
1712

    
1713
HOTPLUG_IF_POSSIBLE_OPT = cli_option("--hotplug-if-possible",
1714
                                     dest="hotplug_if_possible",
1715
                                     action="store_true", default=False,
1716
                                     help="Hotplug devices in case"
1717
                                          " hotplug is supported")
1718

    
1719
INSTANCE_COMMUNICATION_OPT = \
1720
    cli_option("-c", "--communication",
1721
               default=False,
1722
               dest="instance_communication",
1723
               help=constants.INSTANCE_COMMUNICATION_DOC,
1724
               type="bool")
1725

    
1726
#: Options provided by all commands
1727
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1728

    
1729
# options related to asynchronous job handling
1730

    
1731
SUBMIT_OPTS = [
1732
  SUBMIT_OPT,
1733
  PRINT_JOBID_OPT,
1734
  ]
1735

    
1736
# common options for creating instances. add and import then add their own
1737
# specific ones.
1738
COMMON_CREATE_OPTS = [
1739
  BACKEND_OPT,
1740
  DISK_OPT,
1741
  DISK_TEMPLATE_OPT,
1742
  FILESTORE_DIR_OPT,
1743
  FILESTORE_DRIVER_OPT,
1744
  HYPERVISOR_OPT,
1745
  IALLOCATOR_OPT,
1746
  NET_OPT,
1747
  NODE_PLACEMENT_OPT,
1748
  NOIPCHECK_OPT,
1749
  NOCONFLICTSCHECK_OPT,
1750
  NONAMECHECK_OPT,
1751
  NONICS_OPT,
1752
  NWSYNC_OPT,
1753
  OSPARAMS_OPT,
1754
  OS_SIZE_OPT,
1755
  SUBMIT_OPT,
1756
  PRINT_JOBID_OPT,
1757
  TAG_ADD_OPT,
1758
  DRY_RUN_OPT,
1759
  PRIORITY_OPT,
1760
  ]
1761

    
1762
# common instance policy options
1763
INSTANCE_POLICY_OPTS = [
1764
  IPOLICY_BOUNDS_SPECS_OPT,
1765
  IPOLICY_DISK_TEMPLATES,
1766
  IPOLICY_VCPU_RATIO,
1767
  IPOLICY_SPINDLE_RATIO,
1768
  ]
1769

    
1770
# instance policy split specs options
1771
SPLIT_ISPECS_OPTS = [
1772
  SPECS_CPU_COUNT_OPT,
1773
  SPECS_DISK_COUNT_OPT,
1774
  SPECS_DISK_SIZE_OPT,
1775
  SPECS_MEM_SIZE_OPT,
1776
  SPECS_NIC_COUNT_OPT,
1777
  ]
1778

    
1779

    
1780
class _ShowUsage(Exception):
1781
  """Exception class for L{_ParseArgs}.
1782

1783
  """
1784
  def __init__(self, exit_error):
1785
    """Initializes instances of this class.
1786

1787
    @type exit_error: bool
1788
    @param exit_error: Whether to report failure on exit
1789

1790
    """
1791
    Exception.__init__(self)
1792
    self.exit_error = exit_error
1793

    
1794

    
1795
class _ShowVersion(Exception):
1796
  """Exception class for L{_ParseArgs}.
1797

1798
  """
1799

    
1800

    
1801
def _ParseArgs(binary, argv, commands, aliases, env_override):
1802
  """Parser for the command line arguments.
1803

1804
  This function parses the arguments and returns the function which
1805
  must be executed together with its (modified) arguments.
1806

1807
  @param binary: Script name
1808
  @param argv: Command line arguments
1809
  @param commands: Dictionary containing command definitions
1810
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1811
  @param env_override: list of env variables allowed for default args
1812
  @raise _ShowUsage: If usage description should be shown
1813
  @raise _ShowVersion: If version should be shown
1814

1815
  """
1816
  assert not (env_override - set(commands))
1817
  assert not (set(aliases.keys()) & set(commands.keys()))
1818

    
1819
  if len(argv) > 1:
1820
    cmd = argv[1]
1821
  else:
1822
    # No option or command given
1823
    raise _ShowUsage(exit_error=True)
1824

    
1825
  if cmd == "--version":
1826
    raise _ShowVersion()
1827
  elif cmd == "--help":
1828
    raise _ShowUsage(exit_error=False)
1829
  elif not (cmd in commands or cmd in aliases):
1830
    raise _ShowUsage(exit_error=True)
1831

    
1832
  # get command, unalias it, and look it up in commands
1833
  if cmd in aliases:
1834
    if aliases[cmd] not in commands:
1835
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1836
                                   " command '%s'" % (cmd, aliases[cmd]))
1837

    
1838
    cmd = aliases[cmd]
1839

    
1840
  if cmd in env_override:
1841
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1842
    env_args = os.environ.get(args_env_name)
1843
    if env_args:
1844
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1845

    
1846
  func, args_def, parser_opts, usage, description = commands[cmd]
1847
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1848
                        description=description,
1849
                        formatter=TitledHelpFormatter(),
1850
                        usage="%%prog %s %s" % (cmd, usage))
1851
  parser.disable_interspersed_args()
1852
  options, args = parser.parse_args(args=argv[2:])
1853

    
1854
  if not _CheckArguments(cmd, args_def, args):
1855
    return None, None, None
1856

    
1857
  return func, options, args
1858

    
1859

    
1860
def _FormatUsage(binary, commands):
1861
  """Generates a nice description of all commands.
1862

1863
  @param binary: Script name
1864
  @param commands: Dictionary containing command definitions
1865

1866
  """
1867
  # compute the max line length for cmd + usage
1868
  mlen = min(60, max(map(len, commands)))
1869

    
1870
  yield "Usage: %s {command} [options...] [argument...]" % binary
1871
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1872
  yield ""
1873
  yield "Commands:"
1874

    
1875
  # and format a nice command list
1876
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1877
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1878
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1879
    for line in help_lines:
1880
      yield " %-*s   %s" % (mlen, "", line)
1881

    
1882
  yield ""
1883

    
1884

    
1885
def _CheckArguments(cmd, args_def, args):
1886
  """Verifies the arguments using the argument definition.
1887

1888
  Algorithm:
1889

1890
    1. Abort with error if values specified by user but none expected.
1891

1892
    1. For each argument in definition
1893

1894
      1. Keep running count of minimum number of values (min_count)
1895
      1. Keep running count of maximum number of values (max_count)
1896
      1. If it has an unlimited number of values
1897

1898
        1. Abort with error if it's not the last argument in the definition
1899

1900
    1. If last argument has limited number of values
1901

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

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

1906
  """
1907
  if args and not args_def:
1908
    ToStderr("Error: Command %s expects no arguments", cmd)
1909
    return False
1910

    
1911
  min_count = None
1912
  max_count = None
1913
  check_max = None
1914

    
1915
  last_idx = len(args_def) - 1
1916

    
1917
  for idx, arg in enumerate(args_def):
1918
    if min_count is None:
1919
      min_count = arg.min
1920
    elif arg.min is not None:
1921
      min_count += arg.min
1922

    
1923
    if max_count is None:
1924
      max_count = arg.max
1925
    elif arg.max is not None:
1926
      max_count += arg.max
1927

    
1928
    if idx == last_idx:
1929
      check_max = (arg.max is not None)
1930

    
1931
    elif arg.max is None:
1932
      raise errors.ProgrammerError("Only the last argument can have max=None")
1933

    
1934
  if check_max:
1935
    # Command with exact number of arguments
1936
    if (min_count is not None and max_count is not None and
1937
        min_count == max_count and len(args) != min_count):
1938
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1939
      return False
1940

    
1941
    # Command with limited number of arguments
1942
    if max_count is not None and len(args) > max_count:
1943
      ToStderr("Error: Command %s expects only %d argument(s)",
1944
               cmd, max_count)
1945
      return False
1946

    
1947
  # Command with some required arguments
1948
  if min_count is not None and len(args) < min_count:
1949
    ToStderr("Error: Command %s expects at least %d argument(s)",
1950
             cmd, min_count)
1951
    return False
1952

    
1953
  return True
1954

    
1955

    
1956
def SplitNodeOption(value):
1957
  """Splits the value of a --node option.
1958

1959
  """
1960
  if value and ":" in value:
1961
    return value.split(":", 1)
1962
  else:
1963
    return (value, None)
1964

    
1965

    
1966
def CalculateOSNames(os_name, os_variants):
1967
  """Calculates all the names an OS can be called, according to its variants.
1968

1969
  @type os_name: string
1970
  @param os_name: base name of the os
1971
  @type os_variants: list or None
1972
  @param os_variants: list of supported variants
1973
  @rtype: list
1974
  @return: list of valid names
1975

1976
  """
1977
  if os_variants:
1978
    return ["%s+%s" % (os_name, v) for v in os_variants]
1979
  else:
1980
    return [os_name]
1981

    
1982

    
1983
def ParseFields(selected, default):
1984
  """Parses the values of "--field"-like options.
1985

1986
  @type selected: string or None
1987
  @param selected: User-selected options
1988
  @type default: list
1989
  @param default: Default fields
1990

1991
  """
1992
  if selected is None:
1993
    return default
1994

    
1995
  if selected.startswith("+"):
1996
    return default + selected[1:].split(",")
1997

    
1998
  return selected.split(",")
1999

    
2000

    
2001
UsesRPC = rpc.RunWithRPC
2002

    
2003

    
2004
def AskUser(text, choices=None):
2005
  """Ask the user a question.
2006

2007
  @param text: the question to ask
2008

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

2014
  @return: one of the return values from the choices list; if input is
2015
      not possible (i.e. not running with a tty, we return the last
2016
      entry from the list
2017

2018
  """
2019
  if choices is None:
2020
    choices = [("y", True, "Perform the operation"),
2021
               ("n", False, "Do not perform the operation")]
2022
  if not choices or not isinstance(choices, list):
2023
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
2024
  for entry in choices:
2025
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
2026
      raise errors.ProgrammerError("Invalid choices element to AskUser")
2027

    
2028
  answer = choices[-1][1]
2029
  new_text = []
2030
  for line in text.splitlines():
2031
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
2032
  text = "\n".join(new_text)
2033
  try:
2034
    f = file("/dev/tty", "a+")
2035
  except IOError:
2036
    return answer
2037
  try:
2038
    chars = [entry[0] for entry in choices]
2039
    chars[-1] = "[%s]" % chars[-1]
2040
    chars.append("?")
2041
    maps = dict([(entry[0], entry[1]) for entry in choices])
2042
    while True:
2043
      f.write(text)
2044
      f.write("\n")
2045
      f.write("/".join(chars))
2046
      f.write(": ")
2047
      line = f.readline(2).strip().lower()
2048
      if line in maps:
2049
        answer = maps[line]
2050
        break
2051
      elif line == "?":
2052
        for entry in choices:
2053
          f.write(" %s - %s\n" % (entry[0], entry[2]))
2054
        f.write("\n")
2055
        continue
2056
  finally:
2057
    f.close()
2058
  return answer
2059

    
2060

    
2061
class JobSubmittedException(Exception):
2062
  """Job was submitted, client should exit.
2063

2064
  This exception has one argument, the ID of the job that was
2065
  submitted. The handler should print this ID.
2066

2067
  This is not an error, just a structured way to exit from clients.
2068

2069
  """
2070

    
2071

    
2072
def SendJob(ops, cl=None):
2073
  """Function to submit an opcode without waiting for the results.
2074

2075
  @type ops: list
2076
  @param ops: list of opcodes
2077
  @type cl: luxi.Client
2078
  @param cl: the luxi client to use for communicating with the master;
2079
             if None, a new client will be created
2080

2081
  """
2082
  if cl is None:
2083
    cl = GetClient()
2084

    
2085
  job_id = cl.SubmitJob(ops)
2086

    
2087
  return job_id
2088

    
2089

    
2090
def GenericPollJob(job_id, cbs, report_cbs):
2091
  """Generic job-polling function.
2092

2093
  @type job_id: number
2094
  @param job_id: Job ID
2095
  @type cbs: Instance of L{JobPollCbBase}
2096
  @param cbs: Data callbacks
2097
  @type report_cbs: Instance of L{JobPollReportCbBase}
2098
  @param report_cbs: Reporting callbacks
2099

2100
  """
2101
  prev_job_info = None
2102
  prev_logmsg_serial = None
2103

    
2104
  status = None
2105

    
2106
  while True:
2107
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
2108
                                      prev_logmsg_serial)
2109
    if not result:
2110
      # job not found, go away!
2111
      raise errors.JobLost("Job with id %s lost" % job_id)
2112

    
2113
    if result == constants.JOB_NOTCHANGED:
2114
      report_cbs.ReportNotChanged(job_id, status)
2115

    
2116
      # Wait again
2117
      continue
2118

    
2119
    # Split result, a tuple of (field values, log entries)
2120
    (job_info, log_entries) = result
2121
    (status, ) = job_info
2122

    
2123
    if log_entries:
2124
      for log_entry in log_entries:
2125
        (serial, timestamp, log_type, message) = log_entry
2126
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
2127
                                    log_type, message)
2128
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
2129

    
2130
    # TODO: Handle canceled and archived jobs
2131
    elif status in (constants.JOB_STATUS_SUCCESS,
2132
                    constants.JOB_STATUS_ERROR,
2133
                    constants.JOB_STATUS_CANCELING,
2134
                    constants.JOB_STATUS_CANCELED):
2135
      break
2136

    
2137
    prev_job_info = job_info
2138

    
2139
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
2140
  if not jobs:
2141
    raise errors.JobLost("Job with id %s lost" % job_id)
2142

    
2143
  status, opstatus, result = jobs[0]
2144

    
2145
  if status == constants.JOB_STATUS_SUCCESS:
2146
    return result
2147

    
2148
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
2149
    raise errors.OpExecError("Job was canceled")
2150

    
2151
  has_ok = False
2152
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
2153
    if status == constants.OP_STATUS_SUCCESS:
2154
      has_ok = True
2155
    elif status == constants.OP_STATUS_ERROR:
2156
      errors.MaybeRaise(msg)
2157

    
2158
      if has_ok:
2159
        raise errors.OpExecError("partial failure (opcode %d): %s" %
2160
                                 (idx, msg))
2161

    
2162
      raise errors.OpExecError(str(msg))
2163

    
2164
  # default failure mode
2165
  raise errors.OpExecError(result)
2166

    
2167

    
2168
class JobPollCbBase:
2169
  """Base class for L{GenericPollJob} callbacks.
2170

2171
  """
2172
  def __init__(self):
2173
    """Initializes this class.
2174

2175
    """
2176

    
2177
  def WaitForJobChangeOnce(self, job_id, fields,
2178
                           prev_job_info, prev_log_serial):
2179
    """Waits for changes on a job.
2180

2181
    """
2182
    raise NotImplementedError()
2183

    
2184
  def QueryJobs(self, job_ids, fields):
2185
    """Returns the selected fields for the selected job IDs.
2186

2187
    @type job_ids: list of numbers
2188
    @param job_ids: Job IDs
2189
    @type fields: list of strings
2190
    @param fields: Fields
2191

2192
    """
2193
    raise NotImplementedError()
2194

    
2195

    
2196
class JobPollReportCbBase:
2197
  """Base class for L{GenericPollJob} reporting callbacks.
2198

2199
  """
2200
  def __init__(self):
2201
    """Initializes this class.
2202

2203
    """
2204

    
2205
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2206
    """Handles a log message.
2207

2208
    """
2209
    raise NotImplementedError()
2210

    
2211
  def ReportNotChanged(self, job_id, status):
2212
    """Called for if a job hasn't changed in a while.
2213

2214
    @type job_id: number
2215
    @param job_id: Job ID
2216
    @type status: string or None
2217
    @param status: Job status if available
2218

2219
    """
2220
    raise NotImplementedError()
2221

    
2222

    
2223
class _LuxiJobPollCb(JobPollCbBase):
2224
  def __init__(self, cl):
2225
    """Initializes this class.
2226

2227
    """
2228
    JobPollCbBase.__init__(self)
2229
    self.cl = cl
2230

    
2231
  def WaitForJobChangeOnce(self, job_id, fields,
2232
                           prev_job_info, prev_log_serial):
2233
    """Waits for changes on a job.
2234

2235
    """
2236
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2237
                                        prev_job_info, prev_log_serial)
2238

    
2239
  def QueryJobs(self, job_ids, fields):
2240
    """Returns the selected fields for the selected job IDs.
2241

2242
    """
2243
    return self.cl.QueryJobs(job_ids, fields)
2244

    
2245

    
2246
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2247
  def __init__(self, feedback_fn):
2248
    """Initializes this class.
2249

2250
    """
2251
    JobPollReportCbBase.__init__(self)
2252

    
2253
    self.feedback_fn = feedback_fn
2254

    
2255
    assert callable(feedback_fn)
2256

    
2257
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2258
    """Handles a log message.
2259

2260
    """
2261
    self.feedback_fn((timestamp, log_type, log_msg))
2262

    
2263
  def ReportNotChanged(self, job_id, status):
2264
    """Called if a job hasn't changed in a while.
2265

2266
    """
2267
    # Ignore
2268

    
2269

    
2270
class StdioJobPollReportCb(JobPollReportCbBase):
2271
  def __init__(self):
2272
    """Initializes this class.
2273

2274
    """
2275
    JobPollReportCbBase.__init__(self)
2276

    
2277
    self.notified_queued = False
2278
    self.notified_waitlock = False
2279

    
2280
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2281
    """Handles a log message.
2282

2283
    """
2284
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2285
             FormatLogMessage(log_type, log_msg))
2286

    
2287
  def ReportNotChanged(self, job_id, status):
2288
    """Called if a job hasn't changed in a while.
2289

2290
    """
2291
    if status is None:
2292
      return
2293

    
2294
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2295
      ToStderr("Job %s is waiting in queue", job_id)
2296
      self.notified_queued = True
2297

    
2298
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2299
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2300
      self.notified_waitlock = True
2301

    
2302

    
2303
def FormatLogMessage(log_type, log_msg):
2304
  """Formats a job message according to its type.
2305

2306
  """
2307
  if log_type != constants.ELOG_MESSAGE:
2308
    log_msg = str(log_msg)
2309

    
2310
  return utils.SafeEncode(log_msg)
2311

    
2312

    
2313
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2314
  """Function to poll for the result of a job.
2315

2316
  @type job_id: job identified
2317
  @param job_id: the job to poll for results
2318
  @type cl: luxi.Client
2319
  @param cl: the luxi client to use for communicating with the master;
2320
             if None, a new client will be created
2321

2322
  """
2323
  if cl is None:
2324
    cl = GetClient()
2325

    
2326
  if reporter is None:
2327
    if feedback_fn:
2328
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2329
    else:
2330
      reporter = StdioJobPollReportCb()
2331
  elif feedback_fn:
2332
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2333

    
2334
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2335

    
2336

    
2337
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2338
  """Legacy function to submit an opcode.
2339

2340
  This is just a simple wrapper over the construction of the processor
2341
  instance. It should be extended to better handle feedback and
2342
  interaction functions.
2343

2344
  """
2345
  if cl is None:
2346
    cl = GetClient()
2347

    
2348
  SetGenericOpcodeOpts([op], opts)
2349

    
2350
  job_id = SendJob([op], cl=cl)
2351
  if hasattr(opts, "print_jobid") and opts.print_jobid:
2352
    ToStdout("%d" % job_id)
2353

    
2354
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2355
                       reporter=reporter)
2356

    
2357
  return op_results[0]
2358

    
2359

    
2360
def SubmitOpCodeToDrainedQueue(op):
2361
  """Forcefully insert a job in the queue, even if it is drained.
2362

2363
  """
2364
  cl = GetClient()
2365
  job_id = cl.SubmitJobToDrainedQueue([op])
2366
  op_results = PollJob(job_id, cl=cl)
2367
  return op_results[0]
2368

    
2369

    
2370
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2371
  """Wrapper around SubmitOpCode or SendJob.
2372

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

2378
  It will also process the opcodes if we're sending the via SendJob
2379
  (otherwise SubmitOpCode does it).
2380

2381
  """
2382
  if opts and opts.submit_only:
2383
    job = [op]
2384
    SetGenericOpcodeOpts(job, opts)
2385
    job_id = SendJob(job, cl=cl)
2386
    if opts.print_jobid:
2387
      ToStdout("%d" % job_id)
2388
    raise JobSubmittedException(job_id)
2389
  else:
2390
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2391

    
2392

    
2393
def _InitReasonTrail(op, opts):
2394
  """Builds the first part of the reason trail
2395

2396
  Builds the initial part of the reason trail, adding the user provided reason
2397
  (if it exists) and the name of the command starting the operation.
2398

2399
  @param op: the opcode the reason trail will be added to
2400
  @param opts: the command line options selected by the user
2401

2402
  """
2403
  assert len(sys.argv) >= 2
2404
  trail = []
2405

    
2406
  if opts.reason:
2407
    trail.append((constants.OPCODE_REASON_SRC_USER,
2408
                  opts.reason,
2409
                  utils.EpochNano()))
2410

    
2411
  binary = os.path.basename(sys.argv[0])
2412
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2413
  command = sys.argv[1]
2414
  trail.append((source, command, utils.EpochNano()))
2415
  op.reason = trail
2416

    
2417

    
2418
def SetGenericOpcodeOpts(opcode_list, options):
2419
  """Processor for generic options.
2420

2421
  This function updates the given opcodes based on generic command
2422
  line options (like debug, dry-run, etc.).
2423

2424
  @param opcode_list: list of opcodes
2425
  @param options: command line options or None
2426
  @return: None (in-place modification)
2427

2428
  """
2429
  if not options:
2430
    return
2431
  for op in opcode_list:
2432
    op.debug_level = options.debug
2433
    if hasattr(options, "dry_run"):
2434
      op.dry_run = options.dry_run
2435
    if getattr(options, "priority", None) is not None:
2436
      op.priority = options.priority
2437
    _InitReasonTrail(op, options)
2438

    
2439

    
2440
def FormatError(err):
2441
  """Return a formatted error message for a given error.
2442

2443
  This function takes an exception instance and returns a tuple
2444
  consisting of two values: first, the recommended exit code, and
2445
  second, a string describing the error message (not
2446
  newline-terminated).
2447

2448
  """
2449
  retcode = 1
2450
  obuf = StringIO()
2451
  msg = str(err)
2452
  if isinstance(err, errors.ConfigurationError):
2453
    txt = "Corrupt configuration file: %s" % msg
2454
    logging.error(txt)
2455
    obuf.write(txt + "\n")
2456
    obuf.write("Aborting.")
2457
    retcode = 2
2458
  elif isinstance(err, errors.HooksAbort):
2459
    obuf.write("Failure: hooks execution failed:\n")
2460
    for node, script, out in err.args[0]:
2461
      if out:
2462
        obuf.write("  node: %s, script: %s, output: %s\n" %
2463
                   (node, script, out))
2464
      else:
2465
        obuf.write("  node: %s, script: %s (no output)\n" %
2466
                   (node, script))
2467
  elif isinstance(err, errors.HooksFailure):
2468
    obuf.write("Failure: hooks general failure: %s" % msg)
2469
  elif isinstance(err, errors.ResolverError):
2470
    this_host = netutils.Hostname.GetSysName()
2471
    if err.args[0] == this_host:
2472
      msg = "Failure: can't resolve my own hostname ('%s')"
2473
    else:
2474
      msg = "Failure: can't resolve hostname '%s'"
2475
    obuf.write(msg % err.args[0])
2476
  elif isinstance(err, errors.OpPrereqError):
2477
    if len(err.args) == 2:
2478
      obuf.write("Failure: prerequisites not met for this"
2479
                 " operation:\nerror type: %s, error details:\n%s" %
2480
                 (err.args[1], err.args[0]))
2481
    else:
2482
      obuf.write("Failure: prerequisites not met for this"
2483
                 " operation:\n%s" % msg)
2484
  elif isinstance(err, errors.OpExecError):
2485
    obuf.write("Failure: command execution error:\n%s" % msg)
2486
  elif isinstance(err, errors.TagError):
2487
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2488
  elif isinstance(err, errors.JobQueueDrainError):
2489
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2490
               " accept new requests\n")
2491
  elif isinstance(err, errors.JobQueueFull):
2492
    obuf.write("Failure: the job queue is full and doesn't accept new"
2493
               " job submissions until old jobs are archived\n")
2494
  elif isinstance(err, errors.TypeEnforcementError):
2495
    obuf.write("Parameter Error: %s" % msg)
2496
  elif isinstance(err, errors.ParameterError):
2497
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2498
  elif isinstance(err, rpcerr.NoMasterError):
2499
    if err.args[0] == pathutils.MASTER_SOCKET:
2500
      daemon = "the master daemon"
2501
    elif err.args[0] == pathutils.QUERY_SOCKET:
2502
      daemon = "the config daemon"
2503
    else:
2504
      daemon = "socket '%s'" % str(err.args[0])
2505
    obuf.write("Cannot communicate with %s.\nIs the process running"
2506
               " and listening for connections?" % daemon)
2507
  elif isinstance(err, rpcerr.TimeoutError):
2508
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2509
               " been submitted and will continue to run even if the call"
2510
               " timed out. Useful commands in this situation are \"gnt-job"
2511
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2512
    obuf.write(msg)
2513
  elif isinstance(err, rpcerr.PermissionError):
2514
    obuf.write("It seems you don't have permissions to connect to the"
2515
               " master daemon.\nPlease retry as a different user.")
2516
  elif isinstance(err, rpcerr.ProtocolError):
2517
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2518
               "%s" % msg)
2519
  elif isinstance(err, errors.JobLost):
2520
    obuf.write("Error checking job status: %s" % msg)
2521
  elif isinstance(err, errors.QueryFilterParseError):
2522
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2523
    obuf.write("\n".join(err.GetDetails()))
2524
  elif isinstance(err, errors.GenericError):
2525
    obuf.write("Unhandled Ganeti error: %s" % msg)
2526
  elif isinstance(err, JobSubmittedException):
2527
    obuf.write("JobID: %s\n" % err.args[0])
2528
    retcode = 0
2529
  else:
2530
    obuf.write("Unhandled exception: %s" % msg)
2531
  return retcode, obuf.getvalue().rstrip("\n")
2532

    
2533

    
2534
def GenericMain(commands, override=None, aliases=None,
2535
                env_override=frozenset()):
2536
  """Generic main function for all the gnt-* commands.
2537

2538
  @param commands: a dictionary with a special structure, see the design doc
2539
                   for command line handling.
2540
  @param override: if not None, we expect a dictionary with keys that will
2541
                   override command line options; this can be used to pass
2542
                   options from the scripts to generic functions
2543
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2544
  @param env_override: list of environment names which are allowed to submit
2545
                       default args for commands
2546

2547
  """
2548
  # save the program name and the entire command line for later logging
2549
  if sys.argv:
2550
    binary = os.path.basename(sys.argv[0])
2551
    if not binary:
2552
      binary = sys.argv[0]
2553

    
2554
    if len(sys.argv) >= 2:
2555
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2556
    else:
2557
      logname = binary
2558

    
2559
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2560
  else:
2561
    binary = "<unknown program>"
2562
    cmdline = "<unknown>"
2563

    
2564
  if aliases is None:
2565
    aliases = {}
2566

    
2567
  try:
2568
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2569
                                       env_override)
2570
  except _ShowVersion:
2571
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2572
             constants.RELEASE_VERSION)
2573
    return constants.EXIT_SUCCESS
2574
  except _ShowUsage, err:
2575
    for line in _FormatUsage(binary, commands):
2576
      ToStdout(line)
2577

    
2578
    if err.exit_error:
2579
      return constants.EXIT_FAILURE
2580
    else:
2581
      return constants.EXIT_SUCCESS
2582
  except errors.ParameterError, err:
2583
    result, err_msg = FormatError(err)
2584
    ToStderr(err_msg)
2585
    return 1
2586

    
2587
  if func is None: # parse error
2588
    return 1
2589

    
2590
  if override is not None:
2591
    for key, val in override.iteritems():
2592
      setattr(options, key, val)
2593

    
2594
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2595
                     stderr_logging=True)
2596

    
2597
  logging.debug("Command line: %s", cmdline)
2598

    
2599
  try:
2600
    result = func(options, args)
2601
  except (errors.GenericError, rpcerr.ProtocolError,
2602
          JobSubmittedException), err:
2603
    result, err_msg = FormatError(err)
2604
    logging.exception("Error during command processing")
2605
    ToStderr(err_msg)
2606
  except KeyboardInterrupt:
2607
    result = constants.EXIT_FAILURE
2608
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2609
             " might have been submitted and"
2610
             " will continue to run in the background.")
2611
  except IOError, err:
2612
    if err.errno == errno.EPIPE:
2613
      # our terminal went away, we'll exit
2614
      sys.exit(constants.EXIT_FAILURE)
2615
    else:
2616
      raise
2617

    
2618
  return result
2619

    
2620

    
2621
def ParseNicOption(optvalue):
2622
  """Parses the value of the --net option(s).
2623

2624
  """
2625
  try:
2626
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2627
  except (TypeError, ValueError), err:
2628
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2629
                               errors.ECODE_INVAL)
2630

    
2631
  nics = [{}] * nic_max
2632
  for nidx, ndict in optvalue:
2633
    nidx = int(nidx)
2634

    
2635
    if not isinstance(ndict, dict):
2636
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2637
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2638

    
2639
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2640

    
2641
    nics[nidx] = ndict
2642

    
2643
  return nics
2644

    
2645

    
2646
def FixHvParams(hvparams):
2647
  # In Ganeti 2.8.4 the separator for the usb_devices hvparam was changed from
2648
  # comma to space because commas cannot be accepted on the command line
2649
  # (they already act as the separator between different hvparams). Still,
2650
  # RAPI should be able to accept commas for backwards compatibility.
2651
  # Therefore, we convert spaces into commas here, and we keep the old
2652
  # parsing logic everywhere else.
2653
  try:
2654
    new_usb_devices = hvparams[constants.HV_USB_DEVICES].replace(" ", ",")
2655
    hvparams[constants.HV_USB_DEVICES] = new_usb_devices
2656
  except KeyError:
2657
    #No usb_devices, no modification required
2658
    pass
2659

    
2660

    
2661
def GenericInstanceCreate(mode, opts, args):
2662
  """Add an instance to the cluster via either creation or import.
2663

2664
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2665
  @param opts: the command line options selected by the user
2666
  @type args: list
2667
  @param args: should contain only one element, the new instance name
2668
  @rtype: int
2669
  @return: the desired exit code
2670

2671
  """
2672
  instance = args[0]
2673

    
2674
  (pnode, snode) = SplitNodeOption(opts.node)
2675

    
2676
  hypervisor = None
2677
  hvparams = {}
2678
  if opts.hypervisor:
2679
    hypervisor, hvparams = opts.hypervisor
2680

    
2681
  if opts.nics:
2682
    nics = ParseNicOption(opts.nics)
2683
  elif opts.no_nics:
2684
    # no nics
2685
    nics = []
2686
  elif mode == constants.INSTANCE_CREATE:
2687
    # default of one nic, all auto
2688
    nics = [{}]
2689
  else:
2690
    # mode == import
2691
    nics = []
2692

    
2693
  if opts.disk_template == constants.DT_DISKLESS:
2694
    if opts.disks or opts.sd_size is not None:
2695
      raise errors.OpPrereqError("Diskless instance but disk"
2696
                                 " information passed", errors.ECODE_INVAL)
2697
    disks = []
2698
  else:
2699
    if (not opts.disks and not opts.sd_size
2700
        and mode == constants.INSTANCE_CREATE):
2701
      raise errors.OpPrereqError("No disk information specified",
2702
                                 errors.ECODE_INVAL)
2703
    if opts.disks and opts.sd_size is not None:
2704
      raise errors.OpPrereqError("Please use either the '--disk' or"
2705
                                 " '-s' option", errors.ECODE_INVAL)
2706
    if opts.sd_size is not None:
2707
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2708

    
2709
    if opts.disks:
2710
      try:
2711
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2712
      except ValueError, err:
2713
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
2714
                                   errors.ECODE_INVAL)
2715
      disks = [{}] * disk_max
2716
    else:
2717
      disks = []
2718
    for didx, ddict in opts.disks:
2719
      didx = int(didx)
2720
      if not isinstance(ddict, dict):
2721
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2722
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
2723
      elif constants.IDISK_SIZE in ddict:
2724
        if constants.IDISK_ADOPT in ddict:
2725
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2726
                                     " (disk %d)" % didx, errors.ECODE_INVAL)
2727
        try:
2728
          ddict[constants.IDISK_SIZE] = \
2729
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2730
        except ValueError, err:
2731
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2732
                                     (didx, err), errors.ECODE_INVAL)
2733
      elif constants.IDISK_ADOPT in ddict:
2734
        if constants.IDISK_SPINDLES in ddict:
2735
          raise errors.OpPrereqError("spindles is not a valid option when"
2736
                                     " adopting a disk", errors.ECODE_INVAL)
2737
        if mode == constants.INSTANCE_IMPORT:
2738
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2739
                                     " import", errors.ECODE_INVAL)
2740
        ddict[constants.IDISK_SIZE] = 0
2741
      else:
2742
        raise errors.OpPrereqError("Missing size or adoption source for"
2743
                                   " disk %d" % didx, errors.ECODE_INVAL)
2744
      if constants.IDISK_SPINDLES in ddict:
2745
        ddict[constants.IDISK_SPINDLES] = int(ddict[constants.IDISK_SPINDLES])
2746

    
2747
      disks[didx] = ddict
2748

    
2749
  if opts.tags is not None:
2750
    tags = opts.tags.split(",")
2751
  else:
2752
    tags = []
2753

    
2754
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2755
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2756
  FixHvParams(hvparams)
2757

    
2758
  if mode == constants.INSTANCE_CREATE:
2759
    start = opts.start
2760
    os_type = opts.os
2761
    force_variant = opts.force_variant
2762
    src_node = None
2763
    src_path = None
2764
    no_install = opts.no_install
2765
    identify_defaults = False
2766
    compress = constants.IEC_NONE
2767
    instance_communication = opts.instance_communication
2768
  elif mode == constants.INSTANCE_IMPORT:
2769
    start = False
2770
    os_type = None
2771
    force_variant = False
2772
    src_node = opts.src_node
2773
    src_path = opts.src_dir
2774
    no_install = None
2775
    identify_defaults = opts.identify_defaults
2776
    compress = opts.compress
2777
    instance_communication = False
2778
  else:
2779
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2780

    
2781
  osparams_private = opts.osparams_private or serializer.PrivateDict()
2782
  osparams_secret = opts.osparams_private or serializer.PrivateDict()
2783

    
2784
  op = opcodes.OpInstanceCreate(instance_name=instance,
2785
                                disks=disks,
2786
                                disk_template=opts.disk_template,
2787
                                nics=nics,
2788
                                conflicts_check=opts.conflicts_check,
2789
                                pnode=pnode, snode=snode,
2790
                                ip_check=opts.ip_check,
2791
                                name_check=opts.name_check,
2792
                                wait_for_sync=opts.wait_for_sync,
2793
                                file_storage_dir=opts.file_storage_dir,
2794
                                file_driver=opts.file_driver,
2795
                                iallocator=opts.iallocator,
2796
                                hypervisor=hypervisor,
2797
                                hvparams=hvparams,
2798
                                beparams=opts.beparams,
2799
                                osparams=opts.osparams,
2800
                                osparams_private=osparams_private,
2801
                                osparams_secret=osparams_secret,
2802
                                mode=mode,
2803
                                start=start,
2804
                                os_type=os_type,
2805
                                force_variant=force_variant,
2806
                                src_node=src_node,
2807
                                src_path=src_path,
2808
                                compress=compress,
2809
                                tags=tags,
2810
                                no_install=no_install,
2811
                                identify_defaults=identify_defaults,
2812
                                ignore_ipolicy=opts.ignore_ipolicy,
2813
                                instance_communication=instance_communication)
2814

    
2815
  SubmitOrSend(op, opts)
2816
  return 0
2817

    
2818

    
2819
class _RunWhileClusterStoppedHelper:
2820
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2821

2822
  """
2823
  def __init__(self, feedback_fn, cluster_name, master_node,
2824
               online_nodes, ssh_ports):
2825
    """Initializes this class.
2826

2827
    @type feedback_fn: callable
2828
    @param feedback_fn: Feedback function
2829
    @type cluster_name: string
2830
    @param cluster_name: Cluster name
2831
    @type master_node: string
2832
    @param master_node Master node name
2833
    @type online_nodes: list
2834
    @param online_nodes: List of names of online nodes
2835
    @type ssh_ports: list
2836
    @param ssh_ports: List of SSH ports of online nodes
2837

2838
    """
2839
    self.feedback_fn = feedback_fn
2840
    self.cluster_name = cluster_name
2841
    self.master_node = master_node
2842
    self.online_nodes = online_nodes
2843
    self.ssh_ports = dict(zip(online_nodes, ssh_ports))
2844

    
2845
    self.ssh = ssh.SshRunner(self.cluster_name)
2846

    
2847
    self.nonmaster_nodes = [name for name in online_nodes
2848
                            if name != master_node]
2849

    
2850
    assert self.master_node not in self.nonmaster_nodes
2851

    
2852
  def _RunCmd(self, node_name, cmd):
2853
    """Runs a command on the local or a remote machine.
2854

2855
    @type node_name: string
2856
    @param node_name: Machine name
2857
    @type cmd: list
2858
    @param cmd: Command
2859

2860
    """
2861
    if node_name is None or node_name == self.master_node:
2862
      # No need to use SSH
2863
      result = utils.RunCmd(cmd)
2864
    else:
2865
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2866
                            utils.ShellQuoteArgs(cmd),
2867
                            port=self.ssh_ports[node_name])
2868

    
2869
    if result.failed:
2870
      errmsg = ["Failed to run command %s" % result.cmd]
2871
      if node_name:
2872
        errmsg.append("on node %s" % node_name)
2873
      errmsg.append(": exitcode %s and error %s" %
2874
                    (result.exit_code, result.output))
2875
      raise errors.OpExecError(" ".join(errmsg))
2876

    
2877
  def Call(self, fn, *args):
2878
    """Call function while all daemons are stopped.
2879

2880
    @type fn: callable
2881
    @param fn: Function to be called
2882

2883
    """
2884
    # Pause watcher by acquiring an exclusive lock on watcher state file
2885
    self.feedback_fn("Blocking watcher")
2886
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2887
    try:
2888
      # TODO: Currently, this just blocks. There's no timeout.
2889
      # TODO: Should it be a shared lock?
2890
      watcher_block.Exclusive(blocking=True)
2891

    
2892
      # Stop master daemons, so that no new jobs can come in and all running
2893
      # ones are finished
2894
      self.feedback_fn("Stopping master daemons")
2895
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2896
      try:
2897
        # Stop daemons on all nodes
2898
        for node_name in self.online_nodes:
2899
          self.feedback_fn("Stopping daemons on %s" % node_name)
2900
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2901

    
2902
        # All daemons are shut down now
2903
        try:
2904
          return fn(self, *args)
2905
        except Exception, err:
2906
          _, errmsg = FormatError(err)
2907
          logging.exception("Caught exception")
2908
          self.feedback_fn(errmsg)
2909
          raise
2910
      finally:
2911
        # Start cluster again, master node last
2912
        for node_name in self.nonmaster_nodes + [self.master_node]:
2913
          self.feedback_fn("Starting daemons on %s" % node_name)
2914
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"])
2915
    finally:
2916
      # Resume watcher
2917
      watcher_block.Close()
2918

    
2919

    
2920
def RunWhileClusterStopped(feedback_fn, fn, *args):
2921
  """Calls a function while all cluster daemons are stopped.
2922

2923
  @type feedback_fn: callable
2924
  @param feedback_fn: Feedback function
2925
  @type fn: callable
2926
  @param fn: Function to be called when daemons are stopped
2927

2928
  """
2929
  feedback_fn("Gathering cluster information")
2930

    
2931
  # This ensures we're running on the master daemon
2932
  cl = GetClient()
2933
  # Query client
2934
  qcl = GetClient(query=True)
2935

    
2936
  (cluster_name, master_node) = \
2937
    cl.QueryConfigValues(["cluster_name", "master_node"])
2938

    
2939
  online_nodes = GetOnlineNodes([], cl=qcl)
2940
  ssh_ports = GetNodesSshPorts(online_nodes, qcl)
2941

    
2942
  # Don't keep a reference to the client. The master daemon will go away.
2943
  del cl
2944
  del qcl
2945

    
2946
  assert master_node in online_nodes
2947

    
2948
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2949
                                       online_nodes, ssh_ports).Call(fn, *args)
2950

    
2951

    
2952
def GenerateTable(headers, fields, separator, data,
2953
                  numfields=None, unitfields=None,
2954
                  units=None):
2955
  """Prints a table with headers and different fields.
2956

2957
  @type headers: dict
2958
  @param headers: dictionary mapping field names to headers for
2959
      the table
2960
  @type fields: list
2961
  @param fields: the field names corresponding to each row in
2962
      the data field
2963
  @param separator: the separator to be used; if this is None,
2964
      the default 'smart' algorithm is used which computes optimal
2965
      field width, otherwise just the separator is used between
2966
      each field
2967
  @type data: list
2968
  @param data: a list of lists, each sublist being one row to be output
2969
  @type numfields: list
2970
  @param numfields: a list with the fields that hold numeric
2971
      values and thus should be right-aligned
2972
  @type unitfields: list
2973
  @param unitfields: a list with the fields that hold numeric
2974
      values that should be formatted with the units field
2975
  @type units: string or None
2976
  @param units: the units we should use for formatting, or None for
2977
      automatic choice (human-readable for non-separator usage, otherwise
2978
      megabytes); this is a one-letter string
2979

2980
  """
2981
  if units is None:
2982
    if separator:
2983
      units = "m"
2984
    else:
2985
      units = "h"
2986

    
2987
  if numfields is None:
2988
    numfields = []
2989
  if unitfields is None:
2990
    unitfields = []
2991

    
2992
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2993
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2994

    
2995
  format_fields = []
2996
  for field in fields:
2997
    if headers and field not in headers:
2998
      # TODO: handle better unknown fields (either revert to old
2999
      # style of raising exception, or deal more intelligently with
3000
      # variable fields)
3001
      headers[field] = field
3002
    if separator is not None:
3003
      format_fields.append("%s")
3004
    elif numfields.Matches(field):
3005
      format_fields.append("%*s")
3006
    else:
3007
      format_fields.append("%-*s")
3008

    
3009
  if separator is None:
3010
    mlens = [0 for name in fields]
3011
    format_str = " ".join(format_fields)
3012
  else:
3013
    format_str = separator.replace("%", "%%").join(format_fields)
3014

    
3015
  for row in data:
3016
    if row is None:
3017
      continue
3018
    for idx, val in enumerate(row):
3019
      if unitfields.Matches(fields[idx]):
3020
        try:
3021
          val = int(val)
3022
        except (TypeError, ValueError):
3023
          pass
3024
        else:
3025
          val = row[idx] = utils.FormatUnit(val, units)
3026
      val = row[idx] = str(val)
3027
      if separator is None:
3028
        mlens[idx] = max(mlens[idx], len(val))
3029

    
3030
  result = []
3031
  if headers:
3032
    args = []
3033
    for idx, name in enumerate(fields):
3034
      hdr = headers[name]
3035
      if separator is None:
3036
        mlens[idx] = max(mlens[idx], len(hdr))
3037
        args.append(mlens[idx])
3038
      args.append(hdr)
3039
    result.append(format_str % tuple(args))
3040

    
3041
  if separator is None:
3042
    assert len(mlens) == len(fields)
3043

    
3044
    if fields and not numfields.Matches(fields[-1]):
3045
      mlens[-1] = 0
3046

    
3047
  for line in data:
3048
    args = []
3049
    if line is None:
3050
      line = ["-" for _ in fields]
3051
    for idx in range(len(fields)):
3052
      if separator is None:
3053
        args.append(mlens[idx])
3054
      args.append(line[idx])
3055
    result.append(format_str % tuple(args))
3056

    
3057
  return result
3058

    
3059

    
3060
def _FormatBool(value):
3061
  """Formats a boolean value as a string.
3062

3063
  """
3064
  if value:
3065
    return "Y"
3066
  return "N"
3067

    
3068

    
3069
#: Default formatting for query results; (callback, align right)
3070
_DEFAULT_FORMAT_QUERY = {
3071
  constants.QFT_TEXT: (str, False),
3072
  constants.QFT_BOOL: (_FormatBool, False),
3073
  constants.QFT_NUMBER: (str, True),
3074
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
3075
  constants.QFT_OTHER: (str, False),
3076
  constants.QFT_UNKNOWN: (str, False),
3077
  }
3078

    
3079

    
3080
def _GetColumnFormatter(fdef, override, unit):
3081
  """Returns formatting function for a field.
3082

3083
  @type fdef: L{objects.QueryFieldDefinition}
3084
  @type override: dict
3085
  @param override: Dictionary for overriding field formatting functions,
3086
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3087
  @type unit: string
3088
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
3089
  @rtype: tuple; (callable, bool)
3090
  @return: Returns the function to format a value (takes one parameter) and a
3091
    boolean for aligning the value on the right-hand side
3092

3093
  """
3094
  fmt = override.get(fdef.name, None)
3095
  if fmt is not None:
3096
    return fmt
3097

    
3098
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3099

    
3100
  if fdef.kind == constants.QFT_UNIT:
3101
    # Can't keep this information in the static dictionary
3102
    return (lambda value: utils.FormatUnit(value, unit), True)
3103

    
3104
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
3105
  if fmt is not None:
3106
    return fmt
3107

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

    
3110

    
3111
class _QueryColumnFormatter:
3112
  """Callable class for formatting fields of a query.
3113

3114
  """
3115
  def __init__(self, fn, status_fn, verbose):
3116
    """Initializes this class.
3117

3118
    @type fn: callable
3119
    @param fn: Formatting function
3120
    @type status_fn: callable
3121
    @param status_fn: Function to report fields' status
3122
    @type verbose: boolean
3123
    @param verbose: whether to use verbose field descriptions or not
3124

3125
    """
3126
    self._fn = fn
3127
    self._status_fn = status_fn
3128
    self._verbose = verbose
3129

    
3130
  def __call__(self, data):
3131
    """Returns a field's string representation.
3132

3133
    """
3134
    (status, value) = data
3135

    
3136
    # Report status
3137
    self._status_fn(status)
3138

    
3139
    if status == constants.RS_NORMAL:
3140
      return self._fn(value)
3141

    
3142
    assert value is None, \
3143
           "Found value %r for abnormal status %s" % (value, status)
3144

    
3145
    return FormatResultError(status, self._verbose)
3146

    
3147

    
3148
def FormatResultError(status, verbose):
3149
  """Formats result status other than L{constants.RS_NORMAL}.
3150

3151
  @param status: The result status
3152
  @type verbose: boolean
3153
  @param verbose: Whether to return the verbose text
3154
  @return: Text of result status
3155

3156
  """
3157
  assert status != constants.RS_NORMAL, \
3158
         "FormatResultError called with status equal to constants.RS_NORMAL"
3159
  try:
3160
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
3161
  except KeyError:
3162
    raise NotImplementedError("Unknown status %s" % status)
3163
  else:
3164
    if verbose:
3165
      return verbose_text
3166
    return normal_text
3167

    
3168

    
3169
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3170
                      header=False, verbose=False):
3171
  """Formats data in L{objects.QueryResponse}.
3172

3173
  @type result: L{objects.QueryResponse}
3174
  @param result: result of query operation
3175
  @type unit: string
3176
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
3177
    see L{utils.text.FormatUnit}
3178
  @type format_override: dict
3179
  @param format_override: Dictionary for overriding field formatting functions,
3180
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3181
  @type separator: string or None
3182
  @param separator: String used to separate fields
3183
  @type header: bool
3184
  @param header: Whether to output header row
3185
  @type verbose: boolean
3186
  @param verbose: whether to use verbose field descriptions or not
3187

3188
  """
3189
  if unit is None:
3190
    if separator:
3191
      unit = "m"
3192
    else:
3193
      unit = "h"
3194

    
3195
  if format_override is None:
3196
    format_override = {}
3197

    
3198
  stats = dict.fromkeys(constants.RS_ALL, 0)
3199

    
3200
  def _RecordStatus(status):
3201
    if status in stats:
3202
      stats[status] += 1
3203

    
3204
  columns = []
3205
  for fdef in result.fields:
3206
    assert fdef.title and fdef.name
3207
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3208
    columns.append(TableColumn(fdef.title,
3209
                               _QueryColumnFormatter(fn, _RecordStatus,
3210
                                                     verbose),
3211
                               align_right))
3212

    
3213
  table = FormatTable(result.data, columns, header, separator)
3214

    
3215
  # Collect statistics
3216
  assert len(stats) == len(constants.RS_ALL)
3217
  assert compat.all(count >= 0 for count in stats.values())
3218

    
3219
  # Determine overall status. If there was no data, unknown fields must be
3220
  # detected via the field definitions.
3221
  if (stats[constants.RS_UNKNOWN] or
3222
      (not result.data and _GetUnknownFields(result.fields))):
3223
    status = QR_UNKNOWN
3224
  elif compat.any(count > 0 for key, count in stats.items()
3225
                  if key != constants.RS_NORMAL):
3226
    status = QR_INCOMPLETE
3227
  else:
3228
    status = QR_NORMAL
3229

    
3230
  return (status, table)
3231

    
3232

    
3233
def _GetUnknownFields(fdefs):
3234
  """Returns list of unknown fields included in C{fdefs}.
3235

3236
  @type fdefs: list of L{objects.QueryFieldDefinition}
3237

3238
  """
3239
  return [fdef for fdef in fdefs
3240
          if fdef.kind == constants.QFT_UNKNOWN]
3241

    
3242

    
3243
def _WarnUnknownFields(fdefs):
3244
  """Prints a warning to stderr if a query included unknown fields.
3245

3246
  @type fdefs: list of L{objects.QueryFieldDefinition}
3247

3248
  """
3249
  unknown = _GetUnknownFields(fdefs)
3250
  if unknown:
3251
    ToStderr("Warning: Queried for unknown fields %s",
3252
             utils.CommaJoin(fdef.name for fdef in unknown))
3253
    return True
3254

    
3255
  return False
3256

    
3257

    
3258
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3259
                format_override=None, verbose=False, force_filter=False,
3260
                namefield=None, qfilter=None, isnumeric=False):
3261
  """Generic implementation for listing all items of a resource.
3262

3263
  @param resource: One of L{constants.QR_VIA_LUXI}
3264
  @type fields: list of strings
3265
  @param fields: List of fields to query for
3266
  @type names: list of strings
3267
  @param names: Names of items to query for
3268
  @type unit: string or None
3269
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
3270
    None for automatic choice (human-readable for non-separator usage,
3271
    otherwise megabytes); this is a one-letter string
3272
  @type separator: string or None
3273
  @param separator: String used to separate fields
3274
  @type header: bool
3275
  @param header: Whether to show header row
3276
  @type force_filter: bool
3277
  @param force_filter: Whether to always treat names as filter
3278
  @type format_override: dict
3279
  @param format_override: Dictionary for overriding field formatting functions,
3280
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3281
  @type verbose: boolean
3282
  @param verbose: whether to use verbose field descriptions or not
3283
  @type namefield: string
3284
  @param namefield: Name of field to use for simple filters (see
3285
    L{qlang.MakeFilter} for details)
3286
  @type qfilter: list or None
3287
  @param qfilter: Query filter (in addition to names)
3288
  @param isnumeric: bool
3289
  @param isnumeric: Whether the namefield's type is numeric, and therefore
3290
    any simple filters built by namefield should use integer values to
3291
    reflect that
3292

3293
  """
3294
  if not names:
3295
    names = None
3296

    
3297
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3298
                                isnumeric=isnumeric)
3299

    
3300
  if qfilter is None:
3301
    qfilter = namefilter
3302
  elif namefilter is not None:
3303
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3304

    
3305
  if cl is None:
3306
    cl = GetClient()
3307

    
3308
  response = cl.Query(resource, fields, qfilter)
3309

    
3310
  found_unknown = _WarnUnknownFields(response.fields)
3311

    
3312
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3313
                                     header=header,
3314
                                     format_override=format_override,
3315
                                     verbose=verbose)
3316

    
3317
  for line in data:
3318
    ToStdout(line)
3319

    
3320
  assert ((found_unknown and status == QR_UNKNOWN) or
3321
          (not found_unknown and status != QR_UNKNOWN))
3322

    
3323
  if status == QR_UNKNOWN:
3324
    return constants.EXIT_UNKNOWN_FIELD
3325

    
3326
  # TODO: Should the list command fail if not all data could be collected?
3327
  return constants.EXIT_SUCCESS
3328

    
3329

    
3330
def _FieldDescValues(fdef):
3331
  """Helper function for L{GenericListFields} to get query field description.
3332

3333
  @type fdef: L{objects.QueryFieldDefinition}
3334
  @rtype: list
3335

3336
  """
3337
  return [
3338
    fdef.name,
3339
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3340
    fdef.title,
3341
    fdef.doc,
3342
    ]
3343

    
3344

    
3345
def GenericListFields(resource, fields, separator, header, cl=None):
3346
  """Generic implementation for listing fields for a resource.
3347

3348
  @param resource: One of L{constants.QR_VIA_LUXI}
3349
  @type fields: list of strings
3350
  @param fields: List of fields to query for
3351
  @type separator: string or None
3352
  @param separator: String used to separate fields
3353
  @type header: bool
3354
  @param header: Whether to show header row
3355

3356
  """
3357
  if cl is None:
3358
    cl = GetClient()
3359

    
3360
  if not fields:
3361
    fields = None
3362

    
3363
  response = cl.QueryFields(resource, fields)
3364

    
3365
  found_unknown = _WarnUnknownFields(response.fields)
3366

    
3367
  columns = [
3368
    TableColumn("Name", str, False),
3369
    TableColumn("Type", str, False),
3370
    TableColumn("Title", str, False),
3371
    TableColumn("Description", str, False),
3372
    ]
3373

    
3374
  rows = map(_FieldDescValues, response.fields)
3375

    
3376
  for line in FormatTable(rows, columns, header, separator):
3377
    ToStdout(line)
3378

    
3379
  if found_unknown:
3380
    return constants.EXIT_UNKNOWN_FIELD
3381

    
3382
  return constants.EXIT_SUCCESS
3383

    
3384

    
3385
class TableColumn:
3386
  """Describes a column for L{FormatTable}.
3387

3388
  """
3389
  def __init__(self, title, fn, align_right):
3390
    """Initializes this class.
3391

3392
    @type title: string
3393
    @param title: Column title
3394
    @type fn: callable
3395
    @param fn: Formatting function
3396
    @type align_right: bool
3397
    @param align_right: Whether to align values on the right-hand side
3398

3399
    """
3400
    self.title = title
3401
    self.format = fn
3402
    self.align_right = align_right
3403

    
3404

    
3405
def _GetColFormatString(width, align_right):
3406
  """Returns the format string for a field.
3407

3408
  """
3409
  if align_right:
3410
    sign = ""
3411
  else:
3412
    sign = "-"
3413

    
3414
  return "%%%s%ss" % (sign, width)
3415

    
3416

    
3417
def FormatTable(rows, columns, header, separator):
3418
  """Formats data as a table.
3419

3420
  @type rows: list of lists
3421
  @param rows: Row data, one list per row
3422
  @type columns: list of L{TableColumn}
3423
  @param columns: Column descriptions
3424
  @type header: bool
3425
  @param header: Whether to show header row
3426
  @type separator: string or None
3427
  @param separator: String used to separate columns
3428

3429
  """
3430
  if header:
3431
    data = [[col.title for col in columns]]
3432
    colwidth = [len(col.title) for col in columns]
3433
  else:
3434
    data = []
3435
    colwidth = [0 for _ in columns]
3436

    
3437
  # Format row data
3438
  for row in rows:
3439
    assert len(row) == len(columns)
3440

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

    
3443
    if separator is None:
3444
      # Update column widths
3445
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3446
        # Modifying a list's items while iterating is fine
3447
        colwidth[idx] = max(oldwidth, len(value))
3448

    
3449
    data.append(formatted)
3450

    
3451
  if separator is not None:
3452
    # Return early if a separator is used
3453
    return [separator.join(row) for row in data]
3454

    
3455
  if columns and not columns[-1].align_right:
3456
    # Avoid unnecessary spaces at end of line
3457
    colwidth[-1] = 0
3458

    
3459
  # Build format string
3460
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3461
                  for col, width in zip(columns, colwidth)])
3462

    
3463
  return [fmt % tuple(row) for row in data]
3464

    
3465

    
3466
def FormatTimestamp(ts):
3467
  """Formats a given timestamp.
3468

3469
  @type ts: timestamp
3470
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3471

3472
  @rtype: string
3473
  @return: a string with the formatted timestamp
3474

3475
  """
3476
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3477
    return "?"
3478

    
3479
  (sec, usecs) = ts
3480
  return utils.FormatTime(sec, usecs=usecs)
3481

    
3482

    
3483
def ParseTimespec(value):
3484
  """Parse a time specification.
3485

3486
  The following suffixed will be recognized:
3487

3488
    - s: seconds
3489
    - m: minutes
3490
    - h: hours
3491
    - d: day
3492
    - w: weeks
3493

3494
  Without any suffix, the value will be taken to be in seconds.
3495

3496
  """
3497
  value = str(value)
3498
  if not value:
3499
    raise errors.OpPrereqError("Empty time specification passed",
3500
                               errors.ECODE_INVAL)
3501
  suffix_map = {
3502
    "s": 1,
3503
    "m": 60,
3504
    "h": 3600,
3505
    "d": 86400,
3506
    "w": 604800,
3507
    }
3508
  if value[-1] not in suffix_map:
3509
    try:
3510
      value = int(value)
3511
    except (TypeError, ValueError):
3512
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3513
                                 errors.ECODE_INVAL)
3514
  else:
3515
    multiplier = suffix_map[value[-1]]
3516
    value = value[:-1]
3517
    if not value: # no data left after stripping the suffix
3518
      raise errors.OpPrereqError("Invalid time specification (only"
3519
                                 " suffix passed)", errors.ECODE_INVAL)
3520
    try:
3521
      value = int(value) * multiplier
3522
    except (TypeError, ValueError):
3523
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3524
                                 errors.ECODE_INVAL)
3525
  return value
3526

    
3527

    
3528
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3529
                   filter_master=False, nodegroup=None):
3530
  """Returns the names of online nodes.
3531

3532
  This function will also log a warning on stderr with the names of
3533
  the online nodes.
3534

3535
  @param nodes: if not empty, use only this subset of nodes (minus the
3536
      offline ones)
3537
  @param cl: if not None, luxi client to use
3538
  @type nowarn: boolean
3539
  @param nowarn: by default, this function will output a note with the
3540
      offline nodes that are skipped; if this parameter is True the
3541
      note is not displayed
3542
  @type secondary_ips: boolean
3543
  @param secondary_ips: if True, return the secondary IPs instead of the
3544
      names, useful for doing network traffic over the replication interface
3545
      (if any)
3546
  @type filter_master: boolean
3547
  @param filter_master: if True, do not return the master node in the list
3548
      (useful in coordination with secondary_ips where we cannot check our
3549
      node name against the list)
3550
  @type nodegroup: string
3551
  @param nodegroup: If set, only return nodes in this node group
3552

3553
  """
3554
  if cl is None:
3555
    cl = GetClient(query=True)
3556

    
3557
  qfilter = []
3558

    
3559
  if nodes:
3560
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3561

    
3562
  if nodegroup is not None:
3563
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3564
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3565

    
3566
  if filter_master:
3567
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3568

    
3569
  if qfilter:
3570
    if len(qfilter) > 1:
3571
      final_filter = [qlang.OP_AND] + qfilter
3572
    else:
3573
      assert len(qfilter) == 1
3574
      final_filter = qfilter[0]
3575
  else:
3576
    final_filter = None
3577

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

    
3580
  def _IsOffline(row):
3581
    (_, (_, offline), _) = row
3582
    return offline
3583

    
3584
  def _GetName(row):
3585
    ((_, name), _, _) = row
3586
    return name
3587

    
3588
  def _GetSip(row):
3589
    (_, _, (_, sip)) = row
3590
    return sip
3591

    
3592
  (offline, online) = compat.partition(result.data, _IsOffline)
3593

    
3594
  if offline and not nowarn:
3595
    ToStderr("Note: skipping offline node(s): %s" %
3596
             utils.CommaJoin(map(_GetName, offline)))
3597

    
3598
  if secondary_ips:
3599
    fn = _GetSip
3600
  else:
3601
    fn = _GetName
3602

    
3603
  return map(fn, online)
3604

    
3605

    
3606
def GetNodesSshPorts(nodes, cl):
3607
  """Retrieves SSH ports of given nodes.
3608

3609
  @param nodes: the names of nodes
3610
  @type nodes: a list of strings
3611
  @param cl: a client to use for the query
3612
  @type cl: L{Client}
3613
  @return: the list of SSH ports corresponding to the nodes
3614
  @rtype: a list of tuples
3615
  """
3616
  return map(lambda t: t[0],
3617
             cl.QueryNodes(names=nodes,
3618
                           fields=["ndp/ssh_port"],
3619
                           use_locking=False))
3620

    
3621

    
3622
def _ToStream(stream, txt, *args):
3623
  """Write a message to a stream, bypassing the logging system
3624

3625
  @type stream: file object
3626
  @param stream: the file to which we should write
3627
  @type txt: str
3628
  @param txt: the message
3629

3630
  """
3631
  try:
3632
    if args:
3633
      args = tuple(args)
3634
      stream.write(txt % args)
3635
    else:
3636
      stream.write(txt)
3637
    stream.write("\n")
3638
    stream.flush()
3639
  except IOError, err:
3640
    if err.errno == errno.EPIPE:
3641
      # our terminal went away, we'll exit
3642
      sys.exit(constants.EXIT_FAILURE)
3643
    else:
3644
      raise
3645

    
3646

    
3647
def ToStdout(txt, *args):
3648
  """Write a message to stdout only, bypassing the logging system
3649

3650
  This is just a wrapper over _ToStream.
3651

3652
  @type txt: str
3653
  @param txt: the message
3654

3655
  """
3656
  _ToStream(sys.stdout, txt, *args)
3657

    
3658

    
3659
def ToStderr(txt, *args):
3660
  """Write a message to stderr only, bypassing the logging system
3661

3662
  This is just a wrapper over _ToStream.
3663

3664
  @type txt: str
3665
  @param txt: the message
3666

3667
  """
3668
  _ToStream(sys.stderr, txt, *args)
3669

    
3670

    
3671
class JobExecutor(object):
3672
  """Class which manages the submission and execution of multiple jobs.
3673

3674
  Note that instances of this class should not be reused between
3675
  GetResults() calls.
3676

3677
  """
3678
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3679
    self.queue = []
3680
    if cl is None:
3681
      cl = GetClient()
3682
    self.cl = cl
3683
    self.verbose = verbose
3684
    self.jobs = []
3685
    self.opts = opts
3686
    self.feedback_fn = feedback_fn
3687
    self._counter = itertools.count()
3688

    
3689
  @staticmethod
3690
  def _IfName(name, fmt):
3691
    """Helper function for formatting name.
3692

3693
    """
3694
    if name:
3695
      return fmt % name
3696

    
3697
    return ""
3698

    
3699
  def QueueJob(self, name, *ops):
3700
    """Record a job for later submit.
3701

3702
    @type name: string
3703
    @param name: a description of the job, will be used in WaitJobSet
3704

3705
    """
3706
    SetGenericOpcodeOpts(ops, self.opts)
3707
    self.queue.append((self._counter.next(), name, ops))
3708

    
3709
  def AddJobId(self, name, status, job_id):
3710
    """Adds a job ID to the internal queue.
3711

3712
    """
3713
    self.jobs.append((self._counter.next(), status, job_id, name))
3714

    
3715
  def SubmitPending(self, each=False):
3716
    """Submit all pending jobs.
3717

3718
    """
3719
    if each:
3720
      results = []
3721
      for (_, _, ops) in self.queue:
3722
        # SubmitJob will remove the success status, but raise an exception if
3723
        # the submission fails, so we'll notice that anyway.
3724
        results.append([True, self.cl.SubmitJob(ops)[0]])
3725
    else:
3726
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3727
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3728
      self.jobs.append((idx, status, data, name))
3729

    
3730
  def _ChooseJob(self):
3731
    """Choose a non-waiting/queued job to poll next.
3732

3733
    """
3734
    assert self.jobs, "_ChooseJob called with empty job list"
3735

    
3736
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3737
                               ["status"])
3738
    assert result
3739

    
3740
    for job_data, status in zip(self.jobs, result):
3741
      if (isinstance(status, list) and status and
3742
          status[0] in (constants.JOB_STATUS_QUEUED,
3743
                        constants.JOB_STATUS_WAITING,
3744
                        constants.JOB_STATUS_CANCELING)):
3745
        # job is still present and waiting
3746
        continue
3747
      # good candidate found (either running job or lost job)
3748
      self.jobs.remove(job_data)
3749
      return job_data
3750

    
3751
    # no job found
3752
    return self.jobs.pop(0)
3753

    
3754
  def GetResults(self):
3755
    """Wait for and return the results of all jobs.
3756

3757
    @rtype: list
3758
    @return: list of tuples (success, job results), in the same order
3759
        as the submitted jobs; if a job has failed, instead of the result
3760
        there will be the error message
3761

3762
    """
3763
    if not self.jobs:
3764
      self.SubmitPending()
3765
    results = []
3766
    if self.verbose:
3767
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3768
      if ok_jobs:
3769
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3770

    
3771
    # first, remove any non-submitted jobs
3772
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3773
    for idx, _, jid, name in failures:
3774
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3775
      results.append((idx, False, jid))
3776

    
3777
    while self.jobs:
3778
      (idx, _, jid, name) = self._ChooseJob()
3779
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3780
      try:
3781
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3782
        success = True
3783
      except errors.JobLost, err:
3784
        _, job_result = FormatError(err)
3785
        ToStderr("Job %s%s has been archived, cannot check its result",
3786
                 jid, self._IfName(name, " for %s"))
3787
        success = False