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