Statistics
| Branch: | Tag: | Revision:

root / lib / objects.py @ 9ff7e35c

History | View | Annotate | Download (17.2 kB)

1
#!/usr/bin/python
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
"""Transportable objects for Ganeti.
23

24
This module provides small, mostly data-only objects which are safe to
25
pass to and from external parties.
26

27
"""
28

    
29

    
30
import simplejson
31
from cStringIO import StringIO
32
import ConfigParser
33
import re
34

    
35
from ganeti import errors
36
from ganeti import constants
37

    
38

    
39
__all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
40
           "OS", "Node", "Cluster"]
41

    
42

    
43
class ConfigObject(object):
44
  """A generic config object.
45

46
  It has the following properties:
47

48
    - provides somewhat safe recursive unpickling and pickling for its classes
49
    - unset attributes which are defined in slots are always returned
50
      as None instead of raising an error
51

52
  Classes derived from this must always declare __slots__ (we use many
53
  config objects and the memory reduction is useful.
54

55
  """
56
  __slots__ = []
57

    
58
  def __init__(self, **kwargs):
59
    for k, v in kwargs.iteritems():
60
      setattr(self, k, v)
61

    
62
  def __getattr__(self, name):
63
    if name not in self.__slots__:
64
      raise AttributeError("Invalid object attribute %s.%s" %
65
                           (type(self).__name__, name))
66
    return None
67

    
68
  def __setitem__(self, key, value):
69
    if key not in self.__slots__:
70
      raise KeyError(key)
71
    setattr(self, key, value)
72

    
73
  def __getstate__(self):
74
    state = {}
75
    for name in self.__slots__:
76
      if hasattr(self, name):
77
        state[name] = getattr(self, name)
78
    return state
79

    
80
  def __setstate__(self, state):
81
    for name in state:
82
      if name in self.__slots__:
83
        setattr(self, name, state[name])
84

    
85
  def Dump(self, fobj):
86
    """Dump to a file object.
87

88
    """
89
    simplejson.dump(self.ToDict(), fobj)
90

    
91
  @classmethod
92
  def Load(cls, fobj):
93
    """Load data from the given stream.
94

95
    """
96
    return cls.FromDict(simplejson.load(fobj))
97

    
98
  def Dumps(self):
99
    """Dump and return the string representation."""
100
    buf = StringIO()
101
    self.Dump(buf)
102
    return buf.getvalue()
103

    
104
  @classmethod
105
  def Loads(cls, data):
106
    """Load data from a string."""
107
    return cls.Load(StringIO(data))
108

    
109
  def ToDict(self):
110
    """Convert to a dict holding only standard python types.
111

112
    The generic routine just dumps all of this object's attributes in
113
    a dict. It does not work if the class has children who are
114
    ConfigObjects themselves (e.g. the nics list in an Instance), in
115
    which case the object should subclass the function in order to
116
    make sure all objects returned are only standard python types.
117

118
    """
119
    return dict([(k, getattr(self, k, None)) for k in self.__slots__])
120

    
121
  @classmethod
122
  def FromDict(cls, val):
123
    """Create an object from a dictionary.
124

125
    This generic routine takes a dict, instantiates a new instance of
126
    the given class, and sets attributes based on the dict content.
127

128
    As for `ToDict`, this does not work if the class has children
129
    who are ConfigObjects themselves (e.g. the nics list in an
130
    Instance), in which case the object should subclass the function
131
    and alter the objects.
132

133
    """
134
    if not isinstance(val, dict):
135
      raise errors.ConfigurationError("Invalid object passed to FromDict:"
136
                                      " expected dict, got %s" % type(val))
137
    val_str = dict([(str(k), v) for k, v in val.iteritems()])
138
    obj = cls(**val_str)
139
    return obj
140

    
141
  @staticmethod
142
  def _ContainerToDicts(container):
143
    """Convert the elements of a container to standard python types.
144

145
    This method converts a container with elements derived from
146
    ConfigData to standard python types. If the container is a dict,
147
    we don't touch the keys, only the values.
148

149
    """
150
    if isinstance(container, dict):
151
      ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
152
    elif isinstance(container, (list, tuple, set, frozenset)):
153
      ret = [elem.ToDict() for elem in container]
154
    else:
155
      raise TypeError("Invalid type %s passed to _ContainerToDicts" %
156
                      type(container))
157
    return ret
158

    
159
  @staticmethod
160
  def _ContainerFromDicts(source, c_type, e_type):
161
    """Convert a container from standard python types.
162

163
    This method converts a container with standard python types to
164
    ConfigData objects. If the container is a dict, we don't touch the
165
    keys, only the values.
166

167
    """
168
    if not isinstance(c_type, type):
