Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 38f9d2cf

History | View | Annotate | Download (23.7 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010 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-msg=R0903
35

    
36

    
37
class BaseOpCode(object):
38
  """A simple serializable object.
39

40
  This object serves as a parent class for OpCode without any custom
41
  field handling.
42

43
  """
44
  __slots__ = []
45

    
46
  def __init__(self, **kwargs):
47
    """Constructor for BaseOpCode.
48

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

54
    """
55
    slots = self._all_slots()
56
    for key in kwargs:
57
      if key not in slots:
58
        raise TypeError("Object %s doesn't support the parameter '%s'" %
59
                        (self.__class__.__name__, key))
60
      setattr(self, key, kwargs[key])
61

    
62
  def __getstate__(self):
63
    """Generic serializer.
64

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

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

71
    """
72
    state = {}
73
    for name in self._all_slots():
74
      if hasattr(self, name):
75
        state[name] = getattr(self, name)
76
    return state
77

    
78
  def __setstate__(self, state):
79
    """Generic unserializer.
80

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

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

87
    """
88
    if not isinstance(state, dict):
89
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
90
                       type(state))
91

    
92
    for name in self._all_slots():
93
      if name not in state and hasattr(self, name):
94
        delattr(self, name)
95

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

    
99
  @classmethod
100
  def _all_slots(cls):
101
    """Compute the list of all declared slots for a class.
102

103
    """
104
    slots = []
105
    for parent in cls.__mro__:
106
      slots.extend(getattr(parent, "__slots__", []))
107
    return slots
108

    
109

    
110
class OpCode(BaseOpCode):
111
  """Abstract OpCode.
112

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

116
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
117
               children of this class.
118
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
119
                      string returned by Summary(); see the docstring of that
120
                      method for details).
121
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
122
                 the check steps
123
  @ivar priority: Opcode priority for queue
124

125
  """
126
  OP_ID = "OP_ABSTRACT"
127
  __slots__ = ["dry_run", "debug_level", "priority"]
128

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

132
    This method adds to the state dictionary the OP_ID of the class,
133
    so that on unload we can identify the correct class for
134
    instantiating the opcode.
135

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

139
    """
140
    data = BaseOpCode.__getstate__(self)
141
    data["OP_ID"] = self.OP_ID
142
    return data
143

    
144
  @classmethod
145
  def LoadOpCode(cls, data):
146
    """Generic load opcode method.
147

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

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

155
    """
156
    if not isinstance(data, dict):
157
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
158
    if "OP_ID" not in data:
159
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
160
    op_id = data["OP_ID"]
161
    op_class = None
162
    if op_id in OP_MAPPING:
163
      op_class = OP_MAPPING[op_id]
164
    else:
165
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
166
                       op_id)
167
    op = op_class()
168
    new_data = data.copy()
169
    del new_data["OP_ID"]
170
    op.__setstate__(new_data)
171
    return op
172

    
173
  def Summary(self):
174
    """Generates a summary description of this opcode.
175

176
    The summary is the value of the OP_ID attribute (without the "OP_" prefix),
177
    plus the value of the OP_DSC_FIELD attribute, if one was defined; this field
178
    should allow to easily identify the operation (for an instance creation job,
179
    e.g., it would be the instance name).
180

181
    """
182
    # all OP_ID start with OP_, we remove that
183
    txt = self.OP_ID[3:]
184
    field_name = getattr(self, "OP_DSC_FIELD", None)
185
    if field_name:
186
      field_value = getattr(self, field_name, None)
187
      if isinstance(field_value, (list, tuple)):
188
        field_value = ",".join(str(i) for i in field_value)
189
      txt = "%s(%s)" % (txt, field_value)
190
    return txt
191

    
192

    
193
# cluster opcodes
194

    
195
class OpPostInitCluster(OpCode):
196
  """Post cluster initialization.
197

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

201
  """
202
  OP_ID = "OP_CLUSTER_POST_INIT"
203
  __slots__ = []
204

    
205

    
206
class OpDestroyCluster(OpCode):
207
  """Destroy the cluster.
208

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

212
  """
213
  OP_ID = "OP_CLUSTER_DESTROY"
214
  __slots__ = []
215

    
216

    
217
class OpQueryClusterInfo(OpCode):
218
  """Query cluster information."""
219
  OP_ID = "OP_CLUSTER_QUERY"
220
  __slots__ = []
221

    
222

    
223
class OpVerifyCluster(OpCode):
224
  """Verify the cluster state.
