Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ b3cc1646

History | View | Annotate | Download (137.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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

    
48
from ganeti.runtime import (GetClient)
49

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

    
53

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

    
302
NO_PREFIX = "no_"
303
UN_PREFIX = "-"
304

    
305
#: Priorities (sorted)
306
_PRIORITY_NAMES = [
307
  ("low", constants.OP_PRIO_LOW),
308
  ("normal", constants.OP_PRIO_NORMAL),
309
  ("high", constants.OP_PRIO_HIGH),
310
  ]
311

    
312
#: Priority dictionary for easier lookup
313
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
314
# we migrate to Python 2.6
315
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
316

    
317
# Query result status for clients
318
(QR_NORMAL,
319
 QR_UNKNOWN,
320
 QR_INCOMPLETE) = range(3)
321

    
322
#: Maximum batch size for ChooseJob
323
_CHOOSE_BATCH = 25
324

    
325

    
326
# constants used to create InstancePolicy dictionary
327
TISPECS_GROUP_TYPES = {
328
  constants.ISPECS_MIN: constants.VTYPE_INT,
329
  constants.ISPECS_MAX: constants.VTYPE_INT,
330
  }
331

    
332
TISPECS_CLUSTER_TYPES = {
333
  constants.ISPECS_MIN: constants.VTYPE_INT,
334
  constants.ISPECS_MAX: constants.VTYPE_INT,
335
  constants.ISPECS_STD: constants.VTYPE_INT,
336
  }
337

    
338
#: User-friendly names for query2 field types
339
_QFT_NAMES = {
340
  constants.QFT_UNKNOWN: "Unknown",
341
  constants.QFT_TEXT: "Text",
342
  constants.QFT_BOOL: "Boolean",
343
  constants.QFT_NUMBER: "Number",
344
  constants.QFT_UNIT: "Storage size",
345
  constants.QFT_TIMESTAMP: "Timestamp",
346
  constants.QFT_OTHER: "Custom",
347
  }
348

    
349

    
350
class _Argument:
351
  def __init__(self, min=0, max=None): # pylint: disable=W0622
352
    self.min = min
353
    self.max = max
354

    
355
  def __repr__(self):
356
    return ("<%s min=%s max=%s>" %
357
            (self.__class__.__name__, self.min, self.max))
358

    
359

    
360
class ArgSuggest(_Argument):
361
  """Suggesting argument.
362

363
  Value can be any of the ones passed to the constructor.
364

365
  """
366
  # pylint: disable=W0622
367
  def __init__(self, min=0, max=None, choices=None):
368
    _Argument.__init__(self, min=min, max=max)
369
    self.choices = choices
370

    
371
  def __repr__(self):
372
    return ("<%s min=%s max=%s choices=%r>" %
373
            (self.__class__.__name__, self.min, self.max, self.choices))
374

    
375

    
376
class ArgChoice(ArgSuggest):
377
  """Choice argument.
378

379
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
380
  but value must be one of the choices.
381

382
  """
383

    
384

    
385
class ArgUnknown(_Argument):
386
  """Unknown argument to program (e.g. determined at runtime).
387

388
  """
389

    
390

    
391
class ArgInstance(_Argument):
392
  """Instances argument.
393

394
  """
395

    
396

    
397
class ArgNode(_Argument):
398
  """Node argument.
399

400
  """
401

    
402

    
403
class ArgNetwork(_Argument):
404
  """Network argument.
405

406
  """
407

    
408

    
409
class ArgGroup(_Argument):
410
  """Node group argument.
411

412
  """
413

    
414

    
415
class ArgJobId(_Argument):
416
  """Job ID argument.
417

418
  """
419

    
420

    
421
class ArgFile(_Argument):
422
  """File path argument.
423

424
  """
425

    
426

    
427
class ArgCommand(_Argument):
428
  """Command argument.
429

430
  """
431

    
432

    
433
class ArgHost(_Argument):
434
  """Host argument.
435

436
  """
437

    
438

    
439
class ArgOs(_Argument):
440
  """OS argument.
441

442
  """
443

    
444

    
445
class ArgExtStorage(_Argument):
446
  """ExtStorage argument.
447

448
  """
449

    
450

    
451
ARGS_NONE = []
452
ARGS_MANY_INSTANCES = [ArgInstance()]
453
ARGS_MANY_NETWORKS = [ArgNetwork()]
454
ARGS_MANY_NODES = [ArgNode()]
455
ARGS_MANY_GROUPS = [ArgGroup()]
456
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
457
ARGS_ONE_NETWORK = [ArgNetwork(min=1, max=1)]
458
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
459
# TODO
460
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
461
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
462

    
463

    
464
def _ExtractTagsObject(opts, args):
465
  """Extract the tag type object.
466

467
  Note that this function will modify its args parameter.
468

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

    
488

    
489
def _ExtendTags(opts, args):
490
  """Extend the args if a source file has been given.
491

492
  This function will extend the tags with the contents of the file
493
  passed in the 'tags_source' attribute of the opts parameter. A file
494
  named '-' will be replaced by stdin.
495

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

    
517

    
518
def ListTags(opts, args):
519
  """List the tags on a given object.
520

521
  This is a generic implementation that knows how to deal with all
522
  three cases of tag objects (cluster, node, instance). The opts
523
  argument is expected to contain a tag_type field denoting what
524
  object type we work on.
525

526
  """
527
  kind, name = _ExtractTagsObject(opts, args)
528
  cl = GetClient(query=True)
529
  result = cl.QueryTags(kind, name)
530
  result = list(result)
531
  result.sort()
532
  for tag in result:
533
    ToStdout(tag)
534

    
535

    
536
def AddTags(opts, args):
537
  """Add tags on a given object.
538

539
  This is a generic implementation that knows how to deal with all
540
  three cases of tag objects (cluster, node, instance). The opts
541
  argument is expected to contain a tag_type field denoting what
542
  object type we work on.
543

544
  """
545
  kind, name = _ExtractTagsObject(opts, args)
546
  _ExtendTags(opts, args)
547
  if not args:
548
    raise errors.OpPrereqError("No tags to be added", errors.ECODE_INVAL)
549
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
550
  SubmitOrSend(op, opts)
551

    
552

    
553
def RemoveTags(opts, args):
554
  """Remove tags from a given object.
555

556
  This is a generic implementation that knows how to deal with all
557
  three cases of tag objects (cluster, node, instance). The opts
558
  argument is expected to contain a tag_type field denoting what
559
  object type we work on.
560

561
  """
562
  kind, name = _ExtractTagsObject(opts, args)
563
  _ExtendTags(opts, args)
564
  if not args:
565
    raise errors.OpPrereqError("No tags to be removed", errors.ECODE_INVAL)
566
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
567
  SubmitOrSend(op, opts)
568

    
569

    
570
def check_unit(option, opt, value): # pylint: disable=W0613
571
  """OptParsers custom converter for units.
572

573
  """
574
  try:
575
    return utils.ParseUnit(value)
576
  except errors.UnitParseError, err:
577
    raise OptionValueError("option %s: %s" % (opt, err))
578

    
579

    
580
def _SplitKeyVal(opt, data, parse_prefixes):
581
  """Convert a KeyVal string into a dict.
582

583
  This function will convert a key=val[,...] string into a dict. Empty
584
  values will be converted specially: keys which have the prefix 'no_'
585
  will have the value=False and the prefix stripped, keys with the prefix
586
  "-" will have value=None and the prefix stripped, and the others will
587
  have value=True.
588

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

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

    
622

    
623
def _SplitIdentKeyVal(opt, value, parse_prefixes):
624
  """Helper function to parse "ident:key=val,key=val" options.
625

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

637
  """
638
  if ":" not in value:
639
    ident, rest = value, ""
640
  else:
641
    ident, rest = value.split(":", 1)
642

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

    
659

    
660
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
661
  """Custom parser for ident:key=val,key=val options.
662

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

666
  """
667
  return _SplitIdentKeyVal(opt, value, True)
668

    
669

    
670
def check_key_val(option, opt, value):  # pylint: disable=W0613
671
  """Custom parser class for key=val,key=val options.
672

673
  This will store the parsed values as a dict {key: val}.
674

675
  """
676
  return _SplitKeyVal(opt, value, True)
677

    
678

    
679
def _SplitListKeyVal(opt, value):
680
  retval = {}
681
  for elem in value.split("/"):
682
    if not elem:
683
      raise errors.ParameterError("Empty section in option '%s'" % opt)
684
    (ident, valdict) = _SplitIdentKeyVal(opt, elem, False)
685
    if ident in retval:
686
      msg = ("Duplicated parameter '%s' in parsing %s: %s" %
687
             (ident, opt, elem))
688
      raise errors.ParameterError(msg)
689
    retval[ident] = valdict
690
  return retval
691

    
692

    
693
def check_multilist_ident_key_val(_, opt, value):
694
  """Custom parser for "ident:key=val,key=val/ident:key=val//ident:.." options.
695

696
  @rtype: list of dictionary
697
  @return: [{ident: {key: val, key: val}, ident: {key: val}}, {ident:..}]
698

699
  """
700
  retval = []
701
  for line in value.split("//"):
702
    retval.append(_SplitListKeyVal(opt, line))
703
  return retval
704

    
705

    
706
def check_bool(option, opt, value): # pylint: disable=W0613
707
  """Custom parser for yes/no options.
708

709
  This will store the parsed value as either True or False.
710

711
  """
712
  value = value.lower()
713
  if value == constants.VALUE_FALSE or value == "no":
714
    return False
715
  elif value == constants.VALUE_TRUE or value == "yes":
716
    return True
717
  else:
718
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
719

    
720

    
721
def check_list(option, opt, value): # pylint: disable=W0613
722
  """Custom parser for comma-separated lists.
723

724
  """
725
  # we have to make this explicit check since "".split(",") is [""],
726
  # not an empty list :(
727
  if not value:
728
    return []
729
  else:
730
    return utils.UnescapeAndSplit(value)
731

    
732

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

736
  """
737
  value = value.lower()
738

    
739
  if value == constants.VALUE_DEFAULT:
740
    return value
741
  else:
742
    return float(value)
743

    
744

    
745
# completion_suggestion is normally a list. Using numeric values not evaluating
746
# to False for dynamic completion.
747
(OPT_COMPL_MANY_NODES,
748
 OPT_COMPL_ONE_NODE,
749
 OPT_COMPL_ONE_INSTANCE,
750
 OPT_COMPL_ONE_OS,
751
 OPT_COMPL_ONE_EXTSTORAGE,
752
 OPT_COMPL_ONE_IALLOCATOR,
753
 OPT_COMPL_ONE_NETWORK,
754
 OPT_COMPL_INST_ADD_NODES,
755
 OPT_COMPL_ONE_NODEGROUP) = range(100, 109)
756

    
757
OPT_COMPL_ALL = compat.UniqueFrozenset([
758
  OPT_COMPL_MANY_NODES,
759
  OPT_COMPL_ONE_NODE,
760
  OPT_COMPL_ONE_INSTANCE,
761
  OPT_COMPL_ONE_OS,
762
  OPT_COMPL_ONE_EXTSTORAGE,
763
  OPT_COMPL_ONE_IALLOCATOR,
764
  OPT_COMPL_ONE_NETWORK,
765
  OPT_COMPL_INST_ADD_NODES,
766
  OPT_COMPL_ONE_NODEGROUP,
767
  ])
768

    
769

    
770
class CliOption(Option):
771
  """Custom option class for optparse.
772

773
  """
774
  ATTRS = Option.ATTRS + [
775
    "completion_suggest",
776
    ]
777
  TYPES = Option.TYPES + (
778
    "multilistidentkeyval",
779
    "identkeyval",
780
    "keyval",
781
    "unit",
782
    "bool",
783
    "list",
784
    "maybefloat",
785
    )
786
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
787
  TYPE_CHECKER["multilistidentkeyval"] = check_multilist_ident_key_val
788
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
789
  TYPE_CHECKER["keyval"] = check_key_val
790
  TYPE_CHECKER["unit"] = check_unit
791
  TYPE_CHECKER["bool"] = check_bool
792
  TYPE_CHECKER["list"] = check_list
793
  TYPE_CHECKER["maybefloat"] = check_maybefloat
794

    
795

    
796
# optparse.py sets make_option, so we do it for our own option class, too
797
cli_option = CliOption
798

    
799

    
800
_YORNO = "yes|no"
801

    
802
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
803
                       help="Increase debugging level")
804

    
805
NOHDR_OPT = cli_option("--no-headers", default=False,
806
                       action="store_true", dest="no_headers",
807
                       help="Don't display column headers")
808

    
809
SEP_OPT = cli_option("--separator", default=None,
810
                     action="store", dest="separator",
811
                     help=("Separator between output fields"
812
                           " (defaults to one space)"))
813

    
814
USEUNITS_OPT = cli_option("--units", default=None,
815
                          dest="units", choices=("h", "m", "g", "t"),
816
                          help="Specify units for output (one of h/m/g/t)")
817

    
818
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
819
                        type="string", metavar="FIELDS",
820
                        help="Comma separated list of output fields")
821

    
822
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
823
                       default=False, help="Force the operation")
824

    
825
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
826
                         default=False, help="Do not require confirmation")
827

    
828
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
829
                                  action="store_true", default=False,
830
                                  help=("Ignore offline nodes and do as much"
831
                                        " as possible"))
832

    
833
TAG_ADD_OPT = cli_option("--tags", dest="tags",
834
                         default=None, help="Comma-separated list of instance"
835
                                            " tags")
836

    
837
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
838
                         default=None, help="File with tag names")
839

    
840
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
841
                        default=False, action="store_true",
842
                        help=("Submit the job and return the job ID, but"
843
                              " don't wait for the job to finish"))
844

    
845
PRINT_JOBID_OPT = cli_option("--print-jobid", dest="print_jobid",
846
                             default=False, action="store_true",
847
                             help=("Additionally print the job as first line"
848
                                   " on stdout (for scripting)."))
849

    
850
SYNC_OPT = cli_option("--sync", dest="do_locking",
851
                      default=False, action="store_true",
852
                      help=("Grab locks while doing the queries"
853
                            " in order to ensure more consistent results"))
854

    
855
DRY_RUN_OPT = cli_option("--dry-run", default=False,
856
                         action="store_true",
857
                         help=("Do not execute the operation, just run the"
858
                               " check steps and verify if it could be"
859
                               " executed"))
860

    
861
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
862
                         action="store_true",
863
                         help="Increase the verbosity of the operation")
864

    
865
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
866
                              action="store_true", dest="simulate_errors",
867
                              help="Debugging option that makes the operation"
868
                              " treat most runtime checks as failed")
869

    
870
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
871
                        default=True, action="store_false",
872
                        help="Don't wait for sync (DANGEROUS!)")
873

    
874
WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync",
875
                        default=False, action="store_true",
876
                        help="Wait for disks to sync")
877

    
878
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
879
                             action="store_true", default=False,
880
                             help="Enable offline instance")
881

    
882
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
883
                              action="store_true", default=False,
884
                              help="Disable down instance")
885

    
886
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
887
                               help=("Custom disk setup (%s)" %
888
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
889
                               default=None, metavar="TEMPL",
890
                               choices=list(constants.DISK_TEMPLATES))
891

    
892
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
893
                        help="Do not create any network cards for"
894
                        " the instance")
895

    
896
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
897
                               help="Relative path under default cluster-wide"
898
                               " file storage dir to store file-based disks",
899
                               default=None, metavar="<DIR>")
900

    
901
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
902
                                  help="Driver to use for image files",
