Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ d76880d8

History | View | Annotate | Download (69 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
#: 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 node UUID (for use with _PNodeName)
73
_PNodeUuid = ("node_uuid", None, ht.TMaybeString, "Node UUID")
74

    
75
#: a required node group name (for single-group LUs)
76
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
77

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

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

    
87
#: Tag type
88
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
89
             "Tag kind")
90

    
91
#: List of tag strings
92
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
93
          "List of tag names")
94

    
95
_PForceVariant = ("force_variant", False, ht.TBool,
96
                  "Whether to force an unknown OS variant")
97

    
98
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
99
                 "Whether to wait for the disk to synchronize")
100

    
101
_PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
102
                      "Whether to wait for the disk to synchronize"
103
                      " (defaults to false)")
104

    
105
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
106
                       "Whether to ignore disk consistency")
107

    
108
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
109

    
110
_PUseLocking = ("use_locking", False, ht.TBool,
111
                "Whether to use synchronization")
112

    
113
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
114

    
115
_PNodeGroupAllocPolicy = \
116
  ("alloc_policy", None,
117
   ht.TMaybe(ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
118
   "Instance allocation policy")
119

    
120
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
121
                     "Default node parameters for group")
122

    
123
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
124
               "Resource(s) to query for")
125

    
126
_PEarlyRelease = ("early_release", False, ht.TBool,
127
                  "Whether to release locks as soon as possible")
128

    
129
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
130

    
131
#: Do not remember instance state changes
132
_PNoRemember = ("no_remember", False, ht.TBool,
133
                "Do not remember the state change")
134

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

    
139
_PMigrationTargetNodeUuid = ("target_node_uuid", None, ht.TMaybeString,
140
                             "Target node UUID for shared-storage instances")
141

    
142
_PStartupPaused = ("startup_paused", False, ht.TBool,
143
                   "Pause instance at startup")
144

    
145
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
146

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

    
158
# Disk parameters
159
_PDiskParams = \
160
  ("diskparams", None,
161
   ht.TMaybe(ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict)),
162
   "Disk templates' parameter defaults")
163

    
164
# Parameters for node resource model
165
_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
166
_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
167

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

    
175
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
176
                   "Whether to ignore ipolicy violations")
177

    
178
# Allow runtime changes while migrating
179
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
180
                      "Allow runtime changes (eg. memory ballooning)")
181

    
182
#: IAllocator field builder
183
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
184

    
185
#: a required network name
186
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
187
                 "Set network name")
188

    
189
_PTargetGroups = \
190
  ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
191
   "Destination group names or UUIDs (defaults to \"all but current group\")")
192

    
193
#: OP_ID conversion regular expression
194
_OPID_RE = re.compile("([a-z])([A-Z])")
195

    
196
#: Utility function for L{OpClusterSetParams}
197
_TestClusterOsListItem = \
198
  ht.TAnd(ht.TIsLength(2), ht.TItems([
199
    ht.TElemOf(constants.DDMS_VALUES),
200
    ht.TNonEmptyString,
201
    ]))
202

    
203
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
204

    
205
# TODO: Generate check from constants.INIC_PARAMS_TYPES
206
#: Utility function for testing NIC definitions
207
_TestNicDef = \
208
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
209
                                          ht.TMaybeString))
210

    
211
_TSetParamsResultItemItems = [
212
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
213
  ht.Comment("new value")(ht.TAny),
214
  ]
215

    
216
_TSetParamsResult = \
217
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
218
                     ht.TItems(_TSetParamsResultItemItems)))
219

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

    
228
_TQueryRow = \
229
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
230
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
231
                                ht.TAny])))
232

    
233
_TQueryResult = ht.TListOf(_TQueryRow)
234

    
235
_TOldQueryRow = ht.TListOf(ht.TAny)
236

    
237
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
238

    
239

    
240
_SUMMARY_PREFIX = {
241
  "CLUSTER_": "C_",
242
  "GROUP_": "G_",
243
  "NODE_": "N_",
244
  "INSTANCE_": "I_",
245
  }
246

    
247
#: Attribute name for dependencies
248
DEPEND_ATTR = "depends"
249

    
250
#: Attribute name for comment
251
COMMENT_ATTR = "comment"
252

    
253

    
254
def _NameComponents(name):
255
  """Split an opcode class name into its components
256

257
  @type name: string
258
  @param name: the class name, as OpXxxYyy
259
  @rtype: array of strings
260
  @return: the components of the name
261

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

    
273

    
274
def _NameToId(name):
275
  """Convert an opcode class name to an OP_ID.
276

277
  @type name: string
278
  @param name: the class name, as OpXxxYyy
279
  @rtype: string
280
  @return: the name in the OP_XXXX_YYYY format
281

282
  """
283
  if not name.startswith("Op"):
284
    return None
285
  return "_".join(n.upper() for n in _NameComponents(name))
286

    
287

    
288
def NameToReasonSrc(name):
289
  """Convert an opcode class name to a source string for the reason trail
290

291
  @type name: string
292
  @param name: the class name, as OpXxxYyy
293
  @rtype: string
294
  @return: the name in the OP_XXXX_YYYY format
295

296
  """
297
  if not name.startswith("Op"):
298
    return None
299
  return "%s:%s" % (constants.OPCODE_REASON_SRC_OPCODE,
300
                    "_".join(n.lower() for n in _NameComponents(name)))
301

    
302

    
303
def _GenerateObjectTypeCheck(obj, fields_types):
304
  """Helper to generate type checks for objects.
305

306
  @param obj: The object to generate type checks
307
  @param fields_types: The fields and their types as a dict
308
  @return: A ht type check function
309

310
  """
311
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
312
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
313
  return ht.TStrictDict(True, True, fields_types)
314

    
315

    
316
_TQueryFieldDef = \
317
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
318
    "name": ht.TNonEmptyString,
319
    "title": ht.TNonEmptyString,
320
    "kind": ht.TElemOf(constants.QFT_ALL),
321
    "doc": ht.TNonEmptyString,
322
    })
323

    
324

    
325
def RequireFileStorage():
326
  """Checks that file storage is enabled.
327

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

331
  @raise errors.OpPrereqError: when file storage is disabled