225

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

232
  """
233
  OP_ID = "OP_CLUSTER_VERIFY"
234
  __slots__ = ["skip_checks", "verbose", "error_codes",
235
               "debug_simulate_errors"]
236

    
237

    
238
class OpVerifyDisks(OpCode):
239
  """Verify the cluster disks.
240

241
  Parameters: none
242

243
  Result: a tuple of four elements:
244
    - list of node names with bad data returned (unreachable, etc.)
245
    - dict of node names with broken volume groups (values: error msg)
246
    - list of instances with degraded disks (that should be activated)
247
    - dict of instances with missing logical volumes (values: (node, vol)
248
      pairs with details about the missing volumes)
249

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

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

258
  """
259
  OP_ID = "OP_CLUSTER_VERIFY_DISKS"
260
  __slots__ = []
261

    
262

    
263
class OpRepairDiskSizes(OpCode):
264
  """Verify the disk sizes of the instances and fixes configuration
265
  mimatches.
266

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

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

273
  In normal operation, the list should be empty.
274

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

278
  """
279
  OP_ID = "OP_CLUSTER_REPAIR_DISK_SIZES"
280
  __slots__ = ["instances"]
281

    
282

    
283
class OpQueryConfigValues(OpCode):
284
  """Query cluster configuration values."""
285
  OP_ID = "OP_CLUSTER_CONFIG_QUERY"
286
  __slots__ = ["output_fields"]
287

    
288

    
289
class OpRenameCluster(OpCode):
290
  """Rename the cluster.
291

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

297
  """
298
  OP_ID = "OP_CLUSTER_RENAME"
299
  OP_DSC_FIELD = "name"
300
  __slots__ = ["name"]
301

    
302

    
303
class OpSetClusterParams(OpCode):
304
  """Change the parameters of the cluster.
305

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

309
  """
310
  OP_ID = "OP_CLUSTER_SET_PARAMS"
311
  __slots__ = [
312
    "vg_name",
313
    "drbd_helper",
314
    "enabled_hypervisors",
315
    "hvparams",
316
    "os_hvp",
317
    "beparams",
318
    "osparams",
319
    "nicparams",
320
    "ndparams",
321
    "candidate_pool_size",
322
    "maintain_node_health",
323
    "uid_pool",
324
    "add_uids",
325
    "remove_uids",
326
    "default_iallocator",
327
    "reserved_lvs",
328
    "hidden_os",
329
    "blacklisted_os",
330
    "prealloc_wipe_disks",
331
    "master_netdev",
332
    ]
333

    
334

    
335
class OpRedistributeConfig(OpCode):
336
  """Force a full push of the cluster configuration.
337

338
  """
339
  OP_ID = "OP_CLUSTER_REDIST_CONF"
340
  __slots__ = []
341

    
342

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

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

350
  """
351
  OP_ID = "OP_QUERY"
352
  __slots__ = [
353
    "what",
354
    "fields",
355
    "filter",
356
    ]
357

    
358

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

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

365
  """
366
  OP_ID = "OP_QUERY_FIELDS"
367
  __slots__ = [
368
    "what",
369
    "fields",
370
    ]
371

    
372

    
373
class OpOutOfBand(OpCode):
374
  """Interact with OOB."""
375
  OP_ID = "OP_OUT_OF_BAND"
376
  __slots__ = [
377
    "node_name",
378
    "command",
379
    "timeout",
380
    ]
381

    
382

    
383
# node opcodes
384

    
385
class OpRemoveNode(OpCode):
386
  """Remove a node.
387

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

392
  """
393
  OP_ID = "OP_NODE_REMOVE"
394
  OP_DSC_FIELD = "node_name"
395
  __slots__ = ["node_name"]
396

    
397

    
398
class OpAddNode(OpCode):
399
  """Add a node to the cluster.
400

401
  @type node_name: C{str}
402
  @ivar node_name: The name of the node to add. This can be a short name,
403
                   but it will be expanded to the FQDN.
404
  @type primary_ip: IP address
405
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
406
                    opcode is submitted, but will be filled during the node
407
                    add (so it will be visible in the job query).
408
  @type secondary_ip: IP address
409
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
410
                      if the cluster has been initialized in 'dual-network'
411
                      mode, otherwise it must not be given.
412
  @type readd: C{bool}
413
  @ivar readd: Whether to re-add an existing node to the cluster. If
