Statistics
| Branch: | Tag: | Revision:

root / lib / objects.py @ 376631d1

History | View | Annotate | Download (58.3 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Transportable objects for Ganeti.
23

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

27
"""
28

    
29
# pylint: disable=E0203,W0201,R0902
30

    
31
# E0203: Access to member %r before its definition, since we use
32
# objects.py which doesn't 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 time
42
from cStringIO import StringIO
43

    
44
from ganeti import errors
45
from ganeti import constants
46
from ganeti import netutils
47
from ganeti import objectutils
48
from ganeti import utils
49

    
50
from socket import AF_INET
51

    
52

    
53
__all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
54
           "OS", "Node", "NodeGroup", "Cluster", "FillDict", "Network"]
55

    
56
_TIMESTAMPS = ["ctime", "mtime"]
57
_UUID = ["uuid"]
58

    
59

    
60
def FillDict(defaults_dict, custom_dict, skip_keys=None):
61
  """Basic function to apply settings on top a default dict.
62

63
  @type defaults_dict: dict
64
  @param defaults_dict: dictionary holding the default values
65
  @type custom_dict: dict
66
  @param custom_dict: dictionary holding customized value
67
  @type skip_keys: list
68
  @param skip_keys: which keys not to fill
69
  @rtype: dict
70
  @return: dict with the 'full' values
71

72
  """
73
  ret_dict = copy.deepcopy(defaults_dict)
74
  ret_dict.update(custom_dict)
75
  if skip_keys:
76
    for k in skip_keys:
77
      try:
78
        del ret_dict[k]
79
      except KeyError:
80
        pass
81
  return ret_dict
82

    
83

    
84
def FillIPolicy(default_ipolicy, custom_ipolicy, skip_keys=None):
85
  """Fills an instance policy with defaults.
86

87
  """
88
  assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
89
  ret_dict = {}
90
  for key in constants.IPOLICY_ISPECS:
91
    ret_dict[key] = FillDict(default_ipolicy[key],
92
                             custom_ipolicy.get(key, {}),
93
                             skip_keys=skip_keys)
94
  # list items
95
  for key in [constants.IPOLICY_DTS]:
96
    ret_dict[key] = list(custom_ipolicy.get(key, default_ipolicy[key]))
97
  # other items which we know we can directly copy (immutables)
98
  for key in constants.IPOLICY_PARAMETERS:
99
    ret_dict[key] = custom_ipolicy.get(key, default_ipolicy[key])
100

    
101
  return ret_dict
102

    
103

    
104
def FillDiskParams(default_dparams, custom_dparams, skip_keys=None):
105
  """Fills the disk parameter defaults.
106

107
  @see: L{FillDict} for parameters and return value
108

109
  """
110
  assert frozenset(default_dparams.keys()) == constants.DISK_TEMPLATES
111

    
112
  return dict((dt, FillDict(default_dparams[dt], custom_dparams.get(dt, {}),
113
                             skip_keys=skip_keys))
114
              for dt in constants.DISK_TEMPLATES)
115

    
116

    
117
def UpgradeGroupedParams(target, defaults):
118
  """Update all groups for the target parameter.
119

120
  @type target: dict of dicts
121
  @param target: {group: {parameter: value}}
122
  @type defaults: dict
123
  @param defaults: default parameter values
124

125
  """
126
  if target is None:
127
    target = {constants.PP_DEFAULT: defaults}
128
  else:
129
    for group in target:
130
      target[group] = FillDict(defaults, target[group])
131
  return target
132

    
133

    
134
def UpgradeBeParams(target):
135
  """Update the be parameters dict to the new format.
136

137
  @type target: dict
138
  @param target: "be" parameters dict
139

140
  """
141
  if constants.BE_MEMORY in target:
142
    memory = target[constants.BE_MEMORY]
143
    target[constants.BE_MAXMEM] = memory
144
    target[constants.BE_MINMEM] = memory
145
    del target[constants.BE_MEMORY]
146

    
147

    
148
def UpgradeDiskParams(diskparams):
149
  """Upgrade the disk parameters.
150

151
  @type diskparams: dict
152
  @param diskparams: disk parameters to upgrade
153
  @rtype: dict
154
  @return: the upgraded disk parameters dict
155

156
  """
157
  if not diskparams:
158
    result = {}
159
  else:
160
    result = FillDiskParams(constants.DISK_DT_DEFAULTS, diskparams)
161

    
162
  return result
163

    
164

    
165
def UpgradeNDParams(ndparams):
166
  """Upgrade ndparams structure.
167

168
  @type ndparams: dict
169
  @param ndparams: disk parameters to upgrade
170
  @rtype: dict
171
  @return: the upgraded node parameters dict
172

173
  """
174
  if ndparams is None:
175
    ndparams = {}
176

    
177
  if (constants.ND_OOB_PROGRAM in ndparams and
178
      ndparams[constants.ND_OOB_PROGRAM] is None):
179
    # will be reset by the line below
180
    del ndparams[constants.ND_OOB_PROGRAM]
181
  return FillDict(constants.NDC_DEFAULTS, ndparams)
182

    
183

    
184
def MakeEmptyIPolicy():
185
  """Create empty IPolicy dictionary.
186

187
  """
188
  return dict([
189
    (constants.ISPECS_MIN, {}),
190
    (constants.ISPECS_MAX, {}),
191
    (constants.ISPECS_STD, {}),
192
    ])
193

    
194

    
195
class ConfigObject(objectutils.ValidatedSlots):
196
  """A generic config object.
197

198
  It has the following properties:
199

200
    - provides somewhat safe recursive unpickling and pickling for its classes
201
    - unset attributes which are defined in slots are always returned
202
      as None instead of raising an error
203

204
  Classes derived from this must always declare __slots__ (we use many
205
  config objects and the memory reduction is useful)
206

207
  """
208
  __slots__ = []
209

    
210
  def __getattr__(self, name):
211
    if name not in self.GetAllSlots():
212
      raise AttributeError("Invalid object attribute %s.%s" %
213
                           (type(self).__name__, name))
214
    return None
215

    
216
  def __setstate__(self, state):
217
    slots = self.GetAllSlots()
218
    for name in state:
219
      if name in slots:
220
        setattr(self, name, state[name])
221

    
222
  def Validate(self):
223
    """Validates the slots.
224

225
    """
226

    
227
  def ToDict(self):
228
    """Convert to a dict holding only standard python types.
229

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

236
    """
237
    result = {}
238
    for name in self.GetAllSlots():
239
      value = getattr(self, name, None)
240
      if value is not None:
241
        result[name] = value
242
    return result
243

    
244
  __getstate__ = ToDict
245

    
246
  @classmethod
247
  def FromDict(cls, val):
248
    """Create an object from a dictionary.
249

250
    This generic routine takes a dict, instantiates a new instance of
251
    the given class, and sets attributes based on the dict content.
252

253
    As for `ToDict`, this does not work if the class has children
254
    who are ConfigObjects themselves (e.g. the nics list in an
255
    Instance), in which case the object should subclass the function
256
    and alter the objects.
257

258
    """
259
    if not isinstance(val, dict):
260
      raise errors.ConfigurationError("Invalid object passed to FromDict:"
261
                                      " expected dict, got %s" % type(val))
262
    val_str = dict([(str(k), v) for k, v in val.iteritems()])
263
    obj = cls(**val_str) # pylint: disable=W0142
264
    return obj
265

    
266
  @staticmethod
267
  def _ContainerToDicts(container):
268
    """Convert the elements of a container to standard python types.
269

270
    This method converts a container with elements derived from
271
    ConfigData to standard python types. If the container is a dict,
272
    we don't touch the keys, only the values.
273

274
    """
275
    if isinstance(container, dict):
276
      ret = dict([(k, v.ToDict()) for k, v in container.iteritems()])
277
    elif isinstance(container, (list, tuple, set, frozenset)):
278
      ret = [elem.ToDict() for elem in container]
279
    else:
280
      raise TypeError("Invalid type %s passed to _ContainerToDicts" %
281
                      type(container))
282
    return ret
283

    
284
  @staticmethod
285
  def _ContainerFromDicts(source, c_type, e_type):
286
    """Convert a container from standard python types.
287

288
    This method converts a container with standard python types to
289
    ConfigData objects. If the container is a dict, we don't touch the
290
    keys, only the values.
291

292
    """
293
    if not isinstance(c_type, type):
294
      raise TypeError("Container type %s passed to _ContainerFromDicts is"
295
                      " not a type" % type(c_type))
296
    if source is None:
297
      source = c_type()
298
    if c_type is dict:
299
      ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()])
300
    elif c_type in (list, tuple, set, frozenset):
301
      ret = c_type([e_type.FromDict(elem) for elem in source])
302
    else:
303
      raise TypeError("Invalid container type %s passed to"
304
                      " _ContainerFromDicts" % c_type)
305
    return ret
306

    
307
  def Copy(self):
308
    """Makes a deep copy of the current object and its children.
309

310
    """
311
    dict_form = self.ToDict()
312
    clone_obj = self.__class__.FromDict(dict_form)
313
    return clone_obj
314

    
315
  def __repr__(self):
316
    """Implement __repr__ for ConfigObjects."""
317
    return repr(self.ToDict())
318

    
319
  def UpgradeConfig(self):
320
    """Fill defaults for missing configuration values.
321

322
    This method will be called at configuration load time, and its
323
    implementation will be object dependent.
324

325
    """
326
    pass
327

    
328

    
329
class TaggableObject(ConfigObject):
330
  """An generic class supporting tags.
331

332
  """
333
  __slots__ = ["tags"]
334
  VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
335

    
336
  @classmethod
337
  def ValidateTag(cls, tag):
338
    """Check if a tag is valid.
339

340
    If the tag is invalid, an errors.TagError will be raised. The
341
    function has no return value.
342

343
    """
344
    if not isinstance(tag, basestring):
345
      raise errors.TagError("Invalid tag type (not a string)")
346
    if len(tag) > constants.MAX_TAG_LEN:
347
      raise errors.TagError("Tag too long (>%d characters)" %
348
                            constants.MAX_TAG_LEN)
349
    if not tag:
350
      raise errors.TagError("Tags cannot be empty")
351
    if not cls.VALID_TAG_RE.match(tag):
352
      raise errors.TagError("Tag contains invalid characters")
353

    
354
  def GetTags(self):
355
    """Return the tags list.
356

357
    """
358
    tags = getattr(self, "tags", None)
359
    if tags is None:
360
      tags = self.tags = set()
361
    return tags
362

    
363
  def AddTag(self, tag):
364
    """Add a new tag.
365

366
    """
367
    self.ValidateTag(tag)
368
    tags = self.GetTags()
369
    if len(tags) >= constants.MAX_TAGS_PER_OBJ:
370
      raise errors.TagError("Too many tags")
371
    self.GetTags().add(tag)
372

    
373
  def RemoveTag(self, tag):
374
    """Remove a tag.
375

376
    """
377
    self.ValidateTag(tag)
378
    tags = self.GetTags()
379
    try:
380
      tags.remove(tag)
381
    except KeyError:
382
      raise errors.TagError("Tag not found")
383

    
384
  def ToDict(self):
385
    """Taggable-object-specific conversion to standard python types.
386

387
    This replaces the tags set with a list.
388

389
    """
390
    bo = super(TaggableObject, self).ToDict()
391

    
392
    tags = bo.get("tags", None)
393
    if isinstance(tags, set):
394
      bo["tags"] = list(tags)
395
    return bo
396

    
397
  @classmethod
398
  def FromDict(cls, val):
399
    """Custom function for instances.
400

401
    """
402
    obj = super(TaggableObject, cls).FromDict(val)
403
    if hasattr(obj, "tags") and isinstance(obj.tags, list):
404
      obj.tags = set(obj.tags)
405
    return obj
406

    
407

    
408
class MasterNetworkParameters(ConfigObject):
409
  """Network configuration parameters for the master
410

411
  @ivar name: master name
412
  @ivar ip: master IP
413
  @ivar netmask: master netmask
414
  @ivar netdev: master network device
415
  @ivar ip_family: master IP family
416

417
  """
418
  __slots__ = [
419
    "name",
420
    "ip",
421
    "netmask",
422
    "netdev",
423
    "ip_family",
424
    ]
425

    
426

    
427
class ConfigData(ConfigObject):
428
  """Top-level config object."""
429
  __slots__ = [
430
    "version",
431
    "cluster",
432
    "nodes",
433
    "nodegroups",
434
    "instances",
435
    "networks",
436
    "serial_no",
437
    ] + _TIMESTAMPS
438

    
439
  def ToDict(self):
440
    """Custom function for top-level config data.
441

442
    This just replaces the list of instances, nodes and the cluster
443
    with standard python types.
444

445
    """
446
    mydict = super(ConfigData, self).ToDict()
447
    mydict["cluster"] = mydict["cluster"].ToDict()
448
    for key in "nodes", "instances", "nodegroups", "networks":
449
      mydict[key] = self._ContainerToDicts(mydict[key])
450

    
451
    return mydict
452

    
453
  @classmethod
454
  def FromDict(cls, val):
455
    """Custom function for top-level config data
456

457
    """
458
    obj = super(ConfigData, cls).FromDict(val)
459
    obj.cluster = Cluster.FromDict(obj.cluster)
460
    obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
461
    obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
462
    obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
463
    obj.networks = cls._ContainerFromDicts(obj.networks, dict, Network)
464
    return obj
465

    
466
  def HasAnyDiskOfType(self, dev_type):
467
    """Check if in there is at disk of the given type in the configuration.
468

469
    @type dev_type: L{constants.LDS_BLOCK}
470
    @param dev_type: the type to look for
471
    @rtype: boolean
472
    @return: boolean indicating if a disk of the given type was found or not
473

474
    """
475
    for instance in self.instances.values():
476
      for disk in instance.disks:
477
        if disk.IsBasedOnDiskType(dev_type):
478
          return True
479
    return False
480

    
481
  def UpgradeConfig(self):
482
    """Fill defaults for missing configuration values.
483

484
    """
485
    self.cluster.UpgradeConfig()
486
    for node in self.nodes.values():
487
      node.UpgradeConfig()
488
    for instance in self.instances.values():
489
      instance.UpgradeConfig()
490
    if self.nodegroups is None:
491
      self.nodegroups = {}
492
    for nodegroup in self.nodegroups.values():
493
      nodegroup.UpgradeConfig()
494
    if self.cluster.drbd_usermode_helper is None:
495
      # To decide if we set an helper let's check if at least one instance has
496
      # a DRBD disk. This does not cover all the possible scenarios but it
497
      # gives a good approximation.
498
      if self.HasAnyDiskOfType(constants.LD_DRBD8):
499
        self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
500
    if self.networks is None:
501
      self.networks = {}
502

    
503

    
504
class NIC(ConfigObject):
505
  """Config object representing a network card."""
506
  __slots__ = ["mac", "ip", "network", "nicparams", "netinfo"]
507

    
508
  @classmethod
509
  def CheckParameterSyntax(cls, nicparams):
510
    """Check the given parameters for validity.
511

512
    @type nicparams:  dict
513
    @param nicparams: dictionary with parameter names/value
514
    @raise errors.ConfigurationError: when a parameter is not valid
515

516
    """
517
    if (nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES and
518
        nicparams[constants.NIC_MODE] != constants.VALUE_AUTO):
519
      err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
520
      raise errors.ConfigurationError(err)
521

    
522
    if (nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED and
523
        not nicparams[constants.NIC_LINK]):
524
      err = "Missing bridged nic link"
525
      raise errors.ConfigurationError(err)
526

    
527

    
528
class Disk(ConfigObject):
529
  """Config object representing a block device."""
530
  __slots__ = ["dev_type", "logical_id", "physical_id",
531
               "children", "iv_name", "size", "mode", "params"]
532

    
533
  def CreateOnSecondary(self):
534
    """Test if this device needs to be created on a secondary node."""
535
    return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
536

    
537
  def AssembleOnSecondary(self):
538
    """Test if this device needs to be assembled on a secondary node."""
539
    return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
540

    
541
  def OpenOnSecondary(self):
542
    """Test if this device needs to be opened on a secondary node."""
543
    return self.dev_type in (constants.LD_LV,)
544

    
545
  def StaticDevPath(self):
546
    """Return the device path if this device type has a static one.
547

548
    Some devices (LVM for example) live always at the same /dev/ path,
549
    irrespective of their status. For such devices, we return this
550
    path, for others we return None.
551

552
    @warning: The path returned is not a normalized pathname; callers
553
        should check that it is a valid path.
554

555
    """
556
    if self.dev_type == constants.LD_LV:
557
      return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
558
    elif self.dev_type == constants.LD_BLOCKDEV:
559
      return self.logical_id[1]
560
    elif self.dev_type == constants.LD_RBD:
561
      return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
562
    return None
563

    
564
  def ChildrenNeeded(self):
565
    """Compute the needed number of children for activation.
566

567
    This method will return either -1 (all children) or a positive
568
    number denoting the minimum number of children needed for
569
    activation (only mirrored devices will usually return >=0).
570

571
    Currently, only DRBD8 supports diskless activation (therefore we
572
    return 0), for all other we keep the previous semantics and return
573
    -1.
574

575
    """
576
    if self.dev_type == constants.LD_DRBD8:
577
      return 0
578
    return -1
579

    
580
  def IsBasedOnDiskType(self, dev_type):
581
    """Check if the disk or its children are based on the given type.
582

583
    @type dev_type: L{constants.LDS_BLOCK}
584
    @param dev_type: the type to look for
585
    @rtype: boolean
586
    @return: boolean indicating if a device of the given type was found or not
587

588
    """
589
    if self.children:
590
      for child in self.children:
591
        if child.IsBasedOnDiskType(dev_type):
592
          return True
593
    return self.dev_type == dev_type
594

    
595
  def GetNodes(self, node):
596
    """This function returns the nodes this device lives on.
597

598
    Given the node on which the parent of the device lives on (or, in
599
    case of a top-level device, the primary node of the devices'
600
    instance), this function will return a list of nodes on which this
601
    devices needs to (or can) be assembled.
602

603
    """
604
    if self.dev_type in [constants.LD_LV, constants.LD_FILE,
605
                         constants.LD_BLOCKDEV, constants.LD_RBD,
606
                         constants.LD_EXT]:
607
      result = [node]
608
    elif self.dev_type in constants.LDS_DRBD:
609
      result = [self.logical_id[0], self.logical_id[1]]
610
      if node not in result:
611
        raise errors.ConfigurationError("DRBD device passed unknown node")
612
    else:
613
      raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
614
    return result
615

    
616
  def ComputeNodeTree(self, parent_node):
617
    """Compute the node/disk tree for this disk and its children.
618

619
    This method, given the node on which the parent disk lives, will
620
    return the list of all (node, disk) pairs which describe the disk
621
    tree in the most compact way. For example, a drbd/lvm stack
622
    will be returned as (primary_node, drbd) and (secondary_node, drbd)
623
    which represents all the top-level devices on the nodes.
624

625
    """
626
    my_nodes = self.GetNodes(parent_node)
627
    result = [(node, self) for node in my_nodes]
628
    if not self.children:
629
      # leaf device
630
      return result
631
    for node in my_nodes:
632
      for child in self.children:
633
        child_result = child.ComputeNodeTree(node)
634
        if len(child_result) == 1:
635
          # child (and all its descendants) is simple, doesn't split
636
          # over multiple hosts, so we don't need to describe it, our
637
          # own entry for this node describes it completely
638
          continue
639
        else:
640
          # check if child nodes differ from my nodes; note that
641
          # subdisk can differ from the child itself, and be instead
642
          # one of its descendants
643
          for subnode, subdisk in child_result:
644
            if subnode not in my_nodes:
645
              result.append((subnode, subdisk))
646
            # otherwise child is under our own node, so we ignore this
647
            # entry (but probably the other results in the list will
648
            # be different)
649
    return result
650

    
651
  def ComputeGrowth(self, amount):
652
    """Compute the per-VG growth requirements.
653

654
    This only works for VG-based disks.
655

656
    @type amount: integer
657
    @param amount: the desired increase in (user-visible) disk space
658
    @rtype: dict
659
    @return: a dictionary of volume-groups and the required size
660

661
    """
662
    if self.dev_type == constants.LD_LV:
663
      return {self.logical_id[0]: amount}
664
    elif self.dev_type == constants.LD_DRBD8:
665
      if self.children:
666
        return self.children[0].ComputeGrowth(amount)
667
      else:
668
        return {}
669
    else:
670
      # Other disk types do not require VG space
671
      return {}
672

    
673
  def RecordGrow(self, amount):
674
    """Update the size of this disk after growth.
675

676
    This method recurses over the disks's children and updates their
677
    size correspondigly. The method needs to be kept in sync with the
678
    actual algorithms from bdev.
679

680
    """
681
    if self.dev_type in (constants.LD_LV, constants.LD_FILE,
682
                         constants.LD_RBD, constants.LD_EXT):
683
      self.size += amount
684
    elif self.dev_type == constants.LD_DRBD8:
685
      if self.children:
686
        self.children[0].RecordGrow(amount)
687
      self.size += amount
688
    else:
689
      raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
690
                                   " disk type %s" % self.dev_type)
691

    
692
  def Update(self, size=None, mode=None):
693
    """Apply changes to size and mode.
694

695
    """
696
    if self.dev_type == constants.LD_DRBD8:
697
      if self.children:
698
        self.children[0].Update(size=size, mode=mode)
699
    else:
700
      assert not self.children
701

    
702
    if size is not None:
703
      self.size = size
704
    if mode is not None:
705
      self.mode = mode
706

    
707
  def UnsetSize(self):
708
    """Sets recursively the size to zero for the disk and its children.
709

710
    """
711
    if self.children:
712
      for child in self.children:
713
        child.UnsetSize()
714
    self.size = 0
715

    
716
  def SetPhysicalID(self, target_node, nodes_ip):
717
    """Convert the logical ID to the physical ID.
718

719
    This is used only for drbd, which needs ip/port configuration.
720

721
    The routine descends down and updates its children also, because
722
    this helps when the only the top device is passed to the remote
723
    node.
724

725
    Arguments:
726
      - target_node: the node we wish to configure for
727
      - nodes_ip: a mapping of node name to ip
728

729
    The target_node must exist in in nodes_ip, and must be one of the
730
    nodes in the logical ID for each of the DRBD devices encountered
731
    in the disk tree.
732

733
    """
734
    if self.children:
735
      for child in self.children:
736
        child.SetPhysicalID(target_node, nodes_ip)
737

    
738
    if self.logical_id is None and self.physical_id is not None:
739
      return
740
    if self.dev_type in constants.LDS_DRBD:
741
      pnode, snode, port, pminor, sminor, secret = self.logical_id
742
      if target_node not in (pnode, snode):
743
        raise errors.ConfigurationError("DRBD device not knowing node %s" %
744
                                        target_node)
745
      pnode_ip = nodes_ip.get(pnode, None)
746
      snode_ip = nodes_ip.get(snode, None)
747
      if pnode_ip is None or snode_ip is None:
748
        raise errors.ConfigurationError("Can't find primary or secondary node"
749
                                        " for %s" % str(self))
750
      p_data = (pnode_ip, port)
751
      s_data = (snode_ip, port)
752
      if pnode == target_node:
753
        self.physical_id = p_data + s_data + (pminor, secret)
754
      else: # it must be secondary, we tested above
755
        self.physical_id = s_data + p_data + (sminor, secret)
756
    else:
757
      self.physical_id = self.logical_id
758
    return
759

    
760
  def ToDict(self):
761
    """Disk-specific conversion to standard python types.
762

763
    This replaces the children lists of objects with lists of
764
    standard python types.
765

766
    """
767
    bo = super(Disk, self).ToDict()
768

    
769
    for attr in ("children",):
770
      alist = bo.get(attr, None)
771
      if alist:
772
        bo[attr] = self._ContainerToDicts(alist)
773
    return bo
774

    
775
  @classmethod
776
  def FromDict(cls, val):
777
    """Custom function for Disks
778

779
    """
780
    obj = super(Disk, cls).FromDict(val)
781
    if obj.children:
782
      obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
783
    if obj.logical_id and isinstance(obj.logical_id, list):
784
      obj.logical_id = tuple(obj.logical_id)
785
    if obj.physical_id and isinstance(obj.physical_id, list):
786
      obj.physical_id = tuple(obj.physical_id)
787
    if obj.dev_type in constants.LDS_DRBD:
788
      # we need a tuple of length six here
789
      if len(obj.logical_id) < 6:
790
        obj.logical_id += (None,) * (6 - len(obj.logical_id))
791
    return obj
792

    
793
  def __str__(self):
794
    """Custom str() formatter for disks.
795

796
    """
797
    if self.dev_type == constants.LD_LV:
798
      val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
799
    elif self.dev_type in constants.LDS_DRBD:
800
      node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
801
      val = "<DRBD8("
802
      if self.physical_id is None:
803
        phy = "unconfigured"
804
      else:
805
        phy = ("configured as %s:%s %s:%s" %
806
               (self.physical_id[0], self.physical_id[1],
807
                self.physical_id[2], self.physical_id[3]))
808

    
809
      val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
810
              (node_a, minor_a, node_b, minor_b, port, phy))
811
      if self.children and self.children.count(None) == 0:
812
        val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
813
      else:
814
        val += "no local storage"
815
    else:
816
      val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
817
             (self.dev_type, self.logical_id, self.physical_id, self.children))
818
    if self.iv_name is None:
819
      val += ", not visible"
820
    else:
821
      val += ", visible as /dev/%s" % self.iv_name
822
    if isinstance(self.size, int):
823
      val += ", size=%dm)>" % self.size
824
    else:
825
      val += ", size='%s')>" % (self.size,)
826
    return val
827

    
828
  def Verify(self):
829
    """Checks that this disk is correctly configured.
830

831
    """
832
    all_errors = []
833
    if self.mode not in constants.DISK_ACCESS_SET:
834
      all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
835
    return all_errors
836

    
837
  def UpgradeConfig(self):
838
    """Fill defaults for missing configuration values.
839

840
    """
841
    if self.children:
842
      for child in self.children:
843
        child.UpgradeConfig()
844

    
845
    # FIXME: Make this configurable in Ganeti 2.7
846
    self.params = {}
847
    # add here config upgrade for this disk
848

    
849
  @staticmethod
850
  def ComputeLDParams(disk_template, disk_params):
851
    """Computes Logical Disk parameters from Disk Template parameters.
852

853
    @type disk_template: string
854
    @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
855
    @type disk_params: dict
856
    @param disk_params: disk template parameters;
857
                        dict(template_name -> parameters
858
    @rtype: list(dict)
859
    @return: a list of dicts, one for each node of the disk hierarchy. Each dict
860
      contains the LD parameters of the node. The tree is flattened in-order.
861

862
    """
863
    if disk_template not in constants.DISK_TEMPLATES:
864
      raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
865

    
866
    assert disk_template in disk_params
867

    
868
    result = list()
869
    dt_params = disk_params[disk_template]
870
    if disk_template == constants.DT_DRBD8:
871
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_DRBD8], {
872
        constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
873
        constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
874
        constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
875
        constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
876
        constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
877
        constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
878
        constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
879
        constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
880
        constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
881
        constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
882
        constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
883
        constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
884
        }))
885

    
886
      # data LV
887
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
888
        constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
889
        }))
890

    
891
      # metadata LV
892
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
893
        constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
894
        }))
895

    
896
    elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
897
      result.append(constants.DISK_LD_DEFAULTS[constants.LD_FILE])
898

    
899
    elif disk_template == constants.DT_PLAIN:
900
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
901
        constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
902
        }))
903

    
904
    elif disk_template == constants.DT_BLOCK:
905
      result.append(constants.DISK_LD_DEFAULTS[constants.LD_BLOCKDEV])
906

    
907
    elif disk_template == constants.DT_RBD:
908
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_RBD], {
909
        constants.LDP_POOL: dt_params[constants.RBD_POOL],
910
        }))
911

    
912
    return result
913

    
914

    
915
class InstancePolicy(ConfigObject):
916
  """Config object representing instance policy limits dictionary.
917

918

919
  Note that this object is not actually used in the config, it's just
920
  used as a placeholder for a few functions.
921

922
  """
923
  @classmethod
924
  def CheckParameterSyntax(cls, ipolicy, check_std):
925
    """ Check the instance policy for validity.
926

927
    """
928
    for param in constants.ISPECS_PARAMETERS:
929
      InstancePolicy.CheckISpecSyntax(ipolicy, param, check_std)
930
    if constants.IPOLICY_DTS in ipolicy:
931
      InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
932
    for key in constants.IPOLICY_PARAMETERS:
933
      if key in ipolicy:
934
        InstancePolicy.CheckParameter(key, ipolicy[key])
935
    wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
936
    if wrong_keys:
937
      raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
938
                                      utils.CommaJoin(wrong_keys))
939

    
940
  @classmethod
941
  def CheckISpecSyntax(cls, ipolicy, name, check_std):
942
    """Check the instance policy for validity on a given key.
943

944
    We check if the instance policy makes sense for a given key, that is
945
    if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
946

947
    @type ipolicy: dict
948
    @param ipolicy: dictionary with min, max, std specs
949
    @type name: string
950
    @param name: what are the limits for
951
    @type check_std: bool
952
    @param check_std: Whether to check std value or just assume compliance
953
    @raise errors.ConfigureError: when specs for given name are not valid
954

955
    """
956
    min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
957

    
958
    if check_std:
959
      std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
960
      std_msg = std_v
961
    else:
962
      std_v = min_v
963
      std_msg = "-"
964

    
965
    max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
966
    err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
967
           (name,
968
            ipolicy[constants.ISPECS_MIN].get(name, "-"),
969
            ipolicy[constants.ISPECS_MAX].get(name, "-"),
970
            std_msg))
971
    if min_v > std_v or std_v > max_v:
972
      raise errors.ConfigurationError(err)
973

    
974
  @classmethod
975
  def CheckDiskTemplates(cls, disk_templates):
976
    """Checks the disk templates for validity.
977

978
    """
979
    wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
980
    if wrong:
981
      raise errors.ConfigurationError("Invalid disk template(s) %s" %
982
                                      utils.CommaJoin(wrong))
983

    
984
  @classmethod
985
  def CheckParameter(cls, key, value):
986
    """Checks a parameter.
987

988
    Currently we expect all parameters to be float values.
989

990
    """
991
    try:
992
      float(value)
993
    except (TypeError, ValueError), err:
994
      raise errors.ConfigurationError("Invalid value for key" " '%s':"
995
                                      " '%s', error: %s" % (key, value, err))
996

    
997

    
998
class Instance(TaggableObject):
999
  """Config object representing an instance."""
1000
  __slots__ = [
1001
    "name",
1002
    "primary_node",
1003
    "os",
1004
    "hypervisor",
1005
    "hvparams",
1006
    "beparams",
1007
    "osparams",
1008
    "admin_state",
1009
    "nics",
1010
    "disks",
1011
    "disk_template",
1012
    "network_port",
1013
    "serial_no",
1014
    ] + _TIMESTAMPS + _UUID
1015

    
1016
  def _ComputeSecondaryNodes(self):
1017
    """Compute the list of secondary nodes.
1018

1019
    This is a simple wrapper over _ComputeAllNodes.
1020

1021
    """
1022
    all_nodes = set(self._ComputeAllNodes())
1023
    all_nodes.discard(self.primary_node)
1024
    return tuple(all_nodes)
1025

    
1026
  secondary_nodes = property(_ComputeSecondaryNodes, None, None,
1027
                             "List of names of secondary nodes")
1028

    
1029
  def _ComputeAllNodes(self):
1030
    """Compute the list of all nodes.
1031

1032
    Since the data is already there (in the drbd disks), keeping it as
1033
    a separate normal attribute is redundant and if not properly
1034
    synchronised can cause problems. Thus it's better to compute it
1035
    dynamically.
1036

1037
    """
1038
    def _Helper(nodes, device):
1039
      """Recursively computes nodes given a top device."""
1040
      if device.dev_type in constants.LDS_DRBD:
1041
        nodea, nodeb = device.logical_id[:2]
1042
        nodes.add(nodea)
1043
        nodes.add(nodeb)
1044
      if device.children:
1045
        for child in device.children:
1046
          _Helper(nodes, child)
1047

    
1048
    all_nodes = set()
1049
    all_nodes.add(self.primary_node)
1050
    for device in self.disks:
1051
      _Helper(all_nodes, device)
1052
    return tuple(all_nodes)
1053

    
1054
  all_nodes = property(_ComputeAllNodes, None, None,
1055
                       "List of names of all the nodes of the instance")
1056

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

1060
    This function figures out what logical volumes should belong on
1061
    which nodes, recursing through a device tree.
1062

1063
    @param lvmap: optional dictionary to receive the
1064
        'node' : ['lv', ...] data.
1065

1066
    @return: None if lvmap arg is given, otherwise, a dictionary of
1067
        the form { 'nodename' : ['volume1', 'volume2', ...], ... };
1068
        volumeN is of the form "vg_name/lv_name", compatible with
1069
        GetVolumeList()
1070

1071
    """
1072
    if node is None:
1073
      node = self.primary_node
1074

    
1075
    if lvmap is None:
1076
      lvmap = {
1077
        node: [],
1078
        }
1079
      ret = lvmap
1080
    else:
1081
      if not node in lvmap:
1082
        lvmap[node] = []
1083
      ret = None
1084

    
1085
    if not devs:
1086
      devs = self.disks
1087

    
1088
    for dev in devs:
1089
      if dev.dev_type == constants.LD_LV:
1090
        lvmap[node].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1091

    
1092
      elif dev.dev_type in constants.LDS_DRBD:
1093
        if dev.children:
1094
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1095
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1096

    
1097
      elif dev.children:
1098
        self.MapLVsByNode(lvmap, dev.children, node)
1099

    
1100
    return ret
1101

    
1102
  def FindDisk(self, idx):
1103
    """Find a disk given having a specified index.
1104

1105
    This is just a wrapper that does validation of the index.
1106

1107
    @type idx: int
1108
    @param idx: the disk index
1109
    @rtype: L{Disk}
1110
    @return: the corresponding disk
1111
    @raise errors.OpPrereqError: when the given index is not valid
1112

1113
    """
1114
    try:
1115
      idx = int(idx)
1116
      return self.disks[idx]
1117
    except (TypeError, ValueError), err:
1118
      raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1119
                                 errors.ECODE_INVAL)
1120
    except IndexError:
1121
      raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1122
                                 " 0 to %d" % (idx, len(self.disks) - 1),
1123
                                 errors.ECODE_INVAL)