332

333
  """
334
  if not constants.ENABLE_FILE_STORAGE:
335
    raise errors.OpPrereqError("File storage disabled at configure time",
336
                               errors.ECODE_INVAL)
337

    
338

    
339
def RequireSharedFileStorage():
340
  """Checks that shared file storage is enabled.
341

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

345
  @raise errors.OpPrereqError: when shared file storage is disabled
346

347
  """
348
  if not constants.ENABLE_SHARED_FILE_STORAGE:
349
    raise errors.OpPrereqError("Shared file storage disabled at"
350
                               " configure time", errors.ECODE_INVAL)
351

    
352

    
353
@ht.WithDesc("CheckFileStorage")
354
def _CheckFileStorage(value):
355
  """Ensures file storage is enabled if used.
356

357
  """
358
  if value == constants.DT_FILE:
359
    RequireFileStorage()
360
  elif value == constants.DT_SHARED_FILE:
361
    RequireSharedFileStorage()
362
  return True
363

    
364

    
365
def _BuildDiskTemplateCheck(accept_none):
366
  """Builds check for disk template.
367

368
  @type accept_none: bool
369
  @param accept_none: whether to accept None as a correct value
370
  @rtype: callable
371

372
  """
373
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
374

    
375
  if accept_none:
376
    template_check = ht.TMaybe(template_check)
377

    
378
  return ht.TAnd(template_check, _CheckFileStorage)
379

    
380

    
381
def _CheckStorageType(storage_type):
382
  """Ensure a given storage type is valid.
383

384
  """
385
  if storage_type not in constants.VALID_STORAGE_TYPES:
386
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
387
                               errors.ECODE_INVAL)
388
  if storage_type == constants.ST_FILE:
389
    # TODO: What about shared file storage?
390
    RequireFileStorage()
391
  return True
392

    
393

    
394
#: Storage type parameter
395
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
396
                 "Storage type")
397

    
398

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

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

    
410

    
411
@ht.WithDesc("IPv4 address")
412
def _CheckCIDRAddrNotation(value):
413
  """Ensure a given CIDR notation type is valid.
414

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

    
422

    
423
@ht.WithDesc("IPv6 address")
424
def _CheckCIDR6AddrNotation(value):
425
  """Ensure a given CIDR notation type is valid.
426

427
  """
428
  try:
429
    ipaddr.IPv6Address(value)
430
  except ipaddr.AddressValueError:
431
    return False
432
  return True
433

    
434

    
435
@ht.WithDesc("IPv6 network")
436
def _CheckCIDR6NetNotation(value):
437
  """Ensure a given CIDR notation type is valid.
438

439
  """
440
  try:
441
    ipaddr.IPv6Network(value)
442
  except ipaddr.AddressValueError:
443
    return False
444
  return True
445

    
446

    
447
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
448
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
449
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
450
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
451
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
452

    
453

    
454
class _AutoOpParamSlots(outils.AutoSlots):
455
  """Meta class for opcode definitions.
456

457
  """
458
  def __new__(mcs, name, bases, attrs):
459
    """Called when a class should be created.
460

461
    @param mcs: The meta class
462
    @param name: Name of created class
463
    @param bases: Base classes
464
    @type attrs: dict
465
    @param attrs: Class attributes
466

467
    """
468
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
469

    
470
    slots = mcs._GetSlots(attrs)
471
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
472
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
473
    assert ("OP_DSC_FORMATTER" not in attrs or
474
            callable(attrs["OP_DSC_FORMATTER"])), \
475
      ("Class '%s' uses non-callable in OP_DSC_FORMATTER (%s)" %
476
       (name, type(attrs["OP_DSC_FORMATTER"])))
477

    
478
    attrs["OP_ID"] = _NameToId(name)
479

    
480
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
481

    
482
  @classmethod
483
  def _GetSlots(mcs, attrs):
484
    """Build the slots out of OP_PARAMS.
485

486
    """
487
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
488
    params = attrs.setdefault("OP_PARAMS", [])
489

    
490
    # Use parameter names as slots
491
    return [pname for (pname, _, _, _) in params]
492

    
493

    
494
class BaseOpCode(outils.ValidatedSlots):
495
  """A simple serializable object.
496

497
  This object serves as a parent class for OpCode without any custom
498
  field handling.
499

500
  """
501
  # pylint: disable=E1101
502
  # as OP_ID is dynamically defined
503
  __metaclass__ = _AutoOpParamSlots
504

    
505
  def __getstate__(self):
506
    """Generic serializer.
507

508
    This method just returns the contents of the instance as a
509
    dictionary.
510

511
    @rtype:  C{dict}
512
    @return: the instance attributes and their values
513

514
    """
515
    state = {}
516
    for name in self.GetAllSlots():
517
      if hasattr(self, name):
518
        state[name] = getattr(self, name)
519
    return state
520

    
521
  def __setstate__(self, state):
522
    """Generic unserializer.
523

524
    This method just restores from the serialized state the attributes
525
    of the current instance.
526

527
    @param state: the serialized opcode data
528
    @type state:  C{dict}
529

530
    """
531
    if not isinstance(state, dict):
532
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
533
                       type(state))
534

    
535
    for name in self.GetAllSlots():
536
      if name not in state and hasattr(self, name):
537
        delattr(self, name)
538

    
539
    for name in state:
540
      setattr(self, name, state[name])
541

    
542
  @classmethod
543
  def GetAllParams(cls):
544
    """Compute list of all parameters for an opcode.
545

546
    """
547
    slots = []
548
    for parent in cls.__mro__:
549
      slots.extend(getattr(parent, "OP_PARAMS", []))
550
    return slots
551

    
552
  def Validate(self, set_defaults): # pylint: disable=W0221
553
    """Validate opcode parameters, optionally setting default values.
554

555
    @type set_defaults: bool
556
    @param set_defaults: Whether to set default values
557
    @raise errors.OpPrereqError: When a parameter value doesn't match
558
                                 requirements
559

560
    """
561
    for (attr_name, default, test, _) in self.GetAllParams():
562
      assert test == ht.NoType or callable(test)
563

    
564
      if not hasattr(self, attr_name):
565
        if default == ht.NoDefault:
