Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 20777413

History | View | Annotate | Download (16.6 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
    for item in globals().values():
148
      if (isinstance(item, type) and
149
          issubclass(item, cls) and
150
          hasattr(item, "OP_ID") and
151
          getattr(item, "OP_ID") == op_id):
152
        op_class = item
153
        break
154
    if op_class is None:
155
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
156
                       op_id)
157
    op = op_class()
158
    new_data = data.copy()
159
    del new_data["OP_ID"]
160
    op.__setstate__(new_data)
161
    return op
162

    
163
  def Summary(self):
164
    """Generates a summary description of this opcode.
165

166
    """
167
    # all OP_ID start with OP_, we remove that
168
    txt = self.OP_ID[3:]
169
    field_name = getattr(self, "OP_DSC_FIELD", None)
170
    if field_name:
171
      field_value = getattr(self, field_name, None)
172
      txt = "%s(%s)" % (txt, field_value)
173
    return txt
174

    
175

    
176
# cluster opcodes
177

    
178
class OpDestroyCluster(OpCode):
179
  """Destroy the cluster.
180

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

184
  """
185
  OP_ID = "OP_CLUSTER_DESTROY"
186
  __slots__ = OpCode.__slots__ + []
187

    
188

    
189
class OpQueryClusterInfo(OpCode):
190
  """Query cluster information."""
191
  OP_ID = "OP_CLUSTER_QUERY"
192
  __slots__ = OpCode.__slots__ + []
193

    
194

    
195
class OpVerifyCluster(OpCode):
196
  """Verify the cluster state.
197

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

204
  """
205
  OP_ID = "OP_CLUSTER_VERIFY"
206
  __slots__ = OpCode.__slots__ + ["skip_checks"]
207

    
208

    
209
class OpVerifyDisks(OpCode):
210
  """Verify the cluster disks.
211

212
  Parameters: none
213

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

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

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

229
  """
230
  OP_ID = "OP_CLUSTER_VERIFY_DISKS"
231
  __slots__ = OpCode.__slots__ + []
232

    
233

    
234
class OpQueryConfigValues(OpCode):
235
  """Query cluster configuration values."""
236
  OP_ID = "OP_CLUSTER_CONFIG_QUERY"
237
  __slots__ = OpCode.__slots__ + ["output_fields"]
238

    
239

    
240
class OpRenameCluster(OpCode):
241
  """Rename the cluster.
242

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

248
  """
249
  OP_ID = "OP_CLUSTER_RENAME"
250
  OP_DSC_FIELD = "name"
251
  __slots__ = OpCode.__slots__ + ["name"]
252

    
253

    
254
class OpSetClusterParams(OpCode):
255
  """Change the parameters of the cluster.
256

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

260
  """
261
  OP_ID = "OP_CLUSTER_SET_PARAMS"
262
  __slots__ = OpCode.__slots__ + [
263
    "vg_name",
264
    "enabled_hypervisors",
265
    "hvparams",
266
    "beparams",
267
    "nicparams",
268
    "candidate_pool_size",
269
    ]
270

    
271

    
272
class OpRedistributeConfig(OpCode):
273
  """Force a full push of the cluster configuration.
274

275
  """
276
  OP_ID = "OP_CLUSTER_REDIST_CONF"
277
  __slots__ = OpCode.__slots__ + [
278
    ]
279

    
280
# node opcodes
281

    
282
class OpRemoveNode(OpCode):
283
  """Remove a node.
284

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

289
  """
290
  OP_ID = "OP_NODE_REMOVE"
291
  OP_DSC_FIELD = "node_name"
292
  __slots__ = OpCode.__slots__ + ["node_name"]
293

    
294

    
295
class OpAddNode(OpCode):
296
  """Add a node to the cluster.
297

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

316
  """
317
  OP_ID = "OP_NODE_ADD"
318
  OP_DSC_FIELD = "node_name"
319
  __slots__ = OpCode.__slots__ + [
320
    "node_name", "primary_ip", "secondary_ip", "readd",
321
    ]
322

    
323

    
324
class OpQueryNodes(OpCode):
325
  """Compute the list of nodes."""
326
  OP_ID = "OP_NODE_QUERY"
327
  __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
328

    
329

    
330
class OpQueryNodeVolumes(OpCode):
331
  """Get list of volumes on node."""
332
  OP_ID = "OP_NODE_QUERYVOLS"
333
  __slots__ = OpCode.__slots__ + ["nodes", "output_fields"]
334

    
335

    
336
class OpSetNodeParams(OpCode):
337
  """Change the parameters of a node."""
338
  OP_ID = "OP_NODE_SET_PARAMS"
339
  OP_DSC_FIELD = "node_name"
340
  __slots__ = OpCode.__slots__ + [
341
    "node_name",
342
    "force",
343
    "master_candidate",
344
    "offline",
345
    "drained",
346
    ]
347

    
348

    
349
class OpPowercycleNode(OpCode):
350
  """Tries to powercycle a node."""
351
  OP_ID = "OP_NODE_POWERCYCLE"