903
                                  default=None, metavar="<DRIVER>",
904
                                  choices=list(constants.FILE_DRIVER))
905

    
906
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
907
                            help="Select nodes for the instance automatically"
908
                            " using the <NAME> iallocator plugin",
909
                            default=None, type="string",
910
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
911

    
912
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
913
                                    metavar="<NAME>",
914
                                    help="Set the default instance"
915
                                    " allocator plugin",
916
                                    default=None, type="string",
917
                                    completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
918

    
919
DEFAULT_IALLOCATOR_PARAMS_OPT = cli_option("--default-iallocator-params",
920
                                           dest="default_iallocator_params",
921
                                           help="iallocator template"
922
                                           " parameters, in the format"
923
                                           " template:option=value,"
924
                                           " option=value,...",
925
                                           type="keyval",
926
                                           default={})
927

    
928
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
929
                    metavar="<os>",
930
                    completion_suggest=OPT_COMPL_ONE_OS)
931

    
932
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
933
                          type="keyval", default={},
934
                          help="OS parameters")
935

    
936
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
937
                               action="store_true", default=False,
938
                               help="Force an unknown variant")
939

    
940
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
941
                            action="store_true", default=False,
942
                            help="Do not install the OS (will"
943
                            " enable no-start)")
944

    
945
NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes",
946
                                dest="allow_runtime_chgs",
947
                                default=True, action="store_false",
948
                                help="Don't allow runtime changes")
949

    
950
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
951
                         type="keyval", default={},
952
                         help="Backend parameters")
953

    
954
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
955
                        default={}, dest="hvparams",
956
                        help="Hypervisor parameters")
957

    
958
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
959
                             help="Disk template parameters, in the format"
960
                             " template:option=value,option=value,...",
961
                             type="identkeyval", action="append", default=[])
962

    
963
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
964
                                 type="keyval", default={},
965
                                 help="Memory size specs: list of key=value,"
966
                                " where key is one of min, max, std"
967
                                 " (in MB or using a unit)")
968

    
969
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
970
                                 type="keyval", default={},
971
                                 help="CPU count specs: list of key=value,"
972
                                 " where key is one of min, max, std")
973

    
974
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
975
                                  dest="ispecs_disk_count",
976
                                  type="keyval", default={},
977
                                  help="Disk count specs: list of key=value,"
978
                                  " where key is one of min, max, std")
979

    
980
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
981
                                 type="keyval", default={},
982
                                 help="Disk size specs: list of key=value,"
983
                                 " where key is one of min, max, std"
984
                                 " (in MB or using a unit)")
985

    
986
SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
987
                                 type="keyval", default={},
988
                                 help="NIC count specs: list of key=value,"
989
                                 " where key is one of min, max, std")
990

    
991
IPOLICY_BOUNDS_SPECS_STR = "--ipolicy-bounds-specs"
992
IPOLICY_BOUNDS_SPECS_OPT = cli_option(IPOLICY_BOUNDS_SPECS_STR,
993
                                      dest="ipolicy_bounds_specs",
994
                                      type="multilistidentkeyval", default=None,
995
                                      help="Complete instance specs limits")
996

    
997
IPOLICY_STD_SPECS_STR = "--ipolicy-std-specs"
998
IPOLICY_STD_SPECS_OPT = cli_option(IPOLICY_STD_SPECS_STR,
999
                                   dest="ipolicy_std_specs",
1000
                                   type="keyval", default=None,
1001
                                   help="Complte standard instance specs")
1002

    
1003
IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
1004
                                    dest="ipolicy_disk_templates",
1005
                                    type="list", default=None,
1006
                                    help="Comma-separated list of"
1007
                                    " enabled disk templates")
1008

    
1009
IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio",
1010
                                 dest="ipolicy_vcpu_ratio",
1011
                                 type="maybefloat", default=None,
1012
                                 help="The maximum allowed vcpu-to-cpu ratio")
1013

    
1014
IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio",
1015
                                   dest="ipolicy_spindle_ratio",
1016
                                   type="maybefloat", default=None,
1017
                                   help=("The maximum allowed instances to"
1018
                                         " spindle ratio"))
1019

    
1020
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
1021
                            help="Hypervisor and hypervisor options, in the"
1022
                            " format hypervisor:option=value,option=value,...",
1023
                            default=None, type="identkeyval")
1024

    
1025
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
1026
                        help="Hypervisor and hypervisor options, in the"
1027
                        " format hypervisor:option=value,option=value,...",
1028
                        default=[], action="append", type="identkeyval")
1029

    
1030
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
1031
                           action="store_false",
1032
                           help="Don't check that the instance's IP"
1033
                           " is alive")
1034

    
1035
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
1036
                             default=True, action="store_false",
1037
                             help="Don't check that the instance's name"
1038
                             " is resolvable")
1039

    
1040
NET_OPT = cli_option("--net",
1041
                     help="NIC parameters", default=[],
1042
                     dest="nics", action="append", type="identkeyval")
1043

    
1044
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
1045
                      dest="disks", action="append", type="identkeyval")
1046

    
1047
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
1048
                         help="Comma-separated list of disks"
1049
                         " indices to act on (e.g. 0,2) (optional,"
1050
                         " defaults to all disks)")
1051

    
1052
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
1053
                         help="Enforces a single-disk configuration using the"
1054
                         " given disk size, in MiB unless a suffix is used",
1055
                         default=None, type="unit", metavar="<size>")
1056

    
1057
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
1058
                                dest="ignore_consistency",
1059
                                action="store_true", default=False,
1060
                                help="Ignore the consistency of the disks on"
1061
                                " the secondary")
1062

    
1063
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
1064
                                dest="allow_failover",
1065
                                action="store_true", default=False,
1066
                                help="If migration is not possible fallback to"
1067
                                     " failover")
1068

    
1069
NONLIVE_OPT = cli_option("--non-live", dest="live",
1070
                         default=True, action="store_false",
1071
                         help="Do a non-live migration (this usually means"
1072
                         " freeze the instance, save the state, transfer and"
1073
                         " only then resume running on the secondary node)")
1074

    
1075
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
1076
                                default=None,
1077
                                choices=list(constants.HT_MIGRATION_MODES),
1078
                                help="Override default migration mode (choose"
1079
                                " either live or non-live")
1080

    
1081
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
1082
                                help="Target node and optional secondary node",
1083
                                metavar="<pnode>[:<snode>]",
1084
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
1085

    
1086
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
1087
                           action="append", metavar="<node>",
1088
                           help="Use only this node (can be used multiple"
1089
                           " times, if not given defaults to all nodes)",
1090
                           completion_suggest=OPT_COMPL_ONE_NODE)
1091

    
1092
NODEGROUP_OPT_NAME = "--node-group"
1093
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
1094
                           dest="nodegroup",
1095
                           help="Node group (name or uuid)",
1096
                           metavar="<nodegroup>",
1097
                           default=None, type="string",
1098
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1099

    
1100
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
1101
                             metavar="<node>",
1102
                             completion_suggest=OPT_COMPL_ONE_NODE)
1103

    
1104
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
1105
                         action="store_false",
1106
                         help="Don't start the instance after creation")
1107

    
1108
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
1109
                         action="store_true", default=False,
1110
                         help="Show command instead of executing it")
1111

    
1112
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
1113
                         default=False, action="store_true",
1114
                         help="Instead of performing the migration/failover,"
1115
                         " try to recover from a failed cleanup. This is safe"
1116
                         " to run even if the instance is healthy, but it"
1117
                         " will create extra replication traffic and "
1118
                         " disrupt briefly the replication (like during the"
1119
                         " migration/failover")
1120

    
1121
STATIC_OPT = cli_option("-s", "--static", dest="static",
1122
                        action="store_true", default=False,
1123
                        help="Only show configuration data, not runtime data")
1124

    
1125
ALL_OPT = cli_option("--all", dest="show_all",
1126
                     default=False, action="store_true",
1127
                     help="Show info on all instances on the cluster."
1128
                     " This can take a long time to run, use wisely")
1129

    
1130
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
1131
                           action="store_true", default=False,
1132
                           help="Interactive OS reinstall, lists available"
1133
                           " OS templates for selection")
1134

    
1135
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
1136
                                 action="store_true", default=False,
1137
                                 help="Remove the instance from the cluster"
1138
                                 " configuration even if there are failures"
1139
                                 " during the removal process")
1140

    
1141
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
1142
                                        dest="ignore_remove_failures",
1143
                                        action="store_true", default=False,
1144
                                        help="Remove the instance from the"
1145
                                        " cluster configuration even if there"
1146
                                        " are failures during the removal"
1147
                                        " process")
1148

    
1149
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
1150
                                 action="store_true", default=False,
1151
                                 help="Remove the instance from the cluster")
1152

    
1153
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
1154
                               help="Specifies the new node for the instance",
1155
                               metavar="NODE", default=None,
1156
                               completion_suggest=OPT_COMPL_ONE_NODE)
1157

    
1158
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
1159
                               help="Specifies the new secondary node",
1160
                               metavar="NODE", default=None,
1161
                               completion_suggest=OPT_COMPL_ONE_NODE)
1162

    
1163
NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node",
1164
                             help="Specifies the new primary node",
1165
                             metavar="<node>", default=None,
1166
                             completion_suggest=OPT_COMPL_ONE_NODE)
1167

    
1168
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
1169
                            default=False, action="store_true",
1170
                            help="Replace the disk(s) on the primary"
1171
                                 " node (applies only to internally mirrored"
1172
                                 " disk templates, e.g. %s)" %
1173
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
1174

    
1175
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
1176
                              default=False, action="store_true",
1177
                              help="Replace the disk(s) on the secondary"
1178
                                   " node (applies only to internally mirrored"
1179
                                   " disk templates, e.g. %s)" %
1180
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1181

    
1182
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
1183
                              default=False, action="store_true",
1184
                              help="Lock all nodes and auto-promote as needed"
1185
                              " to MC status")
1186

    
1187
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
1188
                              default=False, action="store_true",
1189
                              help="Automatically replace faulty disks"
1190
                                   " (applies only to internally mirrored"
1191
                                   " disk templates, e.g. %s)" %
1192
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1193

    
1194
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
1195
                             default=False, action="store_true",
1196
                             help="Ignore current recorded size"
1197
                             " (useful for forcing activation when"
1198
                             " the recorded size is wrong)")
1199

    
1200
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
1201
                          metavar="<node>",
1202
                          completion_suggest=OPT_COMPL_ONE_NODE)
1203

    
1204
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
1205
                         metavar="<dir>")
1206

    
1207
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
1208
                              help="Specify the secondary ip for the node",
1209
                              metavar="ADDRESS", default=None)
1210

    
1211
READD_OPT = cli_option("--readd", dest="readd",
1212
                       default=False, action="store_true",
1213
                       help="Readd old node after replacing it")
1214

    
1215
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1216
                                default=True, action="store_false",
1217
                                help="Disable SSH key fingerprint checking")
1218

    
1219
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1220
                                 default=False, action="store_true",
1221
                                 help="Force the joining of a node")
1222

    
1223
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1224
                    type="bool", default=None, metavar=_YORNO,
1225
                    help="Set the master_candidate flag on the node")
1226

    
1227
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1228
                         type="bool", default=None,
1229
                         help=("Set the offline flag on the node"
1230
                               " (cluster does not communicate with offline"
1231
                               " nodes)"))
1232

    
1233
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1234
                         type="bool", default=None,
1235
                         help=("Set the drained flag on the node"
1236
                               " (excluded from allocation operations)"))
1237

    
1238
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1239
                              type="bool", default=None, metavar=_YORNO,
1240
                              help="Set the master_capable flag on the node")
1241

    
1242
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1243
                          type="bool", default=None, metavar=_YORNO,
1244
                          help="Set the vm_capable flag on the node")
1245

    
1246
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1247
                             type="bool", default=None, metavar=_YORNO,
1248
                             help="Set the allocatable flag on a volume")
1249

    
1250
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1251
                            dest="enabled_hypervisors",
1252
                            help="Comma-separated list of hypervisors",
1253
                            type="string", default=None)
1254

    
1255
ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates",
1256
                                        dest="enabled_disk_templates",
1257
                                        help="Comma-separated list of "
1258
                                             "disk templates",
1259
                                        type="string", default=None)
1260

    
1261
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1262
                            type="keyval", default={},
1263
                            help="NIC parameters")
1264

    
1265
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1266
                         dest="candidate_pool_size", type="int",
1267
                         help="Set the candidate pool size")
1268

    
1269
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1270
                         help=("Enables LVM and specifies the volume group"
1271
                               " name (cluster-wide) for disk allocation"
1272
                               " [%s]" % constants.DEFAULT_VG),
1273
                         metavar="VG", default=None)
1274

    
1275
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1276
                          help="Destroy cluster", action="store_true")
1277

    
1278
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1279
                          help="Skip node agreement check (dangerous)",
1280
                          action="store_true", default=False)
1281

    
1282
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1283
                            help="Specify the mac prefix for the instance IP"
1284
                            " addresses, in the format XX:XX:XX",
1285
                            metavar="PREFIX",
1286
                            default=None)
1287

    
1288
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1289
                               help="Specify the node interface (cluster-wide)"
1290
                               " on which the master IP address will be added"
1291
                               " (cluster init default: %s)" %
1292
                               constants.DEFAULT_BRIDGE,
1293
                               metavar="NETDEV",
1294
                               default=None)
1295

    
1296
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1297
                                help="Specify the netmask of the master IP",
1298
                                metavar="NETMASK",
1299
                                default=None)
1300

    
1301
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1302
                                     dest="use_external_mip_script",
1303
                                     help="Specify whether to run a"
1304
                                     " user-provided script for the master"
1305
                                     " IP address turnup and"
1306
                                     " turndown operations",
1307
                                     type="bool", metavar=_YORNO, default=None)
1308

    
1309
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1310
                                help="Specify the default directory (cluster-"
1311
                                "wide) for storing the file-based disks [%s]" %
1312
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1313
                                metavar="DIR",
1314
                                default=None)
1315

    
1316
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1317
  "--shared-file-storage-dir",
1318
  dest="shared_file_storage_dir",
1319
  help="Specify the default directory (cluster-wide) for storing the"
1320
  " shared file-based disks [%s]" %
1321
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1322
  metavar="SHAREDDIR", default=None)
1323

    
1324
GLOBAL_GLUSTER_FILEDIR_OPT = cli_option(
1325
  "--gluster-storage-dir",
1326
  dest="gluster_storage_dir",
1327
  help="Specify the default directory (cluster-wide) for mounting Gluster"
1328
  " file systems [%s]" %
1329
  pathutils.DEFAULT_GLUSTER_STORAGE_DIR,
1330
  metavar="GLUSTERDIR",
1331
  default=pathutils.DEFAULT_GLUSTER_STORAGE_DIR)
1332

    
1333
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1334
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1335
                                   action="store_false", default=True)
1336

    
1337
MODIFY_ETCHOSTS_OPT = \
1338
 cli_option("--modify-etc-hosts", dest="modify_etc_hosts", metavar=_YORNO,
1339
            default=None, type="bool",
1340
            help="Defines whether the cluster should autonomously modify"
1341
            " and keep in sync the /etc/hosts file of the nodes")
1342

    
1343
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1344
                                    help="Don't initialize SSH keys",
1345
                                    action="store_false", default=True)
1346

    
1347
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1348
                             help="Enable parseable error messages",
1349
                             action="store_true", default=False)
1350

    
1351
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1352
                          help="Skip N+1 memory redundancy tests",