566
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
567
                                     (self.OP_ID, attr_name),
568
                                     errors.ECODE_INVAL)
569
        elif set_defaults:
570
          if callable(default):
571
            dval = default()
572
          else:
573
            dval = default
574
          setattr(self, attr_name, dval)
575

    
576
      if test == ht.NoType:
577
        # no tests here
578
        continue
579

    
580
      if set_defaults or hasattr(self, attr_name):
581
        attr_val = getattr(self, attr_name)
582
        if not test(attr_val):
583
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
584
                        " '%s' expecting type %s",
585
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
586
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
587
                                     (self.OP_ID, attr_name),
588
                                     errors.ECODE_INVAL)
589

    
590

    
591
def _BuildJobDepCheck(relative):
592
  """Builds check for job dependencies (L{DEPEND_ATTR}).
593

594
  @type relative: bool
595
  @param relative: Whether to accept relative job IDs (negative)
596
  @rtype: callable
597

598
  """
599
  if relative:
600
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
601
  else:
602
    job_id = ht.TJobId
603

    
604
  job_dep = \
605
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
606
            ht.TIsLength(2),
607
            ht.TItems([job_id,
608
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
609

    
610
  return ht.TMaybeListOf(job_dep)
611

    
612

    
613
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
614

    
615
#: List of submission status and job ID as returned by C{SubmitManyJobs}
616
_TJobIdListItem = \
617
  ht.TAnd(ht.TIsLength(2),
618
          ht.TItems([ht.Comment("success")(ht.TBool),
619
                     ht.Comment("Job ID if successful, error message"
620
                                " otherwise")(ht.TOr(ht.TString,
621
                                                     ht.TJobId))]))
622
TJobIdList = ht.TListOf(_TJobIdListItem)
623

    
624
#: Result containing only list of submitted jobs
625
TJobIdListOnly = ht.TStrictDict(True, True, {
626
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
627
  })
628

    
629

    
630
class OpCode(BaseOpCode):
631
  """Abstract OpCode.
632

633
  This is the root of the actual OpCode hierarchy. All clases derived
634
  from this class should override OP_ID.
635

636
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
637
               children of this class.
638
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
639
                      string returned by Summary(); see the docstring of that
640
                      method for details).
641
  @cvar OP_DSC_FORMATTER: A callable that should format the OP_DSC_FIELD; if
642
                          not present, then the field will be simply converted
643
                          to string
644
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
645
                   get if not already defined, and types they must match.
646
  @cvar OP_RESULT: Callable to verify opcode result
647
  @cvar WITH_LU: Boolean that specifies whether this should be included in
648
      mcpu's dispatch table
649
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
650
                 the check steps
651
  @ivar priority: Opcode priority for queue
652

653
  """
654
  # pylint: disable=E1101
655
  # as OP_ID is dynamically defined
656
  WITH_LU = True
657
  OP_PARAMS = [
658
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
659
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
660
    ("priority", constants.OP_PRIO_DEFAULT,
661
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
662
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
663
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
664
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
665
     " for details"),
666
    (COMMENT_ATTR, None, ht.TMaybeString,
667
     "Comment describing the purpose of the opcode"),
668
    (constants.OPCODE_REASON, None, ht.TMaybeList,
669
     "The reason trail, describing why the OpCode is executed"),
670
    ]
671
  OP_RESULT = None
672

    
673
  def __getstate__(self):
674
    """Specialized getstate for opcodes.
675

676
    This method adds to the state dictionary the OP_ID of the class,
677
    so that on unload we can identify the correct class for
678
    instantiating the opcode.
679

680
    @rtype:   C{dict}
681
    @return:  the state as a dictionary
682

683
    """
684
    data = BaseOpCode.__getstate__(self)
685
    data["OP_ID"] = self.OP_ID
686
    return data
687

    
688
  @classmethod
689
  def LoadOpCode(cls, data):
690
    """Generic load opcode method.
691

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

696
    @type data:  C{dict}
697
    @param data: the serialized opcode
698

699
    """
700
    if not isinstance(data, dict):
701
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
702
    if "OP_ID" not in data:
703
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
704
    op_id = data["OP_ID"]
705
    op_class = None
706
    if op_id in OP_MAPPING:
707
      op_class = OP_MAPPING[op_id]
708
    else:
709
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
710
                       op_id)
711
    op = op_class()
712
    new_data = data.copy()
713
    del new_data["OP_ID"]
714
    op.__setstate__(new_data)
715
    return op
716

    
717
  def Summary(self):
718
    """Generates a summary description of this opcode.
719

720
    The summary is the value of the OP_ID attribute (without the "OP_"
721
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
722
    defined; this field should allow to easily identify the operation
723
    (for an instance creation job, e.g., it would be the instance
724
    name).
725

726
    """
727
    assert self.OP_ID is not None and len(self.OP_ID) > 3
728
    # all OP_ID start with OP_, we remove that
729
    txt = self.OP_ID[3:]
730
    field_name = getattr(self, "OP_DSC_FIELD", None)
731
    if field_name:
732
      field_value = getattr(self, field_name, None)
733
      field_formatter = getattr(self, "OP_DSC_FORMATTER", None)
734
      if callable(field_formatter):
735
        field_value = field_formatter(field_value)
736
      elif isinstance(field_value, (list, tuple)):
737
        field_value = ",".join(str(i) for i in field_value)
738
      txt = "%s(%s)" % (txt, field_value)
739
    return txt
740

    
741
  def TinySummary(self):
742
    """Generates a compact summary description of the opcode.
743

744
    """
745
    assert self.OP_ID.startswith("OP_")
746

    
747
    text = self.OP_ID[3:]
748

    
749
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
750
      if text.startswith(prefix):
751
        return supplement + text[len(prefix):]
752

    
753
    return text
754

    
755

    
756
# cluster opcodes
757

    
758
class OpClusterPostInit(OpCode):
759
  """Post cluster initialization.
760

761
  This opcode does not touch the cluster at all. Its purpose is to run hooks
762
  after the cluster has been initialized.
763

764
  """
765
  OP_RESULT = ht.TBool
766

    
767

    
768
class OpClusterDestroy(OpCode):
769
  """Destroy the cluster.
770

771
  This opcode has no other parameters. All the state is irreversibly
772
  lost after the execution of this opcode.
773

774
  """
775
  OP_RESULT = ht.TNonEmptyString
776

    
777

    
778
class OpClusterQuery(OpCode):
779
  """Query cluster information."""
780
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
781

    
782

    
783
class OpClusterVerify(OpCode):
784
  """Submits all jobs necessary to verify the cluster.
785

786
  """
787
  OP_PARAMS = [
788
    _PDebugSimulateErrors,
789
    _PErrorCodes,
790
    _PSkipChecks,
791
    _PIgnoreErrors,
792
    _PVerbose,
793
    ("group_name", None, ht.TMaybeString, "Group to verify"),
794
    ]
795
  OP_RESULT = TJobIdListOnly
796

    
797

    
798
class OpClusterVerifyConfig(OpCode):
799
  """Verify the cluster config.
800

801
  """
802
  OP_PARAMS = [
803
    _PDebugSimulateErrors,
804
    _PErrorCodes,
805
    _PIgnoreErrors,
806
    _PVerbose,
807
    ]
808
  OP_RESULT = ht.TBool
809

    
810

    
811
class OpClusterVerifyGroup(OpCode):
812
  """Run verify on a node group from the cluster.
813

814
  @type skip_checks: C{list}
815
  @ivar skip_checks: steps to be skipped from the verify process; this
816
                     needs to be a subset of
817
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
818
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
819

820
  """
821
  OP_DSC_FIELD = "group_name"
822
  OP_PARAMS = [
823
    _PGroupName,
824
    _PDebugSimulateErrors,
825
    _PErrorCodes,
826
    _PSkipChecks,
827
    _PIgnoreErrors,
828
    _PVerbose,
829
    ]
830
  OP_RESULT = ht.TBool
831

    
832

    
833
class OpClusterVerifyDisks(OpCode):
834
  """Verify the cluster disks.
835

836
  """
837
  OP_RESULT = TJobIdListOnly
838

    
839

    
840
class OpGroupVerifyDisks(OpCode):
841
  """Verifies the status of all disks in a node group.
842

843
  Result: a tuple of three elements:
844
    - dict of node names with issues (values: error msg)
845
    - list of instances with degraded disks (that should be activated)
846
    - dict of instances with missing logical volumes (values: (node, vol)
847
      pairs with details about the missing volumes)
848

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

854
  Note that only instances that are drbd-based are taken into
855
  consideration. This might need to be revisited in the future.
856

857
  """
858
  OP_DSC_FIELD = "group_name"
859
  OP_PARAMS = [
860
    _PGroupName,
861
    ]
862
  OP_RESULT = \
863
    ht.TAnd(ht.TIsLength(3),
864
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
865
                       ht.TListOf(ht.TString),
866
                       ht.TDictOf(ht.TString,
867
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
868

    
869

    
870
class OpClusterRepairDiskSizes(OpCode):
871
  """Verify the disk sizes of the instances and fixes configuration
872
  mimatches.
873

874
  Parameters: optional instances list, in case we want to restrict the
875
  checks to only a subset of the instances.
876

877
  Result: a list of tuples, (instance, disk, parameter, new-size) for changed
878
  configurations.
879

880
  In normal operation, the list should be empty.
881

882
  @type instances: list
883
  @ivar instances: the list of instances to check, or empty for all instances
884

885
  """
886
  OP_PARAMS = [
887
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
888
    ]
889
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(4),
890
                                 ht.TItems([ht.TNonEmptyString,
891
                                            ht.TNonNegativeInt,
892
                                            ht.TNonEmptyString,
893
                                            ht.TNonNegativeInt])))
894

    
895

    
896
class OpClusterConfigQuery(OpCode):
897
  """Query cluster configuration values."""
898
  OP_PARAMS = [
899
    _POutputFields,
900
    ]
901
  OP_RESULT = ht.TListOf(ht.TAny)
902

    
903

    
904
class OpClusterRename(OpCode):
905
  """Rename the cluster.
906

907
  @type name: C{str}
908
  @ivar name: The new name of the cluster. The name and/or the master IP
909
              address will be changed to match the new name and its IP
910
              address.
911

912
  """
913
  OP_DSC_FIELD = "name"
914
  OP_PARAMS = [
915
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
916
    ]
917
  OP_RESULT = ht.TNonEmptyString
918

    
919

    
920
class OpClusterSetParams(OpCode):
921
  """Change the parameters of the cluster.
922

923
  @type vg_name: C{str} or C{None}
924
  @ivar vg_name: The new volume group name or None to disable LVM usage.
925

926
  """
927
  OP_PARAMS = [
928
    _PForce,
929
    _PHvState,
930
    _PDiskState,
931
    ("vg_name", None, ht.TMaybe(ht.TString), "Volume group name"),
932
    ("enabled_hypervisors", None,
933
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)),
934
                       ht.TTrue)),
