Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 651ce6a3

History | View | Annotate | Download (137.7 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
from ganeti import luxi
40
from ganeti import ssconf
41
from ganeti import rpc
42
from ganeti import ssh
43
from ganeti import compat
44
from ganeti import netutils
45
from ganeti import qlang
46
from ganeti import objects
47
from ganeti import pathutils
48

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

    
52

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

    
297
NO_PREFIX = "no_"
298
UN_PREFIX = "-"
299

    
300
#: Priorities (sorted)
301
_PRIORITY_NAMES = [
302
  ("low", constants.OP_PRIO_LOW),
303
  ("normal", constants.OP_PRIO_NORMAL),
304
  ("high", constants.OP_PRIO_HIGH),
305
  ]
306

    
307
#: Priority dictionary for easier lookup
308
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
309
# we migrate to Python 2.6
310
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
311

    
312
# Query result status for clients
313
(QR_NORMAL,
314
 QR_UNKNOWN,
315
 QR_INCOMPLETE) = range(3)
316

    
317
#: Maximum batch size for ChooseJob
318
_CHOOSE_BATCH = 25
319

    
320

    
321
# constants used to create InstancePolicy dictionary
322
TISPECS_GROUP_TYPES = {
323
  constants.ISPECS_MIN: constants.VTYPE_INT,
324
  constants.ISPECS_MAX: constants.VTYPE_INT,
325
  }
326

    
327
TISPECS_CLUSTER_TYPES = {
328
  constants.ISPECS_MIN: constants.VTYPE_INT,
329
  constants.ISPECS_MAX: constants.VTYPE_INT,
330
  constants.ISPECS_STD: constants.VTYPE_INT,
331
  }
332

    
333
#: User-friendly names for query2 field types
334
_QFT_NAMES = {
335
  constants.QFT_UNKNOWN: "Unknown",
336
  constants.QFT_TEXT: "Text",
337
  constants.QFT_BOOL: "Boolean",
338
  constants.QFT_NUMBER: "Number",
339
  constants.QFT_UNIT: "Storage size",
340
  constants.QFT_TIMESTAMP: "Timestamp",
341
  constants.QFT_OTHER: "Custom",
342
  }
343

    
344

    
345
class _Argument:
346
  def __init__(self, min=0, max=None): # pylint: disable=W0622
347
    self.min = min
348
    self.max = max
349

    
350
  def __repr__(self):
351
    return ("<%s min=%s max=%s>" %
352
            (self.__class__.__name__, self.min, self.max))
353

    
354

    
355
class ArgSuggest(_Argument):
356
  """Suggesting argument.
357

358
  Value can be any of the ones passed to the constructor.
359

360
  """
361
  # pylint: disable=W0622
362
  def __init__(self, min=0, max=None, choices=None):
363
    _Argument.__init__(self, min=min, max=max)
364
    self.choices = choices
365

    
366
  def __repr__(self):
367
    return ("<%s min=%s max=%s choices=%r>" %
368
            (self.__class__.__name__, self.min, self.max, self.choices))
369

    
370

    
371
class ArgChoice(ArgSuggest):
372
  """Choice argument.
373

374
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
375
  but value must be one of the choices.
376

377
  """
378

    
379

    
380
class ArgUnknown(_Argument):
381
  """Unknown argument to program (e.g. determined at runtime).
382

383
  """
384

    
385

    
386
class ArgInstance(_Argument):
387
  """Instances argument.
388

389
  """
390

    
391

    
392
class ArgNode(_Argument):
393
  """Node argument.
394

395
  """
396

    
397

    
398
class ArgNetwork(_Argument):
399
  """Network argument.
400

401
  """
402

    
403

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

407
  """
408

    
409

    
410
class ArgJobId(_Argument):
411
  """Job ID argument.
412

413
  """
414

    
415

    
416
class ArgFile(_Argument):
417
  """File path argument.
418

419
  """
420

    
421

    
422
class ArgCommand(_Argument):
423
  """Command argument.
424

425
  """
426

    
427

    
428
class ArgHost(_Argument):
429
  """Host argument.
430

431
  """
432

    
433

    
434
class ArgOs(_Argument):
435
  """OS argument.
436

437
  """
438

    
439

    
440
class ArgExtStorage(_Argument):
441
  """ExtStorage argument.
442

443
  """
444

    
445

    
446
ARGS_NONE = []
447
ARGS_MANY_INSTANCES = [ArgInstance()]
448
ARGS_MANY_NETWORKS = [ArgNetwork()]
449
ARGS_MANY_NODES = [ArgNode()]
450
ARGS_MANY_GROUPS = [ArgGroup()]
451
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
452
ARGS_ONE_NETWORK = [ArgNetwork(min=1, max=1)]
453
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
454
# TODO
455
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
456
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
457

    
458

    
459
def _ExtractTagsObject(opts, args):
460
  """Extract the tag type object.
461

462
  Note that this function will modify its args parameter.
463

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

    
483

    
484
def _ExtendTags(opts, args):
485
  """Extend the args if a source file has been given.
486

487
  This function will extend the tags with the contents of the file
488
  passed in the 'tags_source' attribute of the opts parameter. A file
489
  named '-' will be replaced by stdin.
490

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

    
512

    
513
def ListTags(opts, args):
514
  """List the tags on a given object.
515

516
  This is a generic implementation that knows how to deal with all
517
  three cases of tag objects (cluster, node, instance). The opts
518
  argument is expected to contain a tag_type field denoting what
519
  object type we work on.
520

521
  """
522
  kind, name = _ExtractTagsObject(opts, args)
523
  cl = GetClient(query=True)
524
  result = cl.QueryTags(kind, name)
525
  result = list(result)
526
  result.sort()
527
  for tag in result:
528
    ToStdout(tag)
529

    
530

    
531
def AddTags(opts, args):
532
  """Add tags on a given object.
533

534
  This is a generic implementation that knows how to deal with all
535
  three cases of tag objects (cluster, node, instance). The opts
536
  argument is expected to contain a tag_type field denoting what
537
  object type we work on.
538

539
  """
540
  kind, name = _ExtractTagsObject(opts, args)
541
  _ExtendTags(opts, args)
542
  if not args:
543
    raise errors.OpPrereqError("No tags to be added", errors.ECODE_INVAL)
544
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
545
  SubmitOrSend(op, opts)
546

    
547

    
548
def RemoveTags(opts, args):
549
  """Remove tags from a given object.
550

551
  This is a generic implementation that knows how to deal with all
552
  three cases of tag objects (cluster, node, instance). The opts
553
  argument is expected to contain a tag_type field denoting what
554
  object type we work on.
555

556
  """
557
  kind, name = _ExtractTagsObject(opts, args)
558
  _ExtendTags(opts, args)
559
  if not args:
560
    raise errors.OpPrereqError("No tags to be removed", errors.ECODE_INVAL)
561
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
562
  SubmitOrSend(op, opts)
563

    
564

    
565
def check_unit(option, opt, value): # pylint: disable=W0613
566
  """OptParsers custom converter for units.
567

568
  """
569
  try:
570
    return utils.ParseUnit(value)
571
  except errors.UnitParseError, err:
572
    raise OptionValueError("option %s: %s" % (opt, err))
573

    
574

    
575
def _SplitKeyVal(opt, data, parse_prefixes):
576
  """Convert a KeyVal string into a dict.
577

578
  This function will convert a key=val[,...] string into a dict. Empty
579
  values will be converted specially: keys which have the prefix 'no_'
580
  will have the value=False and the prefix stripped, keys with the prefix
581
  "-" will have value=None and the prefix stripped, and the others will
582
  have value=True.
583

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

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

    
617

    
618
def _SplitIdentKeyVal(opt, value, parse_prefixes):
619
  """Helper function to parse "ident:key=val,key=val" options.
620

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

632
  """
633
  if ":" not in value:
634
    ident, rest = value, ""
635
  else:
636
    ident, rest = value.split(":", 1)
637

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

    
654

    
655
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
656
  """Custom parser for ident:key=val,key=val options.
657

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

661
  """
662
  return _SplitIdentKeyVal(opt, value, True)
663

    
664

    
665
def check_key_val(option, opt, value):  # pylint: disable=W0613
666
  """Custom parser class for key=val,key=val options.
667

668
  This will store the parsed values as a dict {key: val}.
669

670
  """
671
  return _SplitKeyVal(opt, value, True)
672

    
673

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

    
687

    
688
def check_multilist_ident_key_val(_, opt, value):
689
  """Custom parser for "ident:key=val,key=val/ident:key=val//ident:.." options.
690

691
  @rtype: list of dictionary
692
  @return: [{ident: {key: val, key: val}, ident: {key: val}}, {ident:..}]
693

694
  """
695
  retval = []
696
  for line in value.split("//"):
697
    retval.append(_SplitListKeyVal(opt, line))
698
  return retval
699

    
700

    
701
def check_bool(option, opt, value): # pylint: disable=W0613
702
  """Custom parser for yes/no options.
703

704
  This will store the parsed value as either True or False.
705

706
  """
707
  value = value.lower()
708
  if value == constants.VALUE_FALSE or value == "no":
709
    return False
710
  elif value == constants.VALUE_TRUE or value == "yes":
711
    return True
712
  else:
713
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
714

    
715

    
716
def check_list(option, opt, value): # pylint: disable=W0613
717
  """Custom parser for comma-separated lists.
718

719
  """
720
  # we have to make this explicit check since "".split(",") is [""],
721
  # not an empty list :(
722
  if not value:
723
    return []
724
  else:
725
    return utils.UnescapeAndSplit(value)
726

    
727

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

731
  """
732
  value = value.lower()
733

    
734
  if value == constants.VALUE_DEFAULT:
735
    return value
736
  else:
737
    return float(value)
738

    
739

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

    
752
OPT_COMPL_ALL = compat.UniqueFrozenset([
753
  OPT_COMPL_MANY_NODES,
754
  OPT_COMPL_ONE_NODE,
755
  OPT_COMPL_ONE_INSTANCE,
756
  OPT_COMPL_ONE_OS,
757
  OPT_COMPL_ONE_EXTSTORAGE,
758
  OPT_COMPL_ONE_IALLOCATOR,
759
  OPT_COMPL_ONE_NETWORK,
760
  OPT_COMPL_INST_ADD_NODES,
761
  OPT_COMPL_ONE_NODEGROUP,
762
  ])
763

    
764

    
765
class CliOption(Option):
766
  """Custom option class for optparse.
767

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

    
790

    
791
# optparse.py sets make_option, so we do it for our own option class, too
792
cli_option = CliOption
793

    
794

    
795
_YORNO = "yes|no"
796

    
797
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
798
                       help="Increase debugging level")
799

    
800
NOHDR_OPT = cli_option("--no-headers", default=False,
801
                       action="store_true", dest="no_headers",
802
                       help="Don't display column headers")
803

    
804
SEP_OPT = cli_option("--separator", default=None,
805
                     action="store", dest="separator",
806
                     help=("Separator between output fields"
807
                           " (defaults to one space)"))
808

    
809
USEUNITS_OPT = cli_option("--units", default=None,
810
                          dest="units", choices=("h", "m", "g", "t"),
811
                          help="Specify units for output (one of h/m/g/t)")
812

    
813
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
814
                        type="string", metavar="FIELDS",
815
                        help="Comma separated list of output fields")
816

    
817
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
818
                       default=False, help="Force the operation")
819

    
820
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
821
                         default=False, help="Do not require confirmation")
822

    
823
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
824
                                  action="store_true", default=False,
825
                                  help=("Ignore offline nodes and do as much"
826
                                        " as possible"))
827

    
828
TAG_ADD_OPT = cli_option("--tags", dest="tags",
829
                         default=None, help="Comma-separated list of instance"
830
                                            " tags")
831

    
832
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
833
                         default=None, help="File with tag names")
834

    
835
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
836
                        default=False, action="store_true",
837
                        help=("Submit the job and return the job ID, but"
838
                              " don't wait for the job to finish"))
839

    
840
PRINT_JOBID_OPT = cli_option("--print-jobid", dest="print_jobid",
841
                             default=False, action="store_true",
842
                             help=("Additionally print the job as first line"
843
                                   " on stdout (for scripting)."))
844

    
845
SYNC_OPT = cli_option("--sync", dest="do_locking",
846
                      default=False, action="store_true",
847
                      help=("Grab locks while doing the queries"
848
                            " in order to ensure more consistent results"))
849

    
850
DRY_RUN_OPT = cli_option("--dry-run", default=False,
851
                         action="store_true",
852
                         help=("Do not execute the operation, just run the"
853
                               " check steps and verify if it could be"
854
                               " executed"))
855

    
856
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
857
                         action="store_true",
858
                         help="Increase the verbosity of the operation")
859

    
860
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
861
                              action="store_true", dest="simulate_errors",
862
                              help="Debugging option that makes the operation"
863
                              " treat most runtime checks as failed")
864

    
865
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
866
                        default=True, action="store_false",
867
                        help="Don't wait for sync (DANGEROUS!)")
868

    
869
WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync",
870
                        default=False, action="store_true",
871
                        help="Wait for disks to sync")
