opcodes.py 37 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
#
Iustin Pop's avatar
Iustin Pop committed
2
3
#

4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
Iustin Pop's avatar
Iustin Pop committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""OpCodes module

This module implements the data structures which define the cluster
operations - the so-called opcodes.

Iustin Pop's avatar
Iustin Pop committed
27
28
Every operation which modifies the cluster state is expressed via
opcodes.
Iustin Pop's avatar
Iustin Pop committed
29
30
31
32
33
34
35

"""

# this are practically structures, so disable the message about too
# few public methods:
# pylint: disable-msg=R0903

36
import logging
37
import re
38
import operator
39

40
41
42
43
44
45
46
47
from ganeti import constants
from ganeti import errors
from ganeti import ht


# Common opcode attributes

#: output fields for a query operation
48
49
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
                  None)
50
51
52

#: the shutdown timeout
_PShutdownTimeout = ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
53
                     ht.TPositiveInt, None)
54
55

#: the force parameter
56
_PForce = ("force", False, ht.TBool, None)
57
58

#: a required instance name (for single-instance LUs)
59
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString, None)
60
61

#: Whether to ignore offline nodes
62
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool, None)
63
64

#: a required node name (for single-node LUs)
65
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, None)
66
67

#: a required node group name (for single-group LUs)
68
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, None)
69
70
71

#: Migration type (live/non-live)
_PMigrationMode = ("mode", None,
72
73
                   ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
                   None)
74
75

#: Obsolete 'live' migration mode (boolean)
76
_PMigrationLive = ("live", None, ht.TMaybeBool, None)
77
78

#: Tag type
79
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
80
81

#: List of tag strings
82
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
83

84
85
86
#: OP_ID conversion regular expression
_OPID_RE = re.compile("([a-z])([A-Z])")

87
88
89
90
91
92
#: Utility function for L{OpClusterSetParams}
_TestClusterOsList = ht.TOr(ht.TNone,
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
    ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
            ht.TElemOf(constants.DDMS_VALUES)))))

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

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

  @type name: string
  @param name: the class name, as OpXxxYyy
  @rtype: string
  @return: the name in the OP_XXXX_YYYY format

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

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

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

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

  @raise errors.OpPrereqError: when file storage is disabled

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


129
130
131
@ht.WithDesc("CheckFileStorage")
def _CheckFileStorage(value):
  """Ensures file storage is enabled if used.
132
133

  """
134
  if value == constants.DT_FILE:
135
136
137
138
    RequireFileStorage()
  return True


139
140
141
142
_CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
                             _CheckFileStorage)


143
144
145
146
147
148
149
150
151
152
153
154
155
def _CheckStorageType(storage_type):
  """Ensure a given storage type is valid.

  """
  if storage_type not in constants.VALID_STORAGE_TYPES:
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
                               errors.ECODE_INVAL)
  if storage_type == constants.ST_FILE:
    RequireFileStorage()
  return True


#: Storage type parameter
156
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType, None)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174


class _AutoOpParamSlots(type):
  """Meta class for opcode definitions.

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

    @param mcs: The meta class
    @param name: Name of created class
    @param bases: Base classes
    @type attrs: dict
    @param attrs: Class attributes

    """
    assert "__slots__" not in attrs, \
      "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
175
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
176

177
    attrs["OP_ID"] = _NameToId(name)
178
179
180
181
182

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

    # Use parameter names as slots
183
    slots = [pname for (pname, _, _, _) in params]
184
185
186
187
188
189
190
191

    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name

    attrs["__slots__"] = slots

    return type.__new__(mcs, name, bases, attrs)

192

Iustin Pop's avatar
Iustin Pop committed
193
class BaseOpCode(object):
194
195
  """A simple serializable object.

Iustin Pop's avatar
Iustin Pop committed
196
197
198
  This object serves as a parent class for OpCode without any custom
  field handling.

