Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 66af5ec5

History | View | Annotate | Download (67 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 required node group name (for single-group LUs)
73
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
183
_PTargetGroups = \
184
  ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
185
   "Destination group names or UUIDs (defaults to \"all but current group\")")
186

    
187
# The reason for a state change of an instance
188
_PReason = \
189
  ("reason", (constants.INSTANCE_REASON_SOURCE_UNKNOWN, None),
190
   ht.TAnd(ht.TIsLength(2),
191
           ht.TItems([
192
             ht.TElemOf(constants.INSTANCE_REASON_SOURCES),
193
             ht.TMaybeString,
194
           ])),
195
   "The reason why the state of the instance is changing")
196

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

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

    
207
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
208

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

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

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

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

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

    
237
_TQueryResult = ht.TListOf(_TQueryRow)
238

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

    
241
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
242

    
243

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

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

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

    
257

    
258
def _NameToId(name):
259
  """Convert an opcode class name to an OP_ID.
260

261
  @type name: string
262
  @param name: the class name, as OpXxxYyy
263
  @rtype: string
264
  @return: the name in the OP_XXXX_YYYY format
265

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

    
278

    
279
def _GenerateObjectTypeCheck(obj, fields_types):
280
  """Helper to generate type checks for objects.
281

282
  @param obj: The object to generate type checks
283
  @param fields_types: The fields and their types as a dict
284
  @return: A ht type check function
285

286
  """
287
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
288
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
289
  return ht.TStrictDict(True, True, fields_types)
290

    
291

    
292
_TQueryFieldDef = \
293
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
294
    "name": ht.TNonEmptyString,
295
    "title": ht.TNonEmptyString,
296
    "kind": ht.TElemOf(constants.QFT_ALL),
297
    "doc": ht.TNonEmptyString,
298
    })
299

    
300

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

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

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

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

    
314

    
315
def RequireSharedFileStorage():
316
  """Checks that shared file storage is enabled.
317

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

321
  @raise errors.OpPrereqError: when shared file storage is disabled
322

323
  """
324
  if not constants.ENABLE_SHARED_FILE_STORAGE:
325
    raise errors.OpPrereqError("Shared file storage disabled at"
326
                               " configure time", errors.ECODE_INVAL)
327

    
328

    
329
@ht.WithDesc("CheckFileStorage")
330
def _CheckFileStorage(value):
331
  """Ensures file storage is enabled if used.
332

333
  """
334
  if value == constants.DT_FILE:
335
    RequireFileStorage()
336
  elif value == constants.DT_SHARED_FILE:
337
    RequireSharedFileStorage()
338
  return True
339

    
340

    
341
def _BuildDiskTemplateCheck(accept_none):
342
  """Builds check for disk template.
343

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

348
  """
349
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
350

    
351
  if accept_none:
352
    template_check = ht.TMaybe(template_check)
353

    
354
  return ht.TAnd(template_check, _CheckFileStorage)
355

    
356

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

360
  """
361
  if storage_type not in constants.VALID_STORAGE_TYPES:
362
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
363
                               errors.ECODE_INVAL)
364
  if storage_type == constants.ST_FILE:
365
    # TODO: What about shared file storage?
366
    RequireFileStorage()
367
  return True
368

    
369

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

    
374

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

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

    
386

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

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

    
398

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

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

    
410

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

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

    
422

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

    
429

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

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

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

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

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

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

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

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

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

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

    
469

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
566

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

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

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

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

    
586
  return ht.TMaybeListOf(job_dep)
587

    
588

    
589
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
590

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

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

    
605

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
721
    text = self.OP_ID[3:]
722

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

    
727
    return text
728

    
729

    
730
# cluster opcodes
731

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

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

738
  """
739
  OP_RESULT = ht.TBool
740

    
741

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

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

748
  """
749
  OP_RESULT = ht.TNonEmptyString
750

    
751

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

    
756

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

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

    
771

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

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

    
784

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

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

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

    
806

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

810
  """
811
  OP_RESULT = TJobIdListOnly
812

    
813

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

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

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

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

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

    
843

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

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

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

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

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

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

    
868

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

    
876

    
877
class OpClusterRename(OpCode):
878
  """Rename the cluster.
879

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

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

    
892

    
893
class OpClusterSetParams(OpCode):
894
  """Change the parameters of the cluster.
895

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

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

    
969

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

973
  """
974
  OP_RESULT = ht.TNone
975

    
976

    
977
class OpClusterActivateMasterIp(OpCode):
978
  """Activate the master IP on the master node.
979

980
  """
981
  OP_RESULT = ht.TNone
982

    
983

    
984
class OpClusterDeactivateMasterIp(OpCode):
985
  """Deactivate the master IP on the master node.
986

987
  """
988
  OP_RESULT = ht.TNone