935
     "List of enabled hypervisors"),
936
    ("hvparams", None,
937
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
938
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
939
    ("beparams", None, ht.TMaybeDict,
940
     "Cluster-wide backend parameter defaults"),
941
    ("os_hvp", None, ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
942
     "Cluster-wide per-OS hypervisor parameter defaults"),
943
    ("osparams", None,
944
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
945
     "Cluster-wide OS parameter defaults"),
946
    _PDiskParams,
947
    ("candidate_pool_size", None, ht.TMaybe(ht.TPositiveInt),
948
     "Master candidate pool size"),
949
    ("uid_pool", None, ht.NoType,
950
     "Set UID pool, must be list of lists describing UID ranges (two items,"
951
     " start and end inclusive)"),
952
    ("add_uids", None, ht.NoType,
953
     "Extend UID pool, must be list of lists describing UID ranges (two"
954
     " items, start and end inclusive) to be added"),
955
    ("remove_uids", None, ht.NoType,
956
     "Shrink UID pool, must be list of lists describing UID ranges (two"
957
     " items, start and end inclusive) to be removed"),
958
    ("maintain_node_health", None, ht.TMaybeBool,
959
     "Whether to automatically maintain node health"),
960
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
961
     "Whether to wipe disks before allocating them to instances"),
962
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
963
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
964
    ("ipolicy", None, ht.TMaybeDict,
965
     "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
966
    ("drbd_helper", None, ht.TMaybe(ht.TString), "DRBD helper program"),
967
    ("default_iallocator", None, ht.TMaybe(ht.TString),
968
     "Default iallocator for cluster"),
969
    ("master_netdev", None, ht.TMaybe(ht.TString),
970
     "Master network device"),
971
    ("master_netmask", None, ht.TMaybe(ht.TNonNegativeInt),
972
     "Netmask of the master IP"),
973
    ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
974
     "List of reserved LVs"),
