Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ a0eeb01a

History | View | Annotate | Download (140.4 kB)

1
#
2
#
3

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

    
21

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

    
24

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

    
35
from ganeti import utils
36
from ganeti import errors
37
from ganeti import constants
38
from ganeti import opcodes
39
import ganeti.rpc.errors as rpcerr
40
import ganeti.rpc.node as rpc
41
from ganeti import ssh
42
from ganeti import compat
43
from ganeti import netutils
44
from ganeti import qlang
45
from ganeti import objects
46
from ganeti import pathutils
47
from ganeti import serializer
48

    
49
from ganeti.runtime import (GetClient)
50

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

    
54

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

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

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

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

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

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

    
331

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

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

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

    
355

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

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

    
365

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

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

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

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

    
381

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

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

388
  """
389

    
390

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

394
  """
395

    
396

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

400
  """
401

    
402

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

406
  """
407

    
408

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

412
  """
413

    
414

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

418
  """
419

    
420

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

424
  """
425

    
426

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

430
  """
431

    
432

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

436
  """
437

    
438

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

442
  """
443

    
444

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

448
  """
449

    
450

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

454
  """
455

    
456

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

    
469

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

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

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

    
494

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

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

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

    
523

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

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

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

    
541

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

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

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

    
558

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

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

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

    
575

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

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

    
585

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

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

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

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

    
628

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

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

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

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

    
665

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

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

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

    
675

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

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

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

    
684

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

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

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

    
693

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

    
707

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

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

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

    
720

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

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

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

    
735

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

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

    
747

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

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

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

    
759

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

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

    
784

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

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

    
812

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

    
816

    
817
_YORNO = "yes|no"
818

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1554

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

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

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

    
1563

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1729
# options related to asynchronous job handling
1730

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

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

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

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

    
1779

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

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

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

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

    
1794

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

1798
  """
1799

    
1800

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

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

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

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

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

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

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

    
1838
    cmd = aliases[cmd]
1839

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

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

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

    
1857
  return func, options, args
1858

    
1859

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

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

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

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

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

    
1882
  yield ""
1883

    
1884

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

1888
  Algorithm:
1889

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

1892
    1. For each argument in definition
1893

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

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

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

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

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

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

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

    
1915
  last_idx = len(args_def) - 1
1916

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

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

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

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

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

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

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

    
1953
  return True
1954

    
1955

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

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

    
1965

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

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

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

    
1982

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

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

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

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

    
1998
  return selected.split(",")
1999

    
2000

    
2001
UsesRPC = rpc.RunWithRPC
2002

    
2003

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

2007
  @param text: the question to ask
2008

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

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

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

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

    
2060

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

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

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

2069
  """
2070

    
2071

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

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

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

    
2085
  job_id = cl.SubmitJob(ops)
2086

    
2087
  return job_id
2088

    
2089

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

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

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

    
2104
  status = None
2105

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

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

    
2116
      # Wait again
2117
      continue
2118

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

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

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

    
2137
    prev_job_info = job_info
2138

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

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

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

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

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

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

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

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

    
2167

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

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

2175
    """
2176

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

2181
    """
2182
    raise NotImplementedError()
2183

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

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

2192
    """
2193
    raise NotImplementedError()
2194

    
2195

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

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

2203
    """
2204

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

2208
    """
2209
    raise NotImplementedError()
2210

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

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

2219
    """
2220
    raise NotImplementedError()
2221

    
2222

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

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

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

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

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

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

    
2245

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

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

    
2253
    self.feedback_fn = feedback_fn
2254

    
2255
    assert callable(feedback_fn)
2256

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

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

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

2266
    """
2267
    # Ignore
2268

    
2269

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

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

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

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

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

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

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

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

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

    
2302

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

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

    
2310
  return utils.SafeEncode(log_msg)
2311

    
2312

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

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

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

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

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

    
2336

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

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

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

    
2348
  SetGenericOpcodeOpts([op], opts)
2349

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

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

    
2357
  return op_results[0]
2358

    
2359

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

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

    
2369

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

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

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

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

    
2392

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

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

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

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

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

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

    
2417

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

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

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

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

    
2439

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

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

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

    
2533

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2618
  return result
2619

    
2620

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

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

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

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

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

    
2641
    nics[nidx] = ndict
2642

    
2643
  return nics
2644

    
2645

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

    
2660

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

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

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

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

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

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

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

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

    
2747
      disks[didx] = ddict
2748

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

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

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

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

    
2810
  SubmitOrSend(op, opts)
2811
  return 0
2812

    
2813

    
2814
class _RunWhileClusterStoppedHelper:
2815
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2816

2817
  """
2818
  def __init__(self, feedback_fn, cluster_name, master_node,
2819
               online_nodes, ssh_ports):
2820
    """Initializes this class.
2821

2822
    @type feedback_fn: callable
2823
    @param feedback_fn: Feedback function
2824
    @type cluster_name: string
2825
    @param cluster_name: Cluster name
2826
    @type master_node: string
2827
    @param master_node Master node name
2828
    @type online_nodes: list
2829
    @param online_nodes: List of names of online nodes
2830
    @type ssh_ports: list
2831
    @param ssh_ports: List of SSH ports of online nodes
2832

2833
    """
2834
    self.feedback_fn = feedback_fn
2835
    self.cluster_name = cluster_name
2836
    self.master_node = master_node
2837
    self.online_nodes = online_nodes