1124

    
1125
  def ToDict(self):
1126
    """Instance-specific conversion to standard python types.
1127

1128
    This replaces the children lists of objects with lists of standard
1129
    python types.
1130

1131
    """
1132
    bo = super(Instance, self).ToDict()
1133

    
1134
    for attr in "nics", "disks":
1135
      alist = bo.get(attr, None)
1136
      if alist:
1137
        nlist = self._ContainerToDicts(alist)
1138
      else:
1139
        nlist = []
1140
      bo[attr] = nlist
1141
    return bo
1142

    
1143
  @classmethod
1144
  def FromDict(cls, val):
1145
    """Custom function for instances.
1146

1147
    """
1148
    if "admin_state" not in val:
1149
      if val.get("admin_up", False):
1150
        val["admin_state"] = constants.ADMINST_UP
1151
      else:
1152
        val["admin_state"] = constants.ADMINST_DOWN
1153
    if "admin_up" in val:
1154
      del val["admin_up"]
1155
    obj = super(Instance, cls).FromDict(val)
1156
    obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
1157
    obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
1158
    return obj
1159

    
1160
  def UpgradeConfig(self):
1161
    """Fill defaults for missing configuration values.
1162

1163
    """
1164
    for nic in self.nics:
1165
      nic.UpgradeConfig()