975
    ("hidden_os", None, _TestClusterOsList,
976
     "Modify list of hidden operating systems: each modification must have"
977
     " two items, the operation and the OS name; the operation can be"
978
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
979
    ("blacklisted_os", None, _TestClusterOsList,
980
     "Modify list of blacklisted operating systems: each modification must"
981
     " have two items, the operation and the OS name; the operation can be"
982
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
983
    ("use_external_mip_script", None, ht.TMaybeBool,
984
     "Whether to use an external master IP address setup script"),
985
    ("enabled_disk_templates", None,
986
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.DISK_TEMPLATES)),
987
                       ht.TTrue)),
988
     "List of enabled disk templates"),
989
    ]
990
  OP_RESULT = ht.TNone
991

    
992

    
993
class OpClusterRedistConf(OpCode):
994
  """Force a full push of the cluster configuration.
995

996
  """
997
  OP_RESULT = ht.TNone
998

    
999

    
1000
class OpClusterActivateMasterIp(OpCode):
1001
  """Activate the master IP on the master node.
1002

1003
  """
1004
  OP_RESULT = ht.TNone
1005

    
1006

    
1007
class OpClusterDeactivateMasterIp(OpCode):
1008
  """Deactivate the master IP on the master node.
1009

1010
  """
1011
  OP_RESULT = ht.TNone
1012

    
1013

    
1014
class OpQuery(OpCode):
1015
  """Query for resources/items.
1016

1017
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
1018
  @ivar fields: List of fields to retrieve
1019
  @ivar qfilter: Query filter
1020

1021
  """
1022
  OP_DSC_FIELD = "what"
1023
  OP_PARAMS = [
1024
    _PQueryWhat,
1025
    _PUseLocking,
1026
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1027
     "Requested fields"),
1028
    ("qfilter", None, ht.TMaybe(ht.TList),
1029
     "Query filter"),
1030
    ]
1031
  OP_RESULT = \
1032
    _GenerateObjectTypeCheck(objects.QueryResponse, {
1033
      "fields": ht.TListOf(_TQueryFieldDef),
1034
      "data": _TQueryResult,
1035
      })
1036

    
1037

    
1038
class OpQueryFields(OpCode):
1039
  """Query for available resource/item fields.
1040

1041
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
1042
  @ivar fields: List of fields to retrieve
1043

1044
  """
1045
  OP_DSC_FIELD = "what"
1046
  OP_PARAMS = [
1047
    _PQueryWhat,
1048
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
1049
     "Requested fields; if not given, all are returned"),
1050
    ]
1051
  OP_RESULT = \
1052
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
1053
      "fields": ht.TListOf(_TQueryFieldDef),
1054
      })
1055

    
1056

    
1057
class OpOobCommand(OpCode):
1058
  """Interact with OOB."""
1059
  OP_PARAMS = [
1060
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1061
     "List of node names to run the OOB command against"),
1062
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1063
     "List of node UUIDs to run the OOB command against"),
1064
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1065
     "OOB command to be run"),
1066
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1067
     "Timeout before the OOB helper will be terminated"),
1068
    ("ignore_status", False, ht.TBool,
1069
     "Ignores the node offline status for power off"),
1070
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1071
     "Time in seconds to wait between powering on nodes"),
1072
    ]
1073
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1074
  OP_RESULT = _TQueryResult
1075

    
1076

    
1077
class OpRestrictedCommand(OpCode):
1078
  """Runs a restricted command on node(s).
1079

1080
  """
1081
  OP_PARAMS = [
1082
    _PUseLocking,
1083
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1084
     "Nodes on which the command should be run (at least one)"),
1085
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1086
     "Node UUIDs on which the command should be run (at least one)"),
1087
    ("command", ht.NoDefault, ht.TNonEmptyString,
1088
     "Command name (no parameters)"),
1089
    ]
1090

    
1091
  _RESULT_ITEMS = [
1092
    ht.Comment("success")(ht.TBool),
1093
    ht.Comment("output or error message")(ht.TString),
1094
    ]
1095

    
1096
  OP_RESULT = \
1097
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1098
                       ht.TItems(_RESULT_ITEMS)))
1099

    
1100

    
1101
# node opcodes
1102

    
1103
class OpNodeRemove(OpCode):
1104
  """Remove a node.
1105

1106
  @type node_name: C{str}
1107
  @ivar node_name: The name of the node to remove. If the node still has
1108
                   instances on it, the operation will fail.
1109

1110
  """
1111
  OP_DSC_FIELD = "node_name"
1112
  OP_PARAMS = [
1113
    _PNodeName,
1114
    _PNodeUuid
1115
    ]
1116
  OP_RESULT = ht.TNone
1117

    
1118

    
1119
class OpNodeAdd(OpCode):
1120
  """Add a node to the cluster.
1121

1122
  @type node_name: C{str}
1123
  @ivar node_name: The name of the node to add. This can be a short name,
1124
                   but it will be expanded to the FQDN.
1125
  @type primary_ip: IP address
1126
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1127
                    opcode is submitted, but will be filled during the node
1128
                    add (so it will be visible in the job query).
1129
  @type secondary_ip: IP address
1130
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1131
                      if the cluster has been initialized in 'dual-network'
1132
                      mode, otherwise it must not be given.
1133
  @type readd: C{bool}
1134
  @ivar readd: Whether to re-add an existing node to the cluster. If
1135
               this is not passed, then the operation will abort if the node
1136
               name is already in the cluster; use this parameter to 'repair'
1137
               a node that had its configuration broken, or was reinstalled
1138
               without removal from the cluster.
1139
  @type group: C{str}
1140
  @ivar group: The node group to which this node will belong.
1141
  @type vm_capable: C{bool}
1142
  @ivar vm_capable: The vm_capable node attribute
1143
  @type master_capable: C{bool}
1144
  @ivar master_capable: The master_capable node attribute
1145

1146
  """
