Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ d027b72b

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
  OSPARAMS_PRIVATE_OPT,
1755
  OSPARAMS_SECRET_OPT,
1756
  OS_SIZE_OPT,
1757
  SUBMIT_OPT,
1758
  PRINT_JOBID_OPT,
1759
  TAG_ADD_OPT,
1760
  DRY_RUN_OPT,
1761
  PRIORITY_OPT,
1762
  ]
1763

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

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

    
1781

    
1782
class _ShowUsage(Exception):
1783
  """Exception class for L{_ParseArgs}.
1784

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

1789
    @type exit_error: bool
1790
    @param exit_error: Whether to report failure on exit
1791

1792
    """
1793
    Exception.__init__(self)
1794
    self.exit_error = exit_error
1795

    
1796

    
1797
class _ShowVersion(Exception):
1798
  """Exception class for L{_ParseArgs}.
1799

1800
  """
1801

    
1802

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

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

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

1817
  """
1818
  assert not (env_override - set(commands))
1819
  assert not (set(aliases.keys()) & set(commands.keys()))
1820

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

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

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

    
1840
    cmd = aliases[cmd]
1841

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

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

    
1856
  if not _CheckArguments(cmd, args_def, args):
1857
    return None, None, None
1858

    
1859
  return func, options, args
1860

    
1861

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

1865
  @param binary: Script name
1866
  @param commands: Dictionary containing command definitions
1867

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

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

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

    
1884
  yield ""
1885

    
1886

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

1890
  Algorithm:
1891

1892
    1. Abort with error if values specified by user but none expected.
1893

1894
    1. For each argument in definition
1895

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

1900
        1. Abort with error if it's not the last argument in the definition
1901

1902
    1. If last argument has limited number of values
1903

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

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

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

    
1913
  min_count = None
1914
  max_count = None
1915
  check_max = None
1916

    
1917
  last_idx = len(args_def) - 1
1918

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

    
1925
    if max_count is None:
1926
      max_count = arg.max
1927
    elif arg.max is not None:
1928
      max_count += arg.max
1929

    
1930
    if idx == last_idx:
1931
      check_max = (arg.max is not None)
1932

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

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

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

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

    
1955
  return True
1956

    
1957

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

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

    
1967

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

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

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

    
1984

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

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

1993
  """
1994
  if selected is None:
1995
    return default
1996

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

    
2000
  return selected.split(",")
2001

    
2002

    
2003
UsesRPC = rpc.RunWithRPC
2004

    
2005

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

2009
  @param text: the question to ask
2010

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

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

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

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

    
2062

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

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

2069
  This is not an error, just a structured way to exit from clients.
2070

2071
  """
2072

    
2073

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

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

2083
  """
2084
  if cl is None:
2085
    cl = GetClient()
2086

    
2087
  job_id = cl.SubmitJob(ops)
2088

    
2089
  return job_id
2090

    
2091

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

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

2102
  """
2103
  prev_job_info = None
2104
  prev_logmsg_serial = None
2105

    
2106
  status = None
2107

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

    
2115
    if result == constants.JOB_NOTCHANGED:
2116
      report_cbs.ReportNotChanged(job_id, status)
2117

    
2118
      # Wait again
2119
      continue
2120

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

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

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

    
2139
    prev_job_info = job_info
2140

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

    
2145
  status, opstatus, result = jobs[0]
2146

    
2147
  if status == constants.JOB_STATUS_SUCCESS:
2148
    return result
2149

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

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

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

    
2164
      raise errors.OpExecError(str(msg))
2165

    
2166
  # default failure mode
2167
  raise errors.OpExecError(result)
2168

    
2169

    
2170
class JobPollCbBase:
2171
  """Base class for L{GenericPollJob} callbacks.
2172

2173
  """
2174
  def __init__(self):
2175
    """Initializes this class.
2176

2177
    """
2178

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

2183
    """
2184
    raise NotImplementedError()
2185

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

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

2194
    """
2195
    raise NotImplementedError()
2196

    
2197

    
2198
class JobPollReportCbBase:
2199
  """Base class for L{GenericPollJob} reporting callbacks.
2200

2201
  """
2202
  def __init__(self):
2203
    """Initializes this class.
2204

2205
    """
2206

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

2210
    """
2211
    raise NotImplementedError()
2212

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

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

2221
    """
2222
    raise NotImplementedError()
2223

    
2224

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

2229
    """
2230
    JobPollCbBase.__init__(self)
2231
    self.cl = cl
2232

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

2237
    """
2238
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2239
                                        prev_job_info, prev_log_serial)
2240

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

2244
    """
2245
    return self.cl.QueryJobs(job_ids, fields)
2246

    
2247

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

2252
    """
2253
    JobPollReportCbBase.__init__(self)
2254

    
2255
    self.feedback_fn = feedback_fn
2256

    
2257
    assert callable(feedback_fn)
2258

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

2262
    """
2263
    self.feedback_fn((timestamp, log_type, log_msg))
2264

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