414
               this is not passed, then the operation will abort if the node
415
               name is already in the cluster; use this parameter to 'repair'
416
               a node that had its configuration broken, or was reinstalled
417
               without removal from the cluster.
418
  @type group: C{str}
419
  @ivar group: The node group to which this node will belong.
420
  @type vm_capable: C{bool}
421
  @ivar vm_capable: The vm_capable node attribute
422
  @type master_capable: C{bool}
423
  @ivar master_capable: The master_capable node attribute
424

425
  """
426
  OP_ID = "OP_NODE_ADD"
427
  OP_DSC_FIELD = "node_name"
428
  __slots__ = ["node_name", "primary_ip", "secondary_ip", "readd", "group",
429
               "vm_capable", "master_capable", "ndparams"]
430

    
431

    
432
class OpQueryNodes(OpCode):
433
  """Compute the list of nodes."""
434
  OP_ID = "OP_NODE_QUERY"
435
  __slots__ = ["output_fields", "names", "use_locking"]
436

    
437

    
438
class OpQueryNodeVolumes(OpCode):
439
  """Get list of volumes on node."""
440
  OP_ID = "OP_NODE_QUERYVOLS"
441
  __slots__ = ["nodes", "output_fields"]
442

    
443

    
444
class OpQueryNodeStorage(OpCode):
445
  """Get information on storage for node(s)."""
446
  OP_ID = "OP_NODE_QUERY_STORAGE"
447
  __slots__ = [
448
    "nodes",
449
    "storage_type",
450
    "name",
451
    "output_fields",
452
    ]
453

    
454

    
455
class OpModifyNodeStorage(OpCode):
456
  """Modifies the properies of a storage unit"""
457
  OP_ID = "OP_NODE_MODIFY_STORAGE"
458
  __slots__ = [
459
    "node_name",
460
    "storage_type",
461
    "name",
462
    "changes",
463
    ]
464

    
465

    
466
class OpRepairNodeStorage(OpCode):
467
  """Repairs the volume group on a node."""
468
  OP_ID = "OP_REPAIR_NODE_STORAGE"
469
  OP_DSC_FIELD = "node_name"
470
  __slots__ = [
471
    "node_name",
472
    "storage_type",
473
    "name",
474
    "ignore_consistency",
475
    ]
476

    
477

    
478
class OpSetNodeParams(OpCode):
479
  """Change the parameters of a node."""
480
  OP_ID = "OP_NODE_SET_PARAMS"
481
  OP_DSC_FIELD = "node_name"
482
  __slots__ = [
483
    "node_name",
484
    "force",
485
    "master_candidate",
486
    "offline",
487
    "drained",
488
    "auto_promote",
489
    "master_capable",
490
    "vm_capable",
491
    "secondary_ip",
492
    "ndparams",
493
    ]
494

    
495

    
496
class OpPowercycleNode(OpCode):
497
  """Tries to powercycle a node."""
498
  OP_ID = "OP_NODE_POWERCYCLE"
499
  OP_DSC_FIELD = "node_name"
500
  __slots__ = [
501
    "node_name",
502
    "force",
503
    ]
504

    
505

    
506
class OpMigrateNode(OpCode):
507
  """Migrate all instances from a node."""
508
  OP_ID = "OP_NODE_MIGRATE"
509
  OP_DSC_FIELD = "node_name"
510
  __slots__ = [
511
    "node_name",
512
    "mode",
513
    "live",
514
    ]
515

    
516

    
517
class OpNodeEvacuationStrategy(OpCode):
518
  """Compute the evacuation strategy for a list of nodes."""
519
  OP_ID = "OP_NODE_EVAC_STRATEGY"
520
  OP_DSC_FIELD = "nodes"
521
  __slots__ = ["nodes", "iallocator", "remote_node"]
522

    
523

    
524
# instance opcodes
525

    
526
class OpCreateInstance(OpCode):
527
  """Create an instance.
528

529
  @ivar instance_name: Instance name
530
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
531
  @ivar source_handshake: Signed handshake from source (remote import only)
532
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
533
  @ivar source_instance_name: Previous name of instance (remote import only)
534
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
535
    (remote import only)
536

