Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 3e3ddbf0

History | View | Annotate | Download (65.4 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 objectutils
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
#: Whether to ignore offline nodes
66
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
67
                        "Whether to ignore offline nodes")
68

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

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

    
75
#: Migration type (live/non-live)
76
_PMigrationMode = ("mode", None,
77
                   ht.TMaybe(ht.TElemOf(constants.HT_MIGRATION_MODES)),
78
                   "Migration mode")
79

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

    
84
#: Tag type
85
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
86
             "Tag kind")
87

    
88
#: List of tag strings
89
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
90
          "List of tag names")
91

    
92
_PForceVariant = ("force_variant", False, ht.TBool,
93
                  "Whether to force an unknown OS variant")
94

    
95
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
96
                 "Whether to wait for the disk to synchronize")
97

    
98
_PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
99
                      "Whether to wait for the disk to synchronize"
100
                      " (defaults to false)")
101

    
102
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
103
                       "Whether to ignore disk consistency")
104

    
105
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
106

    
107
_PUseLocking = ("use_locking", False, ht.TBool,
108
                "Whether to use synchronization")
109

    
110
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
111

    
112
_PNodeGroupAllocPolicy = \
113
  ("alloc_policy", None,
114
   ht.TMaybe(ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
115
   "Instance allocation policy")
116

    
117
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
118
                     "Default node parameters for group")
119

    
120
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
121
               "Resource(s) to query for")
122

    
123
_PEarlyRelease = ("early_release", False, ht.TBool,
124
                  "Whether to release locks as soon as possible")
125

    
126
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
127

    
128
#: Do not remember instance state changes
129
_PNoRemember = ("no_remember", False, ht.TBool,
130
                "Do not remember the state change")
131

    
132
#: Target node for instance migration/failover
133
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
134
                         "Target node for shared-storage instances")
135

    
136
_PStartupPaused = ("startup_paused", False, ht.TBool,
137
                   "Pause instance at startup")
138

    
139
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
140

    
141
# Parameters for cluster verification
142
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
143
                         "Whether to simulate errors (useful for debugging)")
144
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
145
_PSkipChecks = ("skip_checks", ht.EmptyList,
146
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
147
                "Which checks to skip")
148
_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
149
                  ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
150
                  "List of error codes that should be treated as warnings")
151

    
152
# Disk parameters
153
_PDiskParams = \
154
  ("diskparams", None,
155
   ht.TMaybe(ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict)),
156
   "Disk templates' parameter defaults")
157

    
158
# Parameters for node resource model
159
_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
160
_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
161

    
162
#: Opportunistic locking
163
_POpportunisticLocking = \
164
  ("opportunistic_locking", False, ht.TBool,
165
   ("Whether to employ opportunistic locking for nodes, meaning nodes"
166
    " already locked by another opcode won't be considered for instance"
167
    " allocation (only when an iallocator is used)"))
168

    
169
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
170
                   "Whether to ignore ipolicy violations")
171

    
172
# Allow runtime changes while migrating
173
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
174
                      "Allow runtime changes (eg. memory ballooning)")
175

    
176
#: IAllocator field builder
177
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
178

    
179
#: a required network name
180
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
181
                 "Set network name")
182

    
183
#: OP_ID conversion regular expression
184
_OPID_RE = re.compile("([a-z])([A-Z])")
185

    
186
#: Utility function for L{OpClusterSetParams}
187
_TestClusterOsListItem = \
188
  ht.TAnd(ht.TIsLength(2), ht.TItems([
189
    ht.TElemOf(constants.DDMS_VALUES),
190
    ht.TNonEmptyString,
191
    ]))
192

    
193
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
194

    
195
# TODO: Generate check from constants.INIC_PARAMS_TYPES
196
#: Utility function for testing NIC definitions
197
_TestNicDef = \
198
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
199
                                          ht.TMaybeString))
200

    
201
_TSetParamsResultItemItems = [
202
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
203
  ht.Comment("new value")(ht.TAny),
204
  ]
205

    
206
_TSetParamsResult = \
207
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
208
                     ht.TItems(_TSetParamsResultItemItems)))
209

    
210
# In the disks option we can provide arbitrary parameters too, which
211
# we may not be able to validate at this level, so we just check the
212
# format of the dict here and the checks concerning IDISK_PARAMS will
213
# happen at the LU level
214
_TDiskParams = \
215
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TNonEmptyString,
216
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
217

    
218
_TQueryRow = \
219
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
220
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
221
                                ht.TAny])))
222

    
223
_TQueryResult = ht.TListOf(_TQueryRow)
224

    
225
_TOldQueryRow = ht.TListOf(ht.TAny)
226

    
227
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
228

    
229

    
230
_SUMMARY_PREFIX = {
231
  "CLUSTER_": "C_",
232
  "GROUP_": "G_",
233
  "NODE_": "N_",
234
  "INSTANCE_": "I_",
235
  }
236

    
237
#: Attribute name for dependencies
238
DEPEND_ATTR = "depends"
239

    
240
#: Attribute name for comment
241
COMMENT_ATTR = "comment"
242

    
243

    
244
def _NameToId(name):
245
  """Convert an opcode class name to an OP_ID.
246

247
  @type name: string
248
  @param name: the class name, as OpXxxYyy
249
  @rtype: string
250
  @return: the name in the OP_XXXX_YYYY format
251

252
  """
253
  if not name.startswith("Op"):
254
    return None
255
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
256
  # consume any input, and hence we would just have all the elements
257
  # in the list, one by one; but it seems that split doesn't work on
258
  # non-consuming input, hence we have to process the input string a
259
  # bit
260
  name = _OPID_RE.sub(r"\1,\2", name)
261
  elems = name.split(",")
262
  return "_".join(n.upper() for n in elems)
263

    
264

    
265
def _GenerateObjectTypeCheck(obj, fields_types):
266
  """Helper to generate type checks for objects.
267

268
  @param obj: The object to generate type checks
269
  @param fields_types: The fields and their types as a dict
270
  @return: A ht type check function
271

272
  """
273
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
274
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
275
  return ht.TStrictDict(True, True, fields_types)
276

    
277

    
278
_TQueryFieldDef = \
279
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
280
    "name": ht.TNonEmptyString,
281
    "title": ht.TNonEmptyString,
282
    "kind": ht.TElemOf(constants.QFT_ALL),
283
    "doc": ht.TNonEmptyString,
284
    })
285

    
286

    
287
def RequireFileStorage():
288
  """Checks that file storage is enabled.
289

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

293
  @raise errors.OpPrereqError: when file storage is disabled
294

295
  """
296
  if not constants.ENABLE_FILE_STORAGE:
297
    raise errors.OpPrereqError("File storage disabled at configure time",
298
                               errors.ECODE_INVAL)
299

    
300

    
301
def RequireSharedFileStorage():
302
  """Checks that shared file storage is enabled.
303

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

307
  @raise errors.OpPrereqError: when shared file storage is disabled
308

309
  """
310
  if not constants.ENABLE_SHARED_FILE_STORAGE:
311
    raise errors.OpPrereqError("Shared file storage disabled at"
312
                               " configure time", errors.ECODE_INVAL)
313

    
314

    
315
@ht.WithDesc("CheckFileStorage")
316
def _CheckFileStorage(value):
317
  """Ensures file storage is enabled if used.
318

319
  """
320
  if value == constants.DT_FILE:
321
    RequireFileStorage()
322
  elif value == constants.DT_SHARED_FILE:
323
    RequireSharedFileStorage()
324
  return True
325

    
326

    
327
def _BuildDiskTemplateCheck(accept_none):
328
  """Builds check for disk template.
329

330
  @type accept_none: bool
331
  @param accept_none: whether to accept None as a correct value
332
  @rtype: callable
333

334
  """
335
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
336

    
337
  if accept_none:
338
    template_check = ht.TMaybe(template_check)
339

    
340
  return ht.TAnd(template_check, _CheckFileStorage)
341

    
342

    
343
def _CheckStorageType(storage_type):
344
  """Ensure a given storage type is valid.
345

346
  """
347
  if storage_type not in constants.VALID_STORAGE_TYPES:
348
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
349
                               errors.ECODE_INVAL)
350
  if storage_type == constants.ST_FILE:
351
    # TODO: What about shared file storage?
352
    RequireFileStorage()
353
  return True
