Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ f9780ccd

History | View | Annotate | Download (17.4 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007 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
    for key in kwargs:
56
      if key not in self.__slots__:
57
        raise TypeError("Object %s doesn't support the parameter '%s'" %
58
                        (self.__class__.__name__, key))
59
      setattr(self, key, kwargs[key])
60

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

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

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

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

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

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

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

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

    
91
    for name in self.__slots__:
92
      if name not in state:
93
        delattr(self, name)
94

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

    
98

    
99
class OpCode(BaseOpCode):
100
  """Abstract OpCode.
101

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

105
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
106
               children of this class.
107
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
108
                 the check steps
109

110
  """
111
  OP_ID = "OP_ABSTRACT"
112
  __slots__ = BaseOpCode.__slots__ + ["dry_run"]
113

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

117
    This method adds to the state dictionary the OP_ID of the class,
118
    so that on unload we can identify the correct class for
119
    instantiating the opcode.
120

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

124
    """
125
    data = BaseOpCode.__getstate__(self)
126
    data["OP_ID"] = self.OP_ID
127
    return data
128

    
129
  @classmethod
130
  def LoadOpCode(cls, data):
131
    """Generic load opcode method.
132

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

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

140
    """
141
    if not isinstance(data, dict):
142
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
143
    if "OP_ID" not in data:
144
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
145
    op_id = data["OP_ID"]
146
    op_class = None
147
    if op_id in OP_MAPPING:
148
      op_class = OP_MAPPING[op_id]
149
    else:
150
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
151
                       op_id)
152
    op = op_class()
153
    new_data = data.copy()
154
    del new_data["OP_ID"]
155
    op.__setstate__(new_data)
156
    return op
157

    
158
  def Summary(self):
159
    """Generates a summary description of this opcode.
160

161
    """
162
    # all OP_ID start with OP_, we remove that
163
    txt = self.OP_ID[3:]
164
    field_name = getattr(self, "OP_DSC_FIELD", None)
165
    if field_name:
166
      field_value = getattr(self, field_name, None)
167
      txt = "%s(%s)" % (txt, field_value)
168
    return txt
169

    
170

    
171
# cluster opcodes
172

    
173
class OpDestroyCluster(OpCode):
174
  """Destroy the cluster.
175

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

179
  """
180
  OP_ID = "OP_CLUSTER_DESTROY"
181
  __slots__ = OpCode.__slots__ + []
182

    
183

    
184
class OpQueryClusterInfo(OpCode):
185
  """Query cluster information."""
186
  OP_ID = "OP_CLUSTER_QUERY"
187
  __slots__ = OpCode.__slots__ + []
188

    
189

    
190
class OpVerifyCluster(OpCode):
191
  """Verify the cluster state.
192

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

199
  """
200
  OP_ID = "OP_CLUSTER_VERIFY"
201
  __slots__ = OpCode.__slots__ + ["skip_checks"]
202

    
203

    
204
class OpVerifyDisks(OpCode):
205
  """Verify the cluster disks.
206

207
  Parameters: none
208

209
  Result: a tuple of four elements:
210
    - list of node names with bad data returned (unreachable, etc.)
211
    - dict of node names with broken volume groups (values: error msg)
212
    - list of instances with degraded disks (that should be activated)
213
    - dict of instances with missing logical volumes (values: (node, vol)
214
      pairs with details about the missing volumes)
215

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

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

224
  """
225
  OP_ID = "OP_CLUSTER_VERIFY_DISKS"
226
  __slots__ = OpCode.__slots__ + []
227

    
228

    
229
class OpQueryConfigValues(OpCode):
230
  """Query cluster configuration values."""
231
  OP_ID = "OP_CLUSTER_CONFIG_QUERY"
232
  __slots__ = OpCode.__slots__ + ["output_fields"]
233

    
234

    
235
class OpRenameCluster(OpCode):
236
  """Rename the cluster.
237

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

243
  """
244
  OP_ID = "OP_CLUSTER_RENAME"
245
  OP_DSC_FIELD = "name"
246
  __slots__ = OpCode.__slots__ + ["name"]
247

    
248

    
249
class OpSetClusterParams(OpCode):
250
  """Change the parameters of the cluster.
251

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

255
  """
256
  OP_ID = "OP_CLUSTER_SET_PARAMS"
257
  __slots__ = OpCode.__slots__ + [
258
    "vg_name",
259
    "enabled_hypervisors",
260
    "hvparams",
261
    "beparams",
262
    "nicparams",
263
    "candidate_pool_size",
264
    ]
265

    
266

    
267
class OpRedistributeConfig(OpCode):
268
  """Force a full push of the cluster configuration.
269

270
  """
271
  OP_ID = "OP_CLUSTER_REDIST_CONF"
272
  __slots__ = OpCode.__slots__ + [
273
    ]
274

    
275
# node opcodes
276

    
277
class OpRemoveNode(OpCode):
278
  """Remove a node.
279

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

284
  """
285
  OP_ID = "OP_NODE_REMOVE"
286
  OP_DSC_FIELD = "node_name"
287
  __slots__ = OpCode.__slots__ + ["node_name"]
288

    
289

    
290
class OpAddNode(OpCode):
291
  """Add a node to the cluster.
292

293
  @type node_name: C{str}
294
  @ivar node_name: The name of the node to add. This can be a short name,
295
                   but it will be expanded to the FQDN.
296
  @type primary_ip: IP address
297
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
298
                    opcode is submitted, but will be filled during the node
299
                    add (so it will be visible in the job query).
300
  @type secondary_ip: IP address
301
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
302
                      if the cluster has been initialized in 'dual-network'
303
                      mode, otherwise it must not be given.
304
  @type readd: C{bool}
305
  @ivar readd: Whether to re-add an existing node to the cluster. If
306
               this is not passed, then the operation will abort if the node
307
               name is already in the cluster; use this parameter to 'repair'
308
               a node that had its configuration broken, or was reinstalled
309
               without removal from the cluster.
310

311
  """
312
  OP_ID = "OP_NODE_ADD"
313
  OP_DSC_FIELD = "node_name"
314
  __slots__ = OpCode.__slots__ + [
315
    "node_name", "primary_ip", "secondary_ip", "readd",
316
    ]
317

    
318

    
319
class OpQueryNodes(OpCode):
320
  """Compute the list of nodes."""
321
  OP_ID = "OP_NODE_QUERY"
322
  __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
323

    
324

    
325
class OpQueryNodeVolumes(OpCode):
326
  """Get list of volumes on node."""
327
  OP_ID = "OP_NODE_QUERYVOLS"
328
  __slots__ = OpCode.__slots__ + ["nodes", "output_fields"]
329

    
330

    
331
class OpQueryNodeStorage(OpCode):
332
  """Get information on storage for node(s)."""
333
  OP_ID = "OP_NODE_QUERY_STORAGE"
334
  __slots__ = OpCode.__slots__ + [
335
    "nodes",
336
    "storage_type",
337
    "name",
338
    "output_fields",
339
    ]
340

    
341

    
342
class OpModifyNodeStorage(OpCode):
343
  """"""
344
  OP_ID = "OP_NODE_MODIFY_STORAGE"
345
  __slots__ = OpCode.__slots__ + [
346
    "node_name",
347
    "storage_type",
348
    "name",
349
    "changes",
350
    ]
351

    
352

    
353
class OpSetNodeParams(OpCode):
354
  """Change the parameters of a node."""
355
  OP_ID = "OP_NODE_SET_PARAMS"
356
  OP_DSC_FIELD = "node_name"
357
  __slots__ = OpCode.__slots__ + [
358
    "node_name",
359
    "force",
360
    "master_candidate",
361
    "offline",
362
    "drained",
363
    ]
364

    
365

    
366
class OpPowercycleNode(OpCode):
367
  """Tries to powercycle a node."""
368
  OP_ID = "OP_NODE_POWERCYCLE"
369
  OP_DSC_FIELD = "node_name"
370
  __slots__ = OpCode.__slots__ + [
371
    "node_name",
372
    "force",
373
    ]
374

    
375

    
376
class OpEvacuateNode(OpCode):
377
  """Relocate secondary instances from a node."""
378
  OP_ID = "OP_NODE_EVACUATE"
379
  OP_DSC_FIELD = "node_name"
380
  __slots__ = OpCode.__slots__ + [
381
    "node_name", "remote_node", "iallocator",
382
    ]
383

    
384

    
385
class OpMigrateNode(OpCode):
386
  """Migrate all instances from a node."""
387
  OP_ID = "OP_NODE_MIGRATE"
388
  OP_DSC_FIELD = "node_name"
389
  __slots__ = OpCode.__slots__ + [
390
    "node_name",
391
    "live",
392
    ]
393

    
394

    
395
# instance opcodes
396

    
397
class OpCreateInstance(OpCode):
398
  """Create an instance."""
399
  OP_ID = "OP_INSTANCE_CREATE"
400
  OP_DSC_FIELD = "instance_name"
401
  __slots__ = OpCode.__slots__ + [
402
    "instance_name", "os_type", "pnode",
403
    "disk_template", "snode", "mode",
404
    "disks", "nics",
405
    "src_node", "src_path", "start",
406
    "wait_for_sync", "ip_check",
407
    "file_storage_dir", "file_driver",
408
    "iallocator",
409
    "hypervisor", "hvparams", "beparams",
410
    "dry_run",
411
    ]
412

    
413

    
414
class OpReinstallInstance(OpCode):
415
  """Reinstall an instance's OS."""
416
  OP_ID = "OP_INSTANCE_REINSTALL"
417
  OP_DSC_FIELD = "instance_name"
418
  __slots__ = OpCode.__slots__ + ["instance_name", "os_type"]
419

    
420

    
421
class OpRemoveInstance(OpCode):
422
  """Remove an instance."""
423
  OP_ID = "OP_INSTANCE_REMOVE"
424
  OP_DSC_FIELD = "instance_name"
425
  __slots__ = OpCode.__slots__ + ["instance_name", "ignore_failures"]
426

    
427

    
428
class OpRenameInstance(OpCode):
429
  """Rename an instance."""
430
  OP_ID = "OP_INSTANCE_RENAME"
431
  __slots__ = OpCode.__slots__ + [
432
    "instance_name", "ignore_ip", "new_name",
433
    ]
434

    
435

    
436
class OpStartupInstance(OpCode):
437
  """Startup an instance."""
438
  OP_ID = "OP_INSTANCE_STARTUP"
439
  OP_DSC_FIELD = "instance_name"
440
  __slots__ = OpCode.__slots__ + [
441
    "instance_name", "force", "hvparams", "beparams",
442
    ]
443

    
444

    
445
class OpShutdownInstance(OpCode):
446
  """Shutdown an instance."""
447
  OP_ID = "OP_INSTANCE_SHUTDOWN"
448
  OP_DSC_FIELD = "instance_name"
449
  __slots__ = OpCode.__slots__ + ["instance_name"]
450

    
451

    
452
class OpRebootInstance(OpCode):
453
  """Reboot an instance."""
454
  OP_ID = "OP_INSTANCE_REBOOT"
455
  OP_DSC_FIELD = "instance_name"
456
  __slots__ = OpCode.__slots__ + [
457
    "instance_name", "reboot_type", "ignore_secondaries",
458
    ]
459

    
460

    
461
class OpReplaceDisks(OpCode):
462
  """Replace the disks of an instance."""
463
  OP_ID = "OP_INSTANCE_REPLACE_DISKS"
464
  OP_DSC_FIELD = "instance_name"
465
  __slots__ = OpCode.__slots__ + [
466
    "instance_name", "remote_node", "mode", "disks", "iallocator",
467
    ]
468

    
469

    
470
class OpFailoverInstance(OpCode):
471
  """Failover an instance."""
472
  OP_ID = "OP_INSTANCE_FAILOVER"
473
  OP_DSC_FIELD = "instance_name"
474
  __slots__ = OpCode.__slots__ + ["instance_name", "ignore_consistency"]
475

    
476

    
477
class OpMigrateInstance(OpCode):
478
  """Migrate an instance.
479

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

483
  @ivar instance_name: the name of the instance
484

485
  """
486
  OP_ID = "OP_INSTANCE_MIGRATE"
487
  OP_DSC_FIELD = "instance_name"
488
  __slots__ = OpCode.__slots__ + ["instance_name", "live", "cleanup"]
489

    
490

    
491
class OpConnectConsole(OpCode):
492
  """Connect to an instance's console."""
493
  OP_ID = "OP_INSTANCE_CONSOLE"
494
  OP_DSC_FIELD = "instance_name"
495
  __slots__ = OpCode.__slots__ + ["instance_name"]
496

    
497

    
498
class OpActivateInstanceDisks(OpCode):
499
  """Activate an instance's disks."""
500
  OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
501
  OP_DSC_FIELD = "instance_name"
502
  __slots__ = OpCode.__slots__ + ["instance_name"]
503

    
504

    
505
class OpDeactivateInstanceDisks(OpCode):
506
  """Deactivate an instance's disks."""
507
  OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
508
  OP_DSC_FIELD = "instance_name"
509
  __slots__ = OpCode.__slots__ + ["instance_name"]
510

    
511

    
512
class OpQueryInstances(OpCode):
513
  """Compute the list of instances."""
514
  OP_ID = "OP_INSTANCE_QUERY"
515
  __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
516

    
517

    
518
class OpQueryInstanceData(OpCode):
519
  """Compute the run-time status of instances."""
520
  OP_ID = "OP_INSTANCE_QUERY_DATA"
521
  __slots__ = OpCode.__slots__ + ["instances", "static"]
522

    
523

    
524
class OpSetInstanceParams(OpCode):
525
  """Change the parameters of an instance."""
526
  OP_ID = "OP_INSTANCE_SET_PARAMS"
527
  OP_DSC_FIELD = "instance_name"
528
  __slots__ = OpCode.__slots__ + [
529
    "instance_name",
530
    "hvparams", "beparams", "force",
531
    "nics", "disks",
532
    ]
533

    
534

    
535
class OpGrowDisk(OpCode):
536
  """Grow a disk of an instance."""
537
  OP_ID = "OP_INSTANCE_GROW_DISK"
538
  OP_DSC_FIELD = "instance_name"
539
  __slots__ = OpCode.__slots__ + [
540
    "instance_name", "disk", "amount", "wait_for_sync",
541
    ]
542

    
543

    
544
# OS opcodes
545
class OpDiagnoseOS(OpCode):
546
  """Compute the list of guest operating systems."""
547
  OP_ID = "OP_OS_DIAGNOSE"
548
  __slots__ = OpCode.__slots__ + ["output_fields", "names"]
549

    
550

    
551
# Exports opcodes
552
class OpQueryExports(OpCode):
553
  """Compute the list of exported images."""
554
  OP_ID = "OP_BACKUP_QUERY"
555
  __slots__ = OpCode.__slots__ + ["nodes", "use_locking"]
556

    
557

    
558
class OpExportInstance(OpCode):
559
  """Export an instance."""
560
  OP_ID = "OP_BACKUP_EXPORT"
561
  OP_DSC_FIELD = "instance_name"
562
  __slots__ = OpCode.__slots__ + ["instance_name", "target_node", "shutdown"]
563

    
564

    
565
class OpRemoveExport(OpCode):
566
  """Remove an instance's export."""
567
  OP_ID = "OP_BACKUP_REMOVE"
568
  OP_DSC_FIELD = "instance_name"
569
  __slots__ = OpCode.__slots__ + ["instance_name"]
570

    
571

    
572
# Tags opcodes
573
class OpGetTags(OpCode):
574
  """Returns the tags of the given object."""
575
  OP_ID = "OP_TAGS_GET"
576
  OP_DSC_FIELD = "name"
577
  __slots__ = OpCode.__slots__ + ["kind", "name"]
578

    
579

    
580
class OpSearchTags(OpCode):
581
  """Searches the tags in the cluster for a given pattern."""
582
  OP_ID = "OP_TAGS_SEARCH"
583
  OP_DSC_FIELD = "pattern"
584
  __slots__ = OpCode.__slots__ + ["pattern"]
585

    
586

    
587
class OpAddTags(OpCode):
588
  """Add a list of tags on a given object."""
589
  OP_ID = "OP_TAGS_SET"
590
  __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
591

    
592

    
593
class OpDelTags(OpCode):
594
  """Remove a list of tags from a given object."""
595
  OP_ID = "OP_TAGS_DEL"
596
  __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
597

    
598

    
599
# Test opcodes
600
class OpTestDelay(OpCode):
601
  """Sleeps for a configured amount of time.
602

603
  This is used just for debugging and testing.
604

605
  Parameters:
606
    - duration: the time to sleep
607
    - on_master: if true, sleep on the master
608
    - on_nodes: list of nodes in which to sleep
609

610
  If the on_master parameter is true, it will execute a sleep on the
611
  master (before any node sleep).
612

613
  If the on_nodes list is not empty, it will sleep on those nodes
614
  (after the sleep on the master, if that is enabled).
615

616
  As an additional feature, the case of duration < 0 will be reported
617
  as an execution error, so this opcode can be used as a failure
618
  generator. The case of duration == 0 will not be treated specially.
619

620
  """
621
  OP_ID = "OP_TEST_DELAY"
622
  OP_DSC_FIELD = "duration"
623
  __slots__ = OpCode.__slots__ + ["duration", "on_master", "on_nodes"]
624

    
625

    
626
class OpTestAllocator(OpCode):
627
  """Allocator framework testing.
628

629
  This opcode has two modes:
630
    - gather and return allocator input for a given mode (allocate new
631
      or replace secondary) and a given instance definition (direction
632
      'in')
633
    - run a selected allocator for a given operation (as above) and
634
      return the allocator output (direction 'out')
635

636
  """
637
  OP_ID = "OP_TEST_ALLOCATOR"
638
  OP_DSC_FIELD = "allocator"
639
  __slots__ = OpCode.__slots__ + [
640
    "direction", "mode", "allocator", "name",
641
    "mem_size", "disks", "disk_template",
642
    "os", "tags", "nics", "vcpus", "hypervisor",
643
    ]
644

    
645
OP_MAPPING = dict([(v.OP_ID, v) for v in globals().values()
646
                   if (isinstance(v, type) and issubclass(v, OpCode) and
647
                       hasattr(v, "OP_ID"))])