1147
  OP_DSC_FIELD = "node_name"
1148
  OP_PARAMS = [
1149
    _PNodeName,
1150
    _PHvState,
1151
    _PDiskState,
1152
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1153
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1154
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1155
    ("group", None, ht.TMaybeString, "Initial node group"),
1156
    ("master_capable", None, ht.TMaybeBool,
1157
     "Whether node can become master or master candidate"),
1158
    ("vm_capable", None, ht.TMaybeBool,
1159
     "Whether node can host instances"),
1160
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1161
    ]
1162
  OP_RESULT = ht.TNone
1163

    
1164

    
1165
class OpNodeQuery(OpCode):
1166
  """Compute the list of nodes."""
1167
  OP_PARAMS = [
1168
    _POutputFields,
1169
    _PUseLocking,
1170
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1171
     "Empty list to query all nodes, node names otherwise"),
1172
    ]
1173
  OP_RESULT = _TOldQueryResult
1174

    
1175

    
1176
class OpNodeQueryvols(OpCode):
1177
  """Get list of volumes on node."""
1178
  OP_PARAMS = [
1179
    _POutputFields,
1180
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1181
     "Empty list to query all nodes, node names otherwise"),
1182
    ]
1183
  OP_RESULT = ht.TListOf(ht.TAny)
1184

    
1185

    
1186
class OpNodeQueryStorage(OpCode):
1187
  """Get information on storage for node(s)."""
1188
  OP_PARAMS = [
1189
    _POutputFields,
1190
    _PStorageType,
1191
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1192
    ("name", None, ht.TMaybeString, "Storage name"),
1193
    ]
1194
  OP_RESULT = _TOldQueryResult
1195

    
1196

    
1197
class OpNodeModifyStorage(OpCode):
1198
  """Modifies the properies of a storage unit"""
1199
  OP_DSC_FIELD = "node_name"
1200
  OP_PARAMS = [
1201
    _PNodeName,
1202
    _PNodeUuid,
1203
    _PStorageType,
1204
    _PStorageName,
1205
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1206
    ]
1207
  OP_RESULT = ht.TNone
1208

    
1209

    
1210
class OpRepairNodeStorage(OpCode):
1211
  """Repairs the volume group on a node."""
1212
  OP_DSC_FIELD = "node_name"
1213
  OP_PARAMS = [
1214
    _PNodeName,
1215
    _PNodeUuid,
1216
    _PStorageType,
1217
    _PStorageName,
1218
    _PIgnoreConsistency,
1219
    ]
1220
  OP_RESULT = ht.TNone
1221

    
1222

    
1223
class OpNodeSetParams(OpCode):
1224
  """Change the parameters of a node."""
1225
  OP_DSC_FIELD = "node_name"
1226
  OP_PARAMS = [
1227
    _PNodeName,
1228
    _PNodeUuid,
1229
    _PForce,
1230
    _PHvState,
1231
    _PDiskState,
1232
    ("master_candidate", None, ht.TMaybeBool,
1233
     "Whether the node should become a master candidate"),
1234
    ("offline", None, ht.TMaybeBool,
1235
     "Whether the node should be marked as offline"),
1236
    ("drained", None, ht.TMaybeBool,
1237
     "Whether the node should be marked as drained"),
1238
    ("auto_promote", False, ht.TBool,
1239
     "Whether node(s) should be promoted to master candidate if necessary"),
1240
    ("master_capable", None, ht.TMaybeBool,
1241
     "Denote whether node can become master or master candidate"),
1242
    ("vm_capable", None, ht.TMaybeBool,
1243
     "Denote whether node can host instances"),
1244
    ("secondary_ip", None, ht.TMaybeString,
1245
     "Change node's secondary IP address"),
1246
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1247
    ("powered", None, ht.TMaybeBool,
1248
     "Whether the node should be marked as powered"),
1249
    ]
1250
  OP_RESULT = _TSetParamsResult
1251

    
1252

    
1253
class OpNodePowercycle(OpCode):
1254
  """Tries to powercycle a node."""
1255
  OP_DSC_FIELD = "node_name"
1256
  OP_PARAMS = [
1257
    _PNodeName,
1258
    _PNodeUuid,
1259
    _PForce,
1260
    ]
1261
  OP_RESULT = ht.TMaybeString
1262

    
1263

    
1264
class OpNodeMigrate(OpCode):
1265
  """Migrate all instances from a node."""
1266
  OP_DSC_FIELD = "node_name"
1267
  OP_PARAMS = [
1268
    _PNodeName,
1269
    _PNodeUuid,
1270
    _PMigrationMode,
1271
    _PMigrationLive,
1272
    _PMigrationTargetNode,
1273
    _PMigrationTargetNodeUuid,
1274
    _PAllowRuntimeChgs,
1275
    _PIgnoreIpolicy,
1276
    _PIAllocFromDesc("Iallocator for deciding the target node"
1277
                     " for shared-storage instances"),
1278
    ]
1279
  OP_RESULT = TJobIdListOnly
1280

    
1281

    
1282
class OpNodeEvacuate(OpCode):
1283
  """Evacuate instances off a number of nodes."""
1284
  OP_DSC_FIELD = "node_name"
1285
  OP_PARAMS = [
1286
    _PEarlyRelease,
1287
    _PNodeName,
1288
    _PNodeUuid,
1289
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1290
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1291
    _PIAllocFromDesc("Iallocator for computing solution"),
1292
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1293
     "Node evacuation mode"),
1294
    ]
1295
  OP_RESULT = TJobIdListOnly
1296

    
1297

    
1298
# instance opcodes
1299

    
1300
class OpInstanceCreate(OpCode):
1301
  """Create an instance.
1302

1303
  @ivar instance_name: Instance name
1304
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1305
  @ivar source_handshake: Signed handshake from source (remote import only)
1306
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1307
  @ivar source_instance_name: Previous name of instance (remote import only)
1308
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1309
    (remote import only)
1310

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

    
1376

    
1377
class OpInstanceMultiAlloc(OpCode):
1378
  """Allocates multiple instances.