354

    
355

    
356
#: Storage type parameter
357
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
358
                 "Storage type")
359

    
360
_CheckNetworkType = ht.TElemOf(constants.NETWORK_VALID_TYPES)
361

    
362

    
363
@ht.WithDesc("IPv4 network")
364
def _CheckCIDRNetNotation(value):
365
  """Ensure a given CIDR notation type is valid.
366

367
  """
368
  try:
369
    ipaddr.IPv4Network(value)
370
  except ipaddr.AddressValueError:
371
    return False
372
  return True
373

    
374

    
375
@ht.WithDesc("IPv4 address")
376
def _CheckCIDRAddrNotation(value):
377
  """Ensure a given CIDR notation type is valid.
378

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

    
386

    
387
@ht.WithDesc("IPv6 address")
388
def _CheckCIDR6AddrNotation(value):
389
  """Ensure a given CIDR notation type is valid.
390

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

    
398

    
399
@ht.WithDesc("IPv6 network")
400
def _CheckCIDR6NetNotation(value):
401
  """Ensure a given CIDR notation type is valid.
402

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

    
410

    
411
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
412
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
413
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
414
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
415
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
416

    
417

    
418
class _AutoOpParamSlots(objectutils.AutoSlots):
419
  """Meta class for opcode definitions.
420

421
  """
422
  def __new__(mcs, name, bases, attrs):
423
    """Called when a class should be created.
424

425
    @param mcs: The meta class
426
    @param name: Name of created class
427
    @param bases: Base classes
428
    @type attrs: dict
429
    @param attrs: Class attributes
430

431
    """
432
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
433

    
434
    slots = mcs._GetSlots(attrs)
435
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
436
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
437

    
438
    attrs["OP_ID"] = _NameToId(name)
439

    
440
    return objectutils.AutoSlots.__new__(mcs, name, bases, attrs)
441

    
442
  @classmethod
443
  def _GetSlots(mcs, attrs):
444
    """Build the slots out of OP_PARAMS.
445

446
    """
447
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
448
    params = attrs.setdefault("OP_PARAMS", [])
449

    
450
    # Use parameter names as slots
451
    return [pname for (pname, _, _, _) in params]
452

    
453

    
454
class BaseOpCode(objectutils.ValidatedSlots):
455
  """A simple serializable object.
456

457
  This object serves as a parent class for OpCode without any custom
458
  field handling.
459

460
  """
461
  # pylint: disable=E1101
462
  # as OP_ID is dynamically defined
463
  __metaclass__ = _AutoOpParamSlots
464

    
465
  def __getstate__(self):
466
    """Generic serializer.
467

468
    This method just returns the contents of the instance as a
469
    dictionary.
470

471
    @rtype:  C{dict}
472
    @return: the instance attributes and their values
473

474
    """
475
    state = {}
476
    for name in self.GetAllSlots():
477
      if hasattr(self, name):
478
        state[name] = getattr(self, name)
479
    return state
480

    
481
  def __setstate__(self, state):
482
    """Generic unserializer.
483

484
    This method just restores from the serialized state the attributes
485
    of the current instance.
486

487
    @param state: the serialized opcode data
488
    @type state:  C{dict}
489

490
    """
491
    if not isinstance(state, dict):
492
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
493
                       type(state))
494

    
495
    for name in self.GetAllSlots():
496
      if name not in state and hasattr(self, name):
497
        delattr(self, name)
498

    
499
    for name in state:
500
      setattr(self, name, state[name])
501

    
502
  @classmethod
503
  def GetAllParams(cls):
504
    """Compute list of all parameters for an opcode.
505

506
    """
507
    slots = []
508
    for parent in cls.__mro__:
509
      slots.extend(getattr(parent, "OP_PARAMS", []))
510
    return slots
511

    
512
  def Validate(self, set_defaults): # pylint: disable=W0221
513
    """Validate opcode parameters, optionally setting default values.
514

515
    @type set_defaults: bool
516
    @param set_defaults: Whether to set default values
517
    @raise errors.OpPrereqError: When a parameter value doesn't match
518
                                 requirements
519

520
    """
521
    for (attr_name, default, test, _) in self.GetAllParams():
522
      assert test == ht.NoType or callable(test)
523

    
524
      if not hasattr(self, attr_name):
525
        if default == ht.NoDefault:
526
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
527
                                     (self.OP_ID, attr_name),
528
                                     errors.ECODE_INVAL)
529
        elif set_defaults:
530
          if callable(default):
531
            dval = default()
532
          else:
533
            dval = default
534
          setattr(self, attr_name, dval)
535

    
536
      if test == ht.NoType:
537
        # no tests here
538
        continue
539

    
540
      if set_defaults or hasattr(self, attr_name):
541
        attr_val = getattr(self, attr_name)
542
        if not test(attr_val):
543
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
544
                        " '%s' expecting type %s",
545
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
546
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
547
                                     (self.OP_ID, attr_name),
548
                                     errors.ECODE_INVAL)
549

    
550

    
551
def _BuildJobDepCheck(relative):
552
  """Builds check for job dependencies (L{DEPEND_ATTR}).
553

554
  @type relative: bool
555
  @param relative: Whether to accept relative job IDs (negative)
556
  @rtype: callable
557

558
  """
559
  if relative:
560
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
561
  else:
562
    job_id = ht.TJobId
563

    
564
  job_dep = \
565
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
566
            ht.TIsLength(2),
567
            ht.TItems([job_id,
568
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
569

    
570
  return ht.TMaybeListOf(job_dep)
571

    
572

    
573
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
574

    
575
#: List of submission status and job ID as returned by C{SubmitManyJobs}
576
_TJobIdListItem = \
577
  ht.TAnd(ht.TIsLength(2),
578
          ht.TItems([ht.Comment("success")(ht.TBool),
579
                     ht.Comment("Job ID if successful, error message"
580
                                " otherwise")(ht.TOr(ht.TString,
581
                                                     ht.TJobId))]))
582
TJobIdList = ht.TListOf(_TJobIdListItem)
583

    
584
#: Result containing only list of submitted jobs
585
TJobIdListOnly = ht.TStrictDict(True, True, {
586
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
587
  })
588

    
589

    
590
class OpCode(BaseOpCode):
591
  """Abstract OpCode.
592

593
  This is the root of the actual OpCode hierarchy. All clases derived
594
  from this class should override OP_ID.
595

596
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
597
               children of this class.
598
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
599
                      string returned by Summary(); see the docstring of that
600
                      method for details).
601
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
602
                   get if not already defined, and types they must match.
603
  @cvar OP_RESULT: Callable to verify opcode result
604
  @cvar WITH_LU: Boolean that specifies whether this should be included in
605
      mcpu's dispatch table
606
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
607
                 the check steps
608
  @ivar priority: Opcode priority for queue
609

610
  """
611
  # pylint: disable=E1101
612
  # as OP_ID is dynamically defined
613
  WITH_LU = True
614
  OP_PARAMS = [
615
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
616
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
617
    ("priority", constants.OP_PRIO_DEFAULT,
618
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
619
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
620
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
621
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
622
     " for details"),
623
    (COMMENT_ATTR, None, ht.TMaybeString,
624
     "Comment describing the purpose of the opcode"),
625
    ]
626
  OP_RESULT = None
627

    
628
  def __getstate__(self):
629
    """Specialized getstate for opcodes.
630

631
    This method adds to the state dictionary the OP_ID of the class,
632
    so that on unload we can identify the correct class for
633
    instantiating the opcode.
634

635
    @rtype:   C{dict}
636
    @return:  the state as a dictionary
637

638
    """
639
    data = BaseOpCode.__getstate__(self)
640
    data["OP_ID"] = self.OP_ID
641
    return data
642

    
643
  @classmethod
644
  def LoadOpCode(cls, data):
645
    """Generic load opcode method.
646

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

651
    @type data:  C{dict}
652
    @param data: the serialized opcode
653

654
    """
655
    if not isinstance(data, dict):
656
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
657
    if "OP_ID" not in data:
658
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
659
    op_id = data["OP_ID"]
660
    op_class = None
661
    if op_id in OP_MAPPING:
662
      op_class = OP_MAPPING[op_id]
663
    else:
664
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
665
                       op_id)
666
    op = op_class()
667
    new_data = data.copy()
668
    del new_data["OP_ID"]
669
    op.__setstate__(new_data)
670
    return op
671

    
672
  def Summary(self):
673
    """Generates a summary description of this opcode.
