Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 3039e2dc

History | View | Annotate | Download (68.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""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
import ipaddr
39

    
40
from ganeti import constants
41
from ganeti import errors
42
from ganeti import ht
43
from ganeti import objects
44
from ganeti import outils
45

    
46

    
47
# Common opcode attributes
48

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

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

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

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

    
65
#: a instance UUID (for single-instance LUs)
66
_PInstanceUuid = ("instance_uuid", None, ht.TMaybeString,
67
                  "Instance UUID")
68

    
69
#: Whether to ignore offline nodes
70
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
71
                        "Whether to ignore offline nodes")
72

    
73
#: a required node name (for single-node LUs)
74
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
75

    
76
#: a node UUID (for use with _PNodeName)
77
_PNodeUuid = ("node_uuid", None, ht.TMaybeString, "Node UUID")
78

    
79
#: a required node group name (for single-group LUs)
80
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
81

    
82
#: Migration type (live/non-live)
83
_PMigrationMode = ("mode", None,
84
                   ht.TMaybe(ht.TElemOf(constants.HT_MIGRATION_MODES)),
85
                   "Migration mode")
86

    
87
#: Obsolete 'live' migration mode (boolean)
88
_PMigrationLive = ("live", None, ht.TMaybeBool,
89
                   "Legacy setting for live migration, do not use")
90

    
91
#: Tag type
92
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
93
             "Tag kind")
94

    
95
#: List of tag strings
96
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
97
          "List of tag names")
98

    
99
_PForceVariant = ("force_variant", False, ht.TBool,
100
                  "Whether to force an unknown OS variant")
101

    
102
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
103
                 "Whether to wait for the disk to synchronize")
104

    
105
_PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
106
                      "Whether to wait for the disk to synchronize"
107
                      " (defaults to false)")
108

    
109
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
110
                       "Whether to ignore disk consistency")
111

    
112
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
113

    
114
_PUseLocking = ("use_locking", False, ht.TBool,
115
                "Whether to use synchronization")
116

    
117
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
118

    
119
_PNodeGroupAllocPolicy = \
120
  ("alloc_policy", None,
121
   ht.TMaybe(ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
122
   "Instance allocation policy")
123

    
124
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
125
                     "Default node parameters for group")
126

    
127
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
128
               "Resource(s) to query for")
129

    
130
_PEarlyRelease = ("early_release", False, ht.TBool,
131
                  "Whether to release locks as soon as possible")
132

    
133
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
134

    
135
#: Do not remember instance state changes
136
_PNoRemember = ("no_remember", False, ht.TBool,
137
                "Do not remember the state change")
138

    
139
#: Target node for instance migration/failover
140
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
141
                         "Target node for shared-storage instances")
142

    
143
_PMigrationTargetNodeUuid = ("target_node_uuid", None, ht.TMaybeString,
144
                             "Target node UUID for shared-storage instances")
145

    
146
_PStartupPaused = ("startup_paused", False, ht.TBool,
147
                   "Pause instance at startup")
148

    
149
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
150

    
151
# Parameters for cluster verification
152
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
153
                         "Whether to simulate errors (useful for debugging)")
154
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
155
_PSkipChecks = ("skip_checks", ht.EmptyList,
156
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
157
                "Which checks to skip")
158
_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
159
                  ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
160
                  "List of error codes that should be treated as warnings")
161

    
162
# Disk parameters
163
_PDiskParams = \
164
  ("diskparams", None,
165
   ht.TMaybe(ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict)),
166
   "Disk templates' parameter defaults")
167

    
168
# Parameters for node resource model
169
_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
170
_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
171

    
172
#: Opportunistic locking
173
_POpportunisticLocking = \
174
  ("opportunistic_locking", False, ht.TBool,
175
   ("Whether to employ opportunistic locking for nodes, meaning nodes"
176
    " already locked by another opcode won't be considered for instance"
177
    " allocation (only when an iallocator is used)"))
178

    
179
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
180
                   "Whether to ignore ipolicy violations")
181

    
182
# Allow runtime changes while migrating
183
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
184
                      "Allow runtime changes (eg. memory ballooning)")
185

    
186
#: IAllocator field builder
187
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
188

    
189
#: a required network name
190
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
191
                 "Set network name")
192

    
193
_PTargetGroups = \
194
  ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
195
   "Destination group names or UUIDs (defaults to \"all but current group\")")
196

    
197
#: OP_ID conversion regular expression
198
_OPID_RE = re.compile("([a-z])([A-Z])")
199

    
200
#: Utility function for L{OpClusterSetParams}
201
_TestClusterOsListItem = \
202
  ht.TAnd(ht.TIsLength(2), ht.TItems([
203
    ht.TElemOf(constants.DDMS_VALUES),
204
    ht.TNonEmptyString,
205
    ]))
206

    
207
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
208

    
209
# TODO: Generate check from constants.INIC_PARAMS_TYPES
210
#: Utility function for testing NIC definitions
211
_TestNicDef = \
212
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
213
                                          ht.TMaybeString))
214

    
215
_TSetParamsResultItemItems = [
216
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
217
  ht.Comment("new value")(ht.TAny),
218
  ]
219

    
220
_TSetParamsResult = \
221
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
222
                     ht.TItems(_TSetParamsResultItemItems)))
223

    
224
# In the disks option we can provide arbitrary parameters too, which
225
# we may not be able to validate at this level, so we just check the
226
# format of the dict here and the checks concerning IDISK_PARAMS will
227
# happen at the LU level
228
_TDiskParams = \
229
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TNonEmptyString,
230
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
231

    
232
_TQueryRow = \
233
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
234
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
235
                                ht.TAny])))
236

    
237
_TQueryResult = ht.TListOf(_TQueryRow)
238

    
239
_TOldQueryRow = ht.TListOf(ht.TAny)
240

    
241
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
242

    
243

    
244
_SUMMARY_PREFIX = {
245
  "CLUSTER_": "C_",
246
  "GROUP_": "G_",
247
  "NODE_": "N_",
248
  "INSTANCE_": "I_",
249
  }
250

    
251
#: Attribute name for dependencies
252
DEPEND_ATTR = "depends"
253

    
254
#: Attribute name for comment
255
COMMENT_ATTR = "comment"
256

    
257

    
258
def _NameComponents(name):
259
  """Split an opcode class name into its components
260

261
  @type name: string
262
  @param name: the class name, as OpXxxYyy
263
  @rtype: array of strings
264
  @return: the components of the name
265

266
  """
267
  assert name.startswith("Op")
268
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
269
  # consume any input, and hence we would just have all the elements
270
  # in the list, one by one; but it seems that split doesn't work on
271
  # non-consuming input, hence we have to process the input string a
272
  # bit
273
  name = _OPID_RE.sub(r"\1,\2", name)
274
  elems = name.split(",")
275
  return elems
276

    
277

    
278
def _NameToId(name):
279
  """Convert an opcode class name to an OP_ID.
280

281
  @type name: string
282
  @param name: the class name, as OpXxxYyy
283
  @rtype: string
284
  @return: the name in the OP_XXXX_YYYY format
285

286
  """
287
  if not name.startswith("Op"):
288
    return None
289
  return "_".join(n.upper() for n in _NameComponents(name))
290

    
291

    
292
def NameToReasonSrc(name):
293
  """Convert an opcode class name to a source string for the reason trail
294

295
  @type name: string
296
  @param name: the class name, as OpXxxYyy
297
  @rtype: string
298
  @return: the name in the OP_XXXX_YYYY format
299

300
  """
301
  if not name.startswith("Op"):
302
    return None
303
  return "%s:%s" % (constants.OPCODE_REASON_SRC_OPCODE,
304
                    "_".join(n.lower() for n in _NameComponents(name)))
305

    
306

    
307
def _GenerateObjectTypeCheck(obj, fields_types):
308
  """Helper to generate type checks for objects.
309

310
  @param obj: The object to generate type checks
311
  @param fields_types: The fields and their types as a dict
312
  @return: A ht type check function
313

314
  """
315
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
316
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
317
  return ht.TStrictDict(True, True, fields_types)
318

    
319

    
320
_TQueryFieldDef = \
321
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
322
    "name": ht.TNonEmptyString,
323
    "title": ht.TNonEmptyString,
324
    "kind": ht.TElemOf(constants.QFT_ALL),
325
    "doc": ht.TNonEmptyString,
326
    })
327

    
328

    
329
def RequireSharedFileStorage():
330
  """Checks that shared file storage is enabled.
331

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

335
  @raise errors.OpPrereqError: when shared file storage is disabled
336

337
  """
338
  if not constants.ENABLE_SHARED_FILE_STORAGE:
339
    raise errors.OpPrereqError("Shared file storage disabled at"
340
                               " configure time", errors.ECODE_INVAL)
341

    
342

    
343
def _BuildDiskTemplateCheck(accept_none):
344
  """Builds check for disk template.
345

346
  @type accept_none: bool
347
  @param accept_none: whether to accept None as a correct value
348
  @rtype: callable
349

350
  """