1166
    for disk in self.disks:
1167
      disk.UpgradeConfig()
1168
    if self.hvparams:
1169
      for key in constants.HVC_GLOBALS:
1170
        try:
1171
          del self.hvparams[key]
1172
        except KeyError:
1173
          pass
1174
    if self.osparams is None:
1175
      self.osparams = {}
1176
    UpgradeBeParams(self.beparams)
1177

    
1178

    
1179
class OS(ConfigObject):
1180
  """Config object representing an operating system.
1181

1182
  @type supported_parameters: list
1183
  @ivar supported_parameters: a list of tuples, name and description,
1184
      containing the supported parameters by this OS
1185

1186
  @type VARIANT_DELIM: string
1187
  @cvar VARIANT_DELIM: the variant delimiter
1188

1189
  """
1190
  __slots__ = [
1191
    "name",
1192
    "path",
1193
    "api_versions",
1194
    "create_script",
1195
    "export_script",
1196
    "import_script",
1197
    "rename_script",
1198
    "verify_script",
1199
    "supported_variants",
1200
    "supported_parameters",
1201
    ]
1202

    
1203
  VARIANT_DELIM = "+"
1204

    
1205
  @classmethod
1206
  def SplitNameVariant(cls, name):
1207
    """Splits the name into the proper name and variant.
1208

1209
    @param name: the OS (unprocessed) name
1210
    @rtype: list
1211
    @return: a list of two elements; if the original name didn't
1212
        contain a variant, it's returned as an empty string
1213

1214
    """