674

675
    The summary is the value of the OP_ID attribute (without the "OP_"
676
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
677
    defined; this field should allow to easily identify the operation
678
    (for an instance creation job, e.g., it would be the instance
679
    name).
680

681
    """
682
    assert self.OP_ID is not None and len(self.OP_ID) > 3
683
    # all OP_ID start with OP_, we remove that
684
    txt = self.OP_ID[3:]
685
    field_name = getattr(self, "OP_DSC_FIELD", None)
686
    if field_name:
687
      field_value = getattr(self, field_name, None)
688
      if isinstance(field_value, (list, tuple)):
689
        field_value = ",".join(str(i) for i in field_value)
690
      txt = "%s(%s)" % (txt, field_value)
691
    return txt
692

    
693
  def TinySummary(self):
694
    """Generates a compact summary description of the opcode.
695

696
    """
697
    assert self.OP_ID.startswith("OP_")
698

    
699
    text = self.OP_ID[3:]
700

    
701
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
702
      if text.startswith(prefix):
703
        return supplement + text[len(prefix):]
704

    
705
    return text
706

    
707

    
708
# cluster opcodes
709

    
710
class OpClusterPostInit(OpCode):
711
  """Post cluster initialization.
712

713
  This opcode does not touch the cluster at all. Its purpose is to run hooks
714
  after the cluster has been initialized.
715

716
  """
717
  OP_RESULT = ht.TBool
718

    
719

    
720
class OpClusterDestroy(OpCode):
721
  """Destroy the cluster.
722

723
  This opcode has no other parameters. All the state is irreversibly
724
  lost after the execution of this opcode.
725

726
  """
727
  OP_RESULT = ht.TNonEmptyString
728

    
729

    
730
class OpClusterQuery(OpCode):
731
  """Query cluster information."""
732
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
733

    
734

    
735
class OpClusterVerify(OpCode):
736
  """Submits all jobs necessary to verify the cluster.
737

738
  """
739
  OP_PARAMS = [
740
    _PDebugSimulateErrors,
741
    _PErrorCodes,
742
    _PSkipChecks,
743
    _PIgnoreErrors,
744
    _PVerbose,
745
    ("group_name", None, ht.TMaybeString, "Group to verify"),
746
    ]
747
  OP_RESULT = TJobIdListOnly
748

    
749

    
750
class OpClusterVerifyConfig(OpCode):
751
  """Verify the cluster config.
752

753
  """
754
  OP_PARAMS = [
755
    _PDebugSimulateErrors,
756
    _PErrorCodes,
757
    _PIgnoreErrors,
758
    _PVerbose,
759
    ]
760
  OP_RESULT = ht.TBool
761

    
762

    
763
class OpClusterVerifyGroup(OpCode):
764
  """Run verify on a node group from the cluster.
765

766
  @type skip_checks: C{list}
767
  @ivar skip_checks: steps to be skipped from the verify process; this
768
                     needs to be a subset of
769
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
770
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
771

772
  """
773
  OP_DSC_FIELD = "group_name"
774
  OP_PARAMS = [
775
    _PGroupName,
776
    _PDebugSimulateErrors,
777
    _PErrorCodes,
778
    _PSkipChecks,
779
    _PIgnoreErrors,
780
    _PVerbose,
781
    ]
782
  OP_RESULT = ht.TBool
783

    
784

    
785
class OpClusterVerifyDisks(OpCode):
786
  """Verify the cluster disks.
787

788
  """
789
  OP_RESULT = TJobIdListOnly
790

    
791

    
792
class OpGroupVerifyDisks(OpCode):
793
  """Verifies the status of all disks in a node group.
794

795
  Result: a tuple of three elements:
796
    - dict of node names with issues (values: error msg)
797
    - list of instances with degraded disks (that should be activated)
798
    - dict of instances with missing logical volumes (values: (node, vol)
799
      pairs with details about the missing volumes)
800

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

806
  Note that only instances that are drbd-based are taken into
807
  consideration. This might need to be revisited in the future.
808

809
  """
810
  OP_DSC_FIELD = "group_name"
811
  OP_PARAMS = [
812
    _PGroupName,
813
    ]
814
  OP_RESULT = \
815
    ht.TAnd(ht.TIsLength(3),
816
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
817
                       ht.TListOf(ht.TString),
818
                       ht.TDictOf(ht.TString,
819
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
820

    
821

    
822
class OpClusterRepairDiskSizes(OpCode):
823
  """Verify the disk sizes of the instances and fixes configuration
824
  mimatches.
825

826
  Parameters: optional instances list, in case we want to restrict the
827
  checks to only a subset of the instances.
828

829
  Result: a list of tuples, (instance, disk, new-size) for changed
830
  configurations.
831

832
  In normal operation, the list should be empty.
833

834
  @type instances: list
835
  @ivar instances: the list of instances to check, or empty for all instances
836

837
  """
838
  OP_PARAMS = [
839
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
840
    ]
841
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
842
                                 ht.TItems([ht.TNonEmptyString,
843
                                            ht.TNonNegativeInt,
844
                                            ht.TNonNegativeInt])))
845

    
846

    
847
class OpClusterConfigQuery(OpCode):
848
  """Query cluster configuration values."""
849
  OP_PARAMS = [
850
    _POutputFields,
851
    ]
852
  OP_RESULT = ht.TListOf(ht.TAny)
853

    
854

    
855
class OpClusterRename(OpCode):
856
  """Rename the cluster.
857

858
  @type name: C{str}
859
  @ivar name: The new name of the cluster. The name and/or the master IP
860
              address will be changed to match the new name and its IP
861
              address.
862

863
  """
864
  OP_DSC_FIELD = "name"
865
  OP_PARAMS = [
866
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
867
    ]
868
  OP_RESULT = ht.TNonEmptyString
869

    
870

    
871
class OpClusterSetParams(OpCode):
872
  """Change the parameters of the cluster.
873

874
  @type vg_name: C{str} or C{None}
875
  @ivar vg_name: The new volume group name or None to disable LVM usage.
876

877
  """
878
  OP_PARAMS = [
879
    _PHvState,
880
    _PDiskState,
881
    ("vg_name", None, ht.TMaybe(ht.TString), "Volume group name"),
882
    ("enabled_hypervisors", None,
883
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)),
884
                       ht.TTrue)),
885
     "List of enabled hypervisors"),
886
    ("hvparams", None,
887
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
888
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
889
    ("beparams", None, ht.TMaybeDict,
890
     "Cluster-wide backend parameter defaults"),
891
    ("os_hvp", None, ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
892
     "Cluster-wide per-OS hypervisor parameter defaults"),
893
    ("osparams", None,
894
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
895
     "Cluster-wide OS parameter defaults"),
896
    _PDiskParams,
897
    ("candidate_pool_size", None, ht.TMaybe(ht.TPositiveInt),
898
     "Master candidate pool size"),
899
    ("uid_pool", None, ht.NoType,
900
     "Set UID pool, must be list of lists describing UID ranges (two items,"
901
     " start and end inclusive)"),
902
    ("add_uids", None, ht.NoType,
903
     "Extend UID pool, must be list of lists describing UID ranges (two"
904
     " items, start and end inclusive) to be added"),
905
    ("remove_uids", None, ht.NoType,
906
     "Shrink UID pool, must be list of lists describing UID ranges (two"
907
     " items, start and end inclusive) to be removed"),
908
    ("maintain_node_health", None, ht.TMaybeBool,
909
     "Whether to automatically maintain node health"),
910
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
911
     "Whether to wipe disks before allocating them to instances"),
912
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
913
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
914
    ("ipolicy", None, ht.TMaybeDict,
915
     "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
916
    ("drbd_helper", None, ht.TMaybe(ht.TString), "DRBD helper program"),
917
    ("default_iallocator", None, ht.TMaybe(ht.TString),
918
     "Default iallocator for cluster"),
919
    ("master_netdev", None, ht.TMaybe(ht.TString),
920
     "Master network device"),
921
    ("master_netmask", None, ht.TMaybe(ht.TNonNegativeInt),
922
     "Netmask of the master IP"),
923
    ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
924
     "List of reserved LVs"),
925
    ("hidden_os", None, _TestClusterOsList,
926
     "Modify list of hidden operating systems: each modification must have"
927
     " two items, the operation and the OS name; the operation can be"
928
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
929
    ("blacklisted_os", None, _TestClusterOsList,
930
     "Modify list of blacklisted operating systems: each modification must"
931
     " have two items, the operation and the OS name; the operation can be"
932
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
933
    ("use_external_mip_script", None, ht.TMaybeBool,
934
     "Whether to use an external master IP address setup script"),
935
    ]
936
  OP_RESULT = ht.TNone
937

    
938

    
939
class OpClusterRedistConf(OpCode):
940
  """Force a full push of the cluster configuration.
941

942
  """
943
  OP_RESULT = ht.TNone
944

    
945

    
946
class OpClusterActivateMasterIp(OpCode):
947
  """Activate the master IP on the master node.
948

949
  """
950
  OP_RESULT = ht.TNone
951

    
952

    
953
class OpClusterDeactivateMasterIp(OpCode):
954
  """Deactivate the master IP on the master node.
955

956
  """
957
  OP_RESULT = ht.TNone
958

    
959

    
960
class OpQuery(OpCode):
961
  """Query for resources/items.
962

963
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
964
  @ivar fields: List of fields to retrieve
965
  @ivar qfilter: Query filter
966

967
  """
968
  OP_DSC_FIELD = "what"
969
  OP_PARAMS = [
970
    _PQueryWhat,
971
    _PUseLocking,
972
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
973
     "Requested fields"),
974
    ("qfilter", None, ht.TMaybe(ht.TList),
975
     "Query filter"),
976
    ]
977
  OP_RESULT = \
978
    _GenerateObjectTypeCheck(objects.QueryResponse, {
979
      "fields": ht.TListOf(_TQueryFieldDef),
980
      "data": _TQueryResult,
981
      })
982

    
983

    
984
class OpQueryFields(OpCode):
985
  """Query for available resource/item fields.
986

987
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
988
  @ivar fields: List of fields to retrieve
989

990
  """
991
  OP_DSC_FIELD = "what"
992
  OP_PARAMS = [
993
    _PQueryWhat,
994
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
995
     "Requested fields; if not given, all are returned"),
996
    ]
997
  OP_RESULT = \
998
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
999
      "fields": ht.TListOf(_TQueryFieldDef),
1000
      })
1001

    
1002

    
1003
class OpOobCommand(OpCode):
1004
  """Interact with OOB."""
1005
  OP_PARAMS = [
1006
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1007
     "List of nodes to run the OOB command against"),
1008
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1009
     "OOB command to be run"),
1010
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1011
     "Timeout before the OOB helper will be terminated"),
1012
    ("ignore_status", False, ht.TBool,
1013
     "Ignores the node offline status for power off"),
1014
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1015
     "Time in seconds to wait between powering on nodes"),
1016
    ]
1017
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1018
  OP_RESULT = _TQueryResult
1019

    
1020

    
1021
class OpRestrictedCommand(OpCode):
1022
  """Runs a restricted command on node(s).
1023

1024
  """
1025
  OP_PARAMS = [
1026
    _PUseLocking,
1027
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1028
     "Nodes on which the command should be run (at least one)"),
1029
    ("command", ht.NoDefault, ht.TNonEmptyString,
1030
     "Command name (no parameters)"),
1031
    ]
1032

    
1033
  _RESULT_ITEMS = [
1034
    ht.Comment("success")(ht.TBool),
1035
    ht.Comment("output or error message")(ht.TString),
1036
    ]
1037

    
1038
  OP_RESULT = \
1039
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1040
                       ht.TItems(_RESULT_ITEMS)))
1041

    
1042

    
1043
# node opcodes
1044

    
1045
class OpNodeRemove(OpCode):
1046
  """Remove a node.
1047

1048
  @type node_name: C{str}
1049
  @ivar node_name: The name of the node to remove. If the node still has
1050
                   instances on it, the operation will fail.
1051

1052
  """
1053
  OP_DSC_FIELD = "node_name"
1054
  OP_PARAMS = [
1055
    _PNodeName,
1056
    ]
1057
  OP_RESULT = ht.TNone
1058

    
1059

    
1060
class OpNodeAdd(OpCode):
1061
  """Add a node to the cluster.
1062

1063
  @type node_name: C{str}
1064
  @ivar node_name: The name of the node to add. This can be a short name,
1065
                   but it will be expanded to the FQDN.
1066
  @type primary_ip: IP address
1067
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1068
                    opcode is submitted, but will be filled during the node
1069
                    add (so it will be visible in the job query).
1070
  @type secondary_ip: IP address
1071
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1072
                      if the cluster has been initialized in 'dual-network'
1073
                      mode, otherwise it must not be given.
1074
  @type readd: C{bool}
1075
  @ivar readd: Whether to re-add an existing node to the cluster. If
1076
               this is not passed, then the operation will abort if the node
1077
               name is already in the cluster; use this parameter to 'repair'
1078
               a node that had its configuration broken, or was reinstalled
1079
               without removal from the cluster.
1080
  @type group: C{str}
1081
  @ivar group: The node group to which this node will belong.
1082
  @type vm_capable: C{bool}
1083
  @ivar vm_capable: The vm_capable node attribute
1084
  @type master_capable: C{bool}
1085
  @ivar master_capable: The master_capable node attribute
1086

1087
  """
1088
  OP_DSC_FIELD = "node_name"
1089
  OP_PARAMS = [
1090
    _PNodeName,
1091
    _PHvState,
1092
    _PDiskState,
1093
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1094
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1095
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1096
    ("group", None, ht.TMaybeString, "Initial node group"),
1097
    ("master_capable", None, ht.TMaybeBool,
1098
     "Whether node can become master or master candidate"),
1099
    ("vm_capable", None, ht.TMaybeBool,
1100
     "Whether node can host instances"),
1101
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1102
    ]
1103
  OP_RESULT = ht.TNone
1104

    
1105

    
1106
class OpNodeQuery(OpCode):
1107
  """Compute the list of nodes."""
1108
  OP_PARAMS = [
1109
    _POutputFields,
1110
    _PUseLocking,
1111
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1112
     "Empty list to query all nodes, node names otherwise"),
1113
    ]
1114
  OP_RESULT = _TOldQueryResult
1115

    
1116

    
1117
class OpNodeQueryvols(OpCode):
1118
  """Get list of volumes on node."""
1119
  OP_PARAMS = [
1120
    _POutputFields,
1121
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1122
     "Empty list to query all nodes, node names otherwise"),
1123
    ]
1124
  OP_RESULT = ht.TListOf(ht.TAny)
1125

    
1126

    
1127
class OpNodeQueryStorage(OpCode):
1128
  """Get information on storage for node(s)."""
1129
  OP_PARAMS = [
1130
    _POutputFields,
1131
    _PStorageType,
1132
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1133
    ("name", None, ht.TMaybeString, "Storage name"),
1134
    ]
1135
  OP_RESULT = _TOldQueryResult
1136

    
1137

    
1138
class OpNodeModifyStorage(OpCode):
1139
  """Modifies the properies of a storage unit"""
1140
  OP_DSC_FIELD = "node_name"
1141
  OP_PARAMS = [
1142
    _PNodeName,
1143
    _PStorageType,
1144
    _PStorageName,
1145
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1146
    ]
1147
  OP_RESULT = ht.TNone
1148

    
1149

    
1150
class OpRepairNodeStorage(OpCode):
1151
  """Repairs the volume group on a node."""
1152
  OP_DSC_FIELD = "node_name"
1153
  OP_PARAMS = [
1154
    _PNodeName,
1155
    _PStorageType,
1156
    _PStorageName,
1157
    _PIgnoreConsistency,
1158
    ]
1159
  OP_RESULT = ht.TNone
1160

    
1161

    
1162
class OpNodeSetParams(OpCode):
1163
  """Change the parameters of a node."""
1164
  OP_DSC_FIELD = "node_name"
1165
  OP_PARAMS = [
1166
    _PNodeName,
1167
    _PForce,
1168
    _PHvState,
1169
    _PDiskState,
1170
    ("master_candidate", None, ht.TMaybeBool,
1171
     "Whether the node should become a master candidate"),
1172
    ("offline", None, ht.TMaybeBool,
1173
     "Whether the node should be marked as offline"),
1174
    ("drained", None, ht.TMaybeBool,
1175
     "Whether the node should be marked as drained"),
1176
    ("auto_promote", False, ht.TBool,
1177
     "Whether node(s) should be promoted to master candidate if necessary"),
1178
    ("master_capable", None, ht.TMaybeBool,
1179
     "Denote whether node can become master or master candidate"),
1180
    ("vm_capable", None, ht.TMaybeBool,
1181
     "Denote whether node can host instances"),
1182
    ("secondary_ip", None, ht.TMaybeString,
1183
     "Change node's secondary IP address"),
1184
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1185
    ("powered", None, ht.TMaybeBool,
1186
     "Whether the node should be marked as powered"),
1187
    ]
1188
  OP_RESULT = _TSetParamsResult
1189

    
1190

    
1191
class OpNodePowercycle(OpCode):
1192
  """Tries to powercycle a node."""
1193
  OP_DSC_FIELD = "node_name"
1194
  OP_PARAMS = [
1195
    _PNodeName,
1196
    _PForce,
1197
    ]
1198
  OP_RESULT = ht.TMaybeString
1199

    
1200

    
1201
class OpNodeMigrate(OpCode):
1202
  """Migrate all instances from a node."""
1203
  OP_DSC_FIELD = "node_name"
1204
  OP_PARAMS = [
1205
    _PNodeName,
1206
    _PMigrationMode,
1207
    _PMigrationLive,
1208
    _PMigrationTargetNode,
1209
    _PAllowRuntimeChgs,
1210
    _PIgnoreIpolicy,
1211
    _PIAllocFromDesc("Iallocator for deciding the target node"
1212
                     " for shared-storage instances"),
1213
    ]
1214
  OP_RESULT = TJobIdListOnly
1215

    
1216

    
1217
class OpNodeEvacuate(OpCode):
1218
  """Evacuate instances off a number of nodes."""
1219
  OP_DSC_FIELD = "node_name"
1220
  OP_PARAMS = [
1221
    _PEarlyRelease,
1222
    _PNodeName,
1223
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1224
    _PIAllocFromDesc("Iallocator for computing solution"),
1225
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1226
     "Node evacuation mode"),
1227
    ]
1228
  OP_RESULT = TJobIdListOnly
1229

    
1230

    
1231
# instance opcodes
1232

    
1233
class OpInstanceCreate(OpCode):
1234
  """Create an instance.
1235

1236
  @ivar instance_name: Instance name
1237
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1238
  @ivar source_handshake: Signed handshake from source (remote import only)
1239
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1240
  @ivar source_instance_name: Previous name of instance (remote import only)
1241
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1242
    (remote import only)
1243

1244
  """
1245
  OP_DSC_FIELD = "instance_name"
1246
  OP_PARAMS = [
1247
    _PInstanceName,
1248
    _PForceVariant,
1249
    _PWaitForSync,
1250
    _PNameCheck,
1251
    _PIgnoreIpolicy,
1252
    _POpportunisticLocking,
1253
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1254
    ("disks", ht.NoDefault, ht.TListOf(_TDiskParams),
1255
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1256
     " each disk definition must contain a ``%s`` value and"
1257
     " can contain an optional ``%s`` value denoting the disk access mode"
1258
     " (%s)" %
1259
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1260
      constants.IDISK_MODE,
1261
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1262
    ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1263
     "Disk template"),
1264
    ("file_driver", None, ht.TMaybe(ht.TElemOf(constants.FILE_DRIVER)),
1265
     "Driver for file-backed disks"),
1266
    ("file_storage_dir", None, ht.TMaybeString,
1267
     "Directory for storing file-backed disks"),
1268
    ("hvparams", ht.EmptyDict, ht.TDict,
1269
     "Hypervisor parameters for instance, hypervisor-dependent"),
1270
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1271
    _PIAllocFromDesc("Iallocator for deciding which node(s) to use"),
1272
    ("identify_defaults", False, ht.TBool,
1273
     "Reset instance parameters to default if equal"),
1274
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1275
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1276
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1277
     "Instance creation mode"),
1278
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1279
     "List of NIC (network interface) definitions, for example"
1280
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1281
     " contain the optional values %s" %
1282
     (constants.INIC_IP,
1283
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1284
    ("no_install", None, ht.TMaybeBool,
1285
     "Do not install the OS (will disable automatic start)"),
1286
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1287
    ("os_type", None, ht.TMaybeString, "Operating system"),
1288
    ("pnode", None, ht.TMaybeString, "Primary node"),
1289
    ("snode", None, ht.TMaybeString, "Secondary node"),
1290
    ("source_handshake", None, ht.TMaybe(ht.TList),
1291
     "Signed handshake from source (remote import only)"),
1292
    ("source_instance_name", None, ht.TMaybeString,
1293
     "Source instance name (remote import only)"),
1294
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1295
     ht.TNonNegativeInt,
1296
     "How long source instance was given to shut down (remote import only)"),
1297
    ("source_x509_ca", None, ht.TMaybeString,
1298
     "Source X509 CA in PEM format (remote import only)"),
1299
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1300
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1301
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1302
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1303
    ]
1304
  OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1305

    
1306

    
1307
class OpInstanceMultiAlloc(OpCode):
1308
  """Allocates multiple instances.
1309

1310
  """
1311
  OP_PARAMS = [
1312
    _POpportunisticLocking,
1313
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1314
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1315
     "List of instance create opcodes describing the instances to allocate"),
1316
    ]
1317
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1318
  ALLOCATABLE_KEY = "allocatable"
1319
  FAILED_KEY = "allocatable"
1320
  OP_RESULT = ht.TStrictDict(True, True, {
1321
    constants.JOB_IDS_KEY: _JOB_LIST,
1322
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1323
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1324
    })
1325

    
1326
  def __getstate__(self):
1327
    """Generic serializer.
1328

1329
    """
1330
    state = OpCode.__getstate__(self)
1331
    if hasattr(self, "instances"):
1332
      # pylint: disable=E1101
1333
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1334
    return state
1335

    
1336
  def __setstate__(self, state):
1337
    """Generic unserializer.
1338

1339
    This method just restores from the serialized state the attributes
1340
    of the current instance.
1341

1342
    @param state: the serialized opcode data
1343
    @type state: C{dict}
1344

1345
    """
1346
    if not isinstance(state, dict):
1347
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1348
                       type(state))
1349

    
1350
    if "instances" in state:
1351
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1352

    
1353
    return OpCode.__setstate__(self, state)
1354

    
1355
  def Validate(self, set_defaults):
1356
    """Validates this opcode.
1357

1358
    We do this recursively.
1359

1360
    """
1361
    OpCode.Validate(self, set_defaults)
1362

    
1363
    for inst in self.instances: # pylint: disable=E1101
1364
      inst.Validate(set_defaults)
1365

    
1366

    
1367
class OpInstanceReinstall(OpCode):
1368
  """Reinstall an instance's OS."""
1369
  OP_DSC_FIELD = "instance_name"
1370
  OP_PARAMS = [
1371
    _PInstanceName,
1372
    _PForceVariant,
1373
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1374
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1375
    ]
1376
  OP_RESULT = ht.TNone
1377

    
1378

    
1379
class OpInstanceRemove(OpCode):
1380
  """Remove an instance."""
1381
  OP_DSC_FIELD = "instance_name"
1382
  OP_PARAMS = [
1383
    _PInstanceName,
1384
    _PShutdownTimeout,
1385
    ("ignore_failures", False, ht.TBool,
1386
     "Whether to ignore failures during removal"),
1387
    ]
1388
  OP_RESULT = ht.TNone
1389

    
1390

    
1391
class OpInstanceRename(OpCode):
1392
  """Rename an instance."""
1393
  OP_PARAMS = [
1394
    _PInstanceName,
1395
    _PNameCheck,
1396
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1397
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1398
    ]
1399
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1400

    
1401

    
1402
class OpInstanceStartup(OpCode):
1403
  """Startup an instance."""
1404
  OP_DSC_FIELD = "instance_name"
1405
  OP_PARAMS = [
1406
    _PInstanceName,
1407
    _PForce,
1408
    _PIgnoreOfflineNodes,
1409
    ("hvparams", ht.EmptyDict, ht.TDict,
1410
     "Temporary hypervisor parameters, hypervisor-dependent"),
1411
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1412
    _PNoRemember,
1413
    _PStartupPaused,
1414
    ]
1415
  OP_RESULT = ht.TNone
1416

    
1417

    
1418
class OpInstanceShutdown(OpCode):
1419
  """Shutdown an instance."""
1420
  OP_DSC_FIELD = "instance_name"
1421
  OP_PARAMS = [
1422
    _PInstanceName,
1423
    _PIgnoreOfflineNodes,
1424
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1425
     "How long to wait for instance to shut down"),
1426
    _PNoRemember,
1427
    ]