1353
                          action="store_true", default=False)
1354

    
1355
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1356
                             help="Type of reboot: soft/hard/full",
1357
                             default=constants.INSTANCE_REBOOT_HARD,
1358
                             metavar="<REBOOT>",
1359
                             choices=list(constants.REBOOT_TYPES))
1360

    
1361
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1362
                                    dest="ignore_secondaries",
1363
                                    default=False, action="store_true",
1364
                                    help="Ignore errors from secondaries")
1365

    
1366
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1367
                            action="store_false", default=True,
1368
                            help="Don't shutdown the instance (unsafe)")
1369

    
1370
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1371
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1372
                         help="Maximum time to wait")
1373

    
1374
COMPRESS_OPT = cli_option("--compress", dest="compress",
1375
                          default=constants.IEC_NONE,
1376
                          help="The compression mode to use",
1377
                          choices=list(constants.IEC_ALL))
1378

    
1379
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1380
                                  dest="shutdown_timeout", type="int",
1381
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1382
                                  help="Maximum time to wait for instance"
1383
                                  " shutdown")
1384

    
1385
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1386
                          default=None,
1387
                          help=("Number of seconds between repetions of the"
1388
                                " command"))
1389

    
1390
EARLY_RELEASE_OPT = cli_option("--early-release",
1391
                               dest="early_release", default=False,
1392
                               action="store_true",
1393
                               help="Release the locks on the secondary"
1394
                               " node(s) early")
1395

    
1396
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1397
                                  dest="new_cluster_cert",
1398
                                  default=False, action="store_true",
1399
                                  help="Generate a new cluster certificate")
1400

    
1401
NEW_NODE_CERT_OPT = cli_option(
1402
  "--new-node-certificates", dest="new_node_cert", default=False,
1403
  action="store_true", help="Generate new node certificates (for all nodes)")
1404

    
1405
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1406
                           default=None,
1407
                           help="File containing new RAPI certificate")
1408

    
1409
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1410
                               default=None, action="store_true",
1411
                               help=("Generate a new self-signed RAPI"
1412
                                     " certificate"))
1413

    
1414
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1415
                            default=None,
1416
                            help="File containing new SPICE certificate")
1417

    
1418
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1419
                              default=None,
1420
                              help="File containing the certificate of the CA"
1421
                              " which signed the SPICE certificate")
1422

    
1423
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1424
                                dest="new_spice_cert", default=None,
1425
                                action="store_true",
1426
                                help=("Generate a new self-signed SPICE"
1427
                                      " certificate"))
1428

    
1429
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1430
                                    dest="new_confd_hmac_key",
1431
                                    default=False, action="store_true",
1432
                                    help=("Create a new HMAC key for %s" %
1433
                                          constants.CONFD))
1434

    
1435
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1436
                                       dest="cluster_domain_secret",
1437
                                       default=None,
1438
                                       help=("Load new new cluster domain"
1439
                                             " secret from file"))
1440

    
1441
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1442
                                           dest="new_cluster_domain_secret",
1443
                                           default=False, action="store_true",
1444
                                           help=("Create a new cluster domain"
1445
                                                 " secret"))
1446

    
1447
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1448
                              dest="use_replication_network",
1449
                              help="Whether to use the replication network"
1450
                              " for talking to the nodes",
1451
                              action="store_true", default=False)
1452

    
1453
MAINTAIN_NODE_HEALTH_OPT = \
1454
    cli_option("--maintain-node-health", dest="maintain_node_health",
1455
               metavar=_YORNO, default=None, type="bool",
1456
               help="Configure the cluster to automatically maintain node"
1457
               " health, by shutting down unknown instances, shutting down"
1458
               " unknown DRBD devices, etc.")
1459

    
1460
IDENTIFY_DEFAULTS_OPT = \
1461
    cli_option("--identify-defaults", dest="identify_defaults",
1462
               default=False, action="store_true",
1463
               help="Identify which saved instance parameters are equal to"
1464
               " the current cluster defaults and set them as such, instead"
1465
               " of marking them as overridden")
1466

    
1467
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1468
                         action="store", dest="uid_pool",
1469
                         help=("A list of user-ids or user-id"
1470
                               " ranges separated by commas"))
1471

    
1472
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1473
                          action="store", dest="add_uids",
1474
                          help=("A list of user-ids or user-id"
1475
                                " ranges separated by commas, to be"
1476
                                " added to the user-id pool"))
1477

    
1478
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1479
                             action="store", dest="remove_uids",
1480
                             help=("A list of user-ids or user-id"
1481
                                   " ranges separated by commas, to be"
1482
                                   " removed from the user-id pool"))
1483

    
1484
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1485
                              action="store", dest="reserved_lvs",
1486
                              help=("A comma-separated list of reserved"
1487
                                    " logical volumes names, that will be"
1488
                                    " ignored by cluster verify"))
1489

    
1490
ROMAN_OPT = cli_option("--roman",
1491
                       dest="roman_integers", default=False,
1492
                       action="store_true",
1493
                       help="Use roman numbers for positive integers")
1494

    
1495
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1496
                             action="store", default=None,
1497
                             help="Specifies usermode helper for DRBD")
1498

    
1499
PRIMARY_IP_VERSION_OPT = \
1500
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1501
               action="store", dest="primary_ip_version",
1502
               metavar="%d|%d" % (constants.IP4_VERSION,
1503
                                  constants.IP6_VERSION),
1504
               help="Cluster-wide IP version for primary IP")
1505

    
1506
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1507
                              action="store_true",
1508
                              help="Show machine name for every line in output")
1509

    
1510
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1511
                              action="store_true",
1512
                              help=("Hide successful results and show failures"
1513
                                    " only (determined by the exit code)"))
1514

    
1515
REASON_OPT = cli_option("--reason", default=None,
1516
                        help="The reason for executing the command")
1517

    
1518

    
1519
def _PriorityOptionCb(option, _, value, parser):
1520
  """Callback for processing C{--priority} option.
1521

1522
  """
1523
  value = _PRIONAME_TO_VALUE[value]
1524

    
1525
  setattr(parser.values, option.dest, value)
1526

    
1527

    
1528
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1529
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1530
                          choices=_PRIONAME_TO_VALUE.keys(),
1531
                          action="callback", type="choice",
1532
                          callback=_PriorityOptionCb,
1533
                          help="Priority for opcode processing")
1534

    
1535
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1536
                        type="bool", default=None, metavar=_YORNO,
1537
                        help="Sets the hidden flag on the OS")
1538

    
1539
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1540
                        type="bool", default=None, metavar=_YORNO,
1541
                        help="Sets the blacklisted flag on the OS")
1542

    
1543
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1544
                                     type="bool", metavar=_YORNO,
1545
                                     dest="prealloc_wipe_disks",
1546
                                     help=("Wipe disks prior to instance"
1547
                                           " creation"))
1548

    
1549
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1550
                             type="keyval", default=None,
1551
                             help="Node parameters")
1552

    
1553
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1554
                              action="store", metavar="POLICY", default=None,
1555
                              help="Allocation policy for the node group")
1556

    
1557
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1558
                              type="bool", metavar=_YORNO,
1559
                              dest="node_powered",
1560
                              help="Specify if the SoR for node is powered")
1561

    
1562
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1563
                             default=constants.OOB_TIMEOUT,
1564
                             help="Maximum time to wait for out-of-band helper")
1565

    
1566
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1567
                             default=constants.OOB_POWER_DELAY,
1568
                             help="Time in seconds to wait between power-ons")
1569

    
1570
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1571
                              action="store_true", default=False,
1572
                              help=("Whether command argument should be treated"
1573
                                    " as filter"))
1574

    
1575
NO_REMEMBER_OPT = cli_option("--no-remember",
1576
                             dest="no_remember",
1577
                             action="store_true", default=False,
1578
                             help="Perform but do not record the change"
1579
                             " in the configuration")
1580

    
1581
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1582
                              default=False, action="store_true",
1583
                              help="Evacuate primary instances only")
1584

    
1585
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1586
                                default=False, action="store_true",
1587
                                help="Evacuate secondary instances only"
1588
                                     " (applies only to internally mirrored"
1589
                                     " disk templates, e.g. %s)" %
1590
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1591

    
1592
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1593
                                action="store_true", default=False,
1594
                                help="Pause instance at startup")
1595

    
1596
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1597
                          help="Destination node group (name or uuid)",
1598
                          default=None, action="append",
1599
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1600

    
1601
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1602
                               action="append", dest="ignore_errors",
1603
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1604
                               help="Error code to be ignored")
1605

    
1606
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1607
                            action="append",
1608
                            help=("Specify disk state information in the"
1609
                                  " format"
1610
                                  " storage_type/identifier:option=value,...;"
1611
                                  " note this is unused for now"),
1612
                            type="identkeyval")
1613

    
1614
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1615
                          action="append",
1616
                          help=("Specify hypervisor state information in the"
1617
                                " format hypervisor:option=value,...;"
1618
                                " note this is unused for now"),
1619
                          type="identkeyval")
1620

    
1621
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1622
                                action="store_true", default=False,
1623
                                help="Ignore instance policy violations")
1624

    
1625
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1626
                             help="Sets the instance's runtime memory,"
1627
                             " ballooning it up or down to the new value",
1628
                             default=None, type="unit", metavar="<size>")
1629

    
1630
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1631
                          action="store_true", default=False,
1632
                          help="Marks the grow as absolute instead of the"
1633
                          " (default) relative mode")
1634

    
1635
NETWORK_OPT = cli_option("--network",
1636
                         action="store", default=None, dest="network",
1637
                         help="IP network in CIDR notation")
1638

    
1639
GATEWAY_OPT = cli_option("--gateway",
1640
                         action="store", default=None, dest="gateway",
1641
                         help="IP address of the router (gateway)")
1642

    
1643
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1644
                                  action="store", default=None,
1645
                                  dest="add_reserved_ips",
1646
                                  help="Comma-separated list of"
1647
                                  " reserved IPs to add")
1648

    
1649
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1650
                                     action="store", default=None,
1651
                                     dest="remove_reserved_ips",
1652
                                     help="Comma-delimited list of"
1653
                                     " reserved IPs to remove")
1654

    
1655
NETWORK6_OPT = cli_option("--network6",
1656
                          action="store", default=None, dest="network6",
1657
                          help="IP network in CIDR notation")
1658

    
1659
GATEWAY6_OPT = cli_option("--gateway6",
1660
                          action="store", default=None, dest="gateway6",
1661
                          help="IP6 address of the router (gateway)")
1662

    
1663
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1664
                                  dest="conflicts_check",
1665
                                  default=True,
1666
                                  action="store_false",
1667
                                  help="Don't check for conflicting IPs")
1668

    
1669
INCLUDEDEFAULTS_OPT = cli_option("--include-defaults", dest="include_defaults",
1670
                                 default=False, action="store_true",
1671
                                 help="Include default values")
1672

    
1673
HOTPLUG_OPT = cli_option("--hotplug", dest="hotplug",
1674
                         action="store_true", default=False,
1675
                         help="Hotplug supported devices (NICs and Disks)")
1676

    
1677
HOTPLUG_IF_POSSIBLE_OPT = cli_option("--hotplug-if-possible",
1678
                                     dest="hotplug_if_possible",
1679
                                     action="store_true", default=False,
1680
                                     help="Hotplug devices in case"
1681
                                          " hotplug is supported")
1682

    
1683
#: Options provided by all commands
1684
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1685

    
1686
# options related to asynchronous job handling
1687

    
1688
SUBMIT_OPTS = [
1689
  SUBMIT_OPT,
1690
  PRINT_JOBID_OPT,
1691
  ]
1692

    
1693
# common options for creating instances. add and import then add their own
1694
# specific ones.
1695
COMMON_CREATE_OPTS = [
1696
  BACKEND_OPT,
1697
  DISK_OPT,
1698
  DISK_TEMPLATE_OPT,
1699
  FILESTORE_DIR_OPT,
1700
  FILESTORE_DRIVER_OPT,
1701
  HYPERVISOR_OPT,
1702
  IALLOCATOR_OPT,
1703
  NET_OPT,
1704
  NODE_PLACEMENT_OPT,
1705
  NOIPCHECK_OPT,
1706
  NOCONFLICTSCHECK_OPT,
1707
  NONAMECHECK_OPT,
1708
  NONICS_OPT,
1709
  NWSYNC_OPT,
1710
  OSPARAMS_OPT,
1711
  OS_SIZE_OPT,
1712
  SUBMIT_OPT,
1713
  PRINT_JOBID_OPT,
1714
  TAG_ADD_OPT,
1715
  DRY_RUN_OPT,
1716
  PRIORITY_OPT,
1717
  ]
1718

    
1719
# common instance policy options
1720
INSTANCE_POLICY_OPTS = [
1721
  IPOLICY_BOUNDS_SPECS_OPT,
1722
  IPOLICY_DISK_TEMPLATES,
1723
  IPOLICY_VCPU_RATIO,
1724
  IPOLICY_SPINDLE_RATIO,
1725
  ]
1726

    
1727
# instance policy split specs options
1728
SPLIT_ISPECS_OPTS = [
1729
  SPECS_CPU_COUNT_OPT,
1730
  SPECS_DISK_COUNT_OPT,
1731
  SPECS_DISK_SIZE_OPT,
1732
  SPECS_MEM_SIZE_OPT,
1733
  SPECS_NIC_COUNT_OPT,
1734
  ]
1735

    
1736

    
1737
class _ShowUsage(Exception):
1738
  """Exception class for L{_ParseArgs}.
1739

1740
  """
1741
  def __init__(self, exit_error):
1742
    """Initializes instances of this class.
1743

1744
    @type exit_error: bool
1745
    @param exit_error: Whether to report failure on exit
1746

1747
    """
1748
    Exception.__init__(self)
1749
    self.exit_error = exit_error
1750

    
1751

    
1752
class _ShowVersion(Exception):
1753
  """Exception class for L{_ParseArgs}.
1754

1755
  """
1756

    
1757

    
1758
def _ParseArgs(binary, argv, commands, aliases, env_override):
1759
  """Parser for the command line arguments.
1760

1761
  This function parses the arguments and returns the function which
1762
  must be executed together with its (modified) arguments.
1763

1764
  @param binary: Script name
1765
  @param argv: Command line arguments
1766
  @param commands: Dictionary containing command definitions
1767
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1768
  @param env_override: list of env variables allowed for default args
1769
  @raise _ShowUsage: If usage description should be shown
1770
  @raise _ShowVersion: If version should be shown
1771

1772
  """
1773
  assert not (env_override - set(commands))
1774
  assert not (set(aliases.keys()) & set(commands.keys()))
1775

    
1776
  if len(argv) > 1:
1777
    cmd = argv[1]
1778
  else:
1779
    # No option or command given
1780
    raise _ShowUsage(exit_error=True)
1781

    
1782
  if cmd == "--version":
1783
    raise _ShowVersion()
1784
  elif cmd == "--help":
1785
    raise _ShowUsage(exit_error=False)
1786
  elif not (cmd in commands or cmd in aliases):
1787
    raise _ShowUsage(exit_error=True)
1788

    
1789
  # get command, unalias it, and look it up in commands
1790
  if cmd in aliases:
1791
    if aliases[cmd] not in commands:
1792
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1793
                                   " command '%s'" % (cmd, aliases[cmd]))
1794

    
1795
    cmd = aliases[cmd]
1796

    
1797
  if cmd in env_override:
1798
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1799
    env_args = os.environ.get(args_env_name)
1800
    if env_args:
1801
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1802

    
1803
  func, args_def, parser_opts, usage, description = commands[cmd]
1804
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1805
                        description=description,
1806
                        formatter=TitledHelpFormatter(),
1807
                        usage="%%prog %s %s" % (cmd, usage))
1808
  parser.disable_interspersed_args()
1809
  options, args = parser.parse_args(args=argv[2:])
1810

    
1811
  if not _CheckArguments(cmd, args_def, args):
1812
    return None, None, None
1813

    
1814
  return func, options, args
1815

    
1816

    
1817
def _FormatUsage(binary, commands):
1818
  """Generates a nice description of all commands.
1819

1820
  @param binary: Script name
1821
  @param commands: Dictionary containing command definitions
1822

1823
  """
1824
  # compute the max line length for cmd + usage
1825
  mlen = min(60, max(map(len, commands)))
1826

    
1827
  yield "Usage: %s {command} [options...] [argument...]" % binary
1828
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1829
  yield ""
1830
  yield "Commands:"
1831

    
1832
  # and format a nice command list
1833
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1834
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1835
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1836
    for line in help_lines:
1837
      yield " %-*s   %s" % (mlen, "", line)
1838

    
1839
  yield ""
1840

    
1841

    
1842
def _CheckArguments(cmd, args_def, args):
1843
  """Verifies the arguments using the argument definition.
1844

1845
  Algorithm:
1846

1847
    1. Abort with error if values specified by user but none expected.
1848

1849
    1. For each argument in definition
1850

1851
      1. Keep running count of minimum number of values (min_count)
1852
      1. Keep running count of maximum number of values (max_count)
1853
      1. If it has an unlimited number of values
1854

1855
        1. Abort with error if it's not the last argument in the definition
1856

1857
    1. If last argument has limited number of values
1858

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

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

1863
  """
1864
  if args and not args_def:
1865
    ToStderr("Error: Command %s expects no arguments", cmd)
1866
    return False
1867

    
1868
  min_count = None
1869
  max_count = None
1870
  check_max = None
1871

    
1872
  last_idx = len(args_def) - 1
1873

    
1874
  for idx, arg in enumerate(args_def):
1875
    if min_count is None:
1876
      min_count = arg.min
1877
    elif arg.min is not None:
1878
      min_count += arg.min
1879

    
1880
    if max_count is None:
1881
      max_count = arg.max
1882
    elif arg.max is not None:
1883
      max_count += arg.max
1884

    
1885
    if idx == last_idx:
1886
      check_max = (arg.max is not None)
1887

    
1888
    elif arg.max is None:
1889
      raise errors.ProgrammerError("Only the last argument can have max=None")
1890

    
1891
  if check_max:
1892
    # Command with exact number of arguments
1893
    if (min_count is not None and max_count is not None and
1894
        min_count == max_count and len(args) != min_count):
1895
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1896
      return False
1897

    
1898
    # Command with limited number of arguments
1899
    if max_count is not None and len(args) > max_count:
1900
      ToStderr("Error: Command %s expects only %d argument(s)",
1901
               cmd, max_count)
1902
      return False
1903

    
1904
  # Command with some required arguments
1905
  if min_count is not None and len(args) < min_count:
1906
    ToStderr("Error: Command %s expects at least %d argument(s)",
1907
             cmd, min_count)
1908
    return False
1909

    
1910
  return True
1911

    
1912

    
1913
def SplitNodeOption(value):
1914
  """Splits the value of a --node option.
1915

1916
  """
1917
  if value and ":" in value:
1918
    return value.split(":", 1)
1919
  else:
1920
    return (value, None)
1921

    
1922

    
1923
def CalculateOSNames(os_name, os_variants):
1924
  """Calculates all the names an OS can be called, according to its variants.
1925

1926
  @type os_name: string
1927
  @param os_name: base name of the os
1928
  @type os_variants: list or None
1929
  @param os_variants: list of supported variants
1930
  @rtype: list
1931
  @return: list of valid names
1932

1933
  """
1934
  if os_variants:
1935
    return ["%s+%s" % (os_name, v) for v in os_variants]
1936
  else:
1937
    return [os_name]
1938

    
1939

    
1940
def ParseFields(selected, default):
1941
  """Parses the values of "--field"-like options.
1942

1943
  @type selected: string or None
1944
  @param selected: User-selected options
1945
  @type default: list
1946
  @param default: Default fields
1947

1948
  """
1949
  if selected is None:
1950
    return default
1951

    
1952
  if selected.startswith("+"):
1953
    return default + selected[1:].split(",")
1954

    
1955
  return selected.split(",")
1956

    
1957

    
1958
UsesRPC = rpc.RunWithRPC
1959

    
1960

    
1961
def AskUser(text, choices=None):
1962
  """Ask the user a question.
1963

1964
  @param text: the question to ask
1965

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

1971
  @return: one of the return values from the choices list; if input is
1972
      not possible (i.e. not running with a tty, we return the last
1973
      entry from the list
1974

1975
  """
1976
  if choices is None:
1977
    choices = [("y", True, "Perform the operation"),
1978
               ("n", False, "Do not perform the operation")]
1979
  if not choices or not isinstance(choices, list):
1980
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1981
  for entry in choices:
1982
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1983
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1984

    
1985
  answer = choices[-1][1]
1986
  new_text = []
1987
  for line in text.splitlines():
1988
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1989
  text = "\n".join(new_text)
1990
  try:
1991
    f = file("/dev/tty", "a+")
1992
  except IOError:
1993
    return answer
1994
  try:
1995
    chars = [entry[0] for entry in choices]
1996
    chars[-1] = "[%s]" % chars[-1]
1997
    chars.append("?")
1998
    maps = dict([(entry[0], entry[1]) for entry in choices])
1999
    while True:
2000
      f.write(text)
2001
      f.write("\n")
2002
      f.write("/".join(chars))
2003
      f.write(": ")
2004
      line = f.readline(2).strip().lower()
2005
      if line in maps:
2006
        answer = maps[line]
2007
        break
2008
      elif line == "?":
2009
        for entry in choices:
2010
          f.write(" %s - %s\n" % (entry[0], entry[2]))
2011
        f.write("\n")
2012
        continue
2013
  finally:
2014
    f.close()
2015
  return answer
2016

    
2017

    
2018
class JobSubmittedException(Exception):
2019
  """Job was submitted, client should exit.
2020

2021
  This exception has one argument, the ID of the job that was
2022
  submitted. The handler should print this ID.
2023

2024
  This is not an error, just a structured way to exit from clients.
2025

2026
  """
2027

    
2028

    
2029
def SendJob(ops, cl=None):
2030
  """Function to submit an opcode without waiting for the results.
2031

2032
  @type ops: list
2033
  @param ops: list of opcodes
2034
  @type cl: luxi.Client
2035
  @param cl: the luxi client to use for communicating with the master;
2036
             if None, a new client will be created
2037

2038
  """
2039
  if cl is None:
2040
    cl = GetClient()
2041

    
2042
  job_id = cl.SubmitJob(ops)
2043

    
2044
  return job_id
2045

    
2046

    
2047
def GenericPollJob(job_id, cbs, report_cbs):
2048
  """Generic job-polling function.
2049

2050
  @type job_id: number
2051
  @param job_id: Job ID
2052
  @type cbs: Instance of L{JobPollCbBase}
2053
  @param cbs: Data callbacks
2054
  @type report_cbs: Instance of L{JobPollReportCbBase}
2055
  @param report_cbs: Reporting callbacks
2056

2057
  """
2058
  prev_job_info = None
2059
  prev_logmsg_serial = None
2060

    
2061
  status = None
2062

    
2063
  while True:
2064
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
2065
                                      prev_logmsg_serial)
2066
    if not result:
2067
      # job not found, go away!
2068
      raise errors.JobLost("Job with id %s lost" % job_id)
2069

    
2070
    if result == constants.JOB_NOTCHANGED:
2071
      report_cbs.ReportNotChanged(job_id, status)
2072

    
2073
      # Wait again
2074
      continue
2075

    
2076
    # Split result, a tuple of (field values, log entries)
2077
    (job_info, log_entries) = result
2078
    (status, ) = job_info
2079

    
2080
    if log_entries:
2081
      for log_entry in log_entries:
2082
        (serial, timestamp, log_type, message) = log_entry
2083
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
2084
                                    log_type, message)
2085
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
2086

    
2087
    # TODO: Handle canceled and archived jobs
2088
    elif status in (constants.JOB_STATUS_SUCCESS,
2089
                    constants.JOB_STATUS_ERROR,
2090
                    constants.JOB_STATUS_CANCELING,
2091
                    constants.JOB_STATUS_CANCELED):
2092
      break
2093

    
2094
    prev_job_info = job_info
2095

    
2096
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
2097
  if not jobs:
2098
    raise errors.JobLost("Job with id %s lost" % job_id)
2099

    
2100
  status, opstatus, result = jobs[0]
2101

    
2102
  if status == constants.JOB_STATUS_SUCCESS:
2103
    return result
2104

    
2105
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
2106
    raise errors.OpExecError("Job was canceled")
2107

    
2108
  has_ok = False
2109
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
2110
    if status == constants.OP_STATUS_SUCCESS:
2111
      has_ok = True
2112
    elif status == constants.OP_STATUS_ERROR:
2113
      errors.MaybeRaise(msg)
2114

    
2115
      if has_ok:
2116
        raise errors.OpExecError("partial failure (opcode %d): %s" %
2117
                                 (idx, msg))
2118

    
2119
      raise errors.OpExecError(str(msg))
2120

    
2121
  # default failure mode
2122
  raise errors.OpExecError(result)
2123

    
2124

    
2125
class JobPollCbBase:
2126
  """Base class for L{GenericPollJob} callbacks.
2127

2128
  """
2129
  def __init__(self):
2130
    """Initializes this class.
2131

2132
    """
2133

    
2134
  def WaitForJobChangeOnce(self, job_id, fields,
2135
                           prev_job_info, prev_log_serial):
2136
    """Waits for changes on a job.
2137

2138
    """
2139
    raise NotImplementedError()
2140

    
2141
  def QueryJobs(self, job_ids, fields):
2142
    """Returns the selected fields for the selected job IDs.
2143

2144
    @type job_ids: list of numbers
2145
    @param job_ids: Job IDs
2146
    @type fields: list of strings
2147
    @param fields: Fields
2148

2149
    """
2150
    raise NotImplementedError()
2151

    
2152

    
2153
class JobPollReportCbBase:
2154
  """Base class for L{GenericPollJob} reporting callbacks.
2155

2156
  """
2157
  def __init__(self):
2158
    """Initializes this class.
2159

2160
    """
2161

    
2162
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2163
    """Handles a log message.
2164

2165
    """
2166
    raise NotImplementedError()
2167

    
2168
  def ReportNotChanged(self, job_id, status):
2169
    """Called for if a job hasn't changed in a while.
2170

2171
    @type job_id: number
2172
    @param job_id: Job ID
2173
    @type status: string or None
2174
    @param status: Job status if available
2175

2176
    """
2177
    raise NotImplementedError()
2178

    
2179

    
2180
class _LuxiJobPollCb(JobPollCbBase):
2181
  def __init__(self, cl):
2182
    """Initializes this class.
2183

2184
    """
2185
    JobPollCbBase.__init__(self)
2186
    self.cl = cl
2187

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

2192
    """
2193
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2194
                                        prev_job_info, prev_log_serial)
2195

    
2196
  def QueryJobs(self, job_ids, fields):
2197
    """Returns the selected fields for the selected job IDs.
2198

2199
    """
2200
    return self.cl.QueryJobs(job_ids, fields)
2201

    
2202

    
2203
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2204
  def __init__(self, feedback_fn):
2205
    """Initializes this class.
2206

2207
    """
2208
    JobPollReportCbBase.__init__(self)
2209

    
2210
    self.feedback_fn = feedback_fn
2211

    
2212
    assert callable(feedback_fn)
2213

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

2217
    """
2218
    self.feedback_fn((timestamp, log_type, log_msg))
2219

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

2223
    """
2224
    # Ignore
2225

    
2226

    
2227
class StdioJobPollReportCb(JobPollReportCbBase):
2228
  def __init__(self):
2229
    """Initializes this class.
2230

2231
    """
2232
    JobPollReportCbBase.__init__(self)
2233

    
2234
    self.notified_queued = False
2235
    self.notified_waitlock = False
2236

    
2237
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2238
    """Handles a log message.
2239

2240
    """
2241
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2242
             FormatLogMessage(log_type, log_msg))
2243

    
2244
  def ReportNotChanged(self, job_id, status):
2245
    """Called if a job hasn't changed in a while.
2246

2247
    """
2248
    if status is None:
2249
      return
2250

    
2251
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2252
      ToStderr("Job %s is waiting in queue", job_id)
2253
      self.notified_queued = True
2254

    
2255
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2256
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2257
      self.notified_waitlock = True
2258

    
2259

    
2260
def FormatLogMessage(log_type, log_msg):
2261
  """Formats a job message according to its type.
2262

2263
  """
2264
  if log_type != constants.ELOG_MESSAGE:
2265
    log_msg = str(log_msg)
2266

    
2267
  return utils.SafeEncode(log_msg)
2268

    
2269

    
2270
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2271
  """Function to poll for the result of a job.
2272

2273
  @type job_id: job identified
2274
  @param job_id: the job to poll for results
2275
  @type cl: luxi.Client
2276
  @param cl: the luxi client to use for communicating with the master;
2277
             if None, a new client will be created
2278

2279
  """
2280
  if cl is None:
2281
    cl = GetClient()
2282

    
2283
  if reporter is None:
2284
    if feedback_fn:
2285
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2286
    else:
2287
      reporter = StdioJobPollReportCb()
2288
  elif feedback_fn:
2289
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2290

    
2291
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2292

    
2293

    
2294
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2295
  """Legacy function to submit an opcode.
2296

2297
  This is just a simple wrapper over the construction of the processor
2298
  instance. It should be extended to better handle feedback and
2299
  interaction functions.
2300

2301
  """
2302
  if cl is None:
2303
    cl = GetClient()
2304

    
2305
  SetGenericOpcodeOpts([op], opts)
2306

    
2307
  job_id = SendJob([op], cl=cl)
2308
  if hasattr(opts, "print_jobid") and opts.print_jobid:
2309
    ToStdout("%d" % job_id)
2310

    
2311
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2312
                       reporter=reporter)
2313

    
2314
  return op_results[0]
2315

    
2316

    
2317
def SubmitOpCodeToDrainedQueue(op):
2318
  """Forcefully insert a job in the queue, even if it is drained.
2319

2320
  """
2321
  cl = GetClient()
2322
  job_id = cl.SubmitJobToDrainedQueue([op])
2323
  op_results = PollJob(job_id, cl=cl)
2324
  return op_results[0]
2325

    
2326

    
2327
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2328
  """Wrapper around SubmitOpCode or SendJob.
2329

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

2335
  It will also process the opcodes if we're sending the via SendJob
2336
  (otherwise SubmitOpCode does it).
2337

2338
  """
2339
  if opts and opts.submit_only:
2340
    job = [op]
2341
    SetGenericOpcodeOpts(job, opts)
2342
    job_id = SendJob(job, cl=cl)
2343
    if opts.print_jobid:
2344
      ToStdout("%d" % job_id)
2345
    raise JobSubmittedException(job_id)
2346
  else:
2347
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2348

    
2349

    
2350
def _InitReasonTrail(op, opts):
2351
  """Builds the first part of the reason trail
2352

2353
  Builds the initial part of the reason trail, adding the user provided reason
