Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 0fcb3314

History | View | Annotate | Download (140.9 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
  "INSTANCE_COMMUNICATION_NETWORK_OPT",
189
  "RUNTIME_MEM_OPT",
190
  "ROMAN_OPT",
191
  "SECONDARY_IP_OPT",
192
  "SECONDARY_ONLY_OPT",
193
  "SELECT_OS_OPT",
194
  "SEP_OPT",
195
  "SHOWCMD_OPT",
196
  "SHOW_MACHINE_OPT",
197
  "COMPRESS_OPT",
198
  "SHUTDOWN_TIMEOUT_OPT",
199
  "SINGLE_NODE_OPT",
200
  "SPECS_CPU_COUNT_OPT",
201
  "SPECS_DISK_COUNT_OPT",
202
  "SPECS_DISK_SIZE_OPT",
203
  "SPECS_MEM_SIZE_OPT",
204
  "SPECS_NIC_COUNT_OPT",
205
  "SPLIT_ISPECS_OPTS",
206
  "IPOLICY_STD_SPECS_OPT",
207
  "IPOLICY_DISK_TEMPLATES",
208
  "IPOLICY_VCPU_RATIO",
209
  "SPICE_CACERT_OPT",
210
  "SPICE_CERT_OPT",
211
  "SRC_DIR_OPT",
212
  "SRC_NODE_OPT",
213
  "SUBMIT_OPT",
214
  "SUBMIT_OPTS",
215
  "STARTUP_PAUSED_OPT",
216
  "STATIC_OPT",
217
  "SYNC_OPT",
218
  "TAG_ADD_OPT",
219
  "TAG_SRC_OPT",
220
  "TIMEOUT_OPT",
221
  "TO_GROUP_OPT",
222
  "UIDPOOL_OPT",
223
  "USEUNITS_OPT",
224
  "USE_EXTERNAL_MIP_SCRIPT",
225
  "USE_REPL_NET_OPT",
226
  "VERBOSE_OPT",
227
  "VG_NAME_OPT",
228
  "WFSYNC_OPT",
229
  "YES_DOIT_OPT",
230
  "DISK_STATE_OPT",
231
  "HV_STATE_OPT",
232
  "IGNORE_IPOLICY_OPT",
233
  "INSTANCE_POLICY_OPTS",
234
  # Generic functions for CLI programs
235
  "ConfirmOperation",
236
  "CreateIPolicyFromOpts",
237
  "GenericMain",
238
  "GenericInstanceCreate",
239
  "GenericList",
240
  "GenericListFields",
241
  "GetClient",
242
  "GetOnlineNodes",
243
  "GetNodesSshPorts",
244
  "JobExecutor",
245
  "JobSubmittedException",
246
  "ParseTimespec",
247
  "RunWhileClusterStopped",
248
  "SubmitOpCode",
249
  "SubmitOpCodeToDrainedQueue",
250
  "SubmitOrSend",
251
  "UsesRPC",
252
  # Formatting functions
253
  "ToStderr", "ToStdout",
254
  "FormatError",
255
  "FormatQueryResult",
256
  "FormatParamsDictInfo",
257
  "FormatPolicyInfo",
258
  "PrintIPolicyCommand",
259
  "PrintGenericInfo",
260
  "GenerateTable",
261
  "AskUser",
262
  "FormatTimestamp",
263
  "FormatLogMessage",
264
  # Tags functions
265
  "ListTags",
266
  "AddTags",
267
  "RemoveTags",
268
  # command line options support infrastructure
269
  "ARGS_MANY_INSTANCES",
270
  "ARGS_MANY_NODES",
271
  "ARGS_MANY_GROUPS",
272
  "ARGS_MANY_NETWORKS",
273
  "ARGS_NONE",
274
  "ARGS_ONE_INSTANCE",
275
  "ARGS_ONE_NODE",
276
  "ARGS_ONE_GROUP",
277
  "ARGS_ONE_OS",
278
  "ARGS_ONE_NETWORK",
279
  "ArgChoice",
280
  "ArgCommand",
281
  "ArgFile",
282
  "ArgGroup",
283
  "ArgHost",
284
  "ArgInstance",
285
  "ArgJobId",
286
  "ArgNetwork",
287
  "ArgNode",
288
  "ArgOs",
289
  "ArgExtStorage",
290
  "ArgSuggest",
291
  "ArgUnknown",
292
  "OPT_COMPL_INST_ADD_NODES",
293
  "OPT_COMPL_MANY_NODES",
294
  "OPT_COMPL_ONE_IALLOCATOR",
295
  "OPT_COMPL_ONE_INSTANCE",
296
  "OPT_COMPL_ONE_NODE",
297
  "OPT_COMPL_ONE_NODEGROUP",
298
  "OPT_COMPL_ONE_NETWORK",
299
  "OPT_COMPL_ONE_OS",
300
  "OPT_COMPL_ONE_EXTSTORAGE",
301
  "cli_option",
302
  "FixHvParams",
303
  "SplitNodeOption",
304
  "CalculateOSNames",
305
  "ParseFields",
306
  "COMMON_CREATE_OPTS",
307
  ]
308

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

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

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

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

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

    
332

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

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

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

    
356

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

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

    
366

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

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

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

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

    
382

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

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

389
  """
390

    
391

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

395
  """
396

    
397

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

401
  """
402

    
403

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

407
  """
408

    
409

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

413
  """
414

    
415

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

419
  """
420

    
421

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

425
  """
426

    
427

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

431
  """
432

    
433

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

437
  """
438

    
439

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

443
  """
444

    
445

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

449
  """
450

    
451

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

455
  """
456

    
457

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

    
470

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

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

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

    
495

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

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

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

    
524

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

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

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

    
542

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

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

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

    
559

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

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

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

    
576

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

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

    
586

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

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

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

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

    
629

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

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

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

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

    
666

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

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

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

    
676

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

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

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

    
685

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

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

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

    
694

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

    
708

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

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

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

    
721

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

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

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

    
736

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

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

    
748

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

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

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

    
760

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

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

    
785

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

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

    
813

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

    
817

    
818
_YORNO = "yes|no"
819

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1306
INSTANCE_COMMUNICATION_NETWORK_OPT = \
1307
    cli_option("--instance-communication-network",
1308
               dest="instance_communication_network",
1309
               type="string",
1310
               help="Set the network name for instance communication")
1311

    
1312
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1313
                         help=("Enables LVM and specifies the volume group"
1314
                               " name (cluster-wide) for disk allocation"
1315
                               " [%s]" % constants.DEFAULT_VG),
1316
                         metavar="VG", default=None)
1317

    
1318
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1319
                          help="Destroy cluster", action="store_true")
1320

    
1321
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1322
                          help="Skip node agreement check (dangerous)",
1323
                          action="store_true", default=False)
1324

    
1325
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1326
                            help="Specify the mac prefix for the instance IP"
1327
                            " addresses, in the format XX:XX:XX",
1328
                            metavar="PREFIX",
1329
                            default=None)
1330

    
1331
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1332
                               help="Specify the node interface (cluster-wide)"
1333
                               " on which the master IP address will be added"
1334
                               " (cluster init default: %s)" %
1335
                               constants.DEFAULT_BRIDGE,
1336
                               metavar="NETDEV",
1337
                               default=None)
1338

    
1339
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1340
                                help="Specify the netmask of the master IP",
1341
                                metavar="NETMASK",
1342
                                default=None)
1343

    
1344
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1345
                                     dest="use_external_mip_script",
1346
                                     help="Specify whether to run a"
1347
                                     " user-provided script for the master"
1348
                                     " IP address turnup and"
1349
                                     " turndown operations",
1350
                                     type="bool", metavar=_YORNO, default=None)
1351

    
1352
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1353
                                help="Specify the default directory (cluster-"
1354
                                "wide) for storing the file-based disks [%s]" %
1355
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1356
                                metavar="DIR",
1357
                                default=None)
1358

    
1359
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1360
  "--shared-file-storage-dir",
1361
  dest="shared_file_storage_dir",
1362
  help="Specify the default directory (cluster-wide) for storing the"
1363
  " shared file-based disks [%s]" %
1364
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1365
  metavar="SHAREDDIR", default=None)
1366

    
1367
GLOBAL_GLUSTER_FILEDIR_OPT = cli_option(
1368
  "--gluster-storage-dir",
1369
  dest="gluster_storage_dir",
1370
  help="Specify the default directory (cluster-wide) for mounting Gluster"
1371
  " file systems [%s]" %
1372
  pathutils.DEFAULT_GLUSTER_STORAGE_DIR,
1373
  metavar="GLUSTERDIR",
1374
  default=pathutils.DEFAULT_GLUSTER_STORAGE_DIR)
1375

    
1376
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1377
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1378
                                   action="store_false", default=True)
1379

    
1380
MODIFY_ETCHOSTS_OPT = \
1381
 cli_option("--modify-etc-hosts", dest="modify_etc_hosts", metavar=_YORNO,
1382
            default=None, type="bool",
1383
            help="Defines whether the cluster should autonomously modify"
1384
            " and keep in sync the /etc/hosts file of the nodes")
1385

    
1386
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1387
                                    help="Don't initialize SSH keys",
1388
                                    action="store_false", default=True)
1389

    
1390
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1391
                             help="Enable parseable error messages",
1392
                             action="store_true", default=False)
1393

    
1394
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1395
                          help="Skip N+1 memory redundancy tests",
1396
                          action="store_true", default=False)
1397

    
1398
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1399
                             help="Type of reboot: soft/hard/full",
1400
                             default=constants.INSTANCE_REBOOT_HARD,
1401
                             metavar="<REBOOT>",
1402
                             choices=list(constants.REBOOT_TYPES))
1403

    
1404
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1405
                                    dest="ignore_secondaries",
1406
                                    default=False, action="store_true",
1407
                                    help="Ignore errors from secondaries")
1408

    
1409
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1410
                            action="store_false", default=True,
1411
                            help="Don't shutdown the instance (unsafe)")
1412

    
1413
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1414
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1415
                         help="Maximum time to wait")