1428
  OP_RESULT = ht.TNone
1429

    
1430

    
1431
class OpInstanceReboot(OpCode):
1432
  """Reboot an instance."""
1433
  OP_DSC_FIELD = "instance_name"
1434
  OP_PARAMS = [
1435
    _PInstanceName,
1436
    _PShutdownTimeout,
1437
    ("ignore_secondaries", False, ht.TBool,
1438
     "Whether to start the instance even if secondary disks are failing"),
1439
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1440
     "How to reboot instance"),
1441
    ]
1442
  OP_RESULT = ht.TNone
1443

    
1444

    
1445
class OpInstanceReplaceDisks(OpCode):
1446
  """Replace the disks of an instance."""
1447
  OP_DSC_FIELD = "instance_name"
1448
  OP_PARAMS = [
1449
    _PInstanceName,
1450
    _PEarlyRelease,
1451
    _PIgnoreIpolicy,
1452
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1453
     "Replacement mode"),
1454
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1455
     "Disk indexes"),
1456
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1457
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1458
    ]
1459
  OP_RESULT = ht.TNone
1460

    
1461

    
1462
class OpInstanceFailover(OpCode):
1463
  """Failover an instance."""
1464
  OP_DSC_FIELD = "instance_name"
1465
  OP_PARAMS = [
1466
    _PInstanceName,
1467
    _PShutdownTimeout,
1468
    _PIgnoreConsistency,
1469
    _PMigrationTargetNode,
1470
    _PIgnoreIpolicy,
1471
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1472
                     " shared-storage instances"),