2838
    self.ssh_ports = dict(zip(online_nodes, ssh_ports))
2839

    
2840
    self.ssh = ssh.SshRunner(self.cluster_name)
2841

    
2842
    self.nonmaster_nodes = [name for name in online_nodes
2843
                            if name != master_node]
2844

    
2845
    assert self.master_node not in self.nonmaster_nodes
2846

    
2847
  def _RunCmd(self, node_name, cmd):
2848
    """Runs a command on the local or a remote machine.
2849

2850
    @type node_name: string
2851
    @param node_name: Machine name
2852
    @type cmd: list
2853
    @param cmd: Command
2854

2855
    """
2856
    if node_name is None or node_name == self.master_node:
2857
      # No need to use SSH
2858
      result = utils.RunCmd(cmd)
2859
    else:
2860
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2861
                            utils.ShellQuoteArgs(cmd),
2862
                            port=self.ssh_ports[node_name])
2863

    
2864
    if result.failed:
2865
      errmsg = ["Failed to run command %s" % result.cmd]
2866
      if node_name:
2867
        errmsg.append("on node %s" % node_name)
2868
      errmsg.append(": exitcode %s and error %s" %
2869
                    (result.exit_code, result.output))
2870
      raise errors.OpExecError(" ".join(errmsg))
2871

    
2872
  def Call(self, fn, *args):
2873
    """Call function while all daemons are stopped.
2874

2875
    @type fn: callable
2876
    @param fn: Function to be called
2877

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

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

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

    
2914

    
2915
def RunWhileClusterStopped(feedback_fn, fn, *args):
2916
  """Calls a function while all cluster daemons are stopped.
2917

2918
  @type feedback_fn: callable
2919
  @param feedback_fn: Feedback function
2920
  @type fn: callable
2921
  @param fn: Function to be called when daemons are stopped
2922

2923
  """
2924
  feedback_fn("Gathering cluster information")
2925

    
2926
  # This ensures we're running on the master daemon
2927
  cl = GetClient()
2928
  # Query client
2929
  qcl = GetClient(query=True)
2930

    
2931
  (cluster_name, master_node) = \
2932
    cl.QueryConfigValues(["cluster_name", "master_node"])
2933

    
2934
  online_nodes = GetOnlineNodes([], cl=qcl)
2935
  ssh_ports = GetNodesSshPorts(online_nodes, qcl)
2936

    
2937
  # Don't keep a reference to the client. The master daemon will go away.
2938
  del cl
2939
  del qcl
2940

    
2941
  assert master_node in online_nodes
2942

    
2943
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2944
                                       online_nodes, ssh_ports).Call(fn, *args)
2945

    
2946

    
2947
def GenerateTable(headers, fields, separator, data,
2948
                  numfields=None, unitfields=None,
2949
                  units=None):
2950
  """Prints a table with headers and different fields.
2951

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

2975
  """
2976
  if units is None:
2977
    if separator:
2978
      units = "m"
2979
    else:
2980
      units = "h"
2981

    
2982
  if numfields is None:
2983
    numfields = []
2984
  if unitfields is None:
2985
    unitfields = []
2986

    
2987
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2988
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2989

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

    
3004
  if separator is None:
3005
    mlens = [0 for name in fields]
3006
    format_str = " ".join(format_fields)
3007
  else:
3008
    format_str = separator.replace("%", "%%").join(format_fields)
3009

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

    
3025
  result = []
3026
  if headers:
3027
    args = []
3028
    for idx, name in enumerate(fields):
3029
      hdr = headers[name]
3030
      if separator is None:
3031
        mlens[idx] = max(mlens[idx], len(hdr))
3032
        args.append(mlens[idx])
3033
      args.append(hdr)
3034
    result.append(format_str % tuple(args))
3035

    
3036
  if separator is None:
3037
    assert len(mlens) == len(fields)
3038

    
3039
    if fields and not numfields.Matches(fields[-1]):
3040
      mlens[-1] = 0
3041

    
3042
  for line in data:
3043
    args = []
3044
    if line is None:
3045
      line = ["-" for _ in fields]
3046
    for idx in range(len(fields)):
3047
      if separator is None:
3048
        args.append(mlens[idx])
3049
      args.append(line[idx])
3050
    result.append(format_str % tuple(args))
3051

    
3052
  return result
3053

    
3054

    
3055
def _FormatBool(value):
3056
  """Formats a boolean value as a string.
3057

3058
  """
3059
  if value:
3060
    return "Y"
3061
  return "N"
3062

    
3063

    
3064
#: Default formatting for query results; (callback, align right)
3065
_DEFAULT_FORMAT_QUERY = {
3066
  constants.QFT_TEXT: (str, False),
3067
  constants.QFT_BOOL: (_FormatBool, False),
3068
  constants.QFT_NUMBER: (str, True),
3069
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
3070
  constants.QFT_OTHER: (str, False),
3071
  constants.QFT_UNKNOWN: (str, False),
3072
  }
3073

    
3074

    
3075
def _GetColumnFormatter(fdef, override, unit):
3076
  """Returns formatting function for a field.
3077

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

3088
  """
3089
  fmt = override.get(fdef.name, None)
3090
  if fmt is not None:
3091
    return fmt
3092

    
3093
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3094

    
3095
  if fdef.kind == constants.QFT_UNIT:
3096
    # Can't keep this information in the static dictionary
3097
    return (lambda value: utils.FormatUnit(value, unit), True)
3098

    
3099
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
3100
  if fmt is not None:
3101
    return fmt
3102

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

    
3105

    
3106
class _QueryColumnFormatter:
3107
  """Callable class for formatting fields of a query.