872

    
873
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
874
                             action="store_true", default=False,
875
                             help="Enable offline instance")
876

    
877
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
878
                              action="store_true", default=False,
879
                              help="Disable down instance")
880

    
881
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
882
                               help=("Custom disk setup (%s)" %
883
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
884
                               default=None, metavar="TEMPL",
885
                               choices=list(constants.DISK_TEMPLATES))
886

    
887
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
888
                        help="Do not create any network cards for"
889
                        " the instance")
890

    
891
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
892
                               help="Relative path under default cluster-wide"
893
                               " file storage dir to store file-based disks",
894
                               default=None, metavar="<DIR>")
895

    
896
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
897
                                  help="Driver to use for image files",
898
                                  default=None, metavar="<DRIVER>",
899
                                  choices=list(constants.FILE_DRIVER))
900

    
901
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
902
                            help="Select nodes for the instance automatically"
903
                            " using the <NAME> iallocator plugin",
904
                            default=None, type="string",
905
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
906

    
907
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
908
                                    metavar="<NAME>",
909
                                    help="Set the default instance"
910
                                    " allocator plugin",
911
                                    default=None, type="string",
912
                                    completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
913

    
914
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
915
                    metavar="<os>",
916
                    completion_suggest=OPT_COMPL_ONE_OS)
917

    
918
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
919
                          type="keyval", default={},
920
                          help="OS parameters")
921

    
922
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
923
                               action="store_true", default=False,
924
                               help="Force an unknown variant")
925

    
926
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
927
                            action="store_true", default=False,
928
                            help="Do not install the OS (will"
929
                            " enable no-start)")
930

    
931
NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes",
932
                                dest="allow_runtime_chgs",
933
                                default=True, action="store_false",
934
                                help="Don't allow runtime changes")
935

    
936
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
937
                         type="keyval", default={},
938
                         help="Backend parameters")
939

    
940
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
941
                        default={}, dest="hvparams",
942
                        help="Hypervisor parameters")
943

    
944
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
945
                             help="Disk template parameters, in the format"
946
                             " template:option=value,option=value,...",
947
                             type="identkeyval", action="append", default=[])
948

    
949
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
950
                                 type="keyval", default={},
951
                                 help="Memory size specs: list of key=value,"
952
                                " where key is one of min, max, std"
953
                                 " (in MB or using a unit)")
954

    
955
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
956
                                 type="keyval", default={},
957
                                 help="CPU count specs: list of key=value,"
958
                                 " where key is one of min, max, std")
959

    
960
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
961
                                  dest="ispecs_disk_count",
962
                                  type="keyval", default={},
963
                                  help="Disk count specs: list of key=value,"
964
                                  " where key is one of min, max, std")
965

    
966
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
967
                                 type="keyval", default={},
968
                                 help="Disk size specs: list of key=value,"
969
                                 " where key is one of min, max, std"
970
                                 " (in MB or using a unit)")
971

    
972
SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
973
                                 type="keyval", default={},
974
                                 help="NIC count specs: list of key=value,"
975
                                 " where key is one of min, max, std")
976

    
977
IPOLICY_BOUNDS_SPECS_STR = "--ipolicy-bounds-specs"
978
IPOLICY_BOUNDS_SPECS_OPT = cli_option(IPOLICY_BOUNDS_SPECS_STR,
979
                                      dest="ipolicy_bounds_specs",
980
                                      type="multilistidentkeyval", default=None,
981
                                      help="Complete instance specs limits")
982

    
983
IPOLICY_STD_SPECS_STR = "--ipolicy-std-specs"
984
IPOLICY_STD_SPECS_OPT = cli_option(IPOLICY_STD_SPECS_STR,
985
                                   dest="ipolicy_std_specs",
986
                                   type="keyval", default=None,
987
                                   help="Complte standard instance specs")
988

    
989
IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
990
                                    dest="ipolicy_disk_templates",
991
                                    type="list", default=None,
992
                                    help="Comma-separated list of"
993
                                    " enabled disk templates")
994

    
995
IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio",
996
                                 dest="ipolicy_vcpu_ratio",
997
                                 type="maybefloat", default=None,
998
                                 help="The maximum allowed vcpu-to-cpu ratio")
999

    
1000
IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio",
1001
                                   dest="ipolicy_spindle_ratio",
1002
                                   type="maybefloat", default=None,
1003
                                   help=("The maximum allowed instances to"
1004
                                         " spindle ratio"))
1005

    
1006
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
1007
                            help="Hypervisor and hypervisor options, in the"
1008
                            " format hypervisor:option=value,option=value,...",
1009
                            default=None, type="identkeyval")
1010

    
1011
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
1012
                        help="Hypervisor and hypervisor options, in the"
1013
                        " format hypervisor:option=value,option=value,...",
1014
                        default=[], action="append", type="identkeyval")
1015

    
1016
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
1017
                           action="store_false",
1018
                           help="Don't check that the instance's IP"
1019
                           " is alive")
1020

    
1021
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
1022
                             default=True, action="store_false",
1023
                             help="Don't check that the instance's name"
1024
                             " is resolvable")
1025

    
1026
NET_OPT = cli_option("--net",
1027
                     help="NIC parameters", default=[],
1028
                     dest="nics", action="append", type="identkeyval")
1029

    
1030
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
1031
                      dest="disks", action="append", type="identkeyval")
1032

    
1033
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
1034
                         help="Comma-separated list of disks"
1035
                         " indices to act on (e.g. 0,2) (optional,"
1036
                         " defaults to all disks)")
1037

    
1038
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
1039
                         help="Enforces a single-disk configuration using the"
1040
                         " given disk size, in MiB unless a suffix is used",
1041
                         default=None, type="unit", metavar="<size>")
1042

    
1043
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
1044
                                dest="ignore_consistency",
1045
                                action="store_true", default=False,
1046
                                help="Ignore the consistency of the disks on"
1047
                                " the secondary")
1048

    
1049
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
1050
                                dest="allow_failover",
1051
                                action="store_true", default=False,
1052
                                help="If migration is not possible fallback to"
1053
                                     " failover")
1054

    
1055
NONLIVE_OPT = cli_option("--non-live", dest="live",
1056
                         default=True, action="store_false",
1057
                         help="Do a non-live migration (this usually means"
1058
                         " freeze the instance, save the state, transfer and"
1059
                         " only then resume running on the secondary node)")
1060

    
1061
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
1062
                                default=None,
1063
                                choices=list(constants.HT_MIGRATION_MODES),
1064
                                help="Override default migration mode (choose"
1065
                                " either live or non-live")
1066

    
1067
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
1068
                                help="Target node and optional secondary node",
1069
                                metavar="<pnode>[:<snode>]",
1070
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
1071

    
1072
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
1073
                           action="append", metavar="<node>",
1074
                           help="Use only this node (can be used multiple"
1075
                           " times, if not given defaults to all nodes)",
1076
                           completion_suggest=OPT_COMPL_ONE_NODE)
1077

    
1078
NODEGROUP_OPT_NAME = "--node-group"
1079
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
1080
                           dest="nodegroup",
1081
                           help="Node group (name or uuid)",
1082
                           metavar="<nodegroup>",
1083
                           default=None, type="string",
1084
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1085

    
1086
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
1087
                             metavar="<node>",
1088
                             completion_suggest=OPT_COMPL_ONE_NODE)
1089

    
1090
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
1091
                         action="store_false",
1092
                         help="Don't start the instance after creation")
1093

    
1094
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
1095
                         action="store_true", default=False,
1096
                         help="Show command instead of executing it")
1097

    
1098
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
1099
                         default=False, action="store_true",
1100
                         help="Instead of performing the migration/failover,"
1101
                         " try to recover from a failed cleanup. This is safe"
1102
                         " to run even if the instance is healthy, but it"
1103
                         " will create extra replication traffic and "
1104
                         " disrupt briefly the replication (like during the"
1105
                         " migration/failover")
1106

    
1107
STATIC_OPT = cli_option("-s", "--static", dest="static",
1108
                        action="store_true", default=False,
1109
                        help="Only show configuration data, not runtime data")
1110

    
1111
ALL_OPT = cli_option("--all", dest="show_all",
1112
                     default=False, action="store_true",
1113
                     help="Show info on all instances on the cluster."
1114
                     " This can take a long time to run, use wisely")
1115

    
1116
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
1117
                           action="store_true", default=False,
1118
                           help="Interactive OS reinstall, lists available"
1119
                           " OS templates for selection")
1120

    
1121
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
1122
                                 action="store_true", default=False,
1123
                                 help="Remove the instance from the cluster"
1124
                                 " configuration even if there are failures"
1125
                                 " during the removal process")
1126

    
1127
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
1128
                                        dest="ignore_remove_failures",
1129
                                        action="store_true", default=False,
1130
                                        help="Remove the instance from the"
1131
                                        " cluster configuration even if there"
1132
                                        " are failures during the removal"
1133
                                        " process")
1134

    
1135
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
1136
                                 action="store_true", default=False,
1137
                                 help="Remove the instance from the cluster")
1138

    
1139
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
1140
                               help="Specifies the new node for the instance",
1141
                               metavar="NODE", default=None,
1142
                               completion_suggest=OPT_COMPL_ONE_NODE)
1143

    
1144
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
1145
                               help="Specifies the new secondary node",
1146
                               metavar="NODE", default=None,
1147
                               completion_suggest=OPT_COMPL_ONE_NODE)
1148

    
1149
NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node",
1150
                             help="Specifies the new primary node",
1151
                             metavar="<node>", default=None,
1152
                             completion_suggest=OPT_COMPL_ONE_NODE)
1153

    
1154
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
1155
                            default=False, action="store_true",
1156
                            help="Replace the disk(s) on the primary"
1157
                                 " node (applies only to internally mirrored"
1158
                                 " disk templates, e.g. %s)" %
1159
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
1160

    
1161
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
1162
                              default=False, action="store_true",
1163
                              help="Replace the disk(s) on the secondary"
1164
                                   " node (applies only to internally mirrored"
1165
                                   " disk templates, e.g. %s)" %
1166
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1167

    
1168
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
1169
                              default=False, action="store_true",
1170
                              help="Lock all nodes and auto-promote as needed"
1171
                              " to MC status")
1172

    
1173
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
1174
                              default=False, action="store_true",
1175
                              help="Automatically replace faulty disks"
1176
                                   " (applies only to internally mirrored"
1177
                                   " disk templates, e.g. %s)" %
1178
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1179

    
1180
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
1181
                             default=False, action="store_true",
1182
                             help="Ignore current recorded size"
1183
                             " (useful for forcing activation when"
1184
                             " the recorded size is wrong)")
1185

    
1186
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
1187
                          metavar="<node>",
1188
                          completion_suggest=OPT_COMPL_ONE_NODE)
1189

    
1190
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
1191
                         metavar="<dir>")
1192

    
1193
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
1194
                              help="Specify the secondary ip for the node",
1195
                              metavar="ADDRESS", default=None)
1196

    
1197
READD_OPT = cli_option("--readd", dest="readd",
1198
                       default=False, action="store_true",
1199
                       help="Readd old node after replacing it")
1200

    
1201
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1202
                                default=True, action="store_false",
1203
                                help="Disable SSH key fingerprint checking")
1204

    
1205
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1206
                                 default=False, action="store_true",
1207
                                 help="Force the joining of a node")
1208

    
1209
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1210
                    type="bool", default=None, metavar=_YORNO,
1211
                    help="Set the master_candidate flag on the node")
1212

    
1213
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1214
                         type="bool", default=None,
1215
                         help=("Set the offline flag on the node"
1216
                               " (cluster does not communicate with offline"
1217
                               " nodes)"))
1218

    
1219
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1220
                         type="bool", default=None,
1221
                         help=("Set the drained flag on the node"
1222
                               " (excluded from allocation operations)"))
1223

    
1224
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1225
                              type="bool", default=None, metavar=_YORNO,
1226
                              help="Set the master_capable flag on the node")
1227

    
1228
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1229
                          type="bool", default=None, metavar=_YORNO,
1230
                          help="Set the vm_capable flag on the node")
1231

    
1232
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1233
                             type="bool", default=None, metavar=_YORNO,
1234
                             help="Set the allocatable flag on a volume")
1235

    
1236
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1237
                            dest="enabled_hypervisors",
1238
                            help="Comma-separated list of hypervisors",
1239
                            type="string", default=None)
1240

    
1241
ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates",
1242
                                        dest="enabled_disk_templates",
1243
                                        help="Comma-separated list of "
1244
                                             "disk templates",
1245
                                        type="string", default=None)
1246

    
1247
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1248
                            type="keyval", default={},
1249
                            help="NIC parameters")
1250

    
1251
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1252
                         dest="candidate_pool_size", type="int",
1253
                         help="Set the candidate pool size")
1254

    
1255
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1256
                         help=("Enables LVM and specifies the volume group"
1257
                               " name (cluster-wide) for disk allocation"
1258
                               " [%s]" % constants.DEFAULT_VG),
1259
                         metavar="VG", default=None)
1260

    
1261
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1262
                          help="Destroy cluster", action="store_true")