1473
    ]
1474
  OP_RESULT = ht.TNone
1475

    
1476

    
1477
class OpInstanceMigrate(OpCode):
1478
  """Migrate an instance.
1479

1480
  This migrates (without shutting down an instance) to its secondary
1481
  node.
1482

1483
  @ivar instance_name: the name of the instance
1484
  @ivar mode: the migration mode (live, non-live or None for auto)
1485

1486
  """
1487
  OP_DSC_FIELD = "instance_name"
1488
  OP_PARAMS = [
1489
    _PInstanceName,
1490
    _PMigrationMode,
1491
    _PMigrationLive,
1492
    _PMigrationTargetNode,
1493
    _PAllowRuntimeChgs,
1494
    _PIgnoreIpolicy,
1495
    ("cleanup", False, ht.TBool,
1496
     "Whether a previously failed migration should be cleaned up"),
1497
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1498
                     " shared-storage instances"),
1499
    ("allow_failover", False, ht.TBool,
1500
     "Whether we can fallback to failover if migration is not possible"),
1501
    ]
1502
  OP_RESULT = ht.TNone
1503

    
1504

    
1505
class OpInstanceMove(OpCode):
1506
  """Move an instance.
1507

1508
  This move (with shutting down an instance and data copying) to an
1509
  arbitrary node.
1510

1511
  @ivar instance_name: the name of the instance
1512
  @ivar target_node: the destination node
1513

1514
  """
