Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ eb64da59

History | View | Annotate | Download (23.2 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
    ]
332

    
333

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

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

    
341

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

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

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

    
357

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

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

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

    
371

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

    
381

    
382
# node opcodes
383

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

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

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

    
396

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

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

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

    
430

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

    
436

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

    
442

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

    
453

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

    
464

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

    
476

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

    
494

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

    
504

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

    
515

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

    
522

    
523
# instance opcodes
524

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

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

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

    
555

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

    
562

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

    
573

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

    
581

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

    
590

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

    
599

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

    
608

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

    
618

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

    
627

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

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

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

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

    
642

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

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

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

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

    
659

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

    
666

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

    
673

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

    
680

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

    
687

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

    
693

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

    
699

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

    
711

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

    
720

    
721
# Node group opcodes
722

    
723
class OpQueryGroups(OpCode):
724
  """Compute the list of node groups."""
725
  OP_ID = "OP_GROUP_QUERY"
726
  __slots__ = ["output_fields", "names"]
727

    
728

    
729
# OS opcodes
730
class OpDiagnoseOS(OpCode):
731
  """Compute the list of guest operating systems."""
732
  OP_ID = "OP_OS_DIAGNOSE"
733
  __slots__ = ["output_fields", "names"]
734

    
735

    
736
# Exports opcodes
737
class OpQueryExports(OpCode):
738
  """Compute the list of exported images."""
739
  OP_ID = "OP_BACKUP_QUERY"
740
  __slots__ = ["nodes", "use_locking"]
741

    
742

    
743
class OpPrepareExport(OpCode):
744
  """Prepares an instance export.
745

746
  @ivar instance_name: Instance name
747
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
748

749
  """
750
  OP_ID = "OP_BACKUP_PREPARE"
751
  OP_DSC_FIELD = "instance_name"
752
  __slots__ = [
753
    "instance_name", "mode",
754
    ]
755

    
756

    
757
class OpExportInstance(OpCode):
758
  """Export an instance.
759

760
  For local exports, the export destination is the node name. For remote
761
  exports, the export destination is a list of tuples, each consisting of
762
  hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
763
  the cluster domain secret over the value "${index}:${hostname}:${port}". The
764
  destination X509 CA must be a signed certificate.
765

766
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
767
  @ivar target_node: Export destination
768
  @ivar x509_key_name: X509 key to use (remote export only)
769
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
770
                             only)
771

772
  """
773
  OP_ID = "OP_BACKUP_EXPORT"
774
  OP_DSC_FIELD = "instance_name"
775
  __slots__ = [
776
    # TODO: Rename target_node as it changes meaning for different export modes
777
    # (e.g. "destination")
778
    "instance_name", "target_node", "shutdown", "shutdown_timeout",
779
    "remove_instance",
780
    "ignore_remove_failures",
781
    "mode",
782
    "x509_key_name",
783
    "destination_x509_ca",
784
    ]
785

    
786

    
787
class OpRemoveExport(OpCode):
788
  """Remove an instance's export."""
789
  OP_ID = "OP_BACKUP_REMOVE"
790
  OP_DSC_FIELD = "instance_name"
791
  __slots__ = ["instance_name"]
792

    
793

    
794
# Tags opcodes
795
class OpGetTags(OpCode):
796
  """Returns the tags of the given object."""
797
  OP_ID = "OP_TAGS_GET"
798
  OP_DSC_FIELD = "name"
799
  __slots__ = ["kind", "name"]
800

    
801

    
802
class OpSearchTags(OpCode):
803
  """Searches the tags in the cluster for a given pattern."""
804
  OP_ID = "OP_TAGS_SEARCH"
805
  OP_DSC_FIELD = "pattern"
806
  __slots__ = ["pattern"]
807

    
808

    
809
class OpAddTags(OpCode):
810
  """Add a list of tags on a given object."""
811
  OP_ID = "OP_TAGS_SET"
812
  __slots__ = ["kind", "name", "tags"]
813

    
814

    
815
class OpDelTags(OpCode):
816
  """Remove a list of tags from a given object."""
817
  OP_ID = "OP_TAGS_DEL"
818
  __slots__ = ["kind", "name", "tags"]
819

    
820

    
821
# Test opcodes
822
class OpTestDelay(OpCode):
823
  """Sleeps for a configured amount of time.
824

825
  This is used just for debugging and testing.
826

827
  Parameters:
828
    - duration: the time to sleep
829
    - on_master: if true, sleep on the master
830
    - on_nodes: list of nodes in which to sleep
831

832
  If the on_master parameter is true, it will execute a sleep on the
833
  master (before any node sleep).
834

835
  If the on_nodes list is not empty, it will sleep on those nodes
836
  (after the sleep on the master, if that is enabled).
837

838
  As an additional feature, the case of duration < 0 will be reported
839
  as an execution error, so this opcode can be used as a failure
840
  generator. The case of duration == 0 will not be treated specially.
841

842
  """
843
  OP_ID = "OP_TEST_DELAY"
844
  OP_DSC_FIELD = "duration"
845
  __slots__ = ["duration", "on_master", "on_nodes", "repeat"]
846

    
847

    
848
class OpTestAllocator(OpCode):
849
  """Allocator framework testing.
850

851
  This opcode has two modes:
852
    - gather and return allocator input for a given mode (allocate new
853
      or replace secondary) and a given instance definition (direction
854
      'in')
855
    - run a selected allocator for a given operation (as above) and
856
      return the allocator output (direction 'out')
857

858
  """
859
  OP_ID = "OP_TEST_ALLOCATOR"
860
  OP_DSC_FIELD = "allocator"
861
  __slots__ = [
862
    "direction", "mode", "allocator", "name",
863
    "mem_size", "disks", "disk_template",
864
    "os", "tags", "nics", "vcpus", "hypervisor",
865
    "evac_nodes",
866
    ]
867

    
868

    
869
class OpTestJobqueue(OpCode):
870
  """Utility opcode to test some aspects of the job queue.
871

872
  """
873
  OP_ID = "OP_TEST_JQUEUE"
874
  __slots__ = [
875
    "notify_waitlock",
876
    "notify_exec",
877
    "log_messages",
878
    "fail",
879
    ]
880

    
881

    
882
class OpTestDummy(OpCode):
883
  """Utility opcode used by unittests.
884

885
  """
886
  OP_ID = "OP_TEST_DUMMY"
887
  __slots__ = [
888
    "result",
889
    "messages",
890
    "fail",
891
    ]
892

    
893

    
894
OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
895
                   if (isinstance(v, type) and issubclass(v, OpCode) and
896
                       hasattr(v, "OP_ID"))])