Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 726ae450

History | View | Annotate | Download (129.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
  "HVLIST_OPT",
99
  "HVOPTS_OPT",
100
  "HYPERVISOR_OPT",
101
  "IALLOCATOR_OPT",
102
  "DEFAULT_IALLOCATOR_OPT",
103
  "IDENTIFY_DEFAULTS_OPT",
104
  "IGNORE_CONSIST_OPT",
105
  "IGNORE_ERRORS_OPT",
106
  "IGNORE_FAILURES_OPT",
107
  "IGNORE_OFFLINE_OPT",
108
  "IGNORE_REMOVE_FAILURES_OPT",
109
  "IGNORE_SECONDARIES_OPT",
110
  "IGNORE_SIZE_OPT",
111
  "INTERVAL_OPT",
112
  "MAC_PREFIX_OPT",
113
  "MAINTAIN_NODE_HEALTH_OPT",
114
  "MASTER_NETDEV_OPT",
115
  "MASTER_NETMASK_OPT",
116
  "MC_OPT",
117
  "MIGRATION_MODE_OPT",
118
  "NET_OPT",
119
  "NETWORK_OPT",
120
  "NETWORK6_OPT",
121
  "NEW_CLUSTER_CERT_OPT",
122
  "NEW_CLUSTER_DOMAIN_SECRET_OPT",
123
  "NEW_CONFD_HMAC_KEY_OPT",
124
  "NEW_RAPI_CERT_OPT",
125
  "NEW_PRIMARY_OPT",
126
  "NEW_SECONDARY_OPT",
127
  "NEW_SPICE_CERT_OPT",
128
  "NIC_PARAMS_OPT",
129
  "NOCONFLICTSCHECK_OPT",
130
  "NODE_FORCE_JOIN_OPT",
131
  "NODE_LIST_OPT",
132
  "NODE_PLACEMENT_OPT",
133
  "NODEGROUP_OPT",
134
  "NODE_PARAMS_OPT",
135
  "NODE_POWERED_OPT",
136
  "NODRBD_STORAGE_OPT",
137
  "NOHDR_OPT",
138
  "NOIPCHECK_OPT",
139
  "NO_INSTALL_OPT",
140
  "NONAMECHECK_OPT",
141
  "NOLVM_STORAGE_OPT",
142
  "NOMODIFY_ETCHOSTS_OPT",
143
  "NOMODIFY_SSH_SETUP_OPT",
144
  "NONICS_OPT",
145
  "NONLIVE_OPT",
146
  "NONPLUS1_OPT",
147
  "NORUNTIME_CHGS_OPT",
148
  "NOSHUTDOWN_OPT",
149
  "NOSTART_OPT",
150
  "NOSSH_KEYCHECK_OPT",
151
  "NOVOTING_OPT",
152
  "NO_REMEMBER_OPT",
153
  "NWSYNC_OPT",
154
  "OFFLINE_INST_OPT",
155
  "ONLINE_INST_OPT",
156
  "ON_PRIMARY_OPT",
157
  "ON_SECONDARY_OPT",
158
  "OFFLINE_OPT",
159
  "OSPARAMS_OPT",
160
  "OS_OPT",
161
  "OS_SIZE_OPT",
162
  "OOB_TIMEOUT_OPT",
163
  "POWER_DELAY_OPT",
164
  "PREALLOC_WIPE_DISKS_OPT",
165
  "PRIMARY_IP_VERSION_OPT",
166
  "PRIMARY_ONLY_OPT",
167
  "PRIORITY_OPT",
168
  "RAPI_CERT_OPT",
169
  "READD_OPT",
170
  "REASON_OPT",
171
  "REBOOT_TYPE_OPT",
172
  "REMOVE_INSTANCE_OPT",
173
  "REMOVE_RESERVED_IPS_OPT",
174
  "REMOVE_UIDS_OPT",
175
  "RESERVED_LVS_OPT",
176
  "RUNTIME_MEM_OPT",
177
  "ROMAN_OPT",
178
  "SECONDARY_IP_OPT",
179
  "SECONDARY_ONLY_OPT",
180
  "SELECT_OS_OPT",
181
  "SEP_OPT",
182
  "SHOWCMD_OPT",
183
  "SHOW_MACHINE_OPT",
184
  "SHUTDOWN_TIMEOUT_OPT",
185
  "SINGLE_NODE_OPT",
186
  "SPECS_CPU_COUNT_OPT",
187
  "SPECS_DISK_COUNT_OPT",
188
  "SPECS_DISK_SIZE_OPT",
189
  "SPECS_MEM_SIZE_OPT",
190
  "SPECS_NIC_COUNT_OPT",
191
  "IPOLICY_DISK_TEMPLATES",
192
  "IPOLICY_VCPU_RATIO",
193
  "SPICE_CACERT_OPT",
194
  "SPICE_CERT_OPT",
195
  "SRC_DIR_OPT",
196
  "SRC_NODE_OPT",
197
  "SUBMIT_OPT",
198
  "STARTUP_PAUSED_OPT",
199
  "STATIC_OPT",
200
  "SYNC_OPT",
201
  "TAG_ADD_OPT",
202
  "TAG_SRC_OPT",
203
  "TIMEOUT_OPT",
204
  "TO_GROUP_OPT",
205
  "UIDPOOL_OPT",
206
  "USEUNITS_OPT",
207
  "USE_EXTERNAL_MIP_SCRIPT",
208
  "USE_REPL_NET_OPT",
209
  "VERBOSE_OPT",
210
  "VG_NAME_OPT",
211
  "WFSYNC_OPT",
212
  "YES_DOIT_OPT",
213
  "DISK_STATE_OPT",
214
  "HV_STATE_OPT",
215
  "IGNORE_IPOLICY_OPT",
216
  "INSTANCE_POLICY_OPTS",
217
  # Generic functions for CLI programs
218
  "ConfirmOperation",
219
  "CreateIPolicyFromOpts",
220
  "GenericMain",
221
  "GenericInstanceCreate",
222
  "GenericList",
223
  "GenericListFields",
224
  "GetClient",
225
  "GetOnlineNodes",
226
  "JobExecutor",
227
  "JobSubmittedException",
228
  "ParseTimespec",
229
  "RunWhileClusterStopped",
230
  "SubmitOpCode",
231
  "SubmitOrSend",
232
  "UsesRPC",
233
  # Formatting functions
234
  "ToStderr", "ToStdout",
235
  "FormatError",
236
  "FormatQueryResult",
237
  "FormatParamsDictInfo",
238
  "FormatPolicyInfo",
239
  "PrintGenericInfo",
240
  "GenerateTable",
241
  "AskUser",
242
  "FormatTimestamp",
243
  "FormatLogMessage",
244
  # Tags functions
245
  "ListTags",
246
  "AddTags",
247
  "RemoveTags",
248
  # command line options support infrastructure
249
  "ARGS_MANY_INSTANCES",
250
  "ARGS_MANY_NODES",
251
  "ARGS_MANY_GROUPS",
252
  "ARGS_MANY_NETWORKS",
253
  "ARGS_NONE",
254
  "ARGS_ONE_INSTANCE",
255
  "ARGS_ONE_NODE",
256
  "ARGS_ONE_GROUP",
257
  "ARGS_ONE_OS",
258
  "ARGS_ONE_NETWORK",
259
  "ArgChoice",
260
  "ArgCommand",
261
  "ArgFile",
262
  "ArgGroup",
263
  "ArgHost",
264
  "ArgInstance",
265
  "ArgJobId",
266
  "ArgNetwork",
267
  "ArgNode",
268
  "ArgOs",
269
  "ArgExtStorage",
270
  "ArgSuggest",
271
  "ArgUnknown",
272
  "OPT_COMPL_INST_ADD_NODES",
273
  "OPT_COMPL_MANY_NODES",
274
  "OPT_COMPL_ONE_IALLOCATOR",
275
  "OPT_COMPL_ONE_INSTANCE",
276
  "OPT_COMPL_ONE_NODE",
277
  "OPT_COMPL_ONE_NODEGROUP",
278
  "OPT_COMPL_ONE_NETWORK",
279
  "OPT_COMPL_ONE_OS",
280
  "OPT_COMPL_ONE_EXTSTORAGE",
281
  "cli_option",
282
  "SplitNodeOption",
283
  "CalculateOSNames",
284
  "ParseFields",
285
  "COMMON_CREATE_OPTS",
286
  ]
287

    
288
NO_PREFIX = "no_"
289
UN_PREFIX = "-"
290

    
291
#: Priorities (sorted)
292
_PRIORITY_NAMES = [
293
  ("low", constants.OP_PRIO_LOW),
294
  ("normal", constants.OP_PRIO_NORMAL),
295
  ("high", constants.OP_PRIO_HIGH),
296
  ]
297

    
298
#: Priority dictionary for easier lookup
299
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
300
# we migrate to Python 2.6
301
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
302

    
303
# Query result status for clients
304
(QR_NORMAL,
305
 QR_UNKNOWN,
306
 QR_INCOMPLETE) = range(3)
307

    
308
#: Maximum batch size for ChooseJob
309
_CHOOSE_BATCH = 25
310

    
311

    
312
# constants used to create InstancePolicy dictionary
313
TISPECS_GROUP_TYPES = {
314
  constants.ISPECS_MIN: constants.VTYPE_INT,
315
  constants.ISPECS_MAX: constants.VTYPE_INT,
316
  }
317

    
318
TISPECS_CLUSTER_TYPES = {
319
  constants.ISPECS_MIN: constants.VTYPE_INT,
320
  constants.ISPECS_MAX: constants.VTYPE_INT,
321
  constants.ISPECS_STD: constants.VTYPE_INT,
322
  }
323

    
324
#: User-friendly names for query2 field types
325
_QFT_NAMES = {
326
  constants.QFT_UNKNOWN: "Unknown",
327
  constants.QFT_TEXT: "Text",
328
  constants.QFT_BOOL: "Boolean",
329
  constants.QFT_NUMBER: "Number",
330
  constants.QFT_UNIT: "Storage size",
331
  constants.QFT_TIMESTAMP: "Timestamp",
332
  constants.QFT_OTHER: "Custom",
333
  }
334

    
335

    
336
class _Argument:
337
  def __init__(self, min=0, max=None): # pylint: disable=W0622
338
    self.min = min
339
    self.max = max
340

    
341
  def __repr__(self):
342
    return ("<%s min=%s max=%s>" %
343
            (self.__class__.__name__, self.min, self.max))
344

    
345

    
346
class ArgSuggest(_Argument):
347
  """Suggesting argument.
348

349
  Value can be any of the ones passed to the constructor.
350

351
  """
352
  # pylint: disable=W0622
353
  def __init__(self, min=0, max=None, choices=None):
354
    _Argument.__init__(self, min=min, max=max)
355
    self.choices = choices
356

    
357
  def __repr__(self):
358
    return ("<%s min=%s max=%s choices=%r>" %
359
            (self.__class__.__name__, self.min, self.max, self.choices))
360

    
361

    
362
class ArgChoice(ArgSuggest):
363
  """Choice argument.
364

365
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
366
  but value must be one of the choices.
367

368
  """
369

    
370

    
371
class ArgUnknown(_Argument):
372
  """Unknown argument to program (e.g. determined at runtime).
373

374
  """
375

    
376

    
377
class ArgInstance(_Argument):
378
  """Instances argument.
379

380
  """
381

    
382

    
383
class ArgNode(_Argument):
384
  """Node argument.
385

386
  """
387

    
388

    
389
class ArgNetwork(_Argument):
390
  """Network argument.
391

392
  """
393

    
394

    
395
class ArgGroup(_Argument):
396
  """Node group argument.
397

398
  """
399

    
400

    
401
class ArgJobId(_Argument):
402
  """Job ID argument.
403

404
  """
405

    
406

    
407
class ArgFile(_Argument):
408
  """File path argument.
409

410
  """
411

    
412

    
413
class ArgCommand(_Argument):
414
  """Command argument.
415

416
  """
417

    
418

    
419
class ArgHost(_Argument):
420
  """Host argument.
421

422
  """
423

    
424

    
425
class ArgOs(_Argument):
426
  """OS argument.
427

428
  """
429

    
430

    
431
class ArgExtStorage(_Argument):
432
  """ExtStorage argument.
433

434
  """
435

    
436

    
437
ARGS_NONE = []
438
ARGS_MANY_INSTANCES = [ArgInstance()]
439
ARGS_MANY_NETWORKS = [ArgNetwork()]
440
ARGS_MANY_NODES = [ArgNode()]
441
ARGS_MANY_GROUPS = [ArgGroup()]
442
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
443
ARGS_ONE_NETWORK = [ArgNetwork(min=1, max=1)]
444
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
445
# TODO
446
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
447
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
448

    
449

    
450
def _ExtractTagsObject(opts, args):
451
  """Extract the tag type object.
452

453
  Note that this function will modify its args parameter.
454

455
  """
456
  if not hasattr(opts, "tag_type"):
457
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
458
  kind = opts.tag_type
459
  if kind == constants.TAG_CLUSTER:
460
    retval = kind, None
461
  elif kind in (constants.TAG_NODEGROUP,
462
                constants.TAG_NODE,
463
                constants.TAG_NETWORK,
464
                constants.TAG_INSTANCE):
465
    if not args:
466
      raise errors.OpPrereqError("no arguments passed to the command",
467
                                 errors.ECODE_INVAL)
468
    name = args.pop(0)
469
    retval = kind, name
470
  else:
471
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
472
  return retval
473

    
474

    
475
def _ExtendTags(opts, args):
476
  """Extend the args if a source file has been given.
477

478
  This function will extend the tags with the contents of the file
479
  passed in the 'tags_source' attribute of the opts parameter. A file
480
  named '-' will be replaced by stdin.
481

482
  """
483
  fname = opts.tags_source
484
  if fname is None:
485
    return
486
  if fname == "-":
487
    new_fh = sys.stdin
488
  else:
489
    new_fh = open(fname, "r")
490
  new_data = []
491
  try:
492
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
493
    # because of python bug 1633941
494
    while True:
495
      line = new_fh.readline()
496
      if not line:
497
        break
498
      new_data.append(line.strip())
499
  finally:
500
    new_fh.close()
501
  args.extend(new_data)
502

    
503

    
504
def ListTags(opts, args):
505
  """List the tags on a given object.
506

507
  This is a generic implementation that knows how to deal with all
508
  three cases of tag objects (cluster, node, instance). The opts
509
  argument is expected to contain a tag_type field denoting what
510
  object type we work on.
511

512
  """
513
  kind, name = _ExtractTagsObject(opts, args)
514
  cl = GetClient(query=True)
515
  result = cl.QueryTags(kind, name)
516
  result = list(result)
517
  result.sort()
518
  for tag in result:
519
    ToStdout(tag)
520

    
521

    
522
def AddTags(opts, args):
523
  """Add tags on a given object.
524

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

530
  """
531
  kind, name = _ExtractTagsObject(opts, args)
532
  _ExtendTags(opts, args)
533
  if not args:
534
    raise errors.OpPrereqError("No tags to be added", errors.ECODE_INVAL)
535
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
536
  SubmitOrSend(op, opts)
537

    
538

    
539
def RemoveTags(opts, args):
540
  """Remove tags from a given object.
541

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

547
  """
548
  kind, name = _ExtractTagsObject(opts, args)
549
  _ExtendTags(opts, args)
550
  if not args:
551
    raise errors.OpPrereqError("No tags to be removed", errors.ECODE_INVAL)
552
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
553
  SubmitOrSend(op, opts)
554

    
555

    
556
def check_unit(option, opt, value): # pylint: disable=W0613
557
  """OptParsers custom converter for units.
558

559
  """