989

    
990

    
991
class OpQuery(OpCode):
992
  """Query for resources/items.
993

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

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

    
1014

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

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

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

    
1033

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

    
1051

    
1052
class OpRestrictedCommand(OpCode):
1053
  """Runs a restricted command on node(s).
1054

1055
  """
1056
  OP_PARAMS = [
1057
    _PUseLocking,
1058
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1059
     "Nodes on which the command should be run (at least one)"),
1060
    ("command", ht.NoDefault, ht.TNonEmptyString,
1061
     "Command name (no parameters)"),
1062
    ]
1063

    
1064
  _RESULT_ITEMS = [
1065
    ht.Comment("success")(ht.TBool),
1066
    ht.Comment("output or error message")(ht.TString),
1067
    ]
1068

    
1069
  OP_RESULT = \
1070
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1071
                       ht.TItems(_RESULT_ITEMS)))
1072

    
1073

    
1074
# node opcodes
1075

    
1076
class OpNodeRemove(OpCode):
1077
  """Remove a node.
1078

1079
  @type node_name: C{str}
1080
  @ivar node_name: The name of the node to remove. If the node still has
1081
                   instances on it, the operation will fail.
1082

1083
  """
1084
  OP_DSC_FIELD = "node_name"
1085
  OP_PARAMS = [
1086
    _PNodeName,
1087
    ]
1088
  OP_RESULT = ht.TNone
1089

    
1090

    
1091
class OpNodeAdd(OpCode):
1092
  """Add a node to the cluster.
1093

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

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

    
1136

    
1137
class OpNodeQuery(OpCode):
1138
  """Compute the list of nodes."""
1139
  OP_PARAMS = [
1140
    _POutputFields,
1141
    _PUseLocking,
1142
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1143
     "Empty list to query all nodes, node names otherwise"),
1144
    ]
1145
  OP_RESULT = _TOldQueryResult
1146

    
1147

    
1148
class OpNodeQueryvols(OpCode):
1149
  """Get list of volumes on node."""
1150
  OP_PARAMS = [
1151
    _POutputFields,
1152
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1153
     "Empty list to query all nodes, node names otherwise"),
1154
    ]
1155
  OP_RESULT = ht.TListOf(ht.TAny)
1156

    
1157

    
1158
class OpNodeQueryStorage(OpCode):
1159
  """Get information on storage for node(s)."""
1160
  OP_PARAMS = [
1161
    _POutputFields,
1162
    _PStorageType,
1163
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1164
    ("name", None, ht.TMaybeString, "Storage name"),
1165
    ]
1166
  OP_RESULT = _TOldQueryResult
1167

    
1168

    
1169
class OpNodeModifyStorage(OpCode):
1170
  """Modifies the properies of a storage unit"""
1171
  OP_DSC_FIELD = "node_name"
1172
  OP_PARAMS = [
1173
    _PNodeName,
1174
    _PStorageType,
1175
    _PStorageName,
1176
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1177
    ]
1178
  OP_RESULT = ht.TNone
1179

    
1180

    
1181
class OpRepairNodeStorage(OpCode):
1182
  """Repairs the volume group on a node."""
1183
  OP_DSC_FIELD = "node_name"
1184
  OP_PARAMS = [
1185
    _PNodeName,
1186
    _PStorageType,
1187
    _PStorageName,
1188
    _PIgnoreConsistency,
1189
    ]
1190
  OP_RESULT = ht.TNone
1191

    
1192

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

    
1221

    
1222
class OpNodePowercycle(OpCode):
1223
  """Tries to powercycle a node."""
1224
  OP_DSC_FIELD = "node_name"
1225
  OP_PARAMS = [
1226
    _PNodeName,
1227
    _PForce,
1228
    ]
1229
  OP_RESULT = ht.TMaybeString
1230

    
1231

    
1232
class OpNodeMigrate(OpCode):
1233
  """Migrate all instances from a node."""
1234
  OP_DSC_FIELD = "node_name"
1235
  OP_PARAMS = [
1236
    _PNodeName,
1237
    _PMigrationMode,
1238
    _PMigrationLive,
1239
    _PMigrationTargetNode,
1240
    _PAllowRuntimeChgs,
1241
    _PIgnoreIpolicy,
1242
    _PIAllocFromDesc("Iallocator for deciding the target node"
1243
                     " for shared-storage instances"),
1244
    ]
1245
  OP_RESULT = TJobIdListOnly
1246

    
1247

    
1248
class OpNodeEvacuate(OpCode):
1249
  """Evacuate instances off a number of nodes."""
1250
  OP_DSC_FIELD = "node_name"
1251
  OP_PARAMS = [
1252
    _PEarlyRelease,
1253
    _PNodeName,
1254
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1255
    _PIAllocFromDesc("Iallocator for computing solution"),
1256
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1257
     "Node evacuation mode"),
1258
    ]
1259
  OP_RESULT = TJobIdListOnly
