Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 96e0d5cc

History | View | Annotate | Download (51.2 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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
"""OpCodes module
23

24
This module implements the data structures which define the cluster
25
operations - the so-called opcodes.
26

27
Every operation which modifies the cluster state is expressed via
28
opcodes.
29

30
"""
31

    
32
# this are practically structures, so disable the message about too
33
# few public methods:
34
# pylint: disable=R0903
35

    
36
import logging
37
import re
38

    
39
from ganeti import compat
40
from ganeti import constants
41
from ganeti import errors
42
from ganeti import ht
43

    
44

    
45
# Common opcode attributes
46

    
47
#: output fields for a query operation
48
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
49
                  "Selected output fields")
50

    
51
#: the shutdown timeout
52
_PShutdownTimeout = \
53
  ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
54
   "How long to wait for instance to shut down")
55

    
56
#: the force parameter
57
_PForce = ("force", False, ht.TBool, "Whether to force the operation")
58

    
59
#: a required instance name (for single-instance LUs)
60
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
61
                  "Instance name")
62

    
63
#: Whether to ignore offline nodes
64
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
65
                        "Whether to ignore offline nodes")
66

    
67
#: a required node name (for single-node LUs)
68
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
69

    
70
#: a required node group name (for single-group LUs)
71
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
72

    
73
#: Migration type (live/non-live)
74
_PMigrationMode = ("mode", None,
75
                   ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
76
                   "Migration mode")
77

    
78
#: Obsolete 'live' migration mode (boolean)
79
_PMigrationLive = ("live", None, ht.TMaybeBool,
80
                   "Legacy setting for live migration, do not use")
81

    
82
#: Tag type
83
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
84

    
85
#: List of tag strings
86
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
87

    
88
_PForceVariant = ("force_variant", False, ht.TBool,
89
                  "Whether to force an unknown OS variant")
90

    
91
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
92
                 "Whether to wait for the disk to synchronize")
93

    
94
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
95
                       "Whether to ignore disk consistency")
96

    
97
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
98

    
99
_PUseLocking = ("use_locking", False, ht.TBool,
100
                "Whether to use synchronization")
101

    
102
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
103

    
104
_PNodeGroupAllocPolicy = \
105
  ("alloc_policy", None,
106
   ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
107
   "Instance allocation policy")
108

    
109
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
110
                     "Default node parameters for group")
111

    
112
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
113
               "Resource(s) to query for")
114

    
115
_PEarlyRelease = ("early_release", False, ht.TBool,
116
                  "Whether to release locks as soon as possible")
117

    
118
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
119

    
120
#: Do not remember instance state changes
121
_PNoRemember = ("no_remember", False, ht.TBool,
122
                "Do not remember the state change")
123

    
124
#: Target node for instance migration/failover
125
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
126
                         "Target node for shared-storage instances")
127

    
128
_PStartupPaused = ("startup_paused", False, ht.TBool,
129
                   "Pause instance at startup")
130

    
131
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
132

    
133
# Parameters for cluster verification
134
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
135
                         "Whether to simulate errors (useful for debugging)")
136
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
137
_PSkipChecks = ("skip_checks", ht.EmptyList,
138
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
139
                "Which checks to skip")
140
_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
141
                  ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
142
                  "List of error codes that should be treated as warnings")
143

    
144
#: OP_ID conversion regular expression
145
_OPID_RE = re.compile("([a-z])([A-Z])")
146

    
147
#: Utility function for L{OpClusterSetParams}
148
_TestClusterOsList = ht.TOr(ht.TNone,
149
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
150
    ht.TMap(ht.WithDesc("GetFirstItem")(compat.fst),
151
            ht.TElemOf(constants.DDMS_VALUES)))))
152

    
153

    
154
# TODO: Generate check from constants.INIC_PARAMS_TYPES
155
#: Utility function for testing NIC definitions
156
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
157
                         ht.TOr(ht.TNone, ht.TNonEmptyString))
158

    
159
_TSetParamsResultItemItems = [
160
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
161
  ht.Comment("new value")(ht.TAny),
162
  ]
163

    
164
_TSetParamsResult = \
165
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
166
                     ht.TItems(_TSetParamsResultItemItems)))
167

    
168
_SUMMARY_PREFIX = {
169
  "CLUSTER_": "C_",
170
  "GROUP_": "G_",
171
  "NODE_": "N_",
172
  "INSTANCE_": "I_",
173
  }
174

    
175
#: Attribute name for dependencies
176
DEPEND_ATTR = "depends"
177

    
178
#: Attribute name for comment
179
COMMENT_ATTR = "comment"
180

    
181

    
182
def _NameToId(name):
183
  """Convert an opcode class name to an OP_ID.
184

185
  @type name: string
186
  @param name: the class name, as OpXxxYyy
187
  @rtype: string
188
  @return: the name in the OP_XXXX_YYYY format
189

190
  """
191
  if not name.startswith("Op"):
192
    return None
193
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
194
  # consume any input, and hence we would just have all the elements
195
  # in the list, one by one; but it seems that split doesn't work on
196
  # non-consuming input, hence we have to process the input string a
197
  # bit
198
  name = _OPID_RE.sub(r"\1,\2", name)
199
  elems = name.split(",")
200
  return "_".join(n.upper() for n in elems)
201

    
202

    
203
def RequireFileStorage():
204
  """Checks that file storage is enabled.
205

206
  While it doesn't really fit into this module, L{utils} was deemed too large
207
  of a dependency to be imported for just one or two functions.
208

209
  @raise errors.OpPrereqError: when file storage is disabled
210

211
  """
212
  if not constants.ENABLE_FILE_STORAGE:
213
    raise errors.OpPrereqError("File storage disabled at configure time",
214
                               errors.ECODE_INVAL)
215

    
216

    
217
def RequireSharedFileStorage():
218
  """Checks that shared file storage is enabled.
219

220
  While it doesn't really fit into this module, L{utils} was deemed too large
221
  of a dependency to be imported for just one or two functions.
222

223
  @raise errors.OpPrereqError: when shared file storage is disabled
224

225
  """
226
  if not constants.ENABLE_SHARED_FILE_STORAGE:
227
    raise errors.OpPrereqError("Shared file storage disabled at"
228
                               " configure time", errors.ECODE_INVAL)
229

    
230

    
231
@ht.WithDesc("CheckFileStorage")
232
def _CheckFileStorage(value):
233
  """Ensures file storage is enabled if used.
234

235
  """
236
  if value == constants.DT_FILE:
237
    RequireFileStorage()
238
  elif value == constants.DT_SHARED_FILE:
239
    RequireSharedFileStorage()
240
  return True