1416

    
1417
COMPRESS_OPT = cli_option("--compress", dest="compress",
1418
                          default=constants.IEC_NONE,
1419
                          help="The compression mode to use",
1420
                          choices=list(constants.IEC_ALL))
1421

    
1422
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1423
                                  dest="shutdown_timeout", type="int",
1424
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1425
                                  help="Maximum time to wait for instance"
1426
                                  " shutdown")
1427

    
1428
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1429
                          default=None,
1430
                          help=("Number of seconds between repetions of the"
1431
                                " command"))
1432

    
1433
EARLY_RELEASE_OPT = cli_option("--early-release",
1434
                               dest="early_release", default=False,
1435
                               action="store_true",
1436
                               help="Release the locks on the secondary"
1437
                               " node(s) early")
1438

    
1439
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1440
                                  dest="new_cluster_cert",
1441
                                  default=False, action="store_true",
1442
                                  help="Generate a new cluster certificate")
1443

    
1444
NEW_NODE_CERT_OPT = cli_option(
1445
  "--new-node-certificates", dest="new_node_cert", default=False,
1446
  action="store_true", help="Generate new node certificates (for all nodes)")
1447

    
1448
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1449
                           default=None,
1450
                           help="File containing new RAPI certificate")
1451

    
1452
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1453
                               default=None, action="store_true",
1454
                               help=("Generate a new self-signed RAPI"
1455
                                     " certificate"))
1456

    
1457
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1458
                            default=None,
1459
                            help="File containing new SPICE certificate")
1460

    
1461
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1462
                              default=None,
1463
                              help="File containing the certificate of the CA"
1464
                              " which signed the SPICE certificate")
1465

    
1466
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1467
                                dest="new_spice_cert", default=None,
1468
                                action="store_true",
1469
                                help=("Generate a new self-signed SPICE"
1470
                                      " certificate"))
1471

    
1472
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1473
                                    dest="new_confd_hmac_key",
1474
                                    default=False, action="store_true",
1475
                                    help=("Create a new HMAC key for %s" %
1476
                                          constants.CONFD))
1477

    
1478
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1479
                                       dest="cluster_domain_secret",
1480
                                       default=None,
1481
                                       help=("Load new new cluster domain"
1482
                                             " secret from file"))
1483

    
1484
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1485
                                           dest="new_cluster_domain_secret",
1486
                                           default=False, action="store_true",
1487
                                           help=("Create a new cluster domain"
1488
                                                 " secret"))
1489

    
1490
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1491
                              dest="use_replication_network",
1492
                              help="Whether to use the replication network"
1493
                              " for talking to the nodes",
1494
                              action="store_true", default=False)
1495

    
1496
MAINTAIN_NODE_HEALTH_OPT = \
1497
    cli_option("--maintain-node-health", dest="maintain_node_health",
1498
               metavar=_YORNO, default=None, type="bool",
1499
               help="Configure the cluster to automatically maintain node"
1500
               " health, by shutting down unknown instances, shutting down"
1501
               " unknown DRBD devices, etc.")
1502

    
1503
IDENTIFY_DEFAULTS_OPT = \
1504
    cli_option("--identify-defaults", dest="identify_defaults",
1505
               default=False, action="store_true",
1506
               help="Identify which saved instance parameters are equal to"
1507
               " the current cluster defaults and set them as such, instead"
1508
               " of marking them as overridden")
1509

    
1510
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1511
                         action="store", dest="uid_pool",
1512
                         help=("A list of user-ids or user-id"
1513
                               " ranges separated by commas"))
1514

    
1515
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1516
                          action="store", dest="add_uids",
1517
                          help=("A list of user-ids or user-id"
1518
                                " ranges separated by commas, to be"
1519
                                " added to the user-id pool"))
1520

    
1521
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1522
                             action="store", dest="remove_uids",
1523
                             help=("A list of user-ids or user-id"
1524
                                   " ranges separated by commas, to be"
1525
                                   " removed from the user-id pool"))
1526

    
1527
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1528
                              action="store", dest="reserved_lvs",
1529
                              help=("A comma-separated list of reserved"
1530
                                    " logical volumes names, that will be"
1531
                                    " ignored by cluster verify"))
1532

    
1533
ROMAN_OPT = cli_option("--roman",
1534
                       dest="roman_integers", default=False,
1535
                       action="store_true",
1536
                       help="Use roman numbers for positive integers")
1537

    
1538
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1539
                             action="store", default=None,
1540
                             help="Specifies usermode helper for DRBD")
1541

    
1542
PRIMARY_IP_VERSION_OPT = \
1543
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1544
               action="store", dest="primary_ip_version",
1545
               metavar="%d|%d" % (constants.IP4_VERSION,
1546
                                  constants.IP6_VERSION),
1547
               help="Cluster-wide IP version for primary IP")
1548

    
1549
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1550
                              action="store_true",
1551
                              help="Show machine name for every line in output")
1552

    
1553
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1554
                              action="store_true",
1555
                              help=("Hide successful results and show failures"
1556
                                    " only (determined by the exit code)"))
1557

    
1558
REASON_OPT = cli_option("--reason", default=None,
1559
                        help="The reason for executing the command")
1560

    
1561

    
1562
def _PriorityOptionCb(option, _, value, parser):
1563
  """Callback for processing C{--priority} option.
1564

1565
  """
1566
  value = _PRIONAME_TO_VALUE[value]
1567

    
1568
  setattr(parser.values, option.dest, value)
1569

    
1570

    
1571
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1572
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1573
                          choices=_PRIONAME_TO_VALUE.keys(),
1574
                          action="callback", type="choice",
1575
                          callback=_PriorityOptionCb,
1576
                          help="Priority for opcode processing")
1577

    
1578
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1579
                        type="bool", default=None, metavar=_YORNO,
1580
                        help="Sets the hidden flag on the OS")
1581

    
1582
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1583
                        type="bool", default=None, metavar=_YORNO,
1584
                        help="Sets the blacklisted flag on the OS")
1585

    
1586
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1587
                                     type="bool", metavar=_YORNO,
1588
                                     dest="prealloc_wipe_disks",
1589
                                     help=("Wipe disks prior to instance"
1590
                                           " creation"))
1591

    
1592
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1593
                             type="keyval", default=None,
1594
                             help="Node parameters")
1595

    
1596
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1597
                              action="store", metavar="POLICY", default=None,
1598
                              help="Allocation policy for the node group")
1599

    
1600
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1601
                              type="bool", metavar=_YORNO,
1602
                              dest="node_powered",
1603
                              help="Specify if the SoR for node is powered")
1604

    
1605
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1606
                             default=constants.OOB_TIMEOUT,
1607
                             help="Maximum time to wait for out-of-band helper")
1608

    
1609
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1610
                             default=constants.OOB_POWER_DELAY,
1611
                             help="Time in seconds to wait between power-ons")
1612

    
1613
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1614
                              action="store_true", default=False,
1615
                              help=("Whether command argument should be treated"
1616
                                    " as filter"))
1617

    
1618
NO_REMEMBER_OPT = cli_option("--no-remember",
1619
                             dest="no_remember",
1620
                             action="store_true", default=False,
1621
                             help="Perform but do not record the change"
1622
                             " in the configuration")
1623

    
1624
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1625
                              default=False, action="store_true",
1626
                              help="Evacuate primary instances only")
1627

    
1628
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1629
                                default=False, action="store_true",
1630
                                help="Evacuate secondary instances only"
1631
                                     " (applies only to internally mirrored"
1632
                                     " disk templates, e.g. %s)" %
1633
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1634

    
1635
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1636
                                action="store_true", default=False,
1637
                                help="Pause instance at startup")
1638

    
1639
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1640
                          help="Destination node group (name or uuid)",
1641
                          default=None, action="append",
1642
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1643

    
1644
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1645
                               action="append", dest="ignore_errors",
1646
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1647
                               help="Error code to be ignored")
1648

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

    
1657
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1658
                          action="append",
1659
                          help=("Specify hypervisor state information in the"
1660
                                " format hypervisor:option=value,...;"
1661
                                " note this is unused for now"),
1662
                          type="identkeyval")
1663

    
1664
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1665
                                action="store_true", default=False,
1666
                                help="Ignore instance policy violations")
1667

    
1668
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1669
                             help="Sets the instance's runtime memory,"
1670
                             " ballooning it up or down to the new value",
1671
                             default=None, type="unit", metavar="<size>")
1672

    
1673
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1674
                          action="store_true", default=False,
1675
                          help="Marks the grow as absolute instead of the"
1676
                          " (default) relative mode")
1677

    
1678
NETWORK_OPT = cli_option("--network",
1679
                         action="store", default=None, dest="network",
1680
                         help="IP network in CIDR notation")
1681

    
1682
GATEWAY_OPT = cli_option("--gateway",
1683
                         action="store", default=None, dest="gateway",
1684
                         help="IP address of the router (gateway)")
1685

    
1686
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1687
                                  action="store", default=None,
1688
                                  dest="add_reserved_ips",
1689
                                  help="Comma-separated list of"
1690
                                  " reserved IPs to add")
1691

    
1692
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1693
                                     action="store", default=None,
1694
                                     dest="remove_reserved_ips",
1695
                                     help="Comma-delimited list of"
1696
                                     " reserved IPs to remove")
1697

    
1698
NETWORK6_OPT = cli_option("--network6",
1699
                          action="store", default=None, dest="network6",
1700
                          help="IP network in CIDR notation")
1701

    
1702
GATEWAY6_OPT = cli_option("--gateway6",
1703
                          action="store", default=None, dest="gateway6",
1704
                          help="IP6 address of the router (gateway)")
1705

    
1706
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1707
                                  dest="conflicts_check",
1708
                                  default=True,
1709
                                  action="store_false",
1710
                                  help="Don't check for conflicting IPs")
1711

    
1712
INCLUDEDEFAULTS_OPT = cli_option("--include-defaults", dest="include_defaults",
1713
                                 default=False, action="store_true",
1714
                                 help="Include default values")