351
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
352

    
353
  if accept_none:
354
    template_check = ht.TMaybe(template_check)
355

    
356
  return template_check
357

    
358

    
359
def _CheckStorageType(storage_type):
360
  """Ensure a given storage type is valid.
361

362
  """
363
  if storage_type not in constants.VALID_STORAGE_TYPES:
364
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
365
                               errors.ECODE_INVAL)
366
  return True
367

    
368

    
369
#: Storage type parameter
370
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
371
                 "Storage type")
372

    
373

    
374
@ht.WithDesc("IPv4 network")
375
def _CheckCIDRNetNotation(value):
376
  """Ensure a given CIDR notation type is valid.
377

378
  """
379
  try:
380
    ipaddr.IPv4Network(value)
381
  except ipaddr.AddressValueError:
382
    return False
383
  return True
384

    
385

    
386
@ht.WithDesc("IPv4 address")
387
def _CheckCIDRAddrNotation(value):
388
  """Ensure a given CIDR notation type is valid.
389

390
  """
391
  try:
392
    ipaddr.IPv4Address(value)
393
  except ipaddr.AddressValueError:
394
    return False
395
  return True
396

    
397

    
398
@ht.WithDesc("IPv6 address")
399
def _CheckCIDR6AddrNotation(value):
400
  """Ensure a given CIDR notation type is valid.
401

402
  """
403
  try:
404
    ipaddr.IPv6Address(value)
405
  except ipaddr.AddressValueError:
406
    return False
407
  return True
408

    
409

    
410
@ht.WithDesc("IPv6 network")
411
def _CheckCIDR6NetNotation(value):
412
  """Ensure a given CIDR notation type is valid.
413

414
  """
415
  try:
416
    ipaddr.IPv6Network(value)
417
  except ipaddr.AddressValueError:
418
    return False
419
  return True
420

    
421

    
422
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
423
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
424
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
425
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
426
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
427

    
428

    
429
class _AutoOpParamSlots(outils.AutoSlots):
430
  """Meta class for opcode definitions.
431

432
  """
433
  def __new__(mcs, name, bases, attrs):
434
    """Called when a class should be created.
435

436
    @param mcs: The meta class
437
    @param name: Name of created class
438
    @param bases: Base classes
439
    @type attrs: dict
440
    @param attrs: Class attributes
441

442
    """
443
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
444

    
445
    slots = mcs._GetSlots(attrs)
446
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
447
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
448
    assert ("OP_DSC_FORMATTER" not in attrs or
449
            callable(attrs["OP_DSC_FORMATTER"])), \
450
      ("Class '%s' uses non-callable in OP_DSC_FORMATTER (%s)" %
451
       (name, type(attrs["OP_DSC_FORMATTER"])))
452

    
453
    attrs["OP_ID"] = _NameToId(name)
454

    
455
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
456

    
457
  @classmethod
458
  def _GetSlots(mcs, attrs):
459
    """Build the slots out of OP_PARAMS.
460

461
    """
462
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
463
    params = attrs.setdefault("OP_PARAMS", [])
464

    
465
    # Use parameter names as slots
466
    return [pname for (pname, _, _, _) in params]
467

    
468

    
469
class BaseOpCode(outils.ValidatedSlots):
470
  """A simple serializable object.
471

472
  This object serves as a parent class for OpCode without any custom
473
  field handling.
474

475
  """
476
  # pylint: disable=E1101
477
  # as OP_ID is dynamically defined
478
  __metaclass__ = _AutoOpParamSlots
479

    
480
  def __getstate__(self):
481
    """Generic serializer.
482

483
    This method just returns the contents of the instance as a
484
    dictionary.
485

486
    @rtype:  C{dict}
487
    @return: the instance attributes and their values
488

489
    """
490
    state = {}
491
    for name in self.GetAllSlots():
492
      if hasattr(self, name):
493
        state[name] = getattr(self, name)
494
    return state
495

    
496
  def __setstate__(self, state):
497
    """Generic unserializer.
498

499
    This method just restores from the serialized state the attributes
500
    of the current instance.
501

502
    @param state: the serialized opcode data
503
    @type state:  C{dict}
504

505
    """
506
    if not isinstance(state, dict):
507
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
508
                       type(state))
509

    
510
    for name in self.GetAllSlots():
511
      if name not in state and hasattr(self, name):
512
        delattr(self, name)
513

    
514
    for name in state:
515
      setattr(self, name, state[name])
516

    
517
  @classmethod
518
  def GetAllParams(cls):
519
    """Compute list of all parameters for an opcode.
520

521
    """
522
    slots = []
523
    for parent in cls.__mro__:
524
      slots.extend(getattr(parent, "OP_PARAMS", []))
525
    return slots
526

    
527
  def Validate(self, set_defaults): # pylint: disable=W0221
528
    """Validate opcode parameters, optionally setting default values.
529

530
    @type set_defaults: bool
531
    @param set_defaults: Whether to set default values
532
    @raise errors.OpPrereqError: When a parameter value doesn't match
533
                                 requirements
534

535
    """
536
    for (attr_name, default, test, _) in self.GetAllParams():
537
      assert test == ht.NoType or callable(test)
538

    
539
      if not hasattr(self, attr_name):
540
        if default == ht.NoDefault:
541
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
542
                                     (self.OP_ID, attr_name),
543
                                     errors.ECODE_INVAL)
544
        elif set_defaults:
545
          if callable(default):
546
            dval = default()
547
          else:
548
            dval = default
549
          setattr(self, attr_name, dval)
550

    
551
      if test == ht.NoType:
552
        # no tests here
553
        continue
554

    
555
      if set_defaults or hasattr(self, attr_name):
556
        attr_val = getattr(self, attr_name)
557
        if not test(attr_val):
558
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
559
                        " '%s' expecting type %s",
560
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
561
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
562
                                     (self.OP_ID, attr_name),
563
                                     errors.ECODE_INVAL)
564

    
565

    
566
def _BuildJobDepCheck(relative):
567
  """Builds check for job dependencies (L{DEPEND_ATTR}).
568

569
  @type relative: bool
570
  @param relative: Whether to accept relative job IDs (negative)
571
  @rtype: callable
572

573
  """
574
  if relative:
575
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
576
  else:
577
    job_id = ht.TJobId
578

    
579
  job_dep = \
580
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
581
            ht.TIsLength(2),