560
  try:
561
    return utils.ParseUnit(value)
562
  except errors.UnitParseError, err:
563
    raise OptionValueError("option %s: %s" % (opt, err))
564

    
565

    
566
def _SplitKeyVal(opt, data, parse_prefixes):
567
  """Convert a KeyVal string into a dict.
568

569
  This function will convert a key=val[,...] string into a dict. Empty
570
  values will be converted specially: keys which have the prefix 'no_'
571
  will have the value=False and the prefix stripped, keys with the prefix
572
  "-" will have value=None and the prefix stripped, and the others will
573
  have value=True.
574

575
  @type opt: string
576
  @param opt: a string holding the option name for which we process the
577
      data, used in building error messages
578
  @type data: string
579
  @param data: a string of the format key=val,key=val,...
580
  @type parse_prefixes: bool
581
  @param parse_prefixes: whether to handle prefixes specially
582
  @rtype: dict
583
  @return: {key=val, key=val}
584
  @raises errors.ParameterError: if there are duplicate keys
585

586
  """
587
  kv_dict = {}
588
  if data:
589
    for elem in utils.UnescapeAndSplit(data, sep=","):
590
      if "=" in elem:
591
        key, val = elem.split("=", 1)
592
      elif parse_prefixes:
593
        if elem.startswith(NO_PREFIX):
594
          key, val = elem[len(NO_PREFIX):], False
595
        elif elem.startswith(UN_PREFIX):
596
          key, val = elem[len(UN_PREFIX):], None
597
        else:
598
          key, val = elem, True
599
      else:
600
        raise errors.ParameterError("Missing value for key '%s' in option %s" %
601
                                    (elem, opt))
602
      if key in kv_dict:
603
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
604
                                    (key, opt))
605
      kv_dict[key] = val
606
  return kv_dict
607

    
608

    
609
def _SplitIdentKeyVal(opt, value, parse_prefixes):
610
  """Helper function to parse "ident:key=val,key=val" options.
611

612
  @type opt: string
613
  @param opt: option name, used in error messages
614
  @type value: string
615
  @param value: expected to be in the format "ident:key=val,key=val,..."
616
  @type parse_prefixes: bool
617
  @param parse_prefixes: whether to handle prefixes specially (see
618
      L{_SplitKeyVal})
619
  @rtype: tuple
620
  @return: (ident, {key=val, key=val})
621
  @raises errors.ParameterError: in case of duplicates or other parsing errors
622

623
  """
624
  if ":" not in value:
625
    ident, rest = value, ""
626
  else:
627
    ident, rest = value.split(":", 1)
628

    
629
  if parse_prefixes and ident.startswith(NO_PREFIX):
630
    if rest:
631
      msg = "Cannot pass options when removing parameter groups: %s" % value
632
      raise errors.ParameterError(msg)
633
    retval = (ident[len(NO_PREFIX):], False)
634
  elif (parse_prefixes and ident.startswith(UN_PREFIX) and
635
        (len(ident) <= len(UN_PREFIX) or not ident[len(UN_PREFIX)].isdigit())):
636
    if rest:
637
      msg = "Cannot pass options when removing parameter groups: %s" % value
638
      raise errors.ParameterError(msg)
639
    retval = (ident[len(UN_PREFIX):], None)
640
  else:
641
    kv_dict = _SplitKeyVal(opt, rest, parse_prefixes)
642
    retval = (ident, kv_dict)
643
  return retval
644

    
645

    
646
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
647
  """Custom parser for ident:key=val,key=val options.
648

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

652
  """
653
  return _SplitIdentKeyVal(opt, value, True)
654

    
655

    
656
def check_key_val(option, opt, value):  # pylint: disable=W0613
657
  """Custom parser class for key=val,key=val options.
658

659
  This will store the parsed values as a dict {key: val}.
660

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

    
664

    
665
def _SplitListKeyVal(opt, value):
666
  retval = {}
667
  for elem in value.split("/"):
668
    if not elem:
669
      raise errors.ParameterError("Empty section in option '%s'" % opt)
670
    (ident, valdict) = _SplitIdentKeyVal(opt, elem, False)
671
    if ident in retval:
672
      msg = ("Duplicated parameter '%s' in parsing %s: %s" %
673
             (ident, opt, elem))
674
      raise errors.ParameterError(msg)
675
    retval[ident] = valdict
676
  return retval
677

    
678

    
679
def check_list_ident_key_val(_, opt, value):
680
  """Custom parser for "ident:key=val,key=val/ident:key=val" options.
681

682
  @rtype: list of dictionary
683
  @return: {ident: {key: val, key: val}, ident: {key: val}}
684

685
  """
686
  return _SplitListKeyVal(opt, value)
687

    
688

    
689
def check_bool(option, opt, value): # pylint: disable=W0613
690
  """Custom parser for yes/no options.
691

692
  This will store the parsed value as either True or False.
693

694
  """
695
  value = value.lower()
696
  if value == constants.VALUE_FALSE or value == "no":
697
    return False
698
  elif value == constants.VALUE_TRUE or value == "yes":
699
    return True
700
  else:
701
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
702

    
703

    
704
def check_list(option, opt, value): # pylint: disable=W0613
705
  """Custom parser for comma-separated lists.
706

707
  """
708
  # we have to make this explicit check since "".split(",") is [""],
709
  # not an empty list :(
710
  if not value:
711
    return []
712
  else:
713
    return utils.UnescapeAndSplit(value)
714

    
715

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

719
  """
720
  value = value.lower()
721

    
722
  if value == constants.VALUE_DEFAULT:
723
    return value
724
  else:
725
    return float(value)
726

    
727

    
728
# completion_suggestion is normally a list. Using numeric values not evaluating
729
# to False for dynamic completion.
730
(OPT_COMPL_MANY_NODES,
731
 OPT_COMPL_ONE_NODE,
732
 OPT_COMPL_ONE_INSTANCE,
733
 OPT_COMPL_ONE_OS,
734
 OPT_COMPL_ONE_EXTSTORAGE,
735
 OPT_COMPL_ONE_IALLOCATOR,
736
 OPT_COMPL_ONE_NETWORK,
737
 OPT_COMPL_INST_ADD_NODES,
738
 OPT_COMPL_ONE_NODEGROUP) = range(100, 109)
739

    
740
OPT_COMPL_ALL = compat.UniqueFrozenset([
741
  OPT_COMPL_MANY_NODES,
742
  OPT_COMPL_ONE_NODE,
743
  OPT_COMPL_ONE_INSTANCE,
744
  OPT_COMPL_ONE_OS,
745
  OPT_COMPL_ONE_EXTSTORAGE,
746
  OPT_COMPL_ONE_IALLOCATOR,
747
  OPT_COMPL_ONE_NETWORK,
748
  OPT_COMPL_INST_ADD_NODES,
749
  OPT_COMPL_ONE_NODEGROUP,
750
  ])
751

    
752

    
753
class CliOption(Option):
754
  """Custom option class for optparse.
755

756
  """
757
  ATTRS = Option.ATTRS + [
758
    "completion_suggest",
759
    ]
760
  TYPES = Option.TYPES + (
761
    "listidentkeyval",
762
    "identkeyval",
763
    "keyval",
764
    "unit",
765
    "bool",
766
    "list",
767
    "maybefloat",
768
    )
769
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
770
  TYPE_CHECKER["listidentkeyval"] = check_list_ident_key_val
771
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
772
  TYPE_CHECKER["keyval"] = check_key_val
773
  TYPE_CHECKER["unit"] = check_unit
774
  TYPE_CHECKER["bool"] = check_bool
775
  TYPE_CHECKER["list"] = check_list
776
  TYPE_CHECKER["maybefloat"] = check_maybefloat
777

    
778

    
779
# optparse.py sets make_option, so we do it for our own option class, too
780
cli_option = CliOption
781

    
782

    
783
_YORNO = "yes|no"
784

    
785
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
786
                       help="Increase debugging level")
787

    
788
NOHDR_OPT = cli_option("--no-headers", default=False,
789
                       action="store_true", dest="no_headers",
790
                       help="Don't display column headers")
791

    
792
SEP_OPT = cli_option("--separator", default=None,
793
                     action="store", dest="separator",
794
                     help=("Separator between output fields"
795
                           " (defaults to one space)"))
796

    
797
USEUNITS_OPT = cli_option("--units", default=None,
798
                          dest="units", choices=("h", "m", "g", "t"),
799
                          help="Specify units for output (one of h/m/g/t)")
800

    
801
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
802
                        type="string", metavar="FIELDS",
803
                        help="Comma separated list of output fields")
804

    
805
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
806
                       default=False, help="Force the operation")
807

    
808
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
809
                         default=False, help="Do not require confirmation")
810

    
811
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
812
                                  action="store_true", default=False,
813
                                  help=("Ignore offline nodes and do as much"
814
                                        " as possible"))
815

    
816
TAG_ADD_OPT = cli_option("--tags", dest="tags",
817
                         default=None, help="Comma-separated list of instance"
818
                                            " tags")
819

    
820
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
821
                         default=None, help="File with tag names")
822

    
823
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
824
                        default=False, action="store_true",
825
                        help=("Submit the job and return the job ID, but"
826
                              " don't wait for the job to finish"))
827

    
828
SYNC_OPT = cli_option("--sync", dest="do_locking",
829
                      default=False, action="store_true",
830
                      help=("Grab locks while doing the queries"
831
                            " in order to ensure more consistent results"))
832

    
833
DRY_RUN_OPT = cli_option("--dry-run", default=False,
834
                         action="store_true",
835
                         help=("Do not execute the operation, just run the"
836
                               " check steps and verify if it could be"
837
                               " executed"))
838

    
839
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
840
                         action="store_true",
841
                         help="Increase the verbosity of the operation")
842

    
843
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
844
                              action="store_true", dest="simulate_errors",
845
                              help="Debugging option that makes the operation"
846
                              " treat most runtime checks as failed")
847

    
848
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
849
                        default=True, action="store_false",
850
                        help="Don't wait for sync (DANGEROUS!)")
851

    
852
WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync",
853
                        default=False, action="store_true",
854
                        help="Wait for disks to sync")
855

    
856
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
857
                             action="store_true", default=False,
858
                             help="Enable offline instance")
859

    
860
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
861
                              action="store_true", default=False,
862
                              help="Disable down instance")
863

    
864
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
865
                               help=("Custom disk setup (%s)" %
866
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
867
                               default=None, metavar="TEMPL",
868
                               choices=list(constants.DISK_TEMPLATES))
869

    
870
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
871
                        help="Do not create any network cards for"
872
                        " the instance")
873

    
874
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
875
                               help="Relative path under default cluster-wide"
876
                               " file storage dir to store file-based disks",
877
                               default=None, metavar="<DIR>")
878

    
879
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
880
                                  help="Driver to use for image files",
881
                                  default="loop", metavar="<DRIVER>",
882
                                  choices=list(constants.FILE_DRIVER))
883

    
884
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
885
                            help="Select nodes for the instance automatically"
886
                            " using the <NAME> iallocator plugin",
887
                            default=None, type="string",
888
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
889

    
890
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
891
                                    metavar="<NAME>",
892
                                    help="Set the default instance"
893
                                    " allocator plugin",
894
                                    default=None, type="string",
895
                                    completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
896

    
897
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
898
                    metavar="<os>",
899
                    completion_suggest=OPT_COMPL_ONE_OS)
900

    
901
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
902
                          type="keyval", default={},
903
                          help="OS parameters")
904

    
905
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
906
                               action="store_true", default=False,
907
                               help="Force an unknown variant")
908

    
909
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
910
                            action="store_true", default=False,
911
                            help="Do not install the OS (will"
912
                            " enable no-start)")
913

    
914
NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes",
915
                                dest="allow_runtime_chgs",
916
                                default=True, action="store_false",
917
                                help="Don't allow runtime changes")
918

    
919
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
920
                         type="keyval", default={},
921
                         help="Backend parameters")
922

    
923
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
924
                        default={}, dest="hvparams",
925
                        help="Hypervisor parameters")
926

    
927
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
928
                             help="Disk template parameters, in the format"
929
                             " template:option=value,option=value,...",
930
                             type="identkeyval", action="append", default=[])
931

    
932
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
933
                                 type="keyval", default={},
934
                                 help="Memory size specs: list of key=value,"
935
                                " where key is one of min, max, std"
936
                                 " (in MB or using a unit)")
937

    
938
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
939
                                 type="keyval", default={},
940
                                 help="CPU count specs: list of key=value,"
941
                                 " where key is one of min, max, std")
942

    
943
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
944
                                  dest="ispecs_disk_count",
945
                                  type="keyval", default={},
946
                                  help="Disk count specs: list of key=value,"
947
                                  " where key is one of min, max, std")
948

    
949
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
950
                                 type="keyval", default={},
951
                                 help="Disk 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_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
956
                                 type="keyval", default={},
957
                                 help="NIC count specs: list of key=value,"
958
                                 " where key is one of min, max, std")
959

    
960
IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
961
                                    dest="ipolicy_disk_templates",
962
                                    type="list", default=None,
963
                                    help="Comma-separated list of"
964
                                    " enabled disk templates")
965

    
966
IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio",
967
                                 dest="ipolicy_vcpu_ratio",
968
                                 type="maybefloat", default=None,
969
                                 help="The maximum allowed vcpu-to-cpu ratio")
970

    
971
IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio",
972
                                   dest="ipolicy_spindle_ratio",
973
                                   type="maybefloat", default=None,
974
                                   help=("The maximum allowed instances to"
975
                                         " spindle ratio"))
976

    
977
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
978
                            help="Hypervisor and hypervisor options, in the"
979
                            " format hypervisor:option=value,option=value,...",
980
                            default=None, type="identkeyval")
981

    
982
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
983
                        help="Hypervisor and hypervisor options, in the"
984
                        " format hypervisor:option=value,option=value,...",
985
                        default=[], action="append", type="identkeyval")
986

    
987
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
988
                           action="store_false",
989
                           help="Don't check that the instance's IP"
990
                           " is alive")
991

    
992
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
993
                             default=True, action="store_false",
994
                             help="Don't check that the instance's name"
995
                             " is resolvable")
996

    
997
NET_OPT = cli_option("--net",
998
                     help="NIC parameters", default=[],
999
                     dest="nics", action="append", type="identkeyval")
1000

    
1001
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
1002
                      dest="disks", action="append", type="identkeyval")
1003

    
1004
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
1005
                         help="Comma-separated list of disks"
1006
                         " indices to act on (e.g. 0,2) (optional,"
1007
                         " defaults to all disks)")
1008

    
1009
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
1010
                         help="Enforces a single-disk configuration using the"
1011
                         " given disk size, in MiB unless a suffix is used",
1012
                         default=None, type="unit", metavar="<size>")
1013

    
1014
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
1015
                                dest="ignore_consistency",
1016
                                action="store_true", default=False,
1017
                                help="Ignore the consistency of the disks on"
1018
                                " the secondary")
1019

    
1020
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
1021
                                dest="allow_failover",
1022
                                action="store_true", default=False,
1023
                                help="If migration is not possible fallback to"
1024
                                     " failover")
1025

    
1026
NONLIVE_OPT = cli_option("--non-live", dest="live",
1027
                         default=True, action="store_false",
1028
                         help="Do a non-live migration (this usually means"
1029
                         " freeze the instance, save the state, transfer and"
1030
                         " only then resume running on the secondary node)")
1031

    
1032
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
1033
                                default=None,
1034
                                choices=list(constants.HT_MIGRATION_MODES),
1035
                                help="Override default migration mode (choose"
1036
                                " either live or non-live")
1037

    
1038
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
1039
                                help="Target node and optional secondary node",
