Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ f824ae42

History | View | Annotate | Download (128 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):
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, the others will
572
  have value=True.
573

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

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

    
602

    
603
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
604
  """Custom parser for ident:key=val,key=val options.
605

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

609
  """
610
  if ":" not in value:
611
    ident, rest = value, ""
612
  else:
613
    ident, rest = value.split(":", 1)
614

    
615
  if ident.startswith(NO_PREFIX):
616
    if rest:
617
      msg = "Cannot pass options when removing parameter groups: %s" % value
618
      raise errors.ParameterError(msg)
619
    retval = (ident[len(NO_PREFIX):], False)
620
  elif (ident.startswith(UN_PREFIX) and
621
        (len(ident) <= len(UN_PREFIX) or
622
         not ident[len(UN_PREFIX)][0].isdigit())):
623
    if rest:
624
      msg = "Cannot pass options when removing parameter groups: %s" % value
625
      raise errors.ParameterError(msg)
626
    retval = (ident[len(UN_PREFIX):], None)
627
  else:
628
    kv_dict = _SplitKeyVal(opt, rest)
629
    retval = (ident, kv_dict)
630
  return retval
631

    
632

    
633
def check_key_val(option, opt, value):  # pylint: disable=W0613
634
  """Custom parser class for key=val,key=val options.
635

636
  This will store the parsed values as a dict {key: val}.
637

638
  """
639
  return _SplitKeyVal(opt, value)
640

    
641

    
642
def check_bool(option, opt, value): # pylint: disable=W0613
643
  """Custom parser for yes/no options.
644

645
  This will store the parsed value as either True or False.
646

647
  """
648
  value = value.lower()
649
  if value == constants.VALUE_FALSE or value == "no":
650
    return False
651
  elif value == constants.VALUE_TRUE or value == "yes":
652
    return True
653
  else:
654
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
655

    
656

    
657
def check_list(option, opt, value): # pylint: disable=W0613
658
  """Custom parser for comma-separated lists.
659

660
  """
661
  # we have to make this explicit check since "".split(",") is [""],
662
  # not an empty list :(
663
  if not value:
664
    return []
665
  else:
666
    return utils.UnescapeAndSplit(value)
667

    
668

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

672
  """
673
  value = value.lower()
674

    
675
  if value == constants.VALUE_DEFAULT:
676
    return value
677
  else:
678
    return float(value)
679

    
680

    
681
# completion_suggestion is normally a list. Using numeric values not evaluating
682
# to False for dynamic completion.
683
(OPT_COMPL_MANY_NODES,
684
 OPT_COMPL_ONE_NODE,
685
 OPT_COMPL_ONE_INSTANCE,
686
 OPT_COMPL_ONE_OS,
687
 OPT_COMPL_ONE_EXTSTORAGE,
688
 OPT_COMPL_ONE_IALLOCATOR,
689
 OPT_COMPL_ONE_NETWORK,
690
 OPT_COMPL_INST_ADD_NODES,
691
 OPT_COMPL_ONE_NODEGROUP) = range(100, 109)
692

    
693
OPT_COMPL_ALL = compat.UniqueFrozenset([
694
  OPT_COMPL_MANY_NODES,
695
  OPT_COMPL_ONE_NODE,
696
  OPT_COMPL_ONE_INSTANCE,
697
  OPT_COMPL_ONE_OS,
698
  OPT_COMPL_ONE_EXTSTORAGE,
699
  OPT_COMPL_ONE_IALLOCATOR,
700
  OPT_COMPL_ONE_NETWORK,
701
  OPT_COMPL_INST_ADD_NODES,
702
  OPT_COMPL_ONE_NODEGROUP,
703
  ])
704

    
705

    
706
class CliOption(Option):
707
  """Custom option class for optparse.
708