537
  """
538
  OP_ID = "OP_INSTANCE_CREATE"
539
  OP_DSC_FIELD = "instance_name"
540
  __slots__ = [
541
    "instance_name",
542
    "os_type", "force_variant", "no_install",
543
    "pnode", "disk_template", "snode", "mode",
544
    "disks", "nics",
545
    "src_node", "src_path", "start", "identify_defaults",
546
    "wait_for_sync", "ip_check", "name_check",
547
    "file_storage_dir", "file_driver",
548
    "iallocator",
549
    "hypervisor", "hvparams", "beparams", "osparams",
550
    "source_handshake",
551
    "source_x509_ca",
552
    "source_instance_name",
553
    "source_shutdown_timeout",
554
    ]
555

    
556

    
557
class OpReinstallInstance(OpCode):
558
  """Reinstall an instance's OS."""
559
  OP_ID = "OP_INSTANCE_REINSTALL"
560
  OP_DSC_FIELD = "instance_name"
561
  __slots__ = ["instance_name", "os_type", "force_variant", "osparams"]
562

    
563

    
564
class OpRemoveInstance(OpCode):
565
  """Remove an instance."""
566
  OP_ID = "OP_INSTANCE_REMOVE"
567
  OP_DSC_FIELD = "instance_name"
568
  __slots__ = [
569
    "instance_name",
570
    "ignore_failures",
571
    "shutdown_timeout",
572
    ]
573

    
574

    
575
class OpRenameInstance(OpCode):
576
  """Rename an instance."""
577
  OP_ID = "OP_INSTANCE_RENAME"
578
  __slots__ = [
579
    "instance_name", "ip_check", "new_name", "name_check",
580
    ]
581

    
582

    
583
class OpStartupInstance(OpCode):
584
  """Startup an instance."""
585
  OP_ID = "OP_INSTANCE_STARTUP"
586
  OP_DSC_FIELD = "instance_name"
587
  __slots__ = [
588
    "instance_name", "force", "hvparams", "beparams", "ignore_offline_nodes",
589
    ]
590

    
591

    
592
class OpShutdownInstance(OpCode):
593
  """Shutdown an instance."""
594
  OP_ID = "OP_INSTANCE_SHUTDOWN"
595
  OP_DSC_FIELD = "instance_name"
596
  __slots__ = [
597
    "instance_name", "timeout", "ignore_offline_nodes",
598
    ]
599

    
600

    
601
class OpRebootInstance(OpCode):
602
  """Reboot an instance."""
603
  OP_ID = "OP_INSTANCE_REBOOT"
604
  OP_DSC_FIELD = "instance_name"
605
  __slots__ = [
606
    "instance_name", "reboot_type", "ignore_secondaries", "shutdown_timeout",
607
    ]
608

    
609

    
610
class OpReplaceDisks(OpCode):
611
  """Replace the disks of an instance."""
612
  OP_ID = "OP_INSTANCE_REPLACE_DISKS"
613
  OP_DSC_FIELD = "instance_name"
614
  __slots__ = [
615
    "instance_name", "remote_node", "mode", "disks", "iallocator",
616
    "early_release",
617
    ]
618

    
619

    
620
class OpFailoverInstance(OpCode):
621
  """Failover an instance."""
622
  OP_ID = "OP_INSTANCE_FAILOVER"
623
  OP_DSC_FIELD = "instance_name"
624
  __slots__ = [
625
    "instance_name", "ignore_consistency", "shutdown_timeout",
626
    ]
627

    
628

    
629
class OpMigrateInstance(OpCode):
630
  """Migrate an instance.
631

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

635
  @ivar instance_name: the name of the instance
636
  @ivar mode: the migration mode (live, non-live or None for auto)
637

638
  """
639
  OP_ID = "OP_INSTANCE_MIGRATE"
640
  OP_DSC_FIELD = "instance_name"
641
  __slots__ = ["instance_name", "mode", "cleanup", "live"]
642

    
643

    
644
class OpMoveInstance(OpCode):
645
  """Move an instance.
646

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

650
  @ivar instance_name: the name of the instance
651
  @ivar target_node: the destination node
652

653
  """
654
  OP_ID = "OP_INSTANCE_MOVE"
655
  OP_DSC_FIELD = "instance_name"
656
  __slots__ = [
657
    "instance_name", "target_node", "shutdown_timeout",
658
    ]
659

    
660

    
661
class OpConnectConsole(OpCode):
662
  """Connect to an instance's console."""
663
  OP_ID = "OP_INSTANCE_CONSOLE"
664
  OP_DSC_FIELD = "instance_name"
