Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 6a2c20f2

History | View | Annotate | Download (65.6 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
#: OP_ID conversion regular expression
188
_OPID_RE = re.compile("([a-z])([A-Z])")
189

    
190
#: Utility function for L{OpClusterSetParams}
191
_TestClusterOsListItem = \
192
  ht.TAnd(ht.TIsLength(2), ht.TItems([
193
    ht.TElemOf(constants.DDMS_VALUES),
194
    ht.TNonEmptyString,
195
    ]))
196

    
197
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
198

    
199
# TODO: Generate check from constants.INIC_PARAMS_TYPES
200
#: Utility function for testing NIC definitions
201
_TestNicDef = \
202
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
203
                                          ht.TMaybeString))
204

    
205
_TSetParamsResultItemItems = [
206
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
207
  ht.Comment("new value")(ht.TAny),
208
  ]
209

    
210
_TSetParamsResult = \
211
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
212
                     ht.TItems(_TSetParamsResultItemItems)))
213

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

    
222
_TQueryRow = \
223
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
224
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
225
                                ht.TAny])))
226

    
227
_TQueryResult = ht.TListOf(_TQueryRow)
228

    
229
_TOldQueryRow = ht.TListOf(ht.TAny)
230

    
231
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
232

    
233

    
234
_SUMMARY_PREFIX = {
235
  "CLUSTER_": "C_",
236
  "GROUP_": "G_",
237
  "NODE_": "N_",
238
  "INSTANCE_": "I_",
239
  }
240

    
241
#: Attribute name for dependencies
242
DEPEND_ATTR = "depends"
243

    
244
#: Attribute name for comment
245
COMMENT_ATTR = "comment"
246

    
247

    
248
def _NameToId(name):
249
  """Convert an opcode class name to an OP_ID.
250

251
  @type name: string
252
  @param name: the class name, as OpXxxYyy
253
  @rtype: string
254
  @return: the name in the OP_XXXX_YYYY format
255

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

    
268

    
269
def _GenerateObjectTypeCheck(obj, fields_types):
270
  """Helper to generate type checks for objects.
271

272
  @param obj: The object to generate type checks
273
  @param fields_types: The fields and their types as a dict
274
  @return: A ht type check function
275

276
  """
277
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
278
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
279
  return ht.TStrictDict(True, True, fields_types)
280

    
281

    
282
_TQueryFieldDef = \
283
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
284
    "name": ht.TNonEmptyString,
285
    "title": ht.TNonEmptyString,
286
    "kind": ht.TElemOf(constants.QFT_ALL),
287
    "doc": ht.TNonEmptyString,
288
    })
289

    
290

    
291
def RequireFileStorage():
292
  """Checks that file storage is enabled.
293

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

297
  @raise errors.OpPrereqError: when file storage is disabled
298

299
  """
300
  if not constants.ENABLE_FILE_STORAGE:
301
    raise errors.OpPrereqError("File storage disabled at configure time",
302
                               errors.ECODE_INVAL)
303

    
304

    
305
def RequireSharedFileStorage():
306
  """Checks that shared file storage is enabled.
307

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

311
  @raise errors.OpPrereqError: when shared file storage is disabled
312

313
  """
314
  if not constants.ENABLE_SHARED_FILE_STORAGE:
315
    raise errors.OpPrereqError("Shared file storage disabled at"
316
                               " configure time", errors.ECODE_INVAL)
317

    
318

    
319
@ht.WithDesc("CheckFileStorage")
320
def _CheckFileStorage(value):
321
  """Ensures file storage is enabled if used.
322

323
  """
324
  if value == constants.DT_FILE:
325
    RequireFileStorage()
326
  elif value == constants.DT_SHARED_FILE:
327
    RequireSharedFileStorage()
328
  return True
329

    
330

    
331
def _BuildDiskTemplateCheck(accept_none):
332
  """Builds check for disk template.
333

334
  @type accept_none: bool
335
  @param accept_none: whether to accept None as a correct value
336
  @rtype: callable
337

338
  """
339
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
340

    
341
  if accept_none:
342
    template_check = ht.TMaybe(template_check)
343

    
344
  return ht.TAnd(template_check, _CheckFileStorage)
345

    
346

    
347
def _CheckStorageType(storage_type):
348
  """Ensure a given storage type is valid.
349

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

    
359

    
360
#: Storage type parameter
361
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
362
                 "Storage type")
363

    
364

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

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

    
376

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

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

    
388

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

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

    
400

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

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

    
412

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

    
419

    
420
class _AutoOpParamSlots(outils.AutoSlots):
421
  """Meta class for opcode definitions.
422

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

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

433
    """
434
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
435

    
436
    slots = mcs._GetSlots(attrs)
437
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
438
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
439
    assert ("OP_DSC_FORMATTER" not in attrs or
440
            callable(attrs["OP_DSC_FORMATTER"])), \
441
      ("Class '%s' uses non-callable in OP_DSC_FORMATTER (%s)" %
442
       (name, type(attrs["OP_DSC_FORMATTER"])))
443

    
444
    attrs["OP_ID"] = _NameToId(name)
445

    
446
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
447

    
448
  @classmethod
449
  def _GetSlots(mcs, attrs):
450
    """Build the slots out of OP_PARAMS.
451

452
    """
453
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
454
    params = attrs.setdefault("OP_PARAMS", [])
455

    
456
    # Use parameter names as slots
457
    return [pname for (pname, _, _, _) in params]
458

    
459

    
460
class BaseOpCode(outils.ValidatedSlots):
461
  """A simple serializable object.
462

463
  This object serves as a parent class for OpCode without any custom
464
  field handling.
465

466
  """
467
  # pylint: disable=E1101
468
  # as OP_ID is dynamically defined
469
  __metaclass__ = _AutoOpParamSlots
470

    
471
  def __getstate__(self):
472
    """Generic serializer.
473

474
    This method just returns the contents of the instance as a
475
    dictionary.
476

477
    @rtype:  C{dict}
478
    @return: the instance attributes and their values
479

480
    """