199
  """
200
201
  # pylint: disable-msg=E1101
  # as OP_ID is dynamically defined
202
203
  __metaclass__ = _AutoOpParamSlots

Iustin Pop's avatar
Iustin Pop committed
204
  def __init__(self, **kwargs):
Iustin Pop's avatar
Iustin Pop committed
205
206
207
208
209
210
211
212
    """Constructor for BaseOpCode.

    The constructor takes only keyword arguments and will set
    attributes on this object based on the passed arguments. As such,
    it means that you should not pass arguments which are not in the
    __slots__ attribute for this class.

    """
213
    slots = self._all_slots()
Iustin Pop's avatar
Iustin Pop committed
214
    for key in kwargs:
215
      if key not in slots:
216
        raise TypeError("Object %s doesn't support the parameter '%s'" %
217
                        (self.__class__.__name__, key))
Iustin Pop's avatar
Iustin Pop committed
218
219
      setattr(self, key, kwargs[key])

220
  def __getstate__(self):
Iustin Pop's avatar
Iustin Pop committed
221
222
223
224
225
226
227
228
229
    """Generic serializer.

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

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

    """
230
    state = {}
231
    for name in self._all_slots():
232
233
234
235
236
      if hasattr(self, name):
        state[name] = getattr(self, name)
    return state

  def __setstate__(self, state):
Iustin Pop's avatar
Iustin Pop committed
237
238
239
240
241
242
243
244
245
    """Generic unserializer.

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

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

    """
246
247
248
249
    if not isinstance(state, dict):
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
                       type(state))

250
    for name in self._all_slots():
Iustin Pop's avatar
Iustin Pop committed
251
      if name not in state and hasattr(self, name):
252
253
254
255
256
        delattr(self, name)

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

257
258
259
260
261
262
263
264
265
266
  @classmethod
  def _all_slots(cls):
    """Compute the list of all declared slots for a class.

    """
    slots = []
    for parent in cls.__mro__:
      slots.extend(getattr(parent, "__slots__", []))
    return slots

267
268
269
270
271
272
273
274
275
276
  @classmethod
  def GetAllParams(cls):
    """Compute list of all parameters for an opcode.

    """
    slots = []
    for parent in cls.__mro__:
      slots.extend(getattr(parent, "OP_PARAMS", []))
    return slots

277
278
279
280
281
282
283
284
285
  def Validate(self, set_defaults):
    """Validate opcode parameters, optionally setting default values.

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

    """
286
    for (attr_name, default, test, _) in self.GetAllParams():
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      assert test == ht.NoType or callable(test)

      if not hasattr(self, attr_name):
        if default == ht.NoDefault:
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
                                     (self.OP_ID, attr_name),
                                     errors.ECODE_INVAL)
        elif set_defaults:
          if callable(default):
            dval = default()
          else:
            dval = default
          setattr(self, attr_name, dval)

      if test == ht.NoType:
        # no tests here
        continue

      if set_defaults or hasattr(self, attr_name):
        attr_val = getattr(self, attr_name)
        if not test(attr_val):
          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
                        self.OP_ID, attr_name, type(attr_val), attr_val)
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
                                     (self.OP_ID, attr_name),
                                     errors.ECODE_INVAL)

314

Iustin Pop's avatar
Iustin Pop committed
315
class OpCode(BaseOpCode):
Iustin Pop's avatar
Iustin Pop committed
316
317
318
319
320
321
  """Abstract OpCode.

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

  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
322
               children of this class.
323
324
325
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
                      string returned by Summary(); see the docstring of that
                      method for details).
326
327
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
                   get if not already defined, and types they must match.
328
329
  @cvar WITH_LU: Boolean that specifies whether this should be included in
      mcpu's dispatch table
330
331
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
                 the check steps
332
  @ivar priority: Opcode priority for queue
Iustin Pop's avatar
Iustin Pop committed
333
334

  """
335
336
  # pylint: disable-msg=E1101
  # as OP_ID is dynamically defined
337
  WITH_LU = True
338
  OP_PARAMS = [
339
340
    ("dry_run", None, ht.TMaybeBool, None),
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
341
    ("priority", constants.OP_PRIO_DEFAULT,
342
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), None),
343
    ]
344
345
346
347

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

Iustin Pop's avatar
Iustin Pop committed
348
349
350
351
352
353
354
    This method adds to the state dictionary the OP_ID of the class,
    so that on unload we can identify the correct class for
    instantiating the opcode.

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

355
    """
Iustin Pop's avatar
Iustin Pop committed
356
    data = BaseOpCode.__getstate__(self)
357
358
359
360
    data["OP_ID"] = self.OP_ID
    return data

  @classmethod
361
  def LoadOpCode(cls, data):