582
            ht.TItems([job_id,
583
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
584

    
585
  return ht.TMaybeListOf(job_dep)
586

    
587

    
588
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
589

    
590
#: List of submission status and job ID as returned by C{SubmitManyJobs}
591
_TJobIdListItem = \
592
  ht.TAnd(ht.TIsLength(2),
593
          ht.TItems([ht.Comment("success")(ht.TBool),
594
                     ht.Comment("Job ID if successful, error message"
595
                                " otherwise")(ht.TOr(ht.TString,
596
                                                     ht.TJobId))]))
597
TJobIdList = ht.TListOf(_TJobIdListItem)
598

    
599
#: Result containing only list of submitted jobs
600
TJobIdListOnly = ht.TStrictDict(True, True, {
601
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
602
  })
603

    
604

    
605
class OpCode(BaseOpCode):
606
  """Abstract OpCode.
607

608
  This is the root of the actual OpCode hierarchy. All clases derived
609
  from this class should override OP_ID.
610

611
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
612
               children of this class.
613
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
614
                      string returned by Summary(); see the docstring of that
615
                      method for details).
616
  @cvar OP_DSC_FORMATTER: A callable that should format the OP_DSC_FIELD; if
617
                          not present, then the field will be simply converted
618
                          to string
619
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
620
                   get if not already defined, and types they must match.
621
  @cvar OP_RESULT: Callable to verify opcode result
622
  @cvar WITH_LU: Boolean that specifies whether this should be included in
623
      mcpu's dispatch table
624
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
625
                 the check steps
626
  @ivar priority: Opcode priority for queue
627

628
  """
629
  # pylint: disable=E1101
630
  # as OP_ID is dynamically defined
631
  WITH_LU = True
632
  OP_PARAMS = [
633
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
634
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
635
    ("priority", constants.OP_PRIO_DEFAULT,
636
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
637
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
638
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
639
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
640
     " for details"),
641
    (COMMENT_ATTR, None, ht.TMaybeString,
642
     "Comment describing the purpose of the opcode"),
643
    (constants.OPCODE_REASON, None, ht.TMaybeList,
644
     "The reason trail, describing why the OpCode is executed"),
645
    ]
646
  OP_RESULT = None
647

    
648
  def __getstate__(self):
649
    """Specialized getstate for opcodes.
650

651
    This method adds to the state dictionary the OP_ID of the class,
652
    so that on unload we can identify the correct class for
653
    instantiating the opcode.
654

655
    @rtype:   C{dict}
656
    @return:  the state as a dictionary
657

658
    """
659
    data = BaseOpCode.__getstate__(self)
660
    data["OP_ID"] = self.OP_ID
661
    return data
662

    
663
  @classmethod
664
  def LoadOpCode(cls, data):
665
    """Generic load opcode method.
666

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

671
    @type data:  C{dict}
672
    @param data: the serialized opcode
673

674
    """
675
    if not isinstance(data, dict):
676
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
677
    if "OP_ID" not in data:
678
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
679
    op_id = data["OP_ID"]
680
    op_class = None
681
    if op_id in OP_MAPPING:
682
      op_class = OP_MAPPING[op_id]
683
    else:
684
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
685
                       op_id)
686
    op = op_class()
687
    new_data = data.copy()
688
    del new_data["OP_ID"]
689
    op.__setstate__(new_data)
690
    return op
691

    
692
  def Summary(self):
693
    """Generates a summary description of this opcode.
694

695
    The summary is the value of the OP_ID attribute (without the "OP_"
696
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
697
    defined; this field should allow to easily identify the operation
698
    (for an instance creation job, e.g., it would be the instance
699
    name).
700

701
    """
702
    assert self.OP_ID is not None and len(self.OP_ID) > 3
703
    # all OP_ID start with OP_, we remove that
704
    txt = self.OP_ID[3:]
705
    field_name = getattr(self, "OP_DSC_FIELD", None)
706
    if field_name:
707
      field_value = getattr(self, field_name, None)
708
      field_formatter = getattr(self, "OP_DSC_FORMATTER", None)
709
      if callable(field_formatter):
710
        field_value = field_formatter(field_value)
711
      elif isinstance(field_value, (list, tuple)):
712
        field_value = ",".join(str(i) for i in field_value)
713
      txt = "%s(%s)" % (txt, field_value)
714
    return txt
715

    
716
  def TinySummary(self):
717
    """Generates a compact summary description of the opcode.
718

719
    """
720
    assert self.OP_ID.startswith("OP_")
721

    
722
    text = self.OP_ID[3:]
723

    
724
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
725
      if text.startswith(prefix):
726
        return supplement + text[len(prefix):]
727

    
728
    return text
729

    
730

    
731
# cluster opcodes
732

    
733
class OpClusterPostInit(OpCode):
734
  """Post cluster initialization.
735

736
  This opcode does not touch the cluster at all. Its purpose is to run hooks
737
  after the cluster has been initialized.
738

739
  """
740
  OP_RESULT = ht.TBool
741

    
742

    
743
class OpClusterDestroy(OpCode):
744
  """Destroy the cluster.
745

746
  This opcode has no other parameters. All the state is irreversibly
747
  lost after the execution of this opcode.
748

749
  """
750
  OP_RESULT = ht.TNonEmptyString
751

    
752

    
753
class OpClusterQuery(OpCode):
754
  """Query cluster information."""
755
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
756

    
757

    
758
class OpClusterVerify(OpCode):
759
  """Submits all jobs necessary to verify the cluster.
760

761
  """
762
  OP_PARAMS = [
763
    _PDebugSimulateErrors,
764
    _PErrorCodes,
765
    _PSkipChecks,
766
    _PIgnoreErrors,
767
    _PVerbose,
768
    ("group_name", None, ht.TMaybeString, "Group to verify"),
769
    ]
770
  OP_RESULT = TJobIdListOnly
771

    
772

    
773
class OpClusterVerifyConfig(OpCode):
774
  """Verify the cluster config.
775

776
  """
777
  OP_PARAMS = [
778
    _PDebugSimulateErrors,
779
    _PErrorCodes,
780
    _PIgnoreErrors,
781
    _PVerbose,
782
    ]
783
  OP_RESULT = ht.TBool
784

    
785

    
786
class OpClusterVerifyGroup(OpCode):
787
  """Run verify on a node group from the cluster.
788

789
  @type skip_checks: C{list}
790
  @ivar skip_checks: steps to be skipped from the verify process; this
791
                     needs to be a subset of
792
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
793
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
794

795
  """
796
  OP_DSC_FIELD = "group_name"
797
  OP_PARAMS = [
798
    _PGroupName,
799
    _PDebugSimulateErrors,
800
    _PErrorCodes,
801
    _PSkipChecks,
802
    _PIgnoreErrors,
803
    _PVerbose,
804
    ]
805
  OP_RESULT = ht.TBool
806

    
807

    
808
class OpClusterVerifyDisks(OpCode):
809
  """Verify the cluster disks.
810

811
  """
812
  OP_RESULT = TJobIdListOnly
813

    
814

    
815
class OpGroupVerifyDisks(OpCode):
816
  """Verifies the status of all disks in a node group.
817

818
  Result: a tuple of three elements:
819
    - dict of node names with issues (values: error msg)
820
    - list of instances with degraded disks (that should be activated)
821
    - dict of instances with missing logical volumes (values: (node, vol)
822
      pairs with details about the missing volumes)
823

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

829
  Note that only instances that are drbd-based are taken into
830
  consideration. This might need to be revisited in the future.
831

832
  """
833
  OP_DSC_FIELD = "group_name"
834
  OP_PARAMS = [
835
    _PGroupName,
836
    ]
837
  OP_RESULT = \
838
    ht.TAnd(ht.TIsLength(3),
839
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
840
                       ht.TListOf(ht.TString),
841
                       ht.TDictOf(ht.TString,
842
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
843

    
844

    
845
class OpClusterRepairDiskSizes(OpCode):
846
  """Verify the disk sizes of the instances and fixes configuration
847
  mimatches.
848

849
  Parameters: optional instances list, in case we want to restrict the
850
  checks to only a subset of the instances.
851

852
  Result: a list of tuples, (instance, disk, parameter, new-size) for changed
853
  configurations.
854

855
  In normal operation, the list should be empty.
856

857
  @type instances: list
858
  @ivar instances: the list of instances to check, or empty for all instances
859

860
  """
861
  OP_PARAMS = [
862
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
863
    ]
864
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(4),
865
                                 ht.TItems([ht.TNonEmptyString,
866
                                            ht.TNonNegativeInt,
867
                                            ht.TNonEmptyString,
868
                                            ht.TNonNegativeInt])))
869

    
870

    
871
class OpClusterConfigQuery(OpCode):
872
  """Query cluster configuration values."""
873
  OP_PARAMS = [
874
    _POutputFields,
875
    ]
876
  OP_RESULT = ht.TListOf(ht.TAny)
877

    
878

    
879
class OpClusterRename(OpCode):
880
  """Rename the cluster.
881

882
  @type name: C{str}
883
  @ivar name: The new name of the cluster. The name and/or the master IP
884
              address will be changed to match the new name and its IP
885
              address.
886

887
  """
888
  OP_DSC_FIELD = "name"
889
  OP_PARAMS = [
890
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
891
    ]
892
  OP_RESULT = ht.TNonEmptyString
893

    
894

    
895
class OpClusterSetParams(OpCode):
896
  """Change the parameters of the cluster.
897

898
  @type vg_name: C{str} or C{None}
899
  @ivar vg_name: The new volume group name or None to disable LVM usage.
900

901
  """
902
  OP_PARAMS = [
903
    _PForce,
904
    _PHvState,
905
    _PDiskState,
906
    ("vg_name", None, ht.TMaybe(ht.TString), "Volume group name"),
907
    ("enabled_hypervisors", None,
908
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)),
909
                       ht.TTrue)),
910
     "List of enabled hypervisors"),
911
    ("hvparams", None,
912
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
913
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
914
    ("beparams", None, ht.TMaybeDict,
915
     "Cluster-wide backend parameter defaults"),
916
    ("os_hvp", None, ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
917
     "Cluster-wide per-OS hypervisor parameter defaults"),
918
    ("osparams", None,
919
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
920
     "Cluster-wide OS parameter defaults"),
921
    _PDiskParams,
922
    ("candidate_pool_size", None, ht.TMaybe(ht.TPositiveInt),
923
     "Master candidate pool size"),
924
    ("uid_pool", None, ht.NoType,
925
     "Set UID pool, must be list of lists describing UID ranges (two items,"
926
     " start and end inclusive)"),
927
    ("add_uids", None, ht.NoType,
928
     "Extend UID pool, must be list of lists describing UID ranges (two"
929
     " items, start and end inclusive) to be added"),
930
    ("remove_uids", None, ht.NoType,
931
     "Shrink UID pool, must be list of lists describing UID ranges (two"
932
     " items, start and end inclusive) to be removed"),
933
    ("maintain_node_health", None, ht.TMaybeBool,
934
     "Whether to automatically maintain node health"),
935
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
936
     "Whether to wipe disks before allocating them to instances"),
937
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
938
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
939
    ("ipolicy", None, ht.TMaybeDict,
940
     "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
941
    ("drbd_helper", None, ht.TMaybe(ht.TString), "DRBD helper program"),
942
    ("default_iallocator", None, ht.TMaybe(ht.TString),
943
     "Default iallocator for cluster"),
944
    ("master_netdev", None, ht.TMaybe(ht.TString),
945
     "Master network device"),
946
    ("master_netmask", None, ht.TMaybe(ht.TNonNegativeInt),
947
     "Netmask of the master IP"),
948
    ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
949
     "List of reserved LVs"),