1715

    
1716
HOTPLUG_OPT = cli_option("--hotplug", dest="hotplug",
1717
                         action="store_true", default=False,
1718
                         help="Hotplug supported devices (NICs and Disks)")
1719

    
1720
HOTPLUG_IF_POSSIBLE_OPT = cli_option("--hotplug-if-possible",
1721
                                     dest="hotplug_if_possible",
1722
                                     action="store_true", default=False,
1723
                                     help="Hotplug devices in case"
1724
                                          " hotplug is supported")
1725

    
1726
INSTANCE_COMMUNICATION_OPT = \
1727
    cli_option("-c", "--communication",
1728
               default=False,
1729
               dest="instance_communication",
1730
               help=constants.INSTANCE_COMMUNICATION_DOC,
1731
               type="bool")
1732

    
1733
#: Options provided by all commands
1734
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1735

    
1736
# options related to asynchronous job handling
1737

    
1738
SUBMIT_OPTS = [
1739
  SUBMIT_OPT,
1740
  PRINT_JOBID_OPT,
1741
  ]
1742

    
1743
# common options for creating instances. add and import then add their own
1744
# specific ones.
1745
COMMON_CREATE_OPTS = [
1746
  BACKEND_OPT,
1747
  DISK_OPT,
1748
  DISK_TEMPLATE_OPT,
1749
  FILESTORE_DIR_OPT,
1750
  FILESTORE_DRIVER_OPT,
1751
  HYPERVISOR_OPT,
1752
  IALLOCATOR_OPT,
1753
  NET_OPT,
1754
  NODE_PLACEMENT_OPT,
1755
  NOIPCHECK_OPT,
1756
  NOCONFLICTSCHECK_OPT,
1757
  NONAMECHECK_OPT,
1758
  NONICS_OPT,
1759
  NWSYNC_OPT,
1760
  OSPARAMS_OPT,
1761
  OSPARAMS_PRIVATE_OPT,
1762
  OSPARAMS_SECRET_OPT,
1763
  OS_SIZE_OPT,
1764
  SUBMIT_OPT,
1765
  PRINT_JOBID_OPT,
1766
  TAG_ADD_OPT,
1767
  DRY_RUN_OPT,
1768
  PRIORITY_OPT,
1769
  ]
1770

    
1771
# common instance policy options
1772
INSTANCE_POLICY_OPTS = [
1773
  IPOLICY_BOUNDS_SPECS_OPT,
1774
  IPOLICY_DISK_TEMPLATES,
1775
  IPOLICY_VCPU_RATIO,
1776
  IPOLICY_SPINDLE_RATIO,
1777
  ]
1778

    
1779
# instance policy split specs options
1780
SPLIT_ISPECS_OPTS = [
1781
  SPECS_CPU_COUNT_OPT,
1782
  SPECS_DISK_COUNT_OPT,
1783
  SPECS_DISK_SIZE_OPT,
1784
  SPECS_MEM_SIZE_OPT,
1785
  SPECS_NIC_COUNT_OPT,
1786
  ]
1787

    
1788

    
1789
class _ShowUsage(Exception):
1790
  """Exception class for L{_ParseArgs}.
1791

1792
  """
1793
  def __init__(self, exit_error):
1794
    """Initializes instances of this class.
1795

1796
    @type exit_error: bool
1797
    @param exit_error: Whether to report failure on exit
1798

1799
    """
1800
    Exception.__init__(self)
1801
    self.exit_error = exit_error
1802

    
1803

    
1804
class _ShowVersion(Exception):
1805
  """Exception class for L{_ParseArgs}.
1806

1807
  """
1808

    
1809

    
1810
def _ParseArgs(binary, argv, commands, aliases, env_override):
1811
  """Parser for the command line arguments.
1812

1813
  This function parses the arguments and returns the function which
1814
  must be executed together with its (modified) arguments.
1815

1816
  @param binary: Script name
1817
  @param argv: Command line arguments
1818
  @param commands: Dictionary containing command definitions
1819
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1820
  @param env_override: list of env variables allowed for default args
1821
  @raise _ShowUsage: If usage description should be shown
1822
  @raise _ShowVersion: If version should be shown
1823

1824
  """
1825
  assert not (env_override - set(commands))
1826
  assert not (set(aliases.keys()) & set(commands.keys()))
1827

    
1828
  if len(argv) > 1:
1829
    cmd = argv[1]
1830
  else:
1831
    # No option or command given
1832
    raise _ShowUsage(exit_error=True)
1833

    
1834
  if cmd == "--version":
1835
    raise _ShowVersion()
1836
  elif cmd == "--help":
1837
    raise _ShowUsage(exit_error=False)
1838
  elif not (cmd in commands or cmd in aliases):
1839
    raise _ShowUsage(exit_error=True)
1840

    
1841
  # get command, unalias it, and look it up in commands
1842
  if cmd in aliases:
1843
    if aliases[cmd] not in commands:
1844
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1845
                                   " command '%s'" % (cmd, aliases[cmd]))
1846

    
1847
    cmd = aliases[cmd]
1848

    
1849
  if cmd in env_override:
1850
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1851
    env_args = os.environ.get(args_env_name)
1852
    if env_args:
1853
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1854

    
1855
  func, args_def, parser_opts, usage, description = commands[cmd]
1856
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1857
                        description=description,
1858
                        formatter=TitledHelpFormatter(),
1859
                        usage="%%prog %s %s" % (cmd, usage))
1860
  parser.disable_interspersed_args()
1861
  options, args = parser.parse_args(args=argv[2:])
1862

    
1863
  if not _CheckArguments(cmd, args_def, args):
1864
    return None, None, None
1865

    
1866
  return func, options, args
1867

    
1868

    
1869
def _FormatUsage(binary, commands):
1870
  """Generates a nice description of all commands.
1871

1872
  @param binary: Script name
1873
  @param commands: Dictionary containing command definitions
1874

1875
  """
1876
  # compute the max line length for cmd + usage
1877
  mlen = min(60, max(map(len, commands)))
1878

    
1879
  yield "Usage: %s {command} [options...] [argument...]" % binary
1880
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1881
  yield ""
1882
  yield "Commands:"
1883

    
1884
  # and format a nice command list
1885
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1886
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1887
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1888
    for line in help_lines:
1889
      yield " %-*s   %s" % (mlen, "", line)
1890

    
1891
  yield ""
1892

    
1893

    
1894
def _CheckArguments(cmd, args_def, args):
1895
  """Verifies the arguments using the argument definition.
1896

1897
  Algorithm:
1898

1899
    1. Abort with error if values specified by user but none expected.
1900

1901
    1. For each argument in definition
1902

1903
      1. Keep running count of minimum number of values (min_count)
1904
      1. Keep running count of maximum number of values (max_count)
1905
      1. If it has an unlimited number of values
1906

1907
        1. Abort with error if it's not the last argument in the definition
1908

1909
    1. If last argument has limited number of values
1910

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

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

1915
  """
1916
  if args and not args_def:
1917
    ToStderr("Error: Command %s expects no arguments", cmd)
1918
    return False
1919

    
1920
  min_count = None
1921
  max_count = None
1922
  check_max = None
1923

    
1924
  last_idx = len(args_def) - 1
1925

    
1926
  for idx, arg in enumerate(args_def):
1927
    if min_count is None:
1928
      min_count = arg.min
1929
    elif arg.min is not None:
1930
      min_count += arg.min
1931

    
1932
    if max_count is None:
1933
      max_count = arg.max
1934
    elif arg.max is not None:
1935
      max_count += arg.max
1936

    
1937
    if idx == last_idx:
1938
      check_max = (arg.max is not None)
1939

    
1940
    elif arg.max is None:
1941
      raise errors.ProgrammerError("Only the last argument can have max=None")
1942

    
1943
  if check_max:
1944
    # Command with exact number of arguments
1945
    if (min_count is not None and max_count is not None and
1946
        min_count == max_count and len(args) != min_count):
1947
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1948
      return False
1949

    
1950
    # Command with limited number of arguments
1951
    if max_count is not None and len(args) > max_count:
1952
      ToStderr("Error: Command %s expects only %d argument(s)",
1953
               cmd, max_count)
1954
      return False
1955

    
1956
  # Command with some required arguments
1957
  if min_count is not None and len(args) < min_count:
1958
    ToStderr("Error: Command %s expects at least %d argument(s)",
1959
             cmd, min_count)
1960
    return False
1961

    
1962
  return True
1963

    
1964

    
1965
def SplitNodeOption(value):
1966
  """Splits the value of a --node option.
1967

1968
  """
1969
  if value and ":" in value:
1970
    return value.split(":", 1)
1971
  else:
1972
    return (value, None)
1973

    
1974

    
1975
def CalculateOSNames(os_name, os_variants):
1976
  """Calculates all the names an OS can be called, according to its variants.
1977

1978
  @type os_name: string
1979
  @param os_name: base name of the os
1980
  @type os_variants: list or None
1981
  @param os_variants: list of supported variants
1982
  @rtype: list
1983
  @return: list of valid names
1984

1985
  """
1986
  if os_variants:
1987
    return ["%s+%s" % (os_name, v) for v in os_variants]
1988
  else:
1989
    return [os_name]
1990

    
1991

    
1992
def ParseFields(selected, default):
1993
  """Parses the values of "--field"-like options.
1994

1995
  @type selected: string or None
1996
  @param selected: User-selected options
1997
  @type default: list
1998
  @param default: Default fields
1999

2000
  """
2001
  if selected is None:
2002
    return default
2003

    
2004
  if selected.startswith("+"):
2005
    return default + selected[1:].split(",")
2006

    
2007
  return selected.split(",")
2008

    
2009

    
2010
UsesRPC = rpc.RunWithRPC
2011

    
2012

    
2013
def AskUser(text, choices=None):
2014
  """Ask the user a question.
2015

2016
  @param text: the question to ask
2017

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

2023
  @return: one of the return values from the choices list; if input is
2024
      not possible (i.e. not running with a tty, we return the last
2025
      entry from the list
2026

2027
  """