362
363
    """Generic load opcode method.

Iustin Pop's avatar
Iustin Pop committed
364
365
366
367
368
369
370
    The method identifies the correct opcode class from the dict-form
    by looking for a OP_ID key, if this is not found, or its value is
    not available in this module as a child of this class, we fail.

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

371
372
373
374
375
376
377
    """
    if not isinstance(data, dict):
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
    if "OP_ID" not in data:
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
    op_id = data["OP_ID"]
    op_class = None
Iustin Pop's avatar
Iustin Pop committed
378
379
380
    if op_id in OP_MAPPING:
      op_class = OP_MAPPING[op_id]
    else:
381
382
383
384
385
386
387
388
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
                       op_id)
    op = op_class()
    new_data = data.copy()
    del new_data["OP_ID"]
    op.__setstate__(new_data)
    return op

389
390
391
  def Summary(self):
    """Generates a summary description of this opcode.

392
393
394
395
396
    The summary is the value of the OP_ID attribute (without the "OP_"
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
    defined; this field should allow to easily identify the operation
    (for an instance creation job, e.g., it would be the instance
    name).
397

398
    """
399
    assert self.OP_ID is not None and len(self.OP_ID) > 3
400
401
402
403
404
    # all OP_ID start with OP_, we remove that
    txt = self.OP_ID[3:]
    field_name = getattr(self, "OP_DSC_FIELD", None)
    if field_name:
      field_value = getattr(self, field_name, None)
405
406
      if isinstance(field_value, (list, tuple)):
        field_value = ",".join(str(i) for i in field_value)
407
408
409
      txt = "%s(%s)" % (txt, field_value)
    return txt

Iustin Pop's avatar
Iustin Pop committed
410

411
412
# cluster opcodes

413
class OpClusterPostInit(OpCode):
414
415
416
417
418
419
420
421
  """Post cluster initialization.

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

  """


422
class OpClusterDestroy(OpCode):
Iustin Pop's avatar
Iustin Pop committed
423
424
425
426
427
428
  """Destroy the cluster.

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

  """
Iustin Pop's avatar
Iustin Pop committed
429
430


431
class OpClusterQuery(OpCode):
Iustin Pop's avatar
Iustin Pop committed
432
  """Query cluster information."""
Iustin Pop's avatar
Iustin Pop committed
433
434


435
class OpClusterVerify(OpCode):
Iustin Pop's avatar
Iustin Pop committed
436
437
438
439
440
441
442
443
444
  """Verify the cluster state.

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

  """
445
446
  OP_PARAMS = [
    ("skip_checks", ht.EmptyList,
447
448
449
450
     ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)), None),
    ("verbose", False, ht.TBool, None),
    ("error_codes", False, ht.TBool, None),
    ("debug_simulate_errors", False, ht.TBool, None),
451
    ]
Iustin Pop's avatar
Iustin Pop committed
452
453


454
class OpClusterVerifyDisks(OpCode):
Iustin Pop's avatar
Iustin Pop committed
455
456
457
458
  """Verify the cluster disks.

  Parameters: none

459
  Result: a tuple of four elements:
Iustin Pop's avatar
Iustin Pop committed
460
    - list of node names with bad data returned (unreachable, etc.)
Iustin Pop's avatar
Iustin Pop committed
461
    - dict of node names with broken volume groups (values: error msg)
Iustin Pop's avatar
Iustin Pop committed
462
    - list of instances with degraded disks (that should be activated)
463
464
    - dict of instances with missing logical volumes (values: (node, vol)
      pairs with details about the missing volumes)
Iustin Pop's avatar
Iustin Pop committed
465

466
467
468
469
  In normal operation, all lists should be empty. A non-empty instance
  list (3rd element of the result) is still ok (errors were fixed) but
  non-empty node list means some node is down, and probably there are
  unfixable drbd errors.
Iustin Pop's avatar
Iustin Pop committed
470
471
472
473
474
475
476

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

  """


477
class OpClusterRepairDiskSizes(OpCode):
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  """Verify the disk sizes of the instances and fixes configuration
  mimatches.

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

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

  In normal operation, the list should be empty.

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

  """
493
  OP_PARAMS = [
494
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
495
    ]
496
497


498
class OpClusterConfigQuery(OpCode):
499
  """Query cluster configuration values."""
500
501
502
  OP_PARAMS = [
    _POutputFields
    ]
Iustin Pop's avatar
Iustin Pop committed
503
504