1215
    nv = name.split(cls.VARIANT_DELIM, 1)
1216
    if len(nv) == 1:
1217
      nv.append("")
1218
    return nv
1219

    
1220
  @classmethod
1221
  def GetName(cls, name):
1222
    """Returns the proper name of the os (without the variant).
1223

1224
    @param name: the OS (unprocessed) name
1225

1226
    """
1227
    return cls.SplitNameVariant(name)[0]
1228

    
1229
  @classmethod
1230
  def GetVariant(cls, name):
1231
    """Returns the variant the os (without the base name).
1232

1233
    @param name: the OS (unprocessed) name
1234

1235
    """
1236
    return cls.SplitNameVariant(name)[1]
1237

    
1238

    
1239
class ExtStorage(ConfigObject):
1240
  """Config object representing an External Storage Provider.
1241

1242
  """
1243
  __slots__ = [
1244
    "name",
1245
    "path",
1246
    "create_script",
1247
    "remove_script",
1248
    "grow_script",
1249
    "attach_script",
1250
    "detach_script",
1251
    "setinfo_script",
1252
    ]
1253

    
1254

    
1255
class NodeHvState(ConfigObject):
1256
  """Hypvervisor state on a node.
1257

1258
  @ivar mem_total: Total amount of memory
1259
  @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1260
    available)
1261
  @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1262
    rounding
1263
  @ivar mem_inst: Memory used by instances living on node
1264
  @ivar cpu_total: Total node CPU core count
1265
  @ivar cpu_node: Number of CPU cores reserved for the node itself
1266

1267
  """