2268
    """
2269
    # Ignore
2270

    
2271

    
2272
class StdioJobPollReportCb(JobPollReportCbBase):
2273
  def __init__(self):
2274
    """Initializes this class.
2275

2276
    """
2277
    JobPollReportCbBase.__init__(self)
2278

    
2279
    self.notified_queued = False
2280
    self.notified_waitlock = False
2281

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

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

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

2292
    """
2293
    if status is None:
2294
      return
2295

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

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

    
2304

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

2308
  """
2309
  if log_type != constants.ELOG_MESSAGE:
2310
    log_msg = str(log_msg)
2311

    
2312
  return utils.SafeEncode(log_msg)
2313

    
2314

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

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

2324
  """
2325
  if cl is None:
2326
    cl = GetClient()
2327

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

    
2336
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2337

    
2338

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

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

2346
  """
2347
  if cl is None:
2348
    cl = GetClient()
2349

    
2350
  SetGenericOpcodeOpts([op], opts)
2351

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

    
2356
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2357
                       reporter=reporter)
2358

    
2359
  return op_results[0]
2360

    
2361

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

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

    
2371

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

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

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

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

    
2394

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

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

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

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

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

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

    
2419

    
2420
def SetGenericOpcodeOpts(opcode_list, options):
2421
  """Processor for generic options.
2422

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

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

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

    
2441

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

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

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

    
2535

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

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

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

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

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

    
2566
  if aliases is None:
2567
    aliases = {}
2568

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

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

    
2589
  if func is None: # parse error
2590
    return 1
2591

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

    
2596
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2597
                     stderr_logging=True)
2598

    
2599
  logging.debug("Command line: %s", cmdline)
2600

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

    
2620
  return result
2621

    
2622

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

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

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

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

    
2641
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2642

    
2643
    nics[nidx] = ndict
2644

    
2645
  return nics
2646

    
2647

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

    
2662

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

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

2673
  """
2674
  instance = args[0]
2675

    
2676
  (pnode, snode) = SplitNodeOption(opts.node)
2677

    
2678
  hypervisor = None
2679
  hvparams = {}
2680
  if opts.hypervisor:
2681
    hypervisor, hvparams = opts.hypervisor
2682

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

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

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

    
2749
      disks[didx] = ddict
2750

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

    
2756
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2757
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2758
  FixHvParams(hvparams)
2759

    
2760
  osparams_private = opts.osparams_private or serializer.PrivateDict()
2761
  osparams_secret = opts.osparams_secret or serializer.PrivateDict()
2762

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

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

    
2817
  SubmitOrSend(op, opts)
2818
  return 0
2819

    
2820

    
2821
class _RunWhileClusterStoppedHelper:
2822
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2823

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

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

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

    
2847
    self.ssh = ssh.SshRunner(self.cluster_name)
2848

    
2849
    self.nonmaster_nodes = [name for name in online_nodes
2850
                            if name != master_node]
2851

    
2852
    assert self.master_node not in self.nonmaster_nodes
2853

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

2857
    @type node_name: string
2858
    @param node_name: Machine name
2859
    @type cmd: list
2860
    @param cmd: Command
2861

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

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

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

2882
    @type fn: callable
2883
    @param fn: Function to be called
2884

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

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

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

    
2921

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

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

2930
  """
2931
  feedback_fn("Gathering cluster information")
2932

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

    
2938
  (cluster_name, master_node) = \
2939
    cl.QueryConfigValues(["cluster_name", "master_node"])
2940

    
2941
  online_nodes = GetOnlineNodes([], cl=qcl)
2942
  ssh_ports = GetNodesSshPorts(online_nodes, qcl)
2943

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

    
2948
  assert master_node in online_nodes
2949

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

    
2953

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

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

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

    
2989
  if numfields is None:
2990
    numfields = []
2991
  if unitfields is None:
2992
    unitfields = []
2993

    
2994
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2995
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2996

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

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

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

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

    
3043
  if separator is None:
3044
    assert len(mlens) == len(fields)
3045

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

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

    
3059
  return result
3060

    
3061

    
3062
def _FormatBool(value):
3063
  """Formats a boolean value as a string.
3064

3065
  """
3066
  if value:
3067
    return "Y"
3068
  return "N"
3069

    
3070

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

    
3081

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

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

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

    
3100
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3101

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

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

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

    
3112

    
3113
class _QueryColumnFormatter:
3114
  """Callable class for formatting fields of a query.
3115

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

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

3127
    """
3128
    self._fn = fn
3129
    self._status_fn = status_fn
3130
    self._verbose = verbose
3131

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