481
    state = {}
482
    for name in self.GetAllSlots():
483
      if hasattr(self, name):
484
        state[name] = getattr(self, name)
485
    return state
486

    
487
  def __setstate__(self, state):
488
    """Generic unserializer.
489

490
    This method just restores from the serialized state the attributes
491
    of the current instance.
492

493
    @param state: the serialized opcode data
494
    @type state:  C{dict}
495

496
    """
497
    if not isinstance(state, dict):
498
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
499
                       type(state))
500

    
501
    for name in self.GetAllSlots():
502
      if name not in state and hasattr(self, name):
503
        delattr(self, name)
504

    
505
    for name in state:
506
      setattr(self, name, state[name])
507

    
508
  @classmethod
509
  def GetAllParams(cls):
510
    """Compute list of all parameters for an opcode.
511

512
    """
513
    slots = []
514
    for parent in cls.__mro__:
515
      slots.extend(getattr(parent, "OP_PARAMS", []))
516
    return slots
517

    
518
  def Validate(self, set_defaults): # pylint: disable=W0221
519
    """Validate opcode parameters, optionally setting default values.
520

521
    @type set_defaults: bool
522
    @param set_defaults: Whether to set default values
523
    @raise errors.OpPrereqError: When a parameter value doesn't match
524
                                 requirements
525

526
    """
527
    for (attr_name, default, test, _) in self.GetAllParams():
528
      assert test == ht.NoType or callable(test)
529

    
530
      if not hasattr(self, attr_name):
531
        if default == ht.NoDefault:
532
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
533
                                     (self.OP_ID, attr_name),
534
                                     errors.ECODE_INVAL)
535
        elif set_defaults:
536
          if callable(default):
537
            dval = default()
538
          else:
539
            dval = default
540
          setattr(self, attr_name, dval)
541

    
542
      if test == ht.NoType:
543
        # no tests here
544
        continue
545

    
546
      if set_defaults or hasattr(self, attr_name):
547
        attr_val = getattr(self, attr_name)
548
        if not test(attr_val):
549
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
550
                        " '%s' expecting type %s",
551
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
552
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
553
                                     (self.OP_ID, attr_name),
554
                                     errors.ECODE_INVAL)
555

    
556

    
557
def _BuildJobDepCheck(relative):
558
  """Builds check for job dependencies (L{DEPEND_ATTR}).
559

560
  @type relative: bool
561
  @param relative: Whether to accept relative job IDs (negative)
562
  @rtype: callable
563

564
  """
565
  if relative:
566
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
567
  else:
568
    job_id = ht.TJobId
569

    
570
  job_dep = \
571
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
572
            ht.TIsLength(2),