2354
  (if it exists) and the name of the command starting the operation.
2355

2356
  @param op: the opcode the reason trail will be added to
2357
  @param opts: the command line options selected by the user
2358

2359
  """
2360
  assert len(sys.argv) >= 2
2361
  trail = []
2362

    
2363
  if opts.reason:
2364
    trail.append((constants.OPCODE_REASON_SRC_USER,
2365
                  opts.reason,
2366
                  utils.EpochNano()))
2367

    
2368
  binary = os.path.basename(sys.argv[0])
2369
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2370
  command = sys.argv[1]
2371
  trail.append((source, command, utils.EpochNano()))
2372
  op.reason = trail
2373

    
2374

    
2375
def SetGenericOpcodeOpts(opcode_list, options):
2376
  """Processor for generic options.
2377

2378
  This function updates the given opcodes based on generic command
2379
  line options (like debug, dry-run, etc.).
2380

2381
  @param opcode_list: list of opcodes
2382
  @param options: command line options or None
2383
  @return: None (in-place modification)
2384

2385
  """
2386
  if not options:
2387
    return
2388
  for op in opcode_list:
2389
    op.debug_level = options.debug
2390
    if hasattr(options, "dry_run"):
2391
      op.dry_run = options.dry_run
2392
    if getattr(options, "priority", None) is not None:
2393
      op.priority = options.priority
2394
    _InitReasonTrail(op, options)
2395

    
2396

    
2397
def FormatError(err):
2398
  """Return a formatted error message for a given error.
2399

2400
  This function takes an exception instance and returns a tuple
2401
  consisting of two values: first, the recommended exit code, and
2402
  second, a string describing the error message (not
2403
  newline-terminated).
2404

2405
  """
2406
  retcode = 1
2407
  obuf = StringIO()
2408
  msg = str(err)
2409
  if isinstance(err, errors.ConfigurationError):
2410
    txt = "Corrupt configuration file: %s" % msg
2411
    logging.error(txt)
2412
    obuf.write(txt + "\n")
2413
    obuf.write("Aborting.")
2414
    retcode = 2
2415
  elif isinstance(err, errors.HooksAbort):
2416
    obuf.write("Failure: hooks execution failed:\n")
2417
    for node, script, out in err.args[0]:
2418
      if out:
2419
        obuf.write("  node: %s, script: %s, output: %s\n" %
2420
                   (node, script, out))
2421
      else:
2422
        obuf.write("  node: %s, script: %s (no output)\n" %
2423
                   (node, script))
2424
  elif isinstance(err, errors.HooksFailure):
2425
    obuf.write("Failure: hooks general failure: %s" % msg)
2426
  elif isinstance(err, errors.ResolverError):
2427
    this_host = netutils.Hostname.GetSysName()
2428
    if err.args[0] == this_host:
2429
      msg = "Failure: can't resolve my own hostname ('%s')"
2430
    else:
2431
      msg = "Failure: can't resolve hostname '%s'"
2432
    obuf.write(msg % err.args[0])
2433
  elif isinstance(err, errors.OpPrereqError):
2434
    if len(err.args) == 2:
2435
      obuf.write("Failure: prerequisites not met for this"
2436
                 " operation:\nerror type: %s, error details:\n%s" %
2437
                 (err.args[1], err.args[0]))
2438
    else:
2439
      obuf.write("Failure: prerequisites not met for this"
2440
                 " operation:\n%s" % msg)
2441
  elif isinstance(err, errors.OpExecError):
2442
    obuf.write("Failure: command execution error:\n%s" % msg)
2443
  elif isinstance(err, errors.TagError):
2444
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2445
  elif isinstance(err, errors.JobQueueDrainError):
2446
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2447
               " accept new requests\n")
2448
  elif isinstance(err, errors.JobQueueFull):
2449
    obuf.write("Failure: the job queue is full and doesn't accept new"
2450
               " job submissions until old jobs are archived\n")
2451
  elif isinstance(err, errors.TypeEnforcementError):
2452
    obuf.write("Parameter Error: %s" % msg)
2453
  elif isinstance(err, errors.ParameterError):
2454
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2455
  elif isinstance(err, rpcerr.NoMasterError):
2456
    if err.args[0] == pathutils.MASTER_SOCKET:
2457
      daemon = "the master daemon"
2458
    elif err.args[0] == pathutils.QUERY_SOCKET:
2459
      daemon = "the config daemon"
2460
    else:
2461
      daemon = "socket '%s'" % str(err.args[0])
2462
    obuf.write("Cannot communicate with %s.\nIs the process running"
2463
               " and listening for connections?" % daemon)
2464
  elif isinstance(err, rpcerr.TimeoutError):
2465
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2466
               " been submitted and will continue to run even if the call"
2467
               " timed out. Useful commands in this situation are \"gnt-job"
2468
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2469
    obuf.write(msg)
2470
  elif isinstance(err, rpcerr.PermissionError):
2471
    obuf.write("It seems you don't have permissions to connect to the"
2472
               " master daemon.\nPlease retry as a different user.")
2473
  elif isinstance(err, rpcerr.ProtocolError):
2474
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2475
               "%s" % msg)
2476
  elif isinstance(err, errors.JobLost):
2477
    obuf.write("Error checking job status: %s" % msg)
2478
  elif isinstance(err, errors.QueryFilterParseError):
2479
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2480
    obuf.write("\n".join(err.GetDetails()))
2481
  elif isinstance(err, errors.GenericError):
2482
    obuf.write("Unhandled Ganeti error: %s" % msg)
2483
  elif isinstance(err, JobSubmittedException):
2484
    obuf.write("JobID: %s\n" % err.args[0])
2485
    retcode = 0
2486
  else:
2487
    obuf.write("Unhandled exception: %s" % msg)
2488
  return retcode, obuf.getvalue().rstrip("\n")
2489

    
2490

    
2491
def GenericMain(commands, override=None, aliases=None,
2492
                env_override=frozenset()):
2493
  """Generic main function for all the gnt-* commands.
2494

2495
  @param commands: a dictionary with a special structure, see the design doc
2496
                   for command line handling.
2497
  @param override: if not None, we expect a dictionary with keys that will
2498
                   override command line options; this can be used to pass
2499
                   options from the scripts to generic functions
2500
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2501
  @param env_override: list of environment names which are allowed to submit
2502
                       default args for commands
2503

2504
  """
2505
  # save the program name and the entire command line for later logging
2506
  if sys.argv:
2507
    binary = os.path.basename(sys.argv[0])
2508
    if not binary:
2509
      binary = sys.argv[0]
2510

    
2511
    if len(sys.argv) >= 2:
2512
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2513
    else:
2514
      logname = binary
2515

    
2516
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2517
  else:
2518
    binary = "<unknown program>"
2519
    cmdline = "<unknown>"
2520

    
2521
  if aliases is None:
2522
    aliases = {}
2523

    
2524
  try:
2525
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2526
                                       env_override)
2527
  except _ShowVersion:
2528
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2529
             constants.RELEASE_VERSION)
2530
    return constants.EXIT_SUCCESS
2531
  except _ShowUsage, err:
2532
    for line in _FormatUsage(binary, commands):
2533
      ToStdout(line)
2534

    
2535
    if err.exit_error:
2536
      return constants.EXIT_FAILURE
2537
    else:
2538
      return constants.EXIT_SUCCESS
2539
  except errors.ParameterError, err:
2540
    result, err_msg = FormatError(err)
2541
    ToStderr(err_msg)
2542
    return 1
2543

    
2544
  if func is None: # parse error
2545
    return 1
2546

    
2547
  if override is not None:
2548
    for key, val in override.iteritems():
2549
      setattr(options, key, val)
2550

    
2551
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2552
                     stderr_logging=True)
2553

    
2554
  logging.info("Command line: %s", cmdline)
2555

    
2556
  try:
2557
    result = func(options, args)
2558
  except (errors.GenericError, rpcerr.ProtocolError,
2559
          JobSubmittedException), err:
2560
    result, err_msg = FormatError(err)
2561
    logging.exception("Error during command processing")
2562
    ToStderr(err_msg)
2563
  except KeyboardInterrupt:
2564
    result = constants.EXIT_FAILURE
2565
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2566
             " might have been submitted and"
2567
             " will continue to run in the background.")
2568
  except IOError, err:
2569
    if err.errno == errno.EPIPE:
2570
      # our terminal went away, we'll exit
2571
      sys.exit(constants.EXIT_FAILURE)
2572
    else:
2573
      raise
2574

    
2575
  return result
2576

    
2577

    
2578
def ParseNicOption(optvalue):
2579
  """Parses the value of the --net option(s).
2580

2581
  """
2582
  try:
2583
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2584
  except (TypeError, ValueError), err:
2585
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2586
                               errors.ECODE_INVAL)
2587

    
2588
  nics = [{}] * nic_max
2589
  for nidx, ndict in optvalue:
2590
    nidx = int(nidx)
2591

    
2592
    if not isinstance(ndict, dict):
2593
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2594
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2595

    
2596
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2597

    
2598
    nics[nidx] = ndict
2599

    
2600
  return nics
2601

    
2602

    
2603
def GenericInstanceCreate(mode, opts, args):
2604
  """Add an instance to the cluster via either creation or import.
2605

2606
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2607
  @param opts: the command line options selected by the user
2608
  @type args: list
2609
  @param args: should contain only one element, the new instance name
2610
  @rtype: int
2611
  @return: the desired exit code
2612

2613
  """
2614
  instance = args[0]
2615

    
2616
  (pnode, snode) = SplitNodeOption(opts.node)
2617

    
2618
  hypervisor = None
2619
  hvparams = {}
2620
  if opts.hypervisor:
2621
    hypervisor, hvparams = opts.hypervisor
2622

    
2623
  if opts.nics:
2624
    nics = ParseNicOption(opts.nics)
2625
  elif opts.no_nics:
2626
    # no nics
2627
    nics = []
2628
  elif mode == constants.INSTANCE_CREATE:
2629
    # default of one nic, all auto
2630
    nics = [{}]
2631
  else:
2632
    # mode == import
2633
    nics = []
2634

    
2635
  if opts.disk_template == constants.DT_DISKLESS:
2636
    if opts.disks or opts.sd_size is not None:
2637
      raise errors.OpPrereqError("Diskless instance but disk"
2638
                                 " information passed", errors.ECODE_INVAL)
2639
    disks = []
2640
  else:
2641
    if (not opts.disks and not opts.sd_size
2642
        and mode == constants.INSTANCE_CREATE):
2643
      raise errors.OpPrereqError("No disk information specified",
2644
                                 errors.ECODE_INVAL)
2645
    if opts.disks and opts.sd_size is not None:
2646
      raise errors.OpPrereqError("Please use either the '--disk' or"
2647
                                 " '-s' option", errors.ECODE_INVAL)
2648
    if opts.sd_size is not None:
2649
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2650

    
2651
    if opts.disks:
2652
      try:
2653
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2654
      except ValueError, err:
2655
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
2656
                                   errors.ECODE_INVAL)
2657
      disks = [{}] * disk_max
2658
    else:
2659
      disks = []
2660
    for didx, ddict in opts.disks:
2661
      didx = int(didx)
2662
      if not isinstance(ddict, dict):
2663
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2664
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
2665
      elif constants.IDISK_SIZE in ddict:
2666
        if constants.IDISK_ADOPT in ddict:
2667
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2668
                                     " (disk %d)" % didx, errors.ECODE_INVAL)
2669
        try:
2670
          ddict[constants.IDISK_SIZE] = \
2671
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2672
        except ValueError, err:
2673
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2674
                                     (didx, err), errors.ECODE_INVAL)
2675
      elif constants.IDISK_ADOPT in ddict:
2676
        if constants.IDISK_SPINDLES in ddict:
2677
          raise errors.OpPrereqError("spindles is not a valid option when"
2678
                                     " adopting a disk", errors.ECODE_INVAL)
2679
        if mode == constants.INSTANCE_IMPORT:
2680
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2681
                                     " import", errors.ECODE_INVAL)
2682
        ddict[constants.IDISK_SIZE] = 0
2683
      else:
2684
        raise errors.OpPrereqError("Missing size or adoption source for"
2685
                                   " disk %d" % didx, errors.ECODE_INVAL)
2686
      if constants.IDISK_SPINDLES in ddict:
2687
        ddict[constants.IDISK_SPINDLES] = int(ddict[constants.IDISK_SPINDLES])
2688

    
2689
      disks[didx] = ddict
2690

    
2691
  if opts.tags is not None:
2692
    tags = opts.tags.split(",")
2693
  else:
2694
    tags = []
2695

    
2696
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2697
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2698

    
2699
  if mode == constants.INSTANCE_CREATE:
2700
    start = opts.start
2701
    os_type = opts.os
2702
    force_variant = opts.force_variant
2703
    src_node = None
2704
    src_path = None
2705
    no_install = opts.no_install
2706
    identify_defaults = False
2707
    compress = constants.IEC_NONE
2708
  elif mode == constants.INSTANCE_IMPORT:
2709
    start = False
2710
    os_type = None
2711
    force_variant = False
2712
    src_node = opts.src_node
2713
    src_path = opts.src_dir
2714
    no_install = None
2715
    identify_defaults = opts.identify_defaults
2716
    compress = opts.compress
2717
  else:
2718
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2719

    
2720
  op = opcodes.OpInstanceCreate(instance_name=instance,
2721
                                disks=disks,
2722
                                disk_template=opts.disk_template,
2723
                                nics=nics,
2724
                                conflicts_check=opts.conflicts_check,
2725
                                pnode=pnode, snode=snode,
2726
                                ip_check=opts.ip_check,
2727
                                name_check=opts.name_check,
2728
                                wait_for_sync=opts.wait_for_sync,
2729
                                file_storage_dir=opts.file_storage_dir,
2730
                                file_driver=opts.file_driver,
2731
                                iallocator=opts.iallocator,
2732
                                hypervisor=hypervisor,
2733
                                hvparams=hvparams,
2734
                                beparams=opts.beparams,
2735
                                osparams=opts.osparams,
2736
                                mode=mode,
2737
                                start=start,
2738
                                os_type=os_type,
2739
                                force_variant=force_variant,
2740
                                src_node=src_node,
2741
                                src_path=src_path,
2742
                                compress=compress,
2743
                                tags=tags,
2744
                                no_install=no_install,
2745
                                identify_defaults=identify_defaults,
2746
                                ignore_ipolicy=opts.ignore_ipolicy)
2747

    
2748
  SubmitOrSend(op, opts)
2749
  return 0
2750

    
2751

    
2752
class _RunWhileClusterStoppedHelper:
2753
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2754

2755
  """
2756
  def __init__(self, feedback_fn, cluster_name, master_node,
2757
               online_nodes, ssh_ports):
2758
    """Initializes this class.
2759

2760
    @type feedback_fn: callable
2761
    @param feedback_fn: Feedback function
2762
    @type cluster_name: string
2763
    @param cluster_name: Cluster name
2764
    @type master_node: string
2765
    @param master_node Master node name
2766
    @type online_nodes: list
2767
    @param online_nodes: List of names of online nodes
2768
    @type ssh_ports: list
2769
    @param ssh_ports: List of SSH ports of online nodes
2770

2771
    """
2772
    self.feedback_fn = feedback_fn
2773
    self.cluster_name = cluster_name
2774
    self.master_node = master_node
2775
    self.online_nodes = online_nodes
2776
    self.ssh_ports = dict(zip(online_nodes, ssh_ports))
2777

    
2778
    self.ssh = ssh.SshRunner(self.cluster_name)