1268
  __slots__ = [
1269
    "mem_total",
1270
    "mem_node",
1271
    "mem_hv",
1272
    "mem_inst",
1273
    "cpu_total",
1274
    "cpu_node",
1275
    ] + _TIMESTAMPS
1276

    
1277

    
1278
class NodeDiskState(ConfigObject):
1279
  """Disk state on a node.
1280

1281
  """
1282
  __slots__ = [
1283
    "total",
1284
    "reserved",
1285
    "overhead",
1286
    ] + _TIMESTAMPS
1287

    
1288

    
1289
class Node(TaggableObject):
1290
  """Config object representing a node.
1291

1292
  @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1293
  @ivar hv_state_static: Hypervisor state overriden by user
1294
  @ivar disk_state: Disk state (e.g. free space)
1295
  @ivar disk_state_static: Disk state overriden by user
1296

1297
  """
1298
  __slots__ = [
1299
    "name",
1300
    "primary_ip",
1301
    "secondary_ip",
1302
    "serial_no",
1303
    "master_candidate",
1304
    "offline",
1305
    "drained",
1306
    "group",
1307
    "master_capable",
1308
    "vm_capable",
1309
    "ndparams",
1310
    "powered",
1311
    "hv_state",
1312
    "hv_state_static",
1313
    "disk_state",
1314
    "disk_state_static",
1315
    ] + _TIMESTAMPS + _UUID