1263

    
1264
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1265
                          help="Skip node agreement check (dangerous)",
1266
                          action="store_true", default=False)
1267

    
1268
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1269
                            help="Specify the mac prefix for the instance IP"
1270
                            " addresses, in the format XX:XX:XX",
1271
                            metavar="PREFIX",
1272
                            default=None)
1273

    
1274
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1275
                               help="Specify the node interface (cluster-wide)"
1276
                               " on which the master IP address will be added"
1277
                               " (cluster init default: %s)" %
1278
                               constants.DEFAULT_BRIDGE,
1279
                               metavar="NETDEV",
1280
                               default=None)
1281

    
1282
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1283
                                help="Specify the netmask of the master IP",
1284
                                metavar="NETMASK",
1285
                                default=None)
1286

    
1287
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1288
                                     dest="use_external_mip_script",
1289
                                     help="Specify whether to run a"
1290
                                     " user-provided script for the master"
1291
                                     " IP address turnup and"
1292
                                     " turndown operations",
1293
                                     type="bool", metavar=_YORNO, default=None)
1294

    
1295
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1296
                                help="Specify the default directory (cluster-"
1297
                                "wide) for storing the file-based disks [%s]" %
1298
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1299
                                metavar="DIR",
1300
                                default=None)
1301

    
1302
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1303
  "--shared-file-storage-dir",
1304
  dest="shared_file_storage_dir",
1305
  help="Specify the default directory (cluster-wide) for storing the"
1306
  " shared file-based disks [%s]" %
1307
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1308
  metavar="SHAREDDIR", default=None)
1309

    
1310
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1311
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1312
                                   action="store_false", default=True)
1313

    
1314
MODIFY_ETCHOSTS_OPT = \
1315
 cli_option("--modify-etc-hosts", dest="modify_etc_hosts", metavar=_YORNO,
1316
            default=None, type="bool",
1317
            help="Defines whether the cluster should autonomously modify"
1318
            " and keep in sync the /etc/hosts file of the nodes")
1319

    
1320
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1321
                                    help="Don't initialize SSH keys",
1322
                                    action="store_false", default=True)
1323

    
1324
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1325
                             help="Enable parseable error messages",
1326
                             action="store_true", default=False)
1327

    
1328
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1329
                          help="Skip N+1 memory redundancy tests",
1330
                          action="store_true", default=False)
1331

    
1332
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1333
                             help="Type of reboot: soft/hard/full",
1334
                             default=constants.INSTANCE_REBOOT_HARD,
1335
                             metavar="<REBOOT>",
1336
                             choices=list(constants.REBOOT_TYPES))
1337

    
1338
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1339
                                    dest="ignore_secondaries",
1340
                                    default=False, action="store_true",
1341
                                    help="Ignore errors from secondaries")
1342

    
1343
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1344
                            action="store_false", default=True,
1345
                            help="Don't shutdown the instance (unsafe)")
1346

    
1347
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1348
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1349
                         help="Maximum time to wait")
1350

    
1351
COMPRESS_OPT = cli_option("--compress", dest="compress",
1352
                          default=constants.IEC_NONE,
1353
                          help="The compression mode to use",
1354
                          choices=list(constants.IEC_ALL))
1355

    
1356
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1357
                                  dest="shutdown_timeout", type="int",
1358
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1359
                                  help="Maximum time to wait for instance"
1360
                                  " shutdown")
1361

    
1362
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1363
                          default=None,
1364
                          help=("Number of seconds between repetions of the"
1365
                                " command"))
1366

    
1367
EARLY_RELEASE_OPT = cli_option("--early-release",
1368
                               dest="early_release", default=False,
1369
                               action="store_true",
1370
                               help="Release the locks on the secondary"
1371
                               " node(s) early")
1372

    
1373
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1374
                                  dest="new_cluster_cert",
1375
                                  default=False, action="store_true",
1376
                                  help="Generate a new cluster certificate")
1377

    
1378
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1379
                           default=None,
1380
                           help="File containing new RAPI certificate")
1381

    
1382
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1383
                               default=None, action="store_true",
1384
                               help=("Generate a new self-signed RAPI"
1385
                                     " certificate"))
1386

    
1387
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1388
                            default=None,
1389
                            help="File containing new SPICE certificate")
1390

    
1391
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1392
                              default=None,
1393
                              help="File containing the certificate of the CA"
1394
                              " which signed the SPICE certificate")
1395

    
1396
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1397
                                dest="new_spice_cert", default=None,
1398
                                action="store_true",
1399
                                help=("Generate a new self-signed SPICE"
1400
                                      " certificate"))
1401

    
1402
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1403
                                    dest="new_confd_hmac_key",
1404
                                    default=False, action="store_true",
1405
                                    help=("Create a new HMAC key for %s" %
1406
                                          constants.CONFD))
1407

    
1408
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1409
                                       dest="cluster_domain_secret",
1410
                                       default=None,
1411
                                       help=("Load new new cluster domain"
1412
                                             " secret from file"))
1413

    
1414
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1415
                                           dest="new_cluster_domain_secret",
1416
                                           default=False, action="store_true",
1417
                                           help=("Create a new cluster domain"
1418
                                                 " secret"))
1419

    
1420
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1421
                              dest="use_replication_network",
1422
                              help="Whether to use the replication network"
1423
                              " for talking to the nodes",
1424
                              action="store_true", default=False)
1425

    
1426
MAINTAIN_NODE_HEALTH_OPT = \
1427
    cli_option("--maintain-node-health", dest="maintain_node_health",
1428
               metavar=_YORNO, default=None, type="bool",
1429
               help="Configure the cluster to automatically maintain node"
1430
               " health, by shutting down unknown instances, shutting down"
1431
               " unknown DRBD devices, etc.")
1432

    
1433
IDENTIFY_DEFAULTS_OPT = \
1434
    cli_option("--identify-defaults", dest="identify_defaults",
1435
               default=False, action="store_true",
1436
               help="Identify which saved instance parameters are equal to"
1437
               " the current cluster defaults and set them as such, instead"
1438
               " of marking them as overridden")
1439

    
1440
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1441
                         action="store", dest="uid_pool",
1442
                         help=("A list of user-ids or user-id"
1443
                               " ranges separated by commas"))
1444

    
1445
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1446
                          action="store", dest="add_uids",
1447
                          help=("A list of user-ids or user-id"
1448
                                " ranges separated by commas, to be"
1449
                                " added to the user-id pool"))
1450

    
1451
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1452
                             action="store", dest="remove_uids",
1453
                             help=("A list of user-ids or user-id"
1454
                                   " ranges separated by commas, to be"
1455
                                   " removed from the user-id pool"))
1456

    
1457
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1458
                              action="store", dest="reserved_lvs",
1459
                              help=("A comma-separated list of reserved"
1460
                                    " logical volumes names, that will be"
1461
                                    " ignored by cluster verify"))
1462

    
1463
ROMAN_OPT = cli_option("--roman",
1464
                       dest="roman_integers", default=False,
1465
                       action="store_true",
1466
                       help="Use roman numbers for positive integers")
1467

    
1468
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1469
                             action="store", default=None,
1470
                             help="Specifies usermode helper for DRBD")
1471

    
1472
PRIMARY_IP_VERSION_OPT = \
1473
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1474
               action="store", dest="primary_ip_version",
1475
               metavar="%d|%d" % (constants.IP4_VERSION,
1476
                                  constants.IP6_VERSION),
1477
               help="Cluster-wide IP version for primary IP")
1478

    
1479
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1480
                              action="store_true",
1481
                              help="Show machine name for every line in output")
1482

    
1483
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1484
                              action="store_true",
1485
                              help=("Hide successful results and show failures"
1486
                                    " only (determined by the exit code)"))
1487

    
1488
REASON_OPT = cli_option("--reason", default=None,
1489
                        help="The reason for executing the command")
1490

    
1491

    
1492
def _PriorityOptionCb(option, _, value, parser):
1493
  """Callback for processing C{--priority} option.
1494

1495
  """
1496
  value = _PRIONAME_TO_VALUE[value]
1497

    
1498
  setattr(parser.values, option.dest, value)
1499

    
1500

    
1501
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1502
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1503
                          choices=_PRIONAME_TO_VALUE.keys(),
1504
                          action="callback", type="choice",
1505
                          callback=_PriorityOptionCb,
1506
                          help="Priority for opcode processing")
1507

    
1508
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1509
                        type="bool", default=None, metavar=_YORNO,
1510
                        help="Sets the hidden flag on the OS")
1511

    
1512
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1513
                        type="bool", default=None, metavar=_YORNO,
1514
                        help="Sets the blacklisted flag on the OS")
1515

    
1516
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1517
                                     type="bool", metavar=_YORNO,
1518
                                     dest="prealloc_wipe_disks",
1519
                                     help=("Wipe disks prior to instance"
1520
                                           " creation"))
1521

    
1522
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1523
                             type="keyval", default=None,
1524
                             help="Node parameters")
1525

    
1526
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1527
                              action="store", metavar="POLICY", default=None,
1528
                              help="Allocation policy for the node group")
1529

    
1530
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1531
                              type="bool", metavar=_YORNO,
1532
                              dest="node_powered",
1533
                              help="Specify if the SoR for node is powered")
1534

    
1535
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1536
                             default=constants.OOB_TIMEOUT,
1537
                             help="Maximum time to wait for out-of-band helper")
1538

    
1539
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1540
                             default=constants.OOB_POWER_DELAY,
1541
                             help="Time in seconds to wait between power-ons")
1542

    
1543
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1544
                              action="store_true", default=False,
1545
                              help=("Whether command argument should be treated"
1546
                                    " as filter"))
1547

    
1548
NO_REMEMBER_OPT = cli_option("--no-remember",
1549
                             dest="no_remember",
1550
                             action="store_true", default=False,
1551
                             help="Perform but do not record the change"
1552
                             " in the configuration")
1553

    
1554
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1555
                              default=False, action="store_true",
1556
                              help="Evacuate primary instances only")
1557

    
1558
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1559
                                default=False, action="store_true",
1560
                                help="Evacuate secondary instances only"
1561
                                     " (applies only to internally mirrored"
1562
                                     " disk templates, e.g. %s)" %
1563
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1564

    
1565
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1566
                                action="store_true", default=False,
1567
                                help="Pause instance at startup")
1568

    
1569
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1570
                          help="Destination node group (name or uuid)",
1571
                          default=None, action="append",
1572
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1573

    
1574
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1575
                               action="append", dest="ignore_errors",
1576
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1577
                               help="Error code to be ignored")
1578

    
1579
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1580
                            action="append",
1581
                            help=("Specify disk state information in the"
1582
                                  " format"
1583
                                  " storage_type/identifier:option=value,...;"
1584
                                  " note this is unused for now"),
1585
                            type="identkeyval")
1586

    
1587
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1588
                          action="append",
1589
                          help=("Specify hypervisor state information in the"
1590
                                " format hypervisor:option=value,...;"
1591
                                " note this is unused for now"),
1592
                          type="identkeyval")
1593

    
1594
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1595
                                action="store_true", default=False,
1596
                                help="Ignore instance policy violations")
1597

    
1598
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1599
                             help="Sets the instance's runtime memory,"
1600
                             " ballooning it up or down to the new value",
1601
                             default=None, type="unit", metavar="<size>")
1602

    
1603
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1604
                          action="store_true", default=False,
1605
                          help="Marks the grow as absolute instead of the"
1606
                          " (default) relative mode")
1607

    
1608
NETWORK_OPT = cli_option("--network",
1609
                         action="store", default=None, dest="network",
1610
                         help="IP network in CIDR notation")
1611

    
1612
GATEWAY_OPT = cli_option("--gateway",
1613
                         action="store", default=None, dest="gateway",
1614
                         help="IP address of the router (gateway)")
1615

    
1616
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1617
                                  action="store", default=None,
1618
                                  dest="add_reserved_ips",
1619
                                  help="Comma-separated list of"
1620
                                  " reserved IPs to add")
1621

    
1622
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1623
                                     action="store", default=None,
1624
                                     dest="remove_reserved_ips",
1625
                                     help="Comma-delimited list of"
1626
                                     " reserved IPs to remove")
1627

    
1628
NETWORK6_OPT = cli_option("--network6",
1629
                          action="store", default=None, dest="network6",
1630
                          help="IP network in CIDR notation")
1631

    
1632
GATEWAY6_OPT = cli_option("--gateway6",
1633
                          action="store", default=None, dest="gateway6",
1634
                          help="IP6 address of the router (gateway)")
1635

    
1636
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1637
                                  dest="conflicts_check",
1638
                                  default=True,
1639
                                  action="store_false",
1640
                                  help="Don't check for conflicting IPs")
1641

    
1642
INCLUDEDEFAULTS_OPT = cli_option("--include-defaults", dest="include_defaults",
1643
                                 default=False, action="store_true",
1644
                                 help="Include default values")
1645

    
1646
HOTPLUG_OPT = cli_option("--hotplug", dest="hotplug",
1647
                         action="store_true", default=False,
1648
                         help="Hotplug supported devices (NICs and Disks)")