950
    ("hidden_os", None, _TestClusterOsList,
951
     "Modify list of hidden operating systems: each modification must have"
952
     " two items, the operation and the OS name; the operation can be"
953
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
954
    ("blacklisted_os", None, _TestClusterOsList,
955
     "Modify list of blacklisted operating systems: each modification must"
956
     " have two items, the operation and the OS name; the operation can be"
957
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
958
    ("use_external_mip_script", None, ht.TMaybeBool,
959
     "Whether to use an external master IP address setup script"),
960
    ("enabled_disk_templates", None,
961
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.DISK_TEMPLATES)),
962
                       ht.TTrue)),
963
     "List of enabled disk templates"),
964
    ("modify_etc_hosts", None, ht.TMaybeBool,
965
     "Whether the cluster can modify and keep in sync the /etc/hosts files"),
966
    ("file_storage_dir", None, ht.TMaybeString,
967
     "Default directory for storing file-backed disks"),
968
    ]
969
  OP_RESULT = ht.TNone
970

    
971

    
972
class OpClusterRedistConf(OpCode):
973
  """Force a full push of the cluster configuration.
974

975
  """
976
  OP_RESULT = ht.TNone
977

    
978

    
979
class OpClusterActivateMasterIp(OpCode):
980
  """Activate the master IP on the master node.
981

982
  """
983
  OP_RESULT = ht.TNone
984

    
985

    
986
class OpClusterDeactivateMasterIp(OpCode):
987
  """Deactivate the master IP on the master node.
988

989
  """
990
  OP_RESULT = ht.TNone
991

    
992

    
993
class OpQuery(OpCode):
994
  """Query for resources/items.
995

996
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
997
  @ivar fields: List of fields to retrieve
998
  @ivar qfilter: Query filter
999

1000
  """
1001
  OP_DSC_FIELD = "what"
1002
  OP_PARAMS = [
1003
    _PQueryWhat,
1004
    _PUseLocking,
1005
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1006
     "Requested fields"),
1007
    ("qfilter", None, ht.TMaybe(ht.TList),
1008
     "Query filter"),
1009
    ]
1010
  OP_RESULT = \
1011
    _GenerateObjectTypeCheck(objects.QueryResponse, {
1012
      "fields": ht.TListOf(_TQueryFieldDef),
1013
      "data": _TQueryResult,
1014
      })
1015

    
1016

    
1017
class OpQueryFields(OpCode):
1018
  """Query for available resource/item fields.
1019

1020
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
1021
  @ivar fields: List of fields to retrieve
1022

1023
  """
1024
  OP_DSC_FIELD = "what"
1025
  OP_PARAMS = [
1026
    _PQueryWhat,
1027
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
1028
     "Requested fields; if not given, all are returned"),
1029
    ]
1030
  OP_RESULT = \
1031
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
1032
      "fields": ht.TListOf(_TQueryFieldDef),
1033
      })
1034

    
1035

    
1036
class OpOobCommand(OpCode):
1037
  """Interact with OOB."""
1038
  OP_PARAMS = [
1039
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1040
     "List of node names to run the OOB command against"),
1041
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1042
     "List of node UUIDs to run the OOB command against"),
1043
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1044
     "OOB command to be run"),
1045
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1046
     "Timeout before the OOB helper will be terminated"),
1047
    ("ignore_status", False, ht.TBool,
1048
     "Ignores the node offline status for power off"),
1049
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1050
     "Time in seconds to wait between powering on nodes"),
1051
    ]
1052
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1053
  OP_RESULT = _TQueryResult
1054

    
1055

    
1056
class OpRestrictedCommand(OpCode):
1057
  """Runs a restricted command on node(s).
1058

1059
  """
1060
  OP_PARAMS = [
1061
    _PUseLocking,
1062
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1063
     "Nodes on which the command should be run (at least one)"),
1064
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1065
     "Node UUIDs on which the command should be run (at least one)"),
1066
    ("command", ht.NoDefault, ht.TNonEmptyString,
1067
     "Command name (no parameters)"),
1068
    ]
1069

    
1070
  _RESULT_ITEMS = [
1071
    ht.Comment("success")(ht.TBool),
1072
    ht.Comment("output or error message")(ht.TString),
1073
    ]
1074

    
1075
  OP_RESULT = \
1076
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1077
                       ht.TItems(_RESULT_ITEMS)))
1078

    
1079

    
1080
# node opcodes
1081

    
1082
class OpNodeRemove(OpCode):
1083
  """Remove a node.
1084

1085
  @type node_name: C{str}
1086
  @ivar node_name: The name of the node to remove. If the node still has
1087
                   instances on it, the operation will fail.
1088

1089
  """
1090
  OP_DSC_FIELD = "node_name"
1091
  OP_PARAMS = [
1092
    _PNodeName,
1093
    _PNodeUuid
1094
    ]
1095
  OP_RESULT = ht.TNone
1096

    
1097

    
1098
class OpNodeAdd(OpCode):
1099
  """Add a node to the cluster.
1100

1101
  @type node_name: C{str}
1102
  @ivar node_name: The name of the node to add. This can be a short name,
1103
                   but it will be expanded to the FQDN.
1104
  @type primary_ip: IP address
1105
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1106
                    opcode is submitted, but will be filled during the node
1107
                    add (so it will be visible in the job query).
1108
  @type secondary_ip: IP address
1109
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1110
                      if the cluster has been initialized in 'dual-network'
1111
                      mode, otherwise it must not be given.
1112
  @type readd: C{bool}
1113
  @ivar readd: Whether to re-add an existing node to the cluster. If
1114
               this is not passed, then the operation will abort if the node
1115
               name is already in the cluster; use this parameter to 'repair'
1116
               a node that had its configuration broken, or was reinstalled
1117
               without removal from the cluster.
1118
  @type group: C{str}
1119
  @ivar group: The node group to which this node will belong.
1120
  @type vm_capable: C{bool}
1121
  @ivar vm_capable: The vm_capable node attribute
1122
  @type master_capable: C{bool}
1123
  @ivar master_capable: The master_capable node attribute
1124

1125
  """
1126
  OP_DSC_FIELD = "node_name"
1127
  OP_PARAMS = [
1128
    _PNodeName,
1129
    _PHvState,
1130
    _PDiskState,
1131
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1132
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1133
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1134
    ("group", None, ht.TMaybeString, "Initial node group"),
1135
    ("master_capable", None, ht.TMaybeBool,
1136
     "Whether node can become master or master candidate"),
1137
    ("vm_capable", None, ht.TMaybeBool,
1138
     "Whether node can host instances"),
1139
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1140
    ]
1141
  OP_RESULT = ht.TNone
1142

    
1143

    
1144
class OpNodeQuery(OpCode):
1145
  """Compute the list of nodes."""
1146
  OP_PARAMS = [
1147
    _POutputFields,
1148
    _PUseLocking,
1149
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1150
     "Empty list to query all nodes, node names otherwise"),
1151
    ]
1152
  OP_RESULT = _TOldQueryResult
1153

    
1154

    
1155
class OpNodeQueryvols(OpCode):
1156
  """Get list of volumes on node."""
1157
  OP_PARAMS = [
1158
    _POutputFields,
1159
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1160
     "Empty list to query all nodes, node names otherwise"),
1161
    ]
1162
  OP_RESULT = ht.TListOf(ht.TAny)
1163

    
1164

    
1165
class OpNodeQueryStorage(OpCode):
1166
  """Get information on storage for node(s)."""
1167
  OP_PARAMS = [
1168
    _POutputFields,
1169
    _PStorageType,
1170
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1171
    ("name", None, ht.TMaybeString, "Storage name"),
1172
    ]
1173
  OP_RESULT = _TOldQueryResult
1174

    
1175

    
1176
class OpNodeModifyStorage(OpCode):
1177
  """Modifies the properies of a storage unit"""
1178
  OP_DSC_FIELD = "node_name"
1179
  OP_PARAMS = [
1180
    _PNodeName,
1181
    _PNodeUuid,
1182
    _PStorageType,
1183
    _PStorageName,
1184
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1185
    ]
1186
  OP_RESULT = ht.TNone
1187

    
1188

    
1189
class OpRepairNodeStorage(OpCode):
1190
  """Repairs the volume group on a node."""
1191
  OP_DSC_FIELD = "node_name"
1192
  OP_PARAMS = [
1193
    _PNodeName,
1194
    _PNodeUuid,
1195
    _PStorageType,
1196
    _PStorageName,
1197
    _PIgnoreConsistency,
1198
    ]
1199
  OP_RESULT = ht.TNone
1200

    
1201

    
1202
class OpNodeSetParams(OpCode):
1203
  """Change the parameters of a node."""
1204
  OP_DSC_FIELD = "node_name"