573
            ht.TItems([job_id,
574
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
575

    
576
  return ht.TMaybeListOf(job_dep)
577

    
578

    
579
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
580

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

    
590
#: Result containing only list of submitted jobs
591
TJobIdListOnly = ht.TStrictDict(True, True, {
592
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
593
  })
594

    
595

    
596
class OpCode(BaseOpCode):
597
  """Abstract OpCode.
598

599
  This is the root of the actual OpCode hierarchy. All clases derived
600
  from this class should override OP_ID.
601

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

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

    
637
  def __getstate__(self):
638
    """Specialized getstate for opcodes.
639

640
    This method adds to the state dictionary the OP_ID of the class,
641
    so that on unload we can identify the correct class for
642
    instantiating the opcode.
643

644
    @rtype:   C{dict}
645
    @return:  the state as a dictionary
646

647
    """
648
    data = BaseOpCode.__getstate__(self)
649
    data["OP_ID"] = self.OP_ID
650
    return data
651

    
652
  @classmethod
653
  def LoadOpCode(cls, data):
654
    """Generic load opcode method.
655

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

660
    @type data:  C{dict}
661
    @param data: the serialized opcode
662

663
    """
664
    if not isinstance(data, dict):
665
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
666
    if "OP_ID" not in data:
667
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
668
    op_id = data["OP_ID"]
669
    op_class = None
670
    if op_id in OP_MAPPING:
671
      op_class = OP_MAPPING[op_id]
672
    else:
673
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
674
                       op_id)
675
    op = op_class()
676
    new_data = data.copy()
677
    del new_data["OP_ID"]
678
    op.__setstate__(new_data)
679
    return op
680

    
681
  def Summary(self):
682
    """Generates a summary description of this opcode.
683

684
    The summary is the value of the OP_ID attribute (without the "OP_"
685
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
686
    defined; this field should allow to easily identify the operation
687
    (for an instance creation job, e.g., it would be the instance
688
    name).
689

690
    """
691
    assert self.OP_ID is not None and len(self.OP_ID) > 3
692
    # all OP_ID start with OP_, we remove that
693
    txt = self.OP_ID[3:]
694
    field_name = getattr(self, "OP_DSC_FIELD", None)
695
    if field_name:
696
      field_value = getattr(self, field_name, None)
697
      field_formatter = getattr(self, "OP_DSC_FORMATTER", None)
698
      if callable(field_formatter):
699
        field_value = field_formatter(field_value)
700
      elif isinstance(field_value, (list, tuple)):
701
        field_value = ",".join(str(i) for i in field_value)
702
      txt = "%s(%s)" % (txt, field_value)
703
    return txt
704

    
705
  def TinySummary(self):
706
    """Generates a compact summary description of the opcode.
707

708
    """
709
    assert self.OP_ID.startswith("OP_")
710

    
711
    text = self.OP_ID[3:]
712

    
713
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
714
      if text.startswith(prefix):
715
        return supplement + text[len(prefix):]
716

    
717
    return text
718

    
719

    
720
# cluster opcodes
721

    
722
class OpClusterPostInit(OpCode):
723
  """Post cluster initialization.
724

725
  This opcode does not touch the cluster at all. Its purpose is to run hooks
726
  after the cluster has been initialized.
727

728
  """
729
  OP_RESULT = ht.TBool
730

    
731

    
732
class OpClusterDestroy(OpCode):
733
  """Destroy the cluster.
734

735
  This opcode has no other parameters. All the state is irreversibly
736
  lost after the execution of this opcode.
737

738
  """
739
  OP_RESULT = ht.TNonEmptyString
740

    
741

    
742
class OpClusterQuery(OpCode):
743
  """Query cluster information."""
744
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
745

    
746

    
747
class OpClusterVerify(OpCode):
748
  """Submits all jobs necessary to verify the cluster.
749

750
  """
751
  OP_PARAMS = [
752
    _PDebugSimulateErrors,
753
    _PErrorCodes,
754
    _PSkipChecks,
755
    _PIgnoreErrors,
756
    _PVerbose,
757
    ("group_name", None, ht.TMaybeString, "Group to verify"),
758
    ]
759
  OP_RESULT = TJobIdListOnly
760

    
761

    
762
class OpClusterVerifyConfig(OpCode):
763
  """Verify the cluster config.
764

765
  """
766
  OP_PARAMS = [
767
    _PDebugSimulateErrors,
768
    _PErrorCodes,
769
    _PIgnoreErrors,
770
    _PVerbose,
771
    ]
772
  OP_RESULT = ht.TBool
773

    
774

    
775
class OpClusterVerifyGroup(OpCode):
776
  """Run verify on a node group from the cluster.
777

778
  @type skip_checks: C{list}
779
  @ivar skip_checks: steps to be skipped from the verify process; this
780
                     needs to be a subset of
781
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
782
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
783

784
  """
785
  OP_DSC_FIELD = "group_name"
786
  OP_PARAMS = [
787
    _PGroupName,
788
    _PDebugSimulateErrors,
789
    _PErrorCodes,
790
    _PSkipChecks,
791
    _PIgnoreErrors,
792
    _PVerbose,
793
    ]
794
  OP_RESULT = ht.TBool
795

    
796

    
797
class OpClusterVerifyDisks(OpCode):
798
  """Verify the cluster disks.
799

800
  """
801
  OP_RESULT = TJobIdListOnly
802

    
803

    
804
class OpGroupVerifyDisks(OpCode):
805
  """Verifies the status of all disks in a node group.
806

807
  Result: a tuple of three elements:
808
    - dict of node names with issues (values: error msg)
809
    - list of instances with degraded disks (that should be activated)
810
    - dict of instances with missing logical volumes (values: (node, vol)
811
      pairs with details about the missing volumes)
812

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

818
  Note that only instances that are drbd-based are taken into
819
  consideration. This might need to be revisited in the future.
820

821
  """
822
  OP_DSC_FIELD = "group_name"
823
  OP_PARAMS = [
824
    _PGroupName,
825
    ]
826
  OP_RESULT = \
827
    ht.TAnd(ht.TIsLength(3),
828
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
829
                       ht.TListOf(ht.TString),
830
                       ht.TDictOf(ht.TString,
831
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
832

    
833

    
834
class OpClusterRepairDiskSizes(OpCode):
835
  """Verify the disk sizes of the instances and fixes configuration
836
  mimatches.
837

838
  Parameters: optional instances list, in case we want to restrict the
839
  checks to only a subset of the instances.
840

841
  Result: a list of tuples, (instance, disk, new-size) for changed
842
  configurations.
843

844
  In normal operation, the list should be empty.
845

846
  @type instances: list
847
  @ivar instances: the list of instances to check, or empty for all instances
848

849
  """
850
  OP_PARAMS = [
851
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
852
    ]
853
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
854
                                 ht.TItems([ht.TNonEmptyString,
855
                                            ht.TNonNegativeInt,
856
                                            ht.TNonNegativeInt])))
857

    
858

    
859
class OpClusterConfigQuery(OpCode):
860
  """Query cluster configuration values."""
861
  OP_PARAMS = [
862
    _POutputFields,
863
    ]
864
  OP_RESULT = ht.TListOf(ht.TAny)
865

    
866

    
867
class OpClusterRename(OpCode):
868
  """Rename the cluster.
869

870
  @type name: C{str}
871
  @ivar name: The new name of the cluster. The name and/or the master IP
872
              address will be changed to match the new name and its IP
873
              address.
874

875
  """
876
  OP_DSC_FIELD = "name"
877
  OP_PARAMS = [
878
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
879
    ]
880
  OP_RESULT = ht.TNonEmptyString
881

    
882

    
883
class OpClusterSetParams(OpCode):
884
  """Change the parameters of the cluster.
885

886
  @type vg_name: C{str} or C{None}
887
  @ivar vg_name: The new volume group name or None to disable LVM usage.
888

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

    
950

    
951
class OpClusterRedistConf(OpCode):
952
  """Force a full push of the cluster configuration.
953

954
  """
955
  OP_RESULT = ht.TNone
956

    
957

    
958
class OpClusterActivateMasterIp(OpCode):
959
  """Activate the master IP on the master node.
960

961
  """
962
  OP_RESULT = ht.TNone
963

    
964

    
965
class OpClusterDeactivateMasterIp(OpCode):
966
  """Deactivate the master IP on the master node.
967

968
  """
969
  OP_RESULT = ht.TNone
970

    
971

    
972
class OpQuery(OpCode):
973
  """Query for resources/items.
974

975
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
976
  @ivar fields: List of fields to retrieve
977
  @ivar qfilter: Query filter
978

979
  """
980
  OP_DSC_FIELD = "what"
981
  OP_PARAMS = [
982
    _PQueryWhat,
983
    _PUseLocking,
984
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
985
     "Requested fields"),
986
    ("qfilter", None, ht.TMaybe(ht.TList),
987
     "Query filter"),
988
    ]
989
  OP_RESULT = \
990
    _GenerateObjectTypeCheck(objects.QueryResponse, {
991
      "fields": ht.TListOf(_TQueryFieldDef),
992
      "data": _TQueryResult,
993
      })
994

    
995

    
996
class OpQueryFields(OpCode):
997
  """Query for available resource/item fields.
998

999
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
1000
  @ivar fields: List of fields to retrieve
1001

1002
  """
1003
  OP_DSC_FIELD = "what"
1004
  OP_PARAMS = [
1005
    _PQueryWhat,
1006
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
1007
     "Requested fields; if not given, all are returned"),
1008
    ]
1009
  OP_RESULT = \
1010
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
1011
      "fields": ht.TListOf(_TQueryFieldDef),
1012
      })