1649

    
1650
#: Options provided by all commands
1651
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1652

    
1653
# options related to asynchronous job handling
1654

    
1655
SUBMIT_OPTS = [
1656
  SUBMIT_OPT,
1657
  PRINT_JOBID_OPT,
1658
  ]
1659

    
1660
# common options for creating instances. add and import then add their own
1661
# specific ones.
1662
COMMON_CREATE_OPTS = [
1663
  BACKEND_OPT,
1664
  DISK_OPT,
1665
  DISK_TEMPLATE_OPT,
1666
  FILESTORE_DIR_OPT,
1667
  FILESTORE_DRIVER_OPT,
1668
  HYPERVISOR_OPT,
1669
  IALLOCATOR_OPT,
1670
  NET_OPT,
1671
  NODE_PLACEMENT_OPT,
1672
  NOIPCHECK_OPT,
1673
  NOCONFLICTSCHECK_OPT,
1674
  NONAMECHECK_OPT,
1675
  NONICS_OPT,
1676
  NWSYNC_OPT,
1677
  OSPARAMS_OPT,
1678
  OS_SIZE_OPT,
1679
  SUBMIT_OPT,
1680
  PRINT_JOBID_OPT,
1681
  TAG_ADD_OPT,
1682
  DRY_RUN_OPT,
1683
  PRIORITY_OPT,
1684
  ]
1685

    
1686
# common instance policy options
1687
INSTANCE_POLICY_OPTS = [
1688
  IPOLICY_BOUNDS_SPECS_OPT,
1689
  IPOLICY_DISK_TEMPLATES,
1690
  IPOLICY_VCPU_RATIO,
1691
  IPOLICY_SPINDLE_RATIO,
1692
  ]
1693

    
1694
# instance policy split specs options
1695
SPLIT_ISPECS_OPTS = [
1696
  SPECS_CPU_COUNT_OPT,
1697
  SPECS_DISK_COUNT_OPT,
1698
  SPECS_DISK_SIZE_OPT,
1699
  SPECS_MEM_SIZE_OPT,
1700
  SPECS_NIC_COUNT_OPT,
1701
  ]
1702

    
1703

    
1704
class _ShowUsage(Exception):
1705
  """Exception class for L{_ParseArgs}.
1706

1707
  """
1708
  def __init__(self, exit_error):
1709
    """Initializes instances of this class.
1710

1711
    @type exit_error: bool
1712
    @param exit_error: Whether to report failure on exit
1713

1714
    """
1715
    Exception.__init__(self)
1716
    self.exit_error = exit_error
1717

    
1718

    
1719
class _ShowVersion(Exception):
1720
  """Exception class for L{_ParseArgs}.
1721

1722
  """
1723

    
1724

    
1725
def _ParseArgs(binary, argv, commands, aliases, env_override):
1726
  """Parser for the command line arguments.
1727

1728
  This function parses the arguments and returns the function which
1729
  must be executed together with its (modified) arguments.
1730

1731
  @param binary: Script name
1732
  @param argv: Command line arguments
1733
  @param commands: Dictionary containing command definitions
1734
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1735
  @param env_override: list of env variables allowed for default args
1736
  @raise _ShowUsage: If usage description should be shown
1737
  @raise _ShowVersion: If version should be shown
1738

1739
  """
1740
  assert not (env_override - set(commands))
1741
  assert not (set(aliases.keys()) & set(commands.keys()))
1742

    
1743
  if len(argv) > 1:
1744
    cmd = argv[1]
1745
  else:
1746
    # No option or command given
1747
    raise _ShowUsage(exit_error=True)
1748

    
1749
  if cmd == "--version":
1750
    raise _ShowVersion()
1751
  elif cmd == "--help":
1752
    raise _ShowUsage(exit_error=False)
1753
  elif not (cmd in commands or cmd in aliases):
1754
    raise _ShowUsage(exit_error=True)
1755

    
1756
  # get command, unalias it, and look it up in commands
1757
  if cmd in aliases:
1758
    if aliases[cmd] not in commands:
1759
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1760
                                   " command '%s'" % (cmd, aliases[cmd]))
1761

    
1762
    cmd = aliases[cmd]
1763

    
1764
  if cmd in env_override:
1765
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1766
    env_args = os.environ.get(args_env_name)
1767
    if env_args:
1768
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1769

    
1770
  func, args_def, parser_opts, usage, description = commands[cmd]
1771
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1772
                        description=description,
1773
                        formatter=TitledHelpFormatter(),
1774
                        usage="%%prog %s %s" % (cmd, usage))
1775
  parser.disable_interspersed_args()
1776
  options, args = parser.parse_args(args=argv[2:])
1777

    
1778
  if not _CheckArguments(cmd, args_def, args):
1779
    return None, None, None
1780

    
1781
  return func, options, args
1782

    
1783

    
1784
def _FormatUsage(binary, commands):
1785
  """Generates a nice description of all commands.
1786

1787
  @param binary: Script name
1788
  @param commands: Dictionary containing command definitions
1789

1790
  """
1791
  # compute the max line length for cmd + usage
1792
  mlen = min(60, max(map(len, commands)))
1793

    
1794
  yield "Usage: %s {command} [options...] [argument...]" % binary
1795
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1796
  yield ""
1797
  yield "Commands:"
1798

    
1799
  # and format a nice command list
1800
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1801
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1802
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1803
    for line in help_lines:
1804
      yield " %-*s   %s" % (mlen, "", line)
1805

    
1806
  yield ""
1807

    
1808

    
1809
def _CheckArguments(cmd, args_def, args):
1810
  """Verifies the arguments using the argument definition.
1811

1812
  Algorithm:
1813

1814
    1. Abort with error if values specified by user but none expected.
1815

1816
    1. For each argument in definition
1817

1818
      1. Keep running count of minimum number of values (min_count)
1819
      1. Keep running count of maximum number of values (max_count)
1820
      1. If it has an unlimited number of values
1821

1822
        1. Abort with error if it's not the last argument in the definition
1823

1824
    1. If last argument has limited number of values
1825

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

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

1830
  """
1831
  if args and not args_def:
1832
    ToStderr("Error: Command %s expects no arguments", cmd)
1833
    return False
1834

    
1835
  min_count = None
1836
  max_count = None
1837
  check_max = None
1838

    
1839
  last_idx = len(args_def) - 1
1840

    
1841
  for idx, arg in enumerate(args_def):
1842
    if min_count is None:
1843
      min_count = arg.min
1844
    elif arg.min is not None:
1845
      min_count += arg.min
1846

    
1847
    if max_count is None:
1848
      max_count = arg.max
1849
    elif arg.max is not None:
1850
      max_count += arg.max
1851

    
1852
    if idx == last_idx:
1853
      check_max = (arg.max is not None)
1854

    
1855
    elif arg.max is None:
1856
      raise errors.ProgrammerError("Only the last argument can have max=None")
1857

    
1858
  if check_max:
1859
    # Command with exact number of arguments
1860
    if (min_count is not None and max_count is not None and
1861
        min_count == max_count and len(args) != min_count):
1862
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1863
      return False
1864

    
1865
    # Command with limited number of arguments
1866
    if max_count is not None and len(args) > max_count:
1867
      ToStderr("Error: Command %s expects only %d argument(s)",
1868
               cmd, max_count)
1869
      return False
1870

    
1871
  # Command with some required arguments
1872
  if min_count is not None and len(args) < min_count:
1873
    ToStderr("Error: Command %s expects at least %d argument(s)",
1874
             cmd, min_count)
1875
    return False
1876

    
1877
  return True
1878

    
1879

    
1880
def SplitNodeOption(value):
1881
  """Splits the value of a --node option.
1882

1883
  """
1884
  if value and ":" in value:
1885
    return value.split(":", 1)
1886
  else:
1887
    return (value, None)
1888

    
1889

    
1890
def CalculateOSNames(os_name, os_variants):
1891
  """Calculates all the names an OS can be called, according to its variants.
1892

1893
  @type os_name: string
1894
  @param os_name: base name of the os
1895
  @type os_variants: list or None
1896
  @param os_variants: list of supported variants
1897
  @rtype: list
1898
  @return: list of valid names
1899

1900
  """
1901
  if os_variants:
1902
    return ["%s+%s" % (os_name, v) for v in os_variants]
1903
  else:
1904
    return [os_name]
1905

    
1906

    
1907
def ParseFields(selected, default):
1908
  """Parses the values of "--field"-like options.
1909

1910
  @type selected: string or None
1911
  @param selected: User-selected options
1912
  @type default: list
1913
  @param default: Default fields
1914

1915
  """
1916
  if selected is None:
1917
    return default
1918

    
1919
  if selected.startswith("+"):
1920
    return default + selected[1:].split(",")
1921

    
1922
  return selected.split(",")
1923

    
1924

    
1925
UsesRPC = rpc.RunWithRPC
1926

    
1927

    
1928
def AskUser(text, choices=None):
1929
  """Ask the user a question.
1930

1931
  @param text: the question to ask
1932

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

1938
  @return: one of the return values from the choices list; if input is
1939
      not possible (i.e. not running with a tty, we return the last
1940
      entry from the list
1941

1942
  """
1943
  if choices is None:
1944
    choices = [("y", True, "Perform the operation"),
1945
               ("n", False, "Do not perform the operation")]
1946
  if not choices or not isinstance(choices, list):
1947
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1948
  for entry in choices:
1949
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1950
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1951

    
1952
  answer = choices[-1][1]
1953
  new_text = []
1954
  for line in text.splitlines():
1955
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1956
  text = "\n".join(new_text)
1957
  try:
1958
    f = file("/dev/tty", "a+")
1959
  except IOError:
1960
    return answer
1961
  try:
1962
    chars = [entry[0] for entry in choices]
1963
    chars[-1] = "[%s]" % chars[-1]
1964
    chars.append("?")
1965
    maps = dict([(entry[0], entry[1]) for entry in choices])
1966
    while True:
1967
      f.write(text)
1968
      f.write("\n")
1969
      f.write("/".join(chars))
1970
      f.write(": ")
1971
      line = f.readline(2).strip().lower()
1972
      if line in maps:
1973
        answer = maps[line]
1974
        break
1975
      elif line == "?":
1976
        for entry in choices:
1977
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1978
        f.write("\n")
1979
        continue
1980
  finally:
1981
    f.close()
1982
  return answer
1983

    
1984

    
1985
class JobSubmittedException(Exception):
1986
  """Job was submitted, client should exit.
1987

1988
  This exception has one argument, the ID of the job that was
1989
  submitted. The handler should print this ID.
1990

1991
  This is not an error, just a structured way to exit from clients.
1992

1993
  """
1994

    
1995

    
1996
def SendJob(ops, cl=None):
1997
  """Function to submit an opcode without waiting for the results.
1998

1999
  @type ops: list
2000
  @param ops: list of opcodes
2001
  @type cl: luxi.Client
2002
  @param cl: the luxi client to use for communicating with the master;
2003
             if None, a new client will be created
2004

2005
  """
2006
  if cl is None:
2007
    cl = GetClient()
2008

    
2009
  job_id = cl.SubmitJob(ops)
2010

    
2011
  return job_id
2012

    
2013

    
2014
def GenericPollJob(job_id, cbs, report_cbs):
2015
  """Generic job-polling function.
2016

2017
  @type job_id: number
2018
  @param job_id: Job ID
2019
  @type cbs: Instance of L{JobPollCbBase}
2020
  @param cbs: Data callbacks
2021
  @type report_cbs: Instance of L{JobPollReportCbBase}
2022
  @param report_cbs: Reporting callbacks
2023

2024
  """
2025
  prev_job_info = None
2026
  prev_logmsg_serial = None
2027

    
2028
  status = None
2029

    
2030
  while True:
2031
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
2032
                                      prev_logmsg_serial)
2033
    if not result:
2034
      # job not found, go away!
2035
      raise errors.JobLost("Job with id %s lost" % job_id)
2036

    
2037
    if result == constants.JOB_NOTCHANGED:
2038
      report_cbs.ReportNotChanged(job_id, status)
2039

    
2040
      # Wait again
2041
      continue
2042

    
2043
    # Split result, a tuple of (field values, log entries)
2044
    (job_info, log_entries) = result
2045
    (status, ) = job_info
2046

    
2047
    if log_entries:
2048
      for log_entry in log_entries:
2049
        (serial, timestamp, log_type, message) = log_entry
2050
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
2051
                                    log_type, message)
2052
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
2053

    
2054
    # TODO: Handle canceled and archived jobs
2055
    elif status in (constants.JOB_STATUS_SUCCESS,
2056
                    constants.JOB_STATUS_ERROR,
2057
                    constants.JOB_STATUS_CANCELING,
2058
                    constants.JOB_STATUS_CANCELED):
2059
      break
2060

    
2061
    prev_job_info = job_info
2062

    
2063
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
2064
  if not jobs:
2065
    raise errors.JobLost("Job with id %s lost" % job_id)
2066

    
2067
  status, opstatus, result = jobs[0]
2068

    
2069
  if status == constants.JOB_STATUS_SUCCESS:
2070
    return result