2028
  if choices is None:
2029
    choices = [("y", True, "Perform the operation"),
2030
               ("n", False, "Do not perform the operation")]
2031
  if not choices or not isinstance(choices, list):
2032
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
2033
  for entry in choices:
2034
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
2035
      raise errors.ProgrammerError("Invalid choices element to AskUser")
2036

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

    
2069

    
2070
class JobSubmittedException(Exception):
2071
  """Job was submitted, client should exit.
2072

2073
  This exception has one argument, the ID of the job that was
2074
  submitted. The handler should print this ID.
2075

2076
  This is not an error, just a structured way to exit from clients.
2077

2078
  """
2079

    
2080

    
2081
def SendJob(ops, cl=None):
2082
  """Function to submit an opcode without waiting for the results.
2083

2084
  @type ops: list
2085
  @param ops: list of opcodes
2086
  @type cl: luxi.Client
2087
  @param cl: the luxi client to use for communicating with the master;
2088
             if None, a new client will be created
2089

2090
  """
2091
  if cl is None:
2092
    cl = GetClient()
2093

    
2094
  job_id = cl.SubmitJob(ops)
2095

    
2096
  return job_id
2097

    
2098

    
2099
def GenericPollJob(job_id, cbs, report_cbs):
2100
  """Generic job-polling function.
2101

2102
  @type job_id: number
2103
  @param job_id: Job ID
2104
  @type cbs: Instance of L{JobPollCbBase}
2105
  @param cbs: Data callbacks
2106
  @type report_cbs: Instance of L{JobPollReportCbBase}
2107
  @param report_cbs: Reporting callbacks
2108

2109
  """
2110
  prev_job_info = None
2111
  prev_logmsg_serial = None
2112

    
2113
  status = None
2114

    
2115
  while True:
2116
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
2117
                                      prev_logmsg_serial)
2118
    if not result:
2119
      # job not found, go away!
2120
      raise errors.JobLost("Job with id %s lost" % job_id)
2121

    
2122
    if result == constants.JOB_NOTCHANGED:
2123
      report_cbs.ReportNotChanged(job_id, status)
2124

    
2125
      # Wait again
2126
      continue
2127

    
2128
    # Split result, a tuple of (field values, log entries)
2129
    (job_info, log_entries) = result
2130
    (status, ) = job_info
2131

    
2132
    if log_entries:
2133
      for log_entry in log_entries:
2134
        (serial, timestamp, log_type, message) = log_entry
2135
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
2136
                                    log_type, message)
2137
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
2138

    
2139
    # TODO: Handle canceled and archived jobs
2140
    elif status in (constants.JOB_STATUS_SUCCESS,
2141
                    constants.JOB_STATUS_ERROR,
2142
                    constants.JOB_STATUS_CANCELING,
2143
                    constants.JOB_STATUS_CANCELED):
2144
      break
2145

    
2146
    prev_job_info = job_info
2147

    
2148
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
2149
  if not jobs:
2150
    raise errors.JobLost("Job with id %s lost" % job_id)
2151

    
2152
  status, opstatus, result = jobs[0]
2153

    
2154
  if status == constants.JOB_STATUS_SUCCESS:
2155
    return result
2156

    
2157
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
2158
    raise errors.OpExecError("Job was canceled")
2159

    
2160
  has_ok = False
2161
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
2162
    if status == constants.OP_STATUS_SUCCESS:
2163
      has_ok = True
2164
    elif status == constants.OP_STATUS_ERROR:
2165
      errors.MaybeRaise(msg)
2166

    
2167
      if has_ok:
2168
        raise errors.OpExecError("partial failure (opcode %d): %s" %
2169
                                 (idx, msg))
2170

    
2171
      raise errors.OpExecError(str(msg))
2172

    
2173
  # default failure mode
2174
  raise errors.OpExecError(result)
2175

    
2176

    
2177
class JobPollCbBase:
2178
  """Base class for L{GenericPollJob} callbacks.
2179

2180
  """
2181
  def __init__(self):
2182
    """Initializes this class.
2183

2184
    """
2185

    
2186
  def WaitForJobChangeOnce(self, job_id, fields,
2187
                           prev_job_info, prev_log_serial):
2188
    """Waits for changes on a job.
2189

2190
    """
2191
    raise NotImplementedError()
2192

    
2193
  def QueryJobs(self, job_ids, fields):
2194
    """Returns the selected fields for the selected job IDs.
2195

2196
    @type job_ids: list of numbers
2197
    @param job_ids: Job IDs
2198
    @type fields: list of strings
2199
    @param fields: Fields
2200

2201
    """
2202
    raise NotImplementedError()
2203

    
2204

    
2205
class JobPollReportCbBase:
2206
  """Base class for L{GenericPollJob} reporting callbacks.
2207

2208
  """
2209
  def __init__(self):
2210
    """Initializes this class.
2211

2212
    """
2213

    
2214
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2215
    """Handles a log message.
2216

2217
    """
2218
    raise NotImplementedError()
2219

    
2220
  def ReportNotChanged(self, job_id, status):
2221
    """Called for if a job hasn't changed in a while.
2222

2223
    @type job_id: number
2224
    @param job_id: Job ID
2225
    @type status: string or None
2226
    @param status: Job status if available
2227

2228
    """
2229
    raise NotImplementedError()
2230

    
2231

    
2232
class _LuxiJobPollCb(JobPollCbBase):
2233
  def __init__(self, cl):
2234
    """Initializes this class.
2235

2236
    """
2237
    JobPollCbBase.__init__(self)
2238
    self.cl = cl
2239

    
2240
  def WaitForJobChangeOnce(self, job_id, fields,
2241
                           prev_job_info, prev_log_serial):
2242
    """Waits for changes on a job.
2243

2244
    """
2245
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2246
                                        prev_job_info, prev_log_serial)
2247

    
2248
  def QueryJobs(self, job_ids, fields):
2249
    """Returns the selected fields for the selected job IDs.
2250

2251
    """
2252
    return self.cl.QueryJobs(job_ids, fields)
2253

    
2254

    
2255
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2256
  def __init__(self, feedback_fn):
2257
    """Initializes this class.
2258

2259
    """
2260
    JobPollReportCbBase.__init__(self)
2261

    
2262
    self.feedback_fn = feedback_fn
2263

    
2264
    assert callable(feedback_fn)
2265

    
2266
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2267
    """Handles a log message.
2268

2269
    """
2270
    self.feedback_fn((timestamp, log_type, log_msg))
2271

    
2272
  def ReportNotChanged(self, job_id, status):
2273
    """Called if a job hasn't changed in a while.
2274

2275
    """
2276
    # Ignore
2277

    
2278

    
2279
class StdioJobPollReportCb(JobPollReportCbBase):
2280
  def __init__(self):
2281
    """Initializes this class.
2282

2283
    """
2284
    JobPollReportCbBase.__init__(self)
2285

    
2286
    self.notified_queued = False
2287
    self.notified_waitlock = False
2288

    
2289
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2290
    """Handles a log message.
2291

2292
    """
2293
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2294
             FormatLogMessage(log_type, log_msg))
2295

    
2296
  def ReportNotChanged(self, job_id, status):
2297
    """Called if a job hasn't changed in a while.
2298

2299
    """
2300
    if status is None:
2301
      return
2302

    
2303
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2304
      ToStderr("Job %s is waiting in queue", job_id)
2305
      self.notified_queued = True
2306

    
2307
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2308
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2309
      self.notified_waitlock = True
2310

    
2311

    
2312
def FormatLogMessage(log_type, log_msg):
2313
  """Formats a job message according to its type.
2314

2315
  """
2316
  if log_type != constants.ELOG_MESSAGE:
2317
    log_msg = str(log_msg)
2318

    
2319
  return utils.SafeEncode(log_msg)
2320

    
2321

    
2322
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2323
  """Function to poll for the result of a job.
2324

2325
  @type job_id: job identified
2326
  @param job_id: the job to poll for results
2327
  @type cl: luxi.Client
2328
  @param cl: the luxi client to use for communicating with the master;
2329
             if None, a new client will be created
2330

2331
  """
2332
  if cl is None:
2333
    cl = GetClient()
2334

    
2335
  if reporter is None:
2336
    if feedback_fn:
2337
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2338
    else:
2339
      reporter = StdioJobPollReportCb()
2340
  elif feedback_fn:
2341
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2342

    
2343
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2344

    
2345

    
2346
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2347
  """Legacy function to submit an opcode.
2348

2349
  This is just a simple wrapper over the construction of the processor
2350
  instance. It should be extended to better handle feedback and
2351
  interaction functions.
2352

2353
  """
2354
  if cl is None:
2355
    cl = GetClient()
2356

    
2357
  SetGenericOpcodeOpts([op], opts)
2358

    
2359
  job_id = SendJob([op], cl=cl)
2360
  if hasattr(opts, "print_jobid") and opts.print_jobid:
2361
    ToStdout("%d" % job_id)
2362

    
2363
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2364
                       reporter=reporter)
2365

    
2366
  return op_results[0]
2367

    
2368

    
2369
def SubmitOpCodeToDrainedQueue(op):
2370
  """Forcefully insert a job in the queue, even if it is drained.
2371

2372
  """
2373
  cl = GetClient()
2374
  job_id = cl.SubmitJobToDrainedQueue([op])
2375
  op_results = PollJob(job_id, cl=cl)
2376
  return op_results[0]
2377

    
2378

    
2379
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2380
  """Wrapper around SubmitOpCode or SendJob.
2381

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

2387
  It will also process the opcodes if we're sending the via SendJob
2388
  (otherwise SubmitOpCode does it).
2389

2390
  """
2391
  if opts and opts.submit_only:
2392
    job = [op]
2393
    SetGenericOpcodeOpts(job, opts)
2394
    job_id = SendJob(job, cl=cl)
2395
    if opts.print_jobid:
2396
      ToStdout("%d" % job_id)
2397
    raise JobSubmittedException(job_id)
2398
  else:
2399
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2400

    
2401

    
2402
def _InitReasonTrail(op, opts):
2403
  """Builds the first part of the reason trail
2404

2405
  Builds the initial part of the reason trail, adding the user provided reason
2406
  (if it exists) and the name of the command starting the operation.
2407

2408
  @param op: the opcode the reason trail will be added to
2409
  @param opts: the command line options selected by the user
2410

2411
  """
2412
  assert len(sys.argv) >= 2
2413
  trail = []
2414

    
2415
  if opts.reason:
2416
    trail.append((constants.OPCODE_REASON_SRC_USER,
2417
                  opts.reason,
2418
                  utils.EpochNano()))
2419

    
2420
  binary = os.path.basename(sys.argv[0])
2421
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2422
  command = sys.argv[1]
2423
  trail.append((source, command, utils.EpochNano()))
2424
  op.reason = trail
2425

    
2426

    
2427
def SetGenericOpcodeOpts(opcode_list, options):
2428
  """Processor for generic options.
2429

2430
  This function updates the given opcodes based on generic command
2431
  line options (like debug, dry-run, etc.).
2432

2433
  @param opcode_list: list of opcodes
2434
  @param options: command line options or None
2435
  @return: None (in-place modification)
2436

2437
  """
2438
  if not options:
2439
    return
2440
  for op in opcode_list:
2441
    op.debug_level = options.debug
2442
    if hasattr(options, "dry_run"):
2443
      op.dry_run = options.dry_run
2444
    if getattr(options, "priority", None) is not None:
2445
      op.priority = options.priority
2446
    _InitReasonTrail(op, options)
2447

    
2448

    
2449
def FormatError(err):
2450
  """Return a formatted error message for a given error.
2451

2452
  This function takes an exception instance and returns a tuple
2453
  consisting of two values: first, the recommended exit code, and
2454
  second, a string describing the error message (not
2455
  newline-terminated).
2456

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

    
2542

    
2543
def GenericMain(commands, override=None, aliases=None,
2544
                env_override=frozenset()):
2545
  """Generic main function for all the gnt-* commands.
2546

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

2556
  """
2557
  # save the program name and the entire command line for later logging
2558
  if sys.argv:
2559
    binary = os.path.basename(sys.argv[0])
2560
    if not binary:
2561
      binary = sys.argv[0]
2562

    
2563
    if len(sys.argv) >= 2:
2564
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2565
    else:
2566
      logname = binary
2567

    
2568
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2569
  else:
2570
    binary = "<unknown program>"
2571
    cmdline = "<unknown>"
2572

    
2573
  if aliases is None:
2574
    aliases = {}
2575

    
2576
  try:
2577
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2578
                                       env_override)
2579
  except _ShowVersion:
2580
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2581
             constants.RELEASE_VERSION)
2582
    return constants.EXIT_SUCCESS
2583
  except _ShowUsage, err:
2584
    for line in _FormatUsage(binary, commands):
2585
      ToStdout(line)
2586

    
2587
    if err.exit_error:
2588
      return constants.EXIT_FAILURE
2589
    else:
2590
      return constants.EXIT_SUCCESS
2591
  except errors.ParameterError, err:
2592
    result, err_msg = FormatError(err)
2593
    ToStderr(err_msg)
2594
    return 1
2595

    
2596
  if func is None: # parse error
2597
    return 1
2598

    
2599
  if override is not None:
2600
    for key, val in override.iteritems():
2601
      setattr(options, key, val)
2602

    
2603
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2604
                     stderr_logging=True)
2605

    
2606
  logging.debug("Command line: %s", cmdline)
2607

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

    
2627
  return result
2628

    
2629

    
2630
def ParseNicOption(optvalue):
2631
  """Parses the value of the --net option(s).
2632

2633
  """
2634
  try:
2635
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2636
  except (TypeError, ValueError), err:
2637
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2638
                               errors.ECODE_INVAL)
2639

    
2640
  nics = [{}] * nic_max
2641
  for nidx, ndict in optvalue:
2642
    nidx = int(nidx)
2643

    
2644
    if not isinstance(ndict, dict):
2645
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2646
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2647

    
2648
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2649

    
2650
    nics[nidx] = ndict
2651

    
2652
  return nics
2653

    
2654

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

    
2669

    
2670
def GenericInstanceCreate(mode, opts, args):
2671
  """Add an instance to the cluster via either creation or import.
2672

2673
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2674
  @param opts: the command line options selected by the user
2675
  @type args: list
2676
  @param args: should contain only one element, the new instance name
2677
  @rtype: int
2678
  @return: the desired exit code
2679

2680
  """
2681
  instance = args[0]
2682

    
2683
  (pnode, snode) = SplitNodeOption(opts.node)
2684

    
2685
  hypervisor = None
2686
  hvparams = {}
2687
  if opts.hypervisor:
2688
    hypervisor, hvparams = opts.hypervisor
2689

    
2690
  if opts.nics:
2691
    nics = ParseNicOption(opts.nics)
2692
  elif opts.no_nics:
2693
    # no nics
2694
    nics = []
2695
  elif mode == constants.INSTANCE_CREATE:
2696
    # default of one nic, all auto
2697
    nics = [{}]
2698
  else:
2699
    # mode == import
2700
    nics = []
2701

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

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

    
2756
      disks[didx] = ddict
2757

    
2758
  if opts.tags is not None:
2759
    tags = opts.tags.split(",")
2760
  else:
2761
    tags = []
2762

    
2763
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2764
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2765
  FixHvParams(hvparams)
2766

    
2767
  osparams_private = opts.osparams_private or serializer.PrivateDict()
2768
  osparams_secret = opts.osparams_secret or serializer.PrivateDict()
2769

    
2770
  if mode == constants.INSTANCE_CREATE:
2771
    start = opts.start
2772
    os_type = opts.os
2773
    force_variant = opts.force_variant
2774
    src_node = None
2775
    src_path = None
2776
    no_install = opts.no_install
2777
    identify_defaults = False
2778
    compress = constants.IEC_NONE
2779
    instance_communication = opts.instance_communication
2780
  elif mode == constants.INSTANCE_IMPORT:
2781
    start = False
2782
    os_type = None
2783
    force_variant = False
2784
    src_node = opts.src_node
2785
    src_path = opts.src_dir
2786
    no_install = None
2787
    identify_defaults = opts.identify_defaults
2788
    compress = opts.compress
2789
    instance_communication = False
2790
  else:
2791
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2792

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

    
2824
  SubmitOrSend(op, opts)
2825
  return 0
2826

    
2827

    
2828
class _RunWhileClusterStoppedHelper:
2829
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2830

2831
  """
2832
  def __init__(self, feedback_fn, cluster_name, master_node,
2833
               online_nodes, ssh_ports):
2834
    """Initializes this class.
2835

2836
    @type feedback_fn: callable
2837
    @param feedback_fn: Feedback function
2838
    @type cluster_name: string
2839
    @param cluster_name: Cluster name
2840
    @type master_node: string
2841
    @param master_node Master node name
2842
    @type online_nodes: list
2843
    @param online_nodes: List of names of online nodes
2844
    @type ssh_ports: list
2845
    @param ssh_ports: List of SSH ports of online nodes
2846

2847
    """
2848
    self.feedback_fn = feedback_fn
2849
    self.cluster_name = cluster_name
2850
    self.master_node = master_node
2851
    self.online_nodes = online_nodes
2852
    self.ssh_ports = dict(zip(online_nodes, ssh_ports))
2853

    
2854
    self.ssh = ssh.SshRunner(self.cluster_name)
2855

    
2856
    self.nonmaster_nodes = [name for name in online_nodes
2857
                            if name != master_node]
2858

    
2859
    assert self.master_node not in self.nonmaster_nodes
2860

    
2861
  def _RunCmd(self, node_name, cmd):
2862
    """Runs a command on the local or a remote machine.
2863

2864
    @type node_name: string
2865
    @param node_name: Machine name
2866
    @type cmd: list
2867
    @param cmd: Command
2868

2869
    """
2870
    if node_name is None or node_name == self.master_node:
2871
      # No need to use SSH
2872
      result = utils.RunCmd(cmd)
2873
    else:
2874
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2875
                            utils.ShellQuoteArgs(cmd),
2876
                            port=self.ssh_ports[node_name])
2877

    
2878
    if result.failed:
2879
      errmsg = ["Failed to run command %s" % result.cmd]
2880
      if node_name:
2881
        errmsg.append("on node %s" % node_name)
2882
      errmsg.append(": exitcode %s and error %s" %
2883
                    (result.exit_code, result.output))
2884
      raise errors.OpExecError(" ".join(errmsg))
2885

    
2886
  def Call(self, fn, *args):
2887
    """Call function while all daemons are stopped.
2888

2889
    @type fn: callable
2890
    @param fn: Function to be called
2891

2892
    """
2893
    # Pause watcher by acquiring an exclusive lock on watcher state file
2894
    self.feedback_fn("Blocking watcher")
2895
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2896
    try:
2897
      # TODO: Currently, this just blocks. There's no timeout.
2898
      # TODO: Should it be a shared lock?
2899
      watcher_block.Exclusive(blocking=True)
2900

    
2901
      # Stop master daemons, so that no new jobs can come in and all running
2902
      # ones are finished
2903
      self.feedback_fn("Stopping master daemons")
2904
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2905
      try:
2906
        # Stop daemons on all nodes
2907
        for node_name in self.online_nodes:
2908
          self.feedback_fn("Stopping daemons on %s" % node_name)
2909
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2910

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

    
2928

    
2929
def RunWhileClusterStopped(feedback_fn, fn, *args):
2930
  """Calls a function while all cluster daemons are stopped.
2931

2932
  @type feedback_fn: callable
2933
  @param feedback_fn: Feedback function
2934
  @type fn: callable
2935
  @param fn: Function to be called when daemons are stopped
2936

2937
  """
2938
  feedback_fn("Gathering cluster information")
2939

    
2940
  # This ensures we're running on the master daemon
2941
  cl = GetClient()
2942

    
2943
  (cluster_name, master_node) = \