3108

3109
  """
3110
  def __init__(self, fn, status_fn, verbose):
3111
    """Initializes this class.
3112

3113
    @type fn: callable
3114
    @param fn: Formatting function
3115
    @type status_fn: callable
3116
    @param status_fn: Function to report fields' status
3117
    @type verbose: boolean
3118
    @param verbose: whether to use verbose field descriptions or not
3119

3120
    """
3121
    self._fn = fn
3122
    self._status_fn = status_fn
3123
    self._verbose = verbose
3124

    
3125
  def __call__(self, data):
3126
    """Returns a field's string representation.
3127

3128
    """
3129
    (status, value) = data
3130

    
3131
    # Report status
3132
    self._status_fn(status)
3133

    
3134
    if status == constants.RS_NORMAL:
3135
      return self._fn(value)
3136

    
3137
    assert value is None, \
3138
           "Found value %r for abnormal status %s" % (value, status)
3139

    
3140
    return FormatResultError(status, self._verbose)
3141

    
3142

    
3143
def FormatResultError(status, verbose):
3144
  """Formats result status other than L{constants.RS_NORMAL}.
3145

3146
  @param status: The result status
3147
  @type verbose: boolean
3148
  @param verbose: Whether to return the verbose text
3149
  @return: Text of result status
3150

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

    
3163

    
3164
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3165
                      header=False, verbose=False):
3166
  """Formats data in L{objects.QueryResponse}.
3167

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

3183
  """
3184
  if unit is None:
3185
    if separator:
3186
      unit = "m"
3187
    else:
3188
      unit = "h"
3189

    
3190
  if format_override is None:
3191
    format_override = {}
3192

    
3193
  stats = dict.fromkeys(constants.RS_ALL, 0)
3194

    
3195
  def _RecordStatus(status):
3196
    if status in stats:
3197
      stats[status] += 1
3198

    
3199
  columns = []
3200
  for fdef in result.fields:
3201
    assert fdef.title and fdef.name
3202
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3203
    columns.append(TableColumn(fdef.title,
3204
                               _QueryColumnFormatter(fn, _RecordStatus,
3205
                                                     verbose),
3206
                               align_right))
3207

    
3208
  table = FormatTable(result.data, columns, header, separator)
3209

    
3210
  # Collect statistics
3211
  assert len(stats) == len(constants.RS_ALL)
3212
  assert compat.all(count >= 0 for count in stats.values())
3213

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

    
3225
  return (status, table)
3226

    
3227

    
3228
def _GetUnknownFields(fdefs):
3229
  """Returns list of unknown fields included in C{fdefs}.
3230

3231
  @type fdefs: list of L{objects.QueryFieldDefinition}
3232

3233
  """
3234
  return [fdef for fdef in fdefs
3235
          if fdef.kind == constants.QFT_UNKNOWN]
3236

    
3237

    
3238
def _WarnUnknownFields(fdefs):
3239
  """Prints a warning to stderr if a query included unknown fields.
3240

3241
  @type fdefs: list of L{objects.QueryFieldDefinition}
3242

3243
  """
3244
  unknown = _GetUnknownFields(fdefs)
3245
  if unknown:
3246
    ToStderr("Warning: Queried for unknown fields %s",
3247
             utils.CommaJoin(fdef.name for fdef in unknown))
3248
    return True
3249

    
3250
  return False
3251

    
3252

    
3253
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3254
                format_override=None, verbose=False, force_filter=False,
3255
                namefield=None, qfilter=None, isnumeric=False):
3256
  """Generic implementation for listing all items of a resource.
3257

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

3288
  """
3289
  if not names:
3290
    names = None
3291

    
3292
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3293
                                isnumeric=isnumeric)
3294

    
3295
  if qfilter is None:
3296
    qfilter = namefilter
3297
  elif namefilter is not None:
3298
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3299

    
3300
  if cl is None:
3301
    cl = GetClient()
3302

    
3303
  response = cl.Query(resource, fields, qfilter)
3304

    
3305
  found_unknown = _WarnUnknownFields(response.fields)
3306

    
3307
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3308
                                     header=header,
3309
                                     format_override=format_override,
3310
                                     verbose=verbose)
3311

    
3312
  for line in data:
3313
    ToStdout(line)
3314

    
3315
  assert ((found_unknown and status == QR_UNKNOWN) or
3316
          (not found_unknown and status != QR_UNKNOWN))
3317

    
3318
  if status == QR_UNKNOWN:
3319
    return constants.EXIT_UNKNOWN_FIELD
3320

    
3321
  # TODO: Should the list command fail if not all data could be collected?
3322
  return constants.EXIT_SUCCESS
3323

    
3324

    
3325
def _FieldDescValues(fdef):
3326
  """Helper function for L{GenericListFields} to get query field description.
3327

3328
  @type fdef: L{objects.QueryFieldDefinition}
3329
  @rtype: list
3330

3331
  """
3332
  return [
3333
    fdef.name,
3334
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3335
    fdef.title,
3336
    fdef.doc,
3337
    ]