2071

    
2072
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
2073
    raise errors.OpExecError("Job was canceled")
2074

    
2075
  has_ok = False
2076
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
2077
    if status == constants.OP_STATUS_SUCCESS:
2078
      has_ok = True
2079
    elif status == constants.OP_STATUS_ERROR:
2080
      errors.MaybeRaise(msg)
2081

    
2082
      if has_ok:
2083
        raise errors.OpExecError("partial failure (opcode %d): %s" %
2084
                                 (idx, msg))
2085

    
2086
      raise errors.OpExecError(str(msg))
2087

    
2088
  # default failure mode
2089
  raise errors.OpExecError(result)
2090

    
2091

    
2092
class JobPollCbBase:
2093
  """Base class for L{GenericPollJob} callbacks.
2094

2095
  """
2096
  def __init__(self):
2097
    """Initializes this class.
2098

2099
    """
2100

    
2101
  def WaitForJobChangeOnce(self, job_id, fields,
2102
                           prev_job_info, prev_log_serial):
2103
    """Waits for changes on a job.
2104

2105
    """
2106
    raise NotImplementedError()
2107

    
2108
  def QueryJobs(self, job_ids, fields):
2109
    """Returns the selected fields for the selected job IDs.
2110

2111
    @type job_ids: list of numbers
2112
    @param job_ids: Job IDs
2113
    @type fields: list of strings
2114
    @param fields: Fields
2115

2116
    """
2117
    raise NotImplementedError()
2118

    
2119

    
2120
class JobPollReportCbBase:
2121
  """Base class for L{GenericPollJob} reporting callbacks.
2122

2123
  """
2124
  def __init__(self):
2125
    """Initializes this class.
2126

2127
    """
2128

    
2129
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2130
    """Handles a log message.
2131

2132
    """
2133
    raise NotImplementedError()
2134

    
2135
  def ReportNotChanged(self, job_id, status):
2136
    """Called for if a job hasn't changed in a while.
2137

2138
    @type job_id: number
2139
    @param job_id: Job ID
2140
    @type status: string or None
2141
    @param status: Job status if available
2142

2143
    """
2144
    raise NotImplementedError()
2145

    
2146

    
2147
class _LuxiJobPollCb(JobPollCbBase):
2148
  def __init__(self, cl):
2149
    """Initializes this class.
2150

2151
    """
2152
    JobPollCbBase.__init__(self)
2153
    self.cl = cl
2154

    
2155
  def WaitForJobChangeOnce(self, job_id, fields,
2156
                           prev_job_info, prev_log_serial):
2157
    """Waits for changes on a job.
2158

2159
    """
2160
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2161
                                        prev_job_info, prev_log_serial)
2162

    
2163
  def QueryJobs(self, job_ids, fields):
2164
    """Returns the selected fields for the selected job IDs.
2165

2166
    """
2167
    return self.cl.QueryJobs(job_ids, fields)
2168

    
2169

    
2170
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2171
  def __init__(self, feedback_fn):
2172
    """Initializes this class.
2173

2174
    """
2175
    JobPollReportCbBase.__init__(self)
2176

    
2177
    self.feedback_fn = feedback_fn
2178

    
2179
    assert callable(feedback_fn)
2180

    
2181
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2182
    """Handles a log message.
2183

2184
    """
2185
    self.feedback_fn((timestamp, log_type, log_msg))
2186

    
2187
  def ReportNotChanged(self, job_id, status):
2188
    """Called if a job hasn't changed in a while.
2189

2190
    """
2191
    # Ignore
2192

    
2193

    
2194
class StdioJobPollReportCb(JobPollReportCbBase):
2195
  def __init__(self):
2196
    """Initializes this class.
2197

2198
    """
2199
    JobPollReportCbBase.__init__(self)
2200

    
2201
    self.notified_queued = False
2202
    self.notified_waitlock = False
2203

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

2207
    """
2208
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2209
             FormatLogMessage(log_type, log_msg))
2210

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

2214
    """
2215
    if status is None:
2216
      return
2217

    
2218
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2219
      ToStderr("Job %s is waiting in queue", job_id)
2220
      self.notified_queued = True
2221

    
2222
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2223
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2224
      self.notified_waitlock = True
2225

    
2226

    
2227
def FormatLogMessage(log_type, log_msg):
2228
  """Formats a job message according to its type.
2229

2230
  """
2231
  if log_type != constants.ELOG_MESSAGE:
2232
    log_msg = str(log_msg)
2233

    
2234
  return utils.SafeEncode(log_msg)
2235

    
2236

    
2237
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2238
  """Function to poll for the result of a job.
2239

2240
  @type job_id: job identified
2241
  @param job_id: the job to poll for results
2242
  @type cl: luxi.Client
2243
  @param cl: the luxi client to use for communicating with the master;
2244
             if None, a new client will be created
2245

2246
  """
2247
  if cl is None:
2248
    cl = GetClient()
2249

    
2250
  if reporter is None:
2251
    if feedback_fn:
2252
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2253
    else:
2254
      reporter = StdioJobPollReportCb()
2255
  elif feedback_fn:
2256
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2257

    
2258
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2259

    
2260

    
2261
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2262
  """Legacy function to submit an opcode.
2263

2264
  This is just a simple wrapper over the construction of the processor
2265
  instance. It should be extended to better handle feedback and
2266
  interaction functions.
2267

2268
  """
2269
  if cl is None:
2270
    cl = GetClient()
2271

    
2272
  SetGenericOpcodeOpts([op], opts)
2273

    
2274
  job_id = SendJob([op], cl=cl)
2275
  if hasattr(opts, "print_jobid") and opts.print_jobid:
2276
    ToStdout("%d" % job_id)
2277

    
2278
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2279
                       reporter=reporter)
2280

    
2281
  return op_results[0]
2282

    
2283

    
2284
def SubmitOpCodeToDrainedQueue(op):
2285
  """Forcefully insert a job in the queue, even if it is drained.
2286

2287
  """
2288
  cl = GetClient()
2289
  job_id = cl.SubmitJobToDrainedQueue([op])
2290
  op_results = PollJob(job_id, cl=cl)
2291
  return op_results[0]
2292

    
2293

    
2294
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2295
  """Wrapper around SubmitOpCode or SendJob.
2296

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

2302
  It will also process the opcodes if we're sending the via SendJob
2303
  (otherwise SubmitOpCode does it).
2304

2305
  """
2306
  if opts and opts.submit_only:
2307
    job = [op]
2308
    SetGenericOpcodeOpts(job, opts)
2309
    job_id = SendJob(job, cl=cl)
2310
    if opts.print_jobid:
2311
      ToStdout("%d" % job_id)
2312
    raise JobSubmittedException(job_id)
2313
  else:
2314
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2315

    
2316

    
2317
def _InitReasonTrail(op, opts):
2318
  """Builds the first part of the reason trail
2319

2320
  Builds the initial part of the reason trail, adding the user provided reason
2321
  (if it exists) and the name of the command starting the operation.
2322

2323
  @param op: the opcode the reason trail will be added to
2324
  @param opts: the command line options selected by the user
2325

2326
  """
2327
  assert len(sys.argv) >= 2
2328
  trail = []
2329

    
2330
  if opts.reason:
2331
    trail.append((constants.OPCODE_REASON_SRC_USER,
2332
                  opts.reason,
2333
                  utils.EpochNano()))
2334

    
2335
  binary = os.path.basename(sys.argv[0])
2336
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2337
  command = sys.argv[1]
2338
  trail.append((source, command, utils.EpochNano()))
2339
  op.reason = trail
2340

    
2341

    
2342
def SetGenericOpcodeOpts(opcode_list, options):
2343
  """Processor for generic options.
2344

2345
  This function updates the given opcodes based on generic command
2346
  line options (like debug, dry-run, etc.).
2347

2348
  @param opcode_list: list of opcodes
2349
  @param options: command line options or None
2350
  @return: None (in-place modification)
2351

2352
  """
2353
  if not options:
2354
    return
2355
  for op in opcode_list:
2356
    op.debug_level = options.debug
2357
    if hasattr(options, "dry_run"):
2358
      op.dry_run = options.dry_run
2359
    if getattr(options, "priority", None) is not None:
2360
      op.priority = options.priority
2361
    _InitReasonTrail(op, options)
2362

    
2363

    
2364
def GetClient(query=False):
2365
  """Connects to the a luxi socket and returns a client.
2366

2367
  @type query: boolean
2368
  @param query: this signifies that the client will only be
2369
      used for queries; if the build-time parameter
2370
      enable-split-queries is enabled, then the client will be
2371
      connected to the query socket instead of the masterd socket
2372

2373
  """
2374
  override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
2375
  if override_socket:
2376
    if override_socket == constants.LUXI_OVERRIDE_MASTER:
2377
      address = pathutils.MASTER_SOCKET
2378
    elif override_socket == constants.LUXI_OVERRIDE_QUERY:
2379
      address = pathutils.QUERY_SOCKET
2380
    else:
2381
      address = override_socket
2382
  elif query and constants.ENABLE_SPLIT_QUERY:
2383
    address = pathutils.QUERY_SOCKET
2384
  else:
2385
    address = None
2386
  # TODO: Cache object?
2387
  try:
2388
    client = luxi.Client(address=address)
2389
  except luxi.NoMasterError:
2390
    ss = ssconf.SimpleStore()
2391

    
2392
    # Try to read ssconf file
2393
    try:
2394
      ss.GetMasterNode()
2395
    except errors.ConfigurationError:
2396
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
2397
                                 " not part of a cluster",
2398
                                 errors.ECODE_INVAL)
2399

    
2400
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
2401
    if master != myself:
2402
      raise errors.OpPrereqError("This is not the master node, please connect"
2403
                                 " to node '%s' and rerun the command" %
2404
                                 master, errors.ECODE_INVAL)
2405
    raise
2406
  return client
2407

    
2408

    
2409
def FormatError(err):
2410
  """Return a formatted error message for a given error.
2411

2412
  This function takes an exception instance and returns a tuple
2413
  consisting of two values: first, the recommended exit code, and
2414
  second, a string describing the error message (not
2415
  newline-terminated).
2416

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

    
2502

    
2503
def GenericMain(commands, override=None, aliases=None,
2504
                env_override=frozenset()):
2505
  """Generic main function for all the gnt-* commands.
2506

2507
  @param commands: a dictionary with a special structure, see the design doc
2508
                   for command line handling.
2509
  @param override: if not None, we expect a dictionary with keys that will
2510
                   override command line options; this can be used to pass
2511
                   options from the scripts to generic functions
2512
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2513
  @param env_override: list of environment names which are allowed to submit
2514
                       default args for commands
2515

2516
  """
2517
  # save the program name and the entire command line for later logging
2518
  if sys.argv:
2519
    binary = os.path.basename(sys.argv[0])
2520
    if not binary:
2521
      binary = sys.argv[0]
2522

    
2523
    if len(sys.argv) >= 2:
2524
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2525
    else:
2526
      logname = binary
2527

    
2528
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2529
  else:
2530
    binary = "<unknown program>"
2531
    cmdline = "<unknown>"
2532

    
2533
  if aliases is None:
2534
    aliases = {}
2535

    
2536
  try:
2537
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2538
                                       env_override)
2539
  except _ShowVersion:
2540
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2541
             constants.RELEASE_VERSION)
2542
    return constants.EXIT_SUCCESS
2543
  except _ShowUsage, err:
2544
    for line in _FormatUsage(binary, commands):
2545
      ToStdout(line)
2546

    
2547
    if err.exit_error:
2548
      return constants.EXIT_FAILURE
2549
    else:
2550
      return constants.EXIT_SUCCESS
2551
  except errors.ParameterError, err:
2552
    result, err_msg = FormatError(err)
2553
    ToStderr(err_msg)
2554
    return 1
2555

    
2556
  if func is None: # parse error
2557
    return 1
2558

    
2559
  if override is not None:
2560
    for key, val in override.iteritems():
2561
      setattr(options, key, val)
2562

    
2563
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2564
                     stderr_logging=True)
2565

    
2566
  logging.info("Command line: %s", cmdline)
2567

    
2568
  try:
2569
    result = func(options, args)
2570
  except (errors.GenericError, luxi.ProtocolError,
2571
          JobSubmittedException), err:
2572
    result, err_msg = FormatError(err)
2573
    logging.exception("Error during command processing")
2574
    ToStderr(err_msg)
2575
  except KeyboardInterrupt:
2576
    result = constants.EXIT_FAILURE
2577
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2578
             " might have been submitted and"
2579
             " will continue to run in the background.")
2580
  except IOError, err:
2581
    if err.errno == errno.EPIPE:
2582
      # our terminal went away, we'll exit
2583
      sys.exit(constants.EXIT_FAILURE)
2584
    else:
2585
      raise
2586

    
2587
  return result
2588

    
2589

    
2590
def ParseNicOption(optvalue):
2591
  """Parses the value of the --net option(s).
2592