2944
    cl.QueryConfigValues(["cluster_name", "master_node"])
2945

    
2946
  online_nodes = GetOnlineNodes([], cl=cl)
2947
  ssh_ports = GetNodesSshPorts(online_nodes, cl)
2948

    
2949
  # Don't keep a reference to the client. The master daemon will go away.
2950
  del cl
2951

    
2952
  assert master_node in online_nodes
2953

    
2954
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2955
                                       online_nodes, ssh_ports).Call(fn, *args)
2956

    
2957

    
2958
def GenerateTable(headers, fields, separator, data,
2959
                  numfields=None, unitfields=None,
2960
                  units=None):
2961
  """Prints a table with headers and different fields.
2962

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

2986
  """
2987
  if units is None:
2988
    if separator:
2989
      units = "m"
2990
    else:
2991
      units = "h"
2992

    
2993
  if numfields is None:
2994
    numfields = []
2995
  if unitfields is None:
2996
    unitfields = []
2997

    
2998
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2999
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
3000

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

    
3015
  if separator is None:
3016
    mlens = [0 for name in fields]
3017
    format_str = " ".join(format_fields)
3018
  else:
3019
    format_str = separator.replace("%", "%%").join(format_fields)
3020

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

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

    
3047
  if separator is None:
3048
    assert len(mlens) == len(fields)
3049

    
3050
    if fields and not numfields.Matches(fields[-1]):
3051
      mlens[-1] = 0
3052

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

    
3063
  return result
3064

    
3065

    
3066
def _FormatBool(value):
3067
  """Formats a boolean value as a string.
3068

3069
  """
3070
  if value:
3071
    return "Y"
3072
  return "N"
3073

    
3074

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

    
3085

    
3086
def _GetColumnFormatter(fdef, override, unit):
3087
  """Returns formatting function for a field.
3088

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

3099
  """
3100
  fmt = override.get(fdef.name, None)
3101
  if fmt is not None:
3102
    return fmt
3103

    
3104
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3105

    
3106
  if fdef.kind == constants.QFT_UNIT:
3107
    # Can't keep this information in the static dictionary
3108
    return (lambda value: utils.FormatUnit(value, unit), True)
3109

    
3110
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
3111
  if fmt is not None:
3112
    return fmt
3113

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

    
3116

    
3117
class _QueryColumnFormatter:
3118
  """Callable class for formatting fields of a query.
3119

3120
  """
3121
  def __init__(self, fn, status_fn, verbose):
3122
    """Initializes this class.
3123

3124
    @type fn: callable
3125
    @param fn: Formatting function
3126
    @type status_fn: callable
3127
    @param status_fn: Function to report fields' status
3128
    @type verbose: boolean
3129
    @param verbose: whether to use verbose field descriptions or not
3130

3131
    """
3132
    self._fn = fn
3133
    self._status_fn = status_fn
3134
    self._verbose = verbose
3135

    
3136
  def __call__(self, data):
3137
    """Returns a field's string representation.
3138

3139
    """
3140
    (status, value) = data
3141

    
3142
    # Report status
3143
    self._status_fn(status)
3144

    
3145
    if status == constants.RS_NORMAL:
3146
      return self._fn(value)
3147

    
3148
    assert value is None, \
3149
           "Found value %r for abnormal status %s" % (value, status)
3150

    
3151
    return FormatResultError(status, self._verbose)
3152

    
3153

    
3154
def FormatResultError(status, verbose):
3155
  """Formats result status other than L{constants.RS_NORMAL}.
3156

3157
  @param status: The result status
3158
  @type verbose: boolean
3159
  @param verbose: Whether to return the verbose text
3160
  @return: Text of result status
3161

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

    
3174

    
3175
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3176
                      header=False, verbose=False):
3177
  """Formats data in L{objects.QueryResponse}.
3178

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

3194
  """
3195
  if unit is None:
3196
    if separator:
3197
      unit = "m"
3198
    else:
3199
      unit = "h"
3200

    
3201
  if format_override is None:
3202
    format_override = {}
3203

    
3204
  stats = dict.fromkeys(constants.RS_ALL, 0)
3205

    
3206
  def _RecordStatus(status):
3207
    if status in stats:
3208
      stats[status] += 1
3209

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

    
3219
  table = FormatTable(result.data, columns, header, separator)
3220

    
3221
  # Collect statistics
3222
  assert len(stats) == len(constants.RS_ALL)
3223
  assert compat.all(count >= 0 for count in stats.values())
3224

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

    
3236
  return (status, table)
3237

    
3238

    
3239
def _GetUnknownFields(fdefs):
3240
  """Returns list of unknown fields included in C{fdefs}.
3241

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

3244
  """
3245
  return [fdef for fdef in fdefs
3246
          if fdef.kind == constants.QFT_UNKNOWN]
3247

    
3248

    
3249
def _WarnUnknownFields(fdefs):
3250
  """Prints a warning to stderr if a query included unknown fields.
3251

3252
  @type fdefs: list of L{objects.QueryFieldDefinition}
3253

3254
  """
3255
  unknown = _GetUnknownFields(fdefs)
3256
  if unknown:
3257
    ToStderr("Warning: Queried for unknown fields %s",
3258
             utils.CommaJoin(fdef.name for fdef in unknown))
3259
    return True
3260

    
3261
  return False
3262

    
3263

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

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

3299
  """
3300
  if not names:
3301
    names = None
3302

    
3303
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3304
                                isnumeric=isnumeric)
3305

    
3306
  if qfilter is None:
3307
    qfilter = namefilter
3308
  elif namefilter is not None:
3309
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3310

    
3311
  if cl is None:
3312
    cl = GetClient()
3313

    
3314
  response = cl.Query(resource, fields, qfilter)
3315

    
3316
  found_unknown = _WarnUnknownFields(response.fields)
3317

    
3318
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3319
                                     header=header,
3320
                                     format_override=format_override,
3321
                                     verbose=verbose)
3322

    
3323
  for line in data:
3324
    ToStdout(line)
3325

    
3326
  assert ((found_unknown and status == QR_UNKNOWN) or
3327
          (not found_unknown and status != QR_UNKNOWN))
3328

    
3329
  if status == QR_UNKNOWN:
3330
    return constants.EXIT_UNKNOWN_FIELD
3331

    
3332
  # TODO: Should the list command fail if not all data could be collected?
3333
  return constants.EXIT_SUCCESS
3334

    
3335

    
3336
def _FieldDescValues(fdef):
3337
  """Helper function for L{GenericListFields} to get query field description.
3338

3339
  @type fdef: L{objects.QueryFieldDefinition}
3340
  @rtype: list
3341

3342
  """
3343
  return [
3344
    fdef.name,
3345
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3346
    fdef.title,
3347
    fdef.doc,
3348
    ]
3349

    
3350

    
3351
def GenericListFields(resource, fields, separator, header, cl=None):
3352
  """Generic implementation for listing fields for a resource.
3353

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

3362
  """
3363
  if cl is None:
3364
    cl = GetClient()
3365

    
3366
  if not fields:
3367
    fields = None
3368

    
3369
  response = cl.QueryFields(resource, fields)
3370

    
3371
  found_unknown = _WarnUnknownFields(response.fields)
3372

    
3373
  columns = [
3374
    TableColumn("Name", str, False),
3375
    TableColumn("Type", str, False),
3376
    TableColumn("Title", str, False),
3377
    TableColumn("Description", str, False),
3378
    ]
3379

    
3380
  rows = map(_FieldDescValues, response.fields)
3381

    
3382
  for line in FormatTable(rows, columns, header, separator):
3383
    ToStdout(line)
3384

    
3385
  if found_unknown:
3386
    return constants.EXIT_UNKNOWN_FIELD
3387

    
3388
  return constants.EXIT_SUCCESS
3389

    
3390

    
3391
class TableColumn:
3392
  """Describes a column for L{FormatTable}.
3393

3394
  """
3395
  def __init__(self, title, fn, align_right):
3396
    """Initializes this class.
3397

3398
    @type title: string
3399
    @param title: Column title
3400
    @type fn: callable
3401
    @param fn: Formatting function
3402
    @type align_right: bool
3403
    @param align_right: Whether to align values on the right-hand side
3404

3405
    """
3406
    self.title = title
3407
    self.format = fn
3408
    self.align_right = align_right
3409

    
3410

    
3411
def _GetColFormatString(width, align_right):
3412
  """Returns the format string for a field.
3413

3414
  """
3415
  if align_right:
3416
    sign = ""
3417
  else:
3418
    sign = "-"
3419

    
3420
  return "%%%s%ss" % (sign, width)
3421

    
3422

    
3423
def FormatTable(rows, columns, header, separator):
3424
  """Formats data as a table.
3425

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

3435
  """
3436
  if header:
3437
    data = [[col.title for col in columns]]
3438
    colwidth = [len(col.title) for col in columns]
3439
  else:
3440
    data = []
3441
    colwidth = [0 for _ in columns]
3442

    
3443
  # Format row data
3444
  for row in rows:
3445
    assert len(row) == len(columns)
3446

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

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

    
3455
    data.append(formatted)
3456

    
3457
  if separator is not None:
3458
    # Return early if a separator is used
3459
    return [separator.join(row) for row in data]
3460

    
3461
  if columns and not columns[-1].align_right:
3462
    # Avoid unnecessary spaces at end of line
3463
    colwidth[-1] = 0
3464

    
3465
  # Build format string
3466
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3467
                  for col, width in zip(columns, colwidth)])
3468

    
3469
  return [fmt % tuple(row) for row in data]
3470

    
3471

    
3472
def FormatTimestamp(ts):
3473
  """Formats a given timestamp.
3474

3475
  @type ts: timestamp
3476
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3477

3478
  @rtype: string
3479
  @return: a string with the formatted timestamp
3480

3481
  """
3482
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3483
    return "?"
3484

    
3485
  (sec, usecs) = ts
3486
  return utils.FormatTime(sec, usecs=usecs)