241

    
242

    
243
def _BuildDiskTemplateCheck(accept_none):
244
  """Builds check for disk template.
245

246
  @type accept_none: bool
247
  @param accept_none: whether to accept None as a correct value
248
  @rtype: callable
249

250
  """
251
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
252

    
253
  if accept_none:
254
    template_check = ht.TOr(template_check, ht.TNone)
255

    
256
  return ht.TAnd(template_check, _CheckFileStorage)
257

    
258

    
259
def _CheckStorageType(storage_type):
260
  """Ensure a given storage type is valid.
261

262
  """
263
  if storage_type not in constants.VALID_STORAGE_TYPES:
264
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
265
                               errors.ECODE_INVAL)
266
  if storage_type == constants.ST_FILE:
267
    RequireFileStorage()
268
  return True
269

    
270

    
271
#: Storage type parameter
272
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
273
                 "Storage type")
274

    
275

    
276
class _AutoOpParamSlots(type):
277
  """Meta class for opcode definitions.
278

279
  """
280
  def __new__(mcs, name, bases, attrs):
281
    """Called when a class should be created.
282

283
    @param mcs: The meta class
284
    @param name: Name of created class
285
    @param bases: Base classes
286
    @type attrs: dict
287
    @param attrs: Class attributes
288

289
    """
290
    assert "__slots__" not in attrs, \
291
      "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
292
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
293

    
294
    attrs["OP_ID"] = _NameToId(name)
295

    
296
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
297
    params = attrs.setdefault("OP_PARAMS", [])
298

    
299
    # Use parameter names as slots
300
    slots = [pname for (pname, _, _, _) in params]
301

    
302
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
303
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
304

    
305
    attrs["__slots__"] = slots
306

    
307
    return type.__new__(mcs, name, bases, attrs)
308

    
309

    
310
class BaseOpCode(object):
311
  """A simple serializable object.
312

313
  This object serves as a parent class for OpCode without any custom
314
  field handling.
315

316
  """
317
  # pylint: disable=E1101
318
  # as OP_ID is dynamically defined
319
  __metaclass__ = _AutoOpParamSlots
320

    
321
  def __init__(self, **kwargs):
322
    """Constructor for BaseOpCode.
323

324
    The constructor takes only keyword arguments and will set
325
    attributes on this object based on the passed arguments. As such,
326
    it means that you should not pass arguments which are not in the
327
    __slots__ attribute for this class.
328

329
    """
330
    slots = self._all_slots()
331
    for key in kwargs:
332
      if key not in slots:
333
        raise TypeError("Object %s doesn't support the parameter '%s'" %
334
                        (self.__class__.__name__, key))
335
      setattr(self, key, kwargs[key])
336

    
337
  def __getstate__(self):
338
    """Generic serializer.
339

340
    This method just returns the contents of the instance as a
341
    dictionary.
342

343
    @rtype:  C{dict}
344
    @return: the instance attributes and their values
345

346
    """
347
    state = {}
348
    for name in self._all_slots():
349
      if hasattr(self, name):
350
        state[name] = getattr(self, name)
351
    return state
352

    
353
  def __setstate__(self, state):
354
    """Generic unserializer.
355

356
    This method just restores from the serialized state the attributes
357
    of the current instance.
358

359
    @param state: the serialized opcode data
360
    @type state:  C{dict}
361

362
    """
363
    if not isinstance(state, dict):
364
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
365
                       type(state))
366

    
367
    for name in self._all_slots():
368
      if name not in state and hasattr(self, name):
369
        delattr(self, name)
370

    
371
    for name in state:
372
      setattr(self, name, state[name])
373

    
374
  @classmethod
375
  def _all_slots(cls):
376
    """Compute the list of all declared slots for a class.
377

378
    """
379
    slots = []
380
    for parent in cls.__mro__:
381
      slots.extend(getattr(parent, "__slots__", []))
382
    return slots
383

    
384
  @classmethod
385
  def GetAllParams(cls):
386
    """Compute list of all parameters for an opcode.
387

388
    """
389
    slots = []
390
    for parent in cls.__mro__:
391
      slots.extend(getattr(parent, "OP_PARAMS", []))
392
    return slots
393

    
394
  def Validate(self, set_defaults):
395
    """Validate opcode parameters, optionally setting default values.
396

397
    @type set_defaults: bool
398
    @param set_defaults: Whether to set default values
399
    @raise errors.OpPrereqError: When a parameter value doesn't match
400
                                 requirements
401

402
    """
403
    for (attr_name, default, test, _) in self.GetAllParams():
404
      assert test == ht.NoType or callable(test)
405

    
406
      if not hasattr(self, attr_name):
407
        if default == ht.NoDefault:
408
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
409
                                     (self.OP_ID, attr_name),
410
                                     errors.ECODE_INVAL)
411
        elif set_defaults:
412
          if callable(default):
413
            dval = default()
414
          else:
415
            dval = default
416
          setattr(self, attr_name, dval)
417

    
418
      if test == ht.NoType:
419
        # no tests here
420
        continue
421

    
422
      if set_defaults or hasattr(self, attr_name):
423
        attr_val = getattr(self, attr_name)
424
        if not test(attr_val):
425
          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
426
                        self.OP_ID, attr_name, type(attr_val), attr_val)
427
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
428
                                     (self.OP_ID, attr_name),
429
                                     errors.ECODE_INVAL)
430

    
431

    
432
def _BuildJobDepCheck(relative):
433
  """Builds check for job dependencies (L{DEPEND_ATTR}).
434

435
  @type relative: bool
436
  @param relative: Whether to accept relative job IDs (negative)
437
  @rtype: callable
438

439
  """
440
  if relative:
441
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
442
  else:
443
    job_id = ht.TJobId
444

    
445
  job_dep = \