3338

    
3339

    
3340
def GenericListFields(resource, fields, separator, header, cl=None):
3341
  """Generic implementation for listing fields for a resource.
3342

3343
  @param resource: One of L{constants.QR_VIA_LUXI}
3344
  @type fields: list of strings
3345
  @param fields: List of fields to query for
3346
  @type separator: string or None
3347
  @param separator: String used to separate fields
3348
  @type header: bool
3349
  @param header: Whether to show header row
3350

3351
  """
3352
  if cl is None:
3353
    cl = GetClient()
3354

    
3355
  if not fields:
3356
    fields = None
3357

    
3358
  response = cl.QueryFields(resource, fields)
3359

    
3360
  found_unknown = _WarnUnknownFields(response.fields)
3361

    
3362
  columns = [
3363
    TableColumn("Name", str, False),
3364
    TableColumn("Type", str, False),
3365
    TableColumn("Title", str, False),
3366
    TableColumn("Description", str, False),
3367
    ]
3368

    
3369
  rows = map(_FieldDescValues, response.fields)
3370

    
3371
  for line in FormatTable(rows, columns, header, separator):
3372
    ToStdout(line)
3373

    
3374
  if found_unknown:
3375
    return constants.EXIT_UNKNOWN_FIELD
3376

    
3377
  return constants.EXIT_SUCCESS
3378

    
3379

    
3380
class TableColumn:
3381
  """Describes a column for L{FormatTable}.
3382

3383
  """
3384
  def __init__(self, title, fn, align_right):
3385
    """Initializes this class.
3386

3387
    @type title: string
3388
    @param title: Column title
3389
    @type fn: callable
3390
    @param fn: Formatting function
3391
    @type align_right: bool
3392
    @param align_right: Whether to align values on the right-hand side
3393

3394
    """
3395
    self.title = title
3396
    self.format = fn
3397
    self.align_right = align_right
3398

    
3399

    
3400
def _GetColFormatString(width, align_right):
3401
  """Returns the format string for a field.
3402

3403
  """
3404
  if align_right:
3405
    sign = ""
3406
  else:
3407
    sign = "-"
3408

    
3409
  return "%%%s%ss" % (sign, width)
3410

    
3411

    
3412
def FormatTable(rows, columns, header, separator):
3413
  """Formats data as a table.
3414

3415
  @type rows: list of lists
3416
  @param rows: Row data, one list per row
3417
  @type columns: list of L{TableColumn}
3418
  @param columns: Column descriptions
3419
  @type header: bool
3420
  @param header: Whether to show header row
3421
  @type separator: string or None
3422
  @param separator: String used to separate columns
3423

3424
  """
3425
  if header:
3426
    data = [[col.title for col in columns]]
3427
    colwidth = [len(col.title) for col in columns]
3428
  else:
3429
    data = []
3430
    colwidth = [0 for _ in columns]
3431

    
3432
  # Format row data
3433
  for row in rows:
3434
    assert len(row) == len(columns)
3435

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

    
3438
    if separator is None:
3439
      # Update column widths
3440
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3441
        # Modifying a list's items while iterating is fine
3442
        colwidth[idx] = max(oldwidth, len(value))
3443

    
3444
    data.append(formatted)
3445

    
3446
  if separator is not None:
3447
    # Return early if a separator is used
3448
    return [separator.join(row) for row in data]
3449

    
3450
  if columns and not columns[-1].align_right:
3451
    # Avoid unnecessary spaces at end of line
3452
    colwidth[-1] = 0
3453

    
3454
  # Build format string
3455
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3456
                  for col, width in zip(columns, colwidth)])
3457

    
3458
  return [fmt % tuple(row) for row in data]
3459

    
3460

    
3461
def FormatTimestamp(ts):
3462
  """Formats a given timestamp.
3463

3464
  @type ts: timestamp
3465
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3466

3467
  @rtype: string
3468
  @return: a string with the formatted timestamp
3469

3470
  """
3471
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3472
    return "?"
3473

    
3474
  (sec, usecs) = ts
3475
  return utils.FormatTime(sec, usecs=usecs)
3476

    
3477

    
3478
def ParseTimespec(value):
3479
  """Parse a time specification.
3480

3481
  The following suffixed will be recognized:
3482

3483
    - s: seconds
3484
    - m: minutes
3485
    - h: hours
3486
    - d: day
3487
    - w: weeks
3488

3489
  Without any suffix, the value will be taken to be in seconds.
3490

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

    
3522

    
3523
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3524
                   filter_master=False, nodegroup=None):
3525
  """Returns the names of online nodes.
3526

3527
  This function will also log a warning on stderr with the names of
3528
  the online nodes.
3529

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

3548
  """
3549
  if cl is None:
3550
    cl = GetClient(query=True)
3551

    
3552
  qfilter = []
3553

    
3554
  if nodes:
3555
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3556

    
3557
  if nodegroup is not None:
3558
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3559
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3560

    
3561
  if filter_master:
3562
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3563

    
3564
  if qfilter:
3565
    if len(qfilter) > 1:
3566
      final_filter = [qlang.OP_AND] + qfilter
3567
    else:
3568
      assert len(qfilter) == 1
3569
      final_filter = qfilter[0]
3570
  else:
3571
    final_filter = None
3572

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

    
3575
  def _IsOffline(row):
3576
    (_, (_, offline), _) = row
3577
    return offline
3578

    
3579
  def _GetName(row):
3580
    ((_, name), _, _) = row
3581
    return name
3582

    
3583
  def _GetSip(row):
3584
    (_, _, (_, sip)) = row
3585
    return sip
3586

    
3587
  (offline, online) = compat.partition(result.data, _IsOffline)
3588

    
3589
  if offline and not nowarn:
3590
    ToStderr("Note: skipping offline node(s): %s" %
3591
             utils.CommaJoin(map(_GetName, offline)))
3592

    
3593
  if secondary_ips:
3594
    fn = _GetSip
3595
  else:
3596
    fn = _GetName
3597

    
3598
  return map(fn, online)
3599

    
3600

    
3601
def GetNodesSshPorts(nodes, cl):
3602
  """Retrieves SSH ports of given nodes.