1013

    
1014

    
1015
class OpOobCommand(OpCode):
1016
  """Interact with OOB."""
1017
  OP_PARAMS = [
1018
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1019
     "List of nodes to run the OOB command against"),
1020
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1021
     "OOB command to be run"),
1022
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1023
     "Timeout before the OOB helper will be terminated"),
1024
    ("ignore_status", False, ht.TBool,
1025
     "Ignores the node offline status for power off"),
1026
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1027
     "Time in seconds to wait between powering on nodes"),
1028
    ]
1029
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1030
  OP_RESULT = _TQueryResult
1031

    
1032

    
1033
class OpRestrictedCommand(OpCode):
1034
  """Runs a restricted command on node(s).
1035

1036
  """
1037
  OP_PARAMS = [
1038
    _PUseLocking,
1039
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1040
     "Nodes on which the command should be run (at least one)"),
1041
    ("command", ht.NoDefault, ht.TNonEmptyString,
1042
     "Command name (no parameters)"),
1043
    ]
1044

    
1045
  _RESULT_ITEMS = [
1046
    ht.Comment("success")(ht.TBool),
1047
    ht.Comment("output or error message")(ht.TString),
1048
    ]
1049

    
1050
  OP_RESULT = \
1051
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1052
                       ht.TItems(_RESULT_ITEMS)))
1053

    
1054

    
1055
# node opcodes
1056

    
1057
class OpNodeRemove(OpCode):
1058
  """Remove a node.
1059

1060
  @type node_name: C{str}
1061
  @ivar node_name: The name of the node to remove. If the node still has
1062
                   instances on it, the operation will fail.
1063

1064
  """
1065
  OP_DSC_FIELD = "node_name"
1066
  OP_PARAMS = [
1067
    _PNodeName,
1068
    ]
1069
  OP_RESULT = ht.TNone
1070

    
1071

    
1072
class OpNodeAdd(OpCode):
1073
  """Add a node to the cluster.
1074

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

1099
  """
1100
  OP_DSC_FIELD = "node_name"
1101
  OP_PARAMS = [
1102
    _PNodeName,
1103
    _PHvState,
1104
    _PDiskState,
1105
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1106
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1107
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1108
    ("group", None, ht.TMaybeString, "Initial node group"),
1109
    ("master_capable", None, ht.TMaybeBool,
1110
     "Whether node can become master or master candidate"),
1111
    ("vm_capable", None, ht.TMaybeBool,
1112
     "Whether node can host instances"),
1113
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1114
    ]
1115
  OP_RESULT = ht.TNone
1116

    
1117

    
1118
class OpNodeQuery(OpCode):
1119
  """Compute the list of nodes."""
1120
  OP_PARAMS = [
1121
    _POutputFields,
1122
    _PUseLocking,
1123
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1124
     "Empty list to query all nodes, node names otherwise"),
1125
    ]
1126
  OP_RESULT = _TOldQueryResult
1127

    
1128

    
1129
class OpNodeQueryvols(OpCode):
1130
  """Get list of volumes on node."""
1131
  OP_PARAMS = [
1132
    _POutputFields,
1133
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1134
     "Empty list to query all nodes, node names otherwise"),
1135
    ]
1136
  OP_RESULT = ht.TListOf(ht.TAny)
1137

    
1138

    
1139
class OpNodeQueryStorage(OpCode):
1140
  """Get information on storage for node(s)."""
1141
  OP_PARAMS = [
1142
    _POutputFields,
1143
    _PStorageType,
1144
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1145
    ("name", None, ht.TMaybeString, "Storage name"),
1146
    ]
1147
  OP_RESULT = _TOldQueryResult
1148

    
1149

    
1150
class OpNodeModifyStorage(OpCode):
1151
  """Modifies the properies of a storage unit"""
1152
  OP_DSC_FIELD = "node_name"
1153
  OP_PARAMS = [
1154
    _PNodeName,
1155
    _PStorageType,
1156
    _PStorageName,
1157
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1158
    ]
1159
  OP_RESULT = ht.TNone
1160

    
1161

    
1162
class OpRepairNodeStorage(OpCode):
1163
  """Repairs the volume group on a node."""
1164
  OP_DSC_FIELD = "node_name"
1165
  OP_PARAMS = [
1166
    _PNodeName,
1167
    _PStorageType,
1168
    _PStorageName,
1169
    _PIgnoreConsistency,
1170
    ]
1171
  OP_RESULT = ht.TNone
1172

    
1173

    
1174
class OpNodeSetParams(OpCode):
1175
  """Change the parameters of a node."""
1176
  OP_DSC_FIELD = "node_name"
1177
  OP_PARAMS = [
1178
    _PNodeName,
1179
    _PForce,
1180
    _PHvState,
1181
    _PDiskState,
1182
    ("master_candidate", None, ht.TMaybeBool,
1183
     "Whether the node should become a master candidate"),
1184
    ("offline", None, ht.TMaybeBool,
1185
     "Whether the node should be marked as offline"),
1186
    ("drained", None, ht.TMaybeBool,
1187
     "Whether the node should be marked as drained"),
1188
    ("auto_promote", False, ht.TBool,
1189
     "Whether node(s) should be promoted to master candidate if necessary"),
1190
    ("master_capable", None, ht.TMaybeBool,
1191
     "Denote whether node can become master or master candidate"),
1192
    ("vm_capable", None, ht.TMaybeBool,
1193
     "Denote whether node can host instances"),
1194
    ("secondary_ip", None, ht.TMaybeString,
1195
     "Change node's secondary IP address"),
1196
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1197
    ("powered", None, ht.TMaybeBool,
1198
     "Whether the node should be marked as powered"),
1199
    ]
1200
  OP_RESULT = _TSetParamsResult