665
  __slots__ = ["instance_name"]
666

    
667

    
668
class OpActivateInstanceDisks(OpCode):
669
  """Activate an instance's disks."""
670
  OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
671
  OP_DSC_FIELD = "instance_name"
672
  __slots__ = ["instance_name", "ignore_size"]
673

    
674

    
675
class OpDeactivateInstanceDisks(OpCode):
676
  """Deactivate an instance's disks."""
677
  OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
678
  OP_DSC_FIELD = "instance_name"
679
  __slots__ = ["instance_name"]
680

    
681

    
682
class OpRecreateInstanceDisks(OpCode):
683
  """Deactivate an instance's disks."""
684
  OP_ID = "OP_INSTANCE_RECREATE_DISKS"
685
  OP_DSC_FIELD = "instance_name"
686
  __slots__ = ["instance_name", "disks"]
687

    
688

    
689
class OpQueryInstances(OpCode):
690
  """Compute the list of instances."""
691
  OP_ID = "OP_INSTANCE_QUERY"
692
  __slots__ = ["output_fields", "names", "use_locking"]
693

    
694

    
695
class OpQueryInstanceData(OpCode):
696
  """Compute the run-time status of instances."""
697
  OP_ID = "OP_INSTANCE_QUERY_DATA"
698
  __slots__ = ["instances", "static"]
699

    
700

    
701
class OpSetInstanceParams(OpCode):
702
  """Change the parameters of an instance."""
703
  OP_ID = "OP_INSTANCE_SET_PARAMS"
704
  OP_DSC_FIELD = "instance_name"
705
  __slots__ = [
706
    "instance_name",
707
    "hvparams", "beparams", "osparams", "force",
708
    "nics", "disks", "disk_template",
709
    "remote_node", "os_name", "force_variant",
710
    ]
711

    
712

    
713
class OpGrowDisk(OpCode):
714
  """Grow a disk of an instance."""
715
  OP_ID = "OP_INSTANCE_GROW_DISK"
716
  OP_DSC_FIELD = "instance_name"
717
  __slots__ = [
718
    "instance_name", "disk", "amount", "wait_for_sync",
719
    ]
720

    
721

    
722
# Node group opcodes
723

    
724
class OpAddGroup(OpCode):
725
  """Add a node group to the cluster."""
726
  OP_ID = "OP_GROUP_ADD"
727
  OP_DSC_FIELD = "group_name"
728
  __slots__ = ["group_name"]
729

    
730

    
731
class OpQueryGroups(OpCode):
732
  """Compute the list of node groups."""
733
  OP_ID = "OP_GROUP_QUERY"
734
  __slots__ = ["output_fields", "names"]
735

    
736

    
737
class OpRemoveGroup(OpCode):
738
  """Remove a node group from the cluster."""
739
  OP_ID = "OP_GROUP_REMOVE"
740
  OP_DSC_FIELD = "group_name"
741
  __slots__ = ["group_name"]
742

    
743

    
744
class OpRenameGroup(OpCode):
745
  """Rename a node group in the cluster."""
746
  OP_ID = "OP_GROUP_RENAME"
747
  OP_DSC_FIELD = "old_name"
748
  __slots__ = ["old_name", "new_name"]
749

    
750

    
751
# OS opcodes
752
class OpDiagnoseOS(OpCode):
753
  """Compute the list of guest operating systems."""
754
  OP_ID = "OP_OS_DIAGNOSE"
755
  __slots__ = ["output_fields", "names"]
756

    
757

    
758
# Exports opcodes
759
class OpQueryExports(OpCode):
760
  """Compute the list of exported images."""
761
  OP_ID = "OP_BACKUP_QUERY"
762
  __slots__ = ["nodes", "use_locking"]
763

    
764

    
765
class OpPrepareExport(OpCode):
766
  """Prepares an instance export.
767

768
  @ivar instance_name: Instance name
769
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
770

771
  """
772
  OP_ID = "OP_BACKUP_PREPARE"
773
  OP_DSC_FIELD = "instance_name"
774
  __slots__ = [
775
    "instance_name", "mode",
776
    ]
777

    
778

    
779
class OpExportInstance(OpCode):
780
  """Export an instance.
781

782
  For local exports, the export destination is the node name. For remote
783
  exports, the export destination is a list of tuples, each consisting of
784
  hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
785
  the cluster domain secret over the value "${index}:${hostname}:${port}". The
786
  destination X509 CA must be a signed certificate.
787

788
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
789
  @ivar target_node: Export destination
790
  @ivar x509_key_name: X509 key to use (remote export only)
791
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
792
                             only)
793

794
  """