1260

    
1261

    
1262
# instance opcodes
1263

    
1264
class OpInstanceCreate(OpCode):
1265
  """Create an instance.
1266

1267
  @ivar instance_name: Instance name
1268
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1269
  @ivar source_handshake: Signed handshake from source (remote import only)
1270
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1271
  @ivar source_instance_name: Previous name of instance (remote import only)
1272
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1273
    (remote import only)
1274

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

    
1337

    
1338
class OpInstanceMultiAlloc(OpCode):
1339
  """Allocates multiple instances.
1340

1341
  """
1342
  OP_PARAMS = [
1343
    _POpportunisticLocking,
1344
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1345
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1346
     "List of instance create opcodes describing the instances to allocate"),
1347
    ]
1348
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1349
  ALLOCATABLE_KEY = "allocatable"
1350
  FAILED_KEY = "allocatable"
1351
  OP_RESULT = ht.TStrictDict(True, True, {
1352
    constants.JOB_IDS_KEY: _JOB_LIST,
1353
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1354
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1355
    })
1356

    
1357
  def __getstate__(self):
1358
    """Generic serializer.
1359

1360
    """
1361
    state = OpCode.__getstate__(self)
1362
    if hasattr(self, "instances"):
1363
      # pylint: disable=E1101
1364
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1365
    return state
1366

    
1367
  def __setstate__(self, state):
1368
    """Generic unserializer.
1369

1370
    This method just restores from the serialized state the attributes
1371
    of the current instance.
1372

1373
    @param state: the serialized opcode data
1374
    @type state: C{dict}
1375

1376
    """
1377
    if not isinstance(state, dict):
1378
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1379
                       type(state))
1380

    
1381
    if "instances" in state:
1382
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1383

    
1384
    return OpCode.__setstate__(self, state)
1385

    
1386
  def Validate(self, set_defaults):
1387
    """Validates this opcode.
1388

1389
    We do this recursively.
1390

1391
    """
1392
    OpCode.Validate(self, set_defaults)
1393

    
1394
    for inst in self.instances: # pylint: disable=E1101
1395
      inst.Validate(set_defaults)
1396

    
1397

    
1398
class OpInstanceReinstall(OpCode):
1399
  """Reinstall an instance's OS."""
1400
  OP_DSC_FIELD = "instance_name"
1401
  OP_PARAMS = [
1402
    _PInstanceName,
1403
    _PForceVariant,
1404
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1405
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1406
    ]
1407
  OP_RESULT = ht.TNone
1408

    
1409

    
1410
class OpInstanceRemove(OpCode):
1411
  """Remove an instance."""
1412
  OP_DSC_FIELD = "instance_name"
1413
  OP_PARAMS = [
1414
    _PInstanceName,
1415
    _PShutdownTimeout,
1416
    ("ignore_failures", False, ht.TBool,
1417
     "Whether to ignore failures during removal"),
1418
    ]
1419
  OP_RESULT = ht.TNone
1420

    
1421

    
1422
class OpInstanceRename(OpCode):
1423
  """Rename an instance."""
1424
  OP_PARAMS = [
1425
    _PInstanceName,
1426
    _PNameCheck,
1427
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1428
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1429
    ]
1430
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1431

    
1432

    
1433
class OpInstanceStartup(OpCode):
1434
  """Startup an instance."""
1435
  OP_DSC_FIELD = "instance_name"
1436
  OP_PARAMS = [
1437
    _PInstanceName,
1438
    _PForce,
1439
    _PIgnoreOfflineNodes,
1440
    ("hvparams", ht.EmptyDict, ht.TDict,
1441
     "Temporary hypervisor parameters, hypervisor-dependent"),
1442
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1443
    _PNoRemember,
1444
    _PStartupPaused,
1445
    ]
1446
  OP_RESULT = ht.TNone
1447

    
1448

    
1449
class OpInstanceShutdown(OpCode):
1450
  """Shutdown an instance."""
1451
  OP_DSC_FIELD = "instance_name"
1452
  OP_PARAMS = [
1453
    _PInstanceName,
1454
    _PForce,
1455
    _PIgnoreOfflineNodes,
1456
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1457
     "How long to wait for instance to shut down"),
1458
    _PNoRemember,
1459
    ]
1460
  OP_RESULT = ht.TNone
1461

    
1462

    
1463
class OpInstanceReboot(OpCode):
1464
  """Reboot an instance."""
1465
  OP_DSC_FIELD = "instance_name"
1466
  OP_PARAMS = [
1467
    _PInstanceName,
1468
    _PShutdownTimeout,
1469
    ("ignore_secondaries", False, ht.TBool,
1470
     "Whether to start the instance even if secondary disks are failing"),
1471
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1472
     "How to reboot instance"),
1473
    _PReason,
1474
    ]
1475
  OP_RESULT = ht.TNone