446
    ht.TAnd(ht.TIsLength(2),
447
            ht.TItems([job_id,
448
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
449

    
450
  return ht.TOr(ht.TNone, ht.TListOf(job_dep))
451

    
452

    
453
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
454

    
455
#: List of submission status and job ID as returned by C{SubmitManyJobs}
456
_TJobIdListItem = \
457
  ht.TAnd(ht.TIsLength(2),
458
          ht.TItems([ht.Comment("success")(ht.TBool),
459
                     ht.Comment("Job ID if successful, error message"
460
                                " otherwise")(ht.TOr(ht.TString,
461
                                                     ht.TJobId))]))
462
TJobIdList = ht.TListOf(_TJobIdListItem)
463

    
464
#: Result containing only list of submitted jobs
465
TJobIdListOnly = ht.TStrictDict(True, True, {
466
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
467
  })
468

    
469

    
470
class OpCode(BaseOpCode):
471
  """Abstract OpCode.
472

473
  This is the root of the actual OpCode hierarchy. All clases derived
474
  from this class should override OP_ID.
475

476
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
477
               children of this class.
478
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
479
                      string returned by Summary(); see the docstring of that
480
                      method for details).
481
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
482
                   get if not already defined, and types they must match.
483
  @cvar OP_RESULT: Callable to verify opcode result
484
  @cvar WITH_LU: Boolean that specifies whether this should be included in
485
      mcpu's dispatch table
486
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
487
                 the check steps
488
  @ivar priority: Opcode priority for queue
489

490
  """
491
  # pylint: disable=E1101
492
  # as OP_ID is dynamically defined
493
  WITH_LU = True
494
  OP_PARAMS = [
495
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
496
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
497
    ("priority", constants.OP_PRIO_DEFAULT,
498
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
499
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
500
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
501
     " job IDs can be used"),
502
    (COMMENT_ATTR, None, ht.TMaybeString,
503
     "Comment describing the purpose of the opcode"),
504
    ]
505
  OP_RESULT = None
506

    
507
  def __getstate__(self):
508
    """Specialized getstate for opcodes.
509

510
    This method adds to the state dictionary the OP_ID of the class,
511
    so that on unload we can identify the correct class for
512
    instantiating the opcode.
513

514
    @rtype:   C{dict}
515
    @return:  the state as a dictionary
516

517
    """
518
    data = BaseOpCode.__getstate__(self)
519
    data["OP_ID"] = self.OP_ID
520
    return data
521

    
522
  @classmethod
523
  def LoadOpCode(cls, data):
524
    """Generic load opcode method.
525

526
    The method identifies the correct opcode class from the dict-form
527
    by looking for a OP_ID key, if this is not found, or its value is
528
    not available in this module as a child of this class, we fail.
529

530
    @type data:  C{dict}
531
    @param data: the serialized opcode
532

533
    """
534
    if not isinstance(data, dict):
535
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
536
    if "OP_ID" not in data:
537
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
538
    op_id = data["OP_ID"]
539
    op_class = None
540
    if op_id in OP_MAPPING:
541
      op_class = OP_MAPPING[op_id]
542
    else:
543
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
544
                       op_id)
545
    op = op_class()
546
    new_data = data.copy()
547
    del new_data["OP_ID"]
548
    op.__setstate__(new_data)
549
    return op
550

    
551
  def Summary(self):
552
    """Generates a summary description of this opcode.
553

554
    The summary is the value of the OP_ID attribute (without the "OP_"
555
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
556
    defined; this field should allow to easily identify the operation
557
    (for an instance creation job, e.g., it would be the instance
558
    name).
559

560
    """
561
    assert self.OP_ID is not None and len(self.OP_ID) > 3
562
    # all OP_ID start with OP_, we remove that
563
    txt = self.OP_ID[3:]
564
    field_name = getattr(self, "OP_DSC_FIELD", None)
565
    if field_name:
566
      field_value = getattr(self, field_name, None)
567
      if isinstance(field_value, (list, tuple)):
568
        field_value = ",".join(str(i) for i in field_value)
569
      txt = "%s(%s)" % (txt, field_value)
570
    return txt
571

    
572
  def TinySummary(self):
573
    """Generates a compact summary description of the opcode.
574

575
    """
576
    assert self.OP_ID.startswith("OP_")
577

    
578
    text = self.OP_ID[3:]
579

    
580
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
581
      if text.startswith(prefix):
582
        return supplement + text[len(prefix):]
583

    
584
    return text
585

    
586

    
587
# cluster opcodes
588

    
589
class OpClusterPostInit(OpCode):
590
  """Post cluster initialization.
591

592
  This opcode does not touch the cluster at all. Its purpose is to run hooks
593
  after the cluster has been initialized.
594

595
  """
596

    
597

    
598
class OpClusterDestroy(OpCode):
599
  """Destroy the cluster.
600

601
  This opcode has no other parameters. All the state is irreversibly
602
  lost after the execution of this opcode.
603

604
  """
605

    
606

    
607
class OpClusterQuery(OpCode):
608
  """Query cluster information."""
609

    
610

    
611
class OpClusterVerify(OpCode):
612
  """Submits all jobs necessary to verify the cluster.
613

614
  """
615
  OP_PARAMS = [
616
    _PDebugSimulateErrors,
617
    _PErrorCodes,
618
    _PSkipChecks,
619
    _PIgnoreErrors,
620
    _PVerbose,
621
    ("group_name", None, ht.TMaybeString, "Group to verify")
622
    ]
623
  OP_RESULT = TJobIdListOnly
624

    
625

    
626
class OpClusterVerifyConfig(OpCode):
627
  """Verify the cluster config.
628

629
  """
630
  OP_PARAMS = [
631
    _PDebugSimulateErrors,
632
    _PErrorCodes,
633
    _PIgnoreErrors,
634
    _PVerbose,
635
    ]
636
  OP_RESULT = ht.TBool
637

    
638

    
639
class OpClusterVerifyGroup(OpCode):
640
  """Run verify on a node group from the cluster.
641

642
  @type skip_checks: C{list}
643
  @ivar skip_checks: steps to be skipped from the verify process; this
644
                     needs to be a subset of
645
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
646
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
647

648
  """
649
  OP_DSC_FIELD = "group_name"
650
  OP_PARAMS = [
651
    _PGroupName,
652
    _PDebugSimulateErrors,
653
    _PErrorCodes,
654
    _PSkipChecks,
655
    _PIgnoreErrors,
656
    _PVerbose,
657
    ]
658
  OP_RESULT = ht.TBool
659

    
660

    
661
class OpClusterVerifyDisks(OpCode):
662
  """Verify the cluster disks.
663

664
  """
665
  OP_RESULT = TJobIdListOnly
666

    
667

    
668
class OpGroupVerifyDisks(OpCode):
669
  """Verifies the status of all disks in a node group.
670

671
  Result: a tuple of three elements:
672
    - dict of node names with issues (values: error msg)
673
    - list of instances with degraded disks (that should be activated)
674
    - dict of instances with missing logical volumes (values: (node, vol)
675
      pairs with details about the missing volumes)
676

677
  In normal operation, all lists should be empty. A non-empty instance
678
  list (3rd element of the result) is still ok (errors were fixed) but
679
  non-empty node list means some node is down, and probably there are
680
  unfixable drbd errors.
681

682
  Note that only instances that are drbd-based are taken into
683
  consideration. This might need to be revisited in the future.
684

685
  """
686
  OP_DSC_FIELD = "group_name"
687
  OP_PARAMS = [
688
    _PGroupName,
689
    ]
690
  OP_RESULT = \
691
    ht.TAnd(ht.TIsLength(3),
692
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
693
                       ht.TListOf(ht.TString),
694
                       ht.TDictOf(ht.TString,
695
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
696

    
697

    
698
class OpClusterRepairDiskSizes(OpCode):
699
  """Verify the disk sizes of the instances and fixes configuration
700
  mimatches.
701

702
  Parameters: optional instances list, in case we want to restrict the
703
  checks to only a subset of the instances.
704

705
  Result: a list of tuples, (instance, disk, new-size) for changed
706
  configurations.
707

708
  In normal operation, the list should be empty.
709

710
  @type instances: list
711
  @ivar instances: the list of instances to check, or empty for all instances
712

713
  """
714
  OP_PARAMS = [
715
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
716
    ]
717

    
718

    
719
class OpClusterConfigQuery(OpCode):
720
  """Query cluster configuration values."""
721
  OP_PARAMS = [
722
    _POutputFields
723
    ]
724

    
725

    
726
class OpClusterRename(OpCode):
727
  """Rename the cluster.
728

729
  @type name: C{str}
730
  @ivar name: The new name of the cluster. The name and/or the master IP
731
              address will be changed to match the new name and its IP
732
              address.
733

734
  """
735
  OP_DSC_FIELD = "name"
736
  OP_PARAMS = [
737
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
738
    ]
739

    
740

    
741
class OpClusterSetParams(OpCode):
742
  """Change the parameters of the cluster.
743

744
  @type vg_name: C{str} or C{None}
745
  @ivar vg_name: The new volume group name or None to disable LVM usage.
746

747
  """
748
  OP_PARAMS = [
749
    ("vg_name", None, ht.TMaybeString, "Volume group name"),
750
    ("enabled_hypervisors", None,
751
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
752
            ht.TNone),
753
     "List of enabled hypervisors"),
754
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
755
                              ht.TNone),
756
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
757
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
758
     "Cluster-wide backend parameter defaults"),
759
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
760
                            ht.TNone),
761
     "Cluster-wide per-OS hypervisor parameter defaults"),