709
  """
710
  ATTRS = Option.ATTRS + [
711
    "completion_suggest",
712
    ]
713
  TYPES = Option.TYPES + (
714
    "identkeyval",
715
    "keyval",
716
    "unit",
717
    "bool",
718
    "list",
719
    "maybefloat",
720
    )
721
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
722
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
723
  TYPE_CHECKER["keyval"] = check_key_val
724
  TYPE_CHECKER["unit"] = check_unit
725
  TYPE_CHECKER["bool"] = check_bool
726
  TYPE_CHECKER["list"] = check_list
727
  TYPE_CHECKER["maybefloat"] = check_maybefloat
728

    
729

    
730
# optparse.py sets make_option, so we do it for our own option class, too
731
cli_option = CliOption
732

    
733

    
734
_YORNO = "yes|no"
735

    
736
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
737
                       help="Increase debugging level")
738

    
739
NOHDR_OPT = cli_option("--no-headers", default=False,
740
                       action="store_true", dest="no_headers",
741
                       help="Don't display column headers")
742

    
743
SEP_OPT = cli_option("--separator", default=None,
744
                     action="store", dest="separator",
745
                     help=("Separator between output fields"
746
                           " (defaults to one space)"))
747

    
748
USEUNITS_OPT = cli_option("--units", default=None,
749
                          dest="units", choices=("h", "m", "g", "t"),
750
                          help="Specify units for output (one of h/m/g/t)")
751

    
752
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
753
                        type="string", metavar="FIELDS",
754
                        help="Comma separated list of output fields")
755

    
756
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
757
                       default=False, help="Force the operation")
758

    
759
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
760
                         default=False, help="Do not require confirmation")
761

    
762
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
763
                                  action="store_true", default=False,
764
                                  help=("Ignore offline nodes and do as much"
765
                                        " as possible"))
766

    
767
TAG_ADD_OPT = cli_option("--tags", dest="tags",
768
                         default=None, help="Comma-separated list of instance"
769
                                            " tags")
770

    
771
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
772
                         default=None, help="File with tag names")
773

    
774
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
775
                        default=False, action="store_true",
776
                        help=("Submit the job and return the job ID, but"
777
                              " don't wait for the job to finish"))
778

    
779
SYNC_OPT = cli_option("--sync", dest="do_locking",
780
                      default=False, action="store_true",
781
                      help=("Grab locks while doing the queries"
782
                            " in order to ensure more consistent results"))
783

    
784
DRY_RUN_OPT = cli_option("--dry-run", default=False,
785
                         action="store_true",
786
                         help=("Do not execute the operation, just run the"
787
                               " check steps and verify if it could be"
788
                               " executed"))
789

    
790
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
791
                         action="store_true",
792
                         help="Increase the verbosity of the operation")
793

    
794
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
795
                              action="store_true", dest="simulate_errors",
796
                              help="Debugging option that makes the operation"
797
                              " treat most runtime checks as failed")
798

    
799
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
800
                        default=True, action="store_false",
801
                        help="Don't wait for sync (DANGEROUS!)")
802

    
803
WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync",
804
                        default=False, action="store_true",
805
                        help="Wait for disks to sync")
806

    
807
ONLINE_INST_OPT = cli_option("--online", dest="online_inst",
808
                             action="store_true", default=False,
809
                             help="Enable offline instance")
810

    
811
OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst",
812
                              action="store_true", default=False,
813
                              help="Disable down instance")
814

    
815
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
816
                               help=("Custom disk setup (%s)" %
817
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
818
                               default=None, metavar="TEMPL",
819
                               choices=list(constants.DISK_TEMPLATES))
820

    
821
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
822
                        help="Do not create any network cards for"
823
                        " the instance")
824

    
825
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
826
                               help="Relative path under default cluster-wide"
827
                               " file storage dir to store file-based disks",
828
                               default=None, metavar="<DIR>")
829

    
830
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
831
                                  help="Driver to use for image files",
832
                                  default="loop", metavar="<DRIVER>",
833
                                  choices=list(constants.FILE_DRIVER))
834

    
835
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
836
                            help="Select nodes for the instance automatically"
837
                            " using the <NAME> iallocator plugin",
838
                            default=None, type="string",
839
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
840

    
841
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
842
                                    metavar="<NAME>",
843
                                    help="Set the default instance"
844
                                    " allocator plugin",
845
                                    default=None, type="string",
846
                                    completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
847

    
848
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
849
                    metavar="<os>",
850
                    completion_suggest=OPT_COMPL_ONE_OS)
851

    
852
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
853
                          type="keyval", default={},
854
                          help="OS parameters")
855

    
856
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
857
                               action="store_true", default=False,
858
                               help="Force an unknown variant")
859

    
860
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
861
                            action="store_true", default=False,
862
                            help="Do not install the OS (will"
863
                            " enable no-start)")
864

    
865
NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes",
866
                                dest="allow_runtime_chgs",
867
                                default=True, action="store_false",
868
                                help="Don't allow runtime changes")
869

    
870
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
871
                         type="keyval", default={},
872
                         help="Backend parameters")
873

    
874
HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval",
875
                        default={}, dest="hvparams",
876
                        help="Hypervisor parameters")
877

    
878
DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
879
                             help="Disk template parameters, in the format"
880
                             " template:option=value,option=value,...",
881
                             type="identkeyval", action="append", default=[])
882

    
883
SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
884
                                 type="keyval", default={},
885
                                 help="Memory size specs: list of key=value,"
886
                                " where key is one of min, max, std"
887
                                 " (in MB or using a unit)")
888

    
889
SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
890
                                 type="keyval", default={},
891
                                 help="CPU count specs: list of key=value,"
892
                                 " where key is one of min, max, std")
893

    
894
SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
895
                                  dest="ispecs_disk_count",
896
                                  type="keyval", default={},
897
                                  help="Disk count specs: list of key=value,"
898
                                  " where key is one of min, max, std")
899

    
900
SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
901
                                 type="keyval", default={},
902
                                 help="Disk size specs: list of key=value,"
903
                                 " where key is one of min, max, std"
904
                                 " (in MB or using a unit)")
905

    
906
SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
907
                                 type="keyval", default={},
908
                                 help="NIC count specs: list of key=value,"
909
                                 " where key is one of min, max, std")
910

    
911
IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
912
                                    dest="ipolicy_disk_templates",
913
                                    type="list", default=None,
914
                                    help="Comma-separated list of"
915
                                    " enabled disk templates")
916

    
917
IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio",
918
                                 dest="ipolicy_vcpu_ratio",
919
                                 type="maybefloat", default=None,
920
                                 help="The maximum allowed vcpu-to-cpu ratio")
921

    
922
IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio",
923
                                   dest="ipolicy_spindle_ratio",
924
                                   type="maybefloat", default=None,
925
                                   help=("The maximum allowed instances to"
926
                                         " spindle ratio"))
927

    
928
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
929
                            help="Hypervisor and hypervisor options, in the"
930
                            " format hypervisor:option=value,option=value,...",
931
                            default=None, type="identkeyval")
932

    
933
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
934
                        help="Hypervisor and hypervisor options, in the"
935
                        " format hypervisor:option=value,option=value,...",
936
                        default=[], action="append", type="identkeyval")
937

    
938
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
939
                           action="store_false",
940
                           help="Don't check that the instance's IP"
941
                           " is alive")
942

    
943
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
944
                             default=True, action="store_false",
945
                             help="Don't check that the instance's name"
946
                             " is resolvable")
947

    
948
NET_OPT = cli_option("--net",
949
                     help="NIC parameters", default=[],
950
                     dest="nics", action="append", type="identkeyval")
951

    
952
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
953
                      dest="disks", action="append", type="identkeyval")
954

    
955
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
956
                         help="Comma-separated list of disks"
957
                         " indices to act on (e.g. 0,2) (optional,"
958
                         " defaults to all disks)")
959

    
960
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
961
                         help="Enforces a single-disk configuration using the"
962
                         " given disk size, in MiB unless a suffix is used",
963
                         default=None, type="unit", metavar="<size>")
964

    
965
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
966
                                dest="ignore_consistency",
967
                                action="store_true", default=False,
968
                                help="Ignore the consistency of the disks on"
969
                                " the secondary")
970

    
971
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
972
                                dest="allow_failover",
973
                                action="store_true", default=False,
974
                                help="If migration is not possible fallback to"
975
                                     " failover")
976

    
977
NONLIVE_OPT = cli_option("--non-live", dest="live",
978
                         default=True, action="store_false",
979
                         help="Do a non-live migration (this usually means"
980
                         " freeze the instance, save the state, transfer and"
981
                         " only then resume running on the secondary node)")
982

    
983
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
984
                                default=None,
985
                                choices=list(constants.HT_MIGRATION_MODES),
986
                                help="Override default migration mode (choose"
987
                                " either live or non-live")
988

    
989
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
990
                                help="Target node and optional secondary node",
991
                                metavar="<pnode>[:<snode>]",
992
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
993

    
994
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
995
                           action="append", metavar="<node>",
996
                           help="Use only this node (can be used multiple"
997
                           " times, if not given defaults to all nodes)",
998
                           completion_suggest=OPT_COMPL_ONE_NODE)
999

    
1000
NODEGROUP_OPT_NAME = "--node-group"
1001
NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME,
1002
                           dest="nodegroup",
1003
                           help="Node group (name or uuid)",
1004
                           metavar="<nodegroup>",
1005
                           default=None, type="string",
1006
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1007

    
1008
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
1009
                             metavar="<node>",
1010
                             completion_suggest=OPT_COMPL_ONE_NODE)
1011

    
1012
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
1013
                         action="store_false",
1014
                         help="Don't start the instance after creation")
1015

    
1016
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
1017
                         action="store_true", default=False,
1018
                         help="Show command instead of executing it")
1019

    
1020
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
1021
                         default=False, action="store_true",
1022
                         help="Instead of performing the migration, try to"
1023
                         " recover from a failed cleanup. This is safe"
1024
                         " to run even if the instance is healthy, but it"
1025
                         " will create extra replication traffic and "
1026
                         " disrupt briefly the replication (like during the"
1027
                         " migration")
1028

    
1029
STATIC_OPT = cli_option("-s", "--static", dest="static",
1030
                        action="store_true", default=False,
1031
                        help="Only show configuration data, not runtime data")
1032

    
1033
ALL_OPT = cli_option("--all", dest="show_all",
1034
                     default=False, action="store_true",
1035
                     help="Show info on all instances on the cluster."
1036
                     " This can take a long time to run, use wisely")
1037

    
1038
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
1039
                           action="store_true", default=False,
1040
                           help="Interactive OS reinstall, lists available"
1041
                           " OS templates for selection")
1042

    
1043
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
1044
                                 action="store_true", default=False,
1045
                                 help="Remove the instance from the cluster"
1046
                                 " configuration even if there are failures"
1047
                                 " during the removal process")
1048

    
1049
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
1050
                                        dest="ignore_remove_failures",
1051
                                        action="store_true", default=False,
1052
                                        help="Remove the instance from the"
1053
                                        " cluster configuration even if there"
1054
                                        " are failures during the removal"
1055
                                        " process")
1056

    
1057
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
1058
                                 action="store_true", default=False,
1059
                                 help="Remove the instance from the cluster")
1060

    
1061
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
1062
                               help="Specifies the new node for the instance",
1063
                               metavar="NODE", default=None,
1064
                               completion_suggest=OPT_COMPL_ONE_NODE)
1065

    
1066
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
1067
                               help="Specifies the new secondary node",
1068
                               metavar="NODE", default=None,
1069
                               completion_suggest=OPT_COMPL_ONE_NODE)
1070

    
1071
NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node",
1072
                             help="Specifies the new primary node",
1073
                             metavar="<node>", default=None,
1074
                             completion_suggest=OPT_COMPL_ONE_NODE)
1075

    
1076
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
1077
                            default=False, action="store_true",
1078
                            help="Replace the disk(s) on the primary"
1079
                                 " node (applies only to internally mirrored"
1080
                                 " disk templates, e.g. %s)" %
1081
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
1082

    
1083
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
1084
                              default=False, action="store_true",
1085
                              help="Replace the disk(s) on the secondary"
1086
                                   " node (applies only to internally mirrored"
1087
                                   " disk templates, e.g. %s)" %
1088
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1089

    
1090
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
1091
                              default=False, action="store_true",
1092
                              help="Lock all nodes and auto-promote as needed"
1093
                              " to MC status")
1094

    
1095
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
1096
                              default=False, action="store_true",
1097
                              help="Automatically replace faulty disks"
1098
                                   " (applies only to internally mirrored"
1099
                                   " disk templates, e.g. %s)" %
1100
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
1101

    
1102
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
1103
                             default=False, action="store_true",
1104
                             help="Ignore current recorded size"
1105
                             " (useful for forcing activation when"
1106
                             " the recorded size is wrong)")
1107

    
1108
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
1109
                          metavar="<node>",
1110
                          completion_suggest=OPT_COMPL_ONE_NODE)
1111

    
1112
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
1113
                         metavar="<dir>")
1114

    
1115
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
1116
                              help="Specify the secondary ip for the node",
1117
                              metavar="ADDRESS", default=None)
1118

    
1119
READD_OPT = cli_option("--readd", dest="readd",
1120
                       default=False, action="store_true",
1121
                       help="Readd old node after replacing it")
1122

    
1123
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
1124
                                default=True, action="store_false",
1125
                                help="Disable SSH key fingerprint checking")
1126

    
1127
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
1128
                                 default=False, action="store_true",
1129
                                 help="Force the joining of a node")
1130

    
1131
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
1132
                    type="bool", default=None, metavar=_YORNO,
1133
                    help="Set the master_candidate flag on the node")
1134

    
1135
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
1136
                         type="bool", default=None,
1137
                         help=("Set the offline flag on the node"
1138
                               " (cluster does not communicate with offline"
1139
                               " nodes)"))
1140

    
1141
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
1142
                         type="bool", default=None,
1143
                         help=("Set the drained flag on the node"
1144
                               " (excluded from allocation operations)"))
1145

    
1146
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
1147
                              type="bool", default=None, metavar=_YORNO,
1148
                              help="Set the master_capable flag on the node")
1149

    
1150
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
1151
                          type="bool", default=None, metavar=_YORNO,
1152
                          help="Set the vm_capable flag on the node")
1153

    
1154
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
1155
                             type="bool", default=None, metavar=_YORNO,
1156
                             help="Set the allocatable flag on a volume")
1157

    
1158
NOLVM_STORAGE_OPT = cli_option("--no-lvm-storage", dest="lvm_storage",
1159
                               help="Disable support for lvm based instances"
1160
                               " (cluster-wide)",
1161
                               action="store_false", default=True)
1162

    
1163
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
1164
                            dest="enabled_hypervisors",
1165
                            help="Comma-separated list of hypervisors",
1166
                            type="string", default=None)
1167

    
1168
ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates",
1169
                                        dest="enabled_disk_templates",
1170
                                        help="Comma-separated list of "
1171
                                             "disk templates",
1172
                                        type="string", default=None)
1173

    
1174
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
1175
                            type="keyval", default={},
1176
                            help="NIC parameters")
1177

    
1178
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
1179
                         dest="candidate_pool_size", type="int",
1180
                         help="Set the candidate pool size")
1181

    
1182
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
1183
                         help=("Enables LVM and specifies the volume group"
1184
                               " name (cluster-wide) for disk allocation"
1185
                               " [%s]" % constants.DEFAULT_VG),
1186
                         metavar="VG", default=None)
1187

    
1188
YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it",
1189
                          help="Destroy cluster", action="store_true")
1190

    
1191
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
1192
                          help="Skip node agreement check (dangerous)",
1193
                          action="store_true", default=False)
1194

    
1195
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
1196
                            help="Specify the mac prefix for the instance IP"
1197
                            " addresses, in the format XX:XX:XX",
1198
                            metavar="PREFIX",
1199
                            default=None)
1200

    
1201
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
1202
                               help="Specify the node interface (cluster-wide)"
1203
                               " on which the master IP address will be added"
1204
                               " (cluster init default: %s)" %
1205
                               constants.DEFAULT_BRIDGE,
1206
                               metavar="NETDEV",
1207
                               default=None)
1208

    
1209
MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask",
1210
                                help="Specify the netmask of the master IP",
1211
                                metavar="NETMASK",
1212
                                default=None)
1213

    
1214
USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script",
1215
                                     dest="use_external_mip_script",
1216
                                     help="Specify whether to run a"
1217
                                     " user-provided script for the master"
1218
                                     " IP address turnup and"
1219
                                     " turndown operations",
1220
                                     type="bool", metavar=_YORNO, default=None)
1221

    
1222
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1223
                                help="Specify the default directory (cluster-"
1224
                                "wide) for storing the file-based disks [%s]" %
1225
                                pathutils.DEFAULT_FILE_STORAGE_DIR,
1226
                                metavar="DIR",
1227
                                default=pathutils.DEFAULT_FILE_STORAGE_DIR)
1228

    
1229
GLOBAL_SHARED_FILEDIR_OPT = cli_option(
1230
  "--shared-file-storage-dir",
1231
  dest="shared_file_storage_dir",
1232
  help="Specify the default directory (cluster-wide) for storing the"
1233
  " shared file-based disks [%s]" %
1234
  pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
1235
  metavar="SHAREDDIR", default=pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR)
1236

    
1237
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1238
                                   help="Don't modify %s" % pathutils.ETC_HOSTS,
1239
                                   action="store_false", default=True)
1240

    
1241
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1242
                                    help="Don't initialize SSH keys",
1243
                                    action="store_false", default=True)
1244

    
1245
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1246
                             help="Enable parseable error messages",
1247
                             action="store_true", default=False)
1248

    
1249
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1250
                          help="Skip N+1 memory redundancy tests",
1251
                          action="store_true", default=False)
1252

    
1253
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1254
                             help="Type of reboot: soft/hard/full",
1255
                             default=constants.INSTANCE_REBOOT_HARD,
1256
                             metavar="<REBOOT>",
1257
                             choices=list(constants.REBOOT_TYPES))
1258

    
1259
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1260
                                    dest="ignore_secondaries",
1261
                                    default=False, action="store_true",
1262
                                    help="Ignore errors from secondaries")
1263

    
1264
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1265
                            action="store_false", default=True,
1266
                            help="Don't shutdown the instance (unsafe)")
1267

    
1268
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1269
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1270
                         help="Maximum time to wait")
1271

    
1272
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1273
                                  dest="shutdown_timeout", type="int",
1274
                                  default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1275
                                  help="Maximum time to wait for instance"
1276
                                  " shutdown")
1277

    
1278
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1279
                          default=None,
1280
                          help=("Number of seconds between repetions of the"
1281
                                " command"))
1282

    
1283
EARLY_RELEASE_OPT = cli_option("--early-release",
1284
                               dest="early_release", default=False,
1285
                               action="store_true",
1286
                               help="Release the locks on the secondary"
1287
                               " node(s) early")
1288

    
1289
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1290
                                  dest="new_cluster_cert",
1291
                                  default=False, action="store_true",
1292
                                  help="Generate a new cluster certificate")
1293

    
1294
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1295
                           default=None,
1296
                           help="File containing new RAPI certificate")
1297

    
1298
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1299
                               default=None, action="store_true",
1300
                               help=("Generate a new self-signed RAPI"
1301
                                     " certificate"))
1302

    
1303
SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert",
1304
                            default=None,
1305
                            help="File containing new SPICE certificate")
1306

    
1307
SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert",
1308
                              default=None,
1309
                              help="File containing the certificate of the CA"
1310
                              " which signed the SPICE certificate")
1311

    
1312
NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate",
1313
                                dest="new_spice_cert", default=None,
1314
                                action="store_true",
1315
                                help=("Generate a new self-signed SPICE"
1316
                                      " certificate"))
1317

    
1318
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1319
                                    dest="new_confd_hmac_key",
1320
                                    default=False, action="store_true",
1321
                                    help=("Create a new HMAC key for %s" %
1322
                                          constants.CONFD))
1323

    
1324
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1325
                                       dest="cluster_domain_secret",
1326
                                       default=None,
1327
                                       help=("Load new new cluster domain"
1328
                                             " secret from file"))
1329

    
1330
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1331
                                           dest="new_cluster_domain_secret",
1332
                                           default=False, action="store_true",
1333
                                           help=("Create a new cluster domain"
1334
                                                 " secret"))
1335

    
1336
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1337
                              dest="use_replication_network",
1338
                              help="Whether to use the replication network"
1339
                              " for talking to the nodes",
1340
                              action="store_true", default=False)
1341

    
1342
MAINTAIN_NODE_HEALTH_OPT = \
1343
    cli_option("--maintain-node-health", dest="maintain_node_health",
1344
               metavar=_YORNO, default=None, type="bool",
1345
               help="Configure the cluster to automatically maintain node"
1346
               " health, by shutting down unknown instances, shutting down"
1347
               " unknown DRBD devices, etc.")
1348

    
1349
IDENTIFY_DEFAULTS_OPT = \
1350
    cli_option("--identify-defaults", dest="identify_defaults",
1351
               default=False, action="store_true",
1352
               help="Identify which saved instance parameters are equal to"
1353
               " the current cluster defaults and set them as such, instead"
1354
               " of marking them as overridden")
1355

    
1356
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1357
                         action="store", dest="uid_pool",
1358
                         help=("A list of user-ids or user-id"
1359
                               " ranges separated by commas"))
1360

    
1361
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1362
                          action="store", dest="add_uids",
1363
                          help=("A list of user-ids or user-id"
1364
                                " ranges separated by commas, to be"
1365
                                " added to the user-id pool"))
1366

    
1367
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1368
                             action="store", dest="remove_uids",
1369
                             help=("A list of user-ids or user-id"
1370
                                   " ranges separated by commas, to be"
1371
                                   " removed from the user-id pool"))
1372

    
1373
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1374
                              action="store", dest="reserved_lvs",
1375
                              help=("A comma-separated list of reserved"
1376
                                    " logical volumes names, that will be"
1377
                                    " ignored by cluster verify"))
1378

    
1379
ROMAN_OPT = cli_option("--roman",
1380
                       dest="roman_integers", default=False,
1381
                       action="store_true",
1382
                       help="Use roman numbers for positive integers")
1383

    
1384
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1385
                             action="store", default=None,
1386
                             help="Specifies usermode helper for DRBD")
1387

    
1388
NODRBD_STORAGE_OPT = cli_option("--no-drbd-storage", dest="drbd_storage",
1389
                                action="store_false", default=True,
1390
                                help="Disable support for DRBD")
1391

    
1392
PRIMARY_IP_VERSION_OPT = \
1393
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1394
               action="store", dest="primary_ip_version",
1395
               metavar="%d|%d" % (constants.IP4_VERSION,
1396
                                  constants.IP6_VERSION),
1397
               help="Cluster-wide IP version for primary IP")
1398

    
1399
SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
1400
                              action="store_true",
1401
                              help="Show machine name for every line in output")
1402

    
1403
FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
1404
                              action="store_true",
1405
                              help=("Hide successful results and show failures"
1406
                                    " only (determined by the exit code)"))
1407

    
1408
REASON_OPT = cli_option("--reason", default=None,
1409
                        help="The reason for executing the command")
1410

    
1411

    
1412
def _PriorityOptionCb(option, _, value, parser):
1413
  """Callback for processing C{--priority} option.
1414