1476

    
1477

    
1478
class OpInstanceReplaceDisks(OpCode):
1479
  """Replace the disks of an instance."""
1480
  OP_DSC_FIELD = "instance_name"
1481
  OP_PARAMS = [
1482
    _PInstanceName,
1483
    _PEarlyRelease,
1484
    _PIgnoreIpolicy,
1485
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1486
     "Replacement mode"),
1487
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1488
     "Disk indexes"),
1489
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1490
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1491
    ]
1492
  OP_RESULT = ht.TNone
1493

    
1494

    
1495
class OpInstanceFailover(OpCode):
1496
  """Failover an instance."""
1497
  OP_DSC_FIELD = "instance_name"
1498
  OP_PARAMS = [
1499
    _PInstanceName,
1500
    _PShutdownTimeout,
1501
    _PIgnoreConsistency,
1502
    _PMigrationTargetNode,
1503
    _PIgnoreIpolicy,
1504
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1505
                     " shared-storage instances"),
1506
    ]
1507
  OP_RESULT = ht.TNone
1508

    
1509

    
1510
class OpInstanceMigrate(OpCode):
1511
  """Migrate an instance.
1512

1513
  This migrates (without shutting down an instance) to its secondary
1514
  node.
1515

1516
  @ivar instance_name: the name of the instance
1517
  @ivar mode: the migration mode (live, non-live or None for auto)
1518

1519
  """
1520
  OP_DSC_FIELD = "instance_name"
1521
  OP_PARAMS = [
1522
    _PInstanceName,
1523
    _PMigrationMode,
1524
    _PMigrationLive,
1525
    _PMigrationTargetNode,
1526
    _PAllowRuntimeChgs,
1527
    _PIgnoreIpolicy,
1528
    ("cleanup", False, ht.TBool,
1529
     "Whether a previously failed migration should be cleaned up"),
1530
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1531
                     " shared-storage instances"),
1532
    ("allow_failover", False, ht.TBool,
1533
     "Whether we can fallback to failover if migration is not possible"),
1534
    ]
1535
  OP_RESULT = ht.TNone
1536

    
1537

    
1538
class OpInstanceMove(OpCode):
1539
  """Move an instance.
1540

1541
  This move (with shutting down an instance and data copying) to an
1542
  arbitrary node.
1543

1544
  @ivar instance_name: the name of the instance
1545
  @ivar target_node: the destination node
1546

1547
  """
1548
  OP_DSC_FIELD = "instance_name"
1549
  OP_PARAMS = [
1550
    _PInstanceName,
1551
    _PShutdownTimeout,
1552
    _PIgnoreIpolicy,
1553
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1554
    _PIgnoreConsistency,
1555
    ]
1556
  OP_RESULT = ht.TNone
1557

    
1558

    
1559
class OpInstanceConsole(OpCode):
1560
  """Connect to an instance's console."""
1561
  OP_DSC_FIELD = "instance_name"
1562
  OP_PARAMS = [
1563
    _PInstanceName,
1564
    ]
1565
  OP_RESULT = ht.TDict
1566

    
1567

    
1568
class OpInstanceActivateDisks(OpCode):
1569
  """Activate an instance's disks."""
1570
  OP_DSC_FIELD = "instance_name"
1571
  OP_PARAMS = [
1572
    _PInstanceName,
1573
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1574
    _PWaitForSyncFalse,
1575
    ]
1576
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1577
                                 ht.TItems([ht.TNonEmptyString,
1578
                                            ht.TNonEmptyString,
1579
                                            ht.TNonEmptyString])))
1580

    
1581

    
1582
class OpInstanceDeactivateDisks(OpCode):
1583
  """Deactivate an instance's disks."""
1584
  OP_DSC_FIELD = "instance_name"
1585
  OP_PARAMS = [
1586
    _PInstanceName,
1587
    _PForce,
1588
    ]
1589
  OP_RESULT = ht.TNone
1590

    
1591

    
1592
class OpInstanceRecreateDisks(OpCode):
1593
  """Recreate an instance's disks."""
1594
  _TDiskChanges = \
1595
    ht.TAnd(ht.TIsLength(2),
1596
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1597
                       ht.Comment("Parameters")(_TDiskParams)]))
1598

    
1599
  OP_DSC_FIELD = "instance_name"
1600
  OP_PARAMS = [
1601
    _PInstanceName,
1602
    ("disks", ht.EmptyList,
1603
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1604
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1605
     " index and a possibly empty dictionary with disk parameter changes"),
1606
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1607
     "New instance nodes, if relocation is desired"),
1608
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1609
    ]
1610
  OP_RESULT = ht.TNone
1611

    
1612

    
1613
class OpInstanceQuery(OpCode):
1614
  """Compute the list of instances."""
1615
  OP_PARAMS = [
1616
    _POutputFields,
1617
    _PUseLocking,
1618
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1619
     "Empty list to query all instances, instance names otherwise"),