1040
                                metavar="<pnode>[:<snode>]",
1041
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
1042

    
1043
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
1044
                           action="append", metavar="<node>",
1045
                           help="Use only this node (can be used multiple"
1046
                           " times, if not given defaults to all nodes)",
1047
                           completion_suggest=OPT_COMPL_ONE_NODE)
1048

    
1049
NODEGROUP_OPT_NAME = "--node-group"
1050
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
1051
                           dest="nodegroup",
1052
                           help="Node group (name or uuid)",
1053
                           metavar="<nodegroup>",
1054
                           default=None, type="string",
1055
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1056

    
1057
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
1058
                             metavar="<node>",
1059
                             completion_suggest=OPT_COMPL_ONE_NODE)
1060

    
1061
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
1062
                         action="store_false",
1063
                         help="Don't start the instance after creation")
1064

    
1065
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
1066
                         action="store_true", default=False,
1067
                         help="Show command instead of executing it")
1068

    
1069
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
1070
                         default=False, action="store_true",
1071
                         help="Instead of performing the migration, try to"
1072
                         " recover from a failed cleanup. This is safe"
1073
                         " to run even if the instance is healthy, but it"
1074
                         " will create extra replication traffic and "
1075
                         " disrupt briefly the replication (like during the"
1076
                         " migration")
1077

    
1078
STATIC_OPT = cli_option("-s", "--static", dest="static",
1079
                        action="store_true", default=False,
1080
                        help="Only show configuration data, not runtime data")
1081

    
1082
ALL_OPT = cli_option("--all", dest="show_all",
1083
                     default=False, action="store_true",
1084
                     help="Show info on all instances on the cluster."
1085
                     " This can take a long time to run, use wisely")
1086

    
1087
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
1088
                           action="store_true", default=False,
1089
                           help="Interactive OS reinstall, lists available"
1090
                           " OS templates for selection")
1091

    
1092
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
1093
                                 action="store_true", default=False,
1094
                                 help="Remove the instance from the cluster"
1095
                                 " configuration even if there are failures"
1096
                                 " during the removal process")
1097

    
1098
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
1099
                                        dest="ignore_remove_failures",
1100
                                        action="store_true", default=False,
1101
                                        help="Remove the instance from the"
1102
                                        " cluster configuration even if there"
1103
                                        " are failures during the removal"
1104
                                        " process")
1105

    
1106
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
1107
                                 action="store_true", default=False,
1108
                                 help="Remove the instance from the cluster")
1109

    
1110
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
1111
                               help="Specifies the new node for the instance",
1112
                               metavar="NODE", default=None,
1113
                               completion_suggest=OPT_COMPL_ONE_NODE)
1114

    
1115
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
1116
                               help="Specifies the new secondary node",
1117
                               metavar="NODE", default=None,
1118
                               completion_suggest=OPT_COMPL_ONE_NODE)
1119

    
1120
NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node",
1121
                             help="Specifies the new primary node",
1122
                             metavar="<node>", default=None,
1123
                             completion_suggest=OPT_COMPL_ONE_NODE)
1124

    
1125
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
1126
                            default=False, action="store_true",
1127
                            help="Replace the disk(s) on the primary"
1128
                                 " node (applies only to internally mirrored"
1129
                                 " disk templates, e.g. %s)" %
1130
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
1131

    
1132
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
1133
                              default=False, action="store_true",
1134
                              help="Replace the disk(s) on the secondary"
1135
                                   " node (applies only to internally mirrored"
1136
                                   " disk templates, e.g. %s)" %
1137
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1138

    
1139
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
1140
                              default=False, action="store_true",
1141
                              help="Lock all nodes and auto-promote as needed"
1142
                              " to MC status")
1143

    
1144
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
1145
                              default=False, action="store_true",
1146
                              help="Automatically replace faulty disks"
1147
                                   " (applies only to internally mirrored"
1148
                                   " disk templates, e.g. %s)" %
1149
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1150

    
1151
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
1152
                             default=False, action="store_true",
1153
                             help="Ignore current recorded size"
1154
                             " (useful for forcing activation when"
1155
                             " the recorded size is wrong)")
1156

    
1157
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
1158
                          metavar="<node>",
1159
                          completion_suggest=OPT_COMPL_ONE_NODE)
1160

    
1161
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
1162
                         metavar="<dir>")
1163

    
1164
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
1165
                              help="Specify the secondary ip for the node",
1166
                              metavar="ADDRESS", default=None)
1167

    
1168
READD_OPT = cli_option("--readd", dest="readd",
1169
                       default=False, action="store_true",
1170
                       help="Readd old node after replacing it")
1171

    
1172
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1173
                                default=True, action="store_false",
1174
                                help="Disable SSH key fingerprint checking")
1175

    
1176
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1177
                                 default=False, action="store_true",
1178
                                 help="Force the joining of a node")
1179

    
1180
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1181
                    type="bool", default=None, metavar=_YORNO,
1182
                    help="Set the master_candidate flag on the node")
1183

    
1184
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1185
                         type="bool", default=None,
1186
                         help=("Set the offline flag on the node"
1187
                               " (cluster does not communicate with offline"
1188
                               " nodes)"))
1189

    
1190
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1191
                         type="bool", default=None,
1192
                         help=("Set the drained flag on the node"
1193
                               " (excluded from allocation operations)"))
1194

    
1195
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1196
                              type="bool", default=None, metavar=_YORNO,
1197
                              help="Set the master_capable flag on the node")
1198

    
1199
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1200
                          type="bool", default=None, metavar=_YORNO,
1201
                          help="Set the vm_capable flag on the node")
1202

    
1203
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1204
                             type="bool", default=None, metavar=_YORNO,
1205
                             help="Set the allocatable flag on a volume")
1206

    
1207
NOLVM_STORAGE_OPT = cli_option("--no-lvm-storage", dest="lvm_storage",
1208
                               help="Disable support for lvm based instances"
1209
                               " (cluster-wide)",
1210
                               action="store_false", default=True)
1211

    
1212
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1213
                            dest="enabled_hypervisors",
1214
                            help="Comma-separated list of hypervisors",
1215
                            type="string", default=None)
1216

    
1217
ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates",
1218
                                        dest="enabled_disk_templates",
1219
                                        help="Comma-separated list of "
1220
                                             "disk templates",
1221
                                        type="string", default=None)
1222

    
1223
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1224
                            type="keyval", default={},
1225
                            help="NIC parameters")
1226

    
1227
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1228
                         dest="candidate_pool_size", type="int",
1229
                         help="Set the candidate pool size")
1230

    
1231
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1232
                         help=("Enables LVM and specifies the volume group"
1233
                               " name (cluster-wide) for disk allocation"
1234
                               " [%s]" % constants.DEFAULT_VG),
1235
                         metavar="VG", default=None)
1236

    
1237
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1238
                          help="Destroy cluster", action="store_true")
1239

    
1240
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1241
                          help="Skip node agreement check (dangerous)",
1242
                          action="store_true", default=False)
1243

    
1244
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1245
                            help="Specify the mac prefix for the instance IP"
1246
                            " addresses, in the format XX:XX:XX",
1247
                            metavar="PREFIX",
1248
                            default=None)
1249

    
1250
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1251
                               help="Specify the node interface (cluster-wide)"
1252
                               " on which the master IP address will be added"
1253
                               " (cluster init default: %s)" %
1254
                               constants.DEFAULT_BRIDGE,
1255
                               metavar="NETDEV",
1256
                               default=None)
1257

    
1258
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1259
                                help="Specify the netmask of the master IP",
1260
                                metavar="NETMASK",
1261
                                default=None)
1262

    
1263
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1264
                                     dest="use_external_mip_script",
1265
                                     help="Specify whether to run a"
1266
                                     " user-provided script for the master"
1267
                                     " IP address turnup and"
1268
                                     " turndown operations",
1269
                                     type="bool", metavar=_YORNO, default=None)
1270

    
1271
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1272
                                help="Specify the default directory (cluster-"
1273
                                "wide) for storing the file-based disks [%s]" %
1274
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1275
                                metavar="DIR",
1276
                                default=pathutils.DEFAULT_FILE_STORAGE_DIR)
1277

    
1278
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1279
  "--shared-file-storage-dir",
1280
  dest="shared_file_storage_dir",
1281
  help="Specify the default directory (cluster-wide) for storing the"
1282
  " shared file-based disks [%s]" %
1283
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1284
  metavar="SHAREDDIR", default=pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR)
1285

    
1286
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1287
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1288
                                   action="store_false", default=True)
1289

    
1290
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1291
                                    help="Don't initialize SSH keys",
1292
                                    action="store_false", default=True)
1293

    
1294
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1295
                             help="Enable parseable error messages",
1296
                             action="store_true", default=False)
1297

    
1298
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1299
                          help="Skip N+1 memory redundancy tests",
1300
                          action="store_true", default=False)
1301

    
1302
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1303
                             help="Type of reboot: soft/hard/full",
1304
                             default=constants.INSTANCE_REBOOT_HARD,
1305
                             metavar="<REBOOT>",
1306
                             choices=list(constants.REBOOT_TYPES))
1307

    
1308
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1309
                                    dest="ignore_secondaries",
1310
                                    default=False, action="store_true",
1311
                                    help="Ignore errors from secondaries")
1312

    
1313
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1314
                            action="store_false", default=True,
1315
                            help="Don't shutdown the instance (unsafe)")
1316

    
1317
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1318
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1319
                         help="Maximum time to wait")
1320

    
1321
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1322
                                  dest="shutdown_timeout", type="int",
1323
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1324
                                  help="Maximum time to wait for instance"
1325
                                  " shutdown")
1326

    
1327
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1328
                          default=None,
1329
                          help=("Number of seconds between repetions of the"
1330
                                " command"))
1331

    
1332
EARLY_RELEASE_OPT = cli_option("--early-release",
1333
                               dest="early_release", default=False,
1334
                               action="store_true",
1335
                               help="Release the locks on the secondary"
1336
                               " node(s) early")
1337

    
1338
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1339
                                  dest="new_cluster_cert",
1340
                                  default=False, action="store_true",
1341
                                  help="Generate a new cluster certificate")
1342

    
1343
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1344
                           default=None,
1345
                           help="File containing new RAPI certificate")
1346

    
1347
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1348
                               default=None, action="store_true",
1349
                               help=("Generate a new self-signed RAPI"
1350
                                     " certificate"))
1351

    
1352
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1353
                            default=None,
1354
                            help="File containing new SPICE certificate")
1355

    
1356
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1357
                              default=None,
1358
                              help="File containing the certificate of the CA"
1359
                              " which signed the SPICE certificate")
1360

    
1361
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1362
                                dest="new_spice_cert", default=None,
1363
                                action="store_true",
1364
                                help=("Generate a new self-signed SPICE"
1365
                                      " certificate"))
1366

    
1367
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1368
                                    dest="new_confd_hmac_key",
1369
                                    default=False, action="store_true",
1370
                                    help=("Create a new HMAC key for %s" %
1371
                                          constants.CONFD))
1372

    
1373
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1374
                                       dest="cluster_domain_secret",
1375
                                       default=None,
1376
                                       help=("Load new new cluster domain"
1377
                                             " secret from file"))
1378

    
1379
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1380
                                           dest="new_cluster_domain_secret",
1381
                                           default=False, action="store_true",
1382
                                           help=("Create a new cluster domain"
1383
                                                 " secret"))
1384

    
1385
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1386
                              dest="use_replication_network",
1387
                              help="Whether to use the replication network"
1388
                              " for talking to the nodes",
1389
                              action="store_true", default=False)
1390

    
1391
MAINTAIN_NODE_HEALTH_OPT = \
1392
    cli_option("--maintain-node-health", dest="maintain_node_health",
1393
               metavar=_YORNO, default=None, type="bool",
1394
               help="Configure the cluster to automatically maintain node"
1395
               " health, by shutting down unknown instances, shutting down"
1396
               " unknown DRBD devices, etc.")
1397

    
1398
IDENTIFY_DEFAULTS_OPT = \
1399
    cli_option("--identify-defaults", dest="identify_defaults",
1400
               default=False, action="store_true",
1401
               help="Identify which saved instance parameters are equal to"
1402
               " the current cluster defaults and set them as such, instead"
1403
               " of marking them as overridden")
1404

    
1405
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1406
                         action="store", dest="uid_pool",
1407
                         help=("A list of user-ids or user-id"
1408
                               " ranges separated by commas"))
1409

    
1410
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1411
                          action="store", dest="add_uids",
1412
                          help=("A list of user-ids or user-id"
1413
                                " ranges separated by commas, to be"
1414
                                " added to the user-id pool"))
1415

    
1416
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1417
                             action="store", dest="remove_uids",
1418
                             help=("A list of user-ids or user-id"
1419
                                   " ranges separated by commas, to be"
1420
                                   " removed from the user-id pool"))
1421

    
1422
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1423
                              action="store", dest="reserved_lvs",
1424
                              help=("A comma-separated list of reserved"
1425
                                    " logical volumes names, that will be"
1426
                                    " ignored by cluster verify"))
1427

    
1428
ROMAN_OPT = cli_option("--roman",
1429
                       dest="roman_integers", default=False,
1430
                       action="store_true",
1431
                       help="Use roman numbers for positive integers")
1432

    
1433
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1434
                             action="store", default=None,
1435
                             help="Specifies usermode helper for DRBD")
1436

    
1437
NODRBD_STORAGE_OPT = cli_option("--no-drbd-storage", dest="drbd_storage",
1438
                                action="store_false", default=True,
1439
                                help="Disable support for DRBD")
1440

    
1441
PRIMARY_IP_VERSION_OPT = \
1442
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1443
               action="store", dest="primary_ip_version",
1444
               metavar="%d|%d" % (constants.IP4_VERSION,
1445
                                  constants.IP6_VERSION),
1446
               help="Cluster-wide IP version for primary IP")
1447

    
1448
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1449
                              action="store_true",
1450
                              help="Show machine name for every line in output")
1451

    
1452
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1453
                              action="store_true",
1454
                              help=("Hide successful results and show failures"
1455
                                    " only (determined by the exit code)"))
1456

    
1457
REASON_OPT = cli_option("--reason", default=None,
1458
                        help="The reason for executing the command")
1459

    
1460

    
1461
def _PriorityOptionCb(option, _, value, parser):
1462
  """Callback for processing C{--priority} option.
1463

1464
  """
1465
  value = _PRIONAME_TO_VALUE[value]
1466

    
1467
  setattr(parser.values, option.dest, value)
1468

    
1469

    
1470
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1471
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1472
                          choices=_PRIONAME_TO_VALUE.keys(),
1473
                          action="callback", type="choice",
1474
                          callback=_PriorityOptionCb,
1475
                          help="Priority for opcode processing")
1476

    
1477
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1478
                        type="bool", default=None, metavar=_YORNO,
1479
                        help="Sets the hidden flag on the OS")
1480

    
1481
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1482
                        type="bool", default=None, metavar=_YORNO,
1483
                        help="Sets the blacklisted flag on the OS")
1484

    
1485
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1486
                                     type="bool", metavar=_YORNO,
1487
                                     dest="prealloc_wipe_disks",
1488
                                     help=("Wipe disks prior to instance"
1489
                                           " creation"))
1490

    
1491
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1492
                             type="keyval", default=None,
1493
                             help="Node parameters")
1494

    
1495
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1496
                              action="store", metavar="POLICY", default=None,
1497
                              help="Allocation policy for the node group")
1498

    
1499
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1500
                              type="bool", metavar=_YORNO,
1501
                              dest="node_powered",
1502
                              help="Specify if the SoR for node is powered")