762
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
763
                              ht.TNone),
764
     "Cluster-wide OS parameter defaults"),
765
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
766
     "Master candidate pool size"),
767
    ("uid_pool", None, ht.NoType,
768
     "Set UID pool, must be list of lists describing UID ranges (two items,"
769
     " start and end inclusive)"),
770
    ("add_uids", None, ht.NoType,
771
     "Extend UID pool, must be list of lists describing UID ranges (two"
772
     " items, start and end inclusive) to be added"),
773
    ("remove_uids", None, ht.NoType,
774
     "Shrink UID pool, must be list of lists describing UID ranges (two"
775
     " items, start and end inclusive) to be removed"),
776
    ("maintain_node_health", None, ht.TMaybeBool,
777
     "Whether to automatically maintain node health"),
778
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
779
     "Whether to wipe disks before allocating them to instances"),
780
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
781
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
782
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
783
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
784
     "Default iallocator for cluster"),
785
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
786
     "Master network device"),
787
    ("master_netmask", None, ht.TOr(ht.TInt, ht.TNone),
788
     "Netmask of the master IP"),
789
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
790
     "List of reserved LVs"),
791
    ("hidden_os", None, _TestClusterOsList,
792
     "Modify list of hidden operating systems. Each modification must have"
793
     " two items, the operation and the OS name. The operation can be"
794
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
795
    ("blacklisted_os", None, _TestClusterOsList,
796
     "Modify list of blacklisted operating systems. Each modification must have"
797
     " two items, the operation and the OS name. The operation can be"
798
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
799
    ]
800

    
801

    
802
class OpClusterRedistConf(OpCode):
803
  """Force a full push of the cluster configuration.
804

805
  """
806

    
807

    
808
class OpClusterActivateMasterIp(OpCode):
809
  """Activate the master IP on the master node.
810

811
  """
812

    
813

    
814
class OpClusterDeactivateMasterIp(OpCode):
815
  """Deactivate the master IP on the master node.
816

817
  """
818

    
819

    
820
class OpQuery(OpCode):
821
  """Query for resources/items.
822

823
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
824
  @ivar fields: List of fields to retrieve
825
  @ivar qfilter: Query filter
826

827
  """
828
  OP_DSC_FIELD = "what"
829
  OP_PARAMS = [
830
    _PQueryWhat,
831
    _PUseLocking,
832
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
833
     "Requested fields"),
834
    ("qfilter", None, ht.TOr(ht.TNone, ht.TListOf),
835
     "Query filter"),
836
    ]
837

    
838

    
839
class OpQueryFields(OpCode):
840
  """Query for available resource/item fields.
841

842
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
843
  @ivar fields: List of fields to retrieve
844

845
  """
846
  OP_DSC_FIELD = "what"
847
  OP_PARAMS = [
848
    _PQueryWhat,
849
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
850
     "Requested fields; if not given, all are returned"),
851
    ]
852

    
853

    
854
class OpOobCommand(OpCode):
855
  """Interact with OOB."""
856
  OP_PARAMS = [
857
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
858
     "List of nodes to run the OOB command against"),
859
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
860
     "OOB command to be run"),
861
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
862
     "Timeout before the OOB helper will be terminated"),
863
    ("ignore_status", False, ht.TBool,
864
     "Ignores the node offline status for power off"),
865
    ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
866
     "Time in seconds to wait between powering on nodes"),
867
    ]
868

    
869

    
870
# node opcodes
871

    
872
class OpNodeRemove(OpCode):
873
  """Remove a node.
874

875
  @type node_name: C{str}
876
  @ivar node_name: The name of the node to remove. If the node still has
877
                   instances on it, the operation will fail.
878

879
  """
880
  OP_DSC_FIELD = "node_name"
881
  OP_PARAMS = [
882
    _PNodeName,
883
    ]
884

    
885

    
886
class OpNodeAdd(OpCode):
887
  """Add a node to the cluster.
888

889
  @type node_name: C{str}
890
  @ivar node_name: The name of the node to add. This can be a short name,
891
                   but it will be expanded to the FQDN.
892
  @type primary_ip: IP address
893
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
894
                    opcode is submitted, but will be filled during the node
895
                    add (so it will be visible in the job query).
896
  @type secondary_ip: IP address
897
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
898
                      if the cluster has been initialized in 'dual-network'
899
                      mode, otherwise it must not be given.
900
  @type readd: C{bool}
901
  @ivar readd: Whether to re-add an existing node to the cluster. If
902
               this is not passed, then the operation will abort if the node
903
               name is already in the cluster; use this parameter to 'repair'
904
               a node that had its configuration broken, or was reinstalled
905
               without removal from the cluster.
906
  @type group: C{str}
907
  @ivar group: The node group to which this node will belong.
908
  @type vm_capable: C{bool}
909
  @ivar vm_capable: The vm_capable node attribute
910
  @type master_capable: C{bool}
911
  @ivar master_capable: The master_capable node attribute
912

913
  """