505
class OpClusterRename(OpCode):
Iustin Pop's avatar
Iustin Pop committed
506
507
508
509
510
511
512
513
  """Rename the cluster.

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

  """
514
  OP_DSC_FIELD = "name"
515
  OP_PARAMS = [
516
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
517
    ]
518
519


520
class OpClusterSetParams(OpCode):
Iustin Pop's avatar
Iustin Pop committed
521
522
523
524
525
526
  """Change the parameters of the cluster.

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

  """
527
  OP_PARAMS = [
528
    ("vg_name", None, ht.TMaybeString, None),
529
530
    ("enabled_hypervisors", None,
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
531
            ht.TNone), None),
532
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
533
534
                              ht.TNone), None),
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone), None),
535
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
536
                            ht.TNone), None),
537
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
                              ht.TNone), None),
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
     None),
    ("uid_pool", None, ht.NoType, None),
    ("add_uids", None, ht.NoType, None),
    ("remove_uids", None, ht.NoType, None),
    ("maintain_node_health", None, ht.TMaybeBool, None),
    ("prealloc_wipe_disks", None, ht.TMaybeBool, None),
    ("nicparams", None, ht.TMaybeDict, None),
    ("ndparams", None, ht.TMaybeDict, None),
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), None),
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone), None),
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone), None),
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone), None),
    ("hidden_os", None, _TestClusterOsList, None),
    ("blacklisted_os", None, _TestClusterOsList, None),
554
    ]
555
556


557
class OpClusterRedistConf(OpCode):
558
559
560
561
  """Force a full push of the cluster configuration.

  """

Michael Hanselmann's avatar
Michael Hanselmann committed
562
563
564
565
566
567
568
569
570

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

  @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
  @ivar fields: List of fields to retrieve
  @ivar filter: Query filter

  """
571
  OP_PARAMS = [
572
573
    ("what", ht.NoDefault, ht.TElemOf(constants.QR_OP_QUERY), None),
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
574
    ("filter", None, ht.TOr(ht.TNone,
575
                            ht.TListOf(ht.TOr(ht.TNonEmptyString, ht.TList))), None),
Michael Hanselmann's avatar
Michael Hanselmann committed
576
577
578
579
580
581
582
583
584
585
    ]


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

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

  """
586
  OP_PARAMS = [
587
588
    ("what", ht.NoDefault, ht.TElemOf(constants.QR_OP_QUERY), None),
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)), None),
Michael Hanselmann's avatar
Michael Hanselmann committed
589
590
591
    ]


592
class OpOobCommand(OpCode):
René Nussbaumer's avatar
René Nussbaumer committed
593
  """Interact with OOB."""
594
  OP_PARAMS = [
595
596
597
598
599
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS), None),
    ("timeout", constants.OOB_TIMEOUT, ht.TInt, None),
    ("ignore_status", False, ht.TBool, None),
    ("force_master", False, ht.TBool, None),
René Nussbaumer's avatar
René Nussbaumer committed
600
601
602
    ]


603
604
# node opcodes

605
class OpNodeRemove(OpCode):
Iustin Pop's avatar
Iustin Pop committed
606
607
608
609
610
611
612
  """Remove a node.

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

  """
613
  OP_DSC_FIELD = "node_name"
614
615
616
  OP_PARAMS = [
    _PNodeName,
    ]
Iustin Pop's avatar
Iustin Pop committed
617
618


Iustin Pop's avatar
Iustin Pop committed
619
class OpNodeAdd(OpCode):
Iustin Pop's avatar
Iustin Pop committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  """Add a node to the cluster.

  @type node_name: C{str}
  @ivar node_name: The name of the node to add. This can be a short name,
                   but it will be expanded to the FQDN.
  @type primary_ip: IP address
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
                    opcode is submitted, but will be filled during the node
                    add (so it will be visible in the job query).
  @type secondary_ip: IP address
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
                      if the cluster has been initialized in 'dual-network'
                      mode, otherwise it must not be given.
  @type readd: C{bool}
  @ivar readd: Whether to re-add an existing node to the cluster. If
               this is not passed, then the operation will abort if the node
               name is already in the cluster; use this parameter to 'repair'
               a node that had its configuration broken, or was reinstalled
               without removal from the cluster.
639
640
  @type group: C{str}
  @ivar group: The node group to which this node will belong.
641
642
643
644
  @type vm_capable: C{bool}
  @ivar vm_capable: The vm_capable node attribute
  @type master_capable: C{bool}
  @ivar master_capable: The master_capable node attribute
Iustin Pop's avatar
Iustin Pop committed
645
646

  """