1201

    
1202

    
1203
class OpNodePowercycle(OpCode):
1204
  """Tries to powercycle a node."""
1205
  OP_DSC_FIELD = "node_name"
1206
  OP_PARAMS = [
1207
    _PNodeName,
1208
    _PForce,
1209
    ]
1210
  OP_RESULT = ht.TMaybeString
1211

    
1212

    
1213
class OpNodeMigrate(OpCode):
1214
  """Migrate all instances from a node."""
1215
  OP_DSC_FIELD = "node_name"
1216
  OP_PARAMS = [
1217
    _PNodeName,
1218
    _PMigrationMode,
1219
    _PMigrationLive,
1220
    _PMigrationTargetNode,
1221
    _PAllowRuntimeChgs,
1222
    _PIgnoreIpolicy,
1223
    _PIAllocFromDesc("Iallocator for deciding the target node"
1224
                     " for shared-storage instances"),
1225
    ]
1226
  OP_RESULT = TJobIdListOnly
1227

    
1228

    
1229
class OpNodeEvacuate(OpCode):
1230
  """Evacuate instances off a number of nodes."""
1231
  OP_DSC_FIELD = "node_name"
1232
  OP_PARAMS = [
1233
    _PEarlyRelease,
1234
    _PNodeName,
1235
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1236
    _PIAllocFromDesc("Iallocator for computing solution"),
1237
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1238
     "Node evacuation mode"),
1239
    ]
1240
  OP_RESULT = TJobIdListOnly
1241

    
1242

    
1243
# instance opcodes
1244

    
1245
class OpInstanceCreate(OpCode):
1246
  """Create an instance.
1247

1248
  @ivar instance_name: Instance name
1249
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1250
  @ivar source_handshake: Signed handshake from source (remote import only)
1251
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1252
  @ivar source_instance_name: Previous name of instance (remote import only)
1253
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1254
    (remote import only)
1255

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

    
1319

    
1320
class OpInstanceMultiAlloc(OpCode):
1321
  """Allocates multiple instances.
1322

1323
  """
1324
  OP_PARAMS = [
1325
    _POpportunisticLocking,
1326
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1327
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1328
     "List of instance create opcodes describing the instances to allocate"),
1329
    ]
1330
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1331
  ALLOCATABLE_KEY = "allocatable"
1332
  FAILED_KEY = "allocatable"
1333
  OP_RESULT = ht.TStrictDict(True, True, {
1334
    constants.JOB_IDS_KEY: _JOB_LIST,
1335
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1336
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1337
    })
1338

    
1339
  def __getstate__(self):
1340
    """Generic serializer.
1341

1342
    """
1343
    state = OpCode.__getstate__(self)
1344
    if hasattr(self, "instances"):
1345
      # pylint: disable=E1101
1346
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1347
    return state
1348

    
1349
  def __setstate__(self, state):
1350
    """Generic unserializer.
1351

1352
    This method just restores from the serialized state the attributes
1353
    of the current instance.
1354

1355
    @param state: the serialized opcode data
1356
    @type state: C{dict}
1357

1358
    """
1359
    if not isinstance(state, dict):
1360
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1361
                       type(state))
1362

    
1363
    if "instances" in state:
1364
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1365

    
1366
    return OpCode.__setstate__(self, state)
1367

    
1368
  def Validate(self, set_defaults):
1369
    """Validates this opcode.
1370

1371
    We do this recursively.
1372

1373
    """
1374
    OpCode.Validate(self, set_defaults)
1375

    
1376
    for inst in self.instances: # pylint: disable=E1101
1377
      inst.Validate(set_defaults)
1378

    
1379

    
1380
class OpInstanceReinstall(OpCode):
1381
  """Reinstall an instance's OS."""
1382
  OP_DSC_FIELD = "instance_name"
1383
  OP_PARAMS = [
1384
    _PInstanceName,
1385
    _PForceVariant,
1386
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1387
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1388
    ]
1389
  OP_RESULT = ht.TNone
1390

    
1391

    
1392
class OpInstanceRemove(OpCode):
1393
  """Remove an instance."""
1394
  OP_DSC_FIELD = "instance_name"
1395
  OP_PARAMS = [
1396
    _PInstanceName,
1397
    _PShutdownTimeout,
1398
    ("ignore_failures", False, ht.TBool,
1399
     "Whether to ignore failures during removal"),
1400
    ]
1401
  OP_RESULT = ht.TNone
1402

    
1403

    
1404
class OpInstanceRename(OpCode):
1405
  """Rename an instance."""
1406
  OP_PARAMS = [
1407
    _PInstanceName,
1408
    _PNameCheck,
1409
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1410
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1411
    ]
1412
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1413

    
1414

    
1415
class OpInstanceStartup(OpCode):
1416
  """Startup an instance."""
1417
  OP_DSC_FIELD = "instance_name"
1418
  OP_PARAMS = [
1419
    _PInstanceName,
1420
    _PForce,
1421
    _PIgnoreOfflineNodes,
1422
    ("hvparams", ht.EmptyDict, ht.TDict,
1423
     "Temporary hypervisor parameters, hypervisor-dependent"),
1424
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1425
    _PNoRemember,
1426
    _PStartupPaused,
1427
    ]
1428
  OP_RESULT = ht.TNone
1429

    
1430

    
1431
class OpInstanceShutdown(OpCode):
1432
  """Shutdown an instance."""
1433
  OP_DSC_FIELD = "instance_name"
1434
  OP_PARAMS = [
1435
    _PInstanceName,
1436
    _PForce,
1437
    _PIgnoreOfflineNodes,
1438
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1439
     "How long to wait for instance to shut down"),
1440
    _PNoRemember,
1441
    ]
1442
  OP_RESULT = ht.TNone
1443

    
1444

    
1445
class OpInstanceReboot(OpCode):
1446
  """Reboot an instance."""
1447
  OP_DSC_FIELD = "instance_name"