3603

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

    
3616

    
3617
def _ToStream(stream, txt, *args):
3618
  """Write a message to a stream, bypassing the logging system
3619

3620
  @type stream: file object
3621
  @param stream: the file to which we should write
3622
  @type txt: str
3623
  @param txt: the message
3624

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

    
3641

    
3642
def ToStdout(txt, *args):
3643
  """Write a message to stdout only, bypassing the logging system
3644

3645
  This is just a wrapper over _ToStream.
3646

3647
  @type txt: str
3648
  @param txt: the message
3649

3650
  """
3651
  _ToStream(sys.stdout, txt, *args)
3652

    
3653

    
3654
def ToStderr(txt, *args):
3655
  """Write a message to stderr only, bypassing the logging system
3656

3657
  This is just a wrapper over _ToStream.
3658

3659
  @type txt: str
3660
  @param txt: the message
3661

3662
  """
3663
  _ToStream(sys.stderr, txt, *args)
3664

    
3665

    
3666
class JobExecutor(object):
3667
  """Class which manages the submission and execution of multiple jobs.
3668

3669
  Note that instances of this class should not be reused between
3670
  GetResults() calls.
3671

3672
  """
3673
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3674
    self.queue = []
3675
    if cl is None:
3676
      cl = GetClient()
3677
    self.cl = cl
3678
    self.verbose = verbose
3679
    self.jobs = []
3680
    self.opts = opts
3681
    self.feedback_fn = feedback_fn
3682
    self._counter = itertools.count()
3683

    
3684
  @staticmethod
3685
  def _IfName(name, fmt):
3686
    """Helper function for formatting name.
3687

3688
    """
3689
    if name:
3690
      return fmt % name
3691

    
3692
    return ""
3693

    
3694
  def QueueJob(self, name, *ops):
3695
    """Record a job for later submit.
3696

3697
    @type name: string
3698
    @param name: a description of the job, will be used in WaitJobSet
3699

3700
    """
3701
    SetGenericOpcodeOpts(ops, self.opts)
3702
    self.queue.append((self._counter.next(), name, ops))
3703

    
3704
  def AddJobId(self, name, status, job_id):
3705
    """Adds a job ID to the internal queue.
3706

3707
    """
3708
    self.jobs.append((self._counter.next(), status, job_id, name))
3709

    
3710
  def SubmitPending(self, each=False):
3711
    """Submit all pending jobs.
3712

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

    
3725
  def _ChooseJob(self):
3726
    """Choose a non-waiting/queued job to poll next.
3727

3728
    """
3729
    assert self.jobs, "_ChooseJob called with empty job list"
3730

    
3731
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3732
                               ["status"])
3733
    assert result
3734

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

    
3746
    # no job found
3747
    return self.jobs.pop(0)
3748

    
3749
  def GetResults(self):
3750
    """Wait for and return the results of all jobs.
3751

3752
    @rtype: list
3753
    @return: list of tuples (success, job results), in the same order
3754
        as the submitted jobs; if a job has failed, instead of the result
3755
        there will be the error message
3756

3757
    """
3758
    if not self.jobs:
3759
      self.SubmitPending()
3760
    results = []
3761
    if self.verbose:
3762
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3763
      if ok_jobs:
3764
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3765

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

    
3772
    while self.jobs:
3773
      (idx, _, jid, name) = self._ChooseJob()
3774
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3775
      try:
3776
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3777
        success = True
3778
      except errors.JobLost, err:
3779
        _, job_result = FormatError(err)
3780
        ToStderr("Job %s%s has been archived, cannot check its result",
3781
                 jid, self._IfName(name, " for %s"))
3782
        success = False
3783
      except (errors.GenericError, rpcerr.ProtocolError), err:
3784
        _, job_result = FormatError(err)
3785
        success = False
3786
        # the error message will always be shown, verbose or not
3787
        ToStderr("Job %s%s has failed: %s",
3788
                 jid, self._IfName(name, " for %s"), job_result)
3789

    
3790
      results.append((idx, success, job_result))
3791

    
3792
    # sort based on the index, then drop it
3793
    results.sort()
3794
    results = [i[1:] for i in results]
3795

    
3796
    return results
3797

    
3798
  def WaitOrShow(self, wait):
3799
    """Wait for job results or only print the job IDs.
3800

3801
    @type wait: boolean
3802
    @param wait: whether to wait or not
3803

3804
    """
3805
    if wait:
3806
      return self.GetResults()
3807
    else:
3808
      if not self.jobs:
3809
        self.SubmitPending()
3810
      for _, status, result, name in self.jobs:
3811
        if status:
3812
          ToStdout("%s: %s", result, name)
3813
        else:
3814
          ToStderr("Failure for %s: %s", name, result)