1316

    
1317
  def UpgradeConfig(self):
1318
    """Fill defaults for missing configuration values.
1319

1320
    """
1321
    # pylint: disable=E0203
1322
    # because these are "defined" via slots, not manually
1323
    if self.master_capable is None:
1324
      self.master_capable = True
1325

    
1326
    if self.vm_capable is None:
1327
      self.vm_capable = True
1328

    
1329
    if self.ndparams is None:
1330
      self.ndparams = {}
1331

    
1332
    if self.powered is None:
1333
      self.powered = True
1334

    
1335
  def ToDict(self):
1336
    """Custom function for serializing.
1337

1338
    """
1339
    data = super(Node, self).ToDict()
1340

    
1341
    hv_state = data.get("hv_state", None)
1342
    if hv_state is not None:
1343
      data["hv_state"] = self._ContainerToDicts(hv_state)
1344

    
1345
    disk_state = data.get("disk_state", None)
1346
    if disk_state is not None:
1347
      data["disk_state"] = \
1348
        dict((key, self._ContainerToDicts(value))
1349
             for (key, value) in disk_state.items())
1350

    
1351
    return data
1352

    
1353
  @classmethod
1354
  def FromDict(cls, val):
1355
    """Custom function for deserializing.
1356

1357
    """
1358
    obj = super(Node, cls).FromDict(val)
1359

    
1360
    if obj.hv_state is not None:
1361
      obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1362

    
1363
    if obj.disk_state is not None:
1364
      obj.disk_state = \
1365
        dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
1366
             for (key, value) in obj.disk_state.items())
1367

    
1368
    return obj
1369

    
1370

    
1371
class NodeGroup(TaggableObject):
1372
  """Config object representing a node group."""
1373
  __slots__ = [
1374
    "name",
1375
    "members",
1376
    "ndparams",
1377
    "diskparams",
1378
    "ipolicy",
1379
    "serial_no",
1380
    "hv_state_static",
1381
    "disk_state_static",
1382
    "alloc_policy",
1383
    "networks",
1384
    ] + _TIMESTAMPS + _UUID
1385

    
1386
  def ToDict(self):
1387
    """Custom function for nodegroup.
1388

1389
    This discards the members object, which gets recalculated and is only kept
1390
    in memory.
1391

1392
    """
1393
    mydict = super(NodeGroup, self).ToDict()
1394
    del mydict["members"]
1395
    return mydict
1396

    
1397
  @classmethod
1398
  def FromDict(cls, val):
1399
    """Custom function for nodegroup.
1400

1401
    The members slot is initialized to an empty list, upon deserialization.
1402

1403
    """
1404
    obj = super(NodeGroup, cls).FromDict(val)
1405
    obj.members = []
1406
    return obj
1407

    
1408
  def UpgradeConfig(self):
1409
    """Fill defaults for missing configuration values.
1410

1411
    """
1412
    if self.ndparams is None:
1413
      self.ndparams = {}
1414

    
1415
    if self.serial_no is None:
1416
      self.serial_no = 1
1417

    
1418
    if self.alloc_policy is None:
1419
      self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1420

    
1421
    # We only update mtime, and not ctime, since we would not be able
1422
    # to provide a correct value for creation time.
1423
    if self.mtime is None:
1424
      self.mtime = time.time()
1425

    
1426
    if self.diskparams is None:
1427
      self.diskparams = {}
1428
    if self.ipolicy is None:
1429
      self.ipolicy = MakeEmptyIPolicy()
1430

    
1431
    if self.networks is None:
1432
      self.networks = {}
1433

    
1434
  def FillND(self, node):
1435
    """Return filled out ndparams for L{objects.Node}
1436

1437
    @type node: L{objects.Node}
1438
    @param node: A Node object to fill
1439
    @return a copy of the node's ndparams with defaults filled
1440

1441
    """
1442
    return self.SimpleFillND(node.ndparams)
1443

    
1444
  def SimpleFillND(self, ndparams):
1445
    """Fill a given ndparams dict with defaults.
1446

1447
    @type ndparams: dict
1448
    @param ndparams: the dict to fill
1449
    @rtype: dict
1450
    @return: a copy of the passed in ndparams with missing keys filled
1451
        from the node group defaults
1452

1453
    """
1454
    return FillDict(self.ndparams, ndparams)
1455

    
1456

    
1457
class Cluster(TaggableObject):
1458
  """Config object representing the cluster."""
1459
  __slots__ = [
1460
    "serial_no",
1461
    "rsahostkeypub",
1462
    "highest_used_port",
1463
    "tcpudp_port_pool",
1464
    "mac_prefix",
1465
    "volume_group_name",
1466
    "reserved_lvs",
1467
    "drbd_usermode_helper",
1468
    "default_bridge",
1469
    "default_hypervisor",
1470
    "master_node",
1471
    "master_ip",
1472
    "master_netdev",
1473
    "master_netmask",
1474
    "use_external_mip_script",
1475
    "cluster_name",
1476
    "file_storage_dir",
1477
    "shared_file_storage_dir",
1478
    "enabled_hypervisors",
1479
    "hvparams",
1480
    "ipolicy",
1481
    "os_hvp",
1482
    "beparams",
1483
    "osparams",
1484
    "nicparams",
1485
    "ndparams",
1486
    "diskparams",
1487
    "candidate_pool_size",
1488
    "modify_etc_hosts",
1489
    "modify_ssh_setup",
1490
    "maintain_node_health",
1491
    "uid_pool",
1492
    "default_iallocator",
1493
    "hidden_os",
1494
    "blacklisted_os",
1495
    "primary_ip_family",
1496
    "prealloc_wipe_disks",
1497
    "hv_state_static",
1498
    "disk_state_static",
1499
    ] + _TIMESTAMPS + _UUID
1500

    
1501
  def UpgradeConfig(self):
1502
    """Fill defaults for missing configuration values.
1503

1504
    """
1505
    # pylint: disable=E0203
1506
    # because these are "defined" via slots, not manually
1507
    if self.hvparams is None:
1508
      self.hvparams = constants.HVC_DEFAULTS
1509
    else:
1510
      for hypervisor in self.hvparams:
1511
        self.hvparams[hypervisor] = FillDict(
1512
            constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1513

    
1514
    if self.os_hvp is None:
1515
      self.os_hvp = {}
1516

    
1517
    # osparams added before 2.2
1518
    if self.osparams is None:
1519
      self.osparams = {}
1520

    
1521
    self.ndparams = UpgradeNDParams(self.ndparams)
1522

    
1523
    self.beparams = UpgradeGroupedParams(self.beparams,
1524
                                         constants.BEC_DEFAULTS)
1525
    for beparams_group in self.beparams:
1526
      UpgradeBeParams(self.beparams[beparams_group])
1527

    
1528
    migrate_default_bridge = not self.nicparams
1529
    self.nicparams = UpgradeGroupedParams(self.nicparams,
1530
                                          constants.NICC_DEFAULTS)
1531
    if migrate_default_bridge:
1532
      self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1533
        self.default_bridge
1534

    
1535
    if self.modify_etc_hosts is None:
1536
      self.modify_etc_hosts = True
1537

    
1538
    if self.modify_ssh_setup is None:
1539
      self.modify_ssh_setup = True
1540

    
1541
    # default_bridge is no longer used in 2.1. The slot is left there to
1542
    # support auto-upgrading. It can be removed once we decide to deprecate
1543
    # upgrading straight from 2.0.
1544
    if self.default_bridge is not None:
1545
      self.default_bridge = None
1546

    
1547
    # default_hypervisor is just the first enabled one in 2.1. This slot and
1548
    # code can be removed once upgrading straight from 2.0 is deprecated.
1549
    if self.default_hypervisor is not None:
1550
      self.enabled_hypervisors = ([self.default_hypervisor] +
1551
                                  [hvname for hvname in self.enabled_hypervisors
1552
                                   if hvname != self.default_hypervisor])
1553
      self.default_hypervisor = None
1554

    
1555
    # maintain_node_health added after 2.1.1
1556
    if self.maintain_node_health is None:
1557
      self.maintain_node_health = False
1558

    
1559
    if self.uid_pool is None:
1560
      self.uid_pool = []
1561

    
1562
    if self.default_iallocator is None:
1563
      self.default_iallocator = ""
1564

    
1565
    # reserved_lvs added before 2.2
1566
    if self.reserved_lvs is None:
1567
      self.reserved_lvs = []
1568

    
1569
    # hidden and blacklisted operating systems added before 2.2.1
1570
    if self.hidden_os is None:
1571
      self.hidden_os = []
1572

    
1573
    if self.blacklisted_os is None:
1574
      self.blacklisted_os = []
1575

    
1576
    # primary_ip_family added before 2.3
1577
    if self.primary_ip_family is None:
1578
      self.primary_ip_family = AF_INET
1579

    
1580
    if self.master_netmask is None:
1581
      ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1582
      self.master_netmask = ipcls.iplen
1583

    
1584
    if self.prealloc_wipe_disks is None:
1585
      self.prealloc_wipe_disks = False
1586

    
1587
    # shared_file_storage_dir added before 2.5
1588
    if self.shared_file_storage_dir is None:
1589
      self.shared_file_storage_dir = ""
1590

    
1591
    if self.use_external_mip_script is None:
1592
      self.use_external_mip_script = False
1593

    
1594
    if self.diskparams:
1595
      self.diskparams = UpgradeDiskParams(self.diskparams)
1596
    else:
1597
      self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1598

    
1599
    # instance policy added before 2.6
1600
    if self.ipolicy is None:
1601
      self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1602
    else:
1603
      # we can either make sure to upgrade the ipolicy always, or only
1604
      # do it in some corner cases (e.g. missing keys); note that this
1605
      # will break any removal of keys from the ipolicy dict
1606
      self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1607

    
1608
  @property
1609
  def primary_hypervisor(self):
1610
    """The first hypervisor is the primary.
1611

1612
    Useful, for example, for L{Node}'s hv/disk state.
1613

1614
    """
1615
    return self.enabled_hypervisors[0]
1616

    
1617
  def ToDict(self):
1618
    """Custom function for cluster.
1619

1620
    """
1621
    mydict = super(Cluster, self).ToDict()
1622
    mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool)
1623
    return mydict
1624

    
1625
  @classmethod
1626
  def FromDict(cls, val):
1627
    """Custom function for cluster.
1628

1629
    """
1630
    obj = super(Cluster, cls).FromDict(val)
1631
    if not isinstance(obj.tcpudp_port_pool, set):
1632
      obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1633
    return obj
1634

    
1635
  def SimpleFillDP(self, diskparams):
1636
    """Fill a given diskparams dict with cluster defaults.
1637

1638
    @param diskparams: The diskparams
1639
    @return: The defaults dict
1640

1641
    """
1642
    return FillDiskParams(self.diskparams, diskparams)
1643

    
1644
  def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1645
    """Get the default hypervisor parameters for the cluster.
1646

1647
    @param hypervisor: the hypervisor name
1648
    @param os_name: if specified, we'll also update the defaults for this OS
1649
    @param skip_keys: if passed, list of keys not to use
1650
    @return: the defaults dict
1651

1652
    """
1653
    if skip_keys is None:
1654
      skip_keys = []
1655

    
1656
    fill_stack = [self.hvparams.get(hypervisor, {})]
1657
    if os_name is not None:
1658
      os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1659
      fill_stack.append(os_hvp)
1660

    
1661
    ret_dict = {}
1662
    for o_dict in fill_stack:
1663
      ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1664

    
1665
    return ret_dict
1666

    
1667
  def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1668
    """Fill a given hvparams dict with cluster defaults.
1669

1670
    @type hv_name: string
1671
    @param hv_name: the hypervisor to use
1672
    @type os_name: string
1673
    @param os_name: the OS to use for overriding the hypervisor defaults
1674
    @type skip_globals: boolean
1675
    @param skip_globals: if True, the global hypervisor parameters will
1676
        not be filled
1677
    @rtype: dict
1678
    @return: a copy of the given hvparams with missing keys filled from
1679
        the cluster defaults
1680

1681
    """
1682
    if skip_globals:
1683
      skip_keys = constants.HVC_GLOBALS
1684
    else:
1685
      skip_keys = []
1686

    
1687
    def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1688
    return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1689

    
1690
  def FillHV(self, instance, skip_globals=False):
1691
    """Fill an instance's hvparams dict with cluster defaults.
1692

1693
    @type instance: L{objects.Instance}
1694
    @param instance: the instance parameter to fill
1695
    @type skip_globals: boolean
1696
    @param skip_globals: if True, the global hypervisor parameters will
1697
        not be filled
1698
    @rtype: dict
1699
    @return: a copy of the instance's hvparams with missing keys filled from
1700
        the cluster defaults
1701

1702
    """
1703
    return self.SimpleFillHV(instance.hypervisor, instance.os,
1704
                             instance.hvparams, skip_globals)
1705

    
1706
  def SimpleFillBE(self, beparams):
1707
    """Fill a given beparams dict with cluster defaults.
1708

1709
    @type beparams: dict
1710
    @param beparams: the dict to fill
1711
    @rtype: dict
1712
    @return: a copy of the passed in beparams with missing keys filled
1713
        from the cluster defaults
1714

1715
    """
1716
    return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1717

    
1718
  def FillBE(self, instance):
1719
    """Fill an instance's beparams dict with cluster defaults.
1720

1721
    @type instance: L{objects.Instance}
1722
    @param instance: the instance parameter to fill
1723
    @rtype: dict
1724
    @return: a copy of the instance's beparams with missing keys filled from
1725
        the cluster defaults
1726

1727
    """
1728
    return self.SimpleFillBE(instance.beparams)
1729

    
1730
  def SimpleFillNIC(self, nicparams):
1731
    """Fill a given nicparams dict with cluster defaults.
1732

1733
    @type nicparams: dict
1734
    @param nicparams: the dict to fill
1735
    @rtype: dict
1736
    @return: a copy of the passed in nicparams with missing keys filled
1737
        from the cluster defaults
1738

1739
    """
1740
    return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1741

    
1742
  def SimpleFillOS(self, os_name, os_params):
1743
    """Fill an instance's osparams dict with cluster defaults.
1744

1745
    @type os_name: string
1746
    @param os_name: the OS name to use
1747
    @type os_params: dict
1748
    @param os_params: the dict to fill with default values
1749
    @rtype: dict
1750
    @return: a copy of the instance's osparams with missing keys filled from
1751
        the cluster defaults
1752

1753
    """
1754
    name_only = os_name.split("+", 1)[0]
1755
    # base OS
1756
    result = self.osparams.get(name_only, {})
1757
    # OS with variant
1758
    result = FillDict(result, self.osparams.get(os_name, {}))
1759
    # specified params
1760
    return FillDict(result, os_params)
1761

    
1762
  @staticmethod
1763
  def SimpleFillHvState(hv_state):
1764
    """Fill an hv_state sub dict with cluster defaults.
1765

1766
    """
1767
    return FillDict(constants.HVST_DEFAULTS, hv_state)
1768

    
1769
  @staticmethod
1770
  def SimpleFillDiskState(disk_state):
1771
    """Fill an disk_state sub dict with cluster defaults.
1772

1773
    """
1774
    return FillDict(constants.DS_DEFAULTS, disk_state)
1775

    
1776
  def FillND(self, node, nodegroup):
1777
    """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1778

1779
    @type node: L{objects.Node}
1780
    @param node: A Node object to fill
1781
    @type nodegroup: L{objects.NodeGroup}
1782
    @param nodegroup: A Node object to fill
1783
    @return a copy of the node's ndparams with defaults filled
1784

1785
    """
1786
    return self.SimpleFillND(nodegroup.FillND(node))
1787

    
1788
  def SimpleFillND(self, ndparams):