1448
  OP_PARAMS = [
1449
    _PInstanceName,
1450
    _PShutdownTimeout,
1451
    ("ignore_secondaries", False, ht.TBool,
1452
     "Whether to start the instance even if secondary disks are failing"),
1453
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1454
     "How to reboot instance"),
1455
    ]
1456
  OP_RESULT = ht.TNone
1457

    
1458

    
1459
class OpInstanceReplaceDisks(OpCode):
1460
  """Replace the disks of an instance."""
1461
  OP_DSC_FIELD = "instance_name"
1462
  OP_PARAMS = [
1463
    _PInstanceName,
1464
    _PEarlyRelease,
1465
    _PIgnoreIpolicy,
1466
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1467
     "Replacement mode"),
1468
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1469
     "Disk indexes"),
1470
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1471
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1472
    ]
1473
  OP_RESULT = ht.TNone
1474

    
1475

    
1476
class OpInstanceFailover(OpCode):
1477
  """Failover an instance."""
1478
  OP_DSC_FIELD = "instance_name"
1479
  OP_PARAMS = [
1480
    _PInstanceName,
1481
    _PShutdownTimeout,
1482
    _PIgnoreConsistency,
1483
    _PMigrationTargetNode,
1484
    _PIgnoreIpolicy,
1485
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1486
                     " shared-storage instances"),
1487
    ]
1488
  OP_RESULT = ht.TNone
1489

    
1490

    
1491
class OpInstanceMigrate(OpCode):
1492
  """Migrate an instance.
1493

1494
  This migrates (without shutting down an instance) to its secondary
1495
  node.
1496

1497
  @ivar instance_name: the name of the instance
1498
  @ivar mode: the migration mode (live, non-live or None for auto)
1499

1500
  """
1501
  OP_DSC_FIELD = "instance_name"
1502
  OP_PARAMS = [
1503
    _PInstanceName,
1504
    _PMigrationMode,
1505
    _PMigrationLive,
1506
    _PMigrationTargetNode,
1507
    _PAllowRuntimeChgs,
1508
    _PIgnoreIpolicy,
1509
    ("cleanup", False, ht.TBool,
1510
     "Whether a previously failed migration should be cleaned up"),
1511
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1512
                     " shared-storage instances"),
1513
    ("allow_failover", False, ht.TBool,
1514
     "Whether we can fallback to failover if migration is not possible"),
1515
    ]
1516
  OP_RESULT = ht.TNone
1517

    
1518

    
1519
class OpInstanceMove(OpCode):
1520
  """Move an instance.
1521

1522
  This move (with shutting down an instance and data copying) to an
1523
  arbitrary node.
1524

1525
  @ivar instance_name: the name of the instance
1526
  @ivar target_node: the destination node
1527

1528
  """
1529
  OP_DSC_FIELD = "instance_name"
1530
  OP_PARAMS = [
1531
    _PInstanceName,
1532
    _PShutdownTimeout,
1533
    _PIgnoreIpolicy,
1534
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1535
    _PIgnoreConsistency,
1536
    ]
1537
  OP_RESULT = ht.TNone
1538

    
1539

    
1540
class OpInstanceConsole(OpCode):
1541
  """Connect to an instance's console."""
1542
  OP_DSC_FIELD = "instance_name"
1543
  OP_PARAMS = [
1544
    _PInstanceName,
1545
    ]
1546
  OP_RESULT = ht.TDict
1547

    
1548

    
1549
class OpInstanceActivateDisks(OpCode):
1550
  """Activate an instance's disks."""
1551
  OP_DSC_FIELD = "instance_name"
1552
  OP_PARAMS = [
1553
    _PInstanceName,
1554
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1555
    _PWaitForSyncFalse,
1556
    ]
1557
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1558
                                 ht.TItems([ht.TNonEmptyString,
1559
                                            ht.TNonEmptyString,
1560
                                            ht.TNonEmptyString])))
1561

    
1562

    
1563
class OpInstanceDeactivateDisks(OpCode):
1564
  """Deactivate an instance's disks."""
1565
  OP_DSC_FIELD = "instance_name"
1566
  OP_PARAMS = [
1567
    _PInstanceName,
1568
    _PForce,
1569
    ]
1570
  OP_RESULT = ht.TNone
1571

    
1572

    
1573
class OpInstanceRecreateDisks(OpCode):
1574
  """Recreate an instance's disks."""
1575
  _TDiskChanges = \
1576
    ht.TAnd(ht.TIsLength(2),
1577
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1578
                       ht.Comment("Parameters")(_TDiskParams)]))
1579

    
1580
  OP_DSC_FIELD = "instance_name"
1581
  OP_PARAMS = [
1582
    _PInstanceName,
1583
    ("disks", ht.EmptyList,
1584
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1585
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1586
     " index and a possibly empty dictionary with disk parameter changes"),
1587
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1588
     "New instance nodes, if relocation is desired"),
1589
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1590
    ]
1591
  OP_RESULT = ht.TNone
1592

    
1593

    
1594
class OpInstanceQuery(OpCode):
1595
  """Compute the list of instances."""
1596
  OP_PARAMS = [
1597
    _POutputFields,
1598
    _PUseLocking,
1599
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1600
     "Empty list to query all instances, instance names otherwise"),
1601
    ]
1602
  OP_RESULT = _TOldQueryResult
1603

    
1604

    
1605
class OpInstanceQueryData(OpCode):
1606
  """Compute the run-time status of instances."""
1607
  OP_PARAMS = [
1608
    _PUseLocking,
1609
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1610
     "Instance names"),
1611
    ("static", False, ht.TBool,
1612
     "Whether to only return configuration data without querying"
1613
     " nodes"),
1614
    ]
1615
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1616

    
1617

    
1618
def _TestInstSetParamsModList(fn):
1619
  """Generates a check for modification lists.
1620

1621
  """
1622
  mod_item_fn = \
1623
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1624
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1625
      ht.Comment("Device index, can be negative, e.g. -1 for last disk")
1626
                 (ht.TOr(ht.TInt, ht.TString)),
1627
      fn,
1628
      ]))
1629

    
1630
  return ht.TListOf(mod_item_fn)