169
      raise TypeError("Container type %s passed to _ContainerFromDicts is"
170
                      " not a type" % type(c_type))
171
    if c_type is dict:
172
      ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
173
    elif c_type in (list, tuple, set, frozenset):
174
      ret = c_type([e_type.FromDict(elem) for elem in source])
175
    else:
176
      raise TypeError("Invalid container type %s passed to"
177
                      " _ContainerFromDicts" % c_type)
178
    return ret
179

    
180
  def __repr__(self):
181
    """Implement __repr__ for ConfigObjects."""
182
    return repr(self.ToDict())
183

    
184

    
185
class TaggableObject(ConfigObject):
186
  """An generic class supporting tags.
187

188
  """
189
  __slots__ = ConfigObject.__slots__ + ["tags"]
190

    
191
  @staticmethod
192
  def ValidateTag(tag):
193
    """Check if a tag is valid.
194

195
    If the tag is invalid, an errors.TagError will be raised. The
196
    function has no return value.
197

198
    """
199
    if not isinstance(tag, basestring):
200
      raise errors.TagError("Invalid tag type (not a string)")
201
    if len(tag) > constants.MAX_TAG_LEN:
202
      raise errors.TagError("Tag too long (>%d characters)" %
203
                            constants.MAX_TAG_LEN)
204
    if not tag:
205
      raise errors.TagError("Tags cannot be empty")
206
    if not re.match("^[ \w.+*/:-]+$", tag):
207
      raise errors.TagError("Tag contains invalid characters")
208

    
209
  def GetTags(self):
210
    """Return the tags list.
211

212
    """
213
    tags = getattr(self, "tags", None)
214
    if tags is None:
215
      tags = self.tags = set()
216
    return tags
217

    
218
  def AddTag(self, tag):
219
    """Add a new tag.
220

221
    """
222
    self.ValidateTag(tag)
223
    tags = self.GetTags()
224
    if len(tags) >= constants.MAX_TAGS_PER_OBJ:
225
      raise errors.TagError("Too many tags")
226
    self.GetTags().add(tag)
227

    
228
  def RemoveTag(self, tag):
229
    """Remove a tag.
230

231
    """
232
    self.ValidateTag(tag)
233
    tags = self.GetTags()
234
    try:
235
      tags.remove(tag)
236
    except KeyError:
237
      raise errors.TagError("Tag not found")
238

    
239
  def ToDict(self):
240
    """Taggable-object-specific conversion to standard python types.
241

242
    This replaces the tags set with a list.
243

244
    """
245
    bo = super(TaggableObject, self).ToDict()
246

    
247
    tags = bo.get("tags", None)
248
    if isinstance(tags, set):
249
      bo["tags"] = list(tags)
250
    return bo
251

    
252
  @classmethod
253
  def FromDict(cls, val):
254
    """Custom function for instances.
255

256
    """
257
    obj = super(TaggableObject, cls).FromDict(val)
258
    if hasattr(obj, "tags") and isinstance(obj.tags, list):
259
      obj.tags = set(obj.tags)
260
    return obj
261

    
262

    
263
class ConfigData(ConfigObject):
264
  """Top-level config object."""
265
  __slots__ = ["cluster", "nodes", "instances"]
266

    
267
  def ToDict(self):
268
    """Custom function for top-level config data.
269

270
    This just replaces the list of instances, nodes and the cluster
271
    with standard python types.
272

273
    """
274
    mydict = super(ConfigData, self).ToDict()
275
    mydict["cluster"] = mydict["cluster"].ToDict()
276
    for key in "nodes", "instances":
277
      mydict[key] = self._ContainerToDicts(mydict[key])
278

    
279
    return mydict
280

    
281
  @classmethod
282
  def FromDict(cls, val):
283
    """Custom function for top-level config data
284

285
    """
286
    obj = super(ConfigData, cls).FromDict(val)
287
    obj.cluster = Cluster.FromDict(obj.cluster)
288
    obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
289
    obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
290
    return obj
291

    
292

    
293
class NIC(ConfigObject):
294
  """Config object representing a network card."""
295
  __slots__ = ["mac", "ip", "bridge"]
296

    
297

    
298
class Disk(ConfigObject):
299
  """Config object representing a block device."""
300
  __slots__ = ["dev_type", "logical_id", "physical_id",
301
               "children", "iv_name", "size"]
302

    
303
  def CreateOnSecondary(self):
304
    """Test if this device needs to be created on a secondary node."""
305
    return self.dev_type in ("drbd", "lvm")
306

    
307
  def AssembleOnSecondary(self):
308
    """Test if this device needs to be assembled on a secondary node."""
309
    return self.dev_type in ("drbd", "lvm")
310

    
311
  def OpenOnSecondary(self):