1379

1380
  """
1381
  OP_PARAMS = [
1382
    _POpportunisticLocking,
1383
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1384
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1385
     "List of instance create opcodes describing the instances to allocate"),
1386
    ]
1387
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1388
  ALLOCATABLE_KEY = "allocatable"
1389
  FAILED_KEY = "allocatable"
1390
  OP_RESULT = ht.TStrictDict(True, True, {
1391
    constants.JOB_IDS_KEY: _JOB_LIST,
1392
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1393
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1394
    })
1395

    
1396
  def __getstate__(self):
1397
    """Generic serializer.
1398

1399
    """
1400
    state = OpCode.__getstate__(self)
1401
    if hasattr(self, "instances"):
1402
      # pylint: disable=E1101
1403
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1404
    return state
1405

    
1406
  def __setstate__(self, state):
1407
    """Generic unserializer.
1408

1409
    This method just restores from the serialized state the attributes
1410
    of the current instance.
1411

1412
    @param state: the serialized opcode data
1413
    @type state: C{dict}
1414

1415
    """
1416
    if not isinstance(state, dict):
1417
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1418
                       type(state))
1419

    
1420
    if "instances" in state:
1421
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1422

    
1423
    return OpCode.__setstate__(self, state)
1424

    
1425
  def Validate(self, set_defaults):
1426
    """Validates this opcode.
1427

1428
    We do this recursively.
1429

1430
    """
1431
    OpCode.Validate(self, set_defaults)
1432

    
1433
    for inst in self.instances: # pylint: disable=E1101
1434
      inst.Validate(set_defaults)
1435

    
1436

    
1437
class OpInstanceReinstall(OpCode):
1438
  """Reinstall an instance's OS."""
1439
  OP_DSC_FIELD = "instance_name"
1440
  OP_PARAMS = [
1441
    _PInstanceName,
1442
    _PForceVariant,
1443
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1444
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1445
    ]
1446
  OP_RESULT = ht.TNone
1447

    
1448

    
1449
class OpInstanceRemove(OpCode):
1450
  """Remove an instance."""
1451
  OP_DSC_FIELD = "instance_name"
1452
  OP_PARAMS = [
1453
    _PInstanceName,
1454
    _PShutdownTimeout,
1455
    ("ignore_failures", False, ht.TBool,
1456
     "Whether to ignore failures during removal"),
1457
    ]
1458
  OP_RESULT = ht.TNone
1459

    
1460

    
1461
class OpInstanceRename(OpCode):
1462
  """Rename an instance."""
1463
  OP_PARAMS = [
1464
    _PInstanceName,
1465
    _PNameCheck,
1466
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1467
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1468
    ]
1469
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1470

    
1471

    
1472
class OpInstanceStartup(OpCode):
1473
  """Startup an instance."""
1474
  OP_DSC_FIELD = "instance_name"
1475
  OP_PARAMS = [
1476
    _PInstanceName,
1477
    _PForce,
1478
    _PIgnoreOfflineNodes,
1479
    ("hvparams", ht.EmptyDict, ht.TDict,
1480
     "Temporary hypervisor parameters, hypervisor-dependent"),
1481
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1482
    _PNoRemember,
1483
    _PStartupPaused,
1484
    ]
1485
  OP_RESULT = ht.TNone
1486

    
1487

    
1488
class OpInstanceShutdown(OpCode):
1489
  """Shutdown an instance."""
1490
  OP_DSC_FIELD = "instance_name"
1491
  OP_PARAMS = [
1492
    _PInstanceName,
1493
    _PForce,
1494
    _PIgnoreOfflineNodes,
1495
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1496
     "How long to wait for instance to shut down"),
1497
    _PNoRemember,
1498
    ]
1499
  OP_RESULT = ht.TNone
1500

    
1501

    
1502
class OpInstanceReboot(OpCode):
1503
  """Reboot an instance."""
1504
  OP_DSC_FIELD = "instance_name"
1505
  OP_PARAMS = [
1506
    _PInstanceName,
1507
    _PShutdownTimeout,
1508
    ("ignore_secondaries", False, ht.TBool,
1509
     "Whether to start the instance even if secondary disks are failing"),
1510
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1511
     "How to reboot instance"),
1512
    ]
1513
  OP_RESULT = ht.TNone
1514

    
1515

    
1516
class OpInstanceReplaceDisks(OpCode):
1517
  """Replace the disks of an instance."""
1518
  OP_DSC_FIELD = "instance_name"
1519
  OP_PARAMS = [
1520
    _PInstanceName,
1521
    _PEarlyRelease,
1522
    _PIgnoreIpolicy,
1523
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1524
     "Replacement mode"),
1525
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1526
     "Disk indexes"),
1527
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1528
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1529
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1530
    ]
1531
  OP_RESULT = ht.TNone
1532

    
1533

    
1534
class OpInstanceFailover(OpCode):
1535
  """Failover an instance."""
1536
  OP_DSC_FIELD = "instance_name"
1537
  OP_PARAMS = [
1538
    _PInstanceName,
1539
    _PShutdownTimeout,
1540
    _PIgnoreConsistency,
1541
    _PMigrationTargetNode,
1542
    _PMigrationTargetNodeUuid,
1543
    _PIgnoreIpolicy,
1544
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1545
                     " shared-storage instances"),
1546
    ]
1547
  OP_RESULT = ht.TNone
1548

    
1549

    
1550
class OpInstanceMigrate(OpCode):
1551
  """Migrate an instance.
1552

1553
  This migrates (without shutting down an instance) to its secondary
1554
  node.
1555

1556
  @ivar instance_name: the name of the instance
1557
  @ivar mode: the migration mode (live, non-live or None for auto)
1558

1559
  """
1560
  OP_DSC_FIELD = "instance_name"
