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