1620
    ]
1621
  OP_RESULT = _TOldQueryResult
1622

    
1623

    
1624
class OpInstanceQueryData(OpCode):
1625
  """Compute the run-time status of instances."""
1626
  OP_PARAMS = [
1627
    _PUseLocking,
1628
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1629
     "Instance names"),
1630
    ("static", False, ht.TBool,
1631
     "Whether to only return configuration data without querying"
1632
     " nodes"),
1633
    ]
1634
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1635

    
1636

    
1637
def _TestInstSetParamsModList(fn):
1638
  """Generates a check for modification lists.
1639

1640
  """
1641
  # Old format
1642
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1643
  old_mod_item_fn = \
1644
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1645
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1646
      fn,
1647
      ]))
1648

    
1649
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1650
  mod_item_fn = \
1651
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1652
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1653
      ht.Comment("Disk index, can be negative, e.g. -1 for last disk")(ht.TInt),
1654
      fn,
1655
      ]))
1656

    
1657
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1658
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1659

    
1660

    
1661
class OpInstanceSetParams(OpCode):
1662
  """Change the parameters of an instance.
1663

1664
  """
1665
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1666
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1667

    
1668
  OP_DSC_FIELD = "instance_name"
1669
  OP_PARAMS = [
1670
    _PInstanceName,
1671
    _PForce,
1672
    _PForceVariant,
1673
    _PIgnoreIpolicy,
1674
    ("nics", ht.EmptyList, TestNicModifications,
1675
     "List of NIC changes: each item is of the form ``(op, index, settings)``,"
1676
     " ``op`` is one of ``%s``, ``%s`` or ``%s``, ``index`` can be either -1"
1677
     " to refer to the last position, or a zero-based index number; a"
1678
     " deprecated version of this parameter used the form ``(op, settings)``,"
1679
     " where ``op`` can be ``%s`` to add a new NIC with the specified"
1680
     " settings, ``%s`` to remove the last NIC or a number to modify the"
1681
     " settings of the NIC with that index" %
1682
     (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1683
      constants.DDM_ADD, constants.DDM_REMOVE)),
1684
    ("disks", ht.EmptyList, TestDiskModifications,
1685
     "List of disk changes; see ``nics``"),
1686
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1687
    ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1688
    ("hvparams", ht.EmptyDict, ht.TDict,
1689
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1690
    ("disk_template", None, ht.TMaybe(_BuildDiskTemplateCheck(False)),
1691
     "Disk template for instance"),
1692
    ("pnode", None, ht.TMaybeString, "New primary node"),
1693
    ("remote_node", None, ht.TMaybeString,
1694
     "Secondary node (used when changing disk template)"),
1695
    ("os_name", None, ht.TMaybeString,
1696
     "Change the instance's OS without reinstalling the instance"),
1697
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1698
    ("wait_for_sync", True, ht.TBool,
1699
     "Whether to wait for the disk to synchronize, when changing template"),
1700
    ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1701
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1702
    ]
1703
  OP_RESULT = _TSetParamsResult
1704

    
1705

    
1706
class OpInstanceGrowDisk(OpCode):
1707
  """Grow a disk of an instance."""
1708
  OP_DSC_FIELD = "instance_name"
1709
  OP_PARAMS = [
1710
    _PInstanceName,
1711
    _PWaitForSync,
1712
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1713
    ("amount", ht.NoDefault, ht.TNonNegativeInt,
1714
     "Amount of disk space to add (megabytes)"),
1715
    ("absolute", False, ht.TBool,
1716
     "Whether the amount parameter is an absolute target or a relative one"),
1717
    ]
1718
  OP_RESULT = ht.TNone
1719

    
1720

    
1721
class OpInstanceChangeGroup(OpCode):
1722
  """Moves an instance to another node group."""
1723
  OP_DSC_FIELD = "instance_name"
1724
  OP_PARAMS = [
1725
    _PInstanceName,
1726
    _PEarlyRelease,
1727
    _PIAllocFromDesc("Iallocator for computing solution"),
1728
    _PTargetGroups,
1729
    ]
1730
  OP_RESULT = TJobIdListOnly
1731

    
1732

    
1733
# Node group opcodes
1734

    
1735
class OpGroupAdd(OpCode):
1736
  """Add a node group to the cluster."""
1737
  OP_DSC_FIELD = "group_name"
1738
  OP_PARAMS = [
1739
    _PGroupName,
1740
    _PNodeGroupAllocPolicy,
1741
    _PGroupNodeParams,
1742
    _PDiskParams,
1743
    _PHvState,
1744
    _PDiskState,
1745
    ("ipolicy", None, ht.TMaybeDict,
1746
     "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1747
    ]
1748
  OP_RESULT = ht.TNone
1749

    
1750

    
1751
class OpGroupAssignNodes(OpCode):
1752
  """Assign nodes to a node group."""
1753
  OP_DSC_FIELD = "group_name"
1754
  OP_PARAMS = [
1755
    _PGroupName,
1756
    _PForce,
1757
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1758
     "List of nodes to assign"),
1759
    ]
1760
  OP_RESULT = ht.TNone
1761

    
1762

    
1763
class OpGroupQuery(OpCode):
1764
  """Compute the list of node groups."""
1765
  OP_PARAMS = [
1766
    _POutputFields,
1767
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1768
     "Empty list to query all groups, group names otherwise"),
1769
    ]