1515
  OP_DSC_FIELD = "instance_name"
1516
  OP_PARAMS = [
1517
    _PInstanceName,
1518
    _PShutdownTimeout,
1519
    _PIgnoreIpolicy,
1520
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1521
    _PIgnoreConsistency,
1522
    ]
1523
  OP_RESULT = ht.TNone
1524

    
1525

    
1526
class OpInstanceConsole(OpCode):
1527
  """Connect to an instance's console."""
1528
  OP_DSC_FIELD = "instance_name"
1529
  OP_PARAMS = [
1530
    _PInstanceName,
1531
    ]
1532
  OP_RESULT = ht.TDict
1533

    
1534

    
1535
class OpInstanceActivateDisks(OpCode):
1536
  """Activate an instance's disks."""
1537
  OP_DSC_FIELD = "instance_name"
1538
  OP_PARAMS = [
1539
    _PInstanceName,
1540
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1541
    _PWaitForSyncFalse,
1542
    ]
1543
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1544
                                 ht.TItems([ht.TNonEmptyString,
1545
                                            ht.TNonEmptyString,
1546
                                            ht.TNonEmptyString])))
1547

    
1548

    
1549
class OpInstanceDeactivateDisks(OpCode):
1550
  """Deactivate an instance's disks."""
1551
  OP_DSC_FIELD = "instance_name"
1552
  OP_PARAMS = [
1553
    _PInstanceName,
1554
    _PForce,
1555
    ]
1556
  OP_RESULT = ht.TNone
1557

    
1558

    
1559
class OpInstanceRecreateDisks(OpCode):
1560
  """Recreate an instance's disks."""
1561
  _TDiskChanges = \
1562
    ht.TAnd(ht.TIsLength(2),
1563
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1564
                       ht.Comment("Parameters")(_TDiskParams)]))
1565

    
1566
  OP_DSC_FIELD = "instance_name"
1567
  OP_PARAMS = [
1568
    _PInstanceName,
1569
    ("disks", ht.EmptyList,
1570
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1571
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1572
     " index and a possibly empty dictionary with disk parameter changes"),
1573
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1574
     "New instance nodes, if relocation is desired"),
1575
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1576
    ]
1577
  OP_RESULT = ht.TNone
1578

    
1579

    
1580
class OpInstanceQuery(OpCode):
1581
  """Compute the list of instances."""
1582
  OP_PARAMS = [
1583
    _POutputFields,
1584
    _PUseLocking,
1585
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1586
     "Empty list to query all instances, instance names otherwise"),
1587
    ]
1588
  OP_RESULT = _TOldQueryResult
1589

    
1590

    
1591
class OpInstanceQueryData(OpCode):
1592
  """Compute the run-time status of instances."""
1593
  OP_PARAMS = [
1594
    _PUseLocking,
1595
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1596
     "Instance names"),
1597
    ("static", False, ht.TBool,
1598
     "Whether to only return configuration data without querying"
1599
     " nodes"),
1600
    ]
1601
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1602

    
1603

    
1604
def _TestInstSetParamsModList(fn):
1605
  """Generates a check for modification lists.
1606

1607
  """
1608
  # Old format
1609
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1610
  old_mod_item_fn = \
1611
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1612
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1613
      fn,
1614
      ]))
1615

    
1616
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1617
  mod_item_fn = \
1618
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1619
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1620
      ht.Comment("Disk index, can be negative, e.g. -1 for last disk")(ht.TInt),
1621
      fn,
1622
      ]))
1623

    
1624
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1625
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1626

    
1627

    
1628
class OpInstanceSetParams(OpCode):
1629
  """Change the parameters of an instance.
1630

1631
  """
1632
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1633
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1634

    
1635
  OP_DSC_FIELD = "instance_name"