2779

    
2780
    self.nonmaster_nodes = [name for name in online_nodes
2781
                            if name != master_node]
2782

    
2783
    assert self.master_node not in self.nonmaster_nodes
2784

    
2785
  def _RunCmd(self, node_name, cmd):
2786
    """Runs a command on the local or a remote machine.
2787

2788
    @type node_name: string
2789
    @param node_name: Machine name
2790
    @type cmd: list
2791
    @param cmd: Command
2792

2793
    """
2794
    if node_name is None or node_name == self.master_node:
2795
      # No need to use SSH
2796
      result = utils.RunCmd(cmd)
2797
    else:
2798
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2799
                            utils.ShellQuoteArgs(cmd),
2800
                            port=self.ssh_ports[node_name])
2801

    
2802
    if result.failed:
2803
      errmsg = ["Failed to run command %s" % result.cmd]
2804
      if node_name:
2805
        errmsg.append("on node %s" % node_name)
2806
      errmsg.append(": exitcode %s and error %s" %
2807
                    (result.exit_code, result.output))
2808
      raise errors.OpExecError(" ".join(errmsg))
2809

    
2810
  def Call(self, fn, *args):
2811
    """Call function while all daemons are stopped.
2812

2813
    @type fn: callable
2814
    @param fn: Function to be called
2815

2816
    """
2817
    # Pause watcher by acquiring an exclusive lock on watcher state file
2818
    self.feedback_fn("Blocking watcher")
2819
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2820
    try:
2821
      # TODO: Currently, this just blocks. There's no timeout.
2822
      # TODO: Should it be a shared lock?
2823
      watcher_block.Exclusive(blocking=True)
2824

    
2825
      # Stop master daemons, so that no new jobs can come in and all running
2826
      # ones are finished
2827
      self.feedback_fn("Stopping master daemons")
2828
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2829
      try:
2830
        # Stop daemons on all nodes
2831
        for node_name in self.online_nodes:
2832
          self.feedback_fn("Stopping daemons on %s" % node_name)
2833
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2834

    
2835
        # All daemons are shut down now
2836
        try:
2837
          return fn(self, *args)
2838
        except Exception, err:
2839
          _, errmsg = FormatError(err)
2840
          logging.exception("Caught exception")
2841
          self.feedback_fn(errmsg)
2842
          raise
2843
      finally:
2844
        # Start cluster again, master node last
2845
        for node_name in self.nonmaster_nodes + [self.master_node]:
2846
          self.feedback_fn("Starting daemons on %s" % node_name)
2847
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"])
2848
    finally:
2849
      # Resume watcher
2850
      watcher_block.Close()
2851

    
2852

    
2853
def RunWhileClusterStopped(feedback_fn, fn, *args):
2854
  """Calls a function while all cluster daemons are stopped.
2855

2856
  @type feedback_fn: callable
2857
  @param feedback_fn: Feedback function
2858
  @type fn: callable
2859
  @param fn: Function to be called when daemons are stopped
2860

2861
  """
2862
  feedback_fn("Gathering cluster information")
2863

    
2864
  # This ensures we're running on the master daemon
2865
  cl = GetClient()
2866
  # Query client
2867
  qcl = GetClient(query=True)
2868

    
2869
  (cluster_name, master_node) = \
2870
    cl.QueryConfigValues(["cluster_name", "master_node"])
2871

    
2872
  online_nodes = GetOnlineNodes([], cl=qcl)
2873
  ssh_ports = GetNodesSshPorts(online_nodes, qcl)
2874

    
2875
  # Don't keep a reference to the client. The master daemon will go away.
2876
  del cl
2877
  del qcl
2878

    
2879
  assert master_node in online_nodes
2880

    
2881
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2882
                                       online_nodes, ssh_ports).Call(fn, *args)
2883

    
2884

    
2885
def GenerateTable(headers, fields, separator, data,
2886
                  numfields=None, unitfields=None,
2887
                  units=None):
2888
  """Prints a table with headers and different fields.
2889

2890
  @type headers: dict
2891
  @param headers: dictionary mapping field names to headers for
2892
      the table
2893
  @type fields: list
2894
  @param fields: the field names corresponding to each row in
2895
      the data field
2896
  @param separator: the separator to be used; if this is None,
2897
      the default 'smart' algorithm is used which computes optimal
2898
      field width, otherwise just the separator is used between
2899
      each field
2900
  @type data: list
2901
  @param data: a list of lists, each sublist being one row to be output
2902
  @type numfields: list
2903
  @param numfields: a list with the fields that hold numeric
2904
      values and thus should be right-aligned
2905
  @type unitfields: list
2906
  @param unitfields: a list with the fields that hold numeric
2907
      values that should be formatted with the units field
2908
  @type units: string or None
2909
  @param units: the units we should use for formatting, or None for
2910
      automatic choice (human-readable for non-separator usage, otherwise
2911
      megabytes); this is a one-letter string
2912

2913
  """
2914
  if units is None:
2915
    if separator:
2916
      units = "m"
2917
    else:
2918
      units = "h"
2919

    
2920
  if numfields is None:
2921
    numfields = []
2922
  if unitfields is None:
2923
    unitfields = []
2924

    
2925
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2926
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2927

    
2928
  format_fields = []
2929
  for field in fields:
2930
    if headers and field not in headers:
2931
      # TODO: handle better unknown fields (either revert to old
2932
      # style of raising exception, or deal more intelligently with
2933
      # variable fields)
2934
      headers[field] = field
2935
    if separator is not None:
2936
      format_fields.append("%s")
2937
    elif numfields.Matches(field):
2938
      format_fields.append("%*s")
2939
    else:
2940
      format_fields.append("%-*s")
2941

    
2942
  if separator is None:
2943
    mlens = [0 for name in fields]
2944
    format_str = " ".join(format_fields)
2945
  else:
2946
    format_str = separator.replace("%", "%%").join(format_fields)
2947

    
2948
  for row in data:
2949
    if row is None:
2950
      continue
2951
    for idx, val in enumerate(row):
2952
      if unitfields.Matches(fields[idx]):
2953
        try:
2954
          val = int(val)
2955
        except (TypeError, ValueError):
2956
          pass
2957
        else:
2958
          val = row[idx] = utils.FormatUnit(val, units)
2959
      val = row[idx] = str(val)
2960
      if separator is None:
2961
        mlens[idx] = max(mlens[idx], len(val))
2962

    
2963
  result = []
2964
  if headers:
2965
    args = []
2966
    for idx, name in enumerate(fields):
2967
      hdr = headers[name]
2968
      if separator is None:
2969
        mlens[idx] = max(mlens[idx], len(hdr))
2970
        args.append(mlens[idx])
2971
      args.append(hdr)
2972
    result.append(format_str % tuple(args))
2973

    
2974
  if separator is None:
2975
    assert len(mlens) == len(fields)
2976

    
2977
    if fields and not numfields.Matches(fields[-1]):
2978
      mlens[-1] = 0
2979

    
2980
  for line in data:
2981
    args = []
2982
    if line is None:
2983
      line = ["-" for _ in fields]
2984
    for idx in range(len(fields)):
2985
      if separator is None:
2986
        args.append(mlens[idx])
2987
      args.append(line[idx])
2988
    result.append(format_str % tuple(args))
2989

    
2990
  return result
2991

    
2992

    
2993
def _FormatBool(value):
2994
  """Formats a boolean value as a string.
2995

2996
  """
2997
  if value:
2998
    return "Y"
2999
  return "N"
3000

    
3001

    
3002
#: Default formatting for query results; (callback, align right)
3003
_DEFAULT_FORMAT_QUERY = {
3004
  constants.QFT_TEXT: (str, False),
3005
  constants.QFT_BOOL: (_FormatBool, False),
3006
  constants.QFT_NUMBER: (str, True),
3007
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
3008
  constants.QFT_OTHER: (str, False),
3009
  constants.QFT_UNKNOWN: (str, False),
3010
  }
3011

    
3012

    
3013
def _GetColumnFormatter(fdef, override, unit):
3014
  """Returns formatting function for a field.
3015

3016
  @type fdef: L{objects.QueryFieldDefinition}
3017
  @type override: dict
3018
  @param override: Dictionary for overriding field formatting functions,
3019
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3020
  @type unit: string
3021
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
3022
  @rtype: tuple; (callable, bool)
3023
  @return: Returns the function to format a value (takes one parameter) and a
3024
    boolean for aligning the value on the right-hand side
3025

3026
  """
3027
  fmt = override.get(fdef.name, None)
3028
  if fmt is not None:
3029
    return fmt
3030

    
3031
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3032

    
3033
  if fdef.kind == constants.QFT_UNIT:
3034
    # Can't keep this information in the static dictionary
3035
    return (lambda value: utils.FormatUnit(value, unit), True)
3036

    
3037
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
3038
  if fmt is not None:
3039
    return fmt
3040

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

    
3043

    
3044
class _QueryColumnFormatter:
3045
  """Callable class for formatting fields of a query.
3046

3047
  """
3048
  def __init__(self, fn, status_fn, verbose):
3049
    """Initializes this class.
3050

3051
    @type fn: callable
3052
    @param fn: Formatting function
3053
    @type status_fn: callable
3054
    @param status_fn: Function to report fields' status
3055
    @type verbose: boolean
3056
    @param verbose: whether to use verbose field descriptions or not
3057

3058
    """
3059
    self._fn = fn
3060
    self._status_fn = status_fn
3061
    self._verbose = verbose
3062

    
3063
  def __call__(self, data):
3064
    """Returns a field's string representation.
3065

3066
    """
3067
    (status, value) = data
3068

    
3069
    # Report status
3070
    self._status_fn(status)
3071

    
3072
    if status == constants.RS_NORMAL:
3073
      return self._fn(value)
3074

    
3075
    assert value is None, \
3076
           "Found value %r for abnormal status %s" % (value, status)
3077

    
3078
    return FormatResultError(status, self._verbose)
3079

    
3080

    
3081
def FormatResultError(status, verbose):
3082
  """Formats result status other than L{constants.RS_NORMAL}.
3083

3084
  @param status: The result status
3085
  @type verbose: boolean
3086
  @param verbose: Whether to return the verbose text
3087
  @return: Text of result status
3088

3089
  """
3090
  assert status != constants.RS_NORMAL, \
3091
         "FormatResultError called with status equal to constants.RS_NORMAL"
3092
  try:
3093
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
3094
  except KeyError:
3095
    raise NotImplementedError("Unknown status %s" % status)
3096
  else:
3097
    if verbose:
3098
      return verbose_text
3099
    return normal_text
3100

    
3101

    
3102
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3103
                      header=False, verbose=False):
3104
  """Formats data in L{objects.QueryResponse}.
3105

3106
  @type result: L{objects.QueryResponse}
3107
  @param result: result of query operation
3108
  @type unit: string
3109
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
3110
    see L{utils.text.FormatUnit}
3111
  @type format_override: dict
3112
  @param format_override: Dictionary for overriding field formatting functions,
3113
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3114
  @type separator: string or None
3115
  @param separator: String used to separate fields
3116
  @type header: bool
3117
  @param header: Whether to output header row
3118
  @type verbose: boolean
3119
  @param verbose: whether to use verbose field descriptions or not
3120

3121
  """
3122
  if unit is None:
3123
    if separator:
3124
      unit = "m"
3125
    else:
3126
      unit = "h"
3127

    
3128
  if format_override is None:
3129
    format_override = {}
3130

    
3131
  stats = dict.fromkeys(constants.RS_ALL, 0)
3132

    
3133
  def _RecordStatus(status):
3134
    if status in stats:
3135
      stats[status] += 1
3136

    
3137
  columns = []
3138
  for fdef in result.fields:
3139
    assert fdef.title and fdef.name
3140
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3141
    columns.append(TableColumn(fdef.title,
3142
                               _QueryColumnFormatter(fn, _RecordStatus,
3143
                                                     verbose),
3144
                               align_right))
3145

    
3146
  table = FormatTable(result.data, columns, header, separator)
3147

    
3148
  # Collect statistics
3149
  assert len(stats) == len(constants.RS_ALL)
3150
  assert compat.all(count >= 0 for count in stats.values())
3151

    
3152
  # Determine overall status. If there was no data, unknown fields must be
3153
  # detected via the field definitions.
3154
  if (stats[constants.RS_UNKNOWN] or
3155
      (not result.data and _GetUnknownFields(result.fields))):
3156
    status = QR_UNKNOWN
3157
  elif compat.any(count > 0 for key, count in stats.items()
3158
                  if key != constants.RS_NORMAL):
3159
    status = QR_INCOMPLETE
3160
  else:
3161
    status = QR_NORMAL
3162

    
3163
  return (status, table)
3164

    
3165

    
3166
def _GetUnknownFields(fdefs):
3167
  """Returns list of unknown fields included in C{fdefs}.
3168

3169
  @type fdefs: list of L{objects.QueryFieldDefinition}
3170

3171
  """
3172
  return [fdef for fdef in fdefs
3173
          if fdef.kind == constants.QFT_UNKNOWN]
3174

    
3175

    
3176
def _WarnUnknownFields(fdefs):
3177
  """Prints a warning to stderr if a query included unknown fields.
3178

3179
  @type fdefs: list of L{objects.QueryFieldDefinition}
3180

3181
  """
3182
  unknown = _GetUnknownFields(fdefs)
3183
  if unknown:
3184
    ToStderr("Warning: Queried for unknown fields %s",
3185
             utils.CommaJoin(fdef.name for fdef in unknown))
3186
    return True
3187

    
3188
  return False
3189

    
3190

    
3191
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3192
                format_override=None, verbose=False, force_filter=False,
3193
                namefield=None, qfilter=None, isnumeric=False):
3194
  """Generic implementation for listing all items of a resource.
3195

3196
  @param resource: One of L{constants.QR_VIA_LUXI}
3197
  @type fields: list of strings
3198
  @param fields: List of fields to query for
3199
  @type names: list of strings
3200
  @param names: Names of items to query for
3201
  @type unit: string or None
3202
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
3203
    None for automatic choice (human-readable for non-separator usage,
3204
    otherwise megabytes); this is a one-letter string
3205
  @type separator: string or None
3206
  @param separator: String used to separate fields
3207
  @type header: bool
3208
  @param header: Whether to show header row
3209
  @type force_filter: bool
3210
  @param force_filter: Whether to always treat names as filter
3211
  @type format_override: dict
3212
  @param format_override: Dictionary for overriding field formatting functions,
3213
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3214
  @type verbose: boolean
3215
  @param verbose: whether to use verbose field descriptions or not
3216
  @type namefield: string
3217
  @param namefield: Name of field to use for simple filters (see
3218
    L{qlang.MakeFilter} for details)
3219
  @type qfilter: list or None
3220
  @param qfilter: Query filter (in addition to names)
3221
  @param isnumeric: bool
3222
  @param isnumeric: Whether the namefield's type is numeric, and therefore
3223
    any simple filters built by namefield should use integer values to
3224
    reflect that
3225

3226
  """
3227
  if not names:
3228
    names = None
3229

    
3230
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3231
                                isnumeric=isnumeric)
3232

    
3233
  if qfilter is None:
3234
    qfilter = namefilter
3235
  elif namefilter is not None:
3236
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3237

    
3238
  if cl is None:
3239
    cl = GetClient()
3240

    
3241
  response = cl.Query(resource, fields, qfilter)
3242

    
3243
  found_unknown = _WarnUnknownFields(response.fields)
3244

    
3245
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3246
                                     header=header,
3247
                                     format_override=format_override,