647
  OP_DSC_FIELD = "node_name"
648
649
  OP_PARAMS = [
    _PNodeName,
650
651
652
653
654
655
656
    ("primary_ip", None, ht.NoType, None),
    ("secondary_ip", None, ht.TMaybeString, None),
    ("readd", False, ht.TBool, None),
    ("group", None, ht.TMaybeString, None),
    ("master_capable", None, ht.TMaybeBool, None),
    ("vm_capable", None, ht.TMaybeBool, None),
    ("ndparams", None, ht.TMaybeDict, None),
657
    ]
Iustin Pop's avatar
Iustin Pop committed
658
659


660
class OpNodeQuery(OpCode):
Iustin Pop's avatar
Iustin Pop committed
661
  """Compute the list of nodes."""
662
663
  OP_PARAMS = [
    _POutputFields,
664
665
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
    ("use_locking", False, ht.TBool, None),
666
    ]
Iustin Pop's avatar
Iustin Pop committed
667
668


669
class OpNodeQueryvols(OpCode):
670
  """Get list of volumes on node."""
671
672
  OP_PARAMS = [
    _POutputFields,
673
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
674
    ]
675
676


677
class OpNodeQueryStorage(OpCode):
678
  """Get information on storage for node(s)."""
679
680
681
  OP_PARAMS = [
    _POutputFields,
    _PStorageType,
682
683
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
    ("name", None, ht.TMaybeString, None),
684
685
686
    ]


687
class OpNodeModifyStorage(OpCode):
688
  """Modifies the properies of a storage unit"""
689
690
691
  OP_PARAMS = [
    _PNodeName,
    _PStorageType,
692
693
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
    ("changes", ht.NoDefault, ht.TDict, None),
694
695
696
    ]


697
698
699
class OpRepairNodeStorage(OpCode):
  """Repairs the volume group on a node."""
  OP_DSC_FIELD = "node_name"
700
701
702
  OP_PARAMS = [
    _PNodeName,
    _PStorageType,
703
704
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
    ("ignore_consistency", False, ht.TBool, None),
705
706
707
    ]


708
class OpNodeSetParams(OpCode):
Iustin Pop's avatar
Iustin Pop committed
709
710
  """Change the parameters of a node."""
  OP_DSC_FIELD = "node_name"
711
712
713
  OP_PARAMS = [
    _PNodeName,
    _PForce,
714
715
716
717
718
719
720
721
722
    ("master_candidate", None, ht.TMaybeBool, None),
    ("offline", None, ht.TMaybeBool, None),
    ("drained", None, ht.TMaybeBool, None),
    ("auto_promote", False, ht.TBool, None),
    ("master_capable", None, ht.TMaybeBool, None),
    ("vm_capable", None, ht.TMaybeBool, None),
    ("secondary_ip", None, ht.TMaybeString, None),
    ("ndparams", None, ht.TMaybeDict, None),
    ("powered", None, ht.TMaybeBool, None),
Iustin Pop's avatar
Iustin Pop committed
723
724
    ]

Iustin Pop's avatar
Iustin Pop committed
725

726
class OpNodePowercycle(OpCode):
Iustin Pop's avatar
Iustin Pop committed
727
728
  """Tries to powercycle a node."""
  OP_DSC_FIELD = "node_name"
729
730
731
  OP_PARAMS = [
    _PNodeName,
    _PForce,
Iustin Pop's avatar
Iustin Pop committed
732
733
    ]

734

735
class OpNodeMigrate(OpCode):
736
737
  """Migrate all instances from a node."""
  OP_DSC_FIELD = "node_name"
738
739
740
741
  OP_PARAMS = [
    _PNodeName,
    _PMigrationMode,
    _PMigrationLive,
742
743
744
    ]


745
class OpNodeEvacStrategy(OpCode):
746
747
  """Compute the evacuation strategy for a list of nodes."""
  OP_DSC_FIELD = "nodes"
748
  OP_PARAMS = [
749
750
751
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
    ("remote_node", None, ht.TMaybeString, None),
    ("iallocator", None, ht.TMaybeString, None),
752
    ]
753
754