1415
  """
1416
  value = _PRIONAME_TO_VALUE[value]
1417

    
1418
  setattr(parser.values, option.dest, value)
1419

    
1420

    
1421
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1422
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1423
                          choices=_PRIONAME_TO_VALUE.keys(),
1424
                          action="callback", type="choice",
1425
                          callback=_PriorityOptionCb,
1426
                          help="Priority for opcode processing")
1427

    
1428
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1429
                        type="bool", default=None, metavar=_YORNO,
1430
                        help="Sets the hidden flag on the OS")
1431

    
1432
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1433
                        type="bool", default=None, metavar=_YORNO,
1434
                        help="Sets the blacklisted flag on the OS")
1435

    
1436
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1437
                                     type="bool", metavar=_YORNO,
1438
                                     dest="prealloc_wipe_disks",
1439
                                     help=("Wipe disks prior to instance"
1440
                                           " creation"))
1441

    
1442
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1443
                             type="keyval", default=None,
1444
                             help="Node parameters")
1445

    
1446
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1447
                              action="store", metavar="POLICY", default=None,
1448
                              help="Allocation policy for the node group")
1449

    
1450
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1451
                              type="bool", metavar=_YORNO,
1452
                              dest="node_powered",
1453
                              help="Specify if the SoR for node is powered")
1454

    
1455
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1456
                             default=constants.OOB_TIMEOUT,
1457
                             help="Maximum time to wait for out-of-band helper")
1458

    
1459
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1460
                             default=constants.OOB_POWER_DELAY,
1461
                             help="Time in seconds to wait between power-ons")
1462

    
1463
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1464
                              action="store_true", default=False,
1465
                              help=("Whether command argument should be treated"
1466
                                    " as filter"))
1467

    
1468
NO_REMEMBER_OPT = cli_option("--no-remember",
1469
                             dest="no_remember",
1470
                             action="store_true", default=False,
1471
                             help="Perform but do not record the change"
1472
                             " in the configuration")
1473

    
1474
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1475
                              default=False, action="store_true",
1476
                              help="Evacuate primary instances only")
1477

    
1478
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1479
                                default=False, action="store_true",
1480
                                help="Evacuate secondary instances only"
1481
                                     " (applies only to internally mirrored"
1482
                                     " disk templates, e.g. %s)" %
1483
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1484

    
1485
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1486
                                action="store_true", default=False,
1487
                                help="Pause instance at startup")
1488

    
1489
TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>",
1490
                          help="Destination node group (name or uuid)",
1491
                          default=None, action="append",
1492
                          completion_suggest=OPT_COMPL_ONE_NODEGROUP)
1493

    
1494
IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[],
1495
                               action="append", dest="ignore_errors",
1496
                               choices=list(constants.CV_ALL_ECODES_STRINGS),
1497
                               help="Error code to be ignored")
1498

    
1499
DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state",
1500
                            action="append",
1501
                            help=("Specify disk state information in the"
1502
                                  " format"
1503
                                  " storage_type/identifier:option=value,...;"
1504
                                  " note this is unused for now"),
1505
                            type="identkeyval")
1506

    
1507
HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state",
1508
                          action="append",
1509
                          help=("Specify hypervisor state information in the"
1510
                                " format hypervisor:option=value,...;"
1511
                                " note this is unused for now"),
1512
                          type="identkeyval")
1513

    
1514
IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy",
1515
                                action="store_true", default=False,
1516
                                help="Ignore instance policy violations")
1517

    
1518
RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem",
1519
                             help="Sets the instance's runtime memory,"
1520
                             " ballooning it up or down to the new value",
1521
                             default=None, type="unit", metavar="<size>")
1522

    
1523
ABSOLUTE_OPT = cli_option("--absolute", dest="absolute",
1524
                          action="store_true", default=False,
1525
                          help="Marks the grow as absolute instead of the"
1526
                          " (default) relative mode")
1527

    
1528
NETWORK_OPT = cli_option("--network",
1529
                         action="store", default=None, dest="network",
1530
                         help="IP network in CIDR notation")
1531

    
1532
GATEWAY_OPT = cli_option("--gateway",
1533
                         action="store", default=None, dest="gateway",
1534
                         help="IP address of the router (gateway)")
1535

    
1536
ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips",
1537
                                  action="store", default=None,
1538
                                  dest="add_reserved_ips",
1539
                                  help="Comma-separated list of"
1540
                                  " reserved IPs to add")
1541

    
1542
REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips",
1543
                                     action="store", default=None,
1544
                                     dest="remove_reserved_ips",
1545
                                     help="Comma-delimited list of"
1546
                                     " reserved IPs to remove")
1547

    
1548
NETWORK6_OPT = cli_option("--network6",
1549
                          action="store", default=None, dest="network6",
1550
                          help="IP network in CIDR notation")
1551

    
1552
GATEWAY6_OPT = cli_option("--gateway6",
1553
                          action="store", default=None, dest="gateway6",
1554
                          help="IP6 address of the router (gateway)")
1555

    
1556
NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check",
1557
                                  dest="conflicts_check",
1558
                                  default=True,
1559
                                  action="store_false",
1560
                                  help="Don't check for conflicting IPs")
1561

    
1562
#: Options provided by all commands
1563
COMMON_OPTS = [DEBUG_OPT, REASON_OPT]
1564

    
1565
# common options for creating instances. add and import then add their own
1566
# specific ones.
1567
COMMON_CREATE_OPTS = [
1568
  BACKEND_OPT,
1569
  DISK_OPT,
1570
  DISK_TEMPLATE_OPT,
1571
  FILESTORE_DIR_OPT,
1572
  FILESTORE_DRIVER_OPT,
1573
  HYPERVISOR_OPT,
1574
  IALLOCATOR_OPT,
1575
  NET_OPT,
1576
  NODE_PLACEMENT_OPT,
1577
  NOIPCHECK_OPT,
1578
  NOCONFLICTSCHECK_OPT,
1579
  NONAMECHECK_OPT,
1580
  NONICS_OPT,
1581
  NWSYNC_OPT,
1582
  OSPARAMS_OPT,
1583
  OS_SIZE_OPT,
1584
  SUBMIT_OPT,
1585
  TAG_ADD_OPT,
1586
  DRY_RUN_OPT,
1587
  PRIORITY_OPT,
1588
  ]
1589

    
1590
# common instance policy options
1591
INSTANCE_POLICY_OPTS = [
1592
  SPECS_CPU_COUNT_OPT,
1593
  SPECS_DISK_COUNT_OPT,
1594
  SPECS_DISK_SIZE_OPT,
1595
  SPECS_MEM_SIZE_OPT,
1596
  SPECS_NIC_COUNT_OPT,
1597
  IPOLICY_DISK_TEMPLATES,
1598
  IPOLICY_VCPU_RATIO,
1599
  IPOLICY_SPINDLE_RATIO,
1600
  ]
1601

    
1602

    
1603
class _ShowUsage(Exception):
1604
  """Exception class for L{_ParseArgs}.
1605

1606
  """
1607
  def __init__(self, exit_error):
1608
    """Initializes instances of this class.
1609

1610
    @type exit_error: bool
1611
    @param exit_error: Whether to report failure on exit
1612

1613
    """
1614
    Exception.__init__(self)
1615
    self.exit_error = exit_error
1616

    
1617

    
1618
class _ShowVersion(Exception):
1619
  """Exception class for L{_ParseArgs}.
1620

1621
  """
1622

    
1623

    
1624
def _ParseArgs(binary, argv, commands, aliases, env_override):
1625
  """Parser for the command line arguments.
1626

1627
  This function parses the arguments and returns the function which
1628
  must be executed together with its (modified) arguments.
1629

1630
  @param binary: Script name
1631
  @param argv: Command line arguments
1632
  @param commands: Dictionary containing command definitions
1633
  @param aliases: dictionary with command aliases {"alias": "target", ...}
1634
  @param env_override: list of env variables allowed for default args
1635
  @raise _ShowUsage: If usage description should be shown
1636
  @raise _ShowVersion: If version should be shown
1637

1638
  """
1639
  assert not (env_override - set(commands))
1640
  assert not (set(aliases.keys()) & set(commands.keys()))
1641

    
1642
  if len(argv) > 1:
1643
    cmd = argv[1]
1644
  else:
1645
    # No option or command given
1646
    raise _ShowUsage(exit_error=True)
1647

    
1648
  if cmd == "--version":
1649
    raise _ShowVersion()
1650
  elif cmd == "--help":
1651
    raise _ShowUsage(exit_error=False)
1652
  elif not (cmd in commands or cmd in aliases):
1653
    raise _ShowUsage(exit_error=True)
1654

    
1655
  # get command, unalias it, and look it up in commands
1656
  if cmd in aliases:
1657
    if aliases[cmd] not in commands:
1658
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1659
                                   " command '%s'" % (cmd, aliases[cmd]))
1660

    
1661
    cmd = aliases[cmd]
1662

    
1663
  if cmd in env_override:
1664
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1665
    env_args = os.environ.get(args_env_name)
1666
    if env_args:
1667
      argv = utils.InsertAtPos(argv, 2, shlex.split(env_args))
1668

    
1669
  func, args_def, parser_opts, usage, description = commands[cmd]
1670
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1671
                        description=description,
1672
                        formatter=TitledHelpFormatter(),
1673
                        usage="%%prog %s %s" % (cmd, usage))
1674
  parser.disable_interspersed_args()
1675
  options, args = parser.parse_args(args=argv[2:])
1676

    
1677
  if not _CheckArguments(cmd, args_def, args):
1678
    return None, None, None
1679

    
1680
  return func, options, args
1681

    
1682

    
1683
def _FormatUsage(binary, commands):
1684
  """Generates a nice description of all commands.
1685

1686
  @param binary: Script name
1687
  @param commands: Dictionary containing command definitions
1688

1689
  """
1690
  # compute the max line length for cmd + usage
1691
  mlen = min(60, max(map(len, commands)))
1692

    
1693
  yield "Usage: %s {command} [options...] [argument...]" % binary
1694
  yield "%s <command> --help to see details, or man %s" % (binary, binary)
1695
  yield ""
1696
  yield "Commands:"
1697

    
1698
  # and format a nice command list
1699
  for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()):
1700
    help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1701
    yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0))
1702
    for line in help_lines:
1703
      yield " %-*s   %s" % (mlen, "", line)
1704

    
1705
  yield ""
1706

    
1707

    
1708
def _CheckArguments(cmd, args_def, args):
1709
  """Verifies the arguments using the argument definition.
1710

1711
  Algorithm:
1712

1713
    1. Abort with error if values specified by user but none expected.
1714

1715
    1. For each argument in definition
1716

1717
      1. Keep running count of minimum number of values (min_count)
1718
      1. Keep running count of maximum number of values (max_count)
1719
      1. If it has an unlimited number of values
1720

1721
        1. Abort with error if it's not the last argument in the definition
1722

1723
    1. If last argument has limited number of values
1724

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

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

1729
  """
1730
  if args and not args_def:
1731
    ToStderr("Error: Command %s expects no arguments", cmd)
1732
    return False
1733

    
1734
  min_count = None
1735
  max_count = None
1736
  check_max = None
1737

    
1738
  last_idx = len(args_def) - 1
1739

    
1740
  for idx, arg in enumerate(args_def):
1741
    if min_count is None:
1742
      min_count = arg.min
1743
    elif arg.min is not None:
1744
      min_count += arg.min
1745

    
1746
    if max_count is None:
1747
      max_count = arg.max
1748
    elif arg.max is not None:
1749
      max_count += arg.max
1750

    
1751
    if idx == last_idx:
1752
      check_max = (arg.max is not None)
1753

    
1754
    elif arg.max is None:
1755
      raise errors.ProgrammerError("Only the last argument can have max=None")
1756

    
1757
  if check_max:
1758
    # Command with exact number of arguments
1759
    if (min_count is not None and max_count is not None and
1760
        min_count == max_count and len(args) != min_count):
1761
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1762
      return False
1763

    
1764
    # Command with limited number of arguments
1765
    if max_count is not None and len(args) > max_count:
1766
      ToStderr("Error: Command %s expects only %d argument(s)",
1767
               cmd, max_count)
1768
      return False
1769

    
1770
  # Command with some required arguments
1771
  if min_count is not None and len(args) < min_count:
1772
    ToStderr("Error: Command %s expects at least %d argument(s)",
1773
             cmd, min_count)
1774
    return False
1775

    
1776
  return True
1777

    
1778

    
1779
def SplitNodeOption(value):
1780
  """Splits the value of a --node option.
1781

1782
  """
1783
  if value and ":" in value:
1784
    return value.split(":", 1)
1785
  else:
1786
    return (value, None)
1787

    
1788

    
1789
def CalculateOSNames(os_name, os_variants):
1790
  """Calculates all the names an OS can be called, according to its variants.
1791

1792
  @type os_name: string
1793
  @param os_name: base name of the os
1794
  @type os_variants: list or None
1795
  @param os_variants: list of supported variants
1796
  @rtype: list
1797
  @return: list of valid names
1798

1799
  """
1800
  if os_variants:
1801
    return ["%s+%s" % (os_name, v) for v in os_variants]
1802
  else:
1803
    return [os_name]
1804

    
1805

    
1806
def ParseFields(selected, default):
1807
  """Parses the values of "--field"-like options.
1808

1809
  @type selected: string or None
1810
  @param selected: User-selected options
1811
  @type default: list
1812
  @param default: Default fields
1813

1814
  """
1815
  if selected is None:
1816
    return default
1817

    
1818
  if selected.startswith("+"):
1819
    return default + selected[1:].split(",")
1820

    
1821
  return selected.split(",")
1822

    
1823

    
1824
UsesRPC = rpc.RunWithRPC
1825

    
1826

    
1827
def AskUser(text, choices=None):
1828
  """Ask the user a question.