914
  OP_DSC_FIELD = "node_name"
915
  OP_PARAMS = [
916
    _PNodeName,
917
    ("primary_ip", None, ht.NoType, "Primary IP address"),
918
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
919
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
920
    ("group", None, ht.TMaybeString, "Initial node group"),
921
    ("master_capable", None, ht.TMaybeBool,
922
     "Whether node can become master or master candidate"),
923
    ("vm_capable", None, ht.TMaybeBool,
924
     "Whether node can host instances"),
925
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
926
    ]
927

    
928

    
929
class OpNodeQuery(OpCode):
930
  """Compute the list of nodes."""
931
  OP_PARAMS = [
932
    _POutputFields,
933
    _PUseLocking,
934
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
935
     "Empty list to query all nodes, node names otherwise"),
936
    ]
937

    
938

    
939
class OpNodeQueryvols(OpCode):
940
  """Get list of volumes on node."""
941
  OP_PARAMS = [
942
    _POutputFields,
943
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
944
     "Empty list to query all nodes, node names otherwise"),
945
    ]
946

    
947

    
948
class OpNodeQueryStorage(OpCode):
949
  """Get information on storage for node(s)."""
950
  OP_PARAMS = [
951
    _POutputFields,
952
    _PStorageType,
953
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
954
    ("name", None, ht.TMaybeString, "Storage name"),
955
    ]
956

    
957

    
958
class OpNodeModifyStorage(OpCode):
959
  """Modifies the properies of a storage unit"""
960
  OP_PARAMS = [
961
    _PNodeName,
962
    _PStorageType,
963
    _PStorageName,
964
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
965
    ]
966

    
967

    
968
class OpRepairNodeStorage(OpCode):
969
  """Repairs the volume group on a node."""
970
  OP_DSC_FIELD = "node_name"
971
  OP_PARAMS = [
972
    _PNodeName,
973
    _PStorageType,
974
    _PStorageName,
975
    _PIgnoreConsistency,
976
    ]
977

    
978

    
979
class OpNodeSetParams(OpCode):
980
  """Change the parameters of a node."""
981
  OP_DSC_FIELD = "node_name"
982
  OP_PARAMS = [
983
    _PNodeName,
984
    _PForce,
985
    ("master_candidate", None, ht.TMaybeBool,
986
     "Whether the node should become a master candidate"),
987
    ("offline", None, ht.TMaybeBool,
988
     "Whether the node should be marked as offline"),
989
    ("drained", None, ht.TMaybeBool,
990
     "Whether the node should be marked as drained"),
991
    ("auto_promote", False, ht.TBool,
992
     "Whether node(s) should be promoted to master candidate if necessary"),
993
    ("master_capable", None, ht.TMaybeBool,
994
     "Denote whether node can become master or master candidate"),
995
    ("vm_capable", None, ht.TMaybeBool,
996
     "Denote whether node can host instances"),
997
    ("secondary_ip", None, ht.TMaybeString,
998
     "Change node's secondary IP address"),
999
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1000
    ("powered", None, ht.TMaybeBool,
1001
     "Whether the node should be marked as powered"),
1002
    ]
1003
  OP_RESULT = _TSetParamsResult
1004

    
1005

    
1006
class OpNodePowercycle(OpCode):
1007
  """Tries to powercycle a node."""
1008
  OP_DSC_FIELD = "node_name"
1009
  OP_PARAMS = [
1010
    _PNodeName,
1011
    _PForce,
1012
    ]
1013

    
1014

    
1015
class OpNodeMigrate(OpCode):
1016
  """Migrate all instances from a node."""
1017
  OP_DSC_FIELD = "node_name"
1018
  OP_PARAMS = [
1019
    _PNodeName,
1020
    _PMigrationMode,
1021
    _PMigrationLive,
1022
    _PMigrationTargetNode,
1023
    ("iallocator", None, ht.TMaybeString,
1024
     "Iallocator for deciding the target node for shared-storage instances"),
1025
    ]
1026

    
1027

    
1028
class OpNodeEvacuate(OpCode):
1029
  """Evacuate instances off a number of nodes."""
1030
  OP_DSC_FIELD = "node_name"
1031
  OP_PARAMS = [
1032
    _PEarlyRelease,
1033
    _PNodeName,
1034
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1035
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1036
    ("mode", ht.NoDefault, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES),
1037
     "Node evacuation mode"),
1038
    ]
1039
  OP_RESULT = TJobIdListOnly
1040

    
1041

    
1042
# instance opcodes
1043

    
1044
class OpInstanceCreate(OpCode):
1045
  """Create an instance.
1046

1047
  @ivar instance_name: Instance name
1048
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1049
  @ivar source_handshake: Signed handshake from source (remote import only)
1050
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1051
  @ivar source_instance_name: Previous name of instance (remote import only)
1052
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1053
    (remote import only)
1054

1055
  """
1056
  OP_DSC_FIELD = "instance_name"
1057
  OP_PARAMS = [
1058
    _PInstanceName,
1059
    _PForceVariant,
1060
    _PWaitForSync,
1061
    _PNameCheck,
1062
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1063
    ("disks", ht.NoDefault,
1064
     # TODO: Generate check from constants.IDISK_PARAMS_TYPES
1065
     ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
1066
                           ht.TOr(ht.TNonEmptyString, ht.TInt))),
1067
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1068
     " each disk definition must contain a ``%s`` value and"
1069
     " can contain an optional ``%s`` value denoting the disk access mode"
1070
     " (%s)" %
1071
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1072
      constants.IDISK_MODE,
1073
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1074
    ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1075
     "Disk template"),
1076
    ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
1077
     "Driver for file-backed disks"),
1078
    ("file_storage_dir", None, ht.TMaybeString,
1079
     "Directory for storing file-backed disks"),
1080
    ("hvparams", ht.EmptyDict, ht.TDict,
1081
     "Hypervisor parameters for instance, hypervisor-dependent"),
1082
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1083
    ("iallocator", None, ht.TMaybeString,
1084
     "Iallocator for deciding which node(s) to use"),
1085
    ("identify_defaults", False, ht.TBool,
1086
     "Reset instance parameters to default if equal"),
1087
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1088
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1089
     "Instance creation mode"),
1090
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1091
     "List of NIC (network interface) definitions, for example"