1205
  OP_PARAMS = [
1206
    _PNodeName,
1207
    _PNodeUuid,
1208
    _PForce,
1209
    _PHvState,
1210
    _PDiskState,
1211
    ("master_candidate", None, ht.TMaybeBool,
1212
     "Whether the node should become a master candidate"),
1213
    ("offline", None, ht.TMaybeBool,
1214
     "Whether the node should be marked as offline"),
1215
    ("drained", None, ht.TMaybeBool,
1216
     "Whether the node should be marked as drained"),
1217
    ("auto_promote", False, ht.TBool,
1218
     "Whether node(s) should be promoted to master candidate if necessary"),
1219
    ("master_capable", None, ht.TMaybeBool,
1220
     "Denote whether node can become master or master candidate"),
1221
    ("vm_capable", None, ht.TMaybeBool,
1222
     "Denote whether node can host instances"),
1223
    ("secondary_ip", None, ht.TMaybeString,
1224
     "Change node's secondary IP address"),
1225
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1226
    ("powered", None, ht.TMaybeBool,
1227
     "Whether the node should be marked as powered"),
1228
    ]
1229
  OP_RESULT = _TSetParamsResult
1230

    
1231

    
1232
class OpNodePowercycle(OpCode):
1233
  """Tries to powercycle a node."""
1234
  OP_DSC_FIELD = "node_name"
1235
  OP_PARAMS = [
1236
    _PNodeName,
1237
    _PNodeUuid,
1238
    _PForce,
1239
    ]
1240
  OP_RESULT = ht.TMaybeString
1241

    
1242

    
1243
class OpNodeMigrate(OpCode):
1244
  """Migrate all instances from a node."""
1245
  OP_DSC_FIELD = "node_name"
1246
  OP_PARAMS = [
1247
    _PNodeName,
1248
    _PNodeUuid,
1249
    _PMigrationMode,
1250
    _PMigrationLive,
1251
    _PMigrationTargetNode,
1252
    _PMigrationTargetNodeUuid,
1253
    _PAllowRuntimeChgs,
1254
    _PIgnoreIpolicy,
1255
    _PIAllocFromDesc("Iallocator for deciding the target node"
1256
                     " for shared-storage instances"),
1257
    ]
1258
  OP_RESULT = TJobIdListOnly
1259

    
1260

    
1261
class OpNodeEvacuate(OpCode):
1262
  """Evacuate instances off a number of nodes."""
1263
  OP_DSC_FIELD = "node_name"
1264
  OP_PARAMS = [
1265
    _PEarlyRelease,
1266
    _PNodeName,
1267
    _PNodeUuid,
1268
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1269
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1270
    _PIAllocFromDesc("Iallocator for computing solution"),
1271
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1272
     "Node evacuation mode"),
1273
    ]
1274
  OP_RESULT = TJobIdListOnly
1275

    
1276

    
1277
# instance opcodes
1278

    
1279
class OpInstanceCreate(OpCode):
1280
  """Create an instance.
1281

1282
  @ivar instance_name: Instance name
1283
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1284
  @ivar source_handshake: Signed handshake from source (remote import only)
1285
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1286
  @ivar source_instance_name: Previous name of instance (remote import only)
1287
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1288
    (remote import only)
1289

1290
  """
1291
  OP_DSC_FIELD = "instance_name"
1292
  OP_PARAMS = [
1293
    _PInstanceName,
1294
    _PForceVariant,
1295
    _PWaitForSync,
1296
    _PNameCheck,
1297
    _PIgnoreIpolicy,
1298
    _POpportunisticLocking,
1299
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1300
    ("disks", ht.NoDefault, ht.TListOf(_TDiskParams),
1301
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1302
     " each disk definition must contain a ``%s`` value and"
1303
     " can contain an optional ``%s`` value denoting the disk access mode"
1304
     " (%s)" %
1305
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1306
      constants.IDISK_MODE,
1307
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1308
    ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1309
     "Disk template"),
1310
    ("file_driver", None, ht.TMaybe(ht.TElemOf(constants.FILE_DRIVER)),
1311
     "Driver for file-backed disks"),
1312
    ("file_storage_dir", None, ht.TMaybeString,
1313
     "Directory for storing file-backed disks"),
1314
    ("hvparams", ht.EmptyDict, ht.TDict,
1315
     "Hypervisor parameters for instance, hypervisor-dependent"),
1316
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1317
    _PIAllocFromDesc("Iallocator for deciding which node(s) to use"),
1318
    ("identify_defaults", False, ht.TBool,
1319
     "Reset instance parameters to default if equal"),
1320
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1321
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1322
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1323
     "Instance creation mode"),
1324
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1325
     "List of NIC (network interface) definitions, for example"
1326
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1327
     " contain the optional values %s" %
1328
     (constants.INIC_IP,
1329
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1330
    ("no_install", None, ht.TMaybeBool,
1331
     "Do not install the OS (will disable automatic start)"),
1332
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1333
    ("os_type", None, ht.TMaybeString, "Operating system"),
1334
    ("pnode", None, ht.TMaybeString, "Primary node"),
1335
    ("pnode_uuid", None, ht.TMaybeString, "Primary node UUID"),
1336
    ("snode", None, ht.TMaybeString, "Secondary node"),
1337
    ("snode_uuid", None, ht.TMaybeString, "Secondary node UUID"),
1338
    ("source_handshake", None, ht.TMaybe(ht.TList),
1339
     "Signed handshake from source (remote import only)"),
1340
    ("source_instance_name", None, ht.TMaybeString,
1341
     "Source instance name (remote import only)"),
1342
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1343
     ht.TNonNegativeInt,
1344
     "How long source instance was given to shut down (remote import only)"),
1345
    ("source_x509_ca", None, ht.TMaybeString,
1346
     "Source X509 CA in PEM format (remote import only)"),
1347
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1348
    ("src_node_uuid", None, ht.TMaybeString, "Source node UUID for import"),
1349
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1350
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1351
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1352
    ]
1353
  OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1354

    
1355

    
1356
class OpInstanceMultiAlloc(OpCode):
1357
  """Allocates multiple instances.
1358

1359
  """
1360
  OP_PARAMS = [
1361
    _POpportunisticLocking,
1362
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1363
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1364
     "List of instance create opcodes describing the instances to allocate"),
1365
    ]
1366
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1367
  ALLOCATABLE_KEY = "allocatable"
1368
  FAILED_KEY = "allocatable"
1369
  OP_RESULT = ht.TStrictDict(True, True, {
1370
    constants.JOB_IDS_KEY: _JOB_LIST,
1371
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1372
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1373
    })
1374

    
1375
  def __getstate__(self):
1376
    """Generic serializer.
1377

1378
    """
1379
    state = OpCode.__getstate__(self)
1380
    if hasattr(self, "instances"):
1381
      # pylint: disable=E1101
1382
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1383
    return state
1384

    
1385
  def __setstate__(self, state):
1386
    """Generic unserializer.
1387

1388
    This method just restores from the serialized state the attributes
1389
    of the current instance.
1390

1391
    @param state: the serialized opcode data
1392
    @type state: C{dict}
1393

1394
    """
1395
    if not isinstance(state, dict):
1396
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1397
                       type(state))
1398

    
1399
    if "instances" in state:
1400
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1401

    
1402
    return OpCode.__setstate__(self, state)
1403

    
1404
  def Validate(self, set_defaults):
1405
    """Validates this opcode.
1406

1407
    We do this recursively.
1408

1409
    """
1410
    OpCode.Validate(self, set_defaults)
1411

    
1412
    for inst in self.instances: # pylint: disable=E1101
1413
      inst.Validate(set_defaults)
1414

    
1415

    
1416
class OpInstanceReinstall(OpCode):
1417
  """Reinstall an instance's OS."""
1418
  OP_DSC_FIELD = "instance_name"
1419
  OP_PARAMS = [
1420
    _PInstanceName,
1421
    _PInstanceUuid,
1422
    _PForceVariant,
1423
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1424
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1425
    ]
1426
  OP_RESULT = ht.TNone
1427

    
1428

    
1429
class OpInstanceRemove(OpCode):
1430
  """Remove an instance."""
1431
  OP_DSC_FIELD = "instance_name"
1432
  OP_PARAMS = [
1433
    _PInstanceName,
1434
    _PInstanceUuid,
1435
    _PShutdownTimeout,
1436
    ("ignore_failures", False, ht.TBool,
1437
     "Whether to ignore failures during removal"),
1438
    ]
1439
  OP_RESULT = ht.TNone
1440

    
1441

    
1442
class OpInstanceRename(OpCode):
1443
  """Rename an instance."""
1444
  OP_PARAMS = [
1445
    _PInstanceName,
1446
    _PInstanceUuid,
1447
    _PNameCheck,
1448
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1449
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1450
    ]
1451
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1452

    
1453

    
1454
class OpInstanceStartup(OpCode):
1455
  """Startup an instance."""
1456
  OP_DSC_FIELD = "instance_name"