1561
  OP_PARAMS = [
1562
    _PInstanceName,
1563
    _PMigrationMode,
1564
    _PMigrationLive,
1565
    _PMigrationTargetNode,
1566
    _PMigrationTargetNodeUuid,
1567
    _PAllowRuntimeChgs,
1568
    _PIgnoreIpolicy,
1569
    ("cleanup", False, ht.TBool,
1570
     "Whether a previously failed migration should be cleaned up"),
1571
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1572
                     " shared-storage instances"),
1573
    ("allow_failover", False, ht.TBool,
1574
     "Whether we can fallback to failover if migration is not possible"),
1575
    ]
1576
  OP_RESULT = ht.TNone
1577

    
1578

    
1579
class OpInstanceMove(OpCode):
1580
  """Move an instance.
1581

1582
  This move (with shutting down an instance and data copying) to an
1583
  arbitrary node.
1584

1585
  @ivar instance_name: the name of the instance
1586
  @ivar target_node: the destination node
1587

1588
  """
1589
  OP_DSC_FIELD = "instance_name"
1590
  OP_PARAMS = [
1591
    _PInstanceName,
1592
    _PShutdownTimeout,
1593
    _PIgnoreIpolicy,
1594
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1595
    ("target_node_uuid", None, ht.TMaybeString, "Target node UUID"),
1596
    _PIgnoreConsistency,
1597
    ]
1598
  OP_RESULT = ht.TNone
1599

    
1600

    
1601
class OpInstanceConsole(OpCode):
1602
  """Connect to an instance's console."""
1603
  OP_DSC_FIELD = "instance_name"
1604
  OP_PARAMS = [
1605
    _PInstanceName,
1606
    ]
1607
  OP_RESULT = ht.TDict
1608

    
1609

    
1610
class OpInstanceActivateDisks(OpCode):
1611
  """Activate an instance's disks."""
1612
  OP_DSC_FIELD = "instance_name"
1613
  OP_PARAMS = [
1614
    _PInstanceName,
1615
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1616
    _PWaitForSyncFalse,
1617
    ]
1618
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1619
                                 ht.TItems([ht.TNonEmptyString,
1620
                                            ht.TNonEmptyString,
1621
                                            ht.TNonEmptyString])))
1622

    
1623

    
1624
class OpInstanceDeactivateDisks(OpCode):
1625
  """Deactivate an instance's disks."""
1626
  OP_DSC_FIELD = "instance_name"
1627
  OP_PARAMS = [
1628
    _PInstanceName,
1629
    _PForce,
1630
    ]
1631
  OP_RESULT = ht.TNone
1632

    
1633

    
1634
class OpInstanceRecreateDisks(OpCode):
1635
  """Recreate an instance's disks."""
1636
  _TDiskChanges = \
1637
    ht.TAnd(ht.TIsLength(2),
1638
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1639
                       ht.Comment("Parameters")(_TDiskParams)]))
1640

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

    
1656

    
1657
class OpInstanceQuery(OpCode):
1658
  """Compute the list of instances."""
1659
  OP_PARAMS = [
1660
    _POutputFields,
1661
    _PUseLocking,
1662
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1663
     "Empty list to query all instances, instance names otherwise"),
1664
    ]
1665
  OP_RESULT = _TOldQueryResult
1666

    
1667

    
1668
class OpInstanceQueryData(OpCode):
1669
  """Compute the run-time status of instances."""
1670
  OP_PARAMS = [
1671
    _PUseLocking,
1672
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1673
     "Instance names"),
1674
    ("static", False, ht.TBool,
1675
     "Whether to only return configuration data without querying"
1676
     " nodes"),
1677
    ]
1678
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1679

    
1680

    
1681
def _TestInstSetParamsModList(fn):
1682
  """Generates a check for modification lists.
1683

1684
  """
1685
  # Old format
1686
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1687
  old_mod_item_fn = \
1688
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1689
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1690
      fn,
1691
      ]))
1692

    
1693
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1694
  mod_item_fn = \
1695
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1696
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1697
      ht.Comment("Device index, can be negative, e.g. -1 for last disk")
1698
                 (ht.TOr(ht.TInt, ht.TString)),
1699
      fn,
1700
      ]))
1701

    
1702
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1703
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1704

    
1705

    
1706
class OpInstanceSetParams(OpCode):
1707
  """Change the parameters of an instance.
1708

1709
  """
1710
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1711
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1712

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

    
1754

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

    
1769

    
1770
class OpInstanceChangeGroup(OpCode):
1771
  """Moves an instance to another node group."""
1772
  OP_DSC_FIELD = "instance_name"
1773
  OP_PARAMS = [
1774
    _PInstanceName,
1775
    _PEarlyRelease,
1776
    _PIAllocFromDesc("Iallocator for computing solution"),
1777
    _PTargetGroups,
1778
    ]
1779
  OP_RESULT = TJobIdListOnly
1780

    
1781

    
1782
# Node group opcodes
1783

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

    
1799

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

    
1813

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

    
1823

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

    
1838

    
1839
class OpGroupRemove(OpCode):
1840
  """Remove a node group from the cluster."""
1841
  OP_DSC_FIELD = "group_name"
1842
  OP_PARAMS = [
1843
    _PGroupName,
1844
    ]
1845
  OP_RESULT = ht.TNone
1846

    
1847

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

    
1856

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

    
1868

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

    
1879

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

    
1890

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

    
1903

    
1904
class OpBackupPrepare(OpCode):
1905
  """Prepares an instance export.
1906

1907
  @ivar instance_name: Instance name
1908
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1909

1910
  """
1911
  OP_DSC_FIELD = "instance_name"
1912
  OP_PARAMS = [
1913
    _PInstanceName,
1914
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1915
     "Export mode"),
1916
    ]
1917
  OP_RESULT = ht.TMaybeDict
1918

    
1919

    
1920
class OpBackupExport(OpCode):
1921
  """Export an instance.
1922

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

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

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

    
1965

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

    
1974

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

    
1989

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

    
2002

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

    
2014

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

    
2026

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

2031
  This is used just for debugging and testing.
2032

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

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

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

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

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

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

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

    
2068

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

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

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

    
2108

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

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

    
2120

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

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

    
2133

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

    
2155

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

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

    
2168

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

    
2186

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

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

    
2206

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

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

    
2219

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

    
2230

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

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

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

    
2241

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