1631

    
1632

    
1633
class OpInstanceSetParams(OpCode):
1634
  """Change the parameters of an instance.
1635

1636
  """
1637
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1638
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1639

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

    
1673

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

    
1688

    
1689
class OpInstanceChangeGroup(OpCode):
1690
  """Moves an instance to another node group."""
1691
  OP_DSC_FIELD = "instance_name"
1692
  OP_PARAMS = [
1693
    _PInstanceName,
1694
    _PEarlyRelease,
1695
    _PIAllocFromDesc("Iallocator for computing solution"),
1696
    _PTargetGroups,
1697
    ]
1698
  OP_RESULT = TJobIdListOnly
1699

    
1700

    
1701
# Node group opcodes
1702

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

    
1718

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

    
1730

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

    
1740

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

    
1755

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

    
1764

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

    
1773

    
1774
class OpGroupEvacuate(OpCode):
1775
  """Evacuate a node group in the cluster."""
1776
  OP_DSC_FIELD = "group_name"
1777
  OP_PARAMS = [
1778
    _PGroupName,
1779
    _PEarlyRelease,
1780
    _PIAllocFromDesc("Iallocator for computing solution"),
1781
    _PTargetGroups,
1782
    ]
1783
  OP_RESULT = TJobIdListOnly
1784

    
1785

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

    
1796

    
1797
# ExtStorage opcodes
1798
class OpExtStorageDiagnose(OpCode):
1799
  """Compute the list of external storage providers."""
1800
  OP_PARAMS = [
1801
    _POutputFields,
1802
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1803
     "Which ExtStorage Provider to diagnose"),
1804
    ]
1805
  OP_RESULT = _TOldQueryResult
1806

    
1807

    
1808
# Exports opcodes
1809
class OpBackupQuery(OpCode):
1810
  """Compute the list of exported images."""
1811
  OP_PARAMS = [
1812
    _PUseLocking,
1813
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1814
     "Empty list to query all nodes, node names otherwise"),
1815
    ]
1816
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1817
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1818
                                ht.TListOf(ht.TNonEmptyString)))
1819

    
1820

    
1821
class OpBackupPrepare(OpCode):
1822
  """Prepares an instance export.
1823

1824
  @ivar instance_name: Instance name
1825
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1826

1827
  """
1828
  OP_DSC_FIELD = "instance_name"
1829
  OP_PARAMS = [
1830
    _PInstanceName,
1831
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1832
     "Export mode"),
1833
    ]
1834
  OP_RESULT = ht.TMaybeDict
1835

    
1836

    
1837
class OpBackupExport(OpCode):
1838
  """Export an instance.
1839

1840
  For local exports, the export destination is the node name. For
1841
  remote exports, the export destination is a list of tuples, each
1842
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1843
  salt. The HMAC is calculated using the cluster domain secret over
1844
  the value "${index}:${hostname}:${port}". The destination X509 CA
1845
  must be a signed certificate.
1846

1847
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1848
  @ivar target_node: Export destination
1849
  @ivar x509_key_name: X509 key to use (remote export only)
1850
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1851
                             only)
1852

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

    
1880

    
1881
class OpBackupRemove(OpCode):
1882
  """Remove an instance's export."""
1883
  OP_DSC_FIELD = "instance_name"
1884
  OP_PARAMS = [
1885
    _PInstanceName,
1886
    ]
1887
  OP_RESULT = ht.TNone
1888

    
1889

    
1890
# Tags opcodes
1891
class OpTagsGet(OpCode):
1892
  """Returns the tags of the given object."""
1893
  OP_DSC_FIELD = "name"
1894
  OP_PARAMS = [
1895
    _PTagKind,
1896
    # Not using _PUseLocking as the default is different for historical reasons
1897
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1898
    # Name is only meaningful for nodes and instances
1899
    ("name", ht.NoDefault, ht.TMaybeString,
1900
     "Name of object to retrieve tags from"),
1901
    ]
1902
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1903

    
1904

    
1905
class OpTagsSearch(OpCode):
1906
  """Searches the tags in the cluster for a given pattern."""
1907
  OP_DSC_FIELD = "pattern"
1908
  OP_PARAMS = [
1909
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1910
     "Search pattern (regular expression)"),
1911
    ]
1912
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1913
    ht.TNonEmptyString,
1914
    ht.TNonEmptyString,
1915
    ])))
1916

    
1917

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

    
1929

    
1930
class OpTagsDel(OpCode):
1931
  """Remove a list of tags from a given object."""
1932
  OP_PARAMS = [
1933
    _PTagKind,
1934
    _PTags,
1935
    # Name is only meaningful for groups, nodes and instances
1936
    ("name", ht.NoDefault, ht.TMaybeString,
1937
     "Name of object where tag(s) should be deleted"),
1938
    ]
1939
  OP_RESULT = ht.TNone
1940

    
1941

    
1942
# Test opcodes
1943
class OpTestDelay(OpCode):
1944
  """Sleeps for a configured amount of time.
1945

1946
  This is used just for debugging and testing.
1947

1948
  Parameters:
1949
    - duration: the time to sleep
1950
    - on_master: if true, sleep on the master
1951
    - on_nodes: list of nodes in which to sleep
1952

1953
  If the on_master parameter is true, it will execute a sleep on the
1954
  master (before any node sleep).
1955

1956
  If the on_nodes list is not empty, it will sleep on those nodes
1957
  (after the sleep on the master, if that is enabled).
1958

1959
  As an additional feature, the case of duration < 0 will be reported
1960
  as an execution error, so this opcode can be used as a failure
1961
  generator. The case of duration == 0 will not be treated specially.
1962

1963
  """
1964
  OP_DSC_FIELD = "duration"
1965
  OP_PARAMS = [
1966
    ("duration", ht.NoDefault, ht.TNumber, None),
1967
    ("on_master", True, ht.TBool, None),
1968
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1969
    ("repeat", 0, ht.TNonNegativeInt, None),
1970
    ]