3815
      return [row[1:3] for row in self.jobs]
3816

    
3817

    
3818
def FormatParamsDictInfo(param_dict, actual):
3819
  """Formats a parameter dictionary.
3820

3821
  @type param_dict: dict
3822
  @param param_dict: the own parameters
3823
  @type actual: dict
3824
  @param actual: the current parameter set (including defaults)
3825
  @rtype: dict
3826
  @return: dictionary where the value of each parameter is either a fully
3827
      formatted string or a dictionary containing formatted strings
3828

3829
  """
3830
  ret = {}
3831
  for (key, data) in actual.items():
3832
    if isinstance(data, dict) and data:
3833
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3834
    else:
3835
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3836
  return ret
3837

    
3838

    
3839
def _FormatListInfoDefault(data, def_data):
3840
  if data is not None:
3841
    ret = utils.CommaJoin(data)
3842
  else:
3843
    ret = "default (%s)" % utils.CommaJoin(def_data)
3844
  return ret
3845

    
3846

    
3847
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3848
  """Formats an instance policy.
3849

3850
  @type custom_ipolicy: dict
3851
  @param custom_ipolicy: own policy
3852
  @type eff_ipolicy: dict
3853
  @param eff_ipolicy: effective policy (including defaults); ignored for
3854
      cluster
3855
  @type iscluster: bool
3856
  @param iscluster: the policy is at cluster level
3857
  @rtype: list of pairs
3858
  @return: formatted data, suitable for L{PrintGenericInfo}
3859

3860
  """
3861
  if iscluster:
3862
    eff_ipolicy = custom_ipolicy
3863

    
3864
  minmax_out = []
3865
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3866
  if custom_minmax:
3867
    for (k, minmax) in enumerate(custom_minmax):
3868
      minmax_out.append([
3869
        ("%s/%s" % (key, k),
3870
         FormatParamsDictInfo(minmax[key], minmax[key]))
3871
        for key in constants.ISPECS_MINMAX_KEYS
3872
        ])
3873
  else:
3874
    for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]):
3875
      minmax_out.append([
3876
        ("%s/%s" % (key, k),
3877
         FormatParamsDictInfo({}, minmax[key]))
3878
        for key in constants.ISPECS_MINMAX_KEYS
3879
        ])
3880
  ret = [("bounds specs", minmax_out)]
3881

    
3882
  if iscluster:
3883
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3884
    ret.append(
3885
      (constants.ISPECS_STD,
3886
       FormatParamsDictInfo(stdspecs, stdspecs))
3887
      )
3888

    
3889
  ret.append(
3890
    ("allowed disk templates",
3891
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3892
                            eff_ipolicy[constants.IPOLICY_DTS]))
3893
    )
3894
  ret.extend([
3895
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3896
    for key in constants.IPOLICY_PARAMETERS
3897
    ])
3898
  return ret
3899

    
3900

    
3901
def _PrintSpecsParameters(buf, specs):
3902
  values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items()))
3903
  buf.write(",".join(values))
3904

    
3905

    
3906
def PrintIPolicyCommand(buf, ipolicy, isgroup):
3907
  """Print the command option used to generate the given instance policy.
3908

3909
  Currently only the parts dealing with specs are supported.
3910

3911
  @type buf: StringIO
3912
  @param buf: stream to write into
3913
  @type ipolicy: dict
3914
  @param ipolicy: instance policy
3915
  @type isgroup: bool
3916
  @param isgroup: whether the policy is at group level
3917

3918
  """
3919
  if not isgroup:
3920
    stdspecs = ipolicy.get("std")
3921
    if stdspecs:
3922
      buf.write(" %s " % IPOLICY_STD_SPECS_STR)
3923
      _PrintSpecsParameters(buf, stdspecs)
3924
  minmaxes = ipolicy.get("minmax", [])
3925
  first = True
3926
  for minmax in minmaxes:
3927
    minspecs = minmax.get("min")
3928
    maxspecs = minmax.get("max")
3929
    if minspecs and maxspecs:
3930
      if first:
3931
        buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
3932
        first = False
3933
      else:
3934
        buf.write("//")
3935
      buf.write("min:")
3936
      _PrintSpecsParameters(buf, minspecs)
3937
      buf.write("/max:")
3938
      _PrintSpecsParameters(buf, maxspecs)
3939

    
3940

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

3944
  This function is used to request confirmation for doing an operation
3945
  on a given list of list_type.
3946

3947
  @type names: list
3948
  @param names: the list of names that we display when
3949
      we ask for confirmation
3950
  @type list_type: str
3951
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3952
  @type text: str
3953
  @param text: the operation that the user should confirm
3954
  @rtype: boolean
3955
  @return: True or False depending on user's confirmation.
3956

3957
  """
3958
  count = len(names)
3959
  msg = ("The %s will operate on %d %s.\n%s"
3960
         "Do you want to continue?" % (text, count, list_type, extra))
3961
  affected = (("\nAffected %s:\n" % list_type) +
3962
              "\n".join(["  %s" % name for name in names]))
3963

    
3964
  choices = [("y", True, "Yes, execute the %s" % text),
3965
             ("n", False, "No, abort the %s" % text)]
3966

    
3967
  if count > 20:
3968
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3969
    question = msg
3970
  else:
3971
    question = msg + affected
3972

    
3973
  choice = AskUser(question, choices)
3974
  if choice == "v":
3975
    choices.pop(1)
3976
    choice = AskUser(msg + affected, choices)
3977
  return choice
3978

    
3979

    
3980
def _MaybeParseUnit(elements):
3981
  """Parses and returns an array of potential values with units.