1457
  OP_PARAMS = [
1458
    _PInstanceName,
1459
    _PInstanceUuid,
1460
    _PForce,
1461
    _PIgnoreOfflineNodes,
1462
    ("hvparams", ht.EmptyDict, ht.TDict,
1463
     "Temporary hypervisor parameters, hypervisor-dependent"),
1464
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1465
    _PNoRemember,
1466
    _PStartupPaused,
1467
    ]
1468
  OP_RESULT = ht.TNone
1469

    
1470

    
1471
class OpInstanceShutdown(OpCode):
1472
  """Shutdown an instance."""
1473
  OP_DSC_FIELD = "instance_name"
1474
  OP_PARAMS = [
1475
    _PInstanceName,
1476
    _PInstanceUuid,
1477
    _PForce,
1478
    _PIgnoreOfflineNodes,
1479
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1480
     "How long to wait for instance to shut down"),
1481
    _PNoRemember,
1482
    ]
1483
  OP_RESULT = ht.TNone
1484

    
1485

    
1486
class OpInstanceReboot(OpCode):
1487
  """Reboot an instance."""
1488
  OP_DSC_FIELD = "instance_name"
1489
  OP_PARAMS = [
1490
    _PInstanceName,
1491
    _PInstanceUuid,
1492
    _PShutdownTimeout,
1493
    ("ignore_secondaries", False, ht.TBool,
1494
     "Whether to start the instance even if secondary disks are failing"),
1495
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1496
     "How to reboot instance"),
1497
    ]
1498
  OP_RESULT = ht.TNone
1499

    
1500

    
1501
class OpInstanceReplaceDisks(OpCode):
1502
  """Replace the disks of an instance."""
1503
  OP_DSC_FIELD = "instance_name"
1504
  OP_PARAMS = [
1505
    _PInstanceName,
1506
    _PInstanceUuid,
1507
    _PEarlyRelease,
1508
    _PIgnoreIpolicy,
1509
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1510
     "Replacement mode"),
1511
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1512
     "Disk indexes"),
1513
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1514
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1515
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1516
    ]
1517
  OP_RESULT = ht.TNone
1518

    
1519

    
1520
class OpInstanceFailover(OpCode):
1521
  """Failover an instance."""
1522
  OP_DSC_FIELD = "instance_name"
1523
  OP_PARAMS = [
1524
    _PInstanceName,
1525
    _PInstanceUuid,
1526
    _PShutdownTimeout,
1527
    _PIgnoreConsistency,
1528
    _PMigrationTargetNode,
1529
    _PMigrationTargetNodeUuid,
1530
    _PIgnoreIpolicy,
1531
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1532
                     " shared-storage instances"),
1533
    ]
1534
  OP_RESULT = ht.TNone
1535

    
1536

    
1537
class OpInstanceMigrate(OpCode):
1538
  """Migrate an instance.
1539

1540
  This migrates (without shutting down an instance) to its secondary
1541
  node.
1542

1543
  @ivar instance_name: the name of the instance
1544
  @ivar mode: the migration mode (live, non-live or None for auto)
1545

1546
  """
1547
  OP_DSC_FIELD = "instance_name"
1548
  OP_PARAMS = [
1549
    _PInstanceName,
1550
    _PInstanceUuid,
1551
    _PMigrationMode,
1552
    _PMigrationLive,
1553
    _PMigrationTargetNode,
1554
    _PMigrationTargetNodeUuid,
1555
    _PAllowRuntimeChgs,
1556
    _PIgnoreIpolicy,
1557
    ("cleanup", False, ht.TBool,
1558
     "Whether a previously failed migration should be cleaned up"),
1559
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1560
                     " shared-storage instances"),
1561
    ("allow_failover", False, ht.TBool,
1562
     "Whether we can fallback to failover if migration is not possible"),
1563
    ]
1564
  OP_RESULT = ht.TNone
1565

    
1566

    
1567
class OpInstanceMove(OpCode):
1568
  """Move an instance.
1569

1570
  This move (with shutting down an instance and data copying) to an
1571
  arbitrary node.
1572

1573
  @ivar instance_name: the name of the instance
1574
  @ivar target_node: the destination node
1575

1576
  """
1577
  OP_DSC_FIELD = "instance_name"
1578
  OP_PARAMS = [
1579
    _PInstanceName,
1580
    _PInstanceUuid,
1581
    _PShutdownTimeout,
1582
    _PIgnoreIpolicy,
1583
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1584
    ("target_node_uuid", None, ht.TMaybeString, "Target node UUID"),
1585
    _PIgnoreConsistency,
1586
    ]
1587
  OP_RESULT = ht.TNone
1588

    
1589

    
1590
class OpInstanceConsole(OpCode):
1591
  """Connect to an instance's console."""
1592
  OP_DSC_FIELD = "instance_name"
1593
  OP_PARAMS = [
1594
    _PInstanceName,
1595
    _PInstanceUuid,
1596
    ]
1597
  OP_RESULT = ht.TDict
1598

    
1599

    
1600
class OpInstanceActivateDisks(OpCode):
1601
  """Activate an instance's disks."""
1602
  OP_DSC_FIELD = "instance_name"
1603
  OP_PARAMS = [
1604
    _PInstanceName,
1605
    _PInstanceUuid,
1606
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1607
    _PWaitForSyncFalse,
1608
    ]
1609
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1610
                                 ht.TItems([ht.TNonEmptyString,
1611
                                            ht.TNonEmptyString,
1612
                                            ht.TNonEmptyString])))
1613

    
1614

    
1615
class OpInstanceDeactivateDisks(OpCode):
1616
  """Deactivate an instance's disks."""
1617
  OP_DSC_FIELD = "instance_name"
1618
  OP_PARAMS = [
1619
    _PInstanceName,
1620
    _PInstanceUuid,
1621
    _PForce,
1622
    ]
1623
  OP_RESULT = ht.TNone
1624

    
1625

    
1626
class OpInstanceRecreateDisks(OpCode):
1627
  """Recreate an instance's disks."""
1628
  _TDiskChanges = \
1629
    ht.TAnd(ht.TIsLength(2),
1630
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1631
                       ht.Comment("Parameters")(_TDiskParams)]))
1632

    
1633
  OP_DSC_FIELD = "instance_name"
1634
  OP_PARAMS = [
1635
    _PInstanceName,
1636
    _PInstanceUuid,
1637
    ("disks", ht.EmptyList,
1638
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1639
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1640
     " index and a possibly empty dictionary with disk parameter changes"),
1641
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1642
     "New instance nodes, if relocation is desired"),
1643
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1644
     "New instance node UUIDs, if relocation is desired"),
1645
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1646
    ]
1647
  OP_RESULT = ht.TNone
1648

    
1649

    
1650
class OpInstanceQuery(OpCode):
1651
  """Compute the list of instances."""
1652
  OP_PARAMS = [
1653
    _POutputFields,
1654
    _PUseLocking,
1655
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1656
     "Empty list to query all instances, instance names otherwise"),
1657
    ]
1658
  OP_RESULT = _TOldQueryResult
1659

    
1660

    
1661
class OpInstanceQueryData(OpCode):
1662
  """Compute the run-time status of instances."""
1663
  OP_PARAMS = [
1664
    _PUseLocking,
1665
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1666
     "Instance names"),
1667
    ("static", False, ht.TBool,
1668
     "Whether to only return configuration data without querying"
1669
     " nodes"),
1670
    ]
1671
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1672

    
1673

    
1674
def _TestInstSetParamsModList(fn):
1675
  """Generates a check for modification lists.
1676

1677
  """
1678
  # Old format
1679
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1680
  old_mod_item_fn = \
1681
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1682
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1683
      fn,
1684
      ]))
1685

    
1686
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1687
  mod_item_fn = \
1688
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1689
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1690
      ht.Comment("Device index, can be negative, e.g. -1 for last disk")
1691
                 (ht.TOr(ht.TInt, ht.TString)),
1692
      fn,
1693
      ]))
1694

    
1695
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1696
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1697

    
1698

    
1699
class OpInstanceSetParams(OpCode):
1700
  """Change the parameters of an instance.
1701

1702
  """
1703
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1704
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1705

    
1706
  OP_DSC_FIELD = "instance_name"