1971

    
1972
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
1973
    """Custom formatter for duration.
1974

1975
    """
1976
    try:
1977
      v = float(value)
1978
    except TypeError:
1979
      v = value
1980
    return str(v)
1981

    
1982

    
1983
class OpTestAllocator(OpCode):
1984
  """Allocator framework testing.
1985

1986
  This opcode has two modes:
1987
    - gather and return allocator input for a given mode (allocate new
1988
      or replace secondary) and a given instance definition (direction
1989
      'in')
1990
    - run a selected allocator for a given operation (as above) and
1991
      return the allocator output (direction 'out')
1992

1993
  """
1994
  OP_DSC_FIELD = "iallocator"
1995
  OP_PARAMS = [
1996
    ("direction", ht.NoDefault,
1997
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1998
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1999
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2000
    ("nics", ht.NoDefault,
2001
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2002
                                            constants.INIC_IP,
2003
                                            "bridge"]),
2004
                                ht.TMaybeString)),
2005
     None),
2006
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2007
    ("hypervisor", None, ht.TMaybeString, None),
2008
    _PIAllocFromDesc(None),
2009
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2010
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2011
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2012
    ("os", None, ht.TMaybeString, None),
2013
    ("disk_template", None, ht.TMaybeString, None),
2014
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2015
    ("evac_mode", None,
2016
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2017
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2018
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2019
    ("count", 1, ht.TNonNegativeInt, None),
2020
    ]
2021

    
2022

    
2023
class OpTestJqueue(OpCode):
2024
  """Utility opcode to test some aspects of the job queue.
2025

2026
  """
2027
  OP_PARAMS = [
2028
    ("notify_waitlock", False, ht.TBool, None),
2029
    ("notify_exec", False, ht.TBool, None),
2030
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2031
    ("fail", False, ht.TBool, None),
2032
    ]
2033

    
2034

    
2035
class OpTestDummy(OpCode):
2036
  """Utility opcode used by unittests.
2037

2038
  """
2039
  OP_PARAMS = [
2040
    ("result", ht.NoDefault, ht.NoType, None),
2041
    ("messages", ht.NoDefault, ht.NoType, None),
2042
    ("fail", ht.NoDefault, ht.NoType, None),
2043
    ("submit_jobs", None, ht.NoType, None),
2044
    ]
2045
  WITH_LU = False
2046

    
2047

    
2048
# Network opcodes
2049
# Add a new network in the cluster
2050
class OpNetworkAdd(OpCode):
2051
  """Add an IP network to the cluster."""
2052
  OP_DSC_FIELD = "network_name"
2053
  OP_PARAMS = [
2054
    _PNetworkName,
2055
    ("network", None, ht.TMaybe(_TIpNetwork4), "IPv4 subnet"),
2056
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2057
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2058
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2059
    ("mac_prefix", None, ht.TMaybeString,
2060
     "MAC address prefix that overrides cluster one"),
2061
    ("add_reserved_ips", None, _TMaybeAddr4List,
2062
     "Which IP addresses to reserve"),
2063
    ("conflicts_check", True, ht.TBool,
2064
     "Whether to check for conflicting IP addresses"),
2065
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2066
    ]
2067
  OP_RESULT = ht.TNone
2068

    
2069

    
2070
class OpNetworkRemove(OpCode):
2071
  """Remove an existing network from the cluster.
2072
     Must not be connected to any nodegroup.
2073

2074
  """
2075
  OP_DSC_FIELD = "network_name"
2076
  OP_PARAMS = [
2077
    _PNetworkName,
2078
    _PForce,
2079
    ]
2080
  OP_RESULT = ht.TNone
2081

    
2082

    
2083
class OpNetworkSetParams(OpCode):
2084
  """Modify Network's parameters except for IPv4 subnet"""
2085
  OP_DSC_FIELD = "network_name"
2086
  OP_PARAMS = [
2087
    _PNetworkName,
2088
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2089
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2090
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2091
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2092
     "MAC address prefix that overrides cluster one"),
2093
    ("add_reserved_ips", None, _TMaybeAddr4List,
2094
     "Which external IP addresses to reserve"),
2095
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2096
     "Which external IP addresses to release"),
2097
    ]
2098
  OP_RESULT = ht.TNone
2099

    
2100

    
2101
class OpNetworkConnect(OpCode):
2102
  """Connect a Network to a specific Nodegroup with the defined netparams
2103
     (mode, link). Nics in this Network will inherit those params.
2104
     Produce errors if a NIC (that its not already assigned to a network)
2105
     has an IP that is contained in the Network this will produce error unless
2106
     --no-conflicts-check is passed.
2107

2108
  """
2109
  OP_DSC_FIELD = "network_name"
2110
  OP_PARAMS = [
2111
    _PGroupName,
2112
    _PNetworkName,
2113
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2114
     "Connectivity mode"),
2115
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2116
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2117
    ]
2118
  OP_RESULT = ht.TNone
2119

    
2120

    
2121
class OpNetworkDisconnect(OpCode):
2122
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2123
     present in the Network unless --no-conficts-check option is passed.
2124

2125
  """
2126
  OP_DSC_FIELD = "network_name"
2127
  OP_PARAMS = [
2128
    _PGroupName,
2129
    _PNetworkName,
2130
    ]
2131
  OP_RESULT = ht.TNone
2132

    
2133

    
2134
class OpNetworkQuery(OpCode):
2135
  """Compute the list of networks."""
2136
  OP_PARAMS = [
2137
    _POutputFields,
2138
    _PUseLocking,
2139
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2140
     "Empty list to query all groups, group names otherwise"),
2141
    ]
2142
  OP_RESULT = _TOldQueryResult
2143

    
2144

    
2145
def _GetOpList():
2146
  """Returns list of all defined opcodes.
2147

2148
  Does not eliminate duplicates by C{OP_ID}.
2149

2150
  """
2151
  return [v for v in globals().values()
2152
          if (isinstance(v, type) and issubclass(v, OpCode) and
2153
              hasattr(v, "OP_ID") and v is not OpCode)]
2154

    
2155

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