795
  OP_ID = "OP_BACKUP_EXPORT"
796
  OP_DSC_FIELD = "instance_name"
797
  __slots__ = [
798
    # TODO: Rename target_node as it changes meaning for different export modes
799
    # (e.g. "destination")
800
    "instance_name", "target_node", "shutdown", "shutdown_timeout",
801
    "remove_instance",
802
    "ignore_remove_failures",
803
    "mode",
804
    "x509_key_name",
805
    "destination_x509_ca",
806
    ]
807

    
808

    
809
class OpRemoveExport(OpCode):
810
  """Remove an instance's export."""
811
  OP_ID = "OP_BACKUP_REMOVE"
812
  OP_DSC_FIELD = "instance_name"
813
  __slots__ = ["instance_name"]
814

    
815

    
816
# Tags opcodes
817
class OpGetTags(OpCode):
818
  """Returns the tags of the given object."""
819
  OP_ID = "OP_TAGS_GET"
820
  OP_DSC_FIELD = "name"
821
  __slots__ = ["kind", "name"]
822

    
823

    
824
class OpSearchTags(OpCode):
825
  """Searches the tags in the cluster for a given pattern."""
826
  OP_ID = "OP_TAGS_SEARCH"
827
  OP_DSC_FIELD = "pattern"
828
  __slots__ = ["pattern"]
829

    
830

    
831
class OpAddTags(OpCode):
832
  """Add a list of tags on a given object."""
833
  OP_ID = "OP_TAGS_SET"
834
  __slots__ = ["kind", "name", "tags"]
835

    
836

    
837
class OpDelTags(OpCode):
838
  """Remove a list of tags from a given object."""
839
  OP_ID = "OP_TAGS_DEL"
840
  __slots__ = ["kind", "name", "tags"]
841

    
842

    
843
# Test opcodes
844
class OpTestDelay(OpCode):
845
  """Sleeps for a configured amount of time.
846

847
  This is used just for debugging and testing.
848

849
  Parameters:
850
    - duration: the time to sleep
851
    - on_master: if true, sleep on the master
852
    - on_nodes: list of nodes in which to sleep
853

854
  If the on_master parameter is true, it will execute a sleep on the
855
  master (before any node sleep).
856

857
  If the on_nodes list is not empty, it will sleep on those nodes
858
  (after the sleep on the master, if that is enabled).
859

860
  As an additional feature, the case of duration < 0 will be reported
861
  as an execution error, so this opcode can be used as a failure
862
  generator. The case of duration == 0 will not be treated specially.
863

864
  """
865
  OP_ID = "OP_TEST_DELAY"
866
  OP_DSC_FIELD = "duration"
867
  __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
868

    
869

    
870
class OpTestAllocator(OpCode):
871
  """Allocator framework testing.
872

873
  This opcode has two modes:
874
    - gather and return allocator input for a given mode (allocate new
875
      or replace secondary) and a given instance definition (direction
876
      'in')
877
    - run a selected allocator for a given operation (as above) and
878
      return the allocator output (direction 'out')
879

880
  """
881
  OP_ID = "OP_TEST_ALLOCATOR"
882
  OP_DSC_FIELD = "allocator"
883
  __slots__ = [
884
    "direction", "mode", "allocator", "name",
885
    "mem_size", "disks", "disk_template",
886
    "os", "tags", "nics", "vcpus", "hypervisor",
887
    "evac_nodes",
888
    ]
889

    
890

    
891
class OpTestJobqueue(OpCode):
892
  """Utility opcode to test some aspects of the job queue.
893

894
  """
895
  OP_ID = "OP_TEST_JQUEUE"
896
  __slots__ = [
897
    "notify_waitlock",
898
    "notify_exec",
899
    "log_messages",
900
    "fail",
901
    ]
902

    
903

    
904
class OpTestDummy(OpCode):
905
  """Utility opcode used by unittests.
906

907
  """
908
  OP_ID = "OP_TEST_DUMMY"
909
  __slots__ = [
910
    "result",
911
    "messages",
912
    "fail",
913
    ]
914

    
915

    
916
OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
917
                   if (isinstance(v, type) and issubclass(v, OpCode) and
918
                       hasattr(v, "OP_ID"))])