1503

    
1504
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1505
                             default=constants.OOB_TIMEOUT,
1506
                             help="Maximum time to wait for out-of-band helper")
1507

    
1508
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1509
                             default=constants.OOB_POWER_DELAY,
1510
                             help="Time in seconds to wait between power-ons")
1511

    
1512
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1513
                              action="store_true", default=False,
1514
                              help=("Whether command argument should be treated"
1515
                                    " as filter"))
1516

    
1517
NO_REMEMBER_OPT = cli_option("--no-remember",
1518
                             dest="no_remember",
1519
                             action="store_true", default=False,
1520
                             help="Perform but do not record the change"
1521
                             " in the configuration")
1522

    
1523
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1524
                              default=False, action="store_true",
1525
                              help="Evacuate primary instances only")
1526

    
1527
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1528
                                default=False, action="store_true",
1529
                                help="Evacuate secondary instances only"
1530
                                     " (applies only to internally mirrored"
1531
                                     " disk templates, e.g. %s)" %
1532
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1533

    
1534
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1535
                                action="store_true", default=False,
1536
                                help="Pause instance at startup")
1537

    
1538
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1539
                          help="Destination node group (name or uuid)",
1540
                          default=None, action="append",
1541
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1542

    
1543
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1544
                               action="append", dest="ignore_errors",
1545
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1546
                               help="Error code to be ignored")
1547

    
1548
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1549
                            action="append",
1550
                            help=("Specify disk state information in the"
1551
                                  " format"
1552
                                  " storage_type/identifier:option=value,...;"
1553
                                  " note this is unused for now"),
1554
                            type="identkeyval")
1555

    
1556
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1557
                          action="append",
1558
                          help=("Specify hypervisor state information in the"
1559
                                " format hypervisor:option=value,...;"
1560
                                " note this is unused for now"),
1561
                          type="identkeyval")
1562

    
1563
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1564
                                action="store_true", default=False,
1565
                                help="Ignore instance policy violations")
1566

    
1567
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1568
                             help="Sets the instance's runtime memory,"
1569
                             " ballooning it up or down to the new value",
1570
                             default=None, type="unit", metavar="<size>")
1571

    
1572
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1573
                          action="store_true", default=False,
1574
                          help="Marks the grow as absolute instead of the"
1575
                          " (default) relative mode")
1576

    
1577
NETWORK_OPT = cli_option("--network",
1578
                         action="store", default=None, dest="network",
1579
                         help="IP network in CIDR notation")
1580

    
1581
GATEWAY_OPT = cli_option("--gateway",
1582
                         action="store", default=None, dest="gateway",
1583
                         help="IP address of the router (gateway)")
1584

    
1585
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1586
                                  action="store", default=None,
1587
                                  dest="add_reserved_ips",
1588
                                  help="Comma-separated list of"
1589
                                  " reserved IPs to add")
1590

    
1591
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1592
                                     action="store", default=None,
1593
                                     dest="remove_reserved_ips",
1594
                                     help="Comma-delimited list of"
1595
                                     " reserved IPs to remove")
1596

    
1597
NETWORK6_OPT = cli_option("--network6",
1598
                          action="store", default=None, dest="network6",
1599
                          help="IP network in CIDR notation")
1600

    
1601
GATEWAY6_OPT = cli_option("--gateway6",
1602
                          action="store", default=None, dest="gateway6",
1603
                          help="IP6 address of the router (gateway)")
1604

    
1605
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1606
                                  dest="conflicts_check",
1607
                                  default=True,
1608
                                  action="store_false",
1609
                                  help="Don't check for conflicting IPs")
1610

    
1611
#: Options provided by all commands
1612
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1613

    
1614
# common options for creating instances. add and import then add their own
1615
# specific ones.
1616
COMMON_CREATE_OPTS = [
1617
  BACKEND_OPT,
1618
  DISK_OPT,
1619
  DISK_TEMPLATE_OPT,
1620
  FILESTORE_DIR_OPT,
1621
  FILESTORE_DRIVER_OPT,
1622
  HYPERVISOR_OPT,
1623
  IALLOCATOR_OPT,
1624
  NET_OPT,
1625
  NODE_PLACEMENT_OPT,
1626
  NOIPCHECK_OPT,
1627
  NOCONFLICTSCHECK_OPT,
1628
  NONAMECHECK_OPT,
1629
  NONICS_OPT,
1630
  NWSYNC_OPT,
1631
  OSPARAMS_OPT,
1632
  OS_SIZE_OPT,
1633
  SUBMIT_OPT,
1634
  TAG_ADD_OPT,
1635
  DRY_RUN_OPT,
1636
  PRIORITY_OPT,
1637
  ]
1638

    
1639
# common instance policy options
1640
INSTANCE_POLICY_OPTS = [
1641
  SPECS_CPU_COUNT_OPT,
1642
  SPECS_DISK_COUNT_OPT,
1643
  SPECS_DISK_SIZE_OPT,
1644
  SPECS_MEM_SIZE_OPT,
1645
  SPECS_NIC_COUNT_OPT,
1646
  IPOLICY_DISK_TEMPLATES,
1647
  IPOLICY_VCPU_RATIO,
1648
  IPOLICY_SPINDLE_RATIO,
1649
  ]
1650

    
1651

    
1652
class _ShowUsage(Exception):
1653
  """Exception class for L{_ParseArgs}.
1654

1655
  """
1656
  def __init__(self, exit_error):
1657
    """Initializes instances of this class.
1658

1659
    @type exit_error: bool
1660
    @param exit_error: Whether to report failure on exit
1661

1662
    """
1663
    Exception.__init__(self)
1664
    self.exit_error = exit_error
1665

    
1666

    
1667
class _ShowVersion(Exception):
1668
  """Exception class for L{_ParseArgs}.
1669

1670
  """
1671

    
1672

    
1673
def _ParseArgs(binary, argv, commands, aliases, env_override):
1674
  """Parser for the command line arguments.
1675

1676
  This function parses the arguments and returns the function which
1677
  must be executed together with its (modified) arguments.
1678

1679
  @param binary: Script name
1680
  @param argv: Command line arguments
1681
  @param commands: Dictionary containing command definitions
1682
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1683
  @param env_override: list of env variables allowed for default args
1684
  @raise _ShowUsage: If usage description should be shown
1685
  @raise _ShowVersion: If version should be shown
1686

1687
  """
1688
  assert not (env_override - set(commands))
1689
  assert not (set(aliases.keys()) & set(commands.keys()))
1690

    
1691
  if len(argv) > 1:
1692
    cmd = argv[1]
1693
  else:
1694
    # No option or command given
1695
    raise _ShowUsage(exit_error=True)
1696

    
1697
  if cmd == "--version":
1698
    raise _ShowVersion()
1699
  elif cmd == "--help":
1700
    raise _ShowUsage(exit_error=False)
1701
  elif not (cmd in commands or cmd in aliases):
1702
    raise _ShowUsage(exit_error=True)
1703

    
1704
  # get command, unalias it, and look it up in commands
1705
  if cmd in aliases:
1706
    if aliases[cmd] not in commands:
1707
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1708
                                   " command '%s'" % (cmd, aliases[cmd]))
1709

    
1710
    cmd = aliases[cmd]
1711

    
1712
  if cmd in env_override:
1713
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1714
    env_args = os.environ.get(args_env_name)
1715
    if env_args:
1716
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1717

    
1718
  func, args_def, parser_opts, usage, description = commands[cmd]
1719
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1720
                        description=description,
1721
                        formatter=TitledHelpFormatter(),
1722
                        usage="%%prog %s %s" % (cmd, usage))
1723
  parser.disable_interspersed_args()
1724
  options, args = parser.parse_args(args=argv[2:])
1725

    
1726
  if not _CheckArguments(cmd, args_def, args):
1727
    return None, None, None
1728

    
1729
  return func, options, args
1730

    
1731

    
1732
def _FormatUsage(binary, commands):
1733
  """Generates a nice description of all commands.
1734

1735
  @param binary: Script name
1736
  @param commands: Dictionary containing command definitions
1737

1738
  """
1739
  # compute the max line length for cmd + usage
1740
  mlen = min(60, max(map(len, commands)))
1741

    
1742
  yield "Usage: %s {command} [options...] [argument...]" % binary
1743
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1744
  yield ""
1745
  yield "Commands:"
1746

    
1747
  # and format a nice command list
1748
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1749
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1750
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1751
    for line in help_lines:
1752
      yield " %-*s   %s" % (mlen, "", line)
1753

    
1754
  yield ""
1755

    
1756

    
1757
def _CheckArguments(cmd, args_def, args):
1758
  """Verifies the arguments using the argument definition.
1759

1760
  Algorithm:
1761

1762
    1. Abort with error if values specified by user but none expected.
1763

1764
    1. For each argument in definition
1765

1766
      1. Keep running count of minimum number of values (min_count)
1767
      1. Keep running count of maximum number of values (max_count)
1768
      1. If it has an unlimited number of values
1769

1770
        1. Abort with error if it's not the last argument in the definition
1771

1772
    1. If last argument has limited number of values
1773

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

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

1778
  """
1779
  if args and not args_def:
1780
    ToStderr("Error: Command %s expects no arguments", cmd)
1781
    return False
1782

    
1783
  min_count = None
1784
  max_count = None
1785
  check_max = None
1786

    
1787
  last_idx = len(args_def) - 1
1788

    
1789
  for idx, arg in enumerate(args_def):
1790
    if min_count is None:
1791
      min_count = arg.min
1792
    elif arg.min is not None:
1793
      min_count += arg.min
1794

    
1795
    if max_count is None:
1796
      max_count = arg.max
1797
    elif arg.max is not None:
1798
      max_count += arg.max
1799

    
1800
    if idx == last_idx:
1801
      check_max = (arg.max is not None)
1802

    
1803
    elif arg.max is None:
1804
      raise errors.ProgrammerError("Only the last argument can have max=None")
1805

    
1806
  if check_max:
1807
    # Command with exact number of arguments
1808
    if (min_count is not None and max_count is not None and
1809
        min_count == max_count and len(args) != min_count):
1810
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1811
      return False
1812

    
1813
    # Command with limited number of arguments
1814
    if max_count is not None and len(args) > max_count:
1815
      ToStderr("Error: Command %s expects only %d argument(s)",
1816
               cmd, max_count)
1817
      return False
1818

    
1819
  # Command with some required arguments
1820
  if min_count is not None and len(args) < min_count:
1821
    ToStderr("Error: Command %s expects at least %d argument(s)",
1822
             cmd, min_count)
1823
    return False
1824

    
1825
  return True
1826

    
1827

    
1828
def SplitNodeOption(value):
1829
  """Splits the value of a --node option.
1830

1831
  """
1832
  if value and ":" in value:
1833
    return value.split(":", 1)
1834
  else:
1835
    return (value, None)
1836

    
1837

    
1838
def CalculateOSNames(os_name, os_variants):
1839
  """Calculates all the names an OS can be called, according to its variants.
1840

1841
  @type os_name: string
1842
  @param os_name: base name of the os
1843
  @type os_variants: list or None
1844
  @param os_variants: list of supported variants
1845
  @rtype: list
1846
  @return: list of valid names
1847

1848
  """
1849
  if os_variants:
1850
    return ["%s+%s" % (os_name, v) for v in os_variants]
1851
  else:
1852
    return [os_name]
1853

    
1854

    
1855
def ParseFields(selected, default):
1856
  """Parses the values of "--field"-like options.
1857

1858
  @type selected: string or None
1859
  @param selected: User-selected options
1860
  @type default: list
1861
  @param default: Default fields
1862

1863
  """
1864
  if selected is None:
1865
    return default
1866

    
1867
  if selected.startswith("+"):
1868
    return default + selected[1:].split(",")
1869

    
1870
  return selected.split(",")
1871

    
1872

    
1873
UsesRPC = rpc.RunWithRPC
1874

    
1875

    
1876
def AskUser(text, choices=None):
1877
  """Ask the user a question.
1878

1879
  @param text: the question to ask
1880

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

1886
  @return: one of the return values from the choices list; if input is
1887
      not possible (i.e. not running with a tty, we return the last
1888
      entry from the list
1889

1890
  """
1891
  if choices is None:
1892
    choices = [("y", True, "Perform the operation"),
1893
               ("n", False, "Do not perform the operation")]
1894
  if not choices or not isinstance(choices, list):
1895
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1896
  for entry in choices:
1897
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1898
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1899

    
1900
  answer = choices[-1][1]
1901
  new_text = []
1902
  for line in text.splitlines():
1903
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1904
  text = "\n".join(new_text)
1905
  try:
1906
    f = file("/dev/tty", "a+")
1907
  except IOError:
1908
    return answer
1909
  try:
1910
    chars = [entry[0] for entry in choices]
1911
    chars[-1] = "[%s]" % chars[-1]
1912
    chars.append("?")
1913
    maps = dict([(entry[0], entry[1]) for entry in choices])
1914
    while True:
1915
      f.write(text)
1916
      f.write("\n")
1917
      f.write("/".join(chars))
1918
      f.write(": ")
1919
      line = f.readline(2).strip().lower()
1920
      if line in maps:
1921
        answer = maps[line]
1922
        break
1923
      elif line == "?":
1924
        for entry in choices:
1925
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1926
        f.write("\n")
1927
        continue
1928
  finally:
1929
    f.close()
1930
  return answer
1931

    
1932

    
1933
class JobSubmittedException(Exception):
1934
  """Job was submitted, client should exit.
1935

1936
  This exception has one argument, the ID of the job that was
1937
  submitted. The handler should print this ID.
1938

1939
  This is not an error, just a structured way to exit from clients.
1940

1941
  """
1942

    
1943

    
1944
def SendJob(ops, cl=None):
1945
  """Function to submit an opcode without waiting for the results.
1946

1947
  @type ops: list
1948
  @param ops: list of opcodes
1949
  @type cl: luxi.Client
1950
  @param cl: the luxi client to use for communicating with the master;
1951
             if None, a new client will be created
1952

1953
  """
1954
  if cl is None:
1955
    cl = GetClient()
1956

    
1957
  job_id = cl.SubmitJob(ops)
1958

    
1959
  return job_id
1960

    
1961

    
1962
def GenericPollJob(job_id, cbs, report_cbs):
1963
  """Generic job-polling function.
1964

1965
  @type job_id: number
1966
  @param job_id: Job ID
1967
  @type cbs: Instance of L{JobPollCbBase}
1968
  @param cbs: Data callbacks
1969
  @type report_cbs: Instance of L{JobPollReportCbBase}
1970
  @param report_cbs: Reporting callbacks
1971

1972
  """
1973
  prev_job_info = None
1974
  prev_logmsg_serial = None
1975

    
1976
  status = None
1977

    
1978
  while True:
1979
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1980
                                      prev_logmsg_serial)
1981
    if not result:
1982
      # job not found, go away!
1983
      raise errors.JobLost("Job with id %s lost" % job_id)
1984

    
1985
    if result == constants.JOB_NOTCHANGED:
1986
      report_cbs.ReportNotChanged(job_id, status)
1987

    
1988
      # Wait again
1989
      continue
1990

    
1991
    # Split result, a tuple of (field values, log entries)
1992
    (job_info, log_entries) = result
1993
    (status, ) = job_info
1994

    
1995
    if log_entries:
1996
      for log_entry in log_entries:
1997
        (serial, timestamp, log_type, message) = log_entry
1998
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1999
                                    log_type, message)
2000
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
2001

    
2002
    # TODO: Handle canceled and archived jobs
2003
    elif status in (constants.JOB_STATUS_SUCCESS,
2004
                    constants.JOB_STATUS_ERROR,
2005
                    constants.JOB_STATUS_CANCELING,
2006
                    constants.JOB_STATUS_CANCELED):