1789
    """Fill a given ndparams dict with defaults.
1790

1791
    @type ndparams: dict
1792
    @param ndparams: the dict to fill
1793
    @rtype: dict
1794
    @return: a copy of the passed in ndparams with missing keys filled
1795
        from the cluster defaults
1796

1797
    """
1798
    return FillDict(self.ndparams, ndparams)
1799

    
1800
  def SimpleFillIPolicy(self, ipolicy):
1801
    """ Fill instance policy dict with defaults.
1802

1803
    @type ipolicy: dict
1804
    @param ipolicy: the dict to fill
1805
    @rtype: dict
1806
    @return: a copy of passed ipolicy with missing keys filled from
1807
      the cluster defaults
1808

1809
    """
1810
    return FillIPolicy(self.ipolicy, ipolicy)
1811

    
1812

    
1813
class BlockDevStatus(ConfigObject):
1814
  """Config object representing the status of a block device."""
1815
  __slots__ = [
1816
    "dev_path",
1817
    "major",
1818
    "minor",
1819
    "sync_percent",
1820
    "estimated_time",
1821
    "is_degraded",
1822
    "ldisk_status",
1823
    ]
1824

    
1825

    
1826
class ImportExportStatus(ConfigObject):
1827
  """Config object representing the status of an import or export."""
1828
  __slots__ = [
1829
    "recent_output",
1830
    "listen_port",
1831
    "connected",
1832
    "progress_mbytes",
1833
    "progress_throughput",
1834
    "progress_eta",
1835
    "progress_percent",
1836
    "exit_status",
1837
    "error_message",
1838
    ] + _TIMESTAMPS
1839

    
1840

    
1841
class ImportExportOptions(ConfigObject):
1842
  """Options for import/export daemon
1843

1844
  @ivar key_name: X509 key name (None for cluster certificate)
1845
  @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1846
  @ivar compress: Compression method (one of L{constants.IEC_ALL})
1847
  @ivar magic: Used to ensure the connection goes to the right disk
1848
  @ivar ipv6: Whether to use IPv6
1849
  @ivar connect_timeout: Number of seconds for establishing connection
1850

1851
  """
1852
  __slots__ = [
1853
    "key_name",
1854
    "ca_pem",
1855
    "compress",
1856
    "magic",
1857
    "ipv6",
1858
    "connect_timeout",
1859
    ]
1860

    
1861

    
1862
class ConfdRequest(ConfigObject):
1863
  """Object holding a confd request.
1864

1865
  @ivar protocol: confd protocol version
1866
  @ivar type: confd query type
1867
  @ivar query: query request
1868
  @ivar rsalt: requested reply salt
1869

1870
  """
1871
  __slots__ = [
1872
    "protocol",
1873
    "type",
1874
    "query",
1875
    "rsalt",
1876
    ]
1877

    
1878

    
1879
class ConfdReply(ConfigObject):
1880
  """Object holding a confd reply.
1881

1882
  @ivar protocol: confd protocol version
1883
  @ivar status: reply status code (ok, error)
1884
  @ivar answer: confd query reply
1885
  @ivar serial: configuration serial number
1886

1887
  """
1888
  __slots__ = [
1889
    "protocol",
1890
    "status",
1891
    "answer",
1892
    "serial",
1893
    ]
1894

    
1895

    
1896
class QueryFieldDefinition(ConfigObject):
1897
  """Object holding a query field definition.
1898

1899
  @ivar name: Field name
1900
  @ivar title: Human-readable title
1901
  @ivar kind: Field type
1902
  @ivar doc: Human-readable description
1903

1904
  """
1905
  __slots__ = [
1906
    "name",
1907
    "title",
1908
    "kind",
1909
    "doc",
1910
    ]
1911

    
1912

    
1913
class _QueryResponseBase(ConfigObject):
1914
  __slots__ = [
1915
    "fields",
1916
    ]
1917

    
1918
  def ToDict(self):
1919
    """Custom function for serializing.
1920

1921
    """
1922
    mydict = super(_QueryResponseBase, self).ToDict()
1923
    mydict["fields"] = self._ContainerToDicts(mydict["fields"])
1924
    return mydict
1925

    
1926
  @classmethod
1927
  def FromDict(cls, val):
1928
    """Custom function for de-serializing.
1929

1930
    """
1931
    obj = super(_QueryResponseBase, cls).FromDict(val)
1932
    obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1933
    return obj
1934

    
1935

    
1936
class QueryResponse(_QueryResponseBase):
1937
  """Object holding the response to a query.
1938

1939
  @ivar fields: List of L{QueryFieldDefinition} objects
1940
  @ivar data: Requested data
1941

1942
  """
1943
  __slots__ = [
1944
    "data",
1945
    ]
1946

    
1947

    
1948
class QueryFieldsRequest(ConfigObject):
1949
  """Object holding a request for querying available fields.
1950

1951
  """
1952
  __slots__ = [
1953
    "what",
1954
    "fields",
1955
    ]
1956

    
1957

    
1958
class QueryFieldsResponse(_QueryResponseBase):
1959
  """Object holding the response to a query for fields.
1960

1961
  @ivar fields: List of L{QueryFieldDefinition} objects
1962

1963
  """
1964
  __slots__ = []
1965

    
1966

    
1967
class MigrationStatus(ConfigObject):
1968
  """Object holding the status of a migration.
1969

1970
  """
1971
  __slots__ = [
1972
    "status",
1973
    "transferred_ram",
1974
    "total_ram",
1975
    ]
1976

    
1977

    
1978
class InstanceConsole(ConfigObject):
1979
  """Object describing how to access the console of an instance.
1980

1981
  """
1982
  __slots__ = [
1983
    "instance",
1984
    "kind",
1985
    "message",
1986
    "host",
1987
    "port",
1988
    "user",
1989
    "command",
1990
    "display",
1991
    ]
1992

    
1993
  def Validate(self):
1994
    """Validates contents of this object.
1995

1996
    """
1997
    assert self.kind in constants.CONS_ALL, "Unknown console type"
1998
    assert self.instance, "Missing instance name"
1999
    assert self.message or self.kind in [constants.CONS_SSH,
2000
                                         constants.CONS_SPICE,
2001
                                         constants.CONS_VNC]
2002
    assert self.host or self.kind == constants.CONS_MESSAGE
2003
    assert self.port or self.kind in [constants.CONS_MESSAGE,
2004
                                      constants.CONS_SSH]
2005
    assert self.user or self.kind in [constants.CONS_MESSAGE,
2006
                                      constants.CONS_SPICE,
2007
                                      constants.CONS_VNC]
2008
    assert self.command or self.kind in [constants.CONS_MESSAGE,
2009
                                         constants.CONS_SPICE,
2010
                                         constants.CONS_VNC]
2011
    assert self.display or self.kind in [constants.CONS_MESSAGE,
2012
                                         constants.CONS_SPICE,
2013
                                         constants.CONS_SSH]
2014
    return True
2015

    
2016

    
2017
class Network(TaggableObject):
2018
  """Object representing a network definition for ganeti.
2019

2020
  """
2021
  __slots__ = [
2022
    "name",
2023
    "serial_no",
2024
    "network_type",
2025
    "mac_prefix",
2026
    "family",
2027
    "network",
2028
    "network6",
2029
    "gateway",
2030
    "gateway6",
2031
    "size",
2032
    "reservations",
2033
    "ext_reservations",
2034
    ] + _TIMESTAMPS + _UUID
2035

    
2036

    
2037
class SerializableConfigParser(ConfigParser.SafeConfigParser):
2038
  """Simple wrapper over ConfigParse that allows serialization.
2039

2040
  This class is basically ConfigParser.SafeConfigParser with two
2041
  additional methods that allow it to serialize/unserialize to/from a
2042
  buffer.
2043

2044
  """
2045
  def Dumps(self):
2046
    """Dump this instance and return the string representation."""
2047
    buf = StringIO()
2048
    self.write(buf)
2049
    return buf.getvalue()
2050

    
2051
  @classmethod
2052
  def Loads(cls, data):
2053
    """Load data from a string."""
2054
    buf = StringIO(data)
2055
    cfp = cls()
2056
    cfp.readfp(buf)
2057
    return cfp