1092
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1093
     " contain the optional values %s" %
1094
     (constants.INIC_IP,
1095
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1096
    ("no_install", None, ht.TMaybeBool,
1097
     "Do not install the OS (will disable automatic start)"),
1098
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1099
    ("os_type", None, ht.TMaybeString, "Operating system"),
1100
    ("pnode", None, ht.TMaybeString, "Primary node"),
1101
    ("snode", None, ht.TMaybeString, "Secondary node"),
1102
    ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
1103
     "Signed handshake from source (remote import only)"),
1104
    ("source_instance_name", None, ht.TMaybeString,
1105
     "Source instance name (remote import only)"),
1106
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1107
     ht.TPositiveInt,
1108
     "How long source instance was given to shut down (remote import only)"),
1109
    ("source_x509_ca", None, ht.TMaybeString,
1110
     "Source X509 CA in PEM format (remote import only)"),
1111
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1112
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1113
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1114
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1115
    ]
1116
  OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1117

    
1118

    
1119
class OpInstanceReinstall(OpCode):
1120
  """Reinstall an instance's OS."""
1121
  OP_DSC_FIELD = "instance_name"
1122
  OP_PARAMS = [
1123
    _PInstanceName,
1124
    _PForceVariant,
1125
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1126
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1127
    ]
1128

    
1129

    
1130
class OpInstanceRemove(OpCode):
1131
  """Remove an instance."""
1132
  OP_DSC_FIELD = "instance_name"
1133
  OP_PARAMS = [
1134
    _PInstanceName,
1135
    _PShutdownTimeout,
1136
    ("ignore_failures", False, ht.TBool,
1137
     "Whether to ignore failures during removal"),
1138
    ]
1139

    
1140

    
1141
class OpInstanceRename(OpCode):
1142
  """Rename an instance."""
1143
  OP_PARAMS = [
1144
    _PInstanceName,
1145
    _PNameCheck,
1146
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1147
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1148
    ]
1149
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1150

    
1151

    
1152
class OpInstanceStartup(OpCode):
1153
  """Startup an instance."""
1154
  OP_DSC_FIELD = "instance_name"
1155
  OP_PARAMS = [
1156
    _PInstanceName,
1157
    _PForce,
1158
    _PIgnoreOfflineNodes,
1159
    ("hvparams", ht.EmptyDict, ht.TDict,
1160
     "Temporary hypervisor parameters, hypervisor-dependent"),
1161
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1162
    _PNoRemember,
1163
    _PStartupPaused,
1164
    ]
1165

    
1166

    
1167
class OpInstanceShutdown(OpCode):
1168
  """Shutdown an instance."""
1169
  OP_DSC_FIELD = "instance_name"
1170
  OP_PARAMS = [
1171
    _PInstanceName,
1172
    _PIgnoreOfflineNodes,
1173
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
1174
     "How long to wait for instance to shut down"),
1175
    _PNoRemember,
1176
    ]
1177

    
1178

    
1179
class OpInstanceReboot(OpCode):
1180
  """Reboot an instance."""
1181
  OP_DSC_FIELD = "instance_name"
1182
  OP_PARAMS = [
1183
    _PInstanceName,
1184
    _PShutdownTimeout,
1185
    ("ignore_secondaries", False, ht.TBool,
1186
     "Whether to start the instance even if secondary disks are failing"),
1187
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1188
     "How to reboot instance"),
1189
    ]
1190

    
1191

    
1192
class OpInstanceReplaceDisks(OpCode):
1193
  """Replace the disks of an instance."""
1194
  OP_DSC_FIELD = "instance_name"
1195
  OP_PARAMS = [
1196
    _PInstanceName,
1197
    _PEarlyRelease,
1198
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1199
     "Replacement mode"),
1200
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1201
     "Disk indexes"),
1202
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1203
    ("iallocator", None, ht.TMaybeString,
1204
     "Iallocator for deciding new secondary node"),
1205
    ]
1206

    
1207

    
1208
class OpInstanceFailover(OpCode):
1209
  """Failover an instance."""
1210
  OP_DSC_FIELD = "instance_name"
1211
  OP_PARAMS = [
1212
    _PInstanceName,
1213
    _PShutdownTimeout,
1214
    _PIgnoreConsistency,
1215
    _PMigrationTargetNode,
1216
    ("iallocator", None, ht.TMaybeString,
1217
     "Iallocator for deciding the target node for shared-storage instances"),
1218
    ]
1219

    
1220

    
1221
class OpInstanceMigrate(OpCode):
1222
  """Migrate an instance.
1223

1224
  This migrates (without shutting down an instance) to its secondary
1225
  node.
1226

1227
  @ivar instance_name: the name of the instance
1228
  @ivar mode: the migration mode (live, non-live or None for auto)
1229

1230
  """
1231
  OP_DSC_FIELD = "instance_name"
1232
  OP_PARAMS = [
1233
    _PInstanceName,
1234
    _PMigrationMode,
1235
    _PMigrationLive,
1236
    _PMigrationTargetNode,
1237
    ("cleanup", False, ht.TBool,
1238
     "Whether a previously failed migration should be cleaned up"),
1239
    ("iallocator", None, ht.TMaybeString,
1240
     "Iallocator for deciding the target node for shared-storage instances"),
1241
    ("allow_failover", False, ht.TBool,
1242
     "Whether we can fallback to failover if migration is not possible"),
1243
    ]
1244

    
1245

    
1246
class OpInstanceMove(OpCode):
1247
  """Move an instance.
1248

1249
  This move (with shutting down an instance and data copying) to an
1250
  arbitrary node.
1251

1252
  @ivar instance_name: the name of the instance
1253
  @ivar target_node: the destination node
1254

1255
  """
1256
  OP_DSC_FIELD = "instance_name"
1257
  OP_PARAMS = [
1258
    _PInstanceName,
1259
    _PShutdownTimeout,
1260
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1261
    _PIgnoreConsistency,
1262
    ]
1263

    
1264

    
1265
class OpInstanceConsole(OpCode):
1266
  """Connect to an instance's console."""
1267
  OP_DSC_FIELD = "instance_name"
1268
  OP_PARAMS = [
1269
    _PInstanceName
1270
    ]
1271

    
1272

    
1273
class OpInstanceActivateDisks(OpCode):
1274
  """Activate an instance's disks."""
1275
  OP_DSC_FIELD = "instance_name"
1276
  OP_PARAMS = [
1277
    _PInstanceName,
1278
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1279
    ]
1280

    
1281

    
1282
class OpInstanceDeactivateDisks(OpCode):
1283
  """Deactivate an instance's disks."""
1284
  OP_DSC_FIELD = "instance_name"