1829

1830
  @param text: the question to ask
1831

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

1837
  @return: one of the return values from the choices list; if input is
1838
      not possible (i.e. not running with a tty, we return the last
1839
      entry from the list
1840

1841
  """
1842
  if choices is None:
1843
    choices = [("y", True, "Perform the operation"),
1844
               ("n", False, "Do not perform the operation")]
1845
  if not choices or not isinstance(choices, list):
1846
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1847
  for entry in choices:
1848
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1849
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1850

    
1851
  answer = choices[-1][1]
1852
  new_text = []
1853
  for line in text.splitlines():
1854
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1855
  text = "\n".join(new_text)
1856
  try:
1857
    f = file("/dev/tty", "a+")
1858
  except IOError:
1859
    return answer
1860
  try:
1861
    chars = [entry[0] for entry in choices]
1862
    chars[-1] = "[%s]" % chars[-1]
1863
    chars.append("?")
1864
    maps = dict([(entry[0], entry[1]) for entry in choices])
1865
    while True:
1866
      f.write(text)
1867
      f.write("\n")
1868
      f.write("/".join(chars))
1869
      f.write(": ")
1870
      line = f.readline(2).strip().lower()
1871
      if line in maps:
1872
        answer = maps[line]
1873
        break
1874
      elif line == "?":
1875
        for entry in choices:
1876
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1877
        f.write("\n")
1878
        continue
1879
  finally:
1880
    f.close()
1881
  return answer
1882

    
1883

    
1884
class JobSubmittedException(Exception):
1885
  """Job was submitted, client should exit.
1886

1887
  This exception has one argument, the ID of the job that was
1888
  submitted. The handler should print this ID.
1889

1890
  This is not an error, just a structured way to exit from clients.
1891

1892
  """
1893

    
1894

    
1895
def SendJob(ops, cl=None):
1896
  """Function to submit an opcode without waiting for the results.
1897

1898
  @type ops: list
1899
  @param ops: list of opcodes
1900
  @type cl: luxi.Client
1901
  @param cl: the luxi client to use for communicating with the master;
1902
             if None, a new client will be created
1903

1904
  """
1905
  if cl is None:
1906
    cl = GetClient()
1907

    
1908
  job_id = cl.SubmitJob(ops)
1909

    
1910
  return job_id
1911

    
1912

    
1913
def GenericPollJob(job_id, cbs, report_cbs):
1914
  """Generic job-polling function.
1915

1916
  @type job_id: number
1917
  @param job_id: Job ID
1918
  @type cbs: Instance of L{JobPollCbBase}
1919
  @param cbs: Data callbacks
1920
  @type report_cbs: Instance of L{JobPollReportCbBase}
1921
  @param report_cbs: Reporting callbacks
1922

1923
  """
1924
  prev_job_info = None
1925
  prev_logmsg_serial = None
1926

    
1927
  status = None
1928

    
1929
  while True:
1930
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1931
                                      prev_logmsg_serial)
1932
    if not result:
1933
      # job not found, go away!
1934
      raise errors.JobLost("Job with id %s lost" % job_id)
1935

    
1936
    if result == constants.JOB_NOTCHANGED:
1937
      report_cbs.ReportNotChanged(job_id, status)
1938

    
1939
      # Wait again
1940
      continue
1941

    
1942
    # Split result, a tuple of (field values, log entries)
1943
    (job_info, log_entries) = result
1944
    (status, ) = job_info
1945

    
1946
    if log_entries:
1947
      for log_entry in log_entries:
1948
        (serial, timestamp, log_type, message) = log_entry
1949
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1950
                                    log_type, message)
1951
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
1952

    
1953
    # TODO: Handle canceled and archived jobs
1954
    elif status in (constants.JOB_STATUS_SUCCESS,
1955
                    constants.JOB_STATUS_ERROR,
1956
                    constants.JOB_STATUS_CANCELING,
1957
                    constants.JOB_STATUS_CANCELED):
1958
      break
1959

    
1960
    prev_job_info = job_info
1961

    
1962
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
1963
  if not jobs:
1964
    raise errors.JobLost("Job with id %s lost" % job_id)
1965

    
1966
  status, opstatus, result = jobs[0]
1967

    
1968
  if status == constants.JOB_STATUS_SUCCESS:
1969
    return result
1970

    
1971
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
1972
    raise errors.OpExecError("Job was canceled")
1973

    
1974
  has_ok = False
1975
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
1976
    if status == constants.OP_STATUS_SUCCESS:
1977
      has_ok = True
1978
    elif status == constants.OP_STATUS_ERROR:
1979
      errors.MaybeRaise(msg)
1980

    
1981
      if has_ok:
1982
        raise errors.OpExecError("partial failure (opcode %d): %s" %
1983
                                 (idx, msg))
1984

    
1985
      raise errors.OpExecError(str(msg))
1986

    
1987
  # default failure mode
1988
  raise errors.OpExecError(result)
1989

    
1990

    
1991
class JobPollCbBase:
1992
  """Base class for L{GenericPollJob} callbacks.
1993

1994
  """
1995
  def __init__(self):
1996
    """Initializes this class.
1997

1998
    """
1999

    
2000
  def WaitForJobChangeOnce(self, job_id, fields,
2001
                           prev_job_info, prev_log_serial):
2002
    """Waits for changes on a job.
2003

2004
    """
2005
    raise NotImplementedError()
2006

    
2007
  def QueryJobs(self, job_ids, fields):
2008
    """Returns the selected fields for the selected job IDs.
2009

2010
    @type job_ids: list of numbers
2011
    @param job_ids: Job IDs
2012
    @type fields: list of strings
2013
    @param fields: Fields
2014

2015
    """
2016
    raise NotImplementedError()
2017

    
2018

    
2019
class JobPollReportCbBase:
2020
  """Base class for L{GenericPollJob} reporting callbacks.
2021

2022
  """
2023
  def __init__(self):
2024
    """Initializes this class.
2025

2026
    """
2027

    
2028
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2029
    """Handles a log message.
2030

2031
    """
2032
    raise NotImplementedError()
2033

    
2034
  def ReportNotChanged(self, job_id, status):
2035
    """Called for if a job hasn't changed in a while.
2036

2037
    @type job_id: number
2038
    @param job_id: Job ID
2039
    @type status: string or None
2040
    @param status: Job status if available
2041

2042
    """
2043
    raise NotImplementedError()
2044

    
2045

    
2046
class _LuxiJobPollCb(JobPollCbBase):
2047
  def __init__(self, cl):
2048
    """Initializes this class.
2049

2050
    """
2051
    JobPollCbBase.__init__(self)
2052
    self.cl = cl
2053

    
2054
  def WaitForJobChangeOnce(self, job_id, fields,
2055
                           prev_job_info, prev_log_serial):
2056
    """Waits for changes on a job.
2057

2058
    """
2059
    return self.cl.WaitForJobChangeOnce(job_id, fields,
2060
                                        prev_job_info, prev_log_serial)
2061

    
2062
  def QueryJobs(self, job_ids, fields):
2063
    """Returns the selected fields for the selected job IDs.
2064

2065
    """
2066
    return self.cl.QueryJobs(job_ids, fields)
2067

    
2068

    
2069
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2070
  def __init__(self, feedback_fn):
2071
    """Initializes this class.
2072

2073
    """
2074
    JobPollReportCbBase.__init__(self)
2075

    
2076
    self.feedback_fn = feedback_fn
2077

    
2078
    assert callable(feedback_fn)
2079

    
2080
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2081
    """Handles a log message.
2082

2083
    """
2084
    self.feedback_fn((timestamp, log_type, log_msg))
2085

    
2086
  def ReportNotChanged(self, job_id, status):
2087
    """Called if a job hasn't changed in a while.
2088

2089
    """
2090
    # Ignore
2091

    
2092

    
2093
class StdioJobPollReportCb(JobPollReportCbBase):
2094
  def __init__(self):
2095
    """Initializes this class.
2096

2097
    """
2098
    JobPollReportCbBase.__init__(self)
2099

    
2100
    self.notified_queued = False
2101
    self.notified_waitlock = False
2102

    
2103
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2104
    """Handles a log message.
2105

2106
    """
2107
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
2108
             FormatLogMessage(log_type, log_msg))
2109

    
2110
  def ReportNotChanged(self, job_id, status):
2111
    """Called if a job hasn't changed in a while.
2112

2113
    """
2114
    if status is None:
2115
      return
2116

    
2117
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
2118
      ToStderr("Job %s is waiting in queue", job_id)
2119
      self.notified_queued = True
2120

    
2121
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
2122
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
2123
      self.notified_waitlock = True
2124

    
2125

    
2126
def FormatLogMessage(log_type, log_msg):
2127
  """Formats a job message according to its type.
2128

2129
  """
2130
  if log_type != constants.ELOG_MESSAGE:
2131
    log_msg = str(log_msg)
2132

    
2133
  return utils.SafeEncode(log_msg)
2134

    
2135

    
2136
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2137
  """Function to poll for the result of a job.
2138

2139
  @type job_id: job identified
2140
  @param job_id: the job to poll for results
2141
  @type cl: luxi.Client
2142
  @param cl: the luxi client to use for communicating with the master;
2143
             if None, a new client will be created
2144

2145
  """
2146
  if cl is None:
2147
    cl = GetClient()
2148

    
2149
  if reporter is None:
2150
    if feedback_fn:
2151
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
2152
    else:
2153
      reporter = StdioJobPollReportCb()
2154
  elif feedback_fn:
2155
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
2156

    
2157
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2158

    
2159

    
2160
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2161
  """Legacy function to submit an opcode.
2162

2163
  This is just a simple wrapper over the construction of the processor
2164
  instance. It should be extended to better handle feedback and
2165
  interaction functions.
2166

2167
  """
2168
  if cl is None:
2169
    cl = GetClient()
2170

    
2171
  SetGenericOpcodeOpts([op], opts)
2172

    
2173
  job_id = SendJob([op], cl=cl)
2174

    
2175
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
2176
                       reporter=reporter)
2177

    
2178
  return op_results[0]
2179

    
2180

    
2181
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2182
  """Wrapper around SubmitOpCode or SendJob.
2183

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

2189
  It will also process the opcodes if we're sending the via SendJob
2190
  (otherwise SubmitOpCode does it).
2191

2192
  """
2193
  if opts and opts.submit_only:
2194
    job = [op]
2195
    SetGenericOpcodeOpts(job, opts)
2196
    job_id = SendJob(job, cl=cl)
2197
    raise JobSubmittedException(job_id)
2198
  else:
2199
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2200

    
2201

    
2202
def _InitReasonTrail(op, opts):
2203
  """Builds the first part of the reason trail
2204

2205
  Builds the initial part of the reason trail, adding the user provided reason
2206
  (if it exists) and the name of the command starting the operation.
2207

2208
  @param op: the opcode the reason trail will be added to
2209
  @param opts: the command line options selected by the user
2210

2211
  """
2212
  assert len(sys.argv) >= 2
2213
  trail = []
2214

    
2215
  if opts.reason:
2216
    trail.append((constants.OPCODE_REASON_SRC_USER,
2217
                  opts.reason,
2218
                  utils.EpochNano()))
2219

    
2220
  binary = os.path.basename(sys.argv[0])
2221
  source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary)
2222
  command = sys.argv[1]
2223
  trail.append((source, command, utils.EpochNano()))
2224
  op.reason = trail
2225

    
2226

    
2227
def SetGenericOpcodeOpts(opcode_list, options):
2228
  """Processor for generic options.
2229

2230
  This function updates the given opcodes based on generic command
2231
  line options (like debug, dry-run, etc.).
2232

2233
  @param opcode_list: list of opcodes
2234
  @param options: command line options or None
2235
  @return: None (in-place modification)
2236

2237
  """
2238
  if not options:
2239
    return
2240
  for op in opcode_list:
2241
    op.debug_level = options.debug
2242
    if hasattr(options, "dry_run"):
2243
      op.dry_run = options.dry_run
2244
    if getattr(options, "priority", None) is not None:
2245
      op.priority = options.priority
2246
    _InitReasonTrail(op, options)
2247

    
2248

    
2249
def GetClient(query=False):
2250
  """Connects to the a luxi socket and returns a client.
2251

2252
  @type query: boolean
2253
  @param query: this signifies that the client will only be
2254
      used for queries; if the build-time parameter
2255
      enable-split-queries is enabled, then the client will be
2256
      connected to the query socket instead of the masterd socket
2257

2258
  """
2259
  override_socket = os.getenv(constants.LUXI_OVERRIDE, "")
2260
  if override_socket:
2261
    if override_socket == constants.LUXI_OVERRIDE_MASTER:
2262
      address = pathutils.MASTER_SOCKET
2263
    elif override_socket == constants.LUXI_OVERRIDE_QUERY:
2264
      address = pathutils.QUERY_SOCKET
2265
    else:
2266
      address = override_socket
2267
  elif query and constants.ENABLE_SPLIT_QUERY:
2268
    address = pathutils.QUERY_SOCKET
2269
  else:
2270
    address = None
2271
  # TODO: Cache object?
2272
  try:
2273
    client = luxi.Client(address=address)
2274
  except luxi.NoMasterError:
2275
    ss = ssconf.SimpleStore()
2276

    
2277
    # Try to read ssconf file
2278
    try:
2279
      ss.GetMasterNode()
2280
    except errors.ConfigurationError:
2281
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
2282
                                 " not part of a cluster",
2283
                                 errors.ECODE_INVAL)
2284

    
2285
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
2286
    if master != myself:
2287
      raise errors.OpPrereqError("This is not the master node, please connect"
2288
                                 " to node '%s' and rerun the command" %
2289
                                 master, errors.ECODE_INVAL)
2290
    raise
2291
  return client
2292

    
2293

    
2294
def FormatError(err):
2295
  """Return a formatted error message for a given error.
2296

2297
  This function takes an exception instance and returns a tuple
2298
  consisting of two values: first, the recommended exit code, and
2299
  second, a string describing the error message (not
2300
  newline-terminated).
2301

2302
  """
2303
  retcode = 1
2304
  obuf = StringIO()
2305
  msg = str(err)
2306
  if isinstance(err, errors.ConfigurationError):
2307
    txt = "Corrupt configuration file: %s" % msg
2308
    logging.error(txt)
2309
    obuf.write(txt + "\n")
2310
    obuf.write("Aborting.")
2311
    retcode = 2
2312
  elif isinstance(err, errors.HooksAbort):
2313
    obuf.write("Failure: hooks execution failed:\n")
2314
    for node, script, out in err.args[0]:
2315
      if out:
2316
        obuf.write("  node: %s, script: %s, output: %s\n" %
2317
                   (node, script, out))
2318
      else:
2319
        obuf.write("  node: %s, script: %s (no output)\n" %
2320
                   (node, script))
2321
  elif isinstance(err, errors.HooksFailure):
2322
    obuf.write("Failure: hooks general failure: %s" % msg)
2323
  elif isinstance(err, errors.ResolverError):
2324
    this_host = netutils.Hostname.GetSysName()
2325
    if err.args[0] == this_host:
2326
      msg = "Failure: can't resolve my own hostname ('%s')"
2327
    else:
2328
      msg = "Failure: can't resolve hostname '%s'"
2329
    obuf.write(msg % err.args[0])
2330
  elif isinstance(err, errors.OpPrereqError):
2331
    if len(err.args) == 2:
2332
      obuf.write("Failure: prerequisites not met for this"
2333
                 " operation:\nerror type: %s, error details:\n%s" %
2334
                 (err.args[1], err.args[0]))
2335
    else:
2336
      obuf.write("Failure: prerequisites not met for this"
2337
                 " operation:\n%s" % msg)
2338
  elif isinstance(err, errors.OpExecError):
2339
    obuf.write("Failure: command execution error:\n%s" % msg)
2340
  elif isinstance(err, errors.TagError):
2341
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2342
  elif isinstance(err, errors.JobQueueDrainError):
2343
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2344
               " accept new requests\n")
2345
  elif isinstance(err, errors.JobQueueFull):
2346
    obuf.write("Failure: the job queue is full and doesn't accept new"
2347
               " job submissions until old jobs are archived\n")
2348
  elif isinstance(err, errors.TypeEnforcementError):
2349
    obuf.write("Parameter Error: %s" % msg)
2350
  elif isinstance(err, errors.ParameterError):
2351
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2352
  elif isinstance(err, luxi.NoMasterError):
2353
    if err.args[0] == pathutils.MASTER_SOCKET:
2354
      daemon = "the master daemon"
2355
    elif err.args[0] == pathutils.QUERY_SOCKET:
2356
      daemon = "the config daemon"
2357
    else:
2358
      daemon = "socket '%s'" % str(err.args[0])
2359
    obuf.write("Cannot communicate with %s.\nIs the process running"
2360
               " and listening for connections?" % daemon)
2361
  elif isinstance(err, luxi.TimeoutError):
2362
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2363
               " been submitted and will continue to run even if the call"
2364
               " timed out. Useful commands in this situation are \"gnt-job"
2365
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2366
    obuf.write(msg)
2367
  elif isinstance(err, luxi.PermissionError):
2368
    obuf.write("It seems you don't have permissions to connect to the"
2369
               " master daemon.\nPlease retry as a different user.")
2370
  elif isinstance(err, luxi.ProtocolError):
2371
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2372
               "%s" % msg)
2373
  elif isinstance(err, errors.JobLost):
2374
    obuf.write("Error checking job status: %s" % msg)
2375
  elif isinstance(err, errors.QueryFilterParseError):
2376
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2377
    obuf.write("\n".join(err.GetDetails()))
2378
  elif isinstance(err, errors.GenericError):
2379
    obuf.write("Unhandled Ganeti error: %s" % msg)
2380
  elif isinstance(err, JobSubmittedException):
2381
    obuf.write("JobID: %s\n" % err.args[0])
2382
    retcode = 0
2383
  else:
2384
    obuf.write("Unhandled exception: %s" % msg)
2385
  return retcode, obuf.getvalue().rstrip("\n")
2386

    
2387

    
2388
def GenericMain(commands, override=None, aliases=None,
2389
                env_override=frozenset()):
2390
  """Generic main function for all the gnt-* commands.
2391

2392
  @param commands: a dictionary with a special structure, see the design doc
2393
                   for command line handling.
2394
  @param override: if not None, we expect a dictionary with keys that will
2395
                   override command line options; this can be used to pass
2396
                   options from the scripts to generic functions
2397
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2398
  @param env_override: list of environment names which are allowed to submit
2399
                       default args for commands
2400

2401
  """
2402
  # save the program name and the entire command line for later logging
2403
  if sys.argv:
2404
    binary = os.path.basename(sys.argv[0])
2405
    if not binary:
2406
      binary = sys.argv[0]
2407

    
2408
    if len(sys.argv) >= 2:
2409
      logname = utils.ShellQuoteArgs([binary, sys.argv[1]])
2410
    else:
2411
      logname = binary
2412

    
2413
    cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:])
2414
  else:
2415
    binary = "<unknown program>"
2416
    cmdline = "<unknown>"
2417

    
2418
  if aliases is None:
2419
    aliases = {}
2420

    
2421
  try:
2422
    (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases,
2423
                                       env_override)
2424
  except _ShowVersion:
2425
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
2426
             constants.RELEASE_VERSION)
2427
    return constants.EXIT_SUCCESS
2428
  except _ShowUsage, err:
2429
    for line in _FormatUsage(binary, commands):
2430
      ToStdout(line)
2431

    
2432
    if err.exit_error:
2433
      return constants.EXIT_FAILURE
2434
    else:
2435
      return constants.EXIT_SUCCESS
2436
  except errors.ParameterError, err:
2437
    result, err_msg = FormatError(err)
2438
    ToStderr(err_msg)
2439
    return 1
2440

    
2441
  if func is None: # parse error
2442
    return 1
2443

    
2444
  if override is not None:
2445
    for key, val in override.iteritems():
2446
      setattr(options, key, val)
2447

    
2448
  utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug,
2449
                     stderr_logging=True)
2450

    
2451
  logging.info("Command line: %s", cmdline)
2452

    
2453
  try:
2454
    result = func(options, args)
2455
  except (errors.GenericError, luxi.ProtocolError,
2456
          JobSubmittedException), err:
2457
    result, err_msg = FormatError(err)
2458
    logging.exception("Error during command processing")
2459
    ToStderr(err_msg)
2460
  except KeyboardInterrupt:
2461
    result = constants.EXIT_FAILURE
2462
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2463
             " might have been submitted and"
2464
             " will continue to run in the background.")
2465
  except IOError, err:
2466
    if err.errno == errno.EPIPE:
2467
      # our terminal went away, we'll exit
2468
      sys.exit(constants.EXIT_FAILURE)
2469
    else:
2470
      raise
2471

    
2472
  return result
2473

    
2474

    
2475
def ParseNicOption(optvalue):
2476
  """Parses the value of the --net option(s).
2477

2478
  """
2479
  try:
2480
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2481
  except (TypeError, ValueError), err:
2482
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err),
2483
                               errors.ECODE_INVAL)
2484

    
2485
  nics = [{}] * nic_max
2486
  for nidx, ndict in optvalue:
2487
    nidx = int(nidx)
2488

    
2489
    if not isinstance(ndict, dict):
2490
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2491
                                 " got %s" % (nidx, ndict), errors.ECODE_INVAL)
2492

    
2493
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2494

    
2495
    nics[nidx] = ndict
2496

    
2497
  return nics
2498

    
2499

    
2500
def GenericInstanceCreate(mode, opts, args):
2501
  """Add an instance to the cluster via either creation or import.
2502

2503
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2504
  @param opts: the command line options selected by the user
2505
  @type args: list
2506
  @param args: should contain only one element, the new instance name
2507
  @rtype: int
2508
  @return: the desired exit code
2509

2510
  """
2511
  instance = args[0]
2512

    
2513
  (pnode, snode) = SplitNodeOption(opts.node)
2514

    
2515
  hypervisor = None
2516
  hvparams = {}
2517
  if opts.hypervisor:
2518
    hypervisor, hvparams = opts.hypervisor
2519

    
2520
  if opts.nics:
2521
    nics = ParseNicOption(opts.nics)
2522
  elif opts.no_nics:
2523
    # no nics
2524
    nics = []
2525
  elif mode == constants.INSTANCE_CREATE:
2526
    # default of one nic, all auto
2527
    nics = [{}]
2528
  else:
2529
    # mode == import
2530
    nics = []
2531

    
2532
  if opts.disk_template == constants.DT_DISKLESS:
2533
    if opts.disks or opts.sd_size is not None:
2534
      raise errors.OpPrereqError("Diskless instance but disk"
2535
                                 " information passed", errors.ECODE_INVAL)
2536
    disks = []
2537
  else:
2538
    if (not opts.disks and not opts.sd_size
2539
        and mode == constants.INSTANCE_CREATE):
2540
      raise errors.OpPrereqError("No disk information specified",
2541
                                 errors.ECODE_INVAL)
2542
    if opts.disks and opts.sd_size is not None:
2543
      raise errors.OpPrereqError("Please use either the '--disk' or"
2544
                                 " '-s' option", errors.ECODE_INVAL)
2545
    if opts.sd_size is not None:
2546
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2547

    
2548
    if opts.disks:
2549
      try:
2550
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2551
      except ValueError, err:
2552
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err),
2553
                                   errors.ECODE_INVAL)
2554
      disks = [{}] * disk_max
2555
    else:
2556
      disks = []
2557
    for didx, ddict in opts.disks:
2558
      didx = int(didx)
2559
      if not isinstance(ddict, dict):
2560
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2561
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
2562
      elif constants.IDISK_SIZE in ddict:
2563
        if constants.IDISK_ADOPT in ddict:
2564
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2565
                                     " (disk %d)" % didx, errors.ECODE_INVAL)
2566
        try:
2567
          ddict[constants.IDISK_SIZE] = \
2568
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2569
        except ValueError, err:
2570
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2571
                                     (didx, err), errors.ECODE_INVAL)
2572
      elif constants.IDISK_ADOPT in ddict:
2573
        if mode == constants.INSTANCE_IMPORT:
2574
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2575
                                     " import", errors.ECODE_INVAL)
2576
        ddict[constants.IDISK_SIZE] = 0
2577
      else:
2578
        raise errors.OpPrereqError("Missing size or adoption source for"
2579
                                   " disk %d" % didx, errors.ECODE_INVAL)
2580
      disks[didx] = ddict
2581

    
2582
  if opts.tags is not None:
2583
    tags = opts.tags.split(",")
2584
  else:
2585
    tags = []
2586

    
2587
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2588
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2589

    
2590
  if mode == constants.INSTANCE_CREATE:
2591
    start = opts.start
2592
    os_type = opts.os
2593
    force_variant = opts.force_variant
2594
    src_node = None
2595
    src_path = None
2596
    no_install = opts.no_install
2597
    identify_defaults = False
2598
  elif mode == constants.INSTANCE_IMPORT:
2599
    start = False
2600
    os_type = None
2601
    force_variant = False
2602
    src_node = opts.src_node
2603
    src_path = opts.src_dir
2604
    no_install = None
2605
    identify_defaults = opts.identify_defaults
2606
  else:
2607
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2608

    
2609
  op = opcodes.OpInstanceCreate(instance_name=instance,
2610
                                disks=disks,
2611
                                disk_template=opts.disk_template,
2612
                                nics=nics,
2613
                                conflicts_check=opts.conflicts_check,
2614
                                pnode=pnode, snode=snode,
2615
                                ip_check=opts.ip_check,
2616
                                name_check=opts.name_check,
2617
                                wait_for_sync=opts.wait_for_sync,
2618
                                file_storage_dir=opts.file_storage_dir,
2619
                                file_driver=opts.file_driver,
2620
                                iallocator=opts.iallocator,
2621
                                hypervisor=hypervisor,
2622
                                hvparams=hvparams,
2623
                                beparams=opts.beparams,
2624
                                osparams=opts.osparams,
2625
                                mode=mode,
2626
                                start=start,
2627
                                os_type=os_type,
2628
                                force_variant=force_variant,
2629
                                src_node=src_node,
2630
                                src_path=src_path,
2631
                                tags=tags,
2632
                                no_install=no_install,
2633
                                identify_defaults=identify_defaults,
2634
                                ignore_ipolicy=opts.ignore_ipolicy)
2635

    
2636
  SubmitOrSend(op, opts)
2637
  return 0
2638

    
2639

    
2640
class _RunWhileClusterStoppedHelper:
2641
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2642

2643
  """
2644
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
2645
    """Initializes this class.
2646

2647
    @type feedback_fn: callable
2648
    @param feedback_fn: Feedback function
2649
    @type cluster_name: string
2650
    @param cluster_name: Cluster name
2651
    @type master_node: string
2652
    @param master_node Master node name
2653
    @type online_nodes: list
2654
    @param online_nodes: List of names of online nodes
2655

2656
    """
2657
    self.feedback_fn = feedback_fn
2658
    self.cluster_name = cluster_name
2659
    self.master_node = master_node
2660
    self.online_nodes = online_nodes
2661

    
2662
    self.ssh = ssh.SshRunner(self.cluster_name)
2663

    
2664
    self.nonmaster_nodes = [name for name in online_nodes
2665
                            if name != master_node]
2666

    
2667
    assert self.master_node not in self.nonmaster_nodes
2668

    
2669
  def _RunCmd(self, node_name, cmd):
2670
    """Runs a command on the local or a remote machine.
2671

2672
    @type node_name: string
2673
    @param node_name: Machine name
2674
    @type cmd: list
2675
    @param cmd: Command
2676

2677
    """
2678
    if node_name is None or node_name == self.master_node:
2679
      # No need to use SSH
2680
      result = utils.RunCmd(cmd)
2681
    else:
2682
      result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER,
2683
                            utils.ShellQuoteArgs(cmd))