Iustin Pop's avatar
Iustin Pop committed
755
756
# instance opcodes

757
class OpInstanceCreate(OpCode):
758
759
760
761
762
763
764
  """Create an instance.

  @ivar instance_name: Instance name
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
  @ivar source_handshake: Signed handshake from source (remote import only)
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
  @ivar source_instance_name: Previous name of instance (remote import only)
765
766
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
    (remote import only)
767
768

  """
769
  OP_DSC_FIELD = "instance_name"
770
771
  OP_PARAMS = [
    _PInstanceName,
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
    ("beparams", ht.EmptyDict, ht.TDict, None),
    ("disks", ht.NoDefault, ht.TListOf(ht.TDict), None),
    ("disk_template", ht.NoDefault, _CheckDiskTemplate, None),
    ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)), None),
    ("file_storage_dir", None, ht.TMaybeString, None),
    ("force_variant", False, ht.TBool, None),
    ("hvparams", ht.EmptyDict, ht.TDict, None),
    ("hypervisor", None, ht.TMaybeString, None),
    ("iallocator", None, ht.TMaybeString, None),
    ("identify_defaults", False, ht.TBool, None),
    ("ip_check", True, ht.TBool, None),
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES), None),
    ("name_check", True, ht.TBool, None),
    ("nics", ht.NoDefault, ht.TListOf(ht.TDict), None),
    ("no_install", None, ht.TMaybeBool, None),
    ("osparams", ht.EmptyDict, ht.TDict, None),
    ("os_type", None, ht.TMaybeString, None),
    ("pnode", None, ht.TMaybeString, None),
    ("snode", None, ht.TMaybeString, None),
    ("source_handshake", None, ht.TOr(ht.TList, ht.TNone), None),
    ("source_instance_name", None, ht.TMaybeString, None),
793
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
794
795
796
797
798
799
     ht.TPositiveInt, None),
    ("source_x509_ca", None, ht.TMaybeString, None),
    ("src_node", None, ht.TMaybeString, None),
    ("src_path", None, ht.TMaybeString, None),
    ("start", True, ht.TBool, None),
    ("wait_for_sync", True, ht.TBool, None),
800
    ]
Iustin Pop's avatar
Iustin Pop committed
801
802


803
class OpInstanceReinstall(OpCode):
Iustin Pop's avatar
Iustin Pop committed
804
  """Reinstall an instance's OS."""
805
  OP_DSC_FIELD = "instance_name"
806
807
  OP_PARAMS = [
    _PInstanceName,
808
809
810
    ("os_type", None, ht.TMaybeString, None),
    ("force_variant", False, ht.TBool, None),
    ("osparams", None, ht.TMaybeDict, None),
811
    ]
812
813


814
class OpInstanceRemove(OpCode):
Iustin Pop's avatar
Iustin Pop committed
815
  """Remove an instance."""
816
  OP_DSC_FIELD = "instance_name"
817
818
819
  OP_PARAMS = [
    _PInstanceName,
    _PShutdownTimeout,
820
    ("ignore_failures", False, ht.TBool, None),
821
    ]
Iustin Pop's avatar
Iustin Pop committed
822
823


824
class OpInstanceRename(OpCode):
825
  """Rename an instance."""
826
827
  OP_PARAMS = [
    _PInstanceName,
828
829
830
    ("new_name", ht.NoDefault, ht.TNonEmptyString, None),
    ("ip_check", False, ht.TBool, None),
    ("name_check", True, ht.TBool, None),
831
    ]
832
833


834
class OpInstanceStartup(OpCode):
Iustin Pop's avatar
Iustin Pop committed
835
  """Startup an instance."""
836
  OP_DSC_FIELD = "instance_name"
837
838
839
840
  OP_PARAMS = [
    _PInstanceName,
    _PForce,
    _PIgnoreOfflineNodes,
841
842
    ("hvparams", ht.EmptyDict, ht.TDict, None),
    ("beparams", ht.EmptyDict, ht.TDict, None),
843
    ]
Iustin Pop's avatar
Iustin Pop committed
844
845


846
class OpInstanceShutdown(OpCode):
Iustin Pop's avatar
Iustin Pop committed
847
  """Shutdown an instance."""
848
  OP_DSC_FIELD = "instance_name"
849
850
851
  OP_PARAMS = [
    _PInstanceName,
    _PIgnoreOfflineNodes,
852
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt, None),
853
    ]