1285
  OP_PARAMS = [
1286
    _PInstanceName,
1287
    _PForce,
1288
    ]
1289

    
1290

    
1291
class OpInstanceRecreateDisks(OpCode):
1292
  """Recreate an instance's disks."""
1293
  OP_DSC_FIELD = "instance_name"
1294
  OP_PARAMS = [
1295
    _PInstanceName,
1296
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1297
     "List of disk indexes"),
1298
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1299
     "New instance nodes, if relocation is desired"),
1300
    ]
1301

    
1302

    
1303
class OpInstanceQuery(OpCode):
1304
  """Compute the list of instances."""
1305
  OP_PARAMS = [
1306
    _POutputFields,
1307
    _PUseLocking,
1308
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1309
     "Empty list to query all instances, instance names otherwise"),
1310
    ]
1311

    
1312

    
1313
class OpInstanceQueryData(OpCode):
1314
  """Compute the run-time status of instances."""
1315
  OP_PARAMS = [
1316
    _PUseLocking,
1317
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1318
     "Instance names"),
1319
    ("static", False, ht.TBool,
1320
     "Whether to only return configuration data without querying"
1321
     " nodes"),
1322
    ]
1323

    
1324

    
1325
class OpInstanceSetParams(OpCode):
1326
  """Change the parameters of an instance."""
1327
  OP_DSC_FIELD = "instance_name"
1328
  OP_PARAMS = [
1329
    _PInstanceName,
1330
    _PForce,
1331
    _PForceVariant,
1332
    # TODO: Use _TestNicDef
1333
    ("nics", ht.EmptyList, ht.TList,
1334
     "List of NIC changes. Each item is of the form ``(op, settings)``."
1335
     " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1336
     " ``%s`` to remove the last NIC or a number to modify the settings"
1337
     " of the NIC with that index." %
1338
     (constants.DDM_ADD, constants.DDM_REMOVE)),
1339
    ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1340
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1341
    ("hvparams", ht.EmptyDict, ht.TDict,
1342
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1343
    ("disk_template", None, ht.TOr(ht.TNone, _BuildDiskTemplateCheck(False)),
1344
     "Disk template for instance"),
1345
    ("remote_node", None, ht.TMaybeString,
1346
     "Secondary node (used when changing disk template)"),
1347
    ("os_name", None, ht.TMaybeString,
1348
     "Change instance's OS name. Does not reinstall the instance."),
1349
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1350
    ("wait_for_sync", True, ht.TBool,
1351
     "Whether to wait for the disk to synchronize, when changing template"),
1352
    ]
1353
  OP_RESULT = _TSetParamsResult
1354

    
1355

    
1356
class OpInstanceGrowDisk(OpCode):
1357
  """Grow a disk of an instance."""
1358
  OP_DSC_FIELD = "instance_name"
1359
  OP_PARAMS = [
1360
    _PInstanceName,
1361
    _PWaitForSync,
1362
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1363
    ("amount", ht.NoDefault, ht.TInt,
1364
     "Amount of disk space to add (megabytes)"),
1365
    ]
1366

    
1367

    
1368
class OpInstanceChangeGroup(OpCode):
1369
  """Moves an instance to another node group."""
1370
  OP_DSC_FIELD = "instance_name"
1371
  OP_PARAMS = [
1372
    _PInstanceName,
1373
    _PEarlyRelease,
1374
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1375
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1376
     "Destination group names or UUIDs (defaults to \"all but current group\""),
1377
    ]
1378
  OP_RESULT = TJobIdListOnly
1379

    
1380

    
1381
# Node group opcodes
1382

    
1383
class OpGroupAdd(OpCode):
1384
  """Add a node group to the cluster."""
1385
  OP_DSC_FIELD = "group_name"
1386
  OP_PARAMS = [
1387
    _PGroupName,
1388
    _PNodeGroupAllocPolicy,
1389
    _PGroupNodeParams,
1390
    ]
1391

    
1392

    
1393
class OpGroupAssignNodes(OpCode):
1394
  """Assign nodes to a node group."""
1395
  OP_DSC_FIELD = "group_name"
1396
  OP_PARAMS = [
1397
    _PGroupName,
1398
    _PForce,
1399
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1400
     "List of nodes to assign"),
1401
    ]
1402

    
1403

    
1404
class OpGroupQuery(OpCode):
1405
  """Compute the list of node groups."""
1406
  OP_PARAMS = [
1407
    _POutputFields,
1408
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1409
     "Empty list to query all groups, group names otherwise"),
1410
    ]
1411

    
1412

    
1413
class OpGroupSetParams(OpCode):
1414
  """Change the parameters of a node group."""
1415
  OP_DSC_FIELD = "group_name"
1416
  OP_PARAMS = [
1417
    _PGroupName,
1418
    _PNodeGroupAllocPolicy,
1419
    _PGroupNodeParams,
1420
    ]
1421
  OP_RESULT = _TSetParamsResult
1422

    
1423

    
1424
class OpGroupRemove(OpCode):
1425
  """Remove a node group from the cluster."""
1426
  OP_DSC_FIELD = "group_name"
1427
  OP_PARAMS = [
1428
    _PGroupName,
1429
    ]
1430

    
1431

    
1432
class OpGroupRename(OpCode):
1433
  """Rename a node group in the cluster."""
1434
  OP_PARAMS = [
1435
    _PGroupName,
1436
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1437
    ]
1438
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1439

    
1440

    
1441
class OpGroupEvacuate(OpCode):
1442
  """Evacuate a node group in the cluster."""
1443
  OP_DSC_FIELD = "group_name"
1444
  OP_PARAMS = [
1445
    _PGroupName,
1446
    _PEarlyRelease,
1447
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1448
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1449
     "Destination group names or UUIDs"),
1450
    ]
1451
  OP_RESULT = TJobIdListOnly
1452

    
1453

    
1454
# OS opcodes
1455
class OpOsDiagnose(OpCode):
1456
  """Compute the list of guest operating systems."""
1457
  OP_PARAMS = [
1458
    _POutputFields,
1459
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1460
     "Which operating systems to diagnose"),
1461
    ]
1462

    
1463

    
1464
# Exports opcodes
1465
class OpBackupQuery(OpCode):
1466
  """Compute the list of exported images."""
1467
  OP_PARAMS = [
1468
    _PUseLocking,
1469
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1470
     "Empty list to query all nodes, node names otherwise"),
1471
    ]
1472

    
1473

    
1474
class OpBackupPrepare(OpCode):
1475
  """Prepares an instance export.
1476

1477
  @ivar instance_name: Instance name
1478
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1479

1480
  """