2684

    
2685
    if result.failed:
2686
      errmsg = ["Failed to run command %s" % result.cmd]
2687
      if node_name:
2688
        errmsg.append("on node %s" % node_name)
2689
      errmsg.append(": exitcode %s and error %s" %
2690
                    (result.exit_code, result.output))
2691
      raise errors.OpExecError(" ".join(errmsg))
2692

    
2693
  def Call(self, fn, *args):
2694
    """Call function while all daemons are stopped.
2695

2696
    @type fn: callable
2697
    @param fn: Function to be called
2698

2699
    """
2700
    # Pause watcher by acquiring an exclusive lock on watcher state file
2701
    self.feedback_fn("Blocking watcher")
2702
    watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE)
2703
    try:
2704
      # TODO: Currently, this just blocks. There's no timeout.
2705
      # TODO: Should it be a shared lock?
2706
      watcher_block.Exclusive(blocking=True)
2707

    
2708
      # Stop master daemons, so that no new jobs can come in and all running
2709
      # ones are finished
2710
      self.feedback_fn("Stopping master daemons")
2711
      self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"])
2712
      try:
2713
        # Stop daemons on all nodes
2714
        for node_name in self.online_nodes:
2715
          self.feedback_fn("Stopping daemons on %s" % node_name)
2716
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"])
2717

    
2718
        # All daemons are shut down now