1636
  OP_PARAMS = [
1637
    _PInstanceName,
1638
    _PForce,
1639
    _PForceVariant,
1640
    _PIgnoreIpolicy,
1641
    ("nics", ht.EmptyList, TestNicModifications,
1642
     "List of NIC changes: each item is of the form ``(op, index, settings)``,"
1643
     " ``op`` is one of ``%s``, ``%s`` or ``%s``, ``index`` can be either -1"
1644
     " to refer to the last position, or a zero-based index number; a"
1645
     " deprecated version of this parameter used the form ``(op, settings)``,"
1646
     " where ``op`` can be ``%s`` to add a new NIC with the specified"
1647
     " settings, ``%s`` to remove the last NIC or a number to modify the"
1648
     " settings of the NIC with that index" %
1649
     (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1650
      constants.DDM_ADD, constants.DDM_REMOVE)),
1651
    ("disks", ht.EmptyList, TestDiskModifications,
1652
     "List of disk changes; see ``nics``"),
1653
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1654
    ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1655
    ("hvparams", ht.EmptyDict, ht.TDict,
1656
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1657
    ("disk_template", None, ht.TMaybe(_BuildDiskTemplateCheck(False)),
1658
     "Disk template for instance"),
1659
    ("remote_node", None, ht.TMaybeString,
1660
     "Secondary node (used when changing disk template)"),
1661
    ("os_name", None, ht.TMaybeString,
1662
     "Change the instance's OS without reinstalling the instance"),
1663
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1664
    ("wait_for_sync", True, ht.TBool,
1665
     "Whether to wait for the disk to synchronize, when changing template"),
1666
    ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1667
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1668
    ]
1669
  OP_RESULT = _TSetParamsResult
1670

    
1671

    
1672
class OpInstanceGrowDisk(OpCode):
1673
  """Grow a disk of an instance."""
1674
  OP_DSC_FIELD = "instance_name"
1675
  OP_PARAMS = [
1676
    _PInstanceName,
1677
    _PWaitForSync,
1678
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1679
    ("amount", ht.NoDefault, ht.TNonNegativeInt,
1680
     "Amount of disk space to add (megabytes)"),
1681
    ("absolute", False, ht.TBool,
1682
     "Whether the amount parameter is an absolute target or a relative one"),
1683
    ]
1684
  OP_RESULT = ht.TNone
1685

    
1686

    
1687
class OpInstanceChangeGroup(OpCode):
1688
  """Moves an instance to another node group."""
1689
  OP_DSC_FIELD = "instance_name"
1690
  OP_PARAMS = [
1691
    _PInstanceName,
1692
    _PEarlyRelease,
1693
    _PIAllocFromDesc("Iallocator for computing solution"),
1694
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1695
     "Destination group names or UUIDs (defaults to \"all but current group\""),
1696
    ]
1697
  OP_RESULT = TJobIdListOnly
1698

    
1699

    
1700
# Node group opcodes
1701

    
1702
class OpGroupAdd(OpCode):
1703
  """Add a node group to the cluster."""
1704
  OP_DSC_FIELD = "group_name"
1705
  OP_PARAMS = [
1706
    _PGroupName,
1707
    _PNodeGroupAllocPolicy,
1708
    _PGroupNodeParams,
1709
    _PDiskParams,
1710
    _PHvState,
1711
    _PDiskState,
1712
    ("ipolicy", None, ht.TMaybeDict,
1713
     "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1714
    ]
1715
  OP_RESULT = ht.TNone
1716

    
1717

    
1718
class OpGroupAssignNodes(OpCode):
1719
  """Assign nodes to a node group."""
1720
  OP_DSC_FIELD = "group_name"
1721
  OP_PARAMS = [
1722
    _PGroupName,
1723
    _PForce,
1724
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1725
     "List of nodes to assign"),
1726
    ]
1727
  OP_RESULT = ht.TNone
1728

    
1729

    
1730
class OpGroupQuery(OpCode):
1731
  """Compute the list of node groups."""
1732
  OP_PARAMS = [
1733
    _POutputFields,
1734
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1735
     "Empty list to query all groups, group names otherwise"),
1736
    ]
1737
  OP_RESULT = _TOldQueryResult
1738

    
1739

    
1740
class OpGroupSetParams(OpCode):
1741
  """Change the parameters of a node group."""
1742
  OP_DSC_FIELD = "group_name"
1743
  OP_PARAMS = [
1744
    _PGroupName,
1745
    _PNodeGroupAllocPolicy,
1746
    _PGroupNodeParams,
1747
    _PDiskParams,
1748
    _PHvState,
1749
    _PDiskState,
1750
    ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1751
    ]
1752
  OP_RESULT = _TSetParamsResult
1753

    
1754

    
1755
class OpGroupRemove(OpCode):
1756
  """Remove a node group from the cluster."""
1757
  OP_DSC_FIELD = "group_name"
1758
  OP_PARAMS = [
1759
    _PGroupName,
1760
    ]
1761
  OP_RESULT = ht.TNone
1762

    
1763

    
1764
class OpGroupRename(OpCode):
1765
  """Rename a node group in the cluster."""
1766
  OP_PARAMS = [
1767
    _PGroupName,
1768
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1769
    ]
1770
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1771

    
1772

    
1773
class OpGroupEvacuate(OpCode):
1774
  """Evacuate a node group in the cluster."""
1775
  OP_DSC_FIELD = "group_name"
1776
  OP_PARAMS = [
1777
    _PGroupName,
1778
    _PEarlyRelease,
1779
    _PIAllocFromDesc("Iallocator for computing solution"),
1780
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1781
     "Destination group names or UUIDs"),
1782
    ]
1783
  OP_RESULT = TJobIdListOnly
1784

    
1785

    
1786
# OS opcodes
1787
class OpOsDiagnose(OpCode):
1788
  """Compute the list of guest operating systems."""
1789
  OP_PARAMS = [
1790
    _POutputFields,
1791
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1792
     "Which operating systems to diagnose"),
1793
    ]
1794
  OP_RESULT = _TOldQueryResult
1795

    
1796

    
1797
# Exports opcodes
1798
class OpBackupQuery(OpCode):
1799
  """Compute the list of exported images."""
1800
  OP_PARAMS = [
1801
    _PUseLocking,
1802
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1803
     "Empty list to query all nodes, node names otherwise"),
1804
    ]
1805
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1806
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1807
                                ht.TListOf(ht.TNonEmptyString)))
1808

    
1809

    
1810
class OpBackupPrepare(OpCode):
1811
  """Prepares an instance export.
1812

1813
  @ivar instance_name: Instance name
1814
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1815

1816
  """
1817
  OP_DSC_FIELD = "instance_name"
1818
  OP_PARAMS = [
1819
    _PInstanceName,
1820
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1821
     "Export mode"),
1822
    ]
1823
  OP_RESULT = ht.TMaybeDict
1824

    
1825

    
1826
class OpBackupExport(OpCode):
1827
  """Export an instance.
1828

1829
  For local exports, the export destination is the node name. For
1830
  remote exports, the export destination is a list of tuples, each
1831
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1832
  salt. The HMAC is calculated using the cluster domain secret over
1833
  the value "${index}:${hostname}:${port}". The destination X509 CA
1834
  must be a signed certificate.
1835

1836
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1837
  @ivar target_node: Export destination
1838
  @ivar x509_key_name: X509 key to use (remote export only)
1839
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1840
                             only)
1841

1842
  """
1843
  OP_DSC_FIELD = "instance_name"
1844
  OP_PARAMS = [
1845
    _PInstanceName,
1846
    _PShutdownTimeout,
1847
    # TODO: Rename target_node as it changes meaning for different export modes
1848
    # (e.g. "destination")
1849
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1850
     "Destination information, depends on export mode"),
1851
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1852
    ("remove_instance", False, ht.TBool,
1853
     "Whether to remove instance after export"),
1854
    ("ignore_remove_failures", False, ht.TBool,
1855
     "Whether to ignore failures while removing instances"),
1856
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1857
     "Export mode"),
1858
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1859
     "Name of X509 key (remote export only)"),
1860
    ("destination_x509_ca", None, ht.TMaybeString,
1861
     "Destination X509 CA (remote export only)"),
1862
    ]
1863
  OP_RESULT = \
1864
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1865
      ht.Comment("Finalizing status")(ht.TBool),
1866
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1867
      ]))
1868

    
1869

    
1870
class OpBackupRemove(OpCode):
1871
  """Remove an instance's export."""
1872
  OP_DSC_FIELD = "instance_name"
1873
  OP_PARAMS = [
1874
    _PInstanceName,
1875
    ]
1876
  OP_RESULT = ht.TNone
1877

    
1878

    
1879
# Tags opcodes
1880
class OpTagsGet(OpCode):
1881
  """Returns the tags of the given object."""
1882
  OP_DSC_FIELD = "name"
1883
  OP_PARAMS = [
1884
    _PTagKind,
1885
    # Not using _PUseLocking as the default is different for historical reasons
1886
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1887
    # Name is only meaningful for nodes and instances
1888
    ("name", ht.NoDefault, ht.TMaybeString,
1889
     "Name of object to retrieve tags from"),
1890
    ]
1891
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1892

    
1893

    
1894
class OpTagsSearch(OpCode):
1895
  """Searches the tags in the cluster for a given pattern."""
1896
  OP_DSC_FIELD = "pattern"