312
    """Test if this device needs to be opened on a secondary node."""
313
    return self.dev_type in ("lvm",)
314

    
315
  def GetNodes(self, node):
316
    """This function returns the nodes this device lives on.
317

318
    Given the node on which the parent of the device lives on (or, in
319
    case of a top-level device, the primary node of the devices'
320
    instance), this function will return a list of nodes on which this
321
    devices needs to (or can) be assembled.
322

323
    """
324
    if self.dev_type == "lvm" or self.dev_type == "md_raid1":
325
      result = [node]
326
    elif self.dev_type == "drbd":
327
      result = [self.logical_id[0], self.logical_id[1]]
328
      if node not in result:
329
        raise errors.ConfigurationError("DRBD device passed unknown node")
330
    else:
331
      raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
332
    return result
333

    
334
  def ComputeNodeTree(self, parent_node):
335
    """Compute the node/disk tree for this disk and its children.
336

337
    This method, given the node on which the parent disk lives, will
338
    return the list of all (node, disk) pairs which describe the disk
339
    tree in the most compact way. For example, a md/drbd/lvm stack
340
    will be returned as (primary_node, md) and (secondary_node, drbd)
341
    which represents all the top-level devices on the nodes. This
342
    means that on the primary node we need to activate the the md (and
343
    recursively all its children) and on the secondary node we need to
344
    activate the drbd device (and its children, the two lvm volumes).
345

346
    """
347
    my_nodes = self.GetNodes(parent_node)
348
    result = [(node, self) for node in my_nodes]
349
    if not self.children:
350
      # leaf device
351
      return result
352
    for node in my_nodes:
353
      for child in self.children:
354
        child_result = child.ComputeNodeTree(node)
355
        if len(child_result) == 1:
356
          # child (and all its descendants) is simple, doesn't split
357
          # over multiple hosts, so we don't need to describe it, our
358
          # own entry for this node describes it completely
359
          continue
360
        else:
361
          # check if child nodes differ from my nodes; note that
362
          # subdisk can differ from the child itself, and be instead
363
          # one of its descendants
364
          for subnode, subdisk in child_result:
365
            if subnode not in my_nodes:
366
              result.append((subnode, subdisk))
367
            # otherwise child is under our own node, so we ignore this
368
            # entry (but probably the other results in the list will
369
            # be different)
370
    return result
371

    
372
  def ToDict(self):
373
    """Disk-specific conversion to standard python types.
374

375
    This replaces the children lists of objects with lists of
376
    standard python types.
377

378
    """
379
    bo = super(Disk, self).ToDict()
380

    
381
    for attr in ("children",):
382
      alist = bo.get(attr, None)
383
      if alist:
384
        bo[attr] = self._ContainerToDicts(alist)
385
    return bo
386

    
387
  @classmethod
388
  def FromDict(cls, val):
389
    """Custom function for Disks
390

391
    """
392
    obj = super(Disk, cls).FromDict(val)
393
    if obj.children:
394
      obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
395
    if obj.logical_id and isinstance(obj.logical_id, list):
396
      obj.logical_id = tuple(obj.logical_id)
397
    if obj.physical_id and isinstance(obj.physical_id, list):
398
      obj.physical_id = tuple(obj.physical_id)
399
    return obj
400

    
401

    
402
class Instance(TaggableObject):
403
  """Config object representing an instance."""
404
  __slots__ = TaggableObject.__slots__ + [
405
    "name",
406
    "primary_node",
407
    "os",
408
    "status",
409
    "memory",
410
    "vcpus",
411
    "nics",
412
    "disks",
413
    "disk_template",
414
    ]
415

    
416
  def _ComputeSecondaryNodes(self):
417
    """Compute the list of secondary nodes.
418

419
    Since the data is already there (in the drbd disks), keeping it as
420
    a separate normal attribute is redundant and if not properly
421
    synchronised can cause problems. Thus it's better to compute it
422
    dynamically.
423

424
    """
425
    def _Helper(primary, sec_nodes, device):
426
      """Recursively computes secondary nodes given a top device."""
427
      if device.dev_type == 'drbd':
428
        nodea, nodeb, dummy = device.logical_id
429
        if nodea == primary:
430
          candidate = nodeb
431
        else:
432
          candidate = nodea
433
        if candidate not in sec_nodes:
434
          sec_nodes.append(candidate)
435
      if device.children:
436
        for child in device.children:
437
          _Helper(primary, sec_nodes, child)
438

    
439
    secondary_nodes = []
440
    for device in self.disks:
441
      _Helper(self.primary_node, secondary_nodes, device)
442
    return tuple(secondary_nodes)