Iustin Pop's avatar
Iustin Pop committed
854
855


856
class OpInstanceReboot(OpCode):
857
  """Reboot an instance."""
858
  OP_DSC_FIELD = "instance_name"
859
860
861
  OP_PARAMS = [
    _PInstanceName,
    _PShutdownTimeout,
862
863
    ("ignore_secondaries", False, ht.TBool, None),
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES), None),
864
    ]
865
866


867
class OpInstanceReplaceDisks(OpCode):
Iustin Pop's avatar
Iustin Pop committed
868
  """Replace the disks of an instance."""
869
  OP_DSC_FIELD = "instance_name"
870
871
  OP_PARAMS = [
    _PInstanceName,
872
873
874
875
876
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES), None),
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt), None),
    ("remote_node", None, ht.TMaybeString, None),
    ("iallocator", None, ht.TMaybeString, None),
    ("early_release", False, ht.TBool, None),
877
    ]
Iustin Pop's avatar
Iustin Pop committed
878
879


880
class OpInstanceFailover(OpCode):
Iustin Pop's avatar
Iustin Pop committed
881
  """Failover an instance."""
882
  OP_DSC_FIELD = "instance_name"
883
884
885
  OP_PARAMS = [
    _PInstanceName,
    _PShutdownTimeout,
886
    ("ignore_consistency", False, ht.TBool, None),
887
    ]
Iustin Pop's avatar
Iustin Pop committed
888
889


890
class OpInstanceMigrate(OpCode):
891
892
893
894
895
  """Migrate an instance.

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

Iustin Pop's avatar
Iustin Pop committed
896
  @ivar instance_name: the name of the instance
897
  @ivar mode: the migration mode (live, non-live or None for auto)
898
899

  """
Iustin Pop's avatar
Iustin Pop committed
900
  OP_DSC_FIELD = "instance_name"
901
902
903
904
  OP_PARAMS = [
    _PInstanceName,
    _PMigrationMode,
    _PMigrationLive,
905
    ("cleanup", False, ht.TBool, None),
906
    ]
907
908


909
class OpInstanceMove(OpCode):
910
911
912
913
914
915
916
917
918
919
  """Move an instance.

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

  @ivar instance_name: the name of the instance
  @ivar target_node: the destination node

  """
  OP_DSC_FIELD = "instance_name"
920
921
922
  OP_PARAMS = [
    _PInstanceName,
    _PShutdownTimeout,
923
    ("target_node", ht.NoDefault, ht.TNonEmptyString, None),
Balazs Lecz's avatar
Balazs Lecz committed
924
    ]
925
926


927
class OpInstanceConsole(OpCode):
Iustin Pop's avatar
Iustin Pop committed
928
  """Connect to an instance's console."""
929
  OP_DSC_FIELD = "instance_name"
930
931
932
  OP_PARAMS = [
    _PInstanceName
    ]
Iustin Pop's avatar
Iustin Pop committed
933
934


935
class OpInstanceActivateDisks(OpCode):
Iustin Pop's avatar
Iustin Pop committed
936
  """Activate an instance's disks."""
937
  OP_DSC_FIELD = "instance_name"
938
939
  OP_PARAMS = [
    _PInstanceName,
940
    ("ignore_size", False, ht.TBool, None),
941
    ]
Iustin Pop's avatar
Iustin Pop committed
942
943


944
class OpInstanceDeactivateDisks(OpCode):
Iustin Pop's avatar
Iustin Pop committed
945
  """Deactivate an instance's disks."""
946
  OP_DSC_FIELD = "instance_name"
947
  OP_PARAMS = [
948
949
    _PInstanceName,
    _PForce,
950
    ]
Iustin Pop's avatar
Iustin Pop committed
951
952


953
class OpInstanceRecreateDisks(OpCode):
Iustin Pop's avatar
Iustin Pop committed
954
955
  """Deactivate an instance's disks."""
  OP_DSC_FIELD = "instance_name"
956
957
  OP_PARAMS = [
    _PInstanceName,
958
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt), None),
959
    ]
Iustin Pop's avatar
Iustin Pop committed
960
961


962
class OpInstanceQuery(OpCode):
Iustin Pop's avatar
Iustin Pop committed
963
  """Compute the list of instances."""
964
965
  OP_PARAMS = [
    _POutputFields,
966
967
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
    ("use_locking", False, ht.TBool, None),
968
    ]