1897
  OP_PARAMS = [
1898
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1899
     "Search pattern (regular expression)"),
1900
    ]
1901
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1902
    ht.TNonEmptyString,
1903
    ht.TNonEmptyString,
1904
    ])))
1905

    
1906

    
1907
class OpTagsSet(OpCode):
1908
  """Add a list of tags on a given object."""
1909
  OP_PARAMS = [
1910
    _PTagKind,
1911
    _PTags,
1912
    # Name is only meaningful for groups, nodes and instances
1913
    ("name", ht.NoDefault, ht.TMaybeString,
1914
     "Name of object where tag(s) should be added"),
1915
    ]
1916
  OP_RESULT = ht.TNone
1917

    
1918

    
1919
class OpTagsDel(OpCode):
1920
  """Remove a list of tags from a given object."""
1921
  OP_PARAMS = [
1922
    _PTagKind,
1923
    _PTags,
1924
    # Name is only meaningful for groups, nodes and instances
1925
    ("name", ht.NoDefault, ht.TMaybeString,
1926
     "Name of object where tag(s) should be deleted"),
1927
    ]
1928
  OP_RESULT = ht.TNone
1929

    
1930

    
1931
# Test opcodes
1932
class OpTestDelay(OpCode):
1933
  """Sleeps for a configured amount of time.
1934

1935
  This is used just for debugging and testing.
1936

1937
  Parameters:
1938
    - duration: the time to sleep
1939
    - on_master: if true, sleep on the master
1940
    - on_nodes: list of nodes in which to sleep
1941

1942
  If the on_master parameter is true, it will execute a sleep on the
1943
  master (before any node sleep).
1944

1945
  If the on_nodes list is not empty, it will sleep on those nodes
1946
  (after the sleep on the master, if that is enabled).
1947

1948
  As an additional feature, the case of duration < 0 will be reported
1949
  as an execution error, so this opcode can be used as a failure
1950
  generator. The case of duration == 0 will not be treated specially.
1951

1952
  """
1953
  OP_DSC_FIELD = "duration"
1954
  OP_PARAMS = [
1955
    ("duration", ht.NoDefault, ht.TNumber, None),
1956
    ("on_master", True, ht.TBool, None),
1957
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1958
    ("repeat", 0, ht.TNonNegativeInt, None),
1959
    ]
1960

    
1961

    
1962
class OpTestAllocator(OpCode):
1963
  """Allocator framework testing.
1964

1965
  This opcode has two modes:
1966
    - gather and return allocator input for a given mode (allocate new
1967
      or replace secondary) and a given instance definition (direction
1968
      'in')
1969
    - run a selected allocator for a given operation (as above) and
1970
      return the allocator output (direction 'out')
1971

1972
  """
1973
  OP_DSC_FIELD = "iallocator"
1974
  OP_PARAMS = [
1975
    ("direction", ht.NoDefault,
1976
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1977
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1978
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1979
    ("nics", ht.NoDefault,
1980
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
1981
                                            constants.INIC_IP,
1982
                                            "bridge"]),
1983
                                ht.TMaybeString)),
1984
     None),
1985
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
1986
    ("hypervisor", None, ht.TMaybeString, None),
1987
    _PIAllocFromDesc(None),
1988
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1989
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
1990
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
1991
    ("os", None, ht.TMaybeString, None),
1992
    ("disk_template", None, ht.TMaybeString, None),
1993
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1994
    ("evac_mode", None,
1995
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1996
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1997
    ("spindle_use", 1, ht.TNonNegativeInt, None),
1998
    ("count", 1, ht.TNonNegativeInt, None),
1999
    ]
2000

    
2001

    
2002
class OpTestJqueue(OpCode):
2003
  """Utility opcode to test some aspects of the job queue.
2004

2005
  """
2006
  OP_PARAMS = [
2007
    ("notify_waitlock", False, ht.TBool, None),
2008
    ("notify_exec", False, ht.TBool, None),
2009
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2010
    ("fail", False, ht.TBool, None),
2011
    ]
2012

    
2013

    
2014
class OpTestDummy(OpCode):
2015
  """Utility opcode used by unittests.
2016

2017
  """
2018
  OP_PARAMS = [
2019
    ("result", ht.NoDefault, ht.NoType, None),
2020
    ("messages", ht.NoDefault, ht.NoType, None),
2021
    ("fail", ht.NoDefault, ht.NoType, None),
2022
    ("submit_jobs", None, ht.NoType, None),
2023
    ]
2024
  WITH_LU = False
2025

    
2026

    
2027
# Network opcodes
2028
# Add a new network in the cluster
2029
class OpNetworkAdd(OpCode):
2030
  """Add an IP network to the cluster."""
2031
  OP_DSC_FIELD = "network_name"
2032
  OP_PARAMS = [
2033
    _PNetworkName,
2034
    ("network_type", None, ht.TMaybe(_CheckNetworkType), "Network type"),
2035
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2036
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2037
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2038
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2039
    ("mac_prefix", None, ht.TMaybeString,
2040
     "MAC address prefix that overrides cluster one"),
2041
    ("add_reserved_ips", None, _TMaybeAddr4List,
2042
     "Which IP addresses to reserve"),
2043
    ("conflicts_check", True, ht.TBool,
2044
     "Whether to check for conflicting IP addresses"),
2045
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2046
    ]
2047
  OP_RESULT = ht.TNone
2048

    
2049

    
2050
class OpNetworkRemove(OpCode):
2051
  """Remove an existing network from the cluster.
2052
     Must not be connected to any nodegroup.
2053

2054
  """
2055
  OP_DSC_FIELD = "network_name"
2056
  OP_PARAMS = [
2057
    _PNetworkName,
2058
    _PForce,
2059
    ]
2060
  OP_RESULT = ht.TNone
2061

    
2062

    
2063
class OpNetworkSetParams(OpCode):
2064
  """Modify Network's parameters except for IPv4 subnet"""
2065
  OP_DSC_FIELD = "network_name"
2066
  OP_PARAMS = [
2067
    _PNetworkName,
2068
    ("network_type", None, ht.TMaybeValueNone(_CheckNetworkType),
2069
     "Network type"),
2070
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2071
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2072
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2073
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2074
     "MAC address prefix that overrides cluster one"),
2075
    ("add_reserved_ips", None, _TMaybeAddr4List,
2076
     "Which external IP addresses to reserve"),
2077
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2078
     "Which external IP addresses to release"),
2079
    ]
2080
  OP_RESULT = ht.TNone
2081

    
2082

    
2083
class OpNetworkConnect(OpCode):
2084
  """Connect a Network to a specific Nodegroup with the defined netparams
2085
     (mode, link). Nics in this Network will inherit those params.
2086
     Produce errors if a NIC (that its not already assigned to a network)
2087
     has an IP that is contained in the Network this will produce error unless
2088
     --no-conflicts-check is passed.
2089

2090
  """
2091
  OP_DSC_FIELD = "network_name"
2092
  OP_PARAMS = [
2093
    _PGroupName,
2094
    _PNetworkName,
2095
    ("network_mode", ht.NoDefault, ht.TString, "Connectivity mode"),
2096
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2097
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2098
    ]
2099
  OP_RESULT = ht.TNone
2100

    
2101

    
2102
class OpNetworkDisconnect(OpCode):
2103
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2104
     present in the Network unless --no-conficts-check option is passed.
2105

2106
  """
2107
  OP_DSC_FIELD = "network_name"
2108
  OP_PARAMS = [
2109
    _PGroupName,
2110
    _PNetworkName,
2111
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2112
    ]
2113
  OP_RESULT = ht.TNone
2114

    
2115

    
2116
class OpNetworkQuery(OpCode):
2117
  """Compute the list of networks."""
2118
  OP_PARAMS = [
2119
    _POutputFields,
2120
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2121
     "Empty list to query all groups, group names otherwise"),
2122
    ]
2123
  OP_RESULT = _TOldQueryResult
2124

    
2125

    
2126
def _GetOpList():
2127
  """Returns list of all defined opcodes.
2128

2129
  Does not eliminate duplicates by C{OP_ID}.
2130

2131
  """
2132
  return [v for v in globals().values()
2133
          if (isinstance(v, type) and issubclass(v, OpCode) and
2134
              hasattr(v, "OP_ID") and v is not OpCode)]
2135

    
2136

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