443

    
444
  secondary_nodes = property(_ComputeSecondaryNodes, None, None,
445
                             "List of secondary nodes")
446

    
447
  def MapLVsByNode(self, lvmap=None, devs=None, node=None):
448
    """Provide a mapping of nodes to LVs this instance owns.
449

450
    This function figures out what logical volumes should belong on which
451
    nodes, recursing through a device tree.
452

453
    Args:
454
      lvmap: (optional) a dictionary to receive the 'node' : ['lv', ...] data.
455

456
    Returns:
457
      None if lvmap arg is given.
458
      Otherwise, { 'nodename' : ['volume1', 'volume2', ...], ... }
459

460
    """
461
    if node == None:
462
      node = self.primary_node
463

    
464
    if lvmap is None:
465
      lvmap = { node : [] }
466
      ret = lvmap
467
    else:
468
      if not node in lvmap:
469
        lvmap[node] = []
470
      ret = None
471

    
472
    if not devs:
473
      devs = self.disks
474

    
475
    for dev in devs:
476
      if dev.dev_type == "lvm":
477
        lvmap[node].append(dev.logical_id[1])
478

    
479
      elif dev.dev_type == "drbd":
480
        if dev.logical_id[0] not in lvmap:
481
          lvmap[dev.logical_id[0]] = []
482

    
483
        if dev.logical_id[1] not in lvmap:
484
          lvmap[dev.logical_id[1]] = []
485

    
486
        if dev.children:
487
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
488
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
489

    
490
      elif dev.children:
491
        self.MapLVsByNode(lvmap, dev.children, node)
492

    
493
    return ret
494

    
495
  def FindDisk(self, name):
496
    """Find a disk given having a specified name.
497

498
    This will return the disk which has the given iv_name.
499

500
    """
501
    for disk in self.disks:
502
      if disk.iv_name == name:
503
        return disk
504

    
505
    return None
506

    
507
  def ToDict(self):
508
    """Instance-specific conversion to standard python types.
509

510
    This replaces the children lists of objects with lists of standard
511
    python types.
512

513
    """
514
    bo = super(Instance, self).ToDict()
515

    
516
    for attr in "nics", "disks":
517
      alist = bo.get(attr, None)
518
      if alist:
519
        nlist = self._ContainerToDicts(alist)
520
      else:
521
        nlist = []
522
      bo[attr] = nlist
523
    return bo
524

    
525
  @classmethod
526
  def FromDict(cls, val):
527
    """Custom function for instances.
528

529
    """
530
    obj = super(Instance, cls).FromDict(val)
531
    obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
532
    obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
533
    return obj
534

    
535

    
536
class OS(ConfigObject):
537
  """Config object representing an operating system."""
538
  __slots__ = [
539
    "name",
540
    "path",
541
    "api_version",
542
    "create_script",
543
    "export_script",
544
    "import_script",
545
    "rename_script",
546
    ]
547

    
548

    
549
class Node(TaggableObject):
550
  """Config object representing a node."""
551
  __slots__ = TaggableObject.__slots__ + [
552
    "name",
553
    "primary_ip",
554
    "secondary_ip",
555
    ]
556

    
557

    
558
class Cluster(TaggableObject):
559
  """Config object representing the cluster."""
560
  __slots__ = TaggableObject.__slots__ + [
561
    "config_version",
562
    "serial_no",
563
    "rsahostkeypub",
564
    "highest_used_port",
565
    "tcpudp_port_pool",
566
    "mac_prefix",
567
    "volume_group_name",
568
    "default_bridge",
569
    ]
570

    
571
  def ToDict(self):
572
    """Custom function for cluster.
573

574
    """
575
    mydict = super(TaggableObject, self).ToDict()
576
    mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
577
    return mydict
578

    
579
  @classmethod
580
  def FromDict(cls, val):
581
    """Custom function for cluster.
582

583
    """
584
    obj = super(TaggableObject, cls).FromDict(val)
585
    if not isinstance(obj.tcpudp_port_pool, set):
586
      obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
587
    return obj
588

    
589

    
590
class SerializableConfigParser(ConfigParser.SafeConfigParser):
591
  """Simple wrapper over ConfigParse that allows serialization.
592

593
  This class is basically ConfigParser.SafeConfigParser with two
594
  additional methods that allow it to serialize/unserialize to/from a
595
  buffer.
596

597
  """
598
  def Dumps(self):
599
    """Dump this instance and return the string representation."""
600
    buf = StringIO()
601
    self.write(buf)
602
    return buf.getvalue()
603

    
604
  @staticmethod
605
  def Loads(data):
606
    """Load data from a string."""
607
    buf = StringIO(data)
608
    cfp = SerializableConfigParser()
609
    cfp.readfp(buf)
610
    return cfp