1707
  OP_PARAMS = [
1708
    _PInstanceName,
1709
    _PInstanceUuid,
1710
    _PForce,
1711
    _PForceVariant,
1712
    _PIgnoreIpolicy,
1713
    ("nics", ht.EmptyList, TestNicModifications,
1714
     "List of NIC changes: each item is of the form"
1715
     " ``(op, identifier, settings)``, ``op`` is one of ``%s``, ``%s`` or"
1716
     " ``%s``, ``identifier`` can be a zero-based index number (or -1 to refer"
1717
     " to the last position), the NIC's UUID of the NIC's name; a"
1718
     " deprecated version of this parameter used the form ``(op, settings)``,"
1719
     " where ``op`` can be ``%s`` to add a new NIC with the specified"
1720
     " settings, ``%s`` to remove the last NIC or a number to modify the"
1721
     " settings of the NIC with that index" %
1722
     (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1723
      constants.DDM_ADD, constants.DDM_REMOVE)),
1724
    ("disks", ht.EmptyList, TestDiskModifications,
1725
     "List of disk changes; see ``nics``"),
1726
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1727
    ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1728
    ("hvparams", ht.EmptyDict, ht.TDict,
1729
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1730
    ("disk_template", None, ht.TMaybe(_BuildDiskTemplateCheck(False)),
1731
     "Disk template for instance"),
1732
    ("pnode", None, ht.TMaybeString, "New primary node"),
1733
    ("pnode_uuid", None, ht.TMaybeString, "New primary node UUID"),
1734
    ("remote_node", None, ht.TMaybeString,
1735
     "Secondary node (used when changing disk template)"),
1736
    ("remote_node_uuid", None, ht.TMaybeString,
1737
     "Secondary node UUID (used when changing disk template)"),
1738
    ("os_name", None, ht.TMaybeString,
1739
     "Change the instance's OS without reinstalling the instance"),
1740
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1741
    ("wait_for_sync", True, ht.TBool,
1742
     "Whether to wait for the disk to synchronize, when changing template"),
1743
    ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1744
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1745
    ]
1746
  OP_RESULT = _TSetParamsResult
1747

    
1748

    
1749
class OpInstanceGrowDisk(OpCode):
1750
  """Grow a disk of an instance."""
1751
  OP_DSC_FIELD = "instance_name"
1752
  OP_PARAMS = [
1753
    _PInstanceName,
1754
    _PInstanceUuid,
1755
    _PWaitForSync,
1756
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1757
    ("amount", ht.NoDefault, ht.TNonNegativeInt,
1758
     "Amount of disk space to add (megabytes)"),
1759
    ("absolute", False, ht.TBool,
1760
     "Whether the amount parameter is an absolute target or a relative one"),
1761
    ]
1762
  OP_RESULT = ht.TNone
1763

    
1764

    
1765
class OpInstanceChangeGroup(OpCode):
1766
  """Moves an instance to another node group."""
1767
  OP_DSC_FIELD = "instance_name"
1768
  OP_PARAMS = [
1769
    _PInstanceName,
1770
    _PInstanceUuid,
1771
    _PEarlyRelease,
1772
    _PIAllocFromDesc("Iallocator for computing solution"),
1773
    _PTargetGroups,
1774
    ]
1775
  OP_RESULT = TJobIdListOnly
1776

    
1777

    
1778
# Node group opcodes
1779

    
1780
class OpGroupAdd(OpCode):
1781
  """Add a node group to the cluster."""
1782
  OP_DSC_FIELD = "group_name"
1783
  OP_PARAMS = [
1784
    _PGroupName,
1785
    _PNodeGroupAllocPolicy,
1786
    _PGroupNodeParams,
1787
    _PDiskParams,
1788
    _PHvState,
1789
    _PDiskState,
1790
    ("ipolicy", None, ht.TMaybeDict,
1791
     "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1792
    ]
1793
  OP_RESULT = ht.TNone
1794

    
1795

    
1796
class OpGroupAssignNodes(OpCode):
1797
  """Assign nodes to a node group."""
1798
  OP_DSC_FIELD = "group_name"
1799
  OP_PARAMS = [
1800
    _PGroupName,
1801
    _PForce,
1802
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1803
     "List of nodes to assign"),
1804
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1805
     "List of node UUIDs to assign"),
1806
    ]
1807
  OP_RESULT = ht.TNone
1808

    
1809

    
1810
class OpGroupQuery(OpCode):
1811
  """Compute the list of node groups."""
1812
  OP_PARAMS = [
1813
    _POutputFields,
1814
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1815
     "Empty list to query all groups, group names otherwise"),
1816
    ]
1817
  OP_RESULT = _TOldQueryResult
1818

    
1819

    
1820
class OpGroupSetParams(OpCode):
1821
  """Change the parameters of a node group."""
1822
  OP_DSC_FIELD = "group_name"
1823
  OP_PARAMS = [
1824
    _PGroupName,
1825
    _PNodeGroupAllocPolicy,
1826
    _PGroupNodeParams,
1827
    _PDiskParams,
1828
    _PHvState,
1829
    _PDiskState,
1830
    ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1831
    ]
1832
  OP_RESULT = _TSetParamsResult
1833

    
1834

    
1835
class OpGroupRemove(OpCode):
1836
  """Remove a node group from the cluster."""
1837
  OP_DSC_FIELD = "group_name"
1838
  OP_PARAMS = [
1839
    _PGroupName,
1840
    ]
1841
  OP_RESULT = ht.TNone
1842

    
1843

    
1844
class OpGroupRename(OpCode):
1845
  """Rename a node group in the cluster."""
1846
  OP_PARAMS = [
1847
    _PGroupName,
1848
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1849
    ]
1850
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1851

    
1852

    
1853
class OpGroupEvacuate(OpCode):
1854
  """Evacuate a node group in the cluster."""
1855
  OP_DSC_FIELD = "group_name"
1856
  OP_PARAMS = [
1857
    _PGroupName,
1858
    _PEarlyRelease,
1859
    _PIAllocFromDesc("Iallocator for computing solution"),
1860
    _PTargetGroups,
1861
    ]
1862
  OP_RESULT = TJobIdListOnly
1863

    
1864

    
1865
# OS opcodes
1866
class OpOsDiagnose(OpCode):
1867
  """Compute the list of guest operating systems."""
1868
  OP_PARAMS = [
1869
    _POutputFields,
1870
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1871
     "Which operating systems to diagnose"),
1872
    ]
1873
  OP_RESULT = _TOldQueryResult
1874

    
1875

    
1876
# ExtStorage opcodes
1877
class OpExtStorageDiagnose(OpCode):
1878
  """Compute the list of external storage providers."""
1879
  OP_PARAMS = [
1880
    _POutputFields,
1881
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1882
     "Which ExtStorage Provider to diagnose"),
1883
    ]
1884
  OP_RESULT = _TOldQueryResult
1885

    
1886

    
1887
# Exports opcodes
1888
class OpBackupQuery(OpCode):
1889
  """Compute the list of exported images."""
1890
  OP_PARAMS = [
1891
    _PUseLocking,
1892
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1893
     "Empty list to query all nodes, node names otherwise"),
1894
    ]
1895
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1896
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1897
                                ht.TListOf(ht.TNonEmptyString)))
1898

    
1899

    
1900
class OpBackupPrepare(OpCode):
1901
  """Prepares an instance export.
1902

1903
  @ivar instance_name: Instance name
1904
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1905

1906
  """
1907
  OP_DSC_FIELD = "instance_name"
1908
  OP_PARAMS = [
1909
    _PInstanceName,
1910
    _PInstanceUuid,
1911
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1912
     "Export mode"),
1913
    ]
1914
  OP_RESULT = ht.TMaybeDict
1915

    
1916

    
1917
class OpBackupExport(OpCode):
1918
  """Export an instance.
1919

1920
  For local exports, the export destination is the node name. For
1921
  remote exports, the export destination is a list of tuples, each
1922
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1923
  salt. The HMAC is calculated using the cluster domain secret over
1924
  the value "${index}:${hostname}:${port}". The destination X509 CA
1925
  must be a signed certificate.
1926

1927
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1928
  @ivar target_node: Export destination
1929
  @ivar x509_key_name: X509 key to use (remote export only)
1930
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1931
                             only)
1932

1933
  """
1934
  OP_DSC_FIELD = "instance_name"
1935
  OP_PARAMS = [
1936
    _PInstanceName,
1937
    _PInstanceUuid,
1938
    _PShutdownTimeout,
1939
    # TODO: Rename target_node as it changes meaning for different export modes
1940
    # (e.g. "destination")
1941
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1942
     "Destination information, depends on export mode"),
1943
    ("target_node_uuid", None, ht.TMaybeString,
1944
     "Target node UUID (if local export)"),
1945
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1946
    ("remove_instance", False, ht.TBool,
1947
     "Whether to remove instance after export"),
1948
    ("ignore_remove_failures", False, ht.TBool,
1949
     "Whether to ignore failures while removing instances"),
1950
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1951
     "Export mode"),
1952
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1953
     "Name of X509 key (remote export only)"),
1954
    ("destination_x509_ca", None, ht.TMaybeString,
1955
     "Destination X509 CA (remote export only)"),
1956
    ]
1957
  OP_RESULT = \
1958
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1959
      ht.Comment("Finalizing status")(ht.TBool),
1960
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1961
      ]))
1962

    
1963

    
1964
class OpBackupRemove(OpCode):
1965
  """Remove an instance's export."""
1966
  OP_DSC_FIELD = "instance_name"
1967
  OP_PARAMS = [
1968
    _PInstanceName,
1969
    _PInstanceUuid,
1970
    ]
1971
  OP_RESULT = ht.TNone