3248
                                     verbose=verbose)
3249

    
3250
  for line in data:
3251
    ToStdout(line)
3252

    
3253
  assert ((found_unknown and status == QR_UNKNOWN) or
3254
          (not found_unknown and status != QR_UNKNOWN))
3255

    
3256
  if status == QR_UNKNOWN:
3257
    return constants.EXIT_UNKNOWN_FIELD
3258

    
3259
  # TODO: Should the list command fail if not all data could be collected?
3260
  return constants.EXIT_SUCCESS
3261

    
3262

    
3263
def _FieldDescValues(fdef):
3264
  """Helper function for L{GenericListFields} to get query field description.
3265

3266
  @type fdef: L{objects.QueryFieldDefinition}
3267
  @rtype: list
3268

3269
  """
3270
  return [
3271
    fdef.name,
3272
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3273
    fdef.title,
3274
    fdef.doc,
3275
    ]
3276

    
3277

    
3278
def GenericListFields(resource, fields, separator, header, cl=None):
3279
  """Generic implementation for listing fields for a resource.
3280

3281
  @param resource: One of L{constants.QR_VIA_LUXI}
3282
  @type fields: list of strings
3283
  @param fields: List of fields to query for
3284
  @type separator: string or None
3285
  @param separator: String used to separate fields
3286
  @type header: bool
3287
  @param header: Whether to show header row
3288

3289
  """
3290
  if cl is None:
3291
    cl = GetClient()
3292

    
3293
  if not fields:
3294
    fields = None
3295

    
3296
  response = cl.QueryFields(resource, fields)
3297

    
3298
  found_unknown = _WarnUnknownFields(response.fields)
3299

    
3300
  columns = [
3301
    TableColumn("Name", str, False),
3302
    TableColumn("Type", str, False),
3303
    TableColumn("Title", str, False),
3304
    TableColumn("Description", str, False),
3305
    ]
3306

    
3307
  rows = map(_FieldDescValues, response.fields)
3308

    
3309
  for line in FormatTable(rows, columns, header, separator):
3310
    ToStdout(line)
3311

    
3312
  if found_unknown:
3313
    return constants.EXIT_UNKNOWN_FIELD
3314

    
3315
  return constants.EXIT_SUCCESS
3316

    
3317

    
3318
class TableColumn:
3319
  """Describes a column for L{FormatTable}.
3320

3321
  """
3322
  def __init__(self, title, fn, align_right):
3323
    """Initializes this class.
3324

3325
    @type title: string
3326
    @param title: Column title
3327
    @type fn: callable
3328
    @param fn: Formatting function
3329
    @type align_right: bool
3330
    @param align_right: Whether to align values on the right-hand side
3331

3332
    """
3333
    self.title = title
3334
    self.format = fn
3335
    self.align_right = align_right
3336

    
3337

    
3338
def _GetColFormatString(width, align_right):
3339
  """Returns the format string for a field.
3340

3341
  """
3342
  if align_right:
3343
    sign = ""
3344
  else:
3345
    sign = "-"
3346

    
3347
  return "%%%s%ss" % (sign, width)
3348

    
3349

    
3350
def FormatTable(rows, columns, header, separator):
3351
  """Formats data as a table.
3352

3353
  @type rows: list of lists
3354
  @param rows: Row data, one list per row
3355
  @type columns: list of L{TableColumn}
3356
  @param columns: Column descriptions
3357
  @type header: bool
3358
  @param header: Whether to show header row
3359
  @type separator: string or None
3360
  @param separator: String used to separate columns
3361

3362
  """
3363
  if header:
3364
    data = [[col.title for col in columns]]
3365
    colwidth = [len(col.title) for col in columns]
3366
  else:
3367
    data = []
3368
    colwidth = [0 for _ in columns]
3369

    
3370
  # Format row data
3371
  for row in rows:
3372
    assert len(row) == len(columns)
3373

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

    
3376
    if separator is None:
3377
      # Update column widths
3378
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3379
        # Modifying a list's items while iterating is fine
3380
        colwidth[idx] = max(oldwidth, len(value))
3381

    
3382
    data.append(formatted)
3383

    
3384
  if separator is not None:
3385
    # Return early if a separator is used
3386
    return [separator.join(row) for row in data]
3387

    
3388
  if columns and not columns[-1].align_right:
3389
    # Avoid unnecessary spaces at end of line
3390
    colwidth[-1] = 0
3391

    
3392
  # Build format string
3393
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3394
                  for col, width in zip(columns, colwidth)])
3395

    
3396
  return [fmt % tuple(row) for row in data]
3397

    
3398

    
3399
def FormatTimestamp(ts):
3400
  """Formats a given timestamp.
3401

3402
  @type ts: timestamp
3403
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3404

3405
  @rtype: string
3406
  @return: a string with the formatted timestamp
3407

3408
  """
3409
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3410
    return "?"
3411

    
3412
  (sec, usecs) = ts
3413
  return utils.FormatTime(sec, usecs=usecs)
3414

    
3415

    
3416
def ParseTimespec(value):
3417
  """Parse a time specification.
3418

3419
  The following suffixed will be recognized:
3420

3421
    - s: seconds
3422
    - m: minutes
3423
    - h: hours
3424
    - d: day
3425
    - w: weeks
3426

3427
  Without any suffix, the value will be taken to be in seconds.
3428

3429
  """
3430
  value = str(value)
3431
  if not value:
3432
    raise errors.OpPrereqError("Empty time specification passed",
3433
                               errors.ECODE_INVAL)
3434
  suffix_map = {
3435
    "s": 1,
3436
    "m": 60,
3437
    "h": 3600,
3438
    "d": 86400,
3439
    "w": 604800,
3440
    }
3441
  if value[-1] not in suffix_map:
3442
    try:
3443
      value = int(value)
3444
    except (TypeError, ValueError):
3445
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3446
                                 errors.ECODE_INVAL)
3447
  else:
3448
    multiplier = suffix_map[value[-1]]
3449
    value = value[:-1]
3450
    if not value: # no data left after stripping the suffix
3451
      raise errors.OpPrereqError("Invalid time specification (only"
3452
                                 " suffix passed)", errors.ECODE_INVAL)
3453
    try:
3454
      value = int(value) * multiplier
3455
    except (TypeError, ValueError):
3456
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3457
                                 errors.ECODE_INVAL)
3458
  return value
3459

    
3460

    
3461
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3462
                   filter_master=False, nodegroup=None):
3463
  """Returns the names of online nodes.
3464

3465
  This function will also log a warning on stderr with the names of
3466
  the online nodes.
3467

3468
  @param nodes: if not empty, use only this subset of nodes (minus the
3469
      offline ones)
3470
  @param cl: if not None, luxi client to use
3471
  @type nowarn: boolean
3472
  @param nowarn: by default, this function will output a note with the
3473
      offline nodes that are skipped; if this parameter is True the
3474
      note is not displayed
3475
  @type secondary_ips: boolean
3476
  @param secondary_ips: if True, return the secondary IPs instead of the
3477
      names, useful for doing network traffic over the replication interface
3478
      (if any)
3479
  @type filter_master: boolean
3480
  @param filter_master: if True, do not return the master node in the list
3481
      (useful in coordination with secondary_ips where we cannot check our
3482
      node name against the list)
3483
  @type nodegroup: string
3484
  @param nodegroup: If set, only return nodes in this node group
3485

3486
  """
3487
  if cl is None:
3488
    cl = GetClient(query=True)
3489

    
3490
  qfilter = []
3491

    
3492
  if nodes:
3493
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3494

    
3495
  if nodegroup is not None:
3496
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3497
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3498

    
3499
  if filter_master:
3500
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3501

    
3502
  if qfilter:
3503
    if len(qfilter) > 1:
3504
      final_filter = [qlang.OP_AND] + qfilter
3505
    else:
3506
      assert len(qfilter) == 1
3507
      final_filter = qfilter[0]
3508
  else:
3509
    final_filter = None
3510

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

    
3513
  def _IsOffline(row):
3514
    (_, (_, offline), _) = row
3515
    return offline
3516

    
3517
  def _GetName(row):
3518
    ((_, name), _, _) = row
3519
    return name
3520

    
3521
  def _GetSip(row):
3522
    (_, _, (_, sip)) = row
3523
    return sip
3524

    
3525
  (offline, online) = compat.partition(result.data, _IsOffline)
3526

    
3527
  if offline and not nowarn:
3528
    ToStderr("Note: skipping offline node(s): %s" %
3529
             utils.CommaJoin(map(_GetName, offline)))
3530

    
3531
  if secondary_ips:
3532
    fn = _GetSip
3533
  else:
3534
    fn = _GetName
3535

    
3536
  return map(fn, online)
3537

    
3538

    
3539
def GetNodesSshPorts(nodes, cl):
3540
  """Retrieves SSH ports of given nodes.
3541

3542
  @param nodes: the names of nodes
3543
  @type nodes: a list of strings
3544
  @param cl: a client to use for the query
3545
  @type cl: L{Client}
3546
  @return: the list of SSH ports corresponding to the nodes
3547
  @rtype: a list of tuples
3548
  """
3549
  return map(lambda t: t[0],
3550
             cl.QueryNodes(names=nodes,
3551
                           fields=["ndp/ssh_port"],
3552
                           use_locking=False))
3553

    
3554

    
3555
def _ToStream(stream, txt, *args):
3556
  """Write a message to a stream, bypassing the logging system
3557

3558
  @type stream: file object
3559
  @param stream: the file to which we should write
3560
  @type txt: str
3561
  @param txt: the message
3562

3563
  """
3564
  try:
3565
    if args:
3566
      args = tuple(args)
3567
      stream.write(txt % args)
3568
    else:
3569
      stream.write(txt)
3570
    stream.write("\n")
3571
    stream.flush()
3572
  except IOError, err:
3573
    if err.errno == errno.EPIPE:
3574
      # our terminal went away, we'll exit
3575
      sys.exit(constants.EXIT_FAILURE)
3576
    else:
3577
      raise
3578

    
3579

    
3580
def ToStdout(txt, *args):
3581
  """Write a message to stdout only, bypassing the logging system
3582

3583
  This is just a wrapper over _ToStream.
3584

3585
  @type txt: str
3586
  @param txt: the message
3587

3588
  """
3589
  _ToStream(sys.stdout, txt, *args)
3590

    
3591

    
3592
def ToStderr(txt, *args):
3593
  """Write a message to stderr only, bypassing the logging system
3594

3595
  This is just a wrapper over _ToStream.
3596

3597
  @type txt: str
3598
  @param txt: the message
3599

3600
  """
3601
  _ToStream(sys.stderr, txt, *args)
3602

    
3603

    
3604
class JobExecutor(object):
3605
  """Class which manages the submission and execution of multiple jobs.
3606

3607
  Note that instances of this class should not be reused between
3608
  GetResults() calls.
3609

3610
  """
3611
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3612
    self.queue = []
3613
    if cl is None:
3614
      cl = GetClient()
3615
    self.cl = cl
3616
    self.verbose = verbose
3617
    self.jobs = []
3618
    self.opts = opts
3619
    self.feedback_fn = feedback_fn
3620
    self._counter = itertools.count()
3621

    
3622
  @staticmethod
3623
  def _IfName(name, fmt):
3624
    """Helper function for formatting name.
3625

3626
    """
3627
    if name:
3628
      return fmt % name
3629

    
3630
    return ""
3631

    
3632
  def QueueJob(self, name, *ops):
3633
    """Record a job for later submit.
3634

3635
    @type name: string
3636
    @param name: a description of the job, will be used in WaitJobSet
3637

3638
    """
3639
    SetGenericOpcodeOpts(ops, self.opts)
3640
    self.queue.append((self._counter.next(), name, ops))
3641

    
3642
  def AddJobId(self, name, status, job_id):
3643
    """Adds a job ID to the internal queue.
3644

3645
    """
3646
    self.jobs.append((self._counter.next(), status, job_id, name))
3647

    
3648
  def SubmitPending(self, each=False):
3649
    """Submit all pending jobs.
3650

3651
    """
3652
    if each:
3653
      results = []
3654
      for (_, _, ops) in self.queue:
3655
        # SubmitJob will remove the success status, but raise an exception if
3656
        # the submission fails, so we'll notice that anyway.
3657
        results.append([True, self.cl.SubmitJob(ops)[0]])
3658
    else:
3659
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3660
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3661
      self.jobs.append((idx, status, data, name))
3662

    
3663
  def _ChooseJob(self):
3664
    """Choose a non-waiting/queued job to poll next.
3665

3666
    """
3667
    assert self.jobs, "_ChooseJob called with empty job list"
3668

    
3669
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3670
                               ["status"])
3671
    assert result
3672

    
3673
    for job_data, status in zip(self.jobs, result):
3674
      if (isinstance(status, list) and status and
3675
          status[0] in (constants.JOB_STATUS_QUEUED,
3676
                        constants.JOB_STATUS_WAITING,
3677
                        constants.JOB_STATUS_CANCELING)):
3678
        # job is still present and waiting
3679
        continue
3680
      # good candidate found (either running job or lost job)
3681
      self.jobs.remove(job_data)
3682
      return job_data
3683

    
3684
    # no job found
3685
    return self.jobs.pop(0)
3686

    
3687
  def GetResults(self):
3688
    """Wait for and return the results of all jobs.
3689

3690
    @rtype: list
3691
    @return: list of tuples (success, job results), in the same order
3692
        as the submitted jobs; if a job has failed, instead of the result
3693
        there will be the error message
3694

3695
    """
3696
    if not self.jobs:
3697
      self.SubmitPending()
3698
    results = []
3699
    if self.verbose:
3700
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3701
      if ok_jobs:
3702
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3703

    
3704
    # first, remove any non-submitted jobs
3705
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3706
    for idx, _, jid, name in failures:
3707
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3708
      results.append((idx, False, jid))
3709

    
3710
    while self.jobs:
3711
      (idx, _, jid, name) = self._ChooseJob()
3712
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3713
      try:
3714
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3715
        success = True
3716
      except errors.JobLost, err:
3717
        _, job_result = FormatError(err)
3718
        ToStderr("Job %s%s has been archived, cannot check its result",
3719
                 jid, self._IfName(name, " for %s"))
3720
        success = False
3721
      except (errors.GenericError, rpcerr.ProtocolError), err:
3722
        _, job_result = FormatError(err)
3723
        success = False
3724
        # the error message will always be shown, verbose or not
3725
        ToStderr("Job %s%s has failed: %s",
3726
                 jid, self._IfName(name, " for %s"), job_result)
3727

    
3728
      results.append((idx, success, job_result))
3729

    
3730
    # sort based on the index, then drop it
3731
    results.sort()
3732
    results = [i[1:] for i in results]
3733

    
3734
    return results
3735

    
3736
  def WaitOrShow(self, wait):
3737
    """Wait for job results or only print the job IDs.
3738

3739
    @type wait: boolean
3740
    @param wait: whether to wait or not
3741

3742
    """
3743
    if wait:
3744
      return self.GetResults()
3745
    else:
3746
      if not self.jobs:
3747
        self.SubmitPending()
3748
      for _, status, result, name in self.jobs:
3749
        if status:
3750
          ToStdout("%s: %s", result, name)
3751
        else:
3752
          ToStderr("Failure for %s: %s", name, result)
3753
      return [row[1:3] for row in self.jobs]