352
  OP_DSC_FIELD = "node_name"
353
  __slots__ = OpCode.__slots__ + [
354
    "node_name",
355
    "force",
356
    ]
357

    
358
# instance opcodes
359

    
360
class OpCreateInstance(OpCode):
361
  """Create an instance."""
362
  OP_ID = "OP_INSTANCE_CREATE"
363
  OP_DSC_FIELD = "instance_name"
364
  __slots__ = OpCode.__slots__ + [
365
    "instance_name", "os_type", "pnode",
366
    "disk_template", "snode", "mode",
367
    "disks", "nics",
368
    "src_node", "src_path", "start",
369
    "wait_for_sync", "ip_check",
370
    "file_storage_dir", "file_driver",
371
    "iallocator",
372
    "hypervisor", "hvparams", "beparams",
373
    "dry_run",
374
    ]
375

    
376

    
377
class OpReinstallInstance(OpCode):
378
  """Reinstall an instance's OS."""
379
  OP_ID = "OP_INSTANCE_REINSTALL"
380
  OP_DSC_FIELD = "instance_name"
381
  __slots__ = OpCode.__slots__ + ["instance_name", "os_type"]
382

    
383

    
384
class OpRemoveInstance(OpCode):
385
  """Remove an instance."""
386
  OP_ID = "OP_INSTANCE_REMOVE"
387
  OP_DSC_FIELD = "instance_name"
388
  __slots__ = OpCode.__slots__ + ["instance_name", "ignore_failures"]
389

    
390

    
391
class OpRenameInstance(OpCode):
392
  """Rename an instance."""
393
  OP_ID = "OP_INSTANCE_RENAME"
394
  __slots__ = OpCode.__slots__ + [
395
    "instance_name", "ignore_ip", "new_name",
396
    ]
397

    
398

    
399
class OpStartupInstance(OpCode):
400
  """Startup an instance."""
401
  OP_ID = "OP_INSTANCE_STARTUP"
402
  OP_DSC_FIELD = "instance_name"
403
  __slots__ = OpCode.__slots__ + [
404
    "instance_name", "force", "hvparams", "beparams",
405
    ]
406

    
407

    
408
class OpShutdownInstance(OpCode):
409
  """Shutdown an instance."""
410
  OP_ID = "OP_INSTANCE_SHUTDOWN"
411
  OP_DSC_FIELD = "instance_name"
412
  __slots__ = OpCode.__slots__ + ["instance_name"]
413

    
414

    
415
class OpRebootInstance(OpCode):
416
  """Reboot an instance."""
417
  OP_ID = "OP_INSTANCE_REBOOT"
418
  OP_DSC_FIELD = "instance_name"
419
  __slots__ = OpCode.__slots__ + [
420
    "instance_name", "reboot_type", "ignore_secondaries",
421
    ]
422

    
423

    
424
class OpReplaceDisks(OpCode):
425
  """Replace the disks of an instance."""
426
  OP_ID = "OP_INSTANCE_REPLACE_DISKS"
427
  OP_DSC_FIELD = "instance_name"
428
  __slots__ = OpCode.__slots__ + [
429
    "instance_name", "remote_node", "mode", "disks", "iallocator",
430
    ]
431

    
432

    
433
class OpFailoverInstance(OpCode):
434
  """Failover an instance."""
435
  OP_ID = "OP_INSTANCE_FAILOVER"
436
  OP_DSC_FIELD = "instance_name"
437
  __slots__ = OpCode.__slots__ + ["instance_name", "ignore_consistency"]
438

    
439

    
440
class OpMigrateInstance(OpCode):
441
  """Migrate an instance.
442

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

446
  @ivar instance_name: the name of the instance
447

448
  """
449
  OP_ID = "OP_INSTANCE_MIGRATE"
450
  OP_DSC_FIELD = "instance_name"
451
  __slots__ = OpCode.__slots__ + ["instance_name", "live", "cleanup"]
452

    
453

    
454
class OpConnectConsole(OpCode):
455
  """Connect to an instance's console."""
456
  OP_ID = "OP_INSTANCE_CONSOLE"
457
  OP_DSC_FIELD = "instance_name"
458
  __slots__ = OpCode.__slots__ + ["instance_name"]
459

    
460

    
461
class OpActivateInstanceDisks(OpCode):
462
  """Activate an instance's disks."""
463
  OP_ID = "OP_INSTANCE_ACTIVATE_DISKS"
464
  OP_DSC_FIELD = "instance_name"
465
  __slots__ = OpCode.__slots__ + ["instance_name"]
466

    
467

    
468
class OpDeactivateInstanceDisks(OpCode):
469
  """Deactivate an instance's disks."""
470
  OP_ID = "OP_INSTANCE_DEACTIVATE_DISKS"
471
  OP_DSC_FIELD = "instance_name"
472
  __slots__ = OpCode.__slots__ + ["instance_name"]
473

    
474

    
475
class OpQueryInstances(OpCode):
476
  """Compute the list of instances."""
477
  OP_ID = "OP_INSTANCE_QUERY"