3487

    
3488

    
3489
def ParseTimespec(value):
3490
  """Parse a time specification.
3491

3492
  The following suffixed will be recognized:
3493

3494
    - s: seconds
3495
    - m: minutes
3496
    - h: hours
3497
    - d: day
3498
    - w: weeks
3499

3500
  Without any suffix, the value will be taken to be in seconds.
3501

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

    
3533

    
3534
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3535
                   filter_master=False, nodegroup=None):
3536
  """Returns the names of online nodes.
3537

3538
  This function will also log a warning on stderr with the names of
3539
  the online nodes.
3540

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

3559
  """
3560
  if cl is None:
3561
    cl = GetClient()
3562

    
3563
  qfilter = []
3564

    
3565
  if nodes:
3566
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3567

    
3568
  if nodegroup is not None:
3569
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3570
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3571

    
3572
  if filter_master:
3573
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3574

    
3575
  if qfilter:
3576
    if len(qfilter) > 1:
3577
      final_filter = [qlang.OP_AND] + qfilter
3578
    else:
3579
      assert len(qfilter) == 1
3580
      final_filter = qfilter[0]
3581
  else:
3582
    final_filter = None
3583

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

    
3586
  def _IsOffline(row):
3587
    (_, (_, offline), _) = row
3588
    return offline
3589

    
3590
  def _GetName(row):
3591
    ((_, name), _, _) = row
3592
    return name
3593

    
3594
  def _GetSip(row):
3595
    (_, _, (_, sip)) = row
3596
    return sip
3597

    
3598
  (offline, online) = compat.partition(result.data, _IsOffline)
3599

    
3600
  if offline and not nowarn:
3601
    ToStderr("Note: skipping offline node(s): %s" %
3602
             utils.CommaJoin(map(_GetName, offline)))
3603

    
3604
  if secondary_ips:
3605
    fn = _GetSip
3606
  else:
3607
    fn = _GetName
3608

    
3609
  return map(fn, online)
3610

    
3611

    
3612
def GetNodesSshPorts(nodes, cl):
3613
  """Retrieves SSH ports of given nodes.
3614

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

    
3627

    
3628
def _ToStream(stream, txt, *args):
3629
  """Write a message to a stream, bypassing the logging system
3630

3631
  @type stream: file object
3632
  @param stream: the file to which we should write
3633
  @type txt: str
3634
  @param txt: the message
3635

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

    
3652

    
3653
def ToStdout(txt, *args):
3654
  """Write a message to stdout only, bypassing the logging system
3655

3656
  This is just a wrapper over _ToStream.
3657

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

3661
  """
3662
  _ToStream(sys.stdout, txt, *args)
3663

    
3664

    
3665
def ToStderr(txt, *args):
3666
  """Write a message to stderr only, bypassing the logging system
3667

3668
  This is just a wrapper over _ToStream.
3669

3670
  @type txt: str
3671
  @param txt: the message
3672

3673
  """
3674
  _ToStream(sys.stderr, txt, *args)
3675

    
3676

    
3677
class JobExecutor(object):
3678
  """Class which manages the submission and execution of multiple jobs.
3679

3680
  Note that instances of this class should not be reused between
3681
  GetResults() calls.
3682

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

    
3695
  @staticmethod
3696
  def _IfName(name, fmt):
3697
    """Helper function for formatting name.
3698

3699
    """
3700
    if name:
3701
      return fmt % name
3702

    
3703
    return ""
3704

    
3705
  def QueueJob(self, name, *ops):
3706
    """Record a job for later submit.
3707

3708
    @type name: string
3709
    @param name: a description of the job, will be used in WaitJobSet
3710

3711
    """
3712
    SetGenericOpcodeOpts(ops, self.opts)
3713
    self.queue.append((self._counter.next(), name, ops))
3714

    
3715
  def AddJobId(self, name, status, job_id):
3716
    """Adds a job ID to the internal queue.
3717

3718
    """
3719
    self.jobs.append((self._counter.next(), status, job_id, name))
3720

    
3721
  def SubmitPending(self, each=False):
3722
    """Submit all pending jobs.
3723

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

    
3736
  def _ChooseJob(self):
3737
    """Choose a non-waiting/queued job to poll next.
3738

3739
    """
3740
    assert self.jobs, "_ChooseJob called with empty job list"
3741

    
3742
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3743
                               ["status"])
3744
    assert result
3745

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

    
3757
    # no job found
3758
    return self.jobs.pop(0)
3759

    
3760
  def GetResults(self):
3761
    """Wait for and return the results of all jobs.
3762

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

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

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

    
3783
    while self.jobs:
3784
      (idx, _, jid, name) = self._ChooseJob()
3785
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3786
      try:
3787
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3788
        success = True
3789
      except errors.JobLost, err:
3790
        _, job_result = FormatError(err)
3791
        ToStderr("Job %s%s has been archived, cannot check its result",
3792
                 jid, self._IfName(name, " for %s"))
3793
        success = False
3794
      except (errors.GenericError, rpcerr.ProtocolError), err:
3795
        _, job_result = FormatError(err)
3796
        success = False
3797
        # the error message will always be shown, verbose or not
3798
        ToStderr("Job %s%s has failed: %s",
3799
                 jid, self._IfName(name, " for %s"), job_result)
3800

    
3801
      results.append((idx, success, job_result))
3802

    
3803
    # sort based on the index, then drop it
3804
    results.sort()
3805
    results = [i[1:] for i in results]
3806

    
3807
    return results
3808

    
3809
  def WaitOrShow(self, wait):
3810
    """Wait for job results or only print the job IDs.
3811

3812
    @type wait: boolean
3813
    @param wait: whether to wait or not
3814

3815
    """
3816
    if wait:
3817
      return self.GetResults()
3818
    else:
3819
      if not self.jobs:
3820
        self.SubmitPending()
3821
      for _, status, result, name in self.jobs:
3822
        if status:
3823
          ToStdout("%s: %s", result, name)
3824
        else:
3825
          ToStderr("Failure for %s: %s", name, result)
3826
      return [row[1:3] for row in self.jobs]
3827

    
3828

    
3829
def FormatParamsDictInfo(param_dict, actual):
3830
  """Formats a parameter dictionary.
3831

3832
  @type param_dict: dict
3833
  @param param_dict: the own parameters
3834
  @type actual: dict
3835
  @param actual: the current parameter set (including defaults)
3836
  @rtype: dict
3837
  @return: dictionary where the value of each parameter is either a fully
3838
      formatted string or a dictionary containing formatted strings
3839

3840
  """
3841
  ret = {}
3842
  for (key, data) in actual.items():
3843
    if isinstance(data, dict) and data:
3844
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3845
    else:
3846
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3847
  return ret
3848

    
3849

    
3850
def _FormatListInfoDefault(data, def_data):
3851
  if data is not None:
3852
    ret = utils.CommaJoin(data)
3853
  else:
3854
    ret = "default (%s)" % utils.CommaJoin(def_data)
3855
  return ret
3856

    
3857

    
3858
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3859
  """Formats an instance policy.
3860

3861
  @type custom_ipolicy: dict
3862
  @param custom_ipolicy: own policy
3863
  @type eff_ipolicy: dict
3864
  @param eff_ipolicy: effective policy (including defaults); ignored for
3865
      cluster
3866
  @type iscluster: bool
3867
  @param iscluster: the policy is at cluster level
3868
  @rtype: list of pairs
3869
  @return: formatted data, suitable for L{PrintGenericInfo}
3870

3871
  """
3872
  if iscluster:
3873
    eff_ipolicy = custom_ipolicy
3874

    
3875
  minmax_out = []
3876
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3877
  if custom_minmax:
3878
    for (k, minmax) in enumerate(custom_minmax):
3879
      minmax_out.append([
3880
        ("%s/%s" % (key, k),
3881
         FormatParamsDictInfo(minmax[key], minmax[key]))
3882
        for key in constants.ISPECS_MINMAX_KEYS
3883
        ])
3884
  else:
3885
    for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]):
3886
      minmax_out.append([
3887
        ("%s/%s" % (key, k),
3888
         FormatParamsDictInfo({}, minmax[key]))
3889
        for key in constants.ISPECS_MINMAX_KEYS
3890
        ])
3891
  ret = [("bounds specs", minmax_out)]
3892

    
3893
  if iscluster:
3894
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3895
    ret.append(
3896
      (constants.ISPECS_STD,
3897
       FormatParamsDictInfo(stdspecs, stdspecs))
3898
      )
3899

    
3900
  ret.append(
3901
    ("allowed disk templates",
3902
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3903
                            eff_ipolicy[constants.IPOLICY_DTS]))
3904
    )
3905
  ret.extend([
3906
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3907
    for key in constants.IPOLICY_PARAMETERS
3908
    ])
3909
  return ret
3910

    
3911

    
3912
def _PrintSpecsParameters(buf, specs):
3913
  values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items()))
3914
  buf.write(",".join(values))
3915

    
3916

    
3917
def PrintIPolicyCommand(buf, ipolicy, isgroup):
3918
  """Print the command option used to generate the given instance policy.
3919

3920
  Currently only the parts dealing with specs are supported.
3921

3922
  @type buf: StringIO
3923
  @param buf: stream to write into
3924
  @type ipolicy: dict
3925
  @param ipolicy: instance policy
3926
  @type isgroup: bool
3927
  @param isgroup: whether the policy is at group level
3928

3929
  """
3930
  if not isgroup:
3931
    stdspecs = ipolicy.get("std")
3932
    if stdspecs:
3933
      buf.write(" %s " % IPOLICY_STD_SPECS_STR)
3934
      _PrintSpecsParameters(buf, stdspecs)
3935
  minmaxes = ipolicy.get("minmax", [])
3936
  first = True
3937
  for minmax in minmaxes:
3938
    minspecs = minmax.get("min")
3939
    maxspecs = minmax.get("max")
3940
    if minspecs and maxspecs:
3941
      if first:
3942
        buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
3943
        first = False
3944
      else:
3945
        buf.write("//")
3946
      buf.write("min:")
3947
      _PrintSpecsParameters(buf, minspecs)
3948
      buf.write("/max:")