2593
  """
2594
  try:
2595
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2596
  except (TypeError, ValueError), err:
2597
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2598
                               errors.ECODE_INVAL)
2599

    
2600
  nics = [{}] * nic_max
2601
  for nidx, ndict in optvalue:
2602
    nidx = int(nidx)
2603

    
2604
    if not isinstance(ndict, dict):
2605
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2606
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2607

    
2608
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2609

    
2610
    nics[nidx] = ndict
2611

    
2612
  return nics
2613

    
2614

    
2615
def GenericInstanceCreate(mode, opts, args):
2616
  """Add an instance to the cluster via either creation or import.
2617

2618
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2619
  @param opts: the command line options selected by the user
2620
  @type args: list
2621
  @param args: should contain only one element, the new instance name
2622
  @rtype: int
2623
  @return: the desired exit code
2624

2625
  """
2626
  instance = args[0]
2627

    
2628
  (pnode, snode) = SplitNodeOption(opts.node)
2629

    
2630
  hypervisor = None
2631
  hvparams = {}
2632
  if opts.hypervisor:
2633
    hypervisor, hvparams = opts.hypervisor
2634

    
2635
  if opts.nics:
2636
    nics = ParseNicOption(opts.nics)
2637
  elif opts.no_nics:
2638
    # no nics
2639
    nics = []
2640
  elif mode == constants.INSTANCE_CREATE:
2641
    # default of one nic, all auto
2642
    nics = [{}]
2643
  else:
2644
    # mode == import
2645
    nics = []
2646

    
2647
  if opts.disk_template == constants.DT_DISKLESS:
2648
    if opts.disks or opts.sd_size is not None:
2649
      raise errors.OpPrereqError("Diskless instance but disk"
2650
                                 " information passed", errors.ECODE_INVAL)
2651
    disks = []
2652
  else:
2653
    if (not opts.disks and not opts.sd_size
2654
        and mode == constants.INSTANCE_CREATE):
2655
      raise errors.OpPrereqError("No disk information specified",
2656
                                 errors.ECODE_INVAL)
2657
    if opts.disks and opts.sd_size is not None:
2658
      raise errors.OpPrereqError("Please use either the '--disk' or"
2659
                                 " '-s' option", errors.ECODE_INVAL)
2660
    if opts.sd_size is not None:
2661
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2662

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

    
2700
  if opts.tags is not None:
2701
    tags = opts.tags.split(",")
2702
  else:
2703
    tags = []
2704

    
2705
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2706
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2707

    
2708
  if mode == constants.INSTANCE_CREATE:
2709
    start = opts.start
2710
    os_type = opts.os
2711
    force_variant = opts.force_variant
2712
    src_node = None
2713
    src_path = None
2714
    no_install = opts.no_install
2715
    identify_defaults = False
2716
    compress = constants.IEC_NONE
2717
  elif mode == constants.INSTANCE_IMPORT:
2718
    start = False
2719
    os_type = None
2720
    force_variant = False
2721
    src_node = opts.src_node
2722
    src_path = opts.src_dir
2723
    no_install = None
2724
    identify_defaults = opts.identify_defaults
2725
    compress = opts.compress
2726
  else:
2727
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2728

    
2729
  op = opcodes.OpInstanceCreate(instance_name=instance,
2730
                                disks=disks,
2731
                                disk_template=opts.disk_template,
2732
                                nics=nics,
2733
                                conflicts_check=opts.conflicts_check,
2734
                                pnode=pnode, snode=snode,
2735
                                ip_check=opts.ip_check,
2736
                                name_check=opts.name_check,
2737
                                wait_for_sync=opts.wait_for_sync,
2738
                                file_storage_dir=opts.file_storage_dir,
2739
                                file_driver=opts.file_driver,
2740
                                iallocator=opts.iallocator,
2741
                                hypervisor=hypervisor,
2742
                                hvparams=hvparams,
2743
                                beparams=opts.beparams,
2744
                                osparams=opts.osparams,
2745
                                mode=mode,
2746
                                start=start,
2747
                                os_type=os_type,
2748
                                force_variant=force_variant,
2749
                                src_node=src_node,
2750
                                src_path=src_path,
2751
                                compress=compress,
2752
                                tags=tags,
2753
                                no_install=no_install,
2754
                                identify_defaults=identify_defaults,
2755
                                ignore_ipolicy=opts.ignore_ipolicy)
2756

    
2757
  SubmitOrSend(op, opts)
2758
  return 0
2759

    
2760

    
2761
class _RunWhileClusterStoppedHelper:
2762
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2763

2764
  """
2765
  def __init__(self, feedback_fn, cluster_name, master_node,
2766
               online_nodes, ssh_ports):
2767
    """Initializes this class.
2768

2769
    @type feedback_fn: callable
2770
    @param feedback_fn: Feedback function
2771
    @type cluster_name: string
2772
    @param cluster_name: Cluster name
2773
    @type master_node: string
2774
    @param master_node Master node name
2775
    @type online_nodes: list
2776
    @param online_nodes: List of names of online nodes
2777
    @type ssh_ports: list
2778
    @param ssh_ports: List of SSH ports of online nodes
2779

2780
    """
2781
    self.feedback_fn = feedback_fn
2782
    self.cluster_name = cluster_name
2783
    self.master_node = master_node
2784
    self.online_nodes = online_nodes
2785
    self.ssh_ports = dict(zip(online_nodes, ssh_ports))
2786

    
2787
    self.ssh = ssh.SshRunner(self.cluster_name)
2788

    
2789
    self.nonmaster_nodes = [name for name in online_nodes
2790
                            if name != master_node]
2791

    
2792
    assert self.master_node not in self.nonmaster_nodes
2793

    
2794
  def _RunCmd(self, node_name, cmd):
2795
    """Runs a command on the local or a remote machine.
2796

2797
    @type node_name: string
2798
    @param node_name: Machine name
2799
    @type cmd: list
2800
    @param cmd: Command
2801

2802
    """
2803
    if node_name is None or node_name == self.master_node:
2804
      # No need to use SSH
2805
      result = utils.RunCmd(cmd)
2806
    else:
2807
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2808
                            utils.ShellQuoteArgs(cmd),
2809
                            port=self.ssh_ports[node_name])
2810

    
2811
    if result.failed:
2812
      errmsg = ["Failed to run command %s" % result.cmd]
2813
      if node_name:
2814
        errmsg.append("on node %s" % node_name)
2815
      errmsg.append(": exitcode %s and error %s" %
2816
                    (result.exit_code, result.output))
2817
      raise errors.OpExecError(" ".join(errmsg))
2818

    
2819
  def Call(self, fn, *args):
2820
    """Call function while all daemons are stopped.
2821

2822
    @type fn: callable
2823
    @param fn: Function to be called
2824

2825
    """
2826
    # Pause watcher by acquiring an exclusive lock on watcher state file
2827
    self.feedback_fn("Blocking watcher")
2828
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2829
    try:
2830
      # TODO: Currently, this just blocks. There's no timeout.
2831
      # TODO: Should it be a shared lock?
2832
      watcher_block.Exclusive(blocking=True)
2833

    
2834
      # Stop master daemons, so that no new jobs can come in and all running
2835
      # ones are finished
2836
      self.feedback_fn("Stopping master daemons")
2837
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2838
      try:
2839
        # Stop daemons on all nodes
2840
        for node_name in self.online_nodes:
2841
          self.feedback_fn("Stopping daemons on %s" % node_name)
2842
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2843

    
2844
        # All daemons are shut down now
2845
        try:
2846
          return fn(self, *args)
2847
        except Exception, err:
2848
          _, errmsg = FormatError(err)
2849
          logging.exception("Caught exception")
2850
          self.feedback_fn(errmsg)
2851
          raise
2852
      finally:
2853
        # Start cluster again, master node last
2854
        for node_name in self.nonmaster_nodes + [self.master_node]:
2855
          self.feedback_fn("Starting daemons on %s" % node_name)
2856
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"])
2857
    finally:
2858
      # Resume watcher
2859
      watcher_block.Close()
2860

    
2861

    
2862
def RunWhileClusterStopped(feedback_fn, fn, *args):
2863
  """Calls a function while all cluster daemons are stopped.
2864

2865
  @type feedback_fn: callable
2866
  @param feedback_fn: Feedback function
2867
  @type fn: callable
2868
  @param fn: Function to be called when daemons are stopped
2869

2870
  """
2871
  feedback_fn("Gathering cluster information")
2872

    
2873
  # This ensures we're running on the master daemon
2874
  cl = GetClient()
2875

    
2876
  (cluster_name, master_node) = \
2877
    cl.QueryConfigValues(["cluster_name", "master_node"])
2878

    
2879
  online_nodes = GetOnlineNodes([], cl=cl)
2880
  ssh_ports = GetNodesSshPorts(online_nodes, cl)
2881

    
2882
  # Don't keep a reference to the client. The master daemon will go away.
2883
  del cl
2884

    
2885
  assert master_node in online_nodes
2886

    
2887
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2888
                                       online_nodes, ssh_ports).Call(fn, *args)
2889

    
2890

    
2891
def GenerateTable(headers, fields, separator, data,
2892
                  numfields=None, unitfields=None,
2893
                  units=None):
2894
  """Prints a table with headers and different fields.
2895

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

2919
  """
2920
  if units is None:
2921
    if separator:
2922
      units = "m"
2923
    else:
2924
      units = "h"
2925

    
2926
  if numfields is None:
2927
    numfields = []
2928
  if unitfields is None:
2929
    unitfields = []
2930

    
2931
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2932
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2933

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

    
2948
  if separator is None:
2949
    mlens = [0 for name in fields]
2950
    format_str = " ".join(format_fields)
2951
  else:
2952
    format_str = separator.replace("%", "%%").join(format_fields)
2953

    
2954
  for row in data:
2955
    if row is None:
2956
      continue
2957
    for idx, val in enumerate(row):
2958
      if unitfields.Matches(fields[idx]):
2959
        try:
2960
          val = int(val)
2961
        except (TypeError, ValueError):
2962
          pass
2963
        else:
2964
          val = row[idx] = utils.FormatUnit(val, units)
2965
      val = row[idx] = str(val)
2966
      if separator is None:
2967
        mlens[idx] = max(mlens[idx], len(val))
2968

    
2969
  result = []
2970
  if headers:
2971
    args = []
2972
    for idx, name in enumerate(fields):
2973
      hdr = headers[name]
2974
      if separator is None:
2975
        mlens[idx] = max(mlens[idx], len(hdr))
2976
        args.append(mlens[idx])
2977
      args.append(hdr)
2978
    result.append(format_str % tuple(args))
2979

    
2980
  if separator is None:
2981
    assert len(mlens) == len(fields)
2982

    
2983
    if fields and not numfields.Matches(fields[-1]):
2984
      mlens[-1] = 0
2985

    
2986
  for line in data:
2987
    args = []
2988
    if line is None:
2989
      line = ["-" for _ in fields]
2990
    for idx in range(len(fields)):
2991
      if separator is None:
2992
        args.append(mlens[idx])
2993
      args.append(line[idx])
2994
    result.append(format_str % tuple(args))
2995

    
2996
  return result
2997

    
2998

    
2999
def _FormatBool(value):
3000
  """Formats a boolean value as a string.
3001

3002
  """
3003
  if value:
3004
    return "Y"
3005
  return "N"
3006

    
3007

    
3008
#: Default formatting for query results; (callback, align right)
3009
_DEFAULT_FORMAT_QUERY = {
3010
  constants.QFT_TEXT: (str, False),
3011
  constants.QFT_BOOL: (_FormatBool, False),
3012
  constants.QFT_NUMBER: (str, True),
3013
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
3014
  constants.QFT_OTHER: (str, False),
3015
  constants.QFT_UNKNOWN: (str, False),
3016
  }
3017

    
3018

    
3019
def _GetColumnFormatter(fdef, override, unit):
3020
  """Returns formatting function for a field.
3021

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

3032
  """
3033
  fmt = override.get(fdef.name, None)
3034
  if fmt is not None:
3035
    return fmt
3036

    
3037
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
3038

    
3039
  if fdef.kind == constants.QFT_UNIT:
3040
    # Can't keep this information in the static dictionary
3041
    return (lambda value: utils.FormatUnit(value, unit), True)
3042

    
3043
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
3044
  if fmt is not None:
3045
    return fmt
3046

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

    
3049

    
3050
class _QueryColumnFormatter:
3051
  """Callable class for formatting fields of a query.
3052

3053
  """
3054
  def __init__(self, fn, status_fn, verbose):
3055
    """Initializes this class.
3056

3057
    @type fn: callable
3058
    @param fn: Formatting function
3059
    @type status_fn: callable
3060
    @param status_fn: Function to report fields' status
3061
    @type verbose: boolean
3062
    @param verbose: whether to use verbose field descriptions or not
3063

3064
    """
3065
    self._fn = fn
3066
    self._status_fn = status_fn
3067
    self._verbose = verbose
3068

    
3069
  def __call__(self, data):
3070
    """Returns a field's string representation.
3071

3072
    """
3073
    (status, value) = data
3074

    
3075
    # Report status
3076
    self._status_fn(status)
3077

    
3078
    if status == constants.RS_NORMAL:
3079
      return self._fn(value)
3080

    
3081
    assert value is None, \
3082
           "Found value %r for abnormal status %s" % (value, status)
3083

    
3084
    return FormatResultError(status, self._verbose)
3085

    
3086

    
3087
def FormatResultError(status, verbose):
3088
  """Formats result status other than L{constants.RS_NORMAL}.
3089

3090
  @param status: The result status
3091
  @type verbose: boolean
3092
  @param verbose: Whether to return the verbose text
3093
  @return: Text of result status
3094

3095
  """
3096
  assert status != constants.RS_NORMAL, \
3097
         "FormatResultError called with status equal to constants.RS_NORMAL"
3098
  try:
3099
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
3100
  except KeyError:
3101
    raise NotImplementedError("Unknown status %s" % status)
3102
  else:
3103
    if verbose:
3104
      return verbose_text
3105
    return normal_text
3106

    
3107

    
3108
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3109
                      header=False, verbose=False):
3110
  """Formats data in L{objects.QueryResponse}.
3111

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

3127
  """
3128
  if unit is None:
3129
    if separator:
3130
      unit = "m"
3131
    else:
3132
      unit = "h"
3133

    
3134
  if format_override is None:
3135
    format_override = {}
3136

    
3137
  stats = dict.fromkeys(constants.RS_ALL, 0)
3138

    
3139
  def _RecordStatus(status):
3140
    if status in stats:
3141
      stats[status] += 1
3142

    
3143
  columns = []
3144
  for fdef in result.fields:
3145
    assert fdef.title and fdef.name
3146
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3147
    columns.append(TableColumn(fdef.title,
3148
                               _QueryColumnFormatter(fn, _RecordStatus,
3149
                                                     verbose),
3150
                               align_right))
3151

    
3152
  table = FormatTable(result.data, columns, header, separator)
3153

    
3154
  # Collect statistics
3155
  assert len(stats) == len(constants.RS_ALL)
3156
  assert compat.all(count >= 0 for count in stats.values())
3157

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

    
3169
  return (status, table)
3170

    
3171

    
3172
def _GetUnknownFields(fdefs):
3173
  """Returns list of unknown fields included in C{fdefs}.
3174

3175
  @type fdefs: list of L{objects.QueryFieldDefinition}
3176

3177
  """
3178
  return [fdef for fdef in fdefs
3179
          if fdef.kind == constants.QFT_UNKNOWN]
3180

    
3181

    
3182
def _WarnUnknownFields(fdefs):
3183
  """Prints a warning to stderr if a query included unknown fields.
3184

3185
  @type fdefs: list of L{objects.QueryFieldDefinition}
3186

3187
  """
3188
  unknown = _GetUnknownFields(fdefs)
3189
  if unknown:
3190
    ToStderr("Warning: Queried for unknown fields %s",
3191
             utils.CommaJoin(fdef.name for fdef in unknown))
3192
    return True
3193

    
3194
  return False
3195

    
3196

    
3197
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3198
                format_override=None, verbose=False, force_filter=False,
3199
                namefield=None, qfilter=None, isnumeric=False):
3200
  """Generic implementation for listing all items of a resource.
3201

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

3232
  """
3233
  if not names:
3234
    names = None
3235

    
3236
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3237
                                isnumeric=isnumeric)
3238

    
3239
  if qfilter is None:
3240
    qfilter = namefilter
3241
  elif namefilter is not None:
3242
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3243

    
3244
  if cl is None:
3245
    cl = GetClient()
3246

    
3247
  response = cl.Query(resource, fields, qfilter)
3248

    
3249
  found_unknown = _WarnUnknownFields(response.fields)
3250

    
3251
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3252
                                     header=header,
3253
                                     format_override=format_override,
3254
                                     verbose=verbose)
3255

    
3256
  for line in data:
3257
    ToStdout(line)
3258

    
3259
  assert ((found_unknown and status == QR_UNKNOWN) or
3260
          (not found_unknown and status != QR_UNKNOWN))
3261

    
3262
  if status == QR_UNKNOWN:
3263
    return constants.EXIT_UNKNOWN_FIELD
3264

    
3265
  # TODO: Should the list command fail if not all data could be collected?
3266
  return constants.EXIT_SUCCESS
3267

    
3268

    
3269
def _FieldDescValues(fdef):
3270
  """Helper function for L{GenericListFields} to get query field description.
3271

3272
  @type fdef: L{objects.QueryFieldDefinition}
3273
  @rtype: list
3274

3275
  """
3276
  return [
3277
    fdef.name,
3278
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3279
    fdef.title,
3280
    fdef.doc,
3281
    ]
3282

    
3283

    
3284
def GenericListFields(resource, fields, separator, header, cl=None):
3285
  """Generic implementation for listing fields for a resource.
3286

3287
  @param resource: One of L{constants.QR_VIA_LUXI}
3288
  @type fields: list of strings
3289
  @param fields: List of fields to query for
3290
  @type separator: string or None
3291
  @param separator: String used to separate fields
3292
  @type header: bool
3293
  @param header: Whether to show header row
3294

3295
  """
3296
  if cl is None:
3297
    cl = GetClient()
3298

    
3299
  if not fields:
3300
    fields = None
3301

    
3302
  response = cl.QueryFields(resource, fields)
3303

    
3304
  found_unknown = _WarnUnknownFields(response.fields)
3305

    
3306
  columns = [
3307
    TableColumn("Name", str, False),
3308
    TableColumn("Type", str, False),
3309
    TableColumn("Title", str, False),
3310
    TableColumn("Description", str, False),
3311
    ]
3312

    
3313
  rows = map(_FieldDescValues, response.fields)
3314

    
3315
  for line in FormatTable(rows, columns, header, separator):
3316
    ToStdout(line)
3317

    
3318
  if found_unknown:
3319
    return constants.EXIT_UNKNOWN_FIELD
3320

    
3321
  return constants.EXIT_SUCCESS
3322

    
3323

    
3324
class TableColumn:
3325
  """Describes a column for L{FormatTable}.
3326

3327
  """
3328
  def __init__(self, title, fn, align_right):
3329
    """Initializes this class.
3330

3331
    @type title: string
3332
    @param title: Column title
3333
    @type fn: callable
3334
    @param fn: Formatting function
3335
    @type align_right: bool
3336
    @param align_right: Whether to align values on the right-hand side
3337

3338
    """
3339
    self.title = title
3340
    self.format = fn
3341
    self.align_right = align_right
3342

    
3343

    
3344
def _GetColFormatString(width, align_right):
3345
  """Returns the format string for a field.
3346

3347
  """
3348
  if align_right:
3349
    sign = ""
3350
  else:
3351
    sign = "-"
3352

    
3353
  return "%%%s%ss" % (sign, width)
3354

    
3355

    
3356
def FormatTable(rows, columns, header, separator):
3357
  """Formats data as a table.
3358

3359
  @type rows: list of lists
3360
  @param rows: Row data, one list per row
3361
  @type columns: list of L{TableColumn}
3362
  @param columns: Column descriptions
3363
  @type header: bool
3364
  @param header: Whether to show header row
3365
  @type separator: string or None
3366
  @param separator: String used to separate columns
3367

3368
  """
3369
  if header:
3370
    data = [[col.title for col in columns]]
3371
    colwidth = [len(col.title) for col in columns]
3372
  else:
3373
    data = []
3374
    colwidth = [0 for _ in columns]
3375

    
3376
  # Format row data
3377
  for row in rows:
3378
    assert len(row) == len(columns)
3379

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

    
3382
    if separator is None:
3383
      # Update column widths
3384
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3385
        # Modifying a list's items while iterating is fine
3386
        colwidth[idx] = max(oldwidth, len(value))
3387

    
3388
    data.append(formatted)
3389

    
3390
  if separator is not None:
3391
    # Return early if a separator is used
3392
    return [separator.join(row) for row in data]
3393

    
3394
  if columns and not columns[-1].align_right:
3395
    # Avoid unnecessary spaces at end of line
3396
    colwidth[-1] = 0
3397

    
3398
  # Build format string
3399
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3400
                  for col, width in zip(columns, colwidth)])
3401

    
3402
  return [fmt % tuple(row) for row in data]
3403

    
3404

    
3405
def FormatTimestamp(ts):
3406
  """Formats a given timestamp.
3407

3408
  @type ts: timestamp
3409
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3410

3411
  @rtype: string
3412
  @return: a string with the formatted timestamp
3413

3414
  """
3415
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3416
    return "?"
3417

    
3418
  (sec, usecs) = ts
3419
  return utils.FormatTime(sec, usecs=usecs)
3420

    
3421

    
3422
def ParseTimespec(value):
3423
  """Parse a time specification.
3424

3425
  The following suffixed will be recognized:
3426

3427
    - s: seconds
3428
    - m: minutes
3429
    - h: hours
3430
    - d: day
3431
    - w: weeks
3432

3433
  Without any suffix, the value will be taken to be in seconds.
3434

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

    
3466

    
3467
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3468
                   filter_master=False, nodegroup=None):
3469
  """Returns the names of online nodes.
3470

3471
  This function will also log a warning on stderr with the names of
3472
  the online nodes.
3473

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

3492
  """
3493
  if cl is None:
3494
    cl = GetClient()
3495

    
3496
  qfilter = []
3497

    
3498
  if nodes:
3499
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3500

    
3501
  if nodegroup is not None:
3502
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3503
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3504

    
3505
  if filter_master:
3506
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3507

    
3508
  if qfilter:
3509
    if len(qfilter) > 1:
3510
      final_filter = [qlang.OP_AND] + qfilter
3511
    else:
3512
      assert len(qfilter) == 1
3513
      final_filter = qfilter[0]
3514
  else:
3515
    final_filter = None
3516

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

    
3519
  def _IsOffline(row):
3520
    (_, (_, offline), _) = row
3521
    return offline
3522

    
3523
  def _GetName(row):
3524
    ((_, name), _, _) = row
3525
    return name
3526

    
3527
  def _GetSip(row):
3528
    (_, _, (_, sip)) = row
3529
    return sip
3530

    
3531
  (offline, online) = compat.partition(result.data, _IsOffline)
3532

    
3533
  if offline and not nowarn:
3534
    ToStderr("Note: skipping offline node(s): %s" %
3535
             utils.CommaJoin(map(_GetName, offline)))
3536

    
3537
  if secondary_ips:
3538
    fn = _GetSip
3539
  else:
3540
    fn = _GetName
3541

    
3542
  return map(fn, online)
3543

    
3544

    
3545
def GetNodesSshPorts(nodes, cl):
3546
  """Retrieves SSH ports of given nodes.
3547

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

    
3560

    
3561
def _ToStream(stream, txt, *args):
3562
  """Write a message to a stream, bypassing the logging system
3563

3564
  @type stream: file object
3565
  @param stream: the file to which we should write
3566
  @type txt: str
3567
  @param txt: the message
3568

3569
  """
3570
  try:
3571
    if args:
3572
      args = tuple(args)
3573
      stream.write(txt % args)
3574
    else:
3575
      stream.write(txt)
3576
    stream.write("\n")
3577
    stream.flush()
3578
  except IOError, err:
3579
    if err.errno == errno.EPIPE:
3580
      # our terminal went away, we'll exit
3581
      sys.exit(constants.EXIT_FAILURE)
3582
    else:
3583
      raise
3584

    
3585

    
3586
def ToStdout(txt, *args):
3587
  """Write a message to stdout only, bypassing the logging system
3588

3589
  This is just a wrapper over _ToStream.
3590

3591
  @type txt: str
3592
  @param txt: the message
3593

3594
  """
3595
  _ToStream(sys.stdout, txt, *args)
3596

    
3597

    
3598
def ToStderr(txt, *args):
3599
  """Write a message to stderr only, bypassing the logging system
3600

3601
  This is just a wrapper over _ToStream.
3602

3603
  @type txt: str
3604
  @param txt: the message
3605

3606
  """
3607
  _ToStream(sys.stderr, txt, *args)
3608

    
3609

    
3610
class JobExecutor(object):
3611
  """Class which manages the submission and execution of multiple jobs.
3612

3613
  Note that instances of this class should not be reused between
3614
  GetResults() calls.
3615

3616
  """
3617
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3618
    self.queue = []
3619
    if cl is None:
3620
      cl = GetClient()
3621
    self.cl = cl
3622
    self.verbose = verbose
3623
    self.jobs = []
3624
    self.opts = opts
3625
    self.feedback_fn = feedback_fn
3626
    self._counter = itertools.count()
3627

    
3628
  @staticmethod
3629
  def _IfName(name, fmt):
3630
    """Helper function for formatting name.
3631

3632
    """
3633
    if name:
3634
      return fmt % name
3635

    
3636
    return ""
3637

    
3638
  def QueueJob(self, name, *ops):
3639
    """Record a job for later submit.
3640

3641
    @type name: string
3642
    @param name: a description of the job, will be used in WaitJobSet
3643

3644
    """
3645
    SetGenericOpcodeOpts(ops, self.opts)
3646
    self.queue.append((self._counter.next(), name, ops))
3647

    
3648
  def AddJobId(self, name, status, job_id):
3649
    """Adds a job ID to the internal queue.
3650

3651
    """
3652
    self.jobs.append((self._counter.next(), status, job_id, name))
3653

    
3654
  def SubmitPending(self, each=False):
3655
    """Submit all pending jobs.
3656

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

    
3669
  def _ChooseJob(self):
3670
    """Choose a non-waiting/queued job to poll next.
3671

3672
    """
3673
    assert self.jobs, "_ChooseJob called with empty job list"
3674

    
3675
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3676
                               ["status"])
3677
    assert result
3678

    
3679
    for job_data, status in zip(self.jobs, result):
3680
      if (isinstance(status, list) and status and
3681
          status[0] in (constants.JOB_STATUS_QUEUED,
3682
                        constants.JOB_STATUS_WAITING,
3683
                        constants.JOB_STATUS_CANCELING)):
3684
        # job is still present and waiting
3685
        continue
3686
      # good candidate found (either running job or lost job)
3687
      self.jobs.remove(job_data)
3688
      return job_data
3689

    
3690
    # no job found
3691
    return self.jobs.pop(0)
3692

    
3693
  def GetResults(self):
3694
    """Wait for and return the results of all jobs.
3695

3696
    @rtype: list
3697
    @return: list of tuples (success, job results), in the same order
3698
        as the submitted jobs; if a job has failed, instead of the result
3699
        there will be the error message
3700

3701
    """
3702
    if not self.jobs:
3703
      self.SubmitPending()
3704
    results = []
3705
    if self.verbose:
3706
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3707
      if ok_jobs:
3708
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3709

    
3710
    # first, remove any non-submitted jobs
3711
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3712
    for idx, _, jid, name in failures:
3713
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3714
      results.append((idx, False, jid))
3715

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

    
3734
      results.append((idx, success, job_result))
3735

    
3736
    # sort based on the index, then drop it
3737
    results.sort()
3738
    results = [i[1:] for i in results]
3739

    
3740
    return results
3741

    
3742
  def WaitOrShow(self, wait):
3743
    """Wait for job results or only print the job IDs.
3744

3745
    @type wait: boolean
3746
    @param wait: whether to wait or not
3747

3748
    """
3749
    if wait:
3750
      return self.GetResults()
3751
    else:
3752
      if not self.jobs:
3753
        self.SubmitPending()
3754
      for _, status, result, name in self.jobs:
3755
        if status:
3756
          ToStdout("%s: %s", result, name)
3757
        else:
3758
          ToStderr("Failure for %s: %s", name, result)
3759
      return [row[1:3] for row in self.jobs]
3760

    
3761

    
3762
def FormatParamsDictInfo(param_dict, actual):
3763
  """Formats a parameter dictionary.
3764

3765
  @type param_dict: dict
3766
  @param param_dict: the own parameters
3767
  @type actual: dict
3768
  @param actual: the current parameter set (including defaults)
3769
  @rtype: dict
3770
  @return: dictionary where the value of each parameter is either a fully
3771
      formatted string or a dictionary containing formatted strings
3772

3773
  """
3774
  ret = {}
3775
  for (key, data) in actual.items():
3776
    if isinstance(data, dict) and data:
3777
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3778
    else:
3779
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3780
  return ret
3781

    
3782

    
3783
def _FormatListInfoDefault(data, def_data):
3784
  if data is not None:
3785
    ret = utils.CommaJoin(data)
3786
  else:
3787
    ret = "default (%s)" % utils.CommaJoin(def_data)
3788
  return ret
3789

    
3790

    
3791
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3792
  """Formats an instance policy.
3793

3794
  @type custom_ipolicy: dict
3795
  @param custom_ipolicy: own policy
3796
  @type eff_ipolicy: dict
3797
  @param eff_ipolicy: effective policy (including defaults); ignored for
3798
      cluster
3799
  @type iscluster: bool
3800
  @param iscluster: the policy is at cluster level
3801
  @rtype: list of pairs
3802
  @return: formatted data, suitable for L{PrintGenericInfo}
3803

3804
  """
3805
  if iscluster:
3806
    eff_ipolicy = custom_ipolicy
3807

    
3808
  minmax_out = []
3809
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3810
  if custom_minmax:
3811
    for (k, minmax) in enumerate(custom_minmax):
3812
      minmax_out.append([
3813
        ("%s/%s" % (key, k),
3814
         FormatParamsDictInfo(minmax[key], minmax[key]))
3815
        for key in constants.ISPECS_MINMAX_KEYS
3816
        ])
3817
  else:
3818
    for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]):
3819
      minmax_out.append([
3820
        ("%s/%s" % (key, k),
3821
         FormatParamsDictInfo({}, minmax[key]))
3822
        for key in constants.ISPECS_MINMAX_KEYS
3823
        ])
3824
  ret = [("bounds specs", minmax_out)]
3825

    
3826
  if iscluster:
3827
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3828
    ret.append(
3829
      (constants.ISPECS_STD,
3830
       FormatParamsDictInfo(stdspecs, stdspecs))
3831
      )
3832

    
3833
  ret.append(
3834
    ("allowed disk templates",
3835
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3836
                            eff_ipolicy[constants.IPOLICY_DTS]))
3837
    )
3838
  ret.extend([
3839
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3840
    for key in constants.IPOLICY_PARAMETERS
3841
    ])
3842
  return ret
3843

    
3844

    
3845
def _PrintSpecsParameters(buf, specs):
3846
  values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items()))
3847
  buf.write(",".join(values))
3848

    
3849

    
3850
def PrintIPolicyCommand(buf, ipolicy, isgroup):
3851
  """Print the command option used to generate the given instance policy.
3852

3853
  Currently only the parts dealing with specs are supported.
3854

3855
  @type buf: StringIO
3856
  @param buf: stream to write into
3857
  @type ipolicy: dict
3858
  @param ipolicy: instance policy
3859
  @type isgroup: bool
3860
  @param isgroup: whether the policy is at group level
3861

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

    
3884

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

3888
  This function is used to request confirmation for doing an operation
3889
  on a given list of list_type.
3890

3891
  @type names: list
3892
  @param names: the list of names that we display when
3893
      we ask for confirmation
3894
  @type list_type: str
3895
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3896
  @type text: str
3897
  @param text: the operation that the user should confirm
3898
  @rtype: boolean
3899
  @return: True or False depending on user's confirmation.
3900

3901
  """
3902
  count = len(names)
3903
  msg = ("The %s will operate on %d %s.\n%s"
3904
         "Do you want to continue?" % (text, count, list_type, extra))
3905
  affected = (("\nAffected %s:\n" % list_type) +
3906
              "\n".join(["  %s" % name for name in names]))
3907

    
3908
  choices = [("y", True, "Yes, execute the %s" % text),
3909
             ("n", False, "No, abort the %s" % text)]
3910

    
3911
  if count > 20:
3912
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3913
    question = msg
3914
  else:
3915
    question = msg + affected
3916

    
3917
  choice = AskUser(question, choices)
3918
  if choice == "v":
3919
    choices.pop(1)
3920
    choice = AskUser(msg + affected, choices)
3921
  return choice
3922

    
3923

    
3924
def _MaybeParseUnit(elements):
3925
  """Parses and returns an array of potential values with units.
3926

3927
  """
3928
  parsed = {}
3929
  for k, v in elements.items():
3930
    if v == constants.VALUE_DEFAULT:
3931
      parsed[k] = v
3932
    else:
3933
      parsed[k] = utils.ParseUnit(v)
3934
  return parsed
3935

    
3936

    
3937
def _InitISpecsFromSplitOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
3938
                             ispecs_disk_count, ispecs_disk_size,
3939
                             ispecs_nic_count, group_ipolicy, fill_all):
3940
  try:
3941
    if ispecs_mem_size:
3942
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
3943
    if ispecs_disk_size:
3944
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
3945
  except (TypeError, ValueError, errors.UnitParseError), err:
3946
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
3947
                               " in policy: %s" %
3948
                               (ispecs_disk_size, ispecs_mem_size, err),
3949
                               errors.ECODE_INVAL)
3950

    
3951
  # prepare ipolicy dict
3952
  ispecs_transposed = {
3953
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
3954
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
3955
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
3956
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
3957
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
3958
    }
3959

    
3960
  # first, check that the values given are correct
3961
  if group_ipolicy:
3962
    forced_type = TISPECS_GROUP_TYPES
3963
  else:
3964
    forced_type = TISPECS_CLUSTER_TYPES
3965
  for specs in ispecs_transposed.values():
3966
    assert type(specs) is dict
3967
    utils.ForceDictType(specs, forced_type)
3968

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

    
3995

    
3996
def _ParseSpecUnit(spec, keyname):
3997
  ret = spec.copy()
3998
  for k in [constants.ISPEC_DISK_SIZE, constants.ISPEC_MEM_SIZE]:
3999
    if k in ret:
4000
      try:
4001
        ret[k] = utils.ParseUnit(ret[k])
4002
      except (TypeError, ValueError, errors.UnitParseError), err:
4003
        raise errors.OpPrereqError(("Invalid parameter %s (%s) in %s instance"
4004
                                    " specs: %s" % (k, ret[k], keyname, err)),
4005
                                   errors.ECODE_INVAL)
4006
  return ret
4007

    
4008

    
4009
def _ParseISpec(spec, keyname, required):
4010
  ret = _ParseSpecUnit(spec, keyname)
4011
  utils.ForceDictType(ret, constants.ISPECS_PARAMETER_TYPES)
4012
  missing = constants.ISPECS_PARAMETERS - frozenset(ret.keys())
4013
  if required and missing:
4014
    raise errors.OpPrereqError("Missing parameters in ipolicy spec %s: %s" %
4015
                               (keyname, utils.CommaJoin(missing)),
4016
                               errors.ECODE_INVAL)
4017
  return ret
4018

    
4019

    
4020
def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
4021
  ret = None
4022
  if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and
4023
      len(minmax_ispecs[0]) == 1):
4024
    for (key, spec) in minmax_ispecs[0].items():
4025
      # This loop is executed exactly once
4026
      if key in allowed_values and not spec:
4027
        ret = key
4028
  return ret
4029

    
4030

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

    
4051

    
4052
def CreateIPolicyFromOpts(ispecs_mem_size=None,
4053
                          ispecs_cpu_count=None,
4054
                          ispecs_disk_count=None,
4055
                          ispecs_disk_size=None,
4056
                          ispecs_nic_count=None,
4057
                          minmax_ispecs=None,
4058
                          std_ispecs=None,
4059
                          ipolicy_disk_templates=None,
4060
                          ipolicy_vcpu_ratio=None,
4061
                          ipolicy_spindle_ratio=None,
4062
                          group_ipolicy=False,
4063
                          allowed_values=None,
4064
                          fill_all=False):
4065
  """Creation of instance policy based on command line options.
4066

4067
  @param fill_all: whether for cluster policies we should ensure that
4068
    all values are filled
4069

4070
  """
4071
  assert not (fill_all and allowed_values)
4072

    
4073
  split_specs = (ispecs_mem_size or ispecs_cpu_count or ispecs_disk_count or
4074
                 ispecs_disk_size or ispecs_nic_count)
4075
  if (split_specs and (minmax_ispecs is not None or std_ispecs is not None)):
4076
    raise errors.OpPrereqError("A --specs-xxx option cannot be specified"
4077
                               " together with any --ipolicy-xxx-specs option",
4078
                               errors.ECODE_INVAL)
4079

    
4080
  ipolicy_out = objects.MakeEmptyIPolicy()
4081
  if split_specs:
4082
    assert fill_all
4083
    _InitISpecsFromSplitOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
4084
                             ispecs_disk_count, ispecs_disk_size,
4085
                             ispecs_nic_count, group_ipolicy, fill_all)
4086
  elif (minmax_ispecs is not None or std_ispecs is not None):
4087
    _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs,
4088
                            group_ipolicy, allowed_values)
4089

    
4090
  if ipolicy_disk_templates is not None:
4091
    if allowed_values and ipolicy_disk_templates in allowed_values:
4092
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
4093
    else:
4094
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
4095
  if ipolicy_vcpu_ratio is not None:
4096
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
4097
  if ipolicy_spindle_ratio is not None:
4098
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
4099

    
4100
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
4101

    
4102
  if not group_ipolicy and fill_all:
4103
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
4104

    
4105
  return ipolicy_out
4106

    
4107

    
4108
def _SerializeGenericInfo(buf, data, level, afterkey=False):
4109
  """Formatting core of L{PrintGenericInfo}.
4110

4111
  @param buf: (string) stream to accumulate the result into
4112
  @param data: data to format
4113
  @type level: int
4114
  @param level: depth in the data hierarchy, used for indenting
4115
  @type afterkey: bool
4116
  @param afterkey: True when we are in the middle of a line after a key (used
4117
      to properly add newlines or indentation)
4118

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

    
4176

    
4177
def PrintGenericInfo(data):
4178
  """Print information formatted according to the hierarchy.
4179

4180
  The output is a valid YAML string.
4181

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

4192
  """
4193
  buf = StringIO()
4194
  _SerializeGenericInfo(buf, data, 0)
4195
  ToStdout(buf.getvalue().rstrip("\n"))