1481
  OP_DSC_FIELD = "instance_name"
1482
  OP_PARAMS = [
1483
    _PInstanceName,
1484
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1485
     "Export mode"),
1486
    ]
1487

    
1488

    
1489
class OpBackupExport(OpCode):
1490
  """Export an instance.
1491

1492
  For local exports, the export destination is the node name. For remote
1493
  exports, the export destination is a list of tuples, each consisting of
1494
  hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1495
  the cluster domain secret over the value "${index}:${hostname}:${port}". The
1496
  destination X509 CA must be a signed certificate.
1497

1498
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1499
  @ivar target_node: Export destination
1500
  @ivar x509_key_name: X509 key to use (remote export only)
1501
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1502
                             only)
1503

1504
  """
1505
  OP_DSC_FIELD = "instance_name"
1506
  OP_PARAMS = [
1507
    _PInstanceName,
1508
    _PShutdownTimeout,
1509
    # TODO: Rename target_node as it changes meaning for different export modes
1510
    # (e.g. "destination")
1511
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1512
     "Destination information, depends on export mode"),
1513
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1514
    ("remove_instance", False, ht.TBool,
1515
     "Whether to remove instance after export"),
1516
    ("ignore_remove_failures", False, ht.TBool,
1517
     "Whether to ignore failures while removing instances"),
1518
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1519
     "Export mode"),
1520
    ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1521
     "Name of X509 key (remote export only)"),
1522
    ("destination_x509_ca", None, ht.TMaybeString,
1523
     "Destination X509 CA (remote export only)"),
1524
    ]
1525

    
1526

    
1527
class OpBackupRemove(OpCode):
1528
  """Remove an instance's export."""
1529
  OP_DSC_FIELD = "instance_name"
1530
  OP_PARAMS = [
1531
    _PInstanceName,
1532
    ]
1533

    
1534

    
1535
# Tags opcodes
1536
class OpTagsGet(OpCode):
1537
  """Returns the tags of the given object."""
1538
  OP_DSC_FIELD = "name"
1539
  OP_PARAMS = [
1540
    _PTagKind,
1541
    # Name is only meaningful for nodes and instances
1542
    ("name", ht.NoDefault, ht.TMaybeString, None),
1543
    ]
1544

    
1545

    
1546
class OpTagsSearch(OpCode):
1547
  """Searches the tags in the cluster for a given pattern."""
1548
  OP_DSC_FIELD = "pattern"
1549
  OP_PARAMS = [
1550
    ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1551
    ]
1552

    
1553

    
1554
class OpTagsSet(OpCode):
1555
  """Add a list of tags on a given object."""
1556
  OP_PARAMS = [
1557
    _PTagKind,
1558
    _PTags,
1559
    # Name is only meaningful for nodes and instances
1560
    ("name", ht.NoDefault, ht.TMaybeString, None),
1561
    ]
1562

    
1563

    
1564
class OpTagsDel(OpCode):
1565
  """Remove a list of tags from a given object."""
1566
  OP_PARAMS = [
1567
    _PTagKind,
1568
    _PTags,
1569
    # Name is only meaningful for nodes and instances
1570
    ("name", ht.NoDefault, ht.TMaybeString, None),
1571
    ]
1572

    
1573

    
1574
# Test opcodes
1575
class OpTestDelay(OpCode):
1576
  """Sleeps for a configured amount of time.
1577

1578
  This is used just for debugging and testing.
1579

1580
  Parameters:
1581
    - duration: the time to sleep
1582
    - on_master: if true, sleep on the master
1583
    - on_nodes: list of nodes in which to sleep
1584

1585
  If the on_master parameter is true, it will execute a sleep on the
1586
  master (before any node sleep).
1587

1588
  If the on_nodes list is not empty, it will sleep on those nodes
1589
  (after the sleep on the master, if that is enabled).
1590

1591
  As an additional feature, the case of duration < 0 will be reported
1592
  as an execution error, so this opcode can be used as a failure
1593
  generator. The case of duration == 0 will not be treated specially.
1594

1595
  """
1596
  OP_DSC_FIELD = "duration"
1597
  OP_PARAMS = [
1598
    ("duration", ht.NoDefault, ht.TNumber, None),
1599
    ("on_master", True, ht.TBool, None),
1600
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1601
    ("repeat", 0, ht.TPositiveInt, None),
1602
    ]
1603

    
1604

    
1605
class OpTestAllocator(OpCode):
1606
  """Allocator framework testing.
1607

1608
  This opcode has two modes:
1609
    - gather and return allocator input for a given mode (allocate new
1610
      or replace secondary) and a given instance definition (direction
1611
      'in')
1612
    - run a selected allocator for a given operation (as above) and
1613
      return the allocator output (direction 'out')
1614

1615
  """
1616
  OP_DSC_FIELD = "allocator"
1617
  OP_PARAMS = [
1618
    ("direction", ht.NoDefault,
1619
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1620
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1621
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1622
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1623
     ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1624
                ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1625
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1626
    ("hypervisor", None, ht.TMaybeString, None),
1627
    ("allocator", None, ht.TMaybeString, None),
1628
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1629
    ("memory", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1630
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1631
    ("os", None, ht.TMaybeString, None),
1632
    ("disk_template", None, ht.TMaybeString, None),
1633
    ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1634
     None),
1635
    ("evac_mode", None,
1636
     ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1637
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1638
     None),
1639
    ]
1640

    
1641

    
1642
class OpTestJqueue(OpCode):
1643
  """Utility opcode to test some aspects of the job queue.
1644

1645
  """
1646
  OP_PARAMS = [
1647
    ("notify_waitlock", False, ht.TBool, None),
1648
    ("notify_exec", False, ht.TBool, None),
1649
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1650
    ("fail", False, ht.TBool, None),
1651
    ]
1652

    
1653

    
1654
class OpTestDummy(OpCode):
1655
  """Utility opcode used by unittests.
1656

1657
  """
1658
  OP_PARAMS = [
1659
    ("result", ht.NoDefault, ht.NoType, None),
1660
    ("messages", ht.NoDefault, ht.NoType, None),
1661
    ("fail", ht.NoDefault, ht.NoType, None),
1662
    ("submit_jobs", None, ht.NoType, None),
1663
    ]
1664
  WITH_LU = False
1665

    
1666

    
1667
def _GetOpList():
1668
  """Returns list of all defined opcodes.
1669

1670
  Does not eliminate duplicates by C{OP_ID}.
1671

1672
  """
1673
  return [v for v in globals().values()
1674
          if (isinstance(v, type) and issubclass(v, OpCode) and
1675
              hasattr(v, "OP_ID") and v is not OpCode)]
1676

    
1677

    
1678
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())