Move some ipolicy functions from objects to cli
[ganeti-local] / lib / objects.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 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=E0203,W0201,R0902
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 # R0902: Allow instances of these objects to have more than 20 attributes
37
38 import ConfigParser
39 import re
40 import copy
41 import time
42 from cStringIO import StringIO
43
44 from ganeti import errors
45 from ganeti import constants
46 from ganeti import netutils
47 from ganeti import utils
48
49 from socket import AF_INET
50
51
52 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
53            "OS", "Node", "NodeGroup", "Cluster", "FillDict"]
54
55 _TIMESTAMPS = ["ctime", "mtime"]
56 _UUID = ["uuid"]
57
58
59 def FillDict(defaults_dict, custom_dict, skip_keys=None):
60   """Basic function to apply settings on top a default dict.
61
62   @type defaults_dict: dict
63   @param defaults_dict: dictionary holding the default values
64   @type custom_dict: dict
65   @param custom_dict: dictionary holding customized value
66   @type skip_keys: list
67   @param skip_keys: which keys not to fill
68   @rtype: dict
69   @return: dict with the 'full' values
70
71   """
72   ret_dict = copy.deepcopy(defaults_dict)
73   ret_dict.update(custom_dict)
74   if skip_keys:
75     for k in skip_keys:
76       try:
77         del ret_dict[k]
78       except KeyError:
79         pass
80   return ret_dict
81
82
83 def FillIPolicy(default_ipolicy, custom_ipolicy, skip_keys=None):
84   """Fills an instance policy with defaults.
85
86   """
87   assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
88   ret_dict = {}
89   for key in constants.IPOLICY_ISPECS:
90     ret_dict[key] = FillDict(default_ipolicy[key],
91                              custom_ipolicy.get(key, {}),
92                              skip_keys=skip_keys)
93   # list items
94   for key in [constants.IPOLICY_DTS]:
95     ret_dict[key] = list(custom_ipolicy.get(key, default_ipolicy[key]))
96   # other items which we know we can directly copy (immutables)
97   for key in constants.IPOLICY_PARAMETERS:
98     ret_dict[key] = custom_ipolicy.get(key, default_ipolicy[key])
99
100   return ret_dict
101
102
103 def FillDiskParams(default_dparams, custom_dparams, skip_keys=None):
104   """Fills the disk parameter defaults.
105
106   @see: L{FillDict} for parameters and return value
107
108   """
109   assert frozenset(default_dparams.keys()) == constants.DISK_TEMPLATES
110
111   return dict((dt, FillDict(default_dparams[dt], custom_dparams.get(dt, {}),
112                              skip_keys=skip_keys))
113               for dt in constants.DISK_TEMPLATES)
114
115
116 def UpgradeGroupedParams(target, defaults):
117   """Update all groups for the target parameter.
118
119   @type target: dict of dicts
120   @param target: {group: {parameter: value}}
121   @type defaults: dict
122   @param defaults: default parameter values
123
124   """
125   if target is None:
126     target = {constants.PP_DEFAULT: defaults}
127   else:
128     for group in target:
129       target[group] = FillDict(defaults, target[group])
130   return target
131
132
133 def UpgradeBeParams(target):
134   """Update the be parameters dict to the new format.
135
136   @type target: dict
137   @param target: "be" parameters dict
138
139   """
140   if constants.BE_MEMORY in target:
141     memory = target[constants.BE_MEMORY]
142     target[constants.BE_MAXMEM] = memory
143     target[constants.BE_MINMEM] = memory
144     del target[constants.BE_MEMORY]
145
146
147 def UpgradeDiskParams(diskparams):
148   """Upgrade the disk parameters.
149
150   @type diskparams: dict
151   @param diskparams: disk parameters to upgrade
152   @rtype: dict
153   @return: the upgraded disk parameters dict
154
155   """
156   if not diskparams:
157     result = {}
158   else:
159     result = FillDiskParams(constants.DISK_DT_DEFAULTS, diskparams)
160
161   return result
162
163
164 def UpgradeNDParams(ndparams):
165   """Upgrade ndparams structure.
166
167   @type ndparams: dict
168   @param ndparams: disk parameters to upgrade
169   @rtype: dict
170   @return: the upgraded node parameters dict
171
172   """
173   if ndparams is None:
174     ndparams = {}
175
176   return FillDict(constants.NDC_DEFAULTS, ndparams)
177
178
179 def MakeEmptyIPolicy():
180   """Create empty IPolicy dictionary.
181
182   """
183   return dict([
184     (constants.ISPECS_MIN, {}),
185     (constants.ISPECS_MAX, {}),
186     (constants.ISPECS_STD, {}),
187     ])
188
189
190 class ConfigObject(object):
191   """A generic config object.
192
193   It has the following properties:
194
195     - provides somewhat safe recursive unpickling and pickling for its classes
196     - unset attributes which are defined in slots are always returned
197       as None instead of raising an error
198
199   Classes derived from this must always declare __slots__ (we use many
200   config objects and the memory reduction is useful)
201
202   """
203   __slots__ = []
204
205   def __init__(self, **kwargs):
206     for k, v in kwargs.iteritems():
207       setattr(self, k, v)
208
209   def __getattr__(self, name):
210     if name not in self._all_slots():
211       raise AttributeError("Invalid object attribute %s.%s" %
212                            (type(self).__name__, name))
213     return None
214
215   def __setstate__(self, state):
216     slots = self._all_slots()
217     for name in state:
218       if name in slots:
219         setattr(self, name, state[name])
220
221   @classmethod
222   def _all_slots(cls):
223     """Compute the list of all declared slots for a class.
224
225     """
226     slots = []
227     for parent in cls.__mro__:
228       slots.extend(getattr(parent, "__slots__", []))
229     return slots
230
231   #: Public getter for the defined slots
232   GetAllSlots = _all_slots
233
234   def ToDict(self):
235     """Convert to a dict holding only standard python types.
236
237     The generic routine just dumps all of this object's attributes in
238     a dict. It does not work if the class has children who are
239     ConfigObjects themselves (e.g. the nics list in an Instance), in
240     which case the object should subclass the function in order to
241     make sure all objects returned are only standard python types.
242
243     """
244     result = {}
245     for name in self._all_slots():
246       value = getattr(self, name, None)
247       if value is not None:
248         result[name] = value
249     return result
250
251   __getstate__ = ToDict
252
253   @classmethod
254   def FromDict(cls, val):
255     """Create an object from a dictionary.
256
257     This generic routine takes a dict, instantiates a new instance of
258     the given class, and sets attributes based on the dict content.
259
260     As for `ToDict`, this does not work if the class has children
261     who are ConfigObjects themselves (e.g. the nics list in an
262     Instance), in which case the object should subclass the function
263     and alter the objects.
264
265     """
266     if not isinstance(val, dict):
267       raise errors.ConfigurationError("Invalid object passed to FromDict:"
268                                       " expected dict, got %s" % type(val))
269     val_str = dict([(str(k), v) for k, v in val.iteritems()])
270     obj = cls(**val_str) # pylint: disable=W0142
271     return obj
272
273   @staticmethod
274   def _ContainerToDicts(container):
275     """Convert the elements of a container to standard python types.
276
277     This method converts a container with elements derived from
278     ConfigData to standard python types. If the container is a dict,
279     we don't touch the keys, only the values.
280
281     """
282     if isinstance(container, dict):
283       ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
284     elif isinstance(container, (list, tuple, set, frozenset)):
285       ret = [elem.ToDict() for elem in container]
286     else:
287       raise TypeError("Invalid type %s passed to _ContainerToDicts" %
288                       type(container))
289     return ret
290
291   @staticmethod
292   def _ContainerFromDicts(source, c_type, e_type):
293     """Convert a container from standard python types.
294
295     This method converts a container with standard python types to
296     ConfigData objects. If the container is a dict, we don't touch the
297     keys, only the values.
298
299     """
300     if not isinstance(c_type, type):
301       raise TypeError("Container type %s passed to _ContainerFromDicts is"
302                       " not a type" % type(c_type))
303     if source is None:
304       source = c_type()
305     if c_type is dict:
306       ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
307     elif c_type in (list, tuple, set, frozenset):
308       ret = c_type([e_type.FromDict(elem) for elem in source])
309     else:
310       raise TypeError("Invalid container type %s passed to"
311                       " _ContainerFromDicts" % c_type)
312     return ret
313
314   def Copy(self):
315     """Makes a deep copy of the current object and its children.
316
317     """
318     dict_form = self.ToDict()
319     clone_obj = self.__class__.FromDict(dict_form)
320     return clone_obj
321
322   def __repr__(self):
323     """Implement __repr__ for ConfigObjects."""
324     return repr(self.ToDict())
325
326   def UpgradeConfig(self):
327     """Fill defaults for missing configuration values.
328
329     This method will be called at configuration load time, and its
330     implementation will be object dependent.
331
332     """
333     pass
334
335
336 class TaggableObject(ConfigObject):
337   """An generic class supporting tags.
338
339   """
340   __slots__ = ["tags"]
341   VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
342
343   @classmethod
344   def ValidateTag(cls, tag):
345     """Check if a tag is valid.
346
347     If the tag is invalid, an errors.TagError will be raised. The
348     function has no return value.
349
350     """
351     if not isinstance(tag, basestring):
352       raise errors.TagError("Invalid tag type (not a string)")
353     if len(tag) > constants.MAX_TAG_LEN:
354       raise errors.TagError("Tag too long (>%d characters)" %
355                             constants.MAX_TAG_LEN)
356     if not tag:
357       raise errors.TagError("Tags cannot be empty")
358     if not cls.VALID_TAG_RE.match(tag):
359       raise errors.TagError("Tag contains invalid characters")
360
361   def GetTags(self):
362     """Return the tags list.
363
364     """
365     tags = getattr(self, "tags", None)
366     if tags is None:
367       tags = self.tags = set()
368     return tags
369
370   def AddTag(self, tag):
371     """Add a new tag.
372
373     """
374     self.ValidateTag(tag)
375     tags = self.GetTags()
376     if len(tags) >= constants.MAX_TAGS_PER_OBJ:
377       raise errors.TagError("Too many tags")
378     self.GetTags().add(tag)
379
380   def RemoveTag(self, tag):
381     """Remove a tag.
382
383     """
384     self.ValidateTag(tag)
385     tags = self.GetTags()
386     try:
387       tags.remove(tag)
388     except KeyError:
389       raise errors.TagError("Tag not found")
390
391   def ToDict(self):
392     """Taggable-object-specific conversion to standard python types.
393
394     This replaces the tags set with a list.
395
396     """
397     bo = super(TaggableObject, self).ToDict()
398
399     tags = bo.get("tags", None)
400     if isinstance(tags, set):
401       bo["tags"] = list(tags)
402     return bo
403
404   @classmethod
405   def FromDict(cls, val):
406     """Custom function for instances.
407
408     """
409     obj = super(TaggableObject, cls).FromDict(val)
410     if hasattr(obj, "tags") and isinstance(obj.tags, list):
411       obj.tags = set(obj.tags)
412     return obj
413
414
415 class MasterNetworkParameters(ConfigObject):
416   """Network configuration parameters for the master
417
418   @ivar name: master name
419   @ivar ip: master IP
420   @ivar netmask: master netmask
421   @ivar netdev: master network device
422   @ivar ip_family: master IP family
423
424   """
425   __slots__ = [
426     "name",
427     "ip",
428     "netmask",
429     "netdev",
430     "ip_family"
431     ]
432
433
434 class ConfigData(ConfigObject):
435   """Top-level config object."""
436   __slots__ = [
437     "version",
438     "cluster",
439     "nodes",
440     "nodegroups",
441     "instances",
442     "serial_no",
443     ] + _TIMESTAMPS
444
445   def ToDict(self):
446     """Custom function for top-level config data.
447
448     This just replaces the list of instances, nodes and the cluster
449     with standard python types.
450
451     """
452     mydict = super(ConfigData, self).ToDict()
453     mydict["cluster"] = mydict["cluster"].ToDict()
454     for key in "nodes", "instances", "nodegroups":
455       mydict[key] = self._ContainerToDicts(mydict[key])
456
457     return mydict
458
459   @classmethod
460   def FromDict(cls, val):
461     """Custom function for top-level config data
462
463     """
464     obj = super(ConfigData, cls).FromDict(val)
465     obj.cluster = Cluster.FromDict(obj.cluster)
466     obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
467     obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
468     obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
469     return obj
470
471   def HasAnyDiskOfType(self, dev_type):
472     """Check if in there is at disk of the given type in the configuration.
473
474     @type dev_type: L{constants.LDS_BLOCK}
475     @param dev_type: the type to look for
476     @rtype: boolean
477     @return: boolean indicating if a disk of the given type was found or not
478
479     """
480     for instance in self.instances.values():
481       for disk in instance.disks:
482         if disk.IsBasedOnDiskType(dev_type):
483           return True
484     return False
485
486   def UpgradeConfig(self):
487     """Fill defaults for missing configuration values.
488
489     """
490     self.cluster.UpgradeConfig()
491     for node in self.nodes.values():
492       node.UpgradeConfig()
493     for instance in self.instances.values():
494       instance.UpgradeConfig()
495     if self.nodegroups is None:
496       self.nodegroups = {}
497     for nodegroup in self.nodegroups.values():
498       nodegroup.UpgradeConfig()
499     if self.cluster.drbd_usermode_helper is None:
500       # To decide if we set an helper let's check if at least one instance has
501       # a DRBD disk. This does not cover all the possible scenarios but it
502       # gives a good approximation.
503       if self.HasAnyDiskOfType(constants.LD_DRBD8):
504         self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
505
506
507 class NIC(ConfigObject):
508   """Config object representing a network card."""
509   __slots__ = ["mac", "ip", "nicparams"]
510
511   @classmethod
512   def CheckParameterSyntax(cls, nicparams):
513     """Check the given parameters for validity.
514
515     @type nicparams:  dict
516     @param nicparams: dictionary with parameter names/value
517     @raise errors.ConfigurationError: when a parameter is not valid
518
519     """
520     if (nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES and
521         nicparams[constants.NIC_MODE] != constants.VALUE_AUTO):
522       err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
523       raise errors.ConfigurationError(err)
524
525     if (nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED and
526         not nicparams[constants.NIC_LINK]):
527       err = "Missing bridged nic link"
528       raise errors.ConfigurationError(err)
529
530
531 class Disk(ConfigObject):
532   """Config object representing a block device."""
533   __slots__ = ["dev_type", "logical_id", "physical_id",
534                "children", "iv_name", "size", "mode", "params"]
535
536   def CreateOnSecondary(self):
537     """Test if this device needs to be created on a secondary node."""
538     return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
539
540   def AssembleOnSecondary(self):
541     """Test if this device needs to be assembled on a secondary node."""
542     return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
543
544   def OpenOnSecondary(self):
545     """Test if this device needs to be opened on a secondary node."""
546     return self.dev_type in (constants.LD_LV,)
547
548   def StaticDevPath(self):
549     """Return the device path if this device type has a static one.
550
551     Some devices (LVM for example) live always at the same /dev/ path,
552     irrespective of their status. For such devices, we return this
553     path, for others we return None.
554
555     @warning: The path returned is not a normalized pathname; callers
556         should check that it is a valid path.
557
558     """
559     if self.dev_type == constants.LD_LV:
560       return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
561     elif self.dev_type == constants.LD_BLOCKDEV:
562       return self.logical_id[1]
563     elif self.dev_type == constants.LD_RBD:
564       return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
565     return None
566
567   def ChildrenNeeded(self):
568     """Compute the needed number of children for activation.
569
570     This method will return either -1 (all children) or a positive
571     number denoting the minimum number of children needed for
572     activation (only mirrored devices will usually return >=0).
573
574     Currently, only DRBD8 supports diskless activation (therefore we
575     return 0), for all other we keep the previous semantics and return
576     -1.
577
578     """
579     if self.dev_type == constants.LD_DRBD8:
580       return 0
581     return -1
582
583   def IsBasedOnDiskType(self, dev_type):
584     """Check if the disk or its children are based on the given type.
585
586     @type dev_type: L{constants.LDS_BLOCK}
587     @param dev_type: the type to look for
588     @rtype: boolean
589     @return: boolean indicating if a device of the given type was found or not
590
591     """
592     if self.children:
593       for child in self.children:
594         if child.IsBasedOnDiskType(dev_type):
595           return True
596     return self.dev_type == dev_type
597
598   def GetNodes(self, node):
599     """This function returns the nodes this device lives on.
600
601     Given the node on which the parent of the device lives on (or, in
602     case of a top-level device, the primary node of the devices'
603     instance), this function will return a list of nodes on which this
604     devices needs to (or can) be assembled.
605
606     """
607     if self.dev_type in [constants.LD_LV, constants.LD_FILE,
608                          constants.LD_BLOCKDEV, constants.LD_RBD]:
609       result = [node]
610     elif self.dev_type in constants.LDS_DRBD:
611       result = [self.logical_id[0], self.logical_id[1]]
612       if node not in result:
613         raise errors.ConfigurationError("DRBD device passed unknown node")
614     else:
615       raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
616     return result
617
618   def ComputeNodeTree(self, parent_node):
619     """Compute the node/disk tree for this disk and its children.
620
621     This method, given the node on which the parent disk lives, will
622     return the list of all (node, disk) pairs which describe the disk
623     tree in the most compact way. For example, a drbd/lvm stack
624     will be returned as (primary_node, drbd) and (secondary_node, drbd)
625     which represents all the top-level devices on the nodes.
626
627     """
628     my_nodes = self.GetNodes(parent_node)
629     result = [(node, self) for node in my_nodes]
630     if not self.children:
631       # leaf device
632       return result
633     for node in my_nodes:
634       for child in self.children:
635         child_result = child.ComputeNodeTree(node)
636         if len(child_result) == 1:
637           # child (and all its descendants) is simple, doesn't split
638           # over multiple hosts, so we don't need to describe it, our
639           # own entry for this node describes it completely
640           continue
641         else:
642           # check if child nodes differ from my nodes; note that
643           # subdisk can differ from the child itself, and be instead
644           # one of its descendants
645           for subnode, subdisk in child_result:
646             if subnode not in my_nodes:
647               result.append((subnode, subdisk))
648             # otherwise child is under our own node, so we ignore this
649             # entry (but probably the other results in the list will
650             # be different)
651     return result
652
653   def ComputeGrowth(self, amount):
654     """Compute the per-VG growth requirements.
655
656     This only works for VG-based disks.
657
658     @type amount: integer
659     @param amount: the desired increase in (user-visible) disk space
660     @rtype: dict
661     @return: a dictionary of volume-groups and the required size
662
663     """
664     if self.dev_type == constants.LD_LV:
665       return {self.logical_id[0]: amount}
666     elif self.dev_type == constants.LD_DRBD8:
667       if self.children:
668         return self.children[0].ComputeGrowth(amount)
669       else:
670         return {}
671     else:
672       # Other disk types do not require VG space
673       return {}
674
675   def RecordGrow(self, amount):
676     """Update the size of this disk after growth.
677
678     This method recurses over the disks's children and updates their
679     size correspondigly. The method needs to be kept in sync with the
680     actual algorithms from bdev.
681
682     """
683     if self.dev_type in (constants.LD_LV, constants.LD_FILE,
684                          constants.LD_RBD):
685       self.size += amount
686     elif self.dev_type == constants.LD_DRBD8:
687       if self.children:
688         self.children[0].RecordGrow(amount)
689       self.size += amount
690     else:
691       raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
692                                    " disk type %s" % self.dev_type)
693
694   def Update(self, size=None, mode=None):
695     """Apply changes to size and mode.
696
697     """
698     if self.dev_type == constants.LD_DRBD8:
699       if self.children:
700         self.children[0].Update(size=size, mode=mode)
701     else:
702       assert not self.children
703
704     if size is not None:
705       self.size = size
706     if mode is not None:
707       self.mode = mode
708
709   def UnsetSize(self):
710     """Sets recursively the size to zero for the disk and its children.
711
712     """
713     if self.children:
714       for child in self.children:
715         child.UnsetSize()
716     self.size = 0
717
718   def SetPhysicalID(self, target_node, nodes_ip):
719     """Convert the logical ID to the physical ID.
720
721     This is used only for drbd, which needs ip/port configuration.
722
723     The routine descends down and updates its children also, because
724     this helps when the only the top device is passed to the remote
725     node.
726
727     Arguments:
728       - target_node: the node we wish to configure for
729       - nodes_ip: a mapping of node name to ip
730
731     The target_node must exist in in nodes_ip, and must be one of the
732     nodes in the logical ID for each of the DRBD devices encountered
733     in the disk tree.
734
735     """
736     if self.children:
737       for child in self.children:
738         child.SetPhysicalID(target_node, nodes_ip)
739
740     if self.logical_id is None and self.physical_id is not None:
741       return
742     if self.dev_type in constants.LDS_DRBD:
743       pnode, snode, port, pminor, sminor, secret = self.logical_id
744       if target_node not in (pnode, snode):
745         raise errors.ConfigurationError("DRBD device not knowing node %s" %
746                                         target_node)
747       pnode_ip = nodes_ip.get(pnode, None)
748       snode_ip = nodes_ip.get(snode, None)
749       if pnode_ip is None or snode_ip is None:
750         raise errors.ConfigurationError("Can't find primary or secondary node"
751                                         " for %s" % str(self))
752       p_data = (pnode_ip, port)
753       s_data = (snode_ip, port)
754       if pnode == target_node:
755         self.physical_id = p_data + s_data + (pminor, secret)
756       else: # it must be secondary, we tested above
757         self.physical_id = s_data + p_data + (sminor, secret)
758     else:
759       self.physical_id = self.logical_id
760     return
761
762   def ToDict(self):
763     """Disk-specific conversion to standard python types.
764
765     This replaces the children lists of objects with lists of
766     standard python types.
767
768     """
769     bo = super(Disk, self).ToDict()
770
771     for attr in ("children",):
772       alist = bo.get(attr, None)
773       if alist:
774         bo[attr] = self._ContainerToDicts(alist)
775     return bo
776
777   @classmethod
778   def FromDict(cls, val):
779     """Custom function for Disks
780
781     """
782     obj = super(Disk, cls).FromDict(val)
783     if obj.children:
784       obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
785     if obj.logical_id and isinstance(obj.logical_id, list):
786       obj.logical_id = tuple(obj.logical_id)
787     if obj.physical_id and isinstance(obj.physical_id, list):
788       obj.physical_id = tuple(obj.physical_id)
789     if obj.dev_type in constants.LDS_DRBD:
790       # we need a tuple of length six here
791       if len(obj.logical_id) < 6:
792         obj.logical_id += (None,) * (6 - len(obj.logical_id))
793     return obj
794
795   def __str__(self):
796     """Custom str() formatter for disks.
797
798     """
799     if self.dev_type == constants.LD_LV:
800       val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
801     elif self.dev_type in constants.LDS_DRBD:
802       node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
803       val = "<DRBD8("
804       if self.physical_id is None:
805         phy = "unconfigured"
806       else:
807         phy = ("configured as %s:%s %s:%s" %
808                (self.physical_id[0], self.physical_id[1],
809                 self.physical_id[2], self.physical_id[3]))
810
811       val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
812               (node_a, minor_a, node_b, minor_b, port, phy))
813       if self.children and self.children.count(None) == 0:
814         val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
815       else:
816         val += "no local storage"
817     else:
818       val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
819              (self.dev_type, self.logical_id, self.physical_id, self.children))
820     if self.iv_name is None:
821       val += ", not visible"
822     else:
823       val += ", visible as /dev/%s" % self.iv_name
824     if isinstance(self.size, int):
825       val += ", size=%dm)>" % self.size
826     else:
827       val += ", size='%s')>" % (self.size,)
828     return val
829
830   def Verify(self):
831     """Checks that this disk is correctly configured.
832
833     """
834     all_errors = []
835     if self.mode not in constants.DISK_ACCESS_SET:
836       all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
837     return all_errors
838
839   def UpgradeConfig(self):
840     """Fill defaults for missing configuration values.
841
842     """
843     if self.children:
844       for child in self.children:
845         child.UpgradeConfig()
846
847     if not self.params:
848       self.params = constants.DISK_LD_DEFAULTS[self.dev_type].copy()
849     else:
850       self.params = FillDict(constants.DISK_LD_DEFAULTS[self.dev_type],
851                              self.params)
852     # add here config upgrade for this disk
853
854   @staticmethod
855   def ComputeLDParams(disk_template, disk_params):
856     """Computes Logical Disk parameters from Disk Template parameters.
857
858     @type disk_template: string
859     @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
860     @type disk_params: dict
861     @param disk_params: disk template parameters;
862                         dict(template_name -> parameters
863     @rtype: list(dict)
864     @return: a list of dicts, one for each node of the disk hierarchy. Each dict
865       contains the LD parameters of the node. The tree is flattened in-order.
866
867     """
868     if disk_template not in constants.DISK_TEMPLATES:
869       raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
870
871     assert disk_template in disk_params
872
873     result = list()
874     dt_params = disk_params[disk_template]
875     if disk_template == constants.DT_DRBD8:
876       drbd_params = {
877         constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
878         constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
879         constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
880         constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
881         constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
882         constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
883         constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
884         constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
885         constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
886         constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
887         constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
888         constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
889         }
890
891       drbd_params = \
892         FillDict(constants.DISK_LD_DEFAULTS[constants.LD_DRBD8],
893                  drbd_params)
894
895       result.append(drbd_params)
896
897       # data LV
898       data_params = {
899         constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
900         }
901       data_params = \
902         FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV],
903                  data_params)
904       result.append(data_params)
905
906       # metadata LV
907       meta_params = {
908         constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
909         }
910       meta_params = \
911         FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV],
912                  meta_params)
913       result.append(meta_params)
914
915     elif (disk_template == constants.DT_FILE or
916           disk_template == constants.DT_SHARED_FILE):
917       result.append(constants.DISK_LD_DEFAULTS[constants.LD_FILE])
918
919     elif disk_template == constants.DT_PLAIN:
920       params = {
921         constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
922         }
923       params = \
924         FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV],
925                  params)
926       result.append(params)
927
928     elif disk_template == constants.DT_BLOCK:
929       result.append(constants.DISK_LD_DEFAULTS[constants.LD_BLOCKDEV])
930
931     elif disk_template == constants.DT_RBD:
932       params = {
933         constants.LDP_POOL: dt_params[constants.RBD_POOL]
934         }
935       params = \
936         FillDict(constants.DISK_LD_DEFAULTS[constants.LD_RBD],
937                  params)
938       result.append(params)
939
940     return result
941
942
943 class InstancePolicy(ConfigObject):
944   """Config object representing instance policy limits dictionary.
945
946
947   Note that this object is not actually used in the config, it's just
948   used as a placeholder for a few functions.
949
950   """
951   @classmethod
952   def CheckParameterSyntax(cls, ipolicy):
953     """ Check the instance policy for validity.
954
955     """
956     for param in constants.ISPECS_PARAMETERS:
957       InstancePolicy.CheckISpecSyntax(ipolicy, param)
958     if constants.IPOLICY_DTS in ipolicy:
959       InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
960     for key in constants.IPOLICY_PARAMETERS:
961       if key in ipolicy:
962         InstancePolicy.CheckParameter(key, ipolicy[key])
963     wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
964     if wrong_keys:
965       raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
966                                       utils.CommaJoin(wrong_keys))
967
968   @classmethod
969   def CheckISpecSyntax(cls, ipolicy, name):
970     """Check the instance policy for validity on a given key.
971
972     We check if the instance policy makes sense for a given key, that is
973     if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
974
975     @type ipolicy: dict
976     @param ipolicy: dictionary with min, max, std specs
977     @type name: string
978     @param name: what are the limits for
979     @raise errors.ConfigureError: when specs for given name are not valid
980
981     """
982     min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
983     std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
984     max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
985     err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
986            (name,
987             ipolicy[constants.ISPECS_MIN].get(name, "-"),
988             ipolicy[constants.ISPECS_MAX].get(name, "-"),
989             ipolicy[constants.ISPECS_STD].get(name, "-")))
990     if min_v > std_v or std_v > max_v:
991       raise errors.ConfigurationError(err)
992
993   @classmethod
994   def CheckDiskTemplates(cls, disk_templates):
995     """Checks the disk templates for validity.
996
997     """
998     wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
999     if wrong:
1000       raise errors.ConfigurationError("Invalid disk template(s) %s" %
1001                                       utils.CommaJoin(wrong))
1002
1003   @classmethod
1004   def CheckParameter(cls, key, value):
1005     """Checks a parameter.
1006
1007     Currently we expect all parameters to be float values.
1008
1009     """
1010     try:
1011       float(value)
1012     except (TypeError, ValueError), err:
1013       raise errors.ConfigurationError("Invalid value for key" " '%s':"
1014                                       " '%s', error: %s" % (key, value, err))
1015
1016
1017 class Instance(TaggableObject):
1018   """Config object representing an instance."""
1019   __slots__ = [
1020     "name",
1021     "primary_node",
1022     "os",
1023     "hypervisor",
1024     "hvparams",
1025     "beparams",
1026     "osparams",
1027     "admin_state",
1028     "nics",
1029     "disks",
1030     "disk_template",
1031     "network_port",
1032     "serial_no",
1033     ] + _TIMESTAMPS + _UUID
1034
1035   def _ComputeSecondaryNodes(self):
1036     """Compute the list of secondary nodes.
1037
1038     This is a simple wrapper over _ComputeAllNodes.
1039
1040     """
1041     all_nodes = set(self._ComputeAllNodes())
1042     all_nodes.discard(self.primary_node)
1043     return tuple(all_nodes)
1044
1045   secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1046                              "List of secondary nodes")
1047
1048   def _ComputeAllNodes(self):
1049     """Compute the list of all nodes.
1050
1051     Since the data is already there (in the drbd disks), keeping it as
1052     a separate normal attribute is redundant and if not properly
1053     synchronised can cause problems. Thus it's better to compute it
1054     dynamically.
1055
1056     """
1057     def _Helper(nodes, device):
1058       """Recursively computes nodes given a top device."""
1059       if device.dev_type in constants.LDS_DRBD:
1060         nodea, nodeb = device.logical_id[:2]
1061         nodes.add(nodea)
1062         nodes.add(nodeb)
1063       if device.children:
1064         for child in device.children:
1065           _Helper(nodes, child)
1066
1067     all_nodes = set()
1068     all_nodes.add(self.primary_node)
1069     for device in self.disks:
1070       _Helper(all_nodes, device)
1071     return tuple(all_nodes)
1072
1073   all_nodes = property(_ComputeAllNodes, None, None,
1074                        "List of all nodes of the instance")
1075
1076   def MapLVsByNode(self, lvmap=None, devs=None, node=None):
1077     """Provide a mapping of nodes to LVs this instance owns.
1078
1079     This function figures out what logical volumes should belong on
1080     which nodes, recursing through a device tree.
1081
1082     @param lvmap: optional dictionary to receive the
1083         'node' : ['lv', ...] data.
1084
1085     @return: None if lvmap arg is given, otherwise, a dictionary of
1086         the form { 'nodename' : ['volume1', 'volume2', ...], ... };
1087         volumeN is of the form "vg_name/lv_name", compatible with
1088         GetVolumeList()
1089
1090     """
1091     if node == None:
1092       node = self.primary_node
1093
1094     if lvmap is None:
1095       lvmap = {
1096         node: [],
1097         }
1098       ret = lvmap
1099     else:
1100       if not node in lvmap:
1101         lvmap[node] = []
1102       ret = None
1103
1104     if not devs:
1105       devs = self.disks
1106
1107     for dev in devs:
1108       if dev.dev_type == constants.LD_LV:
1109         lvmap[node].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1110
1111       elif dev.dev_type in constants.LDS_DRBD:
1112         if dev.children:
1113           self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1114           self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1115
1116       elif dev.children:
1117         self.MapLVsByNode(lvmap, dev.children, node)
1118
1119     return ret
1120
1121   def FindDisk(self, idx):
1122     """Find a disk given having a specified index.
1123
1124     This is just a wrapper that does validation of the index.
1125
1126     @type idx: int
1127     @param idx: the disk index
1128     @rtype: L{Disk}
1129     @return: the corresponding disk
1130     @raise errors.OpPrereqError: when the given index is not valid
1131
1132     """
1133     try:
1134       idx = int(idx)
1135       return self.disks[idx]
1136     except (TypeError, ValueError), err:
1137       raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1138                                  errors.ECODE_INVAL)
1139     except IndexError:
1140       raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1141                                  " 0 to %d" % (idx, len(self.disks) - 1),
1142                                  errors.ECODE_INVAL)
1143
1144   def ToDict(self):
1145     """Instance-specific conversion to standard python types.
1146
1147     This replaces the children lists of objects with lists of standard
1148     python types.
1149
1150     """
1151     bo = super(Instance, self).ToDict()
1152
1153     for attr in "nics", "disks":
1154       alist = bo.get(attr, None)
1155       if alist:
1156         nlist = self._ContainerToDicts(alist)
1157       else:
1158         nlist = []
1159       bo[attr] = nlist
1160     return bo
1161
1162   @classmethod
1163   def FromDict(cls, val):
1164     """Custom function for instances.
1165
1166     """
1167     if "admin_state" not in val:
1168       if val.get("admin_up", False):
1169         val["admin_state"] = constants.ADMINST_UP
1170       else:
1171         val["admin_state"] = constants.ADMINST_DOWN
1172     if "admin_up" in val:
1173       del val["admin_up"]
1174     obj = super(Instance, cls).FromDict(val)
1175     obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
1176     obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
1177     return obj
1178
1179   def UpgradeConfig(self):
1180     """Fill defaults for missing configuration values.
1181
1182     """
1183     for nic in self.nics:
1184       nic.UpgradeConfig()
1185     for disk in self.disks:
1186       disk.UpgradeConfig()
1187     if self.hvparams:
1188       for key in constants.HVC_GLOBALS:
1189         try:
1190           del self.hvparams[key]
1191         except KeyError:
1192           pass
1193     if self.osparams is None:
1194       self.osparams = {}
1195     UpgradeBeParams(self.beparams)
1196
1197
1198 class OS(ConfigObject):
1199   """Config object representing an operating system.
1200
1201   @type supported_parameters: list
1202   @ivar supported_parameters: a list of tuples, name and description,
1203       containing the supported parameters by this OS
1204
1205   @type VARIANT_DELIM: string
1206   @cvar VARIANT_DELIM: the variant delimiter
1207
1208   """
1209   __slots__ = [
1210     "name",
1211     "path",
1212     "api_versions",
1213     "create_script",
1214     "export_script",
1215     "import_script",
1216     "rename_script",
1217     "verify_script",
1218     "supported_variants",
1219     "supported_parameters",
1220     ]
1221
1222   VARIANT_DELIM = "+"
1223
1224   @classmethod
1225   def SplitNameVariant(cls, name):
1226     """Splits the name into the proper name and variant.
1227
1228     @param name: the OS (unprocessed) name
1229     @rtype: list
1230     @return: a list of two elements; if the original name didn't
1231         contain a variant, it's returned as an empty string
1232
1233     """
1234     nv = name.split(cls.VARIANT_DELIM, 1)
1235     if len(nv) == 1:
1236       nv.append("")
1237     return nv
1238
1239   @classmethod
1240   def GetName(cls, name):
1241     """Returns the proper name of the os (without the variant).
1242
1243     @param name: the OS (unprocessed) name
1244
1245     """
1246     return cls.SplitNameVariant(name)[0]
1247
1248   @classmethod
1249   def GetVariant(cls, name):
1250     """Returns the variant the os (without the base name).
1251
1252     @param name: the OS (unprocessed) name
1253
1254     """
1255     return cls.SplitNameVariant(name)[1]
1256
1257
1258 class NodeHvState(ConfigObject):
1259   """Hypvervisor state on a node.
1260
1261   @ivar mem_total: Total amount of memory
1262   @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1263     available)
1264   @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1265     rounding
1266   @ivar mem_inst: Memory used by instances living on node
1267   @ivar cpu_total: Total node CPU core count
1268   @ivar cpu_node: Number of CPU cores reserved for the node itself
1269
1270   """
1271   __slots__ = [
1272     "mem_total",
1273     "mem_node",
1274     "mem_hv",
1275     "mem_inst",
1276     "cpu_total",
1277     "cpu_node",
1278     ] + _TIMESTAMPS
1279
1280
1281 class NodeDiskState(ConfigObject):
1282   """Disk state on a node.
1283
1284   """
1285   __slots__ = [
1286     "total",
1287     "reserved",
1288     "overhead",
1289     ] + _TIMESTAMPS
1290
1291
1292 class Node(TaggableObject):
1293   """Config object representing a node.
1294
1295   @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1296   @ivar hv_state_static: Hypervisor state overriden by user
1297   @ivar disk_state: Disk state (e.g. free space)
1298   @ivar disk_state_static: Disk state overriden by user
1299
1300   """
1301   __slots__ = [
1302     "name",
1303     "primary_ip",
1304     "secondary_ip",
1305     "serial_no",
1306     "master_candidate",
1307     "offline",
1308     "drained",
1309     "group",
1310     "master_capable",
1311     "vm_capable",
1312     "ndparams",
1313     "powered",
1314     "hv_state",
1315     "hv_state_static",
1316     "disk_state",
1317     "disk_state_static",
1318     ] + _TIMESTAMPS + _UUID
1319
1320   def UpgradeConfig(self):
1321     """Fill defaults for missing configuration values.
1322
1323     """
1324     # pylint: disable=E0203
1325     # because these are "defined" via slots, not manually
1326     if self.master_capable is None:
1327       self.master_capable = True
1328
1329     if self.vm_capable is None:
1330       self.vm_capable = True
1331
1332     if self.ndparams is None:
1333       self.ndparams = {}
1334
1335     if self.powered is None:
1336       self.powered = True
1337
1338   def ToDict(self):
1339     """Custom function for serializing.
1340
1341     """
1342     data = super(Node, self).ToDict()
1343
1344     hv_state = data.get("hv_state", None)
1345     if hv_state is not None:
1346       data["hv_state"] = self._ContainerToDicts(hv_state)
1347
1348     disk_state = data.get("disk_state", None)
1349     if disk_state is not None:
1350       data["disk_state"] = \
1351         dict((key, self._ContainerToDicts(value))
1352              for (key, value) in disk_state.items())
1353
1354     return data
1355
1356   @classmethod
1357   def FromDict(cls, val):
1358     """Custom function for deserializing.
1359
1360     """
1361     obj = super(Node, cls).FromDict(val)
1362
1363     if obj.hv_state is not None:
1364       obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1365
1366     if obj.disk_state is not None:
1367       obj.disk_state = \
1368         dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
1369              for (key, value) in obj.disk_state.items())
1370
1371     return obj
1372
1373
1374 class NodeGroup(TaggableObject):
1375   """Config object representing a node group."""
1376   __slots__ = [
1377     "name",
1378     "members",
1379     "ndparams",
1380     "diskparams",
1381     "ipolicy",
1382     "serial_no",
1383     "hv_state_static",
1384     "disk_state_static",
1385     "alloc_policy",
1386     ] + _TIMESTAMPS + _UUID
1387
1388   def ToDict(self):
1389     """Custom function for nodegroup.
1390
1391     This discards the members object, which gets recalculated and is only kept
1392     in memory.
1393
1394     """
1395     mydict = super(NodeGroup, self).ToDict()
1396     del mydict["members"]
1397     return mydict
1398
1399   @classmethod
1400   def FromDict(cls, val):
1401     """Custom function for nodegroup.
1402
1403     The members slot is initialized to an empty list, upon deserialization.
1404
1405     """
1406     obj = super(NodeGroup, cls).FromDict(val)
1407     obj.members = []
1408     return obj
1409
1410   def UpgradeConfig(self):
1411     """Fill defaults for missing configuration values.
1412
1413     """
1414     if self.ndparams is None:
1415       self.ndparams = {}
1416
1417     if self.serial_no is None:
1418       self.serial_no = 1
1419
1420     if self.alloc_policy is None:
1421       self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1422
1423     # We only update mtime, and not ctime, since we would not be able
1424     # to provide a correct value for creation time.
1425     if self.mtime is None:
1426       self.mtime = time.time()
1427
1428     if self.diskparams is None:
1429       self.diskparams = {}
1430     if self.ipolicy is None:
1431       self.ipolicy = MakeEmptyIPolicy()
1432
1433   def FillND(self, node):
1434     """Return filled out ndparams for L{objects.Node}
1435
1436     @type node: L{objects.Node}
1437     @param node: A Node object to fill
1438     @return a copy of the node's ndparams with defaults filled
1439
1440     """
1441     return self.SimpleFillND(node.ndparams)
1442
1443   def SimpleFillND(self, ndparams):
1444     """Fill a given ndparams dict with defaults.
1445
1446     @type ndparams: dict
1447     @param ndparams: the dict to fill
1448     @rtype: dict
1449     @return: a copy of the passed in ndparams with missing keys filled
1450         from the node group defaults
1451
1452     """
1453     return FillDict(self.ndparams, ndparams)
1454
1455
1456 class Cluster(TaggableObject):
1457   """Config object representing the cluster."""
1458   __slots__ = [
1459     "serial_no",
1460     "rsahostkeypub",
1461     "highest_used_port",
1462     "tcpudp_port_pool",
1463     "mac_prefix",
1464     "volume_group_name",
1465     "reserved_lvs",
1466     "drbd_usermode_helper",
1467     "default_bridge",
1468     "default_hypervisor",
1469     "master_node",
1470     "master_ip",
1471     "master_netdev",
1472     "master_netmask",
1473     "use_external_mip_script",
1474     "cluster_name",
1475     "file_storage_dir",
1476     "shared_file_storage_dir",
1477     "enabled_hypervisors",
1478     "hvparams",
1479     "ipolicy",
1480     "os_hvp",
1481     "beparams",
1482     "osparams",
1483     "nicparams",
1484     "ndparams",
1485     "diskparams",
1486     "candidate_pool_size",
1487     "modify_etc_hosts",
1488     "modify_ssh_setup",
1489     "maintain_node_health",
1490     "uid_pool",
1491     "default_iallocator",
1492     "hidden_os",
1493     "blacklisted_os",
1494     "primary_ip_family",
1495     "prealloc_wipe_disks",
1496     "hv_state_static",
1497     "disk_state_static",
1498     ] + _TIMESTAMPS + _UUID
1499
1500   def UpgradeConfig(self):
1501     """Fill defaults for missing configuration values.
1502
1503     """
1504     # pylint: disable=E0203
1505     # because these are "defined" via slots, not manually
1506     if self.hvparams is None:
1507       self.hvparams = constants.HVC_DEFAULTS
1508     else:
1509       for hypervisor in self.hvparams:
1510         self.hvparams[hypervisor] = FillDict(
1511             constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1512
1513     if self.os_hvp is None:
1514       self.os_hvp = {}
1515
1516     # osparams added before 2.2
1517     if self.osparams is None:
1518       self.osparams = {}
1519
1520     self.ndparams = UpgradeNDParams(self.ndparams)
1521
1522     self.beparams = UpgradeGroupedParams(self.beparams,
1523                                          constants.BEC_DEFAULTS)
1524     for beparams_group in self.beparams:
1525       UpgradeBeParams(self.beparams[beparams_group])
1526
1527     migrate_default_bridge = not self.nicparams
1528     self.nicparams = UpgradeGroupedParams(self.nicparams,
1529                                           constants.NICC_DEFAULTS)
1530     if migrate_default_bridge:
1531       self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1532         self.default_bridge
1533
1534     if self.modify_etc_hosts is None:
1535       self.modify_etc_hosts = True
1536
1537     if self.modify_ssh_setup is None:
1538       self.modify_ssh_setup = True
1539
1540     # default_bridge is no longer used in 2.1. The slot is left there to
1541     # support auto-upgrading. It can be removed once we decide to deprecate
1542     # upgrading straight from 2.0.
1543     if self.default_bridge is not None:
1544       self.default_bridge = None
1545
1546     # default_hypervisor is just the first enabled one in 2.1. This slot and
1547     # code can be removed once upgrading straight from 2.0 is deprecated.
1548     if self.default_hypervisor is not None:
1549       self.enabled_hypervisors = ([self.default_hypervisor] +
1550         [hvname for hvname in self.enabled_hypervisors
1551          if hvname != self.default_hypervisor])
1552       self.default_hypervisor = None
1553
1554     # maintain_node_health added after 2.1.1
1555     if self.maintain_node_health is None:
1556       self.maintain_node_health = False
1557
1558     if self.uid_pool is None:
1559       self.uid_pool = []
1560
1561     if self.default_iallocator is None:
1562       self.default_iallocator = ""
1563
1564     # reserved_lvs added before 2.2
1565     if self.reserved_lvs is None:
1566       self.reserved_lvs = []
1567
1568     # hidden and blacklisted operating systems added before 2.2.1
1569     if self.hidden_os is None:
1570       self.hidden_os = []
1571
1572     if self.blacklisted_os is None:
1573       self.blacklisted_os = []
1574
1575     # primary_ip_family added before 2.3
1576     if self.primary_ip_family is None:
1577       self.primary_ip_family = AF_INET
1578
1579     if self.master_netmask is None:
1580       ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1581       self.master_netmask = ipcls.iplen
1582
1583     if self.prealloc_wipe_disks is None:
1584       self.prealloc_wipe_disks = False
1585
1586     # shared_file_storage_dir added before 2.5
1587     if self.shared_file_storage_dir is None:
1588       self.shared_file_storage_dir = ""
1589
1590     if self.use_external_mip_script is None:
1591       self.use_external_mip_script = False
1592
1593     if self.diskparams:
1594       self.diskparams = UpgradeDiskParams(self.diskparams)
1595     else:
1596       self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1597
1598     # instance policy added before 2.6
1599     if self.ipolicy is None:
1600       self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1601     else:
1602       # we can either make sure to upgrade the ipolicy always, or only
1603       # do it in some corner cases (e.g. missing keys); note that this
1604       # will break any removal of keys from the ipolicy dict
1605       self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1606
1607   @property
1608   def primary_hypervisor(self):
1609     """The first hypervisor is the primary.
1610
1611     Useful, for example, for L{Node}'s hv/disk state.
1612
1613     """
1614     return self.enabled_hypervisors[0]
1615
1616   def ToDict(self):
1617     """Custom function for cluster.
1618
1619     """
1620     mydict = super(Cluster, self).ToDict()
1621     mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1622     return mydict
1623
1624   @classmethod
1625   def FromDict(cls, val):
1626     """Custom function for cluster.
1627
1628     """
1629     obj = super(Cluster, cls).FromDict(val)
1630     if not isinstance(obj.tcpudp_port_pool, set):
1631       obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1632     return obj
1633
1634   def SimpleFillDP(self, diskparams):
1635     """Fill a given diskparams dict with cluster defaults.
1636
1637     @param diskparams: The diskparams
1638     @return: The defaults dict
1639
1640     """
1641     return FillDiskParams(self.diskparams, diskparams)
1642
1643   def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1644     """Get the default hypervisor parameters for the cluster.
1645
1646     @param hypervisor: the hypervisor name
1647     @param os_name: if specified, we'll also update the defaults for this OS
1648     @param skip_keys: if passed, list of keys not to use
1649     @return: the defaults dict
1650
1651     """
1652     if skip_keys is None:
1653       skip_keys = []
1654
1655     fill_stack = [self.hvparams.get(hypervisor, {})]
1656     if os_name is not None:
1657       os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1658       fill_stack.append(os_hvp)
1659
1660     ret_dict = {}
1661     for o_dict in fill_stack:
1662       ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1663
1664     return ret_dict
1665
1666   def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1667     """Fill a given hvparams dict with cluster defaults.
1668
1669     @type hv_name: string
1670     @param hv_name: the hypervisor to use
1671     @type os_name: string
1672     @param os_name: the OS to use for overriding the hypervisor defaults
1673     @type skip_globals: boolean
1674     @param skip_globals: if True, the global hypervisor parameters will
1675         not be filled
1676     @rtype: dict
1677     @return: a copy of the given hvparams with missing keys filled from
1678         the cluster defaults
1679
1680     """
1681     if skip_globals:
1682       skip_keys = constants.HVC_GLOBALS
1683     else:
1684       skip_keys = []
1685
1686     def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1687     return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1688
1689   def FillHV(self, instance, skip_globals=False):
1690     """Fill an instance's hvparams dict with cluster defaults.
1691
1692     @type instance: L{objects.Instance}
1693     @param instance: the instance parameter to fill
1694     @type skip_globals: boolean
1695     @param skip_globals: if True, the global hypervisor parameters will
1696         not be filled
1697     @rtype: dict
1698     @return: a copy of the instance's hvparams with missing keys filled from
1699         the cluster defaults
1700
1701     """
1702     return self.SimpleFillHV(instance.hypervisor, instance.os,
1703                              instance.hvparams, skip_globals)
1704
1705   def SimpleFillBE(self, beparams):
1706     """Fill a given beparams dict with cluster defaults.
1707
1708     @type beparams: dict
1709     @param beparams: the dict to fill
1710     @rtype: dict
1711     @return: a copy of the passed in beparams with missing keys filled
1712         from the cluster defaults
1713
1714     """
1715     return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1716
1717   def FillBE(self, instance):
1718     """Fill an instance's beparams dict with cluster defaults.
1719
1720     @type instance: L{objects.Instance}
1721     @param instance: the instance parameter to fill
1722     @rtype: dict
1723     @return: a copy of the instance's beparams with missing keys filled from
1724         the cluster defaults
1725
1726     """
1727     return self.SimpleFillBE(instance.beparams)
1728
1729   def SimpleFillNIC(self, nicparams):
1730     """Fill a given nicparams dict with cluster defaults.
1731
1732     @type nicparams: dict
1733     @param nicparams: the dict to fill
1734     @rtype: dict
1735     @return: a copy of the passed in nicparams with missing keys filled
1736         from the cluster defaults
1737
1738     """
1739     return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1740
1741   def SimpleFillOS(self, os_name, os_params):
1742     """Fill an instance's osparams dict with cluster defaults.
1743
1744     @type os_name: string
1745     @param os_name: the OS name to use
1746     @type os_params: dict
1747     @param os_params: the dict to fill with default values
1748     @rtype: dict
1749     @return: a copy of the instance's osparams with missing keys filled from
1750         the cluster defaults
1751
1752     """
1753     name_only = os_name.split("+", 1)[0]
1754     # base OS
1755     result = self.osparams.get(name_only, {})
1756     # OS with variant
1757     result = FillDict(result, self.osparams.get(os_name, {}))
1758     # specified params
1759     return FillDict(result, os_params)
1760
1761   @staticmethod
1762   def SimpleFillHvState(hv_state):
1763     """Fill an hv_state sub dict with cluster defaults.
1764
1765     """
1766     return FillDict(constants.HVST_DEFAULTS, hv_state)
1767
1768   @staticmethod
1769   def SimpleFillDiskState(disk_state):
1770     """Fill an disk_state sub dict with cluster defaults.
1771
1772     """
1773     return FillDict(constants.DS_DEFAULTS, disk_state)
1774
1775   def FillND(self, node, nodegroup):
1776     """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1777
1778     @type node: L{objects.Node}
1779     @param node: A Node object to fill
1780     @type nodegroup: L{objects.NodeGroup}
1781     @param nodegroup: A Node object to fill
1782     @return a copy of the node's ndparams with defaults filled
1783
1784     """
1785     return self.SimpleFillND(nodegroup.FillND(node))
1786
1787   def SimpleFillND(self, ndparams):
1788     """Fill a given ndparams dict with defaults.
1789
1790     @type ndparams: dict
1791     @param ndparams: the dict to fill
1792     @rtype: dict
1793     @return: a copy of the passed in ndparams with missing keys filled
1794         from the cluster defaults
1795
1796     """
1797     return FillDict(self.ndparams, ndparams)
1798
1799   def SimpleFillIPolicy(self, ipolicy):
1800     """ Fill instance policy dict with defaults.
1801
1802     @type ipolicy: dict
1803     @param ipolicy: the dict to fill
1804     @rtype: dict
1805     @return: a copy of passed ipolicy with missing keys filled from
1806       the cluster defaults
1807
1808     """
1809     return FillIPolicy(self.ipolicy, ipolicy)
1810
1811
1812 class BlockDevStatus(ConfigObject):
1813   """Config object representing the status of a block device."""
1814   __slots__ = [
1815     "dev_path",
1816     "major",
1817     "minor",
1818     "sync_percent",
1819     "estimated_time",
1820     "is_degraded",
1821     "ldisk_status",
1822     ]
1823
1824
1825 class ImportExportStatus(ConfigObject):
1826   """Config object representing the status of an import or export."""
1827   __slots__ = [
1828     "recent_output",
1829     "listen_port",
1830     "connected",
1831     "progress_mbytes",
1832     "progress_throughput",
1833     "progress_eta",
1834     "progress_percent",
1835     "exit_status",
1836     "error_message",
1837     ] + _TIMESTAMPS
1838
1839
1840 class ImportExportOptions(ConfigObject):
1841   """Options for import/export daemon
1842
1843   @ivar key_name: X509 key name (None for cluster certificate)
1844   @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1845   @ivar compress: Compression method (one of L{constants.IEC_ALL})
1846   @ivar magic: Used to ensure the connection goes to the right disk
1847   @ivar ipv6: Whether to use IPv6
1848   @ivar connect_timeout: Number of seconds for establishing connection
1849
1850   """
1851   __slots__ = [
1852     "key_name",
1853     "ca_pem",
1854     "compress",
1855     "magic",
1856     "ipv6",
1857     "connect_timeout",
1858     ]
1859
1860
1861 class ConfdRequest(ConfigObject):
1862   """Object holding a confd request.
1863
1864   @ivar protocol: confd protocol version
1865   @ivar type: confd query type
1866   @ivar query: query request
1867   @ivar rsalt: requested reply salt
1868
1869   """
1870   __slots__ = [
1871     "protocol",
1872     "type",
1873     "query",
1874     "rsalt",
1875     ]
1876
1877
1878 class ConfdReply(ConfigObject):
1879   """Object holding a confd reply.
1880
1881   @ivar protocol: confd protocol version
1882   @ivar status: reply status code (ok, error)
1883   @ivar answer: confd query reply
1884   @ivar serial: configuration serial number
1885
1886   """
1887   __slots__ = [
1888     "protocol",
1889     "status",
1890     "answer",
1891     "serial",
1892     ]
1893
1894
1895 class QueryFieldDefinition(ConfigObject):
1896   """Object holding a query field definition.
1897
1898   @ivar name: Field name
1899   @ivar title: Human-readable title
1900   @ivar kind: Field type
1901   @ivar doc: Human-readable description
1902
1903   """
1904   __slots__ = [
1905     "name",
1906     "title",
1907     "kind",
1908     "doc",
1909     ]
1910
1911
1912 class _QueryResponseBase(ConfigObject):
1913   __slots__ = [
1914     "fields",
1915     ]
1916
1917   def ToDict(self):
1918     """Custom function for serializing.
1919
1920     """
1921     mydict = super(_QueryResponseBase, self).ToDict()
1922     mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1923     return mydict
1924
1925   @classmethod
1926   def FromDict(cls, val):
1927     """Custom function for de-serializing.
1928
1929     """
1930     obj = super(_QueryResponseBase, cls).FromDict(val)
1931     obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1932     return obj
1933
1934
1935 class QueryResponse(_QueryResponseBase):
1936   """Object holding the response to a query.
1937
1938   @ivar fields: List of L{QueryFieldDefinition} objects
1939   @ivar data: Requested data
1940
1941   """
1942   __slots__ = [
1943     "data",
1944     ]
1945
1946
1947 class QueryFieldsRequest(ConfigObject):
1948   """Object holding a request for querying available fields.
1949
1950   """
1951   __slots__ = [
1952     "what",
1953     "fields",
1954     ]
1955
1956
1957 class QueryFieldsResponse(_QueryResponseBase):
1958   """Object holding the response to a query for fields.
1959
1960   @ivar fields: List of L{QueryFieldDefinition} objects
1961
1962   """
1963   __slots__ = [
1964     ]
1965
1966
1967 class MigrationStatus(ConfigObject):
1968   """Object holding the status of a migration.
1969
1970   """
1971   __slots__ = [
1972     "status",
1973     "transferred_ram",
1974     "total_ram",
1975     ]
1976
1977
1978 class InstanceConsole(ConfigObject):
1979   """Object describing how to access the console of an instance.
1980
1981   """
1982   __slots__ = [
1983     "instance",
1984     "kind",
1985     "message",
1986     "host",
1987     "port",
1988     "user",
1989     "command",
1990     "display",
1991     ]
1992
1993   def Validate(self):
1994     """Validates contents of this object.
1995
1996     """
1997     assert self.kind in constants.CONS_ALL, "Unknown console type"
1998     assert self.instance, "Missing instance name"
1999     assert self.message or self.kind in [constants.CONS_SSH,
2000                                          constants.CONS_SPICE,
2001                                          constants.CONS_VNC]
2002     assert self.host or self.kind == constants.CONS_MESSAGE
2003     assert self.port or self.kind in [constants.CONS_MESSAGE,
2004                                       constants.CONS_SSH]
2005     assert self.user or self.kind in [constants.CONS_MESSAGE,
2006                                       constants.CONS_SPICE,
2007                                       constants.CONS_VNC]
2008     assert self.command or self.kind in [constants.CONS_MESSAGE,
2009                                          constants.CONS_SPICE,
2010                                          constants.CONS_VNC]
2011     assert self.display or self.kind in [constants.CONS_MESSAGE,
2012                                          constants.CONS_SPICE,
2013                                          constants.CONS_SSH]
2014     return True
2015
2016
2017 class SerializableConfigParser(ConfigParser.SafeConfigParser):
2018   """Simple wrapper over ConfigParse that allows serialization.
2019
2020   This class is basically ConfigParser.SafeConfigParser with two
2021   additional methods that allow it to serialize/unserialize to/from a
2022   buffer.
2023
2024   """
2025   def Dumps(self):
2026     """Dump this instance and return the string representation."""
2027     buf = StringIO()
2028     self.write(buf)
2029     return buf.getvalue()
2030
2031   @classmethod
2032   def Loads(cls, data):
2033     """Load data from a string."""
2034     buf = StringIO(data)
2035     cfp = cls()
2036     cfp.readfp(buf)
2037     return cfp