2007
      break
2008

    
2009
    prev_job_info = job_info
2010

    
2011
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
2012
  if not jobs:
2013
    raise errors.JobLost("Job with id %s lost" % job_id)
2014

    
2015
  status, opstatus, result = jobs[0]
2016

    
2017
  if status == constants.JOB_STATUS_SUCCESS:
2018
    return result
2019

    
2020
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
2021
    raise errors.OpExecError("Job was canceled")
2022

    
2023
  has_ok = False
2024
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
2025
    if status == constants.OP_STATUS_SUCCESS:
2026
      has_ok = True
2027
    elif status == constants.OP_STATUS_ERROR:
2028
      errors.MaybeRaise(msg)
2029

    
2030
      if has_ok:
2031
        raise errors.OpExecError("partial failure (opcode %d): %s" %
2032
                                 (idx, msg))
2033

    
2034
      raise errors.OpExecError(str(msg))
2035

    
2036
  # default failure mode
2037
  raise errors.OpExecError(result)
2038

    
2039

    
2040
class JobPollCbBase:
2041
  """Base class for L{GenericPollJob} callbacks.
2042

2043
  """
2044
  def __init__(self):
2045
    """Initializes this class.
2046

2047
    """
2048

    
2049
  def WaitForJobChangeOnce(self, job_id, fields,
2050
                           prev_job_info, prev_log_serial):
2051
    """Waits for changes on a job.
2052

2053
    """
2054
    raise NotImplementedError()
2055

    
2056
  def QueryJobs(self, job_ids, fields):
2057
    """Returns the selected fields for the selected job IDs.
2058

2059
    @type job_ids: list of numbers
2060
    @param job_ids: Job IDs
2061
    @type fields: list of strings
2062
    @param fields: Fields
2063

2064
    """
2065
    raise NotImplementedError()
2066

    
2067

    
2068
class JobPollReportCbBase:
2069
  """Base class for L{GenericPollJob} reporting callbacks.
2070

2071
  """
2072
  def __init__(self):
2073
    """Initializes this class.
2074

2075
    """
2076

    
2077
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2078
    """Handles a log message.
2079

2080
    """
2081
    raise NotImplementedError()
2082

    
2083
  def ReportNotChanged(self, job_id, status):
2084
    """Called for if a job hasn't changed in a while.
2085

2086
    @type job_id: number
2087
    @param job_id: Job ID
2088
    @type status: string or None
2089
    @param status: Job status if available
2090

2091
    """
2092
    raise NotImplementedError()
2093

    
2094

    
2095
class _LuxiJobPollCb(JobPollCbBase):
2096
  def __init__(self, cl):
2097
    """Initializes this class.
2098

2099
    """
2100
    JobPollCbBase.__init__(self)
2101
    self.cl = cl
2102

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

2107
    """
2108
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2109
                                        prev_job_info, prev_log_serial)
2110

    
2111
  def QueryJobs(self, job_ids, fields):
2112
    """Returns the selected fields for the selected job IDs.
2113

2114
    """
2115
    return self.cl.QueryJobs(job_ids, fields)
2116

    
2117

    
2118
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2119
  def __init__(self, feedback_fn):
2120
    """Initializes this class.
2121

2122
    """
2123
    JobPollReportCbBase.__init__(self)
2124

    
2125
    self.feedback_fn = feedback_fn
2126

    
2127
    assert callable(feedback_fn)
2128

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

2132
    """
2133
    self.feedback_fn((timestamp, log_type, log_msg))
2134

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

2138
    """
2139
    # Ignore
2140

    
2141

    
2142
class StdioJobPollReportCb(JobPollReportCbBase):
2143
  def __init__(self):
2144
    """Initializes this class.
2145

2146
    """
2147
    JobPollReportCbBase.__init__(self)
2148

    
2149
    self.notified_queued = False
2150
    self.notified_waitlock = False
2151

    
2152
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2153
    """Handles a log message.
2154

2155
    """
2156
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2157
             FormatLogMessage(log_type, log_msg))
2158

    
2159
  def ReportNotChanged(self, job_id, status):
2160
    """Called if a job hasn't changed in a while.
2161

2162
    """
2163
    if status is None:
2164
      return
2165

    
2166
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2167
      ToStderr("Job %s is waiting in queue", job_id)
2168
      self.notified_queued = True
2169

    
2170
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2171
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2172
      self.notified_waitlock = True
2173

    
2174

    
2175
def FormatLogMessage(log_type, log_msg):
2176
  """Formats a job message according to its type.
2177

2178
  """
2179
  if log_type != constants.ELOG_MESSAGE:
2180
    log_msg = str(log_msg)
2181

    
2182
  return utils.SafeEncode(log_msg)
2183

    
2184

    
2185
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2186
  """Function to poll for the result of a job.
2187

2188
  @type job_id: job identified
2189
  @param job_id: the job to poll for results
2190
  @type cl: luxi.Client
2191
  @param cl: the luxi client to use for communicating with the master;
2192
             if None, a new client will be created
2193

2194
  """
2195
  if cl is None:
2196
    cl = GetClient()
2197

    
2198
  if reporter is None:
2199
    if feedback_fn:
2200
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2201
    else:
2202
      reporter = StdioJobPollReportCb()
2203
  elif feedback_fn:
2204
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2205

    
2206
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2207

    
2208

    
2209
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2210
  """Legacy function to submit an opcode.
2211

2212
  This is just a simple wrapper over the construction of the processor
2213
  instance. It should be extended to better handle feedback and
2214
  interaction functions.
2215

2216
  """
2217
  if cl is None:
2218
    cl = GetClient()
2219

    
2220
  SetGenericOpcodeOpts([op], opts)
2221

    
2222
  job_id = SendJob([op], cl=cl)
2223

    
2224
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2225
                       reporter=reporter)
2226

    
2227
  return op_results[0]
2228

    
2229

    
2230
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2231
  """Wrapper around SubmitOpCode or SendJob.
2232

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

2238
  It will also process the opcodes if we're sending the via SendJob
2239
  (otherwise SubmitOpCode does it).
2240

2241
  """
2242
  if opts and opts.submit_only:
2243
    job = [op]
2244
    SetGenericOpcodeOpts(job, opts)
2245
    job_id = SendJob(job, cl=cl)
2246
    raise JobSubmittedException(job_id)
2247
  else:
2248
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2249

    
2250

    
2251
def _InitReasonTrail(op, opts):
2252
  """Builds the first part of the reason trail
2253

2254
  Builds the initial part of the reason trail, adding the user provided reason
2255
  (if it exists) and the name of the command starting the operation.
2256

2257
  @param op: the opcode the reason trail will be added to
2258
  @param opts: the command line options selected by the user
2259

2260
  """
2261
  assert len(sys.argv) >= 2
2262
  trail = []
2263

    
2264
  if opts.reason:
2265
    trail.append((constants.OPCODE_REASON_SRC_USER,
2266
                  opts.reason,
2267
                  utils.EpochNano()))
2268

    
2269
  binary = os.path.basename(sys.argv[0])
2270
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2271
  command = sys.argv[1]
2272
  trail.append((source, command, utils.EpochNano()))
2273
  op.reason = trail
2274

    
2275

    
2276
def SetGenericOpcodeOpts(opcode_list, options):
2277
  """Processor for generic options.
2278

2279
  This function updates the given opcodes based on generic command
2280
  line options (like debug, dry-run, etc.).
2281

2282
  @param opcode_list: list of opcodes
2283
  @param options: command line options or None
2284
  @return: None (in-place modification)
2285

2286
  """
2287
  if not options:
2288
    return
2289
  for op in opcode_list:
2290
    op.debug_level = options.debug
2291
    if hasattr(options, "dry_run"):
2292
      op.dry_run = options.dry_run
2293
    if getattr(options, "priority", None) is not None:
2294
      op.priority = options.priority
2295
    _InitReasonTrail(op, options)
2296

    
2297

    
2298
def GetClient(query=False):
2299
  """Connects to the a luxi socket and returns a client.
2300

2301
  @type query: boolean
2302
  @param query: this signifies that the client will only be
2303
      used for queries; if the build-time parameter
2304
      enable-split-queries is enabled, then the client will be
2305
      connected to the query socket instead of the masterd socket
2306

2307
  """
2308
  override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
2309
  if override_socket:
2310
    if override_socket == constants.LUXI_OVERRIDE_MASTER:
2311
      address = pathutils.MASTER_SOCKET
2312
    elif override_socket == constants.LUXI_OVERRIDE_QUERY:
2313
      address = pathutils.QUERY_SOCKET
2314
    else:
2315
      address = override_socket
2316
  elif query and constants.ENABLE_SPLIT_QUERY:
2317
    address = pathutils.QUERY_SOCKET
2318
  else:
2319
    address = None
2320
  # TODO: Cache object?
2321
  try:
2322
    client = luxi.Client(address=address)
2323
  except luxi.NoMasterError:
2324
    ss = ssconf.SimpleStore()
2325

    
2326
    # Try to read ssconf file
2327
    try:
2328
      ss.GetMasterNode()
2329
    except errors.ConfigurationError:
2330
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
2331
                                 " not part of a cluster",
2332
                                 errors.ECODE_INVAL)
2333

    
2334
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
2335
    if master != myself:
2336
      raise errors.OpPrereqError("This is not the master node, please connect"
2337
                                 " to node '%s' and rerun the command" %
2338
                                 master, errors.ECODE_INVAL)
2339
    raise
2340
  return client
2341

    
2342

    
2343
def FormatError(err):
2344
  """Return a formatted error message for a given error.
2345

2346
  This function takes an exception instance and returns a tuple
2347
  consisting of two values: first, the recommended exit code, and
2348
  second, a string describing the error message (not
2349
  newline-terminated).
2350

2351
  """
2352
  retcode = 1
2353
  obuf = StringIO()
2354
  msg = str(err)
2355
  if isinstance(err, errors.ConfigurationError):
2356
    txt = "Corrupt configuration file: %s" % msg
2357
    logging.error(txt)
2358
    obuf.write(txt + "\n")
2359
    obuf.write("Aborting.")
2360
    retcode = 2
2361
  elif isinstance(err, errors.HooksAbort):
2362
    obuf.write("Failure: hooks execution failed:\n")
2363
    for node, script, out in err.args[0]:
2364
      if out:
2365
        obuf.write("  node: %s, script: %s, output: %s\n" %
2366
                   (node, script, out))
2367
      else:
2368
        obuf.write("  node: %s, script: %s (no output)\n" %
2369
                   (node, script))
2370
  elif isinstance(err, errors.HooksFailure):
2371
    obuf.write("Failure: hooks general failure: %s" % msg)
2372
  elif isinstance(err, errors.ResolverError):
2373
    this_host = netutils.Hostname.GetSysName()
2374
    if err.args[0] == this_host:
2375
      msg = "Failure: can't resolve my own hostname ('%s')"
2376
    else:
2377
      msg = "Failure: can't resolve hostname '%s'"
2378
    obuf.write(msg % err.args[0])
2379
  elif isinstance(err, errors.OpPrereqError):
2380
    if len(err.args) == 2:
2381
      obuf.write("Failure: prerequisites not met for this"
2382
                 " operation:\nerror type: %s, error details:\n%s" %
2383
                 (err.args[1], err.args[0]))
2384
    else:
2385
      obuf.write("Failure: prerequisites not met for this"
2386
                 " operation:\n%s" % msg)
2387
  elif isinstance(err, errors.OpExecError):
2388
    obuf.write("Failure: command execution error:\n%s" % msg)
2389
  elif isinstance(err, errors.TagError):
2390
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2391
  elif isinstance(err, errors.JobQueueDrainError):
2392
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2393
               " accept new requests\n")
2394
  elif isinstance(err, errors.JobQueueFull):
2395
    obuf.write("Failure: the job queue is full and doesn't accept new"
2396
               " job submissions until old jobs are archived\n")
2397
  elif isinstance(err, errors.TypeEnforcementError):
2398
    obuf.write("Parameter Error: %s" % msg)
2399
  elif isinstance(err, errors.ParameterError):
2400
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2401
  elif isinstance(err, luxi.NoMasterError):
2402
    if err.args[0] == pathutils.MASTER_SOCKET:
2403
      daemon = "the master daemon"
2404
    elif err.args[0] == pathutils.QUERY_SOCKET:
2405
      daemon = "the config daemon"
2406
    else:
2407
      daemon = "socket '%s'" % str(err.args[0])
2408
    obuf.write("Cannot communicate with %s.\nIs the process running"
2409
               " and listening for connections?" % daemon)
2410
  elif isinstance(err, luxi.TimeoutError):
2411
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2412
               " been submitted and will continue to run even if the call"
2413
               " timed out. Useful commands in this situation are \"gnt-job"
2414
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2415
    obuf.write(msg)
2416
  elif isinstance(err, luxi.PermissionError):
2417
    obuf.write("It seems you don't have permissions to connect to the"
2418
               " master daemon.\nPlease retry as a different user.")
2419
  elif isinstance(err, luxi.ProtocolError):
2420
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2421
               "%s" % msg)
2422
  elif isinstance(err, errors.JobLost):
2423
    obuf.write("Error checking job status: %s" % msg)
2424
  elif isinstance(err, errors.QueryFilterParseError):
2425
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2426
    obuf.write("\n".join(err.GetDetails()))
2427
  elif isinstance(err, errors.GenericError):
2428
    obuf.write("Unhandled Ganeti error: %s" % msg)
2429
  elif isinstance(err, JobSubmittedException):
2430
    obuf.write("JobID: %s\n" % err.args[0])
2431
    retcode = 0
2432
  else:
2433
    obuf.write("Unhandled exception: %s" % msg)
2434
  return retcode, obuf.getvalue().rstrip("\n")
2435

    
2436

    
2437
def GenericMain(commands, override=None, aliases=None,
2438
                env_override=frozenset()):
2439
  """Generic main function for all the gnt-* commands.
2440

2441
  @param commands: a dictionary with a special structure, see the design doc
2442
                   for command line handling.
2443
  @param override: if not None, we expect a dictionary with keys that will
2444
                   override command line options; this can be used to pass
2445
                   options from the scripts to generic functions
2446
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2447
  @param env_override: list of environment names which are allowed to submit
2448
                       default args for commands
2449

2450
  """
2451
  # save the program name and the entire command line for later logging
2452
  if sys.argv:
2453
    binary = os.path.basename(sys.argv[0])
2454
    if not binary:
2455
      binary = sys.argv[0]
2456

    
2457
    if len(sys.argv) >= 2:
2458
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2459
    else:
2460
      logname = binary
2461

    
2462
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2463
  else:
2464
    binary = "<unknown program>"
2465
    cmdline = "<unknown>"
2466

    
2467
  if aliases is None:
2468
    aliases = {}
2469

    
2470
  try:
2471
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2472
                                       env_override)
2473
  except _ShowVersion:
2474
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2475
             constants.RELEASE_VERSION)
2476
    return constants.EXIT_SUCCESS
2477
  except _ShowUsage, err:
2478
    for line in _FormatUsage(binary, commands):
2479
      ToStdout(line)
2480

    
2481
    if err.exit_error:
2482
      return constants.EXIT_FAILURE
2483
    else:
2484
      return constants.EXIT_SUCCESS
2485
  except errors.ParameterError, err:
2486
    result, err_msg = FormatError(err)
2487
    ToStderr(err_msg)
2488
    return 1
2489

    
2490
  if func is None: # parse error
2491
    return 1
2492

    
2493
  if override is not None:
2494
    for key, val in override.iteritems():
2495
      setattr(options, key, val)
2496

    
2497
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2498
                     stderr_logging=True)
2499

    
2500
  logging.info("Command line: %s", cmdline)
2501

    
2502
  try:
2503
    result = func(options, args)
2504
  except (errors.GenericError, luxi.ProtocolError,
2505
          JobSubmittedException), err:
2506
    result, err_msg = FormatError(err)
2507
    logging.exception("Error during command processing")
2508
    ToStderr(err_msg)
2509
  except KeyboardInterrupt:
2510
    result = constants.EXIT_FAILURE
2511
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2512
             " might have been submitted and"
2513
             " will continue to run in the background.")
2514
  except IOError, err:
2515
    if err.errno == errno.EPIPE:
2516
      # our terminal went away, we'll exit
2517
      sys.exit(constants.EXIT_FAILURE)
2518
    else:
2519
      raise
2520

    
2521
  return result
2522

    
2523

    
2524
def ParseNicOption(optvalue):
2525
  """Parses the value of the --net option(s).