3135
    """
3136
    (status, value) = data
3137

    
3138
    # Report status
3139
    self._status_fn(status)
3140

    
3141
    if status == constants.RS_NORMAL:
3142
      return self._fn(value)
3143

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

    
3147
    return FormatResultError(status, self._verbose)
3148

    
3149

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

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

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

    
3170

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

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

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

    
3197
  if format_override is None:
3198
    format_override = {}
3199

    
3200
  stats = dict.fromkeys(constants.RS_ALL, 0)
3201

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

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

    
3215
  table = FormatTable(result.data, columns, header, separator)
3216

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

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

    
3232
  return (status, table)
3233

    
3234

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

3238
  @type fdefs: list of L{objects.QueryFieldDefinition}
3239

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

    
3244

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

3248
  @type fdefs: list of L{objects.QueryFieldDefinition}
3249

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

    
3257
  return False
3258

    
3259

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

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

3295
  """
3296
  if not names:
3297
    names = None
3298

    
3299
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3300
                                isnumeric=isnumeric)
3301

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

    
3307
  if cl is None:
3308
    cl = GetClient()
3309

    
3310
  response = cl.Query(resource, fields, qfilter)
3311

    
3312
  found_unknown = _WarnUnknownFields(response.fields)
3313

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

    
3319
  for line in data:
3320
    ToStdout(line)
3321

    
3322
  assert ((found_unknown and status == QR_UNKNOWN) or
3323
          (not found_unknown and status != QR_UNKNOWN))
3324

    
3325
  if status == QR_UNKNOWN:
3326
    return constants.EXIT_UNKNOWN_FIELD
3327

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

    
3331

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

3335
  @type fdef: L{objects.QueryFieldDefinition}
3336
  @rtype: list
3337

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

    
3346

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

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

3358
  """
3359
  if cl is None:
3360
    cl = GetClient()
3361

    
3362
  if not fields:
3363
    fields = None
3364

    
3365
  response = cl.QueryFields(resource, fields)
3366

    
3367
  found_unknown = _WarnUnknownFields(response.fields)
3368

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

    
3376
  rows = map(_FieldDescValues, response.fields)
3377

    
3378
  for line in FormatTable(rows, columns, header, separator):
3379
    ToStdout(line)
3380

    
3381
  if found_unknown:
3382
    return constants.EXIT_UNKNOWN_FIELD
3383

    
3384
  return constants.EXIT_SUCCESS
3385

    
3386

    
3387
class TableColumn:
3388
  """Describes a column for L{FormatTable}.
3389

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

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

3401
    """
3402
    self.title = title
3403
    self.format = fn
3404
    self.align_right = align_right
3405

    
3406

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

3410
  """
3411
  if align_right:
3412
    sign = ""
3413
  else:
3414
    sign = "-"
3415

    
3416
  return "%%%s%ss" % (sign, width)
3417

    
3418

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

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

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

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

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

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

    
3451
    data.append(formatted)
3452

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

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

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

    
3465
  return [fmt % tuple(row) for row in data]
3466

    
3467

    
3468
def FormatTimestamp(ts):
3469
  """Formats a given timestamp.
3470

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

3474
  @rtype: string
3475
  @return: a string with the formatted timestamp
3476

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

    
3481
  (sec, usecs) = ts
3482
  return utils.FormatTime(sec, usecs=usecs)
3483

    
3484

    
3485
def ParseTimespec(value):
3486
  """Parse a time specification.
3487

3488
  The following suffixed will be recognized:
3489

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

3496
  Without any suffix, the value will be taken to be in seconds.
3497

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

    
3529

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

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

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

3555
  """
3556
  if cl is None:
3557
    cl = GetClient(query=True)
3558

    
3559
  qfilter = []
3560

    
3561
  if nodes:
3562
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3563

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

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

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

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

    
3582
  def _IsOffline(row):
3583
    (_, (_, offline), _) = row
3584
    return offline
3585

    
3586
  def _GetName(row):
3587
    ((_, name), _, _) = row
3588
    return name
3589

    
3590
  def _GetSip(row):
3591
    (_, _, (_, sip)) = row
3592
    return sip
3593

    
3594
  (offline, online) = compat.partition(result.data, _IsOffline)
3595

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

    
3600
  if secondary_ips:
3601
    fn = _GetSip
3602
  else:
3603
    fn = _GetName
3604

    
3605
  return map(fn, online)
3606

    
3607

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

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

    
3623

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

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

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

    
3648

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

3652
  This is just a wrapper over _ToStream.
3653

3654
  @type txt: str
3655
  @param txt: the message
3656

3657
  """
3658
  _ToStream(sys.stdout, txt, *args)
3659

    
3660

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

3664
  This is just a wrapper over _ToStream.
3665

3666
  @type txt: str
3667
  @param txt: the message
3668

3669
  """
3670
  _ToStream(sys.stderr, txt, *args)
3671

    
3672

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

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

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

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

3695
    """
3696
    if name:
3697
      return fmt % name
3698

    
3699
    return ""
3700

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

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

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

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

3714
    """
3715
    self.jobs.append((self._counter.next(), status, job_id, name))
3716

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

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

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

3735
    """
3736
    assert self.jobs, "_ChooseJob called with empty job list"
3737

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

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

    
3753
    # no job found
3754
    return self.jobs.pop(0)
3755

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

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

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

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

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