3982

3983
  """
3984
  parsed = {}
3985
  for k, v in elements.items():
3986
    if v == constants.VALUE_DEFAULT:
3987
      parsed[k] = v
3988
    else:
3989
      parsed[k] = utils.ParseUnit(v)
3990
  return parsed
3991

    
3992

    
3993
def _InitISpecsFromSplitOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
3994
                             ispecs_disk_count, ispecs_disk_size,
3995
                             ispecs_nic_count, group_ipolicy, fill_all):
3996
  try:
3997
    if ispecs_mem_size:
3998
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
3999
    if ispecs_disk_size:
4000
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
4001
  except (TypeError, ValueError, errors.UnitParseError), err:
4002
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
4003
                               " in policy: %s" %
4004
                               (ispecs_disk_size, ispecs_mem_size, err),
4005
                               errors.ECODE_INVAL)
4006

    
4007
  # prepare ipolicy dict
4008
  ispecs_transposed = {
4009
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
4010
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
4011
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
4012
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
4013
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
4014
    }
4015

    
4016
  # first, check that the values given are correct
4017
  if group_ipolicy:
4018
    forced_type = TISPECS_GROUP_TYPES
4019
  else:
4020
    forced_type = TISPECS_CLUSTER_TYPES
4021
  for specs in ispecs_transposed.values():
4022
    assert type(specs) is dict
4023
    utils.ForceDictType(specs, forced_type)
4024

    
4025
  # then transpose
4026
  ispecs = {
4027
    constants.ISPECS_MIN: {},
4028
    constants.ISPECS_MAX: {},
4029
    constants.ISPECS_STD: {},
4030
    }
4031
  for (name, specs) in ispecs_transposed.iteritems():
4032
    assert name in constants.ISPECS_PARAMETERS
4033
    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
4034
      assert key in ispecs
4035
      ispecs[key][name] = val
4036
  minmax_out = {}
4037
  for key in constants.ISPECS_MINMAX_KEYS:
4038
    if fill_all:
4039
      minmax_out[key] = \
4040
        objects.FillDict(constants.ISPECS_MINMAX_DEFAULTS[key], ispecs[key])
4041
    else:
4042
      minmax_out[key] = ispecs[key]
4043
  ipolicy[constants.ISPECS_MINMAX] = [minmax_out]
4044
  if fill_all:
4045
    ipolicy[constants.ISPECS_STD] = \
4046
        objects.FillDict(constants.IPOLICY_DEFAULTS[constants.ISPECS_STD],
4047
                         ispecs[constants.ISPECS_STD])
4048
  else:
4049
    ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
4050

    
4051

    
4052
def _ParseSpecUnit(spec, keyname):
4053
  ret = spec.copy()
4054
  for k in [constants.ISPEC_DISK_SIZE, constants.ISPEC_MEM_SIZE]:
4055
    if k in ret:
4056
      try:
4057
        ret[k] = utils.ParseUnit(ret[k])
4058
      except (TypeError, ValueError, errors.UnitParseError), err:
4059
        raise errors.OpPrereqError(("Invalid parameter %s (%s) in %s instance"
4060
                                    " specs: %s" % (k, ret[k], keyname, err)),
4061
                                   errors.ECODE_INVAL)
4062
  return ret
4063

    
4064

    
4065
def _ParseISpec(spec, keyname, required):
4066
  ret = _ParseSpecUnit(spec, keyname)
4067
  utils.ForceDictType(ret, constants.ISPECS_PARAMETER_TYPES)
4068
  missing = constants.ISPECS_PARAMETERS - frozenset(ret.keys())
4069
  if required and missing:
4070
    raise errors.OpPrereqError("Missing parameters in ipolicy spec %s: %s" %
4071
                               (keyname, utils.CommaJoin(missing)),
4072
                               errors.ECODE_INVAL)
4073
  return ret
4074

    
4075

    
4076
def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
4077
  ret = None
4078
  if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and
4079
      len(minmax_ispecs[0]) == 1):
4080
    for (key, spec) in minmax_ispecs[0].items():
4081
      # This loop is executed exactly once
4082
      if key in allowed_values and not spec:
4083
        ret = key
4084
  return ret
4085

    
4086

    
4087
def _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4088
                            group_ipolicy, allowed_values):
4089
  found_allowed = _GetISpecsInAllowedValues(minmax_ispecs, allowed_values)
4090
  if found_allowed is not None:
4091
    ipolicy_out[constants.ISPECS_MINMAX] = found_allowed
4092
  elif minmax_ispecs is not None:
4093
    minmax_out = []
4094
    for mmpair in minmax_ispecs:
4095
      mmpair_out = {}
4096
      for (key, spec) in mmpair.items():
4097
        if key not in constants.ISPECS_MINMAX_KEYS:
4098
          msg = "Invalid key in bounds instance specifications: %s" % key
4099
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
4100
        mmpair_out[key] = _ParseISpec(spec, key, True)
4101
      minmax_out.append(mmpair_out)
4102
    ipolicy_out[constants.ISPECS_MINMAX] = minmax_out
4103
  if std_ispecs is not None:
4104
    assert not group_ipolicy # This is not an option for gnt-group
4105
    ipolicy_out[constants.ISPECS_STD] = _ParseISpec(std_ispecs, "std", False)
4106

    
4107

    
4108
def CreateIPolicyFromOpts(ispecs_mem_size=None,
4109
                          ispecs_cpu_count=None,
4110
                          ispecs_disk_count=None,
4111
                          ispecs_disk_size=None,
4112
                          ispecs_nic_count=None,
4113
                          minmax_ispecs=None,
4114
                          std_ispecs=None,
4115
                          ipolicy_disk_templates=None,
4116
                          ipolicy_vcpu_ratio=None,
4117
                          ipolicy_spindle_ratio=None,
4118
                          group_ipolicy=False,
4119
                          allowed_values=None,
4120
                          fill_all=False):
4121
  """Creation of instance policy based on command line options.