1972

    
1973

    
1974
# Tags opcodes
1975
class OpTagsGet(OpCode):
1976
  """Returns the tags of the given object."""
1977
  OP_DSC_FIELD = "name"
1978
  OP_PARAMS = [
1979
    _PTagKind,
1980
    # Not using _PUseLocking as the default is different for historical reasons
1981
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1982
    # Name is only meaningful for nodes and instances
1983
    ("name", ht.NoDefault, ht.TMaybeString,
1984
     "Name of object to retrieve tags from"),
1985
    ]
1986
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1987

    
1988

    
1989
class OpTagsSearch(OpCode):
1990
  """Searches the tags in the cluster for a given pattern."""
1991
  OP_DSC_FIELD = "pattern"
1992
  OP_PARAMS = [
1993
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1994
     "Search pattern (regular expression)"),
1995
    ]
1996
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1997
    ht.TNonEmptyString,
1998
    ht.TNonEmptyString,
1999
    ])))
2000

    
2001

    
2002
class OpTagsSet(OpCode):
2003
  """Add a list of tags on a given object."""
2004
  OP_PARAMS = [
2005
    _PTagKind,
2006
    _PTags,
2007
    # Name is only meaningful for groups, nodes and instances
2008
    ("name", ht.NoDefault, ht.TMaybeString,
2009
     "Name of object where tag(s) should be added"),
2010
    ]
2011
  OP_RESULT = ht.TNone
2012

    
2013

    
2014
class OpTagsDel(OpCode):
2015
  """Remove a list of tags from a given object."""
2016
  OP_PARAMS = [
2017
    _PTagKind,
2018
    _PTags,
2019
    # Name is only meaningful for groups, nodes and instances
2020
    ("name", ht.NoDefault, ht.TMaybeString,
2021
     "Name of object where tag(s) should be deleted"),
2022
    ]
2023
  OP_RESULT = ht.TNone
2024

    
2025

    
2026
# Test opcodes
2027
class OpTestDelay(OpCode):
2028
  """Sleeps for a configured amount of time.
2029

2030
  This is used just for debugging and testing.
2031

2032
  Parameters:
2033
    - duration: the time to sleep, in seconds
2034
    - on_master: if true, sleep on the master
2035
    - on_nodes: list of nodes in which to sleep
2036

2037
  If the on_master parameter is true, it will execute a sleep on the
2038
  master (before any node sleep).
2039

2040
  If the on_nodes list is not empty, it will sleep on those nodes
2041
  (after the sleep on the master, if that is enabled).
2042

2043
  As an additional feature, the case of duration < 0 will be reported
2044
  as an execution error, so this opcode can be used as a failure
2045
  generator. The case of duration == 0 will not be treated specially.
2046

2047
  """
2048
  OP_DSC_FIELD = "duration"
2049
  OP_PARAMS = [
2050
    ("duration", ht.NoDefault, ht.TNumber, None),
2051
    ("on_master", True, ht.TBool, None),
2052
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2053
    ("on_node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2054
    ("repeat", 0, ht.TNonNegativeInt, None),
2055
    ]
2056

    
2057
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
2058
    """Custom formatter for duration.
2059

2060
    """
2061
    try:
2062
      v = float(value)
2063
    except TypeError:
2064
      v = value
2065
    return str(v)
2066

    
2067

    
2068
class OpTestAllocator(OpCode):
2069
  """Allocator framework testing.
2070

2071
  This opcode has two modes:
2072
    - gather and return allocator input for a given mode (allocate new
2073
      or replace secondary) and a given instance definition (direction
2074
      'in')
2075
    - run a selected allocator for a given operation (as above) and
2076
      return the allocator output (direction 'out')
2077

2078
  """
2079
  OP_DSC_FIELD = "iallocator"
2080
  OP_PARAMS = [
2081
    ("direction", ht.NoDefault,
2082
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
2083
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
2084
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2085
    ("nics", ht.NoDefault,
2086
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2087
                                            constants.INIC_IP,
2088
                                            "bridge"]),
2089
                                ht.TMaybeString)),
2090
     None),
2091
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2092
    ("hypervisor", None, ht.TMaybeString, None),
2093
    _PIAllocFromDesc(None),
2094
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2095
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2096
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2097
    ("os", None, ht.TMaybeString, None),
2098
    ("disk_template", None, ht.TMaybeString, None),
2099
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2100
    ("evac_mode", None,
2101
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2102
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2103
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2104
    ("count", 1, ht.TNonNegativeInt, None),
2105
    ]
2106

    
2107

    
2108
class OpTestJqueue(OpCode):
2109
  """Utility opcode to test some aspects of the job queue.
2110

2111
  """
2112
  OP_PARAMS = [
2113
    ("notify_waitlock", False, ht.TBool, None),
2114
    ("notify_exec", False, ht.TBool, None),
2115
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2116
    ("fail", False, ht.TBool, None),
2117
    ]
2118

    
2119

    
2120
class OpTestDummy(OpCode):
2121
  """Utility opcode used by unittests.
2122

2123
  """
2124
  OP_PARAMS = [
2125
    ("result", ht.NoDefault, ht.NoType, None),
2126
    ("messages", ht.NoDefault, ht.NoType, None),
2127
    ("fail", ht.NoDefault, ht.NoType, None),
2128
    ("submit_jobs", None, ht.NoType, None),
2129
    ]
2130
  WITH_LU = False
2131

    
2132

    
2133
# Network opcodes
2134
# Add a new network in the cluster
2135
class OpNetworkAdd(OpCode):
2136
  """Add an IP network to the cluster."""
2137
  OP_DSC_FIELD = "network_name"
2138
  OP_PARAMS = [
2139
    _PNetworkName,
2140
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2141
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2142
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2143
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2144
    ("mac_prefix", None, ht.TMaybeString,
2145
     "MAC address prefix that overrides cluster one"),
2146
    ("add_reserved_ips", None, _TMaybeAddr4List,
2147
     "Which IP addresses to reserve"),
2148
    ("conflicts_check", True, ht.TBool,
2149
     "Whether to check for conflicting IP addresses"),
2150
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2151
    ]
2152
  OP_RESULT = ht.TNone
2153

    
2154

    
2155
class OpNetworkRemove(OpCode):
2156
  """Remove an existing network from the cluster.
2157
     Must not be connected to any nodegroup.
2158

2159
  """
2160
  OP_DSC_FIELD = "network_name"
2161
  OP_PARAMS = [
2162
    _PNetworkName,
2163
    _PForce,
2164
    ]
2165
  OP_RESULT = ht.TNone
2166

    
2167

    
2168
class OpNetworkSetParams(OpCode):
2169
  """Modify Network's parameters except for IPv4 subnet"""
2170
  OP_DSC_FIELD = "network_name"
2171
  OP_PARAMS = [
2172
    _PNetworkName,
2173
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2174
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2175
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2176
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2177
     "MAC address prefix that overrides cluster one"),
2178
    ("add_reserved_ips", None, _TMaybeAddr4List,
2179
     "Which external IP addresses to reserve"),
2180
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2181
     "Which external IP addresses to release"),
2182
    ]
2183
  OP_RESULT = ht.TNone
2184

    
2185

    
2186
class OpNetworkConnect(OpCode):
2187
  """Connect a Network to a specific Nodegroup with the defined netparams
2188
     (mode, link). Nics in this Network will inherit those params.
2189
     Produce errors if a NIC (that its not already assigned to a network)
2190
     has an IP that is contained in the Network this will produce error unless
2191
     --no-conflicts-check is passed.
2192

2193
  """
2194
  OP_DSC_FIELD = "network_name"
2195
  OP_PARAMS = [
2196
    _PGroupName,
2197
    _PNetworkName,
2198
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2199
     "Connectivity mode"),
2200
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2201
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2202
    ]
2203
  OP_RESULT = ht.TNone
2204

    
2205

    
2206
class OpNetworkDisconnect(OpCode):
2207
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2208
     present in the Network unless --no-conficts-check option is passed.
2209

2210
  """
2211
  OP_DSC_FIELD = "network_name"
2212
  OP_PARAMS = [
2213
    _PGroupName,
2214
    _PNetworkName,
2215
    ]
2216
  OP_RESULT = ht.TNone
2217

    
2218

    
2219
class OpNetworkQuery(OpCode):
2220
  """Compute the list of networks."""
2221
  OP_PARAMS = [
2222
    _POutputFields,
2223
    _PUseLocking,
2224
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2225
     "Empty list to query all groups, group names otherwise"),
2226
    ]
2227
  OP_RESULT = _TOldQueryResult
2228

    
2229

    
2230
def _GetOpList():
2231
  """Returns list of all defined opcodes.
2232

2233
  Does not eliminate duplicates by C{OP_ID}.
2234

2235
  """
2236
  return [v for v in globals().values()
2237
          if (isinstance(v, type) and issubclass(v, OpCode) and
2238
              hasattr(v, "OP_ID") and v is not OpCode)]
2239

    
2240

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