2526

2527
  """
2528
  try:
2529
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2530
  except (TypeError, ValueError), err:
2531
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2532
                               errors.ECODE_INVAL)
2533

    
2534
  nics = [{}] * nic_max
2535
  for nidx, ndict in optvalue:
2536
    nidx = int(nidx)
2537

    
2538
    if not isinstance(ndict, dict):
2539
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2540
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2541

    
2542
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2543

    
2544
    nics[nidx] = ndict
2545

    
2546
  return nics
2547

    
2548

    
2549
def GenericInstanceCreate(mode, opts, args):
2550
  """Add an instance to the cluster via either creation or import.
2551

2552
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2553
  @param opts: the command line options selected by the user
2554
  @type args: list
2555
  @param args: should contain only one element, the new instance name
2556
  @rtype: int
2557
  @return: the desired exit code
2558

2559
  """
2560
  instance = args[0]
2561

    
2562
  (pnode, snode) = SplitNodeOption(opts.node)
2563

    
2564
  hypervisor = None
2565
  hvparams = {}
2566
  if opts.hypervisor:
2567
    hypervisor, hvparams = opts.hypervisor
2568

    
2569
  if opts.nics:
2570
    nics = ParseNicOption(opts.nics)
2571
  elif opts.no_nics:
2572
    # no nics
2573
    nics = []
2574
  elif mode == constants.INSTANCE_CREATE:
2575
    # default of one nic, all auto
2576
    nics = [{}]
2577
  else:
2578
    # mode == import
2579
    nics = []
2580

    
2581
  if opts.disk_template == constants.DT_DISKLESS:
2582
    if opts.disks or opts.sd_size is not None:
2583
      raise errors.OpPrereqError("Diskless instance but disk"
2584
                                 " information passed", errors.ECODE_INVAL)
2585
    disks = []
2586
  else:
2587
    if (not opts.disks and not opts.sd_size
2588
        and mode == constants.INSTANCE_CREATE):
2589
      raise errors.OpPrereqError("No disk information specified",
2590
                                 errors.ECODE_INVAL)
2591
    if opts.disks and opts.sd_size is not None:
2592
      raise errors.OpPrereqError("Please use either the '--disk' or"
2593
                                 " '-s' option", errors.ECODE_INVAL)
2594
    if opts.sd_size is not None:
2595
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2596

    
2597
    if opts.disks:
2598
      try:
2599
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2600
      except ValueError, err:
2601
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
2602
                                   errors.ECODE_INVAL)
2603
      disks = [{}] * disk_max
2604
    else:
2605
      disks = []
2606
    for didx, ddict in opts.disks:
2607
      didx = int(didx)
2608
      if not isinstance(ddict, dict):
2609
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2610
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
2611
      elif constants.IDISK_SIZE in ddict:
2612
        if constants.IDISK_ADOPT in ddict:
2613
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2614
                                     " (disk %d)" % didx, errors.ECODE_INVAL)
2615
        try:
2616
          ddict[constants.IDISK_SIZE] = \
2617
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2618
        except ValueError, err:
2619
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2620
                                     (didx, err), errors.ECODE_INVAL)
2621
      elif constants.IDISK_ADOPT in ddict:
2622
        if mode == constants.INSTANCE_IMPORT:
2623
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2624
                                     " import", errors.ECODE_INVAL)
2625
        ddict[constants.IDISK_SIZE] = 0
2626
      else:
2627
        raise errors.OpPrereqError("Missing size or adoption source for"
2628
                                   " disk %d" % didx, errors.ECODE_INVAL)
2629
      disks[didx] = ddict
2630

    
2631
  if opts.tags is not None:
2632
    tags = opts.tags.split(",")
2633
  else:
2634
    tags = []
2635

    
2636
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2637
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2638

    
2639
  if mode == constants.INSTANCE_CREATE:
2640
    start = opts.start
2641
    os_type = opts.os
2642
    force_variant = opts.force_variant
2643
    src_node = None
2644
    src_path = None
2645
    no_install = opts.no_install
2646
    identify_defaults = False
2647
  elif mode == constants.INSTANCE_IMPORT:
2648
    start = False
2649
    os_type = None
2650
    force_variant = False
2651
    src_node = opts.src_node
2652
    src_path = opts.src_dir
2653
    no_install = None
2654
    identify_defaults = opts.identify_defaults
2655
  else:
2656
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2657

    
2658
  op = opcodes.OpInstanceCreate(instance_name=instance,
2659
                                disks=disks,
2660
                                disk_template=opts.disk_template,
2661
                                nics=nics,
2662
                                conflicts_check=opts.conflicts_check,
2663
                                pnode=pnode, snode=snode,
2664
                                ip_check=opts.ip_check,
2665
                                name_check=opts.name_check,
2666
                                wait_for_sync=opts.wait_for_sync,
2667
                                file_storage_dir=opts.file_storage_dir,
2668
                                file_driver=opts.file_driver,
2669
                                iallocator=opts.iallocator,
2670
                                hypervisor=hypervisor,
2671
                                hvparams=hvparams,
2672
                                beparams=opts.beparams,
2673
                                osparams=opts.osparams,
2674
                                mode=mode,
2675
                                start=start,
2676
                                os_type=os_type,
2677
                                force_variant=force_variant,
2678
                                src_node=src_node,
2679
                                src_path=src_path,
2680
                                tags=tags,
2681
                                no_install=no_install,
2682
                                identify_defaults=identify_defaults,
2683
                                ignore_ipolicy=opts.ignore_ipolicy)
2684

    
2685
  SubmitOrSend(op, opts)
2686
  return 0
2687

    
2688

    
2689
class _RunWhileClusterStoppedHelper:
2690
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2691

2692
  """
2693
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
2694
    """Initializes this class.
2695

2696
    @type feedback_fn: callable
2697
    @param feedback_fn: Feedback function
2698
    @type cluster_name: string
2699
    @param cluster_name: Cluster name
2700
    @type master_node: string
2701
    @param master_node Master node name
2702
    @type online_nodes: list
2703
    @param online_nodes: List of names of online nodes
2704

2705
    """
2706
    self.feedback_fn = feedback_fn
2707
    self.cluster_name = cluster_name
2708
    self.master_node = master_node
2709
    self.online_nodes = online_nodes
2710

    
2711
    self.ssh = ssh.SshRunner(self.cluster_name)
2712

    
2713
    self.nonmaster_nodes = [name for name in online_nodes
2714
                            if name != master_node]
2715

    
2716
    assert self.master_node not in self.nonmaster_nodes
2717

    
2718
  def _RunCmd(self, node_name, cmd):
2719
    """Runs a command on the local or a remote machine.
2720

2721
    @type node_name: string
2722
    @param node_name: Machine name
2723
    @type cmd: list
2724
    @param cmd: Command
2725

2726
    """
2727
    if node_name is None or node_name == self.master_node:
2728
      # No need to use SSH
2729
      result = utils.RunCmd(cmd)
2730
    else:
2731
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2732
                            utils.ShellQuoteArgs(cmd))
2733

    
2734
    if result.failed:
2735
      errmsg = ["Failed to run command %s" % result.cmd]
2736
      if node_name:
2737
        errmsg.append("on node %s" % node_name)
2738
      errmsg.append(": exitcode %s and error %s" %
2739
                    (result.exit_code, result.output))
2740
      raise errors.OpExecError(" ".join(errmsg))
2741

    
2742
  def Call(self, fn, *args):
2743
    """Call function while all daemons are stopped.
2744

2745
    @type fn: callable
2746
    @param fn: Function to be called
2747

2748
    """
2749
    # Pause watcher by acquiring an exclusive lock on watcher state file
2750
    self.feedback_fn("Blocking watcher")
2751
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2752
    try:
2753
      # TODO: Currently, this just blocks. There's no timeout.
2754
      # TODO: Should it be a shared lock?
2755
      watcher_block.Exclusive(blocking=True)
2756

    
2757
      # Stop master daemons, so that no new jobs can come in and all running
2758
      # ones are finished
2759
      self.feedback_fn("Stopping master daemons")
2760
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2761
      try:
2762
        # Stop daemons on all nodes
2763
        for node_name in self.online_nodes:
2764
          self.feedback_fn("Stopping daemons on %s" % node_name)
2765
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2766

    
2767
        # All daemons are shut down now
2768
        try:
2769
          return fn(self, *args)
2770
        except Exception, err:
2771
          _, errmsg = FormatError(err)
2772
          logging.exception("Caught exception")
2773
          self.feedback_fn(errmsg)
2774
          raise
2775
      finally:
2776
        # Start cluster again, master node last
2777
        for node_name in self.nonmaster_nodes + [self.master_node]:
2778
          self.feedback_fn("Starting daemons on %s" % node_name)
2779
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"])
2780
    finally:
2781
      # Resume watcher
2782
      watcher_block.Close()
2783

    
2784

    
2785
def RunWhileClusterStopped(feedback_fn, fn, *args):
2786
  """Calls a function while all cluster daemons are stopped.
2787

2788
  @type feedback_fn: callable
2789
  @param feedback_fn: Feedback function
2790
  @type fn: callable
2791
  @param fn: Function to be called when daemons are stopped
2792

2793
  """
2794
  feedback_fn("Gathering cluster information")
2795

    
2796
  # This ensures we're running on the master daemon
2797
  cl = GetClient()
2798

    
2799
  (cluster_name, master_node) = \
2800
    cl.QueryConfigValues(["cluster_name", "master_node"])
2801

    
2802
  online_nodes = GetOnlineNodes([], cl=cl)
2803

    
2804
  # Don't keep a reference to the client. The master daemon will go away.
2805
  del cl
2806

    
2807
  assert master_node in online_nodes
2808

    
2809
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2810
                                       online_nodes).Call(fn, *args)
2811

    
2812

    
2813
def GenerateTable(headers, fields, separator, data,
2814
                  numfields=None, unitfields=None,
2815
                  units=None):
2816
  """Prints a table with headers and different fields.
2817

2818
  @type headers: dict
2819
  @param headers: dictionary mapping field names to headers for
2820
      the table
2821
  @type fields: list
2822
  @param fields: the field names corresponding to each row in
2823
      the data field
2824
  @param separator: the separator to be used; if this is None,
2825
      the default 'smart' algorithm is used which computes optimal
2826
      field width, otherwise just the separator is used between
2827
      each field
2828
  @type data: list
2829
  @param data: a list of lists, each sublist being one row to be output
2830
  @type numfields: list
2831
  @param numfields: a list with the fields that hold numeric
2832
      values and thus should be right-aligned
2833
  @type unitfields: list
2834
  @param unitfields: a list with the fields that hold numeric
2835
      values that should be formatted with the units field
2836
  @type units: string or None
2837
  @param units: the units we should use for formatting, or None for
2838
      automatic choice (human-readable for non-separator usage, otherwise
2839
      megabytes); this is a one-letter string
2840

2841
  """
2842
  if units is None:
2843
    if separator:
2844
      units = "m"
2845
    else:
2846
      units = "h"
2847

    
2848
  if numfields is None:
2849
    numfields = []
2850
  if unitfields is None:
2851
    unitfields = []
2852

    
2853
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2854
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2855

    
2856
  format_fields = []
2857
  for field in fields:
2858
    if headers and field not in headers:
2859
      # TODO: handle better unknown fields (either revert to old
2860
      # style of raising exception, or deal more intelligently with
2861
      # variable fields)
2862
      headers[field] = field
2863
    if separator is not None:
2864
      format_fields.append("%s")
2865
    elif numfields.Matches(field):
2866
      format_fields.append("%*s")
2867
    else:
2868
      format_fields.append("%-*s")
2869

    
2870
  if separator is None:
2871
    mlens = [0 for name in fields]
2872
    format_str = " ".join(format_fields)
2873
  else:
2874
    format_str = separator.replace("%", "%%").join(format_fields)
2875

    
2876
  for row in data:
2877
    if row is None:
2878
      continue
2879
    for idx, val in enumerate(row):
2880
      if unitfields.Matches(fields[idx]):
2881
        try:
2882
          val = int(val)
2883
        except (TypeError, ValueError):
2884
          pass
2885
        else:
2886
          val = row[idx] = utils.FormatUnit(val, units)
2887
      val = row[idx] = str(val)
2888
      if separator is None:
2889
        mlens[idx] = max(mlens[idx], len(val))
2890

    
2891
  result = []
2892
  if headers:
2893
    args = []
2894
    for idx, name in enumerate(fields):
2895
      hdr = headers[name]
2896
      if separator is None:
2897
        mlens[idx] = max(mlens[idx], len(hdr))
2898
        args.append(mlens[idx])
2899
      args.append(hdr)
2900
    result.append(format_str % tuple(args))
2901

    
2902
  if separator is None:
2903
    assert len(mlens) == len(fields)
2904

    
2905
    if fields and not numfields.Matches(fields[-1]):
2906
      mlens[-1] = 0
2907

    
2908
  for line in data:
2909
    args = []
2910
    if line is None:
2911
      line = ["-" for _ in fields]
2912
    for idx in range(len(fields)):
2913
      if separator is None:
2914
        args.append(mlens[idx])
2915
      args.append(line[idx])
2916
    result.append(format_str % tuple(args))
2917

    
2918
  return result
2919

    
2920

    
2921
def _FormatBool(value):
2922
  """Formats a boolean value as a string.
2923

2924
  """
2925
  if value:
2926
    return "Y"
2927
  return "N"
2928

    
2929

    
2930
#: Default formatting for query results; (callback, align right)
2931
_DEFAULT_FORMAT_QUERY = {
2932
  constants.QFT_TEXT: (str, False),
2933
  constants.QFT_BOOL: (_FormatBool, False),
2934
  constants.QFT_NUMBER: (str, True),
2935
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
2936
  constants.QFT_OTHER: (str, False),
2937
  constants.QFT_UNKNOWN: (str, False),
2938
  }
2939

    
2940

    
2941
def _GetColumnFormatter(fdef, override, unit):
2942
  """Returns formatting function for a field.
2943

2944
  @type fdef: L{objects.QueryFieldDefinition}
2945
  @type override: dict
2946
  @param override: Dictionary for overriding field formatting functions,
2947
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2948
  @type unit: string
2949
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
2950
  @rtype: tuple; (callable, bool)
2951
  @return: Returns the function to format a value (takes one parameter) and a
2952
    boolean for aligning the value on the right-hand side
2953

2954
  """
2955
  fmt = override.get(fdef.name, None)
2956
  if fmt is not None:
2957
    return fmt
2958

    
2959
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
2960

    
2961
  if fdef.kind == constants.QFT_UNIT:
2962
    # Can't keep this information in the static dictionary
2963
    return (lambda value: utils.FormatUnit(value, unit), True)
2964

    
2965
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
2966
  if fmt is not None:
2967
    return fmt