478
  __slots__ = OpCode.__slots__ + ["output_fields", "names", "use_locking"]
479

    
480

    
481
class OpQueryInstanceData(OpCode):
482
  """Compute the run-time status of instances."""
483
  OP_ID = "OP_INSTANCE_QUERY_DATA"
484
  __slots__ = OpCode.__slots__ + ["instances", "static"]
485

    
486

    
487
class OpSetInstanceParams(OpCode):
488
  """Change the parameters of an instance."""
489
  OP_ID = "OP_INSTANCE_SET_PARAMS"
490
  OP_DSC_FIELD = "instance_name"
491
  __slots__ = OpCode.__slots__ + [
492
    "instance_name",
493
    "hvparams", "beparams", "force",
494
    "nics", "disks",
495
    ]
496

    
497

    
498
class OpGrowDisk(OpCode):
499
  """Grow a disk of an instance."""
500
  OP_ID = "OP_INSTANCE_GROW_DISK"
501
  OP_DSC_FIELD = "instance_name"
502
  __slots__ = OpCode.__slots__ + [
503
    "instance_name", "disk", "amount", "wait_for_sync",
504
    ]
505

    
506

    
507
# OS opcodes
508
class OpDiagnoseOS(OpCode):
509
  """Compute the list of guest operating systems."""
510
  OP_ID = "OP_OS_DIAGNOSE"
511
  __slots__ = OpCode.__slots__ + ["output_fields", "names"]
512

    
513

    
514
# Exports opcodes
515
class OpQueryExports(OpCode):
516
  """Compute the list of exported images."""
517
  OP_ID = "OP_BACKUP_QUERY"
518
  __slots__ = OpCode.__slots__ + ["nodes", "use_locking"]
519

    
520

    
521
class OpExportInstance(OpCode):
522
  """Export an instance."""
523
  OP_ID = "OP_BACKUP_EXPORT"
524
  OP_DSC_FIELD = "instance_name"
525
  __slots__ = OpCode.__slots__ + ["instance_name", "target_node", "shutdown"]
526

    
527

    
528
class OpRemoveExport(OpCode):
529
  """Remove an instance's export."""
530
  OP_ID = "OP_BACKUP_REMOVE"
531
  OP_DSC_FIELD = "instance_name"
532
  __slots__ = OpCode.__slots__ + ["instance_name"]
533

    
534

    
535
# Tags opcodes
536
class OpGetTags(OpCode):
537
  """Returns the tags of the given object."""
538
  OP_ID = "OP_TAGS_GET"
539
  OP_DSC_FIELD = "name"
540
  __slots__ = OpCode.__slots__ + ["kind", "name"]
541

    
542

    
543
class OpSearchTags(OpCode):
544
  """Searches the tags in the cluster for a given pattern."""
545
  OP_ID = "OP_TAGS_SEARCH"
546
  OP_DSC_FIELD = "pattern"
547
  __slots__ = OpCode.__slots__ + ["pattern"]
548

    
549

    
550
class OpAddTags(OpCode):
551
  """Add a list of tags on a given object."""
552
  OP_ID = "OP_TAGS_SET"
553
  __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
554

    
555

    
556
class OpDelTags(OpCode):
557
  """Remove a list of tags from a given object."""
558
  OP_ID = "OP_TAGS_DEL"
559
  __slots__ = OpCode.__slots__ + ["kind", "name", "tags"]
560

    
561

    
562
# Test opcodes
563
class OpTestDelay(OpCode):
564
  """Sleeps for a configured amount of time.
565

566
  This is used just for debugging and testing.
567

568
  Parameters:
569
    - duration: the time to sleep
570
    - on_master: if true, sleep on the master
571
    - on_nodes: list of nodes in which to sleep
572

573
  If the on_master parameter is true, it will execute a sleep on the
574
  master (before any node sleep).
575

576
  If the on_nodes list is not empty, it will sleep on those nodes
577
  (after the sleep on the master, if that is enabled).
578

579
  As an additional feature, the case of duration < 0 will be reported
580
  as an execution error, so this opcode can be used as a failure
581
  generator. The case of duration == 0 will not be treated specially.
582

583
  """
584
  OP_ID = "OP_TEST_DELAY"
585
  OP_DSC_FIELD = "duration"
586
  __slots__ = OpCode.__slots__ + ["duration", "on_master", "on_nodes"]
587

    
588

    
589
class OpTestAllocator(OpCode):
590
  """Allocator framework testing.
591

592
  This opcode has two modes:
593
    - gather and return allocator input for a given mode (allocate new
594
      or replace secondary) and a given instance definition (direction
595
      'in')
596
    - run a selected allocator for a given operation (as above) and
597
      return the allocator output (direction 'out')
598

599
  """
600
  OP_ID = "OP_TEST_ALLOCATOR"
601
  OP_DSC_FIELD = "allocator"
602
  __slots__ = OpCode.__slots__ + [
603
    "direction", "mode", "allocator", "name",
604
    "mem_size", "disks", "disk_template",
605
    "os", "tags", "nics", "vcpus", "hypervisor",
606
    ]