1770
  OP_RESULT = _TOldQueryResult
1771

    
1772

    
1773
class OpGroupSetParams(OpCode):
1774
  """Change the parameters of a node group."""
1775
  OP_DSC_FIELD = "group_name"
1776
  OP_PARAMS = [
1777
    _PGroupName,
1778
    _PNodeGroupAllocPolicy,
1779
    _PGroupNodeParams,
1780
    _PDiskParams,
1781
    _PHvState,
1782
    _PDiskState,
1783
    ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1784
    ]
1785
  OP_RESULT = _TSetParamsResult
1786

    
1787

    
1788
class OpGroupRemove(OpCode):
1789
  """Remove a node group from the cluster."""
1790
  OP_DSC_FIELD = "group_name"
1791
  OP_PARAMS = [
1792
    _PGroupName,
1793
    ]
1794
  OP_RESULT = ht.TNone
1795

    
1796

    
1797
class OpGroupRename(OpCode):
1798
  """Rename a node group in the cluster."""
1799
  OP_PARAMS = [
1800
    _PGroupName,
1801
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1802
    ]
1803
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1804

    
1805

    
1806
class OpGroupEvacuate(OpCode):
1807
  """Evacuate a node group in the cluster."""
1808
  OP_DSC_FIELD = "group_name"
1809
  OP_PARAMS = [
1810
    _PGroupName,
1811
    _PEarlyRelease,
1812
    _PIAllocFromDesc("Iallocator for computing solution"),
1813
    _PTargetGroups,
1814
    ]
1815
  OP_RESULT = TJobIdListOnly
1816

    
1817

    
1818
# OS opcodes
1819
class OpOsDiagnose(OpCode):
1820
  """Compute the list of guest operating systems."""
1821
  OP_PARAMS = [
1822
    _POutputFields,
1823
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1824
     "Which operating systems to diagnose"),
1825
    ]
1826
  OP_RESULT = _TOldQueryResult
1827

    
1828

    
1829
# ExtStorage opcodes
1830
class OpExtStorageDiagnose(OpCode):
1831
  """Compute the list of external storage providers."""
1832
  OP_PARAMS = [
1833
    _POutputFields,
1834
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1835
     "Which ExtStorage Provider to diagnose"),
1836
    ]
1837
  OP_RESULT = _TOldQueryResult
1838

    
1839

    
1840
# Exports opcodes
1841
class OpBackupQuery(OpCode):
1842
  """Compute the list of exported images."""
1843
  OP_PARAMS = [
1844
    _PUseLocking,
1845
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1846
     "Empty list to query all nodes, node names otherwise"),
1847
    ]
1848
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1849
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1850
                                ht.TListOf(ht.TNonEmptyString)))
1851

    
1852

    
1853
class OpBackupPrepare(OpCode):
1854
  """Prepares an instance export.
1855

1856
  @ivar instance_name: Instance name
1857
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1858

1859
  """
1860
  OP_DSC_FIELD = "instance_name"
1861
  OP_PARAMS = [
1862
    _PInstanceName,
1863
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1864
     "Export mode"),
1865
    ]
1866
  OP_RESULT = ht.TMaybeDict
1867

    
1868

    
1869
class OpBackupExport(OpCode):
1870
  """Export an instance.
1871

1872
  For local exports, the export destination is the node name. For
1873
  remote exports, the export destination is a list of tuples, each
1874
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1875
  salt. The HMAC is calculated using the cluster domain secret over
1876
  the value "${index}:${hostname}:${port}". The destination X509 CA
1877
  must be a signed certificate.
1878

1879
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1880
  @ivar target_node: Export destination
1881
  @ivar x509_key_name: X509 key to use (remote export only)
1882
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1883
                             only)
1884

1885
  """
1886
  OP_DSC_FIELD = "instance_name"
1887
  OP_PARAMS = [
1888
    _PInstanceName,
1889
    _PShutdownTimeout,
1890
    # TODO: Rename target_node as it changes meaning for different export modes
1891
    # (e.g. "destination")
1892
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1893
     "Destination information, depends on export mode"),
1894
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1895
    ("remove_instance", False, ht.TBool,
1896
     "Whether to remove instance after export"),
1897
    ("ignore_remove_failures", False, ht.TBool,
1898
     "Whether to ignore failures while removing instances"),
1899
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1900
     "Export mode"),