3754

    
3755

    
3756
def FormatParamsDictInfo(param_dict, actual):
3757
  """Formats a parameter dictionary.
3758

3759
  @type param_dict: dict
3760
  @param param_dict: the own parameters
3761
  @type actual: dict
3762
  @param actual: the current parameter set (including defaults)
3763
  @rtype: dict
3764
  @return: dictionary where the value of each parameter is either a fully
3765
      formatted string or a dictionary containing formatted strings
3766

3767
  """
3768
  ret = {}
3769
  for (key, data) in actual.items():
3770
    if isinstance(data, dict) and data:
3771
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3772
    else:
3773
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3774
  return ret
3775

    
3776

    
3777
def _FormatListInfoDefault(data, def_data):
3778
  if data is not None:
3779
    ret = utils.CommaJoin(data)
3780
  else:
3781
    ret = "default (%s)" % utils.CommaJoin(def_data)
3782
  return ret
3783

    
3784

    
3785
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3786
  """Formats an instance policy.
3787

3788
  @type custom_ipolicy: dict
3789
  @param custom_ipolicy: own policy
3790
  @type eff_ipolicy: dict
3791
  @param eff_ipolicy: effective policy (including defaults); ignored for
3792
      cluster
3793
  @type iscluster: bool
3794
  @param iscluster: the policy is at cluster level
3795
  @rtype: list of pairs
3796
  @return: formatted data, suitable for L{PrintGenericInfo}
3797

3798
  """
3799
  if iscluster:
3800
    eff_ipolicy = custom_ipolicy
3801

    
3802
  minmax_out = []
3803
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3804
  if custom_minmax:
3805
    for (k, minmax) in enumerate(custom_minmax):
3806
      minmax_out.append([
3807
        ("%s/%s" % (key, k),
3808
         FormatParamsDictInfo(minmax[key], minmax[key]))
3809
        for key in constants.ISPECS_MINMAX_KEYS
3810
        ])
3811
  else:
3812
    for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]):
3813
      minmax_out.append([
3814
        ("%s/%s" % (key, k),
3815
         FormatParamsDictInfo({}, minmax[key]))
3816
        for key in constants.ISPECS_MINMAX_KEYS
3817
        ])
3818
  ret = [("bounds specs", minmax_out)]
3819

    
3820
  if iscluster:
3821
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3822
    ret.append(
3823
      (constants.ISPECS_STD,
3824
       FormatParamsDictInfo(stdspecs, stdspecs))
3825
      )
3826

    
3827
  ret.append(
3828
    ("allowed disk templates",
3829
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3830
                            eff_ipolicy[constants.IPOLICY_DTS]))
3831
    )
3832
  ret.extend([
3833
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3834
    for key in constants.IPOLICY_PARAMETERS
3835
    ])
3836
  return ret
3837

    
3838

    
3839
def _PrintSpecsParameters(buf, specs):
3840
  values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items()))
3841
  buf.write(",".join(values))
3842

    
3843

    
3844
def PrintIPolicyCommand(buf, ipolicy, isgroup):
3845
  """Print the command option used to generate the given instance policy.
3846

3847
  Currently only the parts dealing with specs are supported.
3848

3849
  @type buf: StringIO
3850
  @param buf: stream to write into
3851
  @type ipolicy: dict
3852
  @param ipolicy: instance policy
3853
  @type isgroup: bool
3854
  @param isgroup: whether the policy is at group level
3855

3856
  """
3857
  if not isgroup:
3858
    stdspecs = ipolicy.get("std")
3859
    if stdspecs:
3860
      buf.write(" %s " % IPOLICY_STD_SPECS_STR)
3861
      _PrintSpecsParameters(buf, stdspecs)
3862
  minmaxes = ipolicy.get("minmax", [])
3863
  first = True
3864
  for minmax in minmaxes:
3865
    minspecs = minmax.get("min")
3866
    maxspecs = minmax.get("max")
3867
    if minspecs and maxspecs:
3868
      if first:
3869
        buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
3870
        first = False
3871
      else:
3872
        buf.write("//")
3873
      buf.write("min:")
3874
      _PrintSpecsParameters(buf, minspecs)
3875
      buf.write("/max:")
3876
      _PrintSpecsParameters(buf, maxspecs)
3877

    
3878

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

3882
  This function is used to request confirmation for doing an operation
3883
  on a given list of list_type.
3884

3885
  @type names: list
3886
  @param names: the list of names that we display when
3887
      we ask for confirmation
3888
  @type list_type: str
3889
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3890
  @type text: str
3891
  @param text: the operation that the user should confirm
3892
  @rtype: boolean
3893
  @return: True or False depending on user's confirmation.
3894

3895
  """
3896
  count = len(names)
3897
  msg = ("The %s will operate on %d %s.\n%s"
3898
         "Do you want to continue?" % (text, count, list_type, extra))
3899
  affected = (("\nAffected %s:\n" % list_type) +
3900
              "\n".join(["  %s" % name for name in names]))
3901

    
3902
  choices = [("y", True, "Yes, execute the %s" % text),
3903
             ("n", False, "No, abort the %s" % text)]
3904

    
3905
  if count > 20:
3906
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3907
    question = msg
3908
  else:
3909
    question = msg + affected
3910

    
3911
  choice = AskUser(question, choices)
3912
  if choice == "v":
3913
    choices.pop(1)
3914
    choice = AskUser(msg + affected, choices)
3915
  return choice
3916

    
3917

    
3918
def _MaybeParseUnit(elements):
3919
  """Parses and returns an array of potential values with units.
3920

3921
  """
3922
  parsed = {}
3923
  for k, v in elements.items():
3924
    if v == constants.VALUE_DEFAULT:
3925
      parsed[k] = v
3926
    else:
3927
      parsed[k] = utils.ParseUnit(v)
3928
  return parsed
3929

    
3930

    
3931
def _InitISpecsFromSplitOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
3932
                             ispecs_disk_count, ispecs_disk_size,
3933
                             ispecs_nic_count, group_ipolicy, fill_all):
3934
  try:
3935
    if ispecs_mem_size:
3936
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
3937
    if ispecs_disk_size:
3938
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
3939
  except (TypeError, ValueError, errors.UnitParseError), err:
3940
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
3941
                               " in policy: %s" %
3942
                               (ispecs_disk_size, ispecs_mem_size, err),
3943
                               errors.ECODE_INVAL)
3944

    
3945
  # prepare ipolicy dict
3946
  ispecs_transposed = {
3947
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
3948
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
3949
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
3950
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
3951
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
3952
    }
3953

    
3954
  # first, check that the values given are correct
3955
  if group_ipolicy:
3956
    forced_type = TISPECS_GROUP_TYPES
3957
  else:
3958
    forced_type = TISPECS_CLUSTER_TYPES
3959
  for specs in ispecs_transposed.values():
3960
    assert type(specs) is dict
3961
    utils.ForceDictType(specs, forced_type)
3962

    
3963
  # then transpose
3964
  ispecs = {
3965
    constants.ISPECS_MIN: {},
3966
    constants.ISPECS_MAX: {},
3967
    constants.ISPECS_STD: {},
3968
    }
3969
  for (name, specs) in ispecs_transposed.iteritems():
3970
    assert name in constants.ISPECS_PARAMETERS
3971
    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
3972
      assert key in ispecs
3973
      ispecs[key][name] = val
3974
  minmax_out = {}
3975
  for key in constants.ISPECS_MINMAX_KEYS:
3976
    if fill_all:
3977
      minmax_out[key] = \
3978
        objects.FillDict(constants.ISPECS_MINMAX_DEFAULTS[key], ispecs[key])
3979
    else:
3980
      minmax_out[key] = ispecs[key]
3981
  ipolicy[constants.ISPECS_MINMAX] = [minmax_out]
3982
  if fill_all:
3983
    ipolicy[constants.ISPECS_STD] = \
3984
        objects.FillDict(constants.IPOLICY_DEFAULTS[constants.ISPECS_STD],
3985
                         ispecs[constants.ISPECS_STD])
3986
  else:
3987
    ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
3988

    
3989

    
3990
def _ParseSpecUnit(spec, keyname):
3991
  ret = spec.copy()
3992
  for k in [constants.ISPEC_DISK_SIZE, constants.ISPEC_MEM_SIZE]:
3993
    if k in ret:
3994
      try:
3995
        ret[k] = utils.ParseUnit(ret[k])
3996
      except (TypeError, ValueError, errors.UnitParseError), err:
3997
        raise errors.OpPrereqError(("Invalid parameter %s (%s) in %s instance"
3998
                                    " specs: %s" % (k, ret[k], keyname, err)),
3999
                                   errors.ECODE_INVAL)
4000
  return ret
4001

    
4002

    
4003
def _ParseISpec(spec, keyname, required):
4004
  ret = _ParseSpecUnit(spec, keyname)
4005
  utils.ForceDictType(ret, constants.ISPECS_PARAMETER_TYPES)
4006
  missing = constants.ISPECS_PARAMETERS - frozenset(ret.keys())
4007
  if required and missing:
4008
    raise errors.OpPrereqError("Missing parameters in ipolicy spec %s: %s" %
4009
                               (keyname, utils.CommaJoin(missing)),
4010
                               errors.ECODE_INVAL)
4011
  return ret
4012

    
4013

    
4014
def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
4015
  ret = None
4016
  if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and
4017
      len(minmax_ispecs[0]) == 1):
4018
    for (key, spec) in minmax_ispecs[0].items():
4019
      # This loop is executed exactly once
4020
      if key in allowed_values and not spec:
4021
        ret = key
4022
  return ret
4023

    
4024

    
4025
def _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4026
                            group_ipolicy, allowed_values):
4027
  found_allowed = _GetISpecsInAllowedValues(minmax_ispecs, allowed_values)
4028
  if found_allowed is not None:
4029
    ipolicy_out[constants.ISPECS_MINMAX] = found_allowed
4030
  elif minmax_ispecs is not None:
4031
    minmax_out = []
4032
    for mmpair in minmax_ispecs:
4033
      mmpair_out = {}
4034
      for (key, spec) in mmpair.items():
4035
        if key not in constants.ISPECS_MINMAX_KEYS:
4036
          msg = "Invalid key in bounds instance specifications: %s" % key
4037
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
4038
        mmpair_out[key] = _ParseISpec(spec, key, True)
4039
      minmax_out.append(mmpair_out)
4040
    ipolicy_out[constants.ISPECS_MINMAX] = minmax_out
4041
  if std_ispecs is not None:
4042
    assert not group_ipolicy # This is not an option for gnt-group
4043
    ipolicy_out[constants.ISPECS_STD] = _ParseISpec(std_ispecs, "std", False)
4044

    
4045

    
4046
def CreateIPolicyFromOpts(ispecs_mem_size=None,
4047
                          ispecs_cpu_count=None,
4048
                          ispecs_disk_count=None,
4049
                          ispecs_disk_size=None,
4050
                          ispecs_nic_count=None,
4051
                          minmax_ispecs=None,
4052
                          std_ispecs=None,
4053
                          ipolicy_disk_templates=None,
4054
                          ipolicy_vcpu_ratio=None,
4055
                          ipolicy_spindle_ratio=None,
4056
                          group_ipolicy=False,
4057
                          allowed_values=None,
4058
                          fill_all=False):
4059
  """Creation of instance policy based on command line options.
4060

4061
  @param fill_all: whether for cluster policies we should ensure that
4062
    all values are filled
4063

4064
  """
4065
  assert not (fill_all and allowed_values)
4066

    
4067
  split_specs = (ispecs_mem_size or ispecs_cpu_count or ispecs_disk_count or
4068
                 ispecs_disk_size or ispecs_nic_count)
4069
  if (split_specs and (minmax_ispecs is not None or std_ispecs is not None)):
4070
    raise errors.OpPrereqError("A --specs-xxx option cannot be specified"
4071
                               " together with any --ipolicy-xxx-specs option",
4072
                               errors.ECODE_INVAL)
4073

    
4074
  ipolicy_out = objects.MakeEmptyIPolicy()
4075
  if split_specs:
4076
    assert fill_all
4077
    _InitISpecsFromSplitOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
4078
                             ispecs_disk_count, ispecs_disk_size,
4079
                             ispecs_nic_count, group_ipolicy, fill_all)
4080
  elif (minmax_ispecs is not None or std_ispecs is not None):
4081
    _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4082
                            group_ipolicy, allowed_values)
4083

    
4084
  if ipolicy_disk_templates is not None:
4085
    if allowed_values and ipolicy_disk_templates in allowed_values:
4086
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
4087
    else:
4088
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
4089
  if ipolicy_vcpu_ratio is not None:
4090
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
4091
  if ipolicy_spindle_ratio is not None:
4092
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
4093

    
4094
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
4095

    
4096
  if not group_ipolicy and fill_all:
4097
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
4098

    
4099
  return ipolicy_out
4100

    
4101

    
4102
def _SerializeGenericInfo(buf, data, level, afterkey=False):
4103
  """Formatting core of L{PrintGenericInfo}.
4104

4105
  @param buf: (string) stream to accumulate the result into
4106
  @param data: data to format
4107
  @type level: int
4108
  @param level: depth in the data hierarchy, used for indenting
4109
  @type afterkey: bool
4110
  @param afterkey: True when we are in the middle of a line after a key (used
4111
      to properly add newlines or indentation)
4112

4113
  """
4114
  baseind = "  "
4115
  if isinstance(data, dict):
4116
    if not data:
4117
      buf.write("\n")
4118
    else:
4119
      if afterkey:
4120
        buf.write("\n")
4121
        doindent = True
4122
      else:
4123
        doindent = False
4124
      for key in sorted(data):
4125
        if doindent:
4126
          buf.write(baseind * level)
4127
        else:
4128
          doindent = True
4129
        buf.write(key)
4130
        buf.write(": ")
4131
        _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True)
4132
  elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple):
4133
    # list of tuples (an ordered dictionary)
4134
    if afterkey:
4135
      buf.write("\n")
4136
      doindent = True
4137
    else:
4138
      doindent = False
4139
    for (key, val) in data:
4140
      if doindent:
4141
        buf.write(baseind * level)
4142
      else:
4143
        doindent = True
4144
      buf.write(key)
4145
      buf.write(": ")
4146
      _SerializeGenericInfo(buf, val, level + 1, afterkey=True)
4147
  elif isinstance(data, list):
4148
    if not data:
4149
      buf.write("\n")
4150
    else:
4151
      if afterkey:
4152
        buf.write("\n")
4153
        doindent = True
4154
      else:
4155
        doindent = False
4156
      for item in data:
4157
        if doindent:
4158
          buf.write(baseind * level)
4159
        else:
4160
          doindent = True
4161
        buf.write("-")
4162
        buf.write(baseind[1:])
4163
        _SerializeGenericInfo(buf, item, level + 1)
4164
  else:
4165
    # This branch should be only taken for strings, but it's practically
4166
    # impossible to guarantee that no other types are produced somewhere
4167
    buf.write(str(data))
4168
    buf.write("\n")
4169

    
4170

    
4171
def PrintGenericInfo(data):
4172
  """Print information formatted according to the hierarchy.
4173

4174
  The output is a valid YAML string.
4175

4176
  @param data: the data to print. It's a hierarchical structure whose elements
4177
      can be:
4178
        - dictionaries, where keys are strings and values are of any of the
4179
          types listed here
4180
        - lists of pairs (key, value), where key is a string and value is of
4181
          any of the types listed here; it's a way to encode ordered
4182
          dictionaries
4183
        - lists of any of the types listed here
4184
        - strings
4185

4186
  """
4187
  buf = StringIO()
4188
  _SerializeGenericInfo(buf, data, 0)
4189
  ToStdout(buf.getvalue().rstrip("\n"))