4122

4123
  @param fill_all: whether for cluster policies we should ensure that
4124
    all values are filled
4125

4126
  """
4127
  assert not (fill_all and allowed_values)
4128

    
4129
  split_specs = (ispecs_mem_size or ispecs_cpu_count or ispecs_disk_count or
4130
                 ispecs_disk_size or ispecs_nic_count)
4131
  if (split_specs and (minmax_ispecs is not None or std_ispecs is not None)):
4132
    raise errors.OpPrereqError("A --specs-xxx option cannot be specified"
4133
                               " together with any --ipolicy-xxx-specs option",
4134
                               errors.ECODE_INVAL)
4135

    
4136
  ipolicy_out = objects.MakeEmptyIPolicy()
4137
  if split_specs:
4138
    assert fill_all
4139
    _InitISpecsFromSplitOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
4140
                             ispecs_disk_count, ispecs_disk_size,
4141
                             ispecs_nic_count, group_ipolicy, fill_all)
4142
  elif (minmax_ispecs is not None or std_ispecs is not None):
4143
    _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4144
                            group_ipolicy, allowed_values)
4145

    
4146
  if ipolicy_disk_templates is not None:
4147
    if allowed_values and ipolicy_disk_templates in allowed_values:
4148
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
4149
    else:
4150
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
4151
  if ipolicy_vcpu_ratio is not None:
4152
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
4153
  if ipolicy_spindle_ratio is not None:
4154
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
4155

    
4156
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
4157

    
4158
  if not group_ipolicy and fill_all:
4159
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
4160

    
4161
  return ipolicy_out
4162

    
4163

    
4164
def _SerializeGenericInfo(buf, data, level, afterkey=False):
4165
  """Formatting core of L{PrintGenericInfo}.
4166

4167
  @param buf: (string) stream to accumulate the result into
4168
  @param data: data to format
4169
  @type level: int
4170
  @param level: depth in the data hierarchy, used for indenting
4171
  @type afterkey: bool
4172
  @param afterkey: True when we are in the middle of a line after a key (used
4173
      to properly add newlines or indentation)
4174

4175
  """
4176
  baseind = "  "
4177
  if isinstance(data, dict):
4178
    if not data:
4179
      buf.write("\n")
4180
    else:
4181
      if afterkey:
4182
        buf.write("\n")
4183
        doindent = True
4184
      else:
4185
        doindent = False
4186
      for key in sorted(data):
4187
        if doindent:
4188
          buf.write(baseind * level)
4189
        else:
4190
          doindent = True
4191
        buf.write(key)
4192
        buf.write(": ")
4193
        _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True)
4194
  elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple):
4195
    # list of tuples (an ordered dictionary)
4196
    if afterkey:
4197
      buf.write("\n")
4198
      doindent = True
4199
    else:
4200
      doindent = False
4201
    for (key, val) in data:
4202
      if doindent:
4203
        buf.write(baseind * level)
4204
      else:
4205
        doindent = True
4206
      buf.write(key)
4207
      buf.write(": ")
4208
      _SerializeGenericInfo(buf, val, level + 1, afterkey=True)
4209
  elif isinstance(data, list):
4210
    if not data:
4211
      buf.write("\n")
4212
    else:
4213
      if afterkey:
4214
        buf.write("\n")
4215
        doindent = True
4216
      else:
4217
        doindent = False
4218
      for item in data:
4219
        if doindent:
4220
          buf.write(baseind * level)
4221
        else:
4222
          doindent = True
4223
        buf.write("-")
4224
        buf.write(baseind[1:])
4225
        _SerializeGenericInfo(buf, item, level + 1)
4226
  else:
4227
    # This branch should be only taken for strings, but it's practically
4228
    # impossible to guarantee that no other types are produced somewhere
4229
    buf.write(str(data))
4230
    buf.write("\n")
4231

    
4232

    
4233
def PrintGenericInfo(data):
4234
  """Print information formatted according to the hierarchy.
4235

4236
  The output is a valid YAML string.
4237

4238
  @param data: the data to print. It's a hierarchical structure whose elements
4239
      can be:
4240
        - dictionaries, where keys are strings and values are of any of the
4241
          types listed here
4242
        - lists of pairs (key, value), where key is a string and value is of
4243
          any of the types listed here; it's a way to encode ordered
4244
          dictionaries
4245
        - lists of any of the types listed here
4246
        - strings
4247

4248
  """
4249
  buf = StringIO()
4250
  _SerializeGenericInfo(buf, data, 0)
4251
  ToStdout(buf.getvalue().rstrip("\n"))