1901
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1902
     "Name of X509 key (remote export only)"),
1903
    ("destination_x509_ca", None, ht.TMaybeString,
1904
     "Destination X509 CA (remote export only)"),
1905
    ]
1906
  OP_RESULT = \
1907
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1908
      ht.Comment("Finalizing status")(ht.TBool),
1909
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1910
      ]))
1911

    
1912

    
1913
class OpBackupRemove(OpCode):
1914
  """Remove an instance's export."""
1915
  OP_DSC_FIELD = "instance_name"
1916
  OP_PARAMS = [
1917
    _PInstanceName,
1918
    ]
1919
  OP_RESULT = ht.TNone
1920

    
1921

    
1922
# Tags opcodes
1923
class OpTagsGet(OpCode):
1924
  """Returns the tags of the given object."""
1925
  OP_DSC_FIELD = "name"
1926
  OP_PARAMS = [
1927
    _PTagKind,
1928
    # Not using _PUseLocking as the default is different for historical reasons
1929
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1930
    # Name is only meaningful for nodes and instances
1931
    ("name", ht.NoDefault, ht.TMaybeString,
1932
     "Name of object to retrieve tags from"),
1933
    ]
1934
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1935

    
1936

    
1937
class OpTagsSearch(OpCode):
1938
  """Searches the tags in the cluster for a given pattern."""
1939
  OP_DSC_FIELD = "pattern"
1940
  OP_PARAMS = [
1941
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1942
     "Search pattern (regular expression)"),
1943
    ]
1944
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1945
    ht.TNonEmptyString,
1946
    ht.TNonEmptyString,
1947
    ])))
1948

    
1949

    
1950
class OpTagsSet(OpCode):
1951
  """Add a list of tags on a given object."""
1952
  OP_PARAMS = [
1953
    _PTagKind,
1954
    _PTags,
1955
    # Name is only meaningful for groups, nodes and instances
1956
    ("name", ht.NoDefault, ht.TMaybeString,
1957
     "Name of object where tag(s) should be added"),
1958
    ]
1959
  OP_RESULT = ht.TNone
1960

    
1961

    
1962
class OpTagsDel(OpCode):
1963
  """Remove a list of tags from a given object."""
1964
  OP_PARAMS = [
1965
    _PTagKind,
1966
    _PTags,
1967
    # Name is only meaningful for groups, nodes and instances
1968
    ("name", ht.NoDefault, ht.TMaybeString,
1969
     "Name of object where tag(s) should be deleted"),
1970
    ]
1971
  OP_RESULT = ht.TNone
1972

    
1973

    
1974
# Test opcodes
1975
class OpTestDelay(OpCode):
1976
  """Sleeps for a configured amount of time.
1977

1978
  This is used just for debugging and testing.
1979

1980
  Parameters:
1981
    - duration: the time to sleep, in seconds
1982
    - on_master: if true, sleep on the master
1983
    - on_nodes: list of nodes in which to sleep
1984

1985
  If the on_master parameter is true, it will execute a sleep on the
1986
  master (before any node sleep).
1987

1988
  If the on_nodes list is not empty, it will sleep on those nodes
1989
  (after the sleep on the master, if that is enabled).
1990

1991
  As an additional feature, the case of duration < 0 will be reported
1992
  as an execution error, so this opcode can be used as a failure
1993
  generator. The case of duration == 0 will not be treated specially.
1994

1995
  """
1996
  OP_DSC_FIELD = "duration"
1997
  OP_PARAMS = [
1998
    ("duration", ht.NoDefault, ht.TNumber, None),
1999
    ("on_master", True, ht.TBool, None),
2000
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2001
    ("repeat", 0, ht.TNonNegativeInt, None),
2002
    ]
2003

    
2004
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
2005
    """Custom formatter for duration.
2006

2007
    """
2008
    try:
2009
      v = float(value)
2010
    except TypeError:
2011
      v = value
2012
    return str(v)
2013

    
2014

    
2015
class OpTestAllocator(OpCode):
2016
  """Allocator framework testing.
2017

2018
  This opcode has two modes:
2019
    - gather and return allocator input for a given mode (allocate new
2020
      or replace secondary) and a given instance definition (direction
2021
      'in')
2022
    - run a selected allocator for a given operation (as above) and
2023
      return the allocator output (direction 'out')
2024

2025
  """
2026
  OP_DSC_FIELD = "iallocator"
2027
  OP_PARAMS = [
2028
    ("direction", ht.NoDefault,
2029
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
2030
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
2031
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2032
    ("nics", ht.NoDefault,
2033
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2034
                                            constants.INIC_IP,
2035
                                            "bridge"]),
2036
                                ht.TMaybeString)),
2037
     None),
2038
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2039
    ("hypervisor", None, ht.TMaybeString, None),
2040
    _PIAllocFromDesc(None),
2041
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2042
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2043
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2044
    ("os", None, ht.TMaybeString, None),
2045
    ("disk_template", None, ht.TMaybeString, None),
2046
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2047
    ("evac_mode", None,
2048
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2049
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2050
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2051
    ("count", 1, ht.TNonNegativeInt, None),
2052
    ]
2053

    
2054

    
2055
class OpTestJqueue(OpCode):
2056
  """Utility opcode to test some aspects of the job queue.
2057

2058
  """
2059
  OP_PARAMS = [
2060
    ("notify_waitlock", False, ht.TBool, None),
2061
    ("notify_exec", False, ht.TBool, None),
2062
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2063
    ("fail", False, ht.TBool, None),
2064
    ]
2065

    
2066

    
2067
class OpTestDummy(OpCode):
2068
  """Utility opcode used by unittests.
2069

2070
  """
2071
  OP_PARAMS = [
2072
    ("result", ht.NoDefault, ht.NoType, None),
2073
    ("messages", ht.NoDefault, ht.NoType, None),
2074
    ("fail", ht.NoDefault, ht.NoType, None),
2075
    ("submit_jobs", None, ht.NoType, None),
2076
    ]
2077
  WITH_LU = False
2078

    
2079

    
2080
# Network opcodes
2081
# Add a new network in the cluster
2082
class OpNetworkAdd(OpCode):
2083
  """Add an IP network to the cluster."""
2084
  OP_DSC_FIELD = "network_name"
2085
  OP_PARAMS = [
2086
    _PNetworkName,
2087
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2088
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2089
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2090
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2091
    ("mac_prefix", None, ht.TMaybeString,
2092
     "MAC address prefix that overrides cluster one"),
2093
    ("add_reserved_ips", None, _TMaybeAddr4List,
2094
     "Which IP addresses to reserve"),
2095
    ("conflicts_check", True, ht.TBool,
2096
     "Whether to check for conflicting IP addresses"),
2097
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2098
    ]
2099
  OP_RESULT = ht.TNone
2100

    
2101

    
2102
class OpNetworkRemove(OpCode):
2103
  """Remove an existing network from the cluster.
2104
     Must not be connected to any nodegroup.
2105

2106
  """
2107
  OP_DSC_FIELD = "network_name"
2108
  OP_PARAMS = [
2109
    _PNetworkName,
2110
    _PForce,
2111
    ]
2112
  OP_RESULT = ht.TNone
2113

    
2114

    
2115
class OpNetworkSetParams(OpCode):
2116
  """Modify Network's parameters except for IPv4 subnet"""
2117
  OP_DSC_FIELD = "network_name"
2118
  OP_PARAMS = [
2119
    _PNetworkName,
2120
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2121
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2122
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2123
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2124
     "MAC address prefix that overrides cluster one"),
2125
    ("add_reserved_ips", None, _TMaybeAddr4List,
2126
     "Which external IP addresses to reserve"),
2127
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2128
     "Which external IP addresses to release"),
2129
    ]
2130
  OP_RESULT = ht.TNone
2131

    
2132

    
2133
class OpNetworkConnect(OpCode):
2134
  """Connect a Network to a specific Nodegroup with the defined netparams
2135
     (mode, link). Nics in this Network will inherit those params.
2136
     Produce errors if a NIC (that its not already assigned to a network)
2137
     has an IP that is contained in the Network this will produce error unless
2138
     --no-conflicts-check is passed.
2139

2140
  """
2141
  OP_DSC_FIELD = "network_name"
2142
  OP_PARAMS = [
2143
    _PGroupName,
2144
    _PNetworkName,
2145
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2146
     "Connectivity mode"),
2147
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2148
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2149
    ]
2150
  OP_RESULT = ht.TNone
2151

    
2152

    
2153
class OpNetworkDisconnect(OpCode):
2154
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2155
     present in the Network unless --no-conficts-check option is passed.
2156

2157
  """
2158
  OP_DSC_FIELD = "network_name"
2159
  OP_PARAMS = [
2160
    _PGroupName,
2161
    _PNetworkName,
2162
    ]
2163
  OP_RESULT = ht.TNone
2164

    
2165

    
2166
class OpNetworkQuery(OpCode):
2167
  """Compute the list of networks."""
2168
  OP_PARAMS = [
2169
    _POutputFields,
2170
    _PUseLocking,
2171
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2172
     "Empty list to query all groups, group names otherwise"),
2173
    ]
2174
  OP_RESULT = _TOldQueryResult
2175

    
2176

    
2177
def _GetOpList():
2178
  """Returns list of all defined opcodes.
2179

2180
  Does not eliminate duplicates by C{OP_ID}.
2181

2182
  """
2183
  return [v for v in globals().values()
2184
          if (isinstance(v, type) and issubclass(v, OpCode) and
2185
              hasattr(v, "OP_ID") and v is not OpCode)]
2186

    
2187

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