objects: Add custom de-/serializing code for query responses
[ganeti-local] / lib / objects.py
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 """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 # pylint: disable-msg=E0203,W0201
30
31 # E0203: Access to member %r before its definition, since we use
32 # objects.py which doesn't explicitely initialise its members
33
34 # W0201: Attribute '%s' defined outside __init__
35
36 import ConfigParser
37 import re
38 import copy
39 import time
40 from cStringIO import StringIO
41
42 from ganeti import errors
43 from ganeti import constants
44
45 from socket import AF_INET
46
47
48 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
49            "OS", "Node", "NodeGroup", "Cluster", "FillDict"]
50
51 _TIMESTAMPS = ["ctime", "mtime"]
52 _UUID = ["uuid"]
53
54
55 def FillDict(defaults_dict, custom_dict, skip_keys=None):
56   """Basic function to apply settings on top a default dict.
57
58   @type defaults_dict: dict
59   @param defaults_dict: dictionary holding the default values
60   @type custom_dict: dict
61   @param custom_dict: dictionary holding customized value
62   @type skip_keys: list
63   @param skip_keys: which keys not to fill
64   @rtype: dict
65   @return: dict with the 'full' values
66
67   """
68   ret_dict = copy.deepcopy(defaults_dict)
69   ret_dict.update(custom_dict)
70   if skip_keys:
71     for k in skip_keys:
72       try:
73         del ret_dict[k]
74       except KeyError:
75         pass
76   return ret_dict
77
78
79 def UpgradeGroupedParams(target, defaults):
80   """Update all groups for the target parameter.
81
82   @type target: dict of dicts
83   @param target: {group: {parameter: value}}
84   @type defaults: dict
85   @param defaults: default parameter values
86
87   """
88   if target is None:
89     target = {constants.PP_DEFAULT: defaults}
90   else:
91     for group in target:
92       target[group] = FillDict(defaults, target[group])
93   return target
94
95
96 class ConfigObject(object):
97   """A generic config object.
98
99   It has the following properties:
100
101     - provides somewhat safe recursive unpickling and pickling for its classes
102     - unset attributes which are defined in slots are always returned
103       as None instead of raising an error
104
105   Classes derived from this must always declare __slots__ (we use many
106   config objects and the memory reduction is useful)
107
108   """
109   __slots__ = []
110
111   def __init__(self, **kwargs):
112     for k, v in kwargs.iteritems():
113       setattr(self, k, v)
114
115   def __getattr__(self, name):
116     if name not in self._all_slots():
117       raise AttributeError("Invalid object attribute %s.%s" %
118                            (type(self).__name__, name))
119     return None
120
121   def __setstate__(self, state):
122     slots = self._all_slots()
123     for name in state:
124       if name in slots:
125         setattr(self, name, state[name])
126
127   @classmethod
128   def _all_slots(cls):
129     """Compute the list of all declared slots for a class.
130
131     """
132     slots = []
133     for parent in cls.__mro__:
134       slots.extend(getattr(parent, "__slots__", []))
135     return slots
136
137   def ToDict(self):
138     """Convert to a dict holding only standard python types.
139
140     The generic routine just dumps all of this object's attributes in
141     a dict. It does not work if the class has children who are
142     ConfigObjects themselves (e.g. the nics list in an Instance), in
143     which case the object should subclass the function in order to
144     make sure all objects returned are only standard python types.
145
146     """
147     result = {}
148     for name in self._all_slots():
149       value = getattr(self, name, None)
150       if value is not None:
151         result[name] = value
152     return result
153
154   __getstate__ = ToDict
155
156   @classmethod
157   def FromDict(cls, val):
158     """Create an object from a dictionary.
159
160     This generic routine takes a dict, instantiates a new instance of
161     the given class, and sets attributes based on the dict content.
162
163     As for `ToDict`, this does not work if the class has children
164     who are ConfigObjects themselves (e.g. the nics list in an
165     Instance), in which case the object should subclass the function
166     and alter the objects.
167
168     """
169     if not isinstance(val, dict):
170       raise errors.ConfigurationError("Invalid object passed to FromDict:"
171                                       " expected dict, got %s" % type(val))
172     val_str = dict([(str(k), v) for k, v in val.iteritems()])
173     obj = cls(**val_str) # pylint: disable-msg=W0142
174     return obj
175
176   @staticmethod
177   def _ContainerToDicts(container):
178     """Convert the elements of a container to standard python types.
179
180     This method converts a container with elements derived from
181     ConfigData to standard python types. If the container is a dict,
182     we don't touch the keys, only the values.
183
184     """
185     if isinstance(container, dict):
186       ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
187     elif isinstance(container, (list, tuple, set, frozenset)):
188       ret = [elem.ToDict() for elem in container]
189     else:
190       raise TypeError("Invalid type %s passed to _ContainerToDicts" %
191                       type(container))
192     return ret
193
194   @staticmethod
195   def _ContainerFromDicts(source, c_type, e_type):
196     """Convert a container from standard python types.
197
198     This method converts a container with standard python types to
199     ConfigData objects. If the container is a dict, we don't touch the
200     keys, only the values.
201
202     """
203     if not isinstance(c_type, type):
204       raise TypeError("Container type %s passed to _ContainerFromDicts is"
205                       " not a type" % type(c_type))
206     if source is None:
207       source = c_type()
208     if c_type is dict:
209       ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
210     elif c_type in (list, tuple, set, frozenset):
211       ret = c_type([e_type.FromDict(elem) for elem in source])
212     else:
213       raise TypeError("Invalid container type %s passed to"
214                       " _ContainerFromDicts" % c_type)
215     return ret
216
217   def Copy(self):
218     """Makes a deep copy of the current object and its children.
219
220     """
221     dict_form = self.ToDict()
222     clone_obj = self.__class__.FromDict(dict_form)
223     return clone_obj
224
225   def __repr__(self):
226     """Implement __repr__ for ConfigObjects."""
227     return repr(self.ToDict())
228
229   def UpgradeConfig(self):
230     """Fill defaults for missing configuration values.
231
232     This method will be called at configuration load time, and its
233     implementation will be object dependent.
234
235     """
236     pass
237
238
239 class TaggableObject(ConfigObject):
240   """An generic class supporting tags.
241
242   """
243   __slots__ = ["tags"]
244   VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
245
246   @classmethod
247   def ValidateTag(cls, tag):
248     """Check if a tag is valid.
249
250     If the tag is invalid, an errors.TagError will be raised. The
251     function has no return value.
252
253     """
254     if not isinstance(tag, basestring):
255       raise errors.TagError("Invalid tag type (not a string)")
256     if len(tag) > constants.MAX_TAG_LEN:
257       raise errors.TagError("Tag too long (>%d characters)" %
258                             constants.MAX_TAG_LEN)
259     if not tag:
260       raise errors.TagError("Tags cannot be empty")
261     if not cls.VALID_TAG_RE.match(tag):
262       raise errors.TagError("Tag contains invalid characters")
263
264   def GetTags(self):
265     """Return the tags list.
266
267     """
268     tags = getattr(self, "tags", None)
269     if tags is None:
270       tags = self.tags = set()
271     return tags
272
273   def AddTag(self, tag):
274     """Add a new tag.
275
276     """
277     self.ValidateTag(tag)
278     tags = self.GetTags()
279     if len(tags) >= constants.MAX_TAGS_PER_OBJ:
280       raise errors.TagError("Too many tags")
281     self.GetTags().add(tag)
282
283   def RemoveTag(self, tag):
284     """Remove a tag.
285
286     """
287     self.ValidateTag(tag)
288     tags = self.GetTags()
289     try:
290       tags.remove(tag)
291     except KeyError:
292       raise errors.TagError("Tag not found")
293
294   def ToDict(self):
295     """Taggable-object-specific conversion to standard python types.
296
297     This replaces the tags set with a list.
298
299     """
300     bo = super(TaggableObject, self).ToDict()
301
302     tags = bo.get("tags", None)
303     if isinstance(tags, set):
304       bo["tags"] = list(tags)
305     return bo
306
307   @classmethod
308   def FromDict(cls, val):
309     """Custom function for instances.
310
311     """
312     obj = super(TaggableObject, cls).FromDict(val)
313     if hasattr(obj, "tags") and isinstance(obj.tags, list):
314       obj.tags = set(obj.tags)
315     return obj
316
317
318 class ConfigData(ConfigObject):
319   """Top-level config object."""
320   __slots__ = [
321     "version",
322     "cluster",
323     "nodes",
324     "nodegroups",
325     "instances",
326     "serial_no",
327     ] + _TIMESTAMPS
328
329   def ToDict(self):
330     """Custom function for top-level config data.
331
332     This just replaces the list of instances, nodes and the cluster
333     with standard python types.
334
335     """
336     mydict = super(ConfigData, self).ToDict()
337     mydict["cluster"] = mydict["cluster"].ToDict()
338     for key in "nodes", "instances", "nodegroups":
339       mydict[key] = self._ContainerToDicts(mydict[key])
340
341     return mydict
342
343   @classmethod
344   def FromDict(cls, val):
345     """Custom function for top-level config data
346
347     """
348     obj = super(ConfigData, cls).FromDict(val)
349     obj.cluster = Cluster.FromDict(obj.cluster)
350     obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
351     obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
352     obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
353     return obj
354
355   def HasAnyDiskOfType(self, dev_type):
356     """Check if in there is at disk of the given type in the configuration.
357
358     @type dev_type: L{constants.LDS_BLOCK}
359     @param dev_type: the type to look for
360     @rtype: boolean
361     @return: boolean indicating if a disk of the given type was found or not
362
363     """
364     for instance in self.instances.values():
365       for disk in instance.disks:
366         if disk.IsBasedOnDiskType(dev_type):
367           return True
368     return False
369
370   def UpgradeConfig(self):
371     """Fill defaults for missing configuration values.
372
373     """
374     self.cluster.UpgradeConfig()
375     for node in self.nodes.values():
376       node.UpgradeConfig()
377     for instance in self.instances.values():
378       instance.UpgradeConfig()
379     if self.nodegroups is None:
380       self.nodegroups = {}
381     for nodegroup in self.nodegroups.values():
382       nodegroup.UpgradeConfig()
383     if self.cluster.drbd_usermode_helper is None:
384       # To decide if we set an helper let's check if at least one instance has
385       # a DRBD disk. This does not cover all the possible scenarios but it
386       # gives a good approximation.
387       if self.HasAnyDiskOfType(constants.LD_DRBD8):
388         self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
389
390
391 class NIC(ConfigObject):
392   """Config object representing a network card."""
393   __slots__ = ["mac", "ip", "nicparams"]
394
395   @classmethod
396   def CheckParameterSyntax(cls, nicparams):
397     """Check the given parameters for validity.
398
399     @type nicparams:  dict
400     @param nicparams: dictionary with parameter names/value
401     @raise errors.ConfigurationError: when a parameter is not valid
402
403     """
404     if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES:
405       err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
406       raise errors.ConfigurationError(err)
407
408     if (nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED and
409         not nicparams[constants.NIC_LINK]):
410       err = "Missing bridged nic link"
411       raise errors.ConfigurationError(err)
412
413
414 class Disk(ConfigObject):
415   """Config object representing a block device."""
416   __slots__ = ["dev_type", "logical_id", "physical_id",
417                "children", "iv_name", "size", "mode"]
418
419   def CreateOnSecondary(self):
420     """Test if this device needs to be created on a secondary node."""
421     return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
422
423   def AssembleOnSecondary(self):
424     """Test if this device needs to be assembled on a secondary node."""
425     return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
426
427   def OpenOnSecondary(self):
428     """Test if this device needs to be opened on a secondary node."""
429     return self.dev_type in (constants.LD_LV,)
430
431   def StaticDevPath(self):
432     """Return the device path if this device type has a static one.
433
434     Some devices (LVM for example) live always at the same /dev/ path,
435     irrespective of their status. For such devices, we return this
436     path, for others we return None.
437
438     @warning: The path returned is not a normalized pathname; callers
439         should check that it is a valid path.
440
441     """
442     if self.dev_type == constants.LD_LV:
443       return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
444     return None
445
446   def ChildrenNeeded(self):
447     """Compute the needed number of children for activation.
448
449     This method will return either -1 (all children) or a positive
450     number denoting the minimum number of children needed for
451     activation (only mirrored devices will usually return >=0).
452
453     Currently, only DRBD8 supports diskless activation (therefore we
454     return 0), for all other we keep the previous semantics and return
455     -1.
456
457     """
458     if self.dev_type == constants.LD_DRBD8:
459       return 0
460     return -1
461
462   def IsBasedOnDiskType(self, dev_type):
463     """Check if the disk or its children are based on the given type.
464
465     @type dev_type: L{constants.LDS_BLOCK}
466     @param dev_type: the type to look for
467     @rtype: boolean
468     @return: boolean indicating if a device of the given type was found or not
469
470     """
471     if self.children:
472       for child in self.children:
473         if child.IsBasedOnDiskType(dev_type):
474           return True
475     return self.dev_type == dev_type
476
477   def GetNodes(self, node):
478     """This function returns the nodes this device lives on.
479
480     Given the node on which the parent of the device lives on (or, in
481     case of a top-level device, the primary node of the devices'
482     instance), this function will return a list of nodes on which this
483     devices needs to (or can) be assembled.
484
485     """
486     if self.dev_type in [constants.LD_LV, constants.LD_FILE]:
487       result = [node]
488     elif self.dev_type in constants.LDS_DRBD:
489       result = [self.logical_id[0], self.logical_id[1]]
490       if node not in result:
491         raise errors.ConfigurationError("DRBD device passed unknown node")
492     else:
493       raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
494     return result
495
496   def ComputeNodeTree(self, parent_node):
497     """Compute the node/disk tree for this disk and its children.
498
499     This method, given the node on which the parent disk lives, will
500     return the list of all (node, disk) pairs which describe the disk
501     tree in the most compact way. For example, a drbd/lvm stack
502     will be returned as (primary_node, drbd) and (secondary_node, drbd)
503     which represents all the top-level devices on the nodes.
504
505     """
506     my_nodes = self.GetNodes(parent_node)
507     result = [(node, self) for node in my_nodes]
508     if not self.children:
509       # leaf device
510       return result
511     for node in my_nodes:
512       for child in self.children:
513         child_result = child.ComputeNodeTree(node)
514         if len(child_result) == 1:
515           # child (and all its descendants) is simple, doesn't split
516           # over multiple hosts, so we don't need to describe it, our
517           # own entry for this node describes it completely
518           continue
519         else:
520           # check if child nodes differ from my nodes; note that
521           # subdisk can differ from the child itself, and be instead
522           # one of its descendants
523           for subnode, subdisk in child_result:
524             if subnode not in my_nodes:
525               result.append((subnode, subdisk))
526             # otherwise child is under our own node, so we ignore this
527             # entry (but probably the other results in the list will
528             # be different)
529     return result
530
531   def RecordGrow(self, amount):
532     """Update the size of this disk after growth.
533
534     This method recurses over the disks's children and updates their
535     size correspondigly. The method needs to be kept in sync with the
536     actual algorithms from bdev.
537
538     """
539     if self.dev_type == constants.LD_LV or self.dev_type == constants.LD_FILE:
540       self.size += amount
541     elif self.dev_type == constants.LD_DRBD8:
542       if self.children:
543         self.children[0].RecordGrow(amount)
544       self.size += amount
545     else:
546       raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
547                                    " disk type %s" % self.dev_type)
548
549   def UnsetSize(self):
550     """Sets recursively the size to zero for the disk and its children.
551
552     """
553     if self.children:
554       for child in self.children:
555         child.UnsetSize()
556     self.size = 0
557
558   def SetPhysicalID(self, target_node, nodes_ip):
559     """Convert the logical ID to the physical ID.
560
561     This is used only for drbd, which needs ip/port configuration.
562
563     The routine descends down and updates its children also, because
564     this helps when the only the top device is passed to the remote
565     node.
566
567     Arguments:
568       - target_node: the node we wish to configure for
569       - nodes_ip: a mapping of node name to ip
570
571     The target_node must exist in in nodes_ip, and must be one of the
572     nodes in the logical ID for each of the DRBD devices encountered
573     in the disk tree.
574
575     """
576     if self.children:
577       for child in self.children:
578         child.SetPhysicalID(target_node, nodes_ip)
579
580     if self.logical_id is None and self.physical_id is not None:
581       return
582     if self.dev_type in constants.LDS_DRBD:
583       pnode, snode, port, pminor, sminor, secret = self.logical_id
584       if target_node not in (pnode, snode):
585         raise errors.ConfigurationError("DRBD device not knowing node %s" %
586                                         target_node)
587       pnode_ip = nodes_ip.get(pnode, None)
588       snode_ip = nodes_ip.get(snode, None)
589       if pnode_ip is None or snode_ip is None:
590         raise errors.ConfigurationError("Can't find primary or secondary node"
591                                         " for %s" % str(self))
592       p_data = (pnode_ip, port)
593       s_data = (snode_ip, port)
594       if pnode == target_node:
595         self.physical_id = p_data + s_data + (pminor, secret)
596       else: # it must be secondary, we tested above
597         self.physical_id = s_data + p_data + (sminor, secret)
598     else:
599       self.physical_id = self.logical_id
600     return
601
602   def ToDict(self):
603     """Disk-specific conversion to standard python types.
604
605     This replaces the children lists of objects with lists of
606     standard python types.
607
608     """
609     bo = super(Disk, self).ToDict()
610
611     for attr in ("children",):
612       alist = bo.get(attr, None)
613       if alist:
614         bo[attr] = self._ContainerToDicts(alist)
615     return bo
616
617   @classmethod
618   def FromDict(cls, val):
619     """Custom function for Disks
620
621     """
622     obj = super(Disk, cls).FromDict(val)
623     if obj.children:
624       obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
625     if obj.logical_id and isinstance(obj.logical_id, list):
626       obj.logical_id = tuple(obj.logical_id)
627     if obj.physical_id and isinstance(obj.physical_id, list):
628       obj.physical_id = tuple(obj.physical_id)
629     if obj.dev_type in constants.LDS_DRBD:
630       # we need a tuple of length six here
631       if len(obj.logical_id) < 6:
632         obj.logical_id += (None,) * (6 - len(obj.logical_id))
633     return obj
634
635   def __str__(self):
636     """Custom str() formatter for disks.
637
638     """
639     if self.dev_type == constants.LD_LV:
640       val =  "<LogicalVolume(/dev/%s/%s" % self.logical_id
641     elif self.dev_type in constants.LDS_DRBD:
642       node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
643       val = "<DRBD8("
644       if self.physical_id is None:
645         phy = "unconfigured"
646       else:
647         phy = ("configured as %s:%s %s:%s" %
648                (self.physical_id[0], self.physical_id[1],
649                 self.physical_id[2], self.physical_id[3]))
650
651       val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
652               (node_a, minor_a, node_b, minor_b, port, phy))
653       if self.children and self.children.count(None) == 0:
654         val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
655       else:
656         val += "no local storage"
657     else:
658       val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
659              (self.dev_type, self.logical_id, self.physical_id, self.children))
660     if self.iv_name is None:
661       val += ", not visible"
662     else:
663       val += ", visible as /dev/%s" % self.iv_name
664     if isinstance(self.size, int):
665       val += ", size=%dm)>" % self.size
666     else:
667       val += ", size='%s')>" % (self.size,)
668     return val
669
670   def Verify(self):
671     """Checks that this disk is correctly configured.
672
673     """
674     all_errors = []
675     if self.mode not in constants.DISK_ACCESS_SET:
676       all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
677     return all_errors
678
679   def UpgradeConfig(self):
680     """Fill defaults for missing configuration values.
681
682     """
683     if self.children:
684       for child in self.children:
685         child.UpgradeConfig()
686     # add here config upgrade for this disk
687
688
689 class Instance(TaggableObject):
690   """Config object representing an instance."""
691   __slots__ = [
692     "name",
693     "primary_node",
694     "os",
695     "hypervisor",
696     "hvparams",
697     "beparams",
698     "osparams",
699     "admin_up",
700     "nics",
701     "disks",
702     "disk_template",
703     "network_port",
704     "serial_no",
705     ] + _TIMESTAMPS + _UUID
706
707   def _ComputeSecondaryNodes(self):
708     """Compute the list of secondary nodes.
709
710     This is a simple wrapper over _ComputeAllNodes.
711
712     """
713     all_nodes = set(self._ComputeAllNodes())
714     all_nodes.discard(self.primary_node)
715     return tuple(all_nodes)
716
717   secondary_nodes = property(_ComputeSecondaryNodes, None, None,
718                              "List of secondary nodes")
719
720   def _ComputeAllNodes(self):
721     """Compute the list of all nodes.
722
723     Since the data is already there (in the drbd disks), keeping it as
724     a separate normal attribute is redundant and if not properly
725     synchronised can cause problems. Thus it's better to compute it
726     dynamically.
727
728     """
729     def _Helper(nodes, device):
730       """Recursively computes nodes given a top device."""
731       if device.dev_type in constants.LDS_DRBD:
732         nodea, nodeb = device.logical_id[:2]
733         nodes.add(nodea)
734         nodes.add(nodeb)
735       if device.children:
736         for child in device.children:
737           _Helper(nodes, child)
738
739     all_nodes = set()
740     all_nodes.add(self.primary_node)
741     for device in self.disks:
742       _Helper(all_nodes, device)
743     return tuple(all_nodes)
744
745   all_nodes = property(_ComputeAllNodes, None, None,
746                        "List of all nodes of the instance")
747
748   def MapLVsByNode(self, lvmap=None, devs=None, node=None):
749     """Provide a mapping of nodes to LVs this instance owns.
750
751     This function figures out what logical volumes should belong on
752     which nodes, recursing through a device tree.
753
754     @param lvmap: optional dictionary to receive the
755         'node' : ['lv', ...] data.
756
757     @return: None if lvmap arg is given, otherwise, a dictionary of
758         the form { 'nodename' : ['volume1', 'volume2', ...], ... };
759         volumeN is of the form "vg_name/lv_name", compatible with
760         GetVolumeList()
761
762     """
763     if node == None:
764       node = self.primary_node
765
766     if lvmap is None:
767       lvmap = { node : [] }
768       ret = lvmap
769     else:
770       if not node in lvmap:
771         lvmap[node] = []
772       ret = None
773
774     if not devs:
775       devs = self.disks
776
777     for dev in devs:
778       if dev.dev_type == constants.LD_LV:
779         lvmap[node].append(dev.logical_id[0]+"/"+dev.logical_id[1])
780
781       elif dev.dev_type in constants.LDS_DRBD:
782         if dev.children:
783           self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
784           self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
785
786       elif dev.children:
787         self.MapLVsByNode(lvmap, dev.children, node)
788
789     return ret
790
791   def FindDisk(self, idx):
792     """Find a disk given having a specified index.
793
794     This is just a wrapper that does validation of the index.
795
796     @type idx: int
797     @param idx: the disk index
798     @rtype: L{Disk}
799     @return: the corresponding disk
800     @raise errors.OpPrereqError: when the given index is not valid
801
802     """
803     try:
804       idx = int(idx)
805       return self.disks[idx]
806     except (TypeError, ValueError), err:
807       raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
808                                  errors.ECODE_INVAL)
809     except IndexError:
810       raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
811                                  " 0 to %d" % (idx, len(self.disks)),
812                                  errors.ECODE_INVAL)
813
814   def ToDict(self):
815     """Instance-specific conversion to standard python types.
816
817     This replaces the children lists of objects with lists of standard
818     python types.
819
820     """
821     bo = super(Instance, self).ToDict()
822
823     for attr in "nics", "disks":
824       alist = bo.get(attr, None)
825       if alist:
826         nlist = self._ContainerToDicts(alist)
827       else:
828         nlist = []
829       bo[attr] = nlist
830     return bo
831
832   @classmethod
833   def FromDict(cls, val):
834     """Custom function for instances.
835
836     """
837     obj = super(Instance, cls).FromDict(val)
838     obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
839     obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
840     return obj
841
842   def UpgradeConfig(self):
843     """Fill defaults for missing configuration values.
844
845     """
846     for nic in self.nics:
847       nic.UpgradeConfig()
848     for disk in self.disks:
849       disk.UpgradeConfig()
850     if self.hvparams:
851       for key in constants.HVC_GLOBALS:
852         try:
853           del self.hvparams[key]
854         except KeyError:
855           pass
856     if self.osparams is None:
857       self.osparams = {}
858
859
860 class OS(ConfigObject):
861   """Config object representing an operating system.
862
863   @type supported_parameters: list
864   @ivar supported_parameters: a list of tuples, name and description,
865       containing the supported parameters by this OS
866
867   @type VARIANT_DELIM: string
868   @cvar VARIANT_DELIM: the variant delimiter
869
870   """
871   __slots__ = [
872     "name",
873     "path",
874     "api_versions",
875     "create_script",
876     "export_script",
877     "import_script",
878     "rename_script",
879     "verify_script",
880     "supported_variants",
881     "supported_parameters",
882     ]
883
884   VARIANT_DELIM = "+"
885
886   @classmethod
887   def SplitNameVariant(cls, name):
888     """Splits the name into the proper name and variant.
889
890     @param name: the OS (unprocessed) name
891     @rtype: list
892     @return: a list of two elements; if the original name didn't
893         contain a variant, it's returned as an empty string
894
895     """
896     nv = name.split(cls.VARIANT_DELIM, 1)
897     if len(nv) == 1:
898       nv.append("")
899     return nv
900
901   @classmethod
902   def GetName(cls, name):
903     """Returns the proper name of the os (without the variant).
904
905     @param name: the OS (unprocessed) name
906
907     """
908     return cls.SplitNameVariant(name)[0]
909
910   @classmethod
911   def GetVariant(cls, name):
912     """Returns the variant the os (without the base name).
913
914     @param name: the OS (unprocessed) name
915
916     """
917     return cls.SplitNameVariant(name)[1]
918
919
920 class Node(TaggableObject):
921   """Config object representing a node."""
922   __slots__ = [
923     "name",
924     "primary_ip",
925     "secondary_ip",
926     "serial_no",
927     "master_candidate",
928     "offline",
929     "drained",
930     "group",
931     "master_capable",
932     "vm_capable",
933     "ndparams",
934     ] + _TIMESTAMPS + _UUID
935
936   def UpgradeConfig(self):
937     """Fill defaults for missing configuration values.
938
939     """
940     # pylint: disable-msg=E0203
941     # because these are "defined" via slots, not manually
942     if self.master_capable is None:
943       self.master_capable = True
944
945     if self.vm_capable is None:
946       self.vm_capable = True
947
948     if self.ndparams is None:
949       self.ndparams = {}
950
951
952 class NodeGroup(ConfigObject):
953   """Config object representing a node group."""
954   __slots__ = [
955     "name",
956     "members",
957     "ndparams",
958     "serial_no",
959     ] + _TIMESTAMPS + _UUID
960
961   def ToDict(self):
962     """Custom function for nodegroup.
963
964     This discards the members object, which gets recalculated and is only kept
965     in memory.
966
967     """
968     mydict = super(NodeGroup, self).ToDict()
969     del mydict["members"]
970     return mydict
971
972   @classmethod
973   def FromDict(cls, val):
974     """Custom function for nodegroup.
975
976     The members slot is initialized to an empty list, upon deserialization.
977
978     """
979     obj = super(NodeGroup, cls).FromDict(val)
980     obj.members = []
981     return obj
982
983   def UpgradeConfig(self):
984     """Fill defaults for missing configuration values.
985
986     """
987     if self.ndparams is None:
988       self.ndparams = {}
989
990     if self.serial_no is None:
991       self.serial_no = 1
992
993     # We only update mtime, and not ctime, since we would not be able to provide
994     # a correct value for creation time.
995     if self.mtime is None:
996       self.mtime = time.time()
997
998   def FillND(self, node):
999     """Return filled out ndparams for L{object.Node}
1000
1001     @type node: L{objects.Node}
1002     @param node: A Node object to fill
1003     @return a copy of the node's ndparams with defaults filled
1004
1005     """
1006     return self.SimpleFillND(node.ndparams)
1007
1008   def SimpleFillND(self, ndparams):
1009     """Fill a given ndparams dict with defaults.
1010
1011     @type ndparams: dict
1012     @param ndparams: the dict to fill
1013     @rtype: dict
1014     @return: a copy of the passed in ndparams with missing keys filled
1015         from the node group defaults
1016
1017     """
1018     return FillDict(self.ndparams, ndparams)
1019
1020
1021 class Cluster(TaggableObject):
1022   """Config object representing the cluster."""
1023   __slots__ = [
1024     "serial_no",
1025     "rsahostkeypub",
1026     "highest_used_port",
1027     "tcpudp_port_pool",
1028     "mac_prefix",
1029     "volume_group_name",
1030     "reserved_lvs",
1031     "drbd_usermode_helper",
1032     "default_bridge",
1033     "default_hypervisor",
1034     "master_node",
1035     "master_ip",
1036     "master_netdev",
1037     "cluster_name",
1038     "file_storage_dir",
1039     "enabled_hypervisors",
1040     "hvparams",
1041     "os_hvp",
1042     "beparams",
1043     "osparams",
1044     "nicparams",
1045     "ndparams",
1046     "candidate_pool_size",
1047     "modify_etc_hosts",
1048     "modify_ssh_setup",
1049     "maintain_node_health",
1050     "uid_pool",
1051     "default_iallocator",
1052     "hidden_os",
1053     "blacklisted_os",
1054     "primary_ip_family",
1055     "prealloc_wipe_disks",
1056     ] + _TIMESTAMPS + _UUID
1057
1058   def UpgradeConfig(self):
1059     """Fill defaults for missing configuration values.
1060
1061     """
1062     # pylint: disable-msg=E0203
1063     # because these are "defined" via slots, not manually
1064     if self.hvparams is None:
1065       self.hvparams = constants.HVC_DEFAULTS
1066     else:
1067       for hypervisor in self.hvparams:
1068         self.hvparams[hypervisor] = FillDict(
1069             constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1070
1071     if self.os_hvp is None:
1072       self.os_hvp = {}
1073
1074     # osparams added before 2.2
1075     if self.osparams is None:
1076       self.osparams = {}
1077
1078     if self.ndparams is None:
1079       self.ndparams = constants.NDC_DEFAULTS
1080
1081     self.beparams = UpgradeGroupedParams(self.beparams,
1082                                          constants.BEC_DEFAULTS)
1083     migrate_default_bridge = not self.nicparams
1084     self.nicparams = UpgradeGroupedParams(self.nicparams,
1085                                           constants.NICC_DEFAULTS)
1086     if migrate_default_bridge:
1087       self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1088         self.default_bridge
1089
1090     if self.modify_etc_hosts is None:
1091       self.modify_etc_hosts = True
1092
1093     if self.modify_ssh_setup is None:
1094       self.modify_ssh_setup = True
1095
1096     # default_bridge is no longer used it 2.1. The slot is left there to
1097     # support auto-upgrading. It can be removed once we decide to deprecate
1098     # upgrading straight from 2.0.
1099     if self.default_bridge is not None:
1100       self.default_bridge = None
1101
1102     # default_hypervisor is just the first enabled one in 2.1. This slot and
1103     # code can be removed once upgrading straight from 2.0 is deprecated.
1104     if self.default_hypervisor is not None:
1105       self.enabled_hypervisors = ([self.default_hypervisor] +
1106         [hvname for hvname in self.enabled_hypervisors
1107          if hvname != self.default_hypervisor])
1108       self.default_hypervisor = None
1109
1110     # maintain_node_health added after 2.1.1
1111     if self.maintain_node_health is None:
1112       self.maintain_node_health = False
1113
1114     if self.uid_pool is None:
1115       self.uid_pool = []
1116
1117     if self.default_iallocator is None:
1118       self.default_iallocator = ""
1119
1120     # reserved_lvs added before 2.2
1121     if self.reserved_lvs is None:
1122       self.reserved_lvs = []
1123
1124     # hidden and blacklisted operating systems added before 2.2.1
1125     if self.hidden_os is None:
1126       self.hidden_os = []
1127
1128     if self.blacklisted_os is None:
1129       self.blacklisted_os = []
1130
1131     # primary_ip_family added before 2.3
1132     if self.primary_ip_family is None:
1133       self.primary_ip_family = AF_INET
1134
1135     if self.prealloc_wipe_disks is None:
1136       self.prealloc_wipe_disks = False
1137
1138   def ToDict(self):
1139     """Custom function for cluster.
1140
1141     """
1142     mydict = super(Cluster, self).ToDict()
1143     mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1144     return mydict
1145
1146   @classmethod
1147   def FromDict(cls, val):
1148     """Custom function for cluster.
1149
1150     """
1151     obj = super(Cluster, cls).FromDict(val)
1152     if not isinstance(obj.tcpudp_port_pool, set):
1153       obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1154     return obj
1155
1156   def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1157     """Get the default hypervisor parameters for the cluster.
1158
1159     @param hypervisor: the hypervisor name
1160     @param os_name: if specified, we'll also update the defaults for this OS
1161     @param skip_keys: if passed, list of keys not to use
1162     @return: the defaults dict
1163
1164     """
1165     if skip_keys is None:
1166       skip_keys = []
1167
1168     fill_stack = [self.hvparams.get(hypervisor, {})]
1169     if os_name is not None:
1170       os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1171       fill_stack.append(os_hvp)
1172
1173     ret_dict = {}
1174     for o_dict in fill_stack:
1175       ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1176
1177     return ret_dict
1178
1179   def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1180     """Fill a given hvparams dict with cluster defaults.
1181
1182     @type hv_name: string
1183     @param hv_name: the hypervisor to use
1184     @type os_name: string
1185     @param os_name: the OS to use for overriding the hypervisor defaults
1186     @type skip_globals: boolean
1187     @param skip_globals: if True, the global hypervisor parameters will
1188         not be filled
1189     @rtype: dict
1190     @return: a copy of the given hvparams with missing keys filled from
1191         the cluster defaults
1192
1193     """
1194     if skip_globals:
1195       skip_keys = constants.HVC_GLOBALS
1196     else:
1197       skip_keys = []
1198
1199     def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1200     return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1201
1202   def FillHV(self, instance, skip_globals=False):
1203     """Fill an instance's hvparams dict with cluster defaults.
1204
1205     @type instance: L{objects.Instance}
1206     @param instance: the instance parameter to fill
1207     @type skip_globals: boolean
1208     @param skip_globals: if True, the global hypervisor parameters will
1209         not be filled
1210     @rtype: dict
1211     @return: a copy of the instance's hvparams with missing keys filled from
1212         the cluster defaults
1213
1214     """
1215     return self.SimpleFillHV(instance.hypervisor, instance.os,
1216                              instance.hvparams, skip_globals)
1217
1218   def SimpleFillBE(self, beparams):
1219     """Fill a given beparams dict with cluster defaults.
1220
1221     @type beparams: dict
1222     @param beparams: the dict to fill
1223     @rtype: dict
1224     @return: a copy of the passed in beparams with missing keys filled
1225         from the cluster defaults
1226
1227     """
1228     return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1229
1230   def FillBE(self, instance):
1231     """Fill an instance's beparams dict with cluster defaults.
1232
1233     @type instance: L{objects.Instance}
1234     @param instance: the instance parameter to fill
1235     @rtype: dict
1236     @return: a copy of the instance's beparams with missing keys filled from
1237         the cluster defaults
1238
1239     """
1240     return self.SimpleFillBE(instance.beparams)
1241
1242   def SimpleFillNIC(self, nicparams):
1243     """Fill a given nicparams dict with cluster defaults.
1244
1245     @type nicparams: dict
1246     @param nicparams: the dict to fill
1247     @rtype: dict
1248     @return: a copy of the passed in nicparams with missing keys filled
1249         from the cluster defaults
1250
1251     """
1252     return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1253
1254   def SimpleFillOS(self, os_name, os_params):
1255     """Fill an instance's osparams dict with cluster defaults.
1256
1257     @type os_name: string
1258     @param os_name: the OS name to use
1259     @type os_params: dict
1260     @param os_params: the dict to fill with default values
1261     @rtype: dict
1262     @return: a copy of the instance's osparams with missing keys filled from
1263         the cluster defaults
1264
1265     """
1266     name_only = os_name.split("+", 1)[0]
1267     # base OS
1268     result = self.osparams.get(name_only, {})
1269     # OS with variant
1270     result = FillDict(result, self.osparams.get(os_name, {}))
1271     # specified params
1272     return FillDict(result, os_params)
1273
1274   def FillND(self, node, nodegroup):
1275     """Return filled out ndparams for L{objects.NodeGroup} and L{object.Node}
1276
1277     @type node: L{objects.Node}
1278     @param node: A Node object to fill
1279     @type nodegroup: L{objects.NodeGroup}
1280     @param nodegroup: A Node object to fill
1281     @return a copy of the node's ndparams with defaults filled
1282
1283     """
1284     return self.SimpleFillND(nodegroup.FillND(node))
1285
1286   def SimpleFillND(self, ndparams):
1287     """Fill a given ndparams dict with defaults.
1288
1289     @type ndparams: dict
1290     @param ndparams: the dict to fill
1291     @rtype: dict
1292     @return: a copy of the passed in ndparams with missing keys filled
1293         from the cluster defaults
1294
1295     """
1296     return FillDict(self.ndparams, ndparams)
1297
1298
1299 class BlockDevStatus(ConfigObject):
1300   """Config object representing the status of a block device."""
1301   __slots__ = [
1302     "dev_path",
1303     "major",
1304     "minor",
1305     "sync_percent",
1306     "estimated_time",
1307     "is_degraded",
1308     "ldisk_status",
1309     ]
1310
1311
1312 class ImportExportStatus(ConfigObject):
1313   """Config object representing the status of an import or export."""
1314   __slots__ = [
1315     "recent_output",
1316     "listen_port",
1317     "connected",
1318     "progress_mbytes",
1319     "progress_throughput",
1320     "progress_eta",
1321     "progress_percent",
1322     "exit_status",
1323     "error_message",
1324     ] + _TIMESTAMPS
1325
1326
1327 class ImportExportOptions(ConfigObject):
1328   """Options for import/export daemon
1329
1330   @ivar key_name: X509 key name (None for cluster certificate)
1331   @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1332   @ivar compress: Compression method (one of L{constants.IEC_ALL})
1333   @ivar magic: Used to ensure the connection goes to the right disk
1334   @ivar ipv6: Whether to use IPv6
1335
1336   """
1337   __slots__ = [
1338     "key_name",
1339     "ca_pem",
1340     "compress",
1341     "magic",
1342     "ipv6",
1343     ]
1344
1345
1346 class ConfdRequest(ConfigObject):
1347   """Object holding a confd request.
1348
1349   @ivar protocol: confd protocol version
1350   @ivar type: confd query type
1351   @ivar query: query request
1352   @ivar rsalt: requested reply salt
1353
1354   """
1355   __slots__ = [
1356     "protocol",
1357     "type",
1358     "query",
1359     "rsalt",
1360     ]
1361
1362
1363 class ConfdReply(ConfigObject):
1364   """Object holding a confd reply.
1365
1366   @ivar protocol: confd protocol version
1367   @ivar status: reply status code (ok, error)
1368   @ivar answer: confd query reply
1369   @ivar serial: configuration serial number
1370
1371   """
1372   __slots__ = [
1373     "protocol",
1374     "status",
1375     "answer",
1376     "serial",
1377     ]
1378
1379
1380 class QueryFieldDefinition(ConfigObject):
1381   """Object holding a query field definition.
1382
1383   @ivar name: Field name
1384   @ivar title: Human-readable title
1385   @ivar kind: Field type
1386
1387   """
1388   __slots__ = [
1389     "name",
1390     "title",
1391     "kind",
1392     ]
1393
1394
1395 class _QueryResponseBase(ConfigObject):
1396   __slots__ = [
1397     "fields",
1398     ]
1399
1400   def ToDict(self):
1401     """Custom function for serializing.
1402
1403     """
1404     mydict = super(_QueryResponseBase, self).ToDict()
1405     mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1406     return mydict
1407
1408   @classmethod
1409   def FromDict(cls, val):
1410     """Custom function for de-serializing.
1411
1412     """
1413     obj = super(_QueryResponseBase, cls).FromDict(val)
1414     obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1415     return obj
1416
1417
1418 class QueryRequest(ConfigObject):
1419   """Object holding a query request.
1420
1421   """
1422   __slots__ = [
1423     "what",
1424     "fields",
1425     "filter",
1426     ]
1427
1428
1429 class QueryResponse(_QueryResponseBase):
1430   """Object holding the response to a query.
1431
1432   @ivar fields: List of L{QueryFieldDefinition} objects
1433   @ivar data: Requested data
1434
1435   """
1436   __slots__ = [
1437     "data",
1438     ]
1439
1440
1441 class QueryFieldsRequest(ConfigObject):
1442   """Object holding a request for querying available fields.
1443
1444   """
1445   __slots__ = [
1446     "what",
1447     "fields",
1448     ]
1449
1450
1451 class QueryFieldsResponse(_QueryResponseBase):
1452   """Object holding the response to a query for fields.
1453
1454   @ivar fields: List of L{QueryFieldDefinition} objects
1455
1456   """
1457   __slots__ = [
1458     ]
1459
1460
1461 class SerializableConfigParser(ConfigParser.SafeConfigParser):
1462   """Simple wrapper over ConfigParse that allows serialization.
1463
1464   This class is basically ConfigParser.SafeConfigParser with two
1465   additional methods that allow it to serialize/unserialize to/from a
1466   buffer.
1467
1468   """
1469   def Dumps(self):
1470     """Dump this instance and return the string representation."""
1471     buf = StringIO()
1472     self.write(buf)
1473     return buf.getvalue()
1474
1475   @classmethod
1476   def Loads(cls, data):
1477     """Load data from a string."""
1478     buf = StringIO(data)
1479     cfp = cls()
1480     cfp.readfp(buf)
1481     return cfp