Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 1c3231aa

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

    
991

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

995
  """
996
  OP_RESULT = ht.TNone
997

    
998

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

1002
  """
1003
  OP_RESULT = ht.TNone
1004

    
1005

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

1009
  """
1010
  OP_RESULT = ht.TNone
1011

    
1012

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

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

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

    
1036

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

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

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

    
1055

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

    
1075

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

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

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

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

    
1099

    
1100
# node opcodes
1101

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

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

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

    
1117

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

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

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

    
1163

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

    
1174

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

    
1184

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

    
1195

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

    
1208

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

    
1221

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

    
1251

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

    
1262

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

    
1280

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

    
1296

    
1297
# instance opcodes
1298

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

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

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

    
1375

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

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

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

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

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

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

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

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

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

    
1422
    return OpCode.__setstate__(self, state)
1423

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

1427
    We do this recursively.
1428

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

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

    
1435

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

    
1447

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

    
1459

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

    
1470

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

    
1486

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

    
1500

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

    
1514

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

    
1532

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

    
1548

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

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

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

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

    
1577

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

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

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

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

    
1599

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

    
1608

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

    
1622

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

    
1632

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

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

    
1655

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

    
1666

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

    
1679

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

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

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

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

    
1704

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

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

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

    
1753

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

    
1768

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

    
1780

    
1781
# Node group opcodes
1782

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

    
1798

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

    
1812

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

    
1822

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

    
1837

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

    
1846

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

    
1855

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

    
1867

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

    
1878

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

    
1889

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

    
1902

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

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

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

    
1918

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

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

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

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

    
1964

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

    
1973

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

    
1988

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

    
2001

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

    
2013

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

    
2025

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

2030
  This is used just for debugging and testing.
2031

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

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

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

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

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

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

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

    
2067

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

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

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

    
2107

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

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

    
2119

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

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

    
2132

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

    
2154

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

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

    
2167

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

    
2185

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

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

    
2205

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

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

    
2218

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

    
2229

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

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

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

    
2240

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