2719
        try:
2720
          return fn(self, *args)
2721
        except Exception, err:
2722
          _, errmsg = FormatError(err)
2723
          logging.exception("Caught exception")
2724
          self.feedback_fn(errmsg)
2725
          raise
2726
      finally:
2727
        # Start cluster again, master node last
2728
        for node_name in self.nonmaster_nodes + [self.master_node]:
2729
          self.feedback_fn("Starting daemons on %s" % node_name)
2730
          self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"])
2731
    finally:
2732
      # Resume watcher
2733
      watcher_block.Close()
2734

    
2735

    
2736
def RunWhileClusterStopped(feedback_fn, fn, *args):
2737
  """Calls a function while all cluster daemons are stopped.
2738

2739
  @type feedback_fn: callable
2740
  @param feedback_fn: Feedback function
2741
  @type fn: callable
2742
  @param fn: Function to be called when daemons are stopped
2743

2744
  """
2745
  feedback_fn("Gathering cluster information")
2746

    
2747
  # This ensures we're running on the master daemon
2748
  cl = GetClient()
2749

    
2750
  (cluster_name, master_node) = \
2751
    cl.QueryConfigValues(["cluster_name", "master_node"])
2752

    
2753
  online_nodes = GetOnlineNodes([], cl=cl)
2754

    
2755
  # Don't keep a reference to the client. The master daemon will go away.
2756
  del cl
2757

    
2758
  assert master_node in online_nodes
2759

    
2760
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2761
                                       online_nodes).Call(fn, *args)
2762

    
2763

    
2764
def GenerateTable(headers, fields, separator, data,
2765
                  numfields=None, unitfields=None,
2766
                  units=None):
2767
  """Prints a table with headers and different fields.
2768

2769
  @type headers: dict
2770
  @param headers: dictionary mapping field names to headers for
2771
      the table
2772
  @type fields: list
2773
  @param fields: the field names corresponding to each row in
2774
      the data field
2775
  @param separator: the separator to be used; if this is None,
2776
      the default 'smart' algorithm is used which computes optimal
2777
      field width, otherwise just the separator is used between
2778
      each field
2779
  @type data: list
2780
  @param data: a list of lists, each sublist being one row to be output
2781
  @type numfields: list
2782
  @param numfields: a list with the fields that hold numeric
2783
      values and thus should be right-aligned
2784
  @type unitfields: list
2785
  @param unitfields: a list with the fields that hold numeric
2786
      values that should be formatted with the units field
2787
  @type units: string or None
2788
  @param units: the units we should use for formatting, or None for
2789
      automatic choice (human-readable for non-separator usage, otherwise
2790
      megabytes); this is a one-letter string
2791

2792
  """
2793
  if units is None:
2794
    if separator:
2795
      units = "m"
2796
    else:
2797
      units = "h"
2798

    
2799
  if numfields is None:
2800
    numfields = []
2801
  if unitfields is None:
2802
    unitfields = []
2803

    
2804
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2805
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2806

    
2807
  format_fields = []
2808
  for field in fields:
2809
    if headers and field not in headers:
2810
      # TODO: handle better unknown fields (either revert to old
2811
      # style of raising exception, or deal more intelligently with
2812
      # variable fields)
2813
      headers[field] = field
2814
    if separator is not None:
2815
      format_fields.append("%s")
2816
    elif numfields.Matches(field):
2817
      format_fields.append("%*s")
2818
    else:
2819
      format_fields.append("%-*s")
2820

    
2821
  if separator is None:
2822
    mlens = [0 for name in fields]
2823
    format_str = " ".join(format_fields)
2824
  else:
2825
    format_str = separator.replace("%", "%%").join(format_fields)
2826

    
2827
  for row in data:
2828
    if row is None:
2829
      continue
2830
    for idx, val in enumerate(row):
2831
      if unitfields.Matches(fields[idx]):
2832
        try:
2833
          val = int(val)
2834
        except (TypeError, ValueError):
2835
          pass
2836
        else:
2837
          val = row[idx] = utils.FormatUnit(val, units)
2838
      val = row[idx] = str(val)
2839
      if separator is None:
2840
        mlens[idx] = max(mlens[idx], len(val))
2841

    
2842
  result = []
2843
  if headers:
2844
    args = []
2845
    for idx, name in enumerate(fields):
2846
      hdr = headers[name]
2847
      if separator is None:
2848
        mlens[idx] = max(mlens[idx], len(hdr))
2849
        args.append(mlens[idx])
2850
      args.append(hdr)
2851
    result.append(format_str % tuple(args))
2852

    
2853
  if separator is None:
2854
    assert len(mlens) == len(fields)
2855

    
2856
    if fields and not numfields.Matches(fields[-1]):
2857
      mlens[-1] = 0
2858

    
2859
  for line in data:
2860
    args = []
2861
    if line is None:
2862
      line = ["-" for _ in fields]
2863
    for idx in range(len(fields)):
2864
      if separator is None:
2865
        args.append(mlens[idx])
2866
      args.append(line[idx])
2867
    result.append(format_str % tuple(args))
2868

    
2869
  return result
2870

    
2871

    
2872
def _FormatBool(value):
2873
  """Formats a boolean value as a string.
2874

2875
  """
2876
  if value:
2877
    return "Y"
2878
  return "N"
2879

    
2880

    
2881
#: Default formatting for query results; (callback, align right)
2882
_DEFAULT_FORMAT_QUERY = {
2883
  constants.QFT_TEXT: (str, False),
2884
  constants.QFT_BOOL: (_FormatBool, False),
2885
  constants.QFT_NUMBER: (str, True),
2886
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
2887
  constants.QFT_OTHER: (str, False),
2888
  constants.QFT_UNKNOWN: (str, False),
2889
  }
2890

    
2891

    
2892
def _GetColumnFormatter(fdef, override, unit):
2893
  """Returns formatting function for a field.
2894

2895
  @type fdef: L{objects.QueryFieldDefinition}
2896
  @type override: dict
2897
  @param override: Dictionary for overriding field formatting functions,
2898
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2899
  @type unit: string
2900
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
2901
  @rtype: tuple; (callable, bool)
2902
  @return: Returns the function to format a value (takes one parameter) and a
2903
    boolean for aligning the value on the right-hand side
2904

2905
  """
2906
  fmt = override.get(fdef.name, None)
2907
  if fmt is not None:
2908
    return fmt
2909

    
2910
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
2911

    
2912
  if fdef.kind == constants.QFT_UNIT:
2913
    # Can't keep this information in the static dictionary
2914
    return (lambda value: utils.FormatUnit(value, unit), True)
2915

    
2916
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
2917
  if fmt is not None:
2918
    return fmt
2919

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

    
2922

    
2923
class _QueryColumnFormatter:
2924
  """Callable class for formatting fields of a query.
2925

2926
  """
2927
  def __init__(self, fn, status_fn, verbose):
2928
    """Initializes this class.
2929

2930
    @type fn: callable
2931
    @param fn: Formatting function
2932
    @type status_fn: callable
2933
    @param status_fn: Function to report fields' status
2934
    @type verbose: boolean
2935
    @param verbose: whether to use verbose field descriptions or not
2936

2937
    """
2938
    self._fn = fn
2939
    self._status_fn = status_fn
2940
    self._verbose = verbose
2941

    
2942
  def __call__(self, data):
2943
    """Returns a field's string representation.
2944

2945
    """
2946
    (status, value) = data
2947

    
2948
    # Report status
2949
    self._status_fn(status)
2950

    
2951
    if status == constants.RS_NORMAL:
2952
      return self._fn(value)
2953

    
2954
    assert value is None, \
2955
           "Found value %r for abnormal status %s" % (value, status)
2956

    
2957
    return FormatResultError(status, self._verbose)
2958

    
2959

    
2960
def FormatResultError(status, verbose):
2961
  """Formats result status other than L{constants.RS_NORMAL}.
2962

2963
  @param status: The result status
2964
  @type verbose: boolean
2965
  @param verbose: Whether to return the verbose text
2966
  @return: Text of result status
2967

2968
  """
2969
  assert status != constants.RS_NORMAL, \
2970
         "FormatResultError called with status equal to constants.RS_NORMAL"
2971
  try:
2972
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
2973
  except KeyError:
2974
    raise NotImplementedError("Unknown status %s" % status)
2975
  else:
2976
    if verbose:
2977
      return verbose_text
2978
    return normal_text
2979

    
2980

    
2981
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
2982
                      header=False, verbose=False):
2983
  """Formats data in L{objects.QueryResponse}.
2984

2985
  @type result: L{objects.QueryResponse}
2986
  @param result: result of query operation
2987
  @type unit: string
2988
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
2989
    see L{utils.text.FormatUnit}
2990
  @type format_override: dict
2991
  @param format_override: Dictionary for overriding field formatting functions,
2992
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2993
  @type separator: string or None
2994
  @param separator: String used to separate fields
2995
  @type header: bool
2996
  @param header: Whether to output header row
2997
  @type verbose: boolean
2998
  @param verbose: whether to use verbose field descriptions or not
2999

3000
  """
3001
  if unit is None:
3002
    if separator:
3003
      unit = "m"
3004
    else:
3005
      unit = "h"
3006

    
3007
  if format_override is None:
3008
    format_override = {}
3009

    
3010
  stats = dict.fromkeys(constants.RS_ALL, 0)
3011

    
3012
  def _RecordStatus(status):
3013
    if status in stats:
3014
      stats[status] += 1
3015

    
3016
  columns = []
3017
  for fdef in result.fields:
3018
    assert fdef.title and fdef.name
3019
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
3020
    columns.append(TableColumn(fdef.title,
3021
                               _QueryColumnFormatter(fn, _RecordStatus,
3022
                                                     verbose),
3023
                               align_right))
3024

    
3025
  table = FormatTable(result.data, columns, header, separator)
3026

    
3027
  # Collect statistics
3028
  assert len(stats) == len(constants.RS_ALL)
3029
  assert compat.all(count >= 0 for count in stats.values())
3030

    
3031
  # Determine overall status. If there was no data, unknown fields must be
3032
  # detected via the field definitions.
3033
  if (stats[constants.RS_UNKNOWN] or
3034
      (not result.data and _GetUnknownFields(result.fields))):
3035
    status = QR_UNKNOWN
3036
  elif compat.any(count > 0 for key, count in stats.items()
3037
                  if key != constants.RS_NORMAL):
3038
    status = QR_INCOMPLETE
3039
  else:
3040
    status = QR_NORMAL
3041

    
3042
  return (status, table)
3043

    
3044

    
3045
def _GetUnknownFields(fdefs):
3046
  """Returns list of unknown fields included in C{fdefs}.
3047

3048
  @type fdefs: list of L{objects.QueryFieldDefinition}
3049

3050
  """
3051
  return [fdef for fdef in fdefs
3052
          if fdef.kind == constants.QFT_UNKNOWN]
3053

    
3054

    
3055
def _WarnUnknownFields(fdefs):
3056
  """Prints a warning to stderr if a query included unknown fields.
3057

3058
  @type fdefs: list of L{objects.QueryFieldDefinition}
3059

3060
  """
3061
  unknown = _GetUnknownFields(fdefs)
3062
  if unknown:
3063
    ToStderr("Warning: Queried for unknown fields %s",
3064
             utils.CommaJoin(fdef.name for fdef in unknown))
3065
    return True
3066

    
3067
  return False
3068

    
3069

    
3070
def GenericList(resource, fields, names, unit, separator, header, cl=None,
3071
                format_override=None, verbose=False, force_filter=False,
3072
                namefield=None, qfilter=None, isnumeric=False):