Iustin Pop's avatar
Iustin Pop committed
969
970


971
class OpInstanceQueryData(OpCode):
Iustin Pop's avatar
Iustin Pop committed
972
  """Compute the run-time status of instances."""
973
  OP_PARAMS = [
974
975
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
    ("static", False, ht.TBool, None),
976
    ]
Iustin Pop's avatar
Iustin Pop committed
977
978


979
class OpInstanceSetParams(OpCode):
Iustin Pop's avatar
Iustin Pop committed
980
  """Change the parameters of an instance."""
981
  OP_DSC_FIELD = "instance_name"
982
983
984
  OP_PARAMS = [
    _PInstanceName,
    _PForce,
985
986
987
988
989
990
991
992
993
    ("nics", ht.EmptyList, ht.TList, None),
    ("disks", ht.EmptyList, ht.TList, None),
    ("beparams", ht.EmptyDict, ht.TDict, None),
    ("hvparams", ht.EmptyDict, ht.TDict, None),
    ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate), None),
    ("remote_node", None, ht.TMaybeString, None),
    ("os_name", None, ht.TMaybeString, None),
    ("force_variant", False, ht.TBool, None),
    ("osparams", None, ht.TMaybeDict, None),
994
    ]
Iustin Pop's avatar
Iustin Pop committed
995
996


Iustin Pop's avatar
Iustin Pop committed
997
class OpInstanceGrowDisk(OpCode):
Iustin Pop's avatar
Iustin Pop committed
998
  """Grow a disk of an instance."""
999
  OP_DSC_FIELD = "instance_name"
1000
1001
  OP_PARAMS = [
    _PInstanceName,
1002
1003
1004
    ("disk", ht.NoDefault, ht.TInt, None),
    ("amount", ht.NoDefault, ht.TInt, None),
    ("wait_for_sync", True, ht.TBool, None),
1005
    ]
Iustin Pop's avatar
Iustin Pop committed
1006
1007


1008
1009
# Node group opcodes

Iustin Pop's avatar
Iustin Pop committed
1010
class OpGroupAdd(OpCode):
1011
1012
  """Add a node group to the cluster."""
  OP_DSC_FIELD = "group_name"
1013
1014
  OP_PARAMS = [
    _PGroupName,
1015
    ("ndparams", None, ht.TMaybeDict, None),
1016
    ("alloc_policy", None,
1017
     ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)), None),
1018
    ]
1019
1020


1021
class OpGroupAssignNodes(OpCode):
1022
1023
1024
1025
1026
  """Assign nodes to a node group."""
  OP_DSC_FIELD = "group_name"
  OP_PARAMS = [
    _PGroupName,
    _PForce,
1027
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
1028
1029
1030
    ]


1031
class OpGroupQuery(OpCode):
1032
  """Compute the list of node groups."""
1033
1034
  OP_PARAMS = [
    _POutputFields,
1035
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1036
    ]
1037
1038


1039
class OpGroupSetParams(OpCode):
1040
1041
  """Change the parameters of a node group."""
  OP_DSC_FIELD = "group_name"
1042
1043
  OP_PARAMS = [
    _PGroupName,
1044
    ("ndparams", None, ht.TMaybeDict, None),
1045
    ("alloc_policy", None, ht.TOr(ht.TNone,
1046
                                  ht.TElemOf(constants.VALID_ALLOC_POLICIES)), None),
1047
1048
1049
    ]


1050
class OpGroupRemove(OpCode):
1051
1052
  """Remove a node group from the cluster."""
  OP_DSC_FIELD = "group_name"
1053
1054
1055
  OP_PARAMS = [
    _PGroupName,
    ]
1056
1057


1058
class OpGroupRename(OpCode):
1059
1060
  """Rename a node group in the cluster."""
  OP_DSC_FIELD = "old_name"
1061
  OP_PARAMS = [
1062
1063
    ("old_name", ht.NoDefault, ht.TNonEmptyString, None),
    ("new_name", ht.NoDefault, ht.TNonEmptyString, None),
1064
    ]
1065
1066


Iustin Pop's avatar
Iustin Pop committed
1067
# OS opcodes
1068
class OpOsDiagnose(OpCode):
Iustin Pop's avatar
Iustin Pop committed
1069
  """Compute the list of guest operating systems."""