2968

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

    
2971

    
2972
class _QueryColumnFormatter:
2973
  """Callable class for formatting fields of a query.
2974

2975
  """
2976
  def __init__(self, fn, status_fn, verbose):
2977
    """Initializes this class.
2978

2979
    @type fn: callable
2980
    @param fn: Formatting function
2981
    @type status_fn: callable
2982
    @param status_fn: Function to report fields' status
2983
    @type verbose: boolean
2984
    @param verbose: whether to use verbose field descriptions or not
2985

2986
    """
2987
    self._fn = fn
2988
    self._status_fn = status_fn
2989
    self._verbose = verbose
2990

    
2991
  def __call__(self, data):
2992
    """Returns a field's string representation.
2993

2994
    """
2995
    (status, value) = data
2996

    
2997
    # Report status
2998
    self._status_fn(status)
2999

    
3000
    if status == constants.RS_NORMAL:
3001
      return self._fn(value)
3002

    
3003
    assert value is None, \
3004
           "Found value %r for abnormal status %s" % (value, status)
3005

    
3006
    return FormatResultError(status, self._verbose)
3007

    
3008

    
3009
def FormatResultError(status, verbose):
3010
  """Formats result status other than L{constants.RS_NORMAL}.
3011

3012
  @param status: The result status
3013
  @type verbose: boolean
3014
  @param verbose: Whether to return the verbose text
3015
  @return: Text of result status
3016

3017
  """
3018
  assert status != constants.RS_NORMAL, \
3019
         "FormatResultError called with status equal to constants.RS_NORMAL"
3020
  try:
3021
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
3022
  except KeyError:
3023
    raise NotImplementedError("Unknown status %s" % status)
3024
  else:
3025
    if verbose:
3026
      return verbose_text
3027
    return normal_text
3028

    
3029

    
3030
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
3031
                      header=False, verbose=False):
3032
  """Formats data in L{objects.QueryResponse}.
3033

3034
  @type result: L{objects.QueryResponse}
3035
  @param result: result of query operation
3036
  @type unit: string
3037
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
3038
    see L{utils.text.FormatUnit}
3039
  @type format_override: dict
3040
  @param format_override: Dictionary for overriding field formatting functions,
3041
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3042
  @type separator: string or None
3043
  @param separator: String used to separate fields
3044
  @type header: bool
3045
  @param header: Whether to output header row
3046
  @type verbose: boolean
3047
  @param verbose: whether to use verbose field descriptions or not
3048

3049
  """
3050
  if unit is None:
3051
    if separator:
3052
      unit = "m"
3053
    else:
3054
      unit = "h"
3055

    
3056
  if format_override is None:
3057
    format_override = {}
3058

    
3059
  stats = dict.fromkeys(constants.RS_ALL, 0)
3060

    
3061
  def _RecordStatus(status):
3062
    if status in stats:
3063
      stats[status] += 1
3064

    
3065
  columns = []
3066
  for fdef in result.fields:
3067
    assert fdef.title and fdef.name
3068
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3069
    columns.append(TableColumn(fdef.title,
3070
                               _QueryColumnFormatter(fn, _RecordStatus,
3071
                                                     verbose),
3072
                               align_right))
3073

    
3074
  table = FormatTable(result.data, columns, header, separator)
3075

    
3076
  # Collect statistics
3077
  assert len(stats) == len(constants.RS_ALL)
3078
  assert compat.all(count >= 0 for count in stats.values())
3079

    
3080
  # Determine overall status. If there was no data, unknown fields must be
3081
  # detected via the field definitions.
3082
  if (stats[constants.RS_UNKNOWN] or
3083
      (not result.data and _GetUnknownFields(result.fields))):
3084
    status = QR_UNKNOWN
3085
  elif compat.any(count > 0 for key, count in stats.items()
3086
                  if key != constants.RS_NORMAL):
3087
    status = QR_INCOMPLETE
3088
  else:
3089
    status = QR_NORMAL
3090

    
3091
  return (status, table)
3092

    
3093

    
3094
def _GetUnknownFields(fdefs):
3095
  """Returns list of unknown fields included in C{fdefs}.
3096

3097
  @type fdefs: list of L{objects.QueryFieldDefinition}
3098

3099
  """
3100
  return [fdef for fdef in fdefs
3101
          if fdef.kind == constants.QFT_UNKNOWN]
3102

    
3103

    
3104
def _WarnUnknownFields(fdefs):
3105
  """Prints a warning to stderr if a query included unknown fields.
3106

3107
  @type fdefs: list of L{objects.QueryFieldDefinition}
3108

3109
  """
3110
  unknown = _GetUnknownFields(fdefs)
3111
  if unknown:
3112
    ToStderr("Warning: Queried for unknown fields %s",
3113
             utils.CommaJoin(fdef.name for fdef in unknown))
3114
    return True
3115

    
3116
  return False
3117

    
3118

    
3119
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3120
                format_override=None, verbose=False, force_filter=False,
3121
                namefield=None, qfilter=None, isnumeric=False):
3122
  """Generic implementation for listing all items of a resource.
3123

3124
  @param resource: One of L{constants.QR_VIA_LUXI}
3125
  @type fields: list of strings
3126
  @param fields: List of fields to query for
3127
  @type names: list of strings
3128
  @param names: Names of items to query for
3129
  @type unit: string or None
3130
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
3131
    None for automatic choice (human-readable for non-separator usage,
3132
    otherwise megabytes); this is a one-letter string
3133
  @type separator: string or None
3134
  @param separator: String used to separate fields
3135
  @type header: bool
3136
  @param header: Whether to show header row
3137
  @type force_filter: bool
3138
  @param force_filter: Whether to always treat names as filter
3139
  @type format_override: dict
3140
  @param format_override: Dictionary for overriding field formatting functions,
3141
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3142
  @type verbose: boolean
3143
  @param verbose: whether to use verbose field descriptions or not
3144
  @type namefield: string
3145
  @param namefield: Name of field to use for simple filters (see
3146
    L{qlang.MakeFilter} for details)
3147
  @type qfilter: list or None
3148
  @param qfilter: Query filter (in addition to names)
3149
  @param isnumeric: bool
3150
  @param isnumeric: Whether the namefield's type is numeric, and therefore
3151
    any simple filters built by namefield should use integer values to
3152
    reflect that
3153

3154
  """
3155
  if not names:
3156
    names = None
3157

    
3158
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3159
                                isnumeric=isnumeric)
3160

    
3161
  if qfilter is None:
3162
    qfilter = namefilter
3163
  elif namefilter is not None:
3164
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3165

    
3166
  if cl is None:
3167
    cl = GetClient()
3168

    
3169
  response = cl.Query(resource, fields, qfilter)
3170

    
3171
  found_unknown = _WarnUnknownFields(response.fields)
3172

    
3173
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3174
                                     header=header,
3175
                                     format_override=format_override,
3176
                                     verbose=verbose)
3177

    
3178
  for line in data:
3179
    ToStdout(line)
3180

    
3181
  assert ((found_unknown and status == QR_UNKNOWN) or
3182
          (not found_unknown and status != QR_UNKNOWN))
3183

    
3184
  if status == QR_UNKNOWN:
3185
    return constants.EXIT_UNKNOWN_FIELD
3186

    
3187
  # TODO: Should the list command fail if not all data could be collected?
3188
  return constants.EXIT_SUCCESS
3189

    
3190

    
3191
def _FieldDescValues(fdef):
3192
  """Helper function for L{GenericListFields} to get query field description.
3193

3194
  @type fdef: L{objects.QueryFieldDefinition}
3195
  @rtype: list
3196

3197
  """
3198
  return [
3199
    fdef.name,
3200
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3201
    fdef.title,
3202
    fdef.doc,
3203
    ]
3204

    
3205

    
3206
def GenericListFields(resource, fields, separator, header, cl=None):
3207
  """Generic implementation for listing fields for a resource.
3208

3209
  @param resource: One of L{constants.QR_VIA_LUXI}
3210
  @type fields: list of strings
3211
  @param fields: List of fields to query for
3212
  @type separator: string or None
3213
  @param separator: String used to separate fields
3214
  @type header: bool
3215
  @param header: Whether to show header row
3216

3217
  """
3218
  if cl is None:
3219
    cl = GetClient()
3220

    
3221
  if not fields:
3222
    fields = None
3223

    
3224
  response = cl.QueryFields(resource, fields)
3225

    
3226
  found_unknown = _WarnUnknownFields(response.fields)
3227

    
3228
  columns = [
3229
    TableColumn("Name", str, False),
3230
    TableColumn("Type", str, False),
3231
    TableColumn("Title", str, False),
3232
    TableColumn("Description", str, False),
3233
    ]
3234

    
3235
  rows = map(_FieldDescValues, response.fields)
3236

    
3237
  for line in FormatTable(rows, columns, header, separator):
3238
    ToStdout(line)
3239

    
3240
  if found_unknown:
3241
    return constants.EXIT_UNKNOWN_FIELD
3242

    
3243
  return constants.EXIT_SUCCESS
3244

    
3245

    
3246
class TableColumn:
3247
  """Describes a column for L{FormatTable}.
3248

3249
  """
3250
  def __init__(self, title, fn, align_right):
3251
    """Initializes this class.
3252

3253
    @type title: string
3254
    @param title: Column title
3255
    @type fn: callable
3256
    @param fn: Formatting function
3257
    @type align_right: bool
3258
    @param align_right: Whether to align values on the right-hand side
3259

3260
    """
3261
    self.title = title
3262
    self.format = fn
3263
    self.align_right = align_right
3264

    
3265

    
3266
def _GetColFormatString(width, align_right):
3267
  """Returns the format string for a field.
3268

3269
  """
3270
  if align_right:
3271
    sign = ""
3272
  else:
3273
    sign = "-"
3274

    
3275
  return "%%%s%ss" % (sign, width)
3276

    
3277

    
3278
def FormatTable(rows, columns, header, separator):
3279
  """Formats data as a table.
3280

3281
  @type rows: list of lists
3282
  @param rows: Row data, one list per row
3283
  @type columns: list of L{TableColumn}
3284
  @param columns: Column descriptions
3285
  @type header: bool
3286
  @param header: Whether to show header row
3287
  @type separator: string or None
3288
  @param separator: String used to separate columns
3289

3290
  """
3291
  if header:
3292
    data = [[col.title for col in columns]]
3293
    colwidth = [len(col.title) for col in columns]
3294
  else:
3295
    data = []
3296
    colwidth = [0 for _ in columns]
3297

    
3298
  # Format row data
3299
  for row in rows:
3300
    assert len(row) == len(columns)
3301

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

    
3304
    if separator is None:
3305
      # Update column widths
3306
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3307
        # Modifying a list's items while iterating is fine
3308
        colwidth[idx] = max(oldwidth, len(value))
3309

    
3310
    data.append(formatted)
3311

    
3312
  if separator is not None:
3313
    # Return early if a separator is used
3314
    return [separator.join(row) for row in data]
3315

    
3316
  if columns and not columns[-1].align_right:
3317
    # Avoid unnecessary spaces at end of line
3318
    colwidth[-1] = 0
3319

    
3320
  # Build format string
3321
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3322
                  for col, width in zip(columns, colwidth)])
3323

    
3324
  return [fmt % tuple(row) for row in data]
3325

    
3326

    
3327
def FormatTimestamp(ts):
3328
  """Formats a given timestamp.
3329

3330
  @type ts: timestamp
3331
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3332

3333
  @rtype: string
3334
  @return: a string with the formatted timestamp
3335

3336
  """
3337
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3338
    return "?"
3339

    
3340
  (sec, usecs) = ts
3341
  return utils.FormatTime(sec, usecs=usecs)
3342

    
3343

    
3344
def ParseTimespec(value):
3345
  """Parse a time specification.
3346

3347
  The following suffixed will be recognized:
3348

3349
    - s: seconds
3350
    - m: minutes
3351
    - h: hours
3352
    - d: day
3353
    - w: weeks
3354

3355
  Without any suffix, the value will be taken to be in seconds.
3356

3357
  """
3358
  value = str(value)
3359
  if not value:
3360
    raise errors.OpPrereqError("Empty time specification passed",
3361
                               errors.ECODE_INVAL)
3362
  suffix_map = {
3363
    "s": 1,
3364
    "m": 60,
3365
    "h": 3600,
3366
    "d": 86400,
3367
    "w": 604800,
3368
    }
3369
  if value[-1] not in suffix_map:
3370
    try:
3371
      value = int(value)
3372
    except (TypeError, ValueError):
3373
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3374
                                 errors.ECODE_INVAL)
3375
  else:
3376
    multiplier = suffix_map[value[-1]]
3377
    value = value[:-1]
3378
    if not value: # no data left after stripping the suffix
3379
      raise errors.OpPrereqError("Invalid time specification (only"
3380
                                 " suffix passed)", errors.ECODE_INVAL)
3381
    try:
3382
      value = int(value) * multiplier
3383
    except (TypeError, ValueError):
3384
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3385
                                 errors.ECODE_INVAL)
3386
  return value
3387

    
3388

    
3389
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3390
                   filter_master=False, nodegroup=None):
3391
  """Returns the names of online nodes.
3392

3393
  This function will also log a warning on stderr with the names of
3394
  the online nodes.
3395

3396
  @param nodes: if not empty, use only this subset of nodes (minus the
3397
      offline ones)
3398
  @param cl: if not None, luxi client to use
3399
  @type nowarn: boolean
3400
  @param nowarn: by default, this function will output a note with the
3401
      offline nodes that are skipped; if this parameter is True the
3402
      note is not displayed
3403
  @type secondary_ips: boolean
3404
  @param secondary_ips: if True, return the secondary IPs instead of the
3405
      names, useful for doing network traffic over the replication interface
3406
      (if any)
3407
  @type filter_master: boolean
3408
  @param filter_master: if True, do not return the master node in the list
3409
      (useful in coordination with secondary_ips where we cannot check our
3410
      node name against the list)
3411
  @type nodegroup: string
3412
  @param nodegroup: If set, only return nodes in this node group
3413

3414
  """
3415
  if cl is None:
3416
    cl = GetClient()
3417

    
3418
  qfilter = []
3419

    
3420
  if nodes:
3421
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3422

    
3423
  if nodegroup is not None:
3424
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3425
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3426

    
3427
  if filter_master:
3428
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3429

    
3430
  if qfilter:
3431
    if len(qfilter) > 1:
3432
      final_filter = [qlang.OP_AND] + qfilter
3433
    else:
3434
      assert len(qfilter) == 1
3435
      final_filter = qfilter[0]
3436
  else:
3437
    final_filter = None
3438

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

    
3441
  def _IsOffline(row):
3442
    (_, (_, offline), _) = row
3443
    return offline
3444

    
3445
  def _GetName(row):
3446
    ((_, name), _, _) = row
3447
    return name
3448

    
3449
  def _GetSip(row):
3450
    (_, _, (_, sip)) = row
3451
    return sip
3452

    
3453
  (offline, online) = compat.partition(result.data, _IsOffline)
3454

    
3455
  if offline and not nowarn:
3456
    ToStderr("Note: skipping offline node(s): %s" %
3457
             utils.CommaJoin(map(_GetName, offline)))
3458

    
3459
  if secondary_ips:
3460
    fn = _GetSip
3461
  else:
3462
    fn = _GetName
3463

    
3464
  return map(fn, online)
3465

    
3466

    
3467
def _ToStream(stream, txt, *args):
3468
  """Write a message to a stream, bypassing the logging system
3469

3470
  @type stream: file object
3471
  @param stream: the file to which we should write
3472
  @type txt: str
3473
  @param txt: the message
3474

3475
  """
3476
  try:
3477
    if args:
3478
      args = tuple(args)
3479
      stream.write(txt % args)
3480
    else:
3481
      stream.write(txt)
3482
    stream.write("\n")
3483
    stream.flush()
3484
  except IOError, err:
3485
    if err.errno == errno.EPIPE:
3486
      # our terminal went away, we'll exit
3487
      sys.exit(constants.EXIT_FAILURE)
3488
    else:
3489
      raise
3490

    
3491

    
3492
def ToStdout(txt, *args):
3493
  """Write a message to stdout only, bypassing the logging system
3494

3495
  This is just a wrapper over _ToStream.
3496

3497
  @type txt: str
3498
  @param txt: the message
3499

3500
  """
3501
  _ToStream(sys.stdout, txt, *args)
3502

    
3503

    
3504
def ToStderr(txt, *args):
3505
  """Write a message to stderr only, bypassing the logging system
3506

3507
  This is just a wrapper over _ToStream.
3508

3509
  @type txt: str
3510
  @param txt: the message
3511

3512
  """
3513
  _ToStream(sys.stderr, txt, *args)
3514

    
3515

    
3516
class JobExecutor(object):
3517
  """Class which manages the submission and execution of multiple jobs.
3518

3519
  Note that instances of this class should not be reused between
3520
  GetResults() calls.
3521

3522
  """
3523
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3524
    self.queue = []
3525
    if cl is None:
3526
      cl = GetClient()
3527
    self.cl = cl
3528
    self.verbose = verbose
3529
    self.jobs = []
3530
    self.opts = opts
3531
    self.feedback_fn = feedback_fn
3532
    self._counter = itertools.count()
3533

    
3534
  @staticmethod
3535
  def _IfName(name, fmt):
3536
    """Helper function for formatting name.
3537

3538
    """
3539
    if name:
3540
      return fmt % name
3541

    
3542
    return ""
3543

    
3544
  def QueueJob(self, name, *ops):
3545
    """Record a job for later submit.
3546

3547
    @type name: string
3548
    @param name: a description of the job, will be used in WaitJobSet
3549

3550
    """
3551
    SetGenericOpcodeOpts(ops, self.opts)
3552
    self.queue.append((self._counter.next(), name, ops))
3553

    
3554
  def AddJobId(self, name, status, job_id):
3555
    """Adds a job ID to the internal queue.
3556

3557
    """
3558
    self.jobs.append((self._counter.next(), status, job_id, name))
3559

    
3560
  def SubmitPending(self, each=False):
3561
    """Submit all pending jobs.
3562

3563
    """
3564
    if each:
3565
      results = []
3566
      for (_, _, ops) in self.queue:
3567
        # SubmitJob will remove the success status, but raise an exception if
3568
        # the submission fails, so we'll notice that anyway.
3569
        results.append([True, self.cl.SubmitJob(ops)[0]])
3570
    else:
3571
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3572
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3573
      self.jobs.append((idx, status, data, name))
3574

    
3575
  def _ChooseJob(self):
3576
    """Choose a non-waiting/queued job to poll next.
3577

3578
    """
3579
    assert self.jobs, "_ChooseJob called with empty job list"
3580

    
3581
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3582
                               ["status"])
3583
    assert result
3584

    
3585
    for job_data, status in zip(self.jobs, result):
3586
      if (isinstance(status, list) and status and
3587
          status[0] in (constants.JOB_STATUS_QUEUED,
3588
                        constants.JOB_STATUS_WAITING,
3589
                        constants.JOB_STATUS_CANCELING)):
3590
        # job is still present and waiting
3591
        continue
3592
      # good candidate found (either running job or lost job)
3593
      self.jobs.remove(job_data)
3594
      return job_data
3595

    
3596
    # no job found
3597
    return self.jobs.pop(0)
3598

    
3599
  def GetResults(self):
3600
    """Wait for and return the results of all jobs.
3601

3602
    @rtype: list
3603
    @return: list of tuples (success, job results), in the same order
3604
        as the submitted jobs; if a job has failed, instead of the result
3605
        there will be the error message
3606

3607
    """
3608
    if not self.jobs:
3609
      self.SubmitPending()
3610
    results = []
3611
    if self.verbose:
3612
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3613
      if ok_jobs:
3614
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3615

    
3616
    # first, remove any non-submitted jobs
3617
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3618
    for idx, _, jid, name in failures:
3619
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3620
      results.append((idx, False, jid))
3621

    
3622
    while self.jobs:
3623
      (idx, _, jid, name) = self._ChooseJob()
3624
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3625
      try:
3626
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3627
        success = True
3628
      except errors.JobLost, err:
3629
        _, job_result = FormatError(err)
3630
        ToStderr("Job %s%s has been archived, cannot check its result",
3631
                 jid, self._IfName(name, " for %s"))
3632
        success = False
3633
      except (errors.GenericError, luxi.ProtocolError), err:
3634
        _, job_result = FormatError(err)
3635
        success = False
3636
        # the error message will always be shown, verbose or not
3637
        ToStderr("Job %s%s has failed: %s",
3638
                 jid, self._IfName(name, " for %s"), job_result)
3639

    
3640
      results.append((idx, success, job_result))
3641

    
3642
    # sort based on the index, then drop it
3643
    results.sort()
3644
    results = [i[1:] for i in results]
3645

    
3646
    return results
3647

    
3648
  def WaitOrShow(self, wait):
3649
    """Wait for job results or only print the job IDs.
3650

3651
    @type wait: boolean
3652
    @param wait: whether to wait or not
3653

3654
    """
3655
    if wait:
3656
      return self.GetResults()
3657
    else:
3658
      if not self.jobs:
3659
        self.SubmitPending()
3660
      for _, status, result, name in self.jobs:
3661
        if status:
3662
          ToStdout("%s: %s", result, name)
3663
        else:
3664
          ToStderr("Failure for %s: %s", name, result)
3665
      return [row[1:3] for row in self.jobs]
3666

    
3667

    
3668
def FormatParamsDictInfo(param_dict, actual):
3669
  """Formats a parameter dictionary.
3670

3671
  @type param_dict: dict
3672
  @param param_dict: the own parameters
3673
  @type actual: dict
3674
  @param actual: the current parameter set (including defaults)
3675
  @rtype: dict
3676
  @return: dictionary where the value of each parameter is either a fully
3677
      formatted string or a dictionary containing formatted strings
3678

3679
  """
3680
  ret = {}
3681
  for (key, data) in actual.items():
3682
    if isinstance(data, dict) and data:
3683
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3684
    else:
3685
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3686
  return ret
3687

    
3688

    
3689
def _FormatListInfoDefault(data, def_data):
3690
  if data is not None:
3691
    ret = utils.CommaJoin(data)
3692
  else:
3693
    ret = "default (%s)" % utils.CommaJoin(def_data)
3694
  return ret
3695

    
3696

    
3697
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3698
  """Formats an instance policy.
3699

3700
  @type custom_ipolicy: dict
3701
  @param custom_ipolicy: own policy
3702
  @type eff_ipolicy: dict
3703
  @param eff_ipolicy: effective policy (including defaults); ignored for
3704
      cluster
3705
  @type iscluster: bool
3706
  @param iscluster: the policy is at cluster level
3707
  @rtype: list of pairs
3708
  @return: formatted data, suitable for L{PrintGenericInfo}
3709

3710
  """
3711
  if iscluster:
3712
    eff_ipolicy = custom_ipolicy
3713

    
3714
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3715
  ret = [
3716
    (key,
3717
     FormatParamsDictInfo(custom_minmax.get(key, {}),
3718
                          eff_ipolicy[constants.ISPECS_MINMAX][key]))
3719
    for key in constants.ISPECS_MINMAX_KEYS
3720
    ]
3721
  if iscluster:
3722
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3723
    ret.append(
3724
      (constants.ISPECS_STD,
3725
       FormatParamsDictInfo(stdspecs, stdspecs))
3726
      )
3727

    
3728
  ret.append(
3729
    ("enabled disk templates",
3730
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3731
                            eff_ipolicy[constants.IPOLICY_DTS]))
3732
    )
3733
  ret.extend([
3734
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3735
    for key in constants.IPOLICY_PARAMETERS
3736
    ])
3737
  return ret
3738

    
3739

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

3743
  This function is used to request confirmation for doing an operation
3744
  on a given list of list_type.
3745

3746
  @type names: list
3747
  @param names: the list of names that we display when
3748
      we ask for confirmation
3749
  @type list_type: str
3750
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3751
  @type text: str
3752
  @param text: the operation that the user should confirm
3753
  @rtype: boolean
3754
  @return: True or False depending on user's confirmation.
3755

3756
  """
3757
  count = len(names)
3758
  msg = ("The %s will operate on %d %s.\n%s"
3759
         "Do you want to continue?" % (text, count, list_type, extra))
3760
  affected = (("\nAffected %s:\n" % list_type) +
3761
              "\n".join(["  %s" % name for name in names]))
3762

    
3763
  choices = [("y", True, "Yes, execute the %s" % text),
3764
             ("n", False, "No, abort the %s" % text)]
3765

    
3766
  if count > 20:
3767
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3768
    question = msg
3769
  else:
3770
    question = msg + affected
3771

    
3772
  choice = AskUser(question, choices)
3773
  if choice == "v":
3774
    choices.pop(1)
3775
    choice = AskUser(msg + affected, choices)
3776
  return choice
3777

    
3778

    
3779
def _MaybeParseUnit(elements):
3780
  """Parses and returns an array of potential values with units.
3781

3782
  """
3783
  parsed = {}
3784
  for k, v in elements.items():
3785
    if v == constants.VALUE_DEFAULT:
3786
      parsed[k] = v
3787
    else:
3788
      parsed[k] = utils.ParseUnit(v)
3789
  return parsed
3790

    
3791

    
3792
def _InitIspecsFromOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
3793
                        ispecs_disk_count, ispecs_disk_size, ispecs_nic_count,
3794
                        group_ipolicy, allowed_values):
3795
  try:
3796
    if ispecs_mem_size:
3797
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
3798
    if ispecs_disk_size:
3799
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
3800
  except (TypeError, ValueError, errors.UnitParseError), err:
3801
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
3802
                               " in policy: %s" %
3803
                               (ispecs_disk_size, ispecs_mem_size, err),
3804
                               errors.ECODE_INVAL)
3805

    
3806
  # prepare ipolicy dict
3807
  ispecs_transposed = {
3808
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
3809
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
3810
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
3811
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
3812
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
3813
    }
3814

    
3815
  # first, check that the values given are correct
3816
  if group_ipolicy:
3817
    forced_type = TISPECS_GROUP_TYPES
3818
  else:
3819
    forced_type = TISPECS_CLUSTER_TYPES
3820
  for specs in ispecs_transposed.values():
3821
    utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
3822

    
3823
  # then transpose
3824
  ispecs = {
3825
    constants.ISPECS_MIN: {},
3826
    constants.ISPECS_MAX: {},
3827
    constants.ISPECS_STD: {},
3828
    }
3829
  for (name, specs) in ispecs_transposed.iteritems():
3830
    assert name in constants.ISPECS_PARAMETERS
3831
    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
3832
      assert key in ispecs
3833
      ispecs[key][name] = val
3834
  for key in constants.ISPECS_MINMAX_KEYS:
3835
    ipolicy[constants.ISPECS_MINMAX][key] = ispecs[key]
3836
  ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
3837

    
3838

    
3839
def CreateIPolicyFromOpts(ispecs_mem_size=None,
3840
                          ispecs_cpu_count=None,
3841
                          ispecs_disk_count=None,
3842
                          ispecs_disk_size=None,
3843
                          ispecs_nic_count=None,
3844
                          ipolicy_disk_templates=None,
3845
                          ipolicy_vcpu_ratio=None,
3846
                          ipolicy_spindle_ratio=None,
3847
                          group_ipolicy=False,
3848
                          allowed_values=None,
3849
                          fill_all=False):
3850
  """Creation of instance policy based on command line options.
3851

3852
  @param fill_all: whether for cluster policies we should ensure that
3853
    all values are filled
3854

3855

3856
  """
3857

    
3858
  ipolicy_out = objects.MakeEmptyIPolicy()
3859
  _InitIspecsFromOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
3860
                      ispecs_disk_count, ispecs_disk_size, ispecs_nic_count,
3861
                      group_ipolicy, allowed_values)
3862

    
3863
  if ipolicy_disk_templates is not None:
3864
    if allowed_values and ipolicy_disk_templates in allowed_values:
3865
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
3866
    else:
3867
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
3868
  if ipolicy_vcpu_ratio is not None:
3869
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
3870
  if ipolicy_spindle_ratio is not None:
3871
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
3872

    
3873
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
3874

    
3875
  if not group_ipolicy and fill_all:
3876
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
3877

    
3878
  return ipolicy_out
3879

    
3880

    
3881
def _SerializeGenericInfo(buf, data, level, afterkey=False):
3882
  """Formatting core of L{PrintGenericInfo}.
3883

3884
  @param buf: (string) stream to accumulate the result into
3885
  @param data: data to format
3886
  @type level: int
3887
  @param level: depth in the data hierarchy, used for indenting
3888
  @type afterkey: bool
3889
  @param afterkey: True when we are in the middle of a line after a key (used
3890
      to properly add newlines or indentation)
3891

3892
  """
3893
  baseind = "  "
3894
  if isinstance(data, dict):
3895
    if not data:
3896
      buf.write("\n")
3897
    else:
3898
      if afterkey:
3899
        buf.write("\n")
3900
        doindent = True
3901
      else:
3902
        doindent = False
3903
      for key in sorted(data):
3904
        if doindent:
3905
          buf.write(baseind * level)
3906
        else:
3907
          doindent = True
3908
        buf.write(key)
3909
        buf.write(": ")
3910
        _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True)
3911
  elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple):
3912
    # list of tuples (an ordered dictionary)
3913
    if afterkey:
3914
      buf.write("\n")
3915
      doindent = True
3916
    else:
3917
      doindent = False
3918
    for (key, val) in data:
3919
      if doindent:
3920
        buf.write(baseind * level)
3921
      else:
3922
        doindent = True
3923
      buf.write(key)
3924
      buf.write(": ")
3925
      _SerializeGenericInfo(buf, val, level + 1, afterkey=True)
3926
  elif isinstance(data, list):
3927
    if not data:
3928
      buf.write("\n")
3929
    else:
3930
      if afterkey:
3931
        buf.write("\n")
3932
        doindent = True
3933
      else:
3934
        doindent = False
3935
      for item in data:
3936
        if doindent:
3937
          buf.write(baseind * level)
3938
        else:
3939
          doindent = True
3940
        buf.write("-")
3941
        buf.write(baseind[1:])
3942
        _SerializeGenericInfo(buf, item, level + 1)
3943
  else:
3944
    # This branch should be only taken for strings, but it's practically
3945
    # impossible to guarantee that no other types are produced somewhere
3946
    buf.write(str(data))
3947
    buf.write("\n")
3948

    
3949

    
3950
def PrintGenericInfo(data):
3951
  """Print information formatted according to the hierarchy.
3952

3953
  The output is a valid YAML string.
3954

3955
  @param data: the data to print. It's a hierarchical structure whose elements
3956
      can be:
3957
        - dictionaries, where keys are strings and values are of any of the
3958
          types listed here
3959
        - lists of pairs (key, value), where key is a string and value is of
3960
          any of the types listed here; it's a way to encode ordered
3961
          dictionaries
3962
        - lists of any of the types listed here
3963
        - strings
3964

3965
  """
3966
  buf = StringIO()
3967
  _SerializeGenericInfo(buf, data, 0)
3968
  ToStdout(buf.getvalue().rstrip("\n"))