3073
  """Generic implementation for listing all items of a resource.
3074

3075
  @param resource: One of L{constants.QR_VIA_LUXI}
3076
  @type fields: list of strings
3077
  @param fields: List of fields to query for
3078
  @type names: list of strings
3079
  @param names: Names of items to query for
3080
  @type unit: string or None
3081
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
3082
    None for automatic choice (human-readable for non-separator usage,
3083
    otherwise megabytes); this is a one-letter string
3084
  @type separator: string or None
3085
  @param separator: String used to separate fields
3086
  @type header: bool
3087
  @param header: Whether to show header row
3088
  @type force_filter: bool
3089
  @param force_filter: Whether to always treat names as filter
3090
  @type format_override: dict
3091
  @param format_override: Dictionary for overriding field formatting functions,
3092
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
3093
  @type verbose: boolean
3094
  @param verbose: whether to use verbose field descriptions or not
3095
  @type namefield: string
3096
  @param namefield: Name of field to use for simple filters (see
3097
    L{qlang.MakeFilter} for details)
3098
  @type qfilter: list or None
3099
  @param qfilter: Query filter (in addition to names)
3100
  @param isnumeric: bool
3101
  @param isnumeric: Whether the namefield's type is numeric, and therefore
3102
    any simple filters built by namefield should use integer values to
3103
    reflect that
3104

3105
  """
3106
  if not names:
3107
    names = None
3108

    
3109
  namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield,
3110
                                isnumeric=isnumeric)
3111

    
3112
  if qfilter is None:
3113
    qfilter = namefilter
3114
  elif namefilter is not None:
3115
    qfilter = [qlang.OP_AND, namefilter, qfilter]
3116

    
3117
  if cl is None:
3118
    cl = GetClient()
3119

    
3120
  response = cl.Query(resource, fields, qfilter)
3121

    
3122
  found_unknown = _WarnUnknownFields(response.fields)
3123

    
3124
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
3125
                                     header=header,
3126
                                     format_override=format_override,
3127
                                     verbose=verbose)
3128

    
3129
  for line in data:
3130
    ToStdout(line)
3131

    
3132
  assert ((found_unknown and status == QR_UNKNOWN) or
3133
          (not found_unknown and status != QR_UNKNOWN))
3134

    
3135
  if status == QR_UNKNOWN:
3136
    return constants.EXIT_UNKNOWN_FIELD
3137

    
3138
  # TODO: Should the list command fail if not all data could be collected?
3139
  return constants.EXIT_SUCCESS
3140

    
3141

    
3142
def _FieldDescValues(fdef):
3143
  """Helper function for L{GenericListFields} to get query field description.
3144

3145
  @type fdef: L{objects.QueryFieldDefinition}
3146
  @rtype: list
3147

3148
  """
3149
  return [
3150
    fdef.name,
3151
    _QFT_NAMES.get(fdef.kind, fdef.kind),
3152
    fdef.title,
3153
    fdef.doc,
3154
    ]
3155

    
3156

    
3157
def GenericListFields(resource, fields, separator, header, cl=None):
3158
  """Generic implementation for listing fields for a resource.
3159

3160
  @param resource: One of L{constants.QR_VIA_LUXI}
3161
  @type fields: list of strings
3162
  @param fields: List of fields to query for
3163
  @type separator: string or None
3164
  @param separator: String used to separate fields
3165
  @type header: bool
3166
  @param header: Whether to show header row
3167

3168
  """
3169
  if cl is None:
3170
    cl = GetClient()
3171

    
3172
  if not fields:
3173
    fields = None
3174

    
3175
  response = cl.QueryFields(resource, fields)
3176

    
3177
  found_unknown = _WarnUnknownFields(response.fields)
3178

    
3179
  columns = [
3180
    TableColumn("Name", str, False),
3181
    TableColumn("Type", str, False),
3182
    TableColumn("Title", str, False),
3183
    TableColumn("Description", str, False),
3184
    ]
3185

    
3186
  rows = map(_FieldDescValues, response.fields)
3187

    
3188
  for line in FormatTable(rows, columns, header, separator):
3189
    ToStdout(line)
3190

    
3191
  if found_unknown:
3192
    return constants.EXIT_UNKNOWN_FIELD
3193

    
3194
  return constants.EXIT_SUCCESS
3195

    
3196

    
3197
class TableColumn:
3198
  """Describes a column for L{FormatTable}.
3199

3200
  """
3201
  def __init__(self, title, fn, align_right):
3202
    """Initializes this class.
3203

3204
    @type title: string
3205
    @param title: Column title
3206
    @type fn: callable
3207
    @param fn: Formatting function
3208
    @type align_right: bool
3209
    @param align_right: Whether to align values on the right-hand side
3210

3211
    """
3212
    self.title = title
3213
    self.format = fn
3214
    self.align_right = align_right
3215

    
3216

    
3217
def _GetColFormatString(width, align_right):
3218
  """Returns the format string for a field.
3219

3220
  """
3221
  if align_right:
3222
    sign = ""
3223
  else:
3224
    sign = "-"
3225

    
3226
  return "%%%s%ss" % (sign, width)
3227

    
3228

    
3229
def FormatTable(rows, columns, header, separator):
3230
  """Formats data as a table.
3231

3232
  @type rows: list of lists
3233
  @param rows: Row data, one list per row
3234
  @type columns: list of L{TableColumn}
3235
  @param columns: Column descriptions
3236
  @type header: bool
3237
  @param header: Whether to show header row
3238
  @type separator: string or None
3239
  @param separator: String used to separate columns
3240

3241
  """
3242
  if header:
3243
    data = [[col.title for col in columns]]
3244
    colwidth = [len(col.title) for col in columns]
3245
  else:
3246
    data = []
3247
    colwidth = [0 for _ in columns]
3248

    
3249
  # Format row data
3250
  for row in rows:
3251
    assert len(row) == len(columns)
3252

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

    
3255
    if separator is None:
3256
      # Update column widths
3257
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
3258
        # Modifying a list's items while iterating is fine
3259
        colwidth[idx] = max(oldwidth, len(value))
3260

    
3261
    data.append(formatted)
3262

    
3263
  if separator is not None:
3264
    # Return early if a separator is used
3265
    return [separator.join(row) for row in data]
3266

    
3267
  if columns and not columns[-1].align_right:
3268
    # Avoid unnecessary spaces at end of line
3269
    colwidth[-1] = 0
3270

    
3271
  # Build format string
3272
  fmt = " ".join([_GetColFormatString(width, col.align_right)
3273
                  for col, width in zip(columns, colwidth)])
3274

    
3275
  return [fmt % tuple(row) for row in data]
3276

    
3277

    
3278
def FormatTimestamp(ts):
3279
  """Formats a given timestamp.
3280

3281
  @type ts: timestamp
3282
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
3283

3284
  @rtype: string
3285
  @return: a string with the formatted timestamp
3286

3287
  """
3288
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
3289
    return "?"
3290

    
3291
  (sec, usecs) = ts
3292
  return utils.FormatTime(sec, usecs=usecs)
3293

    
3294

    
3295
def ParseTimespec(value):
3296
  """Parse a time specification.
3297

3298
  The following suffixed will be recognized:
3299

3300
    - s: seconds
3301
    - m: minutes
3302
    - h: hours
3303
    - d: day
3304
    - w: weeks
3305

3306
  Without any suffix, the value will be taken to be in seconds.
3307

3308
  """
3309
  value = str(value)
3310
  if not value:
3311
    raise errors.OpPrereqError("Empty time specification passed",
3312
                               errors.ECODE_INVAL)
3313
  suffix_map = {
3314
    "s": 1,
3315
    "m": 60,
3316
    "h": 3600,
3317
    "d": 86400,
3318
    "w": 604800,
3319
    }
3320
  if value[-1] not in suffix_map:
3321
    try:
3322
      value = int(value)
3323
    except (TypeError, ValueError):
3324
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3325
                                 errors.ECODE_INVAL)
3326
  else:
3327
    multiplier = suffix_map[value[-1]]
3328
    value = value[:-1]
3329
    if not value: # no data left after stripping the suffix
3330
      raise errors.OpPrereqError("Invalid time specification (only"
3331
                                 " suffix passed)", errors.ECODE_INVAL)
3332
    try:
3333
      value = int(value) * multiplier
3334
    except (TypeError, ValueError):
3335
      raise errors.OpPrereqError("Invalid time specification '%s'" % value,
3336
                                 errors.ECODE_INVAL)
3337
  return value
3338

    
3339

    
3340
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
3341
                   filter_master=False, nodegroup=None):
3342
  """Returns the names of online nodes.
3343

3344
  This function will also log a warning on stderr with the names of
3345
  the online nodes.
3346

3347
  @param nodes: if not empty, use only this subset of nodes (minus the
3348
      offline ones)
3349
  @param cl: if not None, luxi client to use
3350
  @type nowarn: boolean
3351
  @param nowarn: by default, this function will output a note with the
3352
      offline nodes that are skipped; if this parameter is True the
3353
      note is not displayed
3354
  @type secondary_ips: boolean
3355
  @param secondary_ips: if True, return the secondary IPs instead of the
3356
      names, useful for doing network traffic over the replication interface
3357
      (if any)
3358
  @type filter_master: boolean
3359
  @param filter_master: if True, do not return the master node in the list
3360
      (useful in coordination with secondary_ips where we cannot check our
3361
      node name against the list)
3362
  @type nodegroup: string
3363
  @param nodegroup: If set, only return nodes in this node group
3364

3365
  """
3366
  if cl is None:
3367
    cl = GetClient()
3368

    
3369
  qfilter = []
3370

    
3371
  if nodes:
3372
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3373

    
3374
  if nodegroup is not None:
3375
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3376
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3377

    
3378
  if filter_master:
3379
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3380

    
3381
  if qfilter:
3382
    if len(qfilter) > 1:
3383
      final_filter = [qlang.OP_AND] + qfilter
3384
    else:
3385
      assert len(qfilter) == 1
3386
      final_filter = qfilter[0]
3387
  else:
3388
    final_filter = None
3389

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

    
3392
  def _IsOffline(row):
3393
    (_, (_, offline), _) = row
3394
    return offline
3395

    
3396
  def _GetName(row):
3397
    ((_, name), _, _) = row
3398
    return name
3399

    
3400
  def _GetSip(row):
3401
    (_, _, (_, sip)) = row
3402
    return sip
3403

    
3404
  (offline, online) = compat.partition(result.data, _IsOffline)
3405

    
3406
  if offline and not nowarn:
3407
    ToStderr("Note: skipping offline node(s): %s" %
3408
             utils.CommaJoin(map(_GetName, offline)))
3409

    
3410
  if secondary_ips:
3411
    fn = _GetSip
3412
  else:
3413
    fn = _GetName
3414

    
3415
  return map(fn, online)
3416

    
3417

    
3418
def _ToStream(stream, txt, *args):
3419
  """Write a message to a stream, bypassing the logging system
3420

3421
  @type stream: file object
3422
  @param stream: the file to which we should write
3423
  @type txt: str
3424
  @param txt: the message
3425

3426
  """
3427
  try:
3428
    if args:
3429
      args = tuple(args)
3430
      stream.write(txt % args)
3431
    else:
3432
      stream.write(txt)
3433
    stream.write("\n")
3434
    stream.flush()
3435
  except IOError, err:
3436
    if err.errno == errno.EPIPE:
3437
      # our terminal went away, we'll exit
3438
      sys.exit(constants.EXIT_FAILURE)
3439
    else:
3440
      raise
3441

    
3442

    
3443
def ToStdout(txt, *args):
3444
  """Write a message to stdout only, bypassing the logging system
3445

3446
  This is just a wrapper over _ToStream.
3447

3448
  @type txt: str
3449
  @param txt: the message
3450

3451
  """
3452
  _ToStream(sys.stdout, txt, *args)
3453

    
3454

    
3455
def ToStderr(txt, *args):
3456
  """Write a message to stderr only, bypassing the logging system
3457

3458
  This is just a wrapper over _ToStream.
3459

3460
  @type txt: str
3461
  @param txt: the message
3462

3463
  """
3464
  _ToStream(sys.stderr, txt, *args)
3465

    
3466

    
3467
class JobExecutor(object):
3468
  """Class which manages the submission and execution of multiple jobs.
3469

3470
  Note that instances of this class should not be reused between
3471
  GetResults() calls.
3472

3473
  """
3474
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3475
    self.queue = []
3476
    if cl is None:
3477
      cl = GetClient()
3478
    self.cl = cl
3479
    self.verbose = verbose
3480
    self.jobs = []
3481
    self.opts = opts
3482
    self.feedback_fn = feedback_fn
3483
    self._counter = itertools.count()
3484

    
3485
  @staticmethod
3486
  def _IfName(name, fmt):
3487
    """Helper function for formatting name.
3488

3489
    """
3490
    if name:
3491
      return fmt % name
3492

    
3493
    return ""
3494

    
3495
  def QueueJob(self, name, *ops):
3496
    """Record a job for later submit.
3497

3498
    @type name: string
3499
    @param name: a description of the job, will be used in WaitJobSet
3500

3501
    """
3502
    SetGenericOpcodeOpts(ops, self.opts)
3503
    self.queue.append((self._counter.next(), name, ops))
3504

    
3505
  def AddJobId(self, name, status, job_id):
3506
    """Adds a job ID to the internal queue.
3507

3508
    """
3509
    self.jobs.append((self._counter.next(), status, job_id, name))
3510

    
3511
  def SubmitPending(self, each=False):
3512
    """Submit all pending jobs.
3513

3514
    """
3515
    if each:
3516
      results = []
3517
      for (_, _, ops) in self.queue:
3518
        # SubmitJob will remove the success status, but raise an exception if
3519
        # the submission fails, so we'll notice that anyway.
3520
        results.append([True, self.cl.SubmitJob(ops)[0]])
3521
    else:
3522
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3523
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3524
      self.jobs.append((idx, status, data, name))
3525

    
3526
  def _ChooseJob(self):
3527
    """Choose a non-waiting/queued job to poll next.
3528

3529
    """
3530
    assert self.jobs, "_ChooseJob called with empty job list"
3531

    
3532
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3533
                               ["status"])
3534
    assert result
3535

    
3536
    for job_data, status in zip(self.jobs, result):
3537
      if (isinstance(status, list) and status and
3538
          status[0] in (constants.JOB_STATUS_QUEUED,
3539
                        constants.JOB_STATUS_WAITING,
3540
                        constants.JOB_STATUS_CANCELING)):
3541
        # job is still present and waiting
3542
        continue
3543
      # good candidate found (either running job or lost job)
3544
      self.jobs.remove(job_data)
3545
      return job_data
3546

    
3547
    # no job found
3548
    return self.jobs.pop(0)
3549

    
3550
  def GetResults(self):
3551
    """Wait for and return the results of all jobs.
3552

3553
    @rtype: list
3554
    @return: list of tuples (success, job results), in the same order
3555
        as the submitted jobs; if a job has failed, instead of the result
3556
        there will be the error message
3557

3558
    """
3559
    if not self.jobs:
3560
      self.SubmitPending()
3561
    results = []
3562
    if self.verbose:
3563
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3564
      if ok_jobs:
3565
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3566

    
3567
    # first, remove any non-submitted jobs
3568
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3569
    for idx, _, jid, name in failures:
3570
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3571
      results.append((idx, False, jid))
3572

    
3573
    while self.jobs:
3574
      (idx, _, jid, name) = self._ChooseJob()
3575
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3576
      try:
3577
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3578
        success = True
3579
      except errors.JobLost, err:
3580
        _, job_result = FormatError(err)
3581
        ToStderr("Job %s%s has been archived, cannot check its result",
3582
                 jid, self._IfName(name, " for %s"))
3583
        success = False
3584
      except (errors.GenericError, luxi.ProtocolError), err:
3585
        _, job_result = FormatError(err)
3586
        success = False
3587
        # the error message will always be shown, verbose or not
3588
        ToStderr("Job %s%s has failed: %s",
3589
                 jid, self._IfName(name, " for %s"), job_result)
3590

    
3591
      results.append((idx, success, job_result))
3592

    
3593
    # sort based on the index, then drop it
3594
    results.sort()
3595
    results = [i[1:] for i in results]
3596

    
3597
    return results
3598

    
3599
  def WaitOrShow(self, wait):
3600
    """Wait for job results or only print the job IDs.
3601

3602
    @type wait: boolean
3603
    @param wait: whether to wait or not
3604

3605
    """
3606
    if wait:
3607
      return self.GetResults()
3608
    else:
3609
      if not self.jobs:
3610
        self.SubmitPending()
3611
      for _, status, result, name in self.jobs:
3612
        if status:
3613
          ToStdout("%s: %s", result, name)
3614
        else:
3615
          ToStderr("Failure for %s: %s", name, result)
3616
      return [row[1:3] for row in self.jobs]
3617

    
3618

    
3619
def FormatParamsDictInfo(param_dict, actual):
3620
  """Formats a parameter dictionary.
3621

3622
  @type param_dict: dict
3623
  @param param_dict: the own parameters
3624
  @type actual: dict
3625
  @param actual: the current parameter set (including defaults)
3626
  @rtype: dict
3627
  @return: dictionary where the value of each parameter is either a fully
3628
      formatted string or a dictionary containing formatted strings
3629

3630
  """
3631
  ret = {}
3632
  for (key, data) in actual.items():
3633
    if isinstance(data, dict) and data:
3634
      ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data)
3635
    else:
3636
      ret[key] = str(param_dict.get(key, "default (%s)" % data))
3637
  return ret
3638

    
3639

    
3640
def _FormatListInfoDefault(data, def_data):
3641
  if data is not None:
3642
    ret = utils.CommaJoin(data)
3643
  else:
3644
    ret = "default (%s)" % utils.CommaJoin(def_data)
3645
  return ret
3646

    
3647

    
3648
def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3649
  """Formats an instance policy.
3650

3651
  @type custom_ipolicy: dict
3652
  @param custom_ipolicy: own policy
3653
  @type eff_ipolicy: dict
3654
  @param eff_ipolicy: effective policy (including defaults); ignored for
3655
      cluster
3656
  @type iscluster: bool
3657
  @param iscluster: the policy is at cluster level
3658
  @rtype: list of pairs
3659
  @return: formatted data, suitable for L{PrintGenericInfo}
3660

3661
  """
3662
  if iscluster:
3663
    eff_ipolicy = custom_ipolicy
3664

    
3665
  custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
3666
  ret = [
3667
    (key,
3668
     FormatParamsDictInfo(custom_minmax.get(key, {}),
3669
                          eff_ipolicy[constants.ISPECS_MINMAX][key]))
3670
    for key in constants.ISPECS_MINMAX_KEYS
3671
    ]
3672
  if iscluster:
3673
    stdspecs = custom_ipolicy[constants.ISPECS_STD]
3674
    ret.append(
3675
      (constants.ISPECS_STD,
3676
       FormatParamsDictInfo(stdspecs, stdspecs))
3677
      )
3678

    
3679
  ret.append(
3680
    ("enabled disk templates",
3681
     _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
3682
                            eff_ipolicy[constants.IPOLICY_DTS]))
3683
    )
3684
  ret.extend([
3685
    (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
3686
    for key in constants.IPOLICY_PARAMETERS
3687
    ])
3688
  return ret
3689

    
3690

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

3694
  This function is used to request confirmation for doing an operation
3695
  on a given list of list_type.
3696

3697
  @type names: list
3698
  @param names: the list of names that we display when
3699
      we ask for confirmation
3700
  @type list_type: str
3701
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3702
  @type text: str
3703
  @param text: the operation that the user should confirm
3704
  @rtype: boolean
3705
  @return: True or False depending on user's confirmation.
3706

3707
  """
3708
  count = len(names)
3709
  msg = ("The %s will operate on %d %s.\n%s"
3710
         "Do you want to continue?" % (text, count, list_type, extra))
3711
  affected = (("\nAffected %s:\n" % list_type) +
3712
              "\n".join(["  %s" % name for name in names]))
3713

    
3714
  choices = [("y", True, "Yes, execute the %s" % text),
3715
             ("n", False, "No, abort the %s" % text)]
3716

    
3717
  if count > 20:
3718
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3719
    question = msg
3720
  else:
3721
    question = msg + affected
3722

    
3723
  choice = AskUser(question, choices)
3724
  if choice == "v":
3725
    choices.pop(1)
3726
    choice = AskUser(msg + affected, choices)
3727
  return choice
3728

    
3729

    
3730
def _MaybeParseUnit(elements):
3731
  """Parses and returns an array of potential values with units.
3732

3733
  """
3734
  parsed = {}
3735
  for k, v in elements.items():
3736
    if v == constants.VALUE_DEFAULT:
3737
      parsed[k] = v
3738
    else:
3739
      parsed[k] = utils.ParseUnit(v)
3740
  return parsed
3741

    
3742

    
3743
def _InitIspecsFromOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count,
3744
                        ispecs_disk_count, ispecs_disk_size, ispecs_nic_count,
3745
                        group_ipolicy, allowed_values):
3746
  try:
3747
    if ispecs_mem_size:
3748
      ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
3749
    if ispecs_disk_size:
3750
      ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
3751
  except (TypeError, ValueError, errors.UnitParseError), err:
3752
    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
3753
                               " in policy: %s" %
3754
                               (ispecs_disk_size, ispecs_mem_size, err),
3755
                               errors.ECODE_INVAL)
3756

    
3757
  # prepare ipolicy dict
3758
  ispecs_transposed = {
3759
    constants.ISPEC_MEM_SIZE: ispecs_mem_size,
3760
    constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
3761
    constants.ISPEC_DISK_COUNT: ispecs_disk_count,
3762
    constants.ISPEC_DISK_SIZE: ispecs_disk_size,
3763
    constants.ISPEC_NIC_COUNT: ispecs_nic_count,
3764
    }
3765

    
3766
  # first, check that the values given are correct
3767
  if group_ipolicy:
3768
    forced_type = TISPECS_GROUP_TYPES
3769
  else:
3770
    forced_type = TISPECS_CLUSTER_TYPES
3771
  for specs in ispecs_transposed.values():
3772
    utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
3773

    
3774
  # then transpose
3775
  ispecs = {
3776
    constants.ISPECS_MIN: {},
3777
    constants.ISPECS_MAX: {},
3778
    constants.ISPECS_STD: {},
3779
    }
3780
  for (name, specs) in ispecs_transposed.iteritems():
3781
    assert name in constants.ISPECS_PARAMETERS
3782
    for key, val in specs.items(): # {min: .. ,max: .., std: ..}
3783
      assert key in ispecs
3784
      ispecs[key][name] = val
3785
  for key in constants.ISPECS_MINMAX_KEYS:
3786
    ipolicy[constants.ISPECS_MINMAX][key] = ispecs[key]
3787
  ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
3788

    
3789

    
3790
def CreateIPolicyFromOpts(ispecs_mem_size=None,
3791
                          ispecs_cpu_count=None,
3792
                          ispecs_disk_count=None,
3793
                          ispecs_disk_size=None,
3794
                          ispecs_nic_count=None,
3795
                          ipolicy_disk_templates=None,
3796
                          ipolicy_vcpu_ratio=None,
3797
                          ipolicy_spindle_ratio=None,
3798
                          group_ipolicy=False,
3799
                          allowed_values=None,
3800
                          fill_all=False):
3801
  """Creation of instance policy based on command line options.
3802

3803
  @param fill_all: whether for cluster policies we should ensure that
3804
    all values are filled
3805

3806

3807
  """
3808

    
3809
  ipolicy_out = objects.MakeEmptyIPolicy()
3810
  _InitIspecsFromOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count,
3811
                      ispecs_disk_count, ispecs_disk_size, ispecs_nic_count,
3812
                      group_ipolicy, allowed_values)
3813

    
3814
  if ipolicy_disk_templates is not None:
3815
    if allowed_values and ipolicy_disk_templates in allowed_values:
3816
      ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates
3817
    else:
3818
      ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
3819
  if ipolicy_vcpu_ratio is not None:
3820
    ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
3821
  if ipolicy_spindle_ratio is not None:
3822
    ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
3823

    
3824
  assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
3825

    
3826
  if not group_ipolicy and fill_all:
3827
    ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out)
3828

    
3829
  return ipolicy_out
3830

    
3831

    
3832
def _SerializeGenericInfo(buf, data, level, afterkey=False):
3833
  """Formatting core of L{PrintGenericInfo}.
3834

3835
  @param buf: (string) stream to accumulate the result into
3836
  @param data: data to format
3837
  @type level: int
3838
  @param level: depth in the data hierarchy, used for indenting
3839
  @type afterkey: bool
3840
  @param afterkey: True when we are in the middle of a line after a key (used
3841
      to properly add newlines or indentation)
3842

3843
  """
3844
  baseind = "  "
3845
  if isinstance(data, dict):
3846
    if not data:
3847
      buf.write("\n")
3848
    else:
3849
      if afterkey:
3850
        buf.write("\n")
3851
        doindent = True
3852
      else:
3853
        doindent = False
3854
      for key in sorted(data):
3855
        if doindent:
3856
          buf.write(baseind * level)
3857
        else:
3858
          doindent = True
3859
        buf.write(key)
3860
        buf.write(": ")
3861
        _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True)
3862
  elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple):
3863
    # list of tuples (an ordered dictionary)
3864
    if afterkey:
3865
      buf.write("\n")
3866
      doindent = True
3867
    else:
3868
      doindent = False
3869
    for (key, val) in data:
3870
      if doindent:
3871
        buf.write(baseind * level)
3872
      else:
3873
        doindent = True
3874
      buf.write(key)
3875
      buf.write(": ")
3876
      _SerializeGenericInfo(buf, val, level + 1, afterkey=True)
3877
  elif isinstance(data, list):
3878
    if not data:
3879
      buf.write("\n")
3880
    else:
3881
      if afterkey:
3882
        buf.write("\n")
3883
        doindent = True
3884
      else:
3885
        doindent = False
3886
      for item in data:
3887
        if doindent:
3888
          buf.write(baseind * level)
3889
        else:
3890
          doindent = True
3891
        buf.write("-")
3892
        buf.write(baseind[1:])
3893
        _SerializeGenericInfo(buf, item, level + 1)
3894
  else:
3895
    # This branch should be only taken for strings, but it's practically
3896
    # impossible to guarantee that no other types are produced somewhere
3897
    buf.write(str(data))
3898
    buf.write("\n")
3899

    
3900

    
3901
def PrintGenericInfo(data):
3902
  """Print information formatted according to the hierarchy.
3903

3904
  The output is a valid YAML string.
3905

3906
  @param data: the data to print. It's a hierarchical structure whose elements
3907
      can be:
3908
        - dictionaries, where keys are strings and values are of any of the
3909
          types listed here
3910
        - lists of pairs (key, value), where key is a string and value is of
3911
          any of the types listed here; it's a way to encode ordered
3912
          dictionaries
3913
        - lists of any of the types listed here
3914
        - strings
3915

3916
  """
3917
  buf = StringIO()
3918
  _SerializeGenericInfo(buf, data, 0)
3919
  ToStdout(buf.getvalue().rstrip("\n"))