3949
      _PrintSpecsParameters(buf, maxspecs)
3950

    
3951

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

3955
  This function is used to request confirmation for doing an operation
3956
  on a given list of list_type.
3957

3958
  @type names: list
3959
  @param names: the list of names that we display when
3960
      we ask for confirmation
3961
  @type list_type: str
3962
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3963
  @type text: str
3964
  @param text: the operation that the user should confirm
3965
  @rtype: boolean
3966
  @return: True or False depending on user's confirmation.
3967

3968
  """
3969
  count = len(names)
3970
  msg = ("The %s will operate on %d %s.\n%s"
3971
         "Do you want to continue?" % (text, count, list_type, extra))
3972
  affected = (("\nAffected %s:\n" % list_type) +
3973
              "\n".join(["  %s" % name for name in names]))
3974

    
3975
  choices = [("y", True, "Yes, execute the %s" % text),
3976
             ("n", False, "No, abort the %s" % text)]
3977

    
3978
  if count > 20:
3979
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3980
    question = msg
3981
  else:
3982
    question = msg + affected
3983

    
3984
  choice = AskUser(question, choices)
3985
  if choice == "v":
3986
    choices.pop(1)
3987
    choice = AskUser(msg + affected, choices)
3988
  return choice
3989

    
3990

    
3991
def _MaybeParseUnit(elements):
3992
  """Parses and returns an array of potential values with units.
3993

3994
  """
3995
  parsed = {}
3996
  for k, v in elements.items():
3997
    if v == constants.VALUE_DEFAULT:
3998
      parsed[k] = v
3999
    else:
4000
      parsed[k] = utils.ParseUnit(v)
4001
  return parsed
4002

    
4003

    
4004
def _InitISpecsFromSplitOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
4005
                             ispecs_disk_count, ispecs_disk_size,
4006
                             ispecs_nic_count, group_ipolicy, fill_all):
4007
  try:
4008
    if ispecs_mem_size:
4009
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
4010
    if ispecs_disk_size:
4011
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
4012
  except (TypeError, ValueError, errors.UnitParseError), err:
4013
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
4014
                               " in policy: %s" %
4015
                               (ispecs_disk_size, ispecs_mem_size, err),
4016
                               errors.ECODE_INVAL)
4017

    
4018
  # prepare ipolicy dict
4019
  ispecs_transposed = {
4020
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
4021
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
4022
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
4023
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
4024
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
4025
    }
4026

    
4027
  # first, check that the values given are correct
4028
  if group_ipolicy:
4029
    forced_type = TISPECS_GROUP_TYPES
4030
  else:
4031
    forced_type = TISPECS_CLUSTER_TYPES
4032
  for specs in ispecs_transposed.values():
4033
    assert type(specs) is dict
4034
    utils.ForceDictType(specs, forced_type)
4035

    
4036
  # then transpose
4037
  ispecs = {
4038
    constants.ISPECS_MIN: {},
4039
    constants.ISPECS_MAX: {},
4040
    constants.ISPECS_STD: {},
4041
    }
4042
  for (name, specs) in ispecs_transposed.iteritems():
4043
    assert name in constants.ISPECS_PARAMETERS
4044
    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
4045
      assert key in ispecs
4046
      ispecs[key][name] = val
4047
  minmax_out = {}
4048
  for key in constants.ISPECS_MINMAX_KEYS:
4049
    if fill_all:
4050
      minmax_out[key] = \
4051
        objects.FillDict(constants.ISPECS_MINMAX_DEFAULTS[key], ispecs[key])
4052
    else:
4053
      minmax_out[key] = ispecs[key]
4054
  ipolicy[constants.ISPECS_MINMAX] = [minmax_out]
4055
  if fill_all:
4056
    ipolicy[constants.ISPECS_STD] = \
4057
        objects.FillDict(constants.IPOLICY_DEFAULTS[constants.ISPECS_STD],
4058
                         ispecs[constants.ISPECS_STD])
4059
  else:
4060
    ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
4061

    
4062

    
4063
def _ParseSpecUnit(spec, keyname):
4064
  ret = spec.copy()
4065
  for k in [constants.ISPEC_DISK_SIZE, constants.ISPEC_MEM_SIZE]:
4066
    if k in ret:
4067
      try:
4068
        ret[k] = utils.ParseUnit(ret[k])
4069
      except (TypeError, ValueError, errors.UnitParseError), err:
4070
        raise errors.OpPrereqError(("Invalid parameter %s (%s) in %s instance"
4071
                                    " specs: %s" % (k, ret[k], keyname, err)),
4072
                                   errors.ECODE_INVAL)
4073
  return ret
4074

    
4075

    
4076
def _ParseISpec(spec, keyname, required):
4077
  ret = _ParseSpecUnit(spec, keyname)
4078
  utils.ForceDictType(ret, constants.ISPECS_PARAMETER_TYPES)
4079
  missing = constants.ISPECS_PARAMETERS - frozenset(ret.keys())
4080
  if required and missing:
4081
    raise errors.OpPrereqError("Missing parameters in ipolicy spec %s: %s" %
4082
                               (keyname, utils.CommaJoin(missing)),
4083
                               errors.ECODE_INVAL)
4084
  return ret
4085

    
4086

    
4087
def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
4088
  ret = None
4089
  if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and
4090
      len(minmax_ispecs[0]) == 1):
4091
    for (key, spec) in minmax_ispecs[0].items():
4092
      # This loop is executed exactly once
4093
      if key in allowed_values and not spec:
4094
        ret = key
4095
  return ret
4096

    
4097

    
4098
def _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4099
                            group_ipolicy, allowed_values):
4100
  found_allowed = _GetISpecsInAllowedValues(minmax_ispecs, allowed_values)
4101
  if found_allowed is not None:
4102
    ipolicy_out[constants.ISPECS_MINMAX] = found_allowed
4103
  elif minmax_ispecs is not None:
4104
    minmax_out = []
4105
    for mmpair in minmax_ispecs:
4106
      mmpair_out = {}
4107
      for (key, spec) in mmpair.items():
4108
        if key not in constants.ISPECS_MINMAX_KEYS:
4109
          msg = "Invalid key in bounds instance specifications: %s" % key
4110
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
4111
        mmpair_out[key] = _ParseISpec(spec, key, True)
4112
      minmax_out.append(mmpair_out)
4113
    ipolicy_out[constants.ISPECS_MINMAX] = minmax_out
4114
  if std_ispecs is not None:
4115
    assert not group_ipolicy # This is not an option for gnt-group
4116
    ipolicy_out[constants.ISPECS_STD] = _ParseISpec(std_ispecs, "std", False)
4117

    
4118

    
4119
def CreateIPolicyFromOpts(ispecs_mem_size=None,
4120
                          ispecs_cpu_count=None,
4121
                          ispecs_disk_count=None,
4122
                          ispecs_disk_size=None,
4123
                          ispecs_nic_count=None,
4124
                          minmax_ispecs=None,
4125
                          std_ispecs=None,
4126
                          ipolicy_disk_templates=None,
4127
                          ipolicy_vcpu_ratio=None,
4128
                          ipolicy_spindle_ratio=None,
4129
                          group_ipolicy=False,
4130
                          allowed_values=None,
4131
                          fill_all=False):
4132
  """Creation of instance policy based on command line options.
4133

4134
  @param fill_all: whether for cluster policies we should ensure that
4135
    all values are filled
4136

4137
  """
4138
  assert not (fill_all and allowed_values)
4139

    
4140
  split_specs = (ispecs_mem_size or ispecs_cpu_count or ispecs_disk_count or
4141
                 ispecs_disk_size or ispecs_nic_count)
4142
  if (split_specs and (minmax_ispecs is not None or std_ispecs is not None)):
4143
    raise errors.OpPrereqError("A --specs-xxx option cannot be specified"
4144
                               " together with any --ipolicy-xxx-specs option",
4145
                               errors.ECODE_INVAL)
4146

    
4147
  ipolicy_out = objects.MakeEmptyIPolicy()
4148
  if split_specs:
4149
    assert fill_all
4150
    _InitISpecsFromSplitOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
4151
                             ispecs_disk_count, ispecs_disk_size,
4152
                             ispecs_nic_count, group_ipolicy, fill_all)
4153
  elif (minmax_ispecs is not None or std_ispecs is not None):
4154
    _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4155
                            group_ipolicy, allowed_values)
4156

    
4157
  if ipolicy_disk_templates is not None:
4158
    if allowed_values and ipolicy_disk_templates in allowed_values:
4159
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
4160
    else:
4161
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
4162
  if ipolicy_vcpu_ratio is not None:
4163
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
4164
  if ipolicy_spindle_ratio is not None:
4165
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
4166

    
4167
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
4168

    
4169
  if not group_ipolicy and fill_all:
4170
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
4171

    
4172
  return ipolicy_out
4173

    
4174

    
4175
def _SerializeGenericInfo(buf, data, level, afterkey=False):
4176
  """Formatting core of L{PrintGenericInfo}.
4177

4178
  @param buf: (string) stream to accumulate the result into
4179
  @param data: data to format
4180
  @type level: int
4181
  @param level: depth in the data hierarchy, used for indenting
4182
  @type afterkey: bool
4183
  @param afterkey: True when we are in the middle of a line after a key (used
4184
      to properly add newlines or indentation)
4185

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

    
4243

    
4244
def PrintGenericInfo(data):
4245
  """Print information formatted according to the hierarchy.
4246

4247
  The output is a valid YAML string.
4248

4249
  @param data: the data to print. It's a hierarchical structure whose elements
4250
      can be:
4251
        - dictionaries, where keys are strings and values are of any of the
4252
          types listed here
4253
        - lists of pairs (key, value), where key is a string and value is of
4254
          any of the types listed here; it's a way to encode ordered
4255
          dictionaries
4256
        - lists of any of the types listed here
4257
        - strings
4258

4259
  """
4260
  buf = StringIO()
4261
  _SerializeGenericInfo(buf, data, 0)
4262
  ToStdout(buf.getvalue().rstrip("\n"))