Statistics
| Branch: | Tag: | Revision:

root / lib / objects.py @ d0de443e

History | View | Annotate | Download (59.8 kB)

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

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

27 a8083063 Iustin Pop
"""
28 a8083063 Iustin Pop
29 0007f3ab Andrea Spadaccini
# pylint: disable=E0203,W0201,R0902
30 6c881c52 Iustin Pop
31 6c881c52 Iustin Pop
# E0203: Access to member %r before its definition, since we use
32 2ed0e208 Iustin Pop
# objects.py which doesn't explicitly initialise its members
33 6c881c52 Iustin Pop
34 7260cfbe Iustin Pop
# W0201: Attribute '%s' defined outside __init__
35 a8083063 Iustin Pop
36 0007f3ab Andrea Spadaccini
# R0902: Allow instances of these objects to have more than 20 attributes
37 0007f3ab Andrea Spadaccini
38 a8083063 Iustin Pop
import ConfigParser
39 5c947f38 Iustin Pop
import re
40 5bf7b5cf Iustin Pop
import copy
41 250a9404 Bernardo Dal Seno
import logging
42 e11a1b77 Adeodato Simo
import time
43 d5835922 Michael Hanselmann
from cStringIO import StringIO
44 a8083063 Iustin Pop
45 a8083063 Iustin Pop
from ganeti import errors
46 5c947f38 Iustin Pop
from ganeti import constants
47 0007f3ab Andrea Spadaccini
from ganeti import netutils
48 473d87a3 Iustin Pop
from ganeti import outils
49 32017174 Agata Murawska
from ganeti import utils
50 a8083063 Iustin Pop
51 f4c9af7a Guido Trotter
from socket import AF_INET
52 f4c9af7a Guido Trotter
53 a8083063 Iustin Pop
54 a8083063 Iustin Pop
__all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
55 eaa4c57c Dimitris Aragiorgis
           "OS", "Node", "NodeGroup", "Cluster", "FillDict", "Network"]
56 a8083063 Iustin Pop
57 d693c864 Iustin Pop
_TIMESTAMPS = ["ctime", "mtime"]
58 e1dcc53a Iustin Pop
_UUID = ["uuid"]
59 96acbc09 Michael Hanselmann
60 8d8d650c Michael Hanselmann
61 e11ddf13 Iustin Pop
def FillDict(defaults_dict, custom_dict, skip_keys=None):
62 29921401 Iustin Pop
  """Basic function to apply settings on top a default dict.
63 abe609b2 Guido Trotter

64 29921401 Iustin Pop
  @type defaults_dict: dict
65 29921401 Iustin Pop
  @param defaults_dict: dictionary holding the default values
66 29921401 Iustin Pop
  @type custom_dict: dict
67 29921401 Iustin Pop
  @param custom_dict: dictionary holding customized value
68 7736a5f2 Iustin Pop
  @type skip_keys: list
69 7736a5f2 Iustin Pop
  @param skip_keys: which keys not to fill
70 29921401 Iustin Pop
  @rtype: dict
71 29921401 Iustin Pop
  @return: dict with the 'full' values
72 abe609b2 Guido Trotter

73 29921401 Iustin Pop
  """
74 29921401 Iustin Pop
  ret_dict = copy.deepcopy(defaults_dict)
75 29921401 Iustin Pop
  ret_dict.update(custom_dict)
76 e11ddf13 Iustin Pop
  if skip_keys:
77 e11ddf13 Iustin Pop
    for k in skip_keys:
78 e11ddf13 Iustin Pop
      try:
79 e11ddf13 Iustin Pop
        del ret_dict[k]
80 e11ddf13 Iustin Pop
      except KeyError:
81 e11ddf13 Iustin Pop
        pass
82 29921401 Iustin Pop
  return ret_dict
83 a8083063 Iustin Pop
84 6e34b628 Guido Trotter
85 2cc673a3 Iustin Pop
def FillIPolicy(default_ipolicy, custom_ipolicy, skip_keys=None):
86 2cc673a3 Iustin Pop
  """Fills an instance policy with defaults.
87 918eb80b Agata Murawska

88 918eb80b Agata Murawska
  """
89 2cc673a3 Iustin Pop
  assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
90 918eb80b Agata Murawska
  ret_dict = {}
91 12378fe3 Iustin Pop
  for key in constants.IPOLICY_ISPECS:
92 2cc673a3 Iustin Pop
    ret_dict[key] = FillDict(default_ipolicy[key],
93 2cc673a3 Iustin Pop
                             custom_ipolicy.get(key, {}),
94 918eb80b Agata Murawska
                             skip_keys=skip_keys)
95 2cc673a3 Iustin Pop
  # list items
96 d04c9d45 Iustin Pop
  for key in [constants.IPOLICY_DTS]:
97 2cc673a3 Iustin Pop
    ret_dict[key] = list(custom_ipolicy.get(key, default_ipolicy[key]))
98 ff6c5e55 Iustin Pop
  # other items which we know we can directly copy (immutables)
99 ff6c5e55 Iustin Pop
  for key in constants.IPOLICY_PARAMETERS:
100 ff6c5e55 Iustin Pop
    ret_dict[key] = custom_ipolicy.get(key, default_ipolicy[key])
101 2cc673a3 Iustin Pop
102 918eb80b Agata Murawska
  return ret_dict
103 918eb80b Agata Murawska
104 918eb80b Agata Murawska
105 57987785 René Nussbaumer
def FillDiskParams(default_dparams, custom_dparams, skip_keys=None):
106 57987785 René Nussbaumer
  """Fills the disk parameter defaults.
107 57987785 René Nussbaumer

108 af9fb4cc René Nussbaumer
  @see: L{FillDict} for parameters and return value
109 57987785 René Nussbaumer

110 57987785 René Nussbaumer
  """
111 57987785 René Nussbaumer
  assert frozenset(default_dparams.keys()) == constants.DISK_TEMPLATES
112 57987785 René Nussbaumer
113 57987785 René Nussbaumer
  return dict((dt, FillDict(default_dparams[dt], custom_dparams.get(dt, {}),
114 57987785 René Nussbaumer
                             skip_keys=skip_keys))
115 57987785 René Nussbaumer
              for dt in constants.DISK_TEMPLATES)
116 57987785 René Nussbaumer
117 57987785 René Nussbaumer
118 6e34b628 Guido Trotter
def UpgradeGroupedParams(target, defaults):
119 6e34b628 Guido Trotter
  """Update all groups for the target parameter.
120 6e34b628 Guido Trotter

121 6e34b628 Guido Trotter
  @type target: dict of dicts
122 6e34b628 Guido Trotter
  @param target: {group: {parameter: value}}
123 6e34b628 Guido Trotter
  @type defaults: dict
124 6e34b628 Guido Trotter
  @param defaults: default parameter values
125 6e34b628 Guido Trotter

126 6e34b628 Guido Trotter
  """
127 6e34b628 Guido Trotter
  if target is None:
128 6e34b628 Guido Trotter
    target = {constants.PP_DEFAULT: defaults}
129 6e34b628 Guido Trotter
  else:
130 6e34b628 Guido Trotter
    for group in target:
131 6e34b628 Guido Trotter
      target[group] = FillDict(defaults, target[group])
132 6e34b628 Guido Trotter
  return target
133 6e34b628 Guido Trotter
134 6e34b628 Guido Trotter
135 8c72ab2b Guido Trotter
def UpgradeBeParams(target):
136 8c72ab2b Guido Trotter
  """Update the be parameters dict to the new format.
137 8c72ab2b Guido Trotter

138 8c72ab2b Guido Trotter
  @type target: dict
139 8c72ab2b Guido Trotter
  @param target: "be" parameters dict
140 8c72ab2b Guido Trotter

141 8c72ab2b Guido Trotter
  """
142 8c72ab2b Guido Trotter
  if constants.BE_MEMORY in target:
143 8c72ab2b Guido Trotter
    memory = target[constants.BE_MEMORY]
144 8c72ab2b Guido Trotter
    target[constants.BE_MAXMEM] = memory
145 8c72ab2b Guido Trotter
    target[constants.BE_MINMEM] = memory
146 b2e233a5 Guido Trotter
    del target[constants.BE_MEMORY]
147 8c72ab2b Guido Trotter
148 8c72ab2b Guido Trotter
149 bc5d0215 Andrea Spadaccini
def UpgradeDiskParams(diskparams):
150 bc5d0215 Andrea Spadaccini
  """Upgrade the disk parameters.
151 bc5d0215 Andrea Spadaccini

152 bc5d0215 Andrea Spadaccini
  @type diskparams: dict
153 bc5d0215 Andrea Spadaccini
  @param diskparams: disk parameters to upgrade
154 bc5d0215 Andrea Spadaccini
  @rtype: dict
155 765ada2b Iustin Pop
  @return: the upgraded disk parameters dict
156 bc5d0215 Andrea Spadaccini

157 bc5d0215 Andrea Spadaccini
  """
158 99ccf8b9 René Nussbaumer
  if not diskparams:
159 99ccf8b9 René Nussbaumer
    result = {}
160 bc5d0215 Andrea Spadaccini
  else:
161 57987785 René Nussbaumer
    result = FillDiskParams(constants.DISK_DT_DEFAULTS, diskparams)
162 bc5d0215 Andrea Spadaccini
163 bc5d0215 Andrea Spadaccini
  return result
164 bc5d0215 Andrea Spadaccini
165 bc5d0215 Andrea Spadaccini
166 2a27dac3 Iustin Pop
def UpgradeNDParams(ndparams):
167 2a27dac3 Iustin Pop
  """Upgrade ndparams structure.
168 2a27dac3 Iustin Pop

169 2a27dac3 Iustin Pop
  @type ndparams: dict
170 2a27dac3 Iustin Pop
  @param ndparams: disk parameters to upgrade
171 2a27dac3 Iustin Pop
  @rtype: dict
172 2a27dac3 Iustin Pop
  @return: the upgraded node parameters dict
173 2a27dac3 Iustin Pop

174 2a27dac3 Iustin Pop
  """
175 2a27dac3 Iustin Pop
  if ndparams is None:
176 2a27dac3 Iustin Pop
    ndparams = {}
177 2a27dac3 Iustin Pop
178 1df4d430 Iustin Pop
  if (constants.ND_OOB_PROGRAM in ndparams and
179 1df4d430 Iustin Pop
      ndparams[constants.ND_OOB_PROGRAM] is None):
180 1df4d430 Iustin Pop
    # will be reset by the line below
181 1df4d430 Iustin Pop
    del ndparams[constants.ND_OOB_PROGRAM]
182 2a27dac3 Iustin Pop
  return FillDict(constants.NDC_DEFAULTS, ndparams)
183 2a27dac3 Iustin Pop
184 2a27dac3 Iustin Pop
185 918eb80b Agata Murawska
def MakeEmptyIPolicy():
186 918eb80b Agata Murawska
  """Create empty IPolicy dictionary.
187 918eb80b Agata Murawska

188 918eb80b Agata Murawska
  """
189 918eb80b Agata Murawska
  return dict([
190 2cc673a3 Iustin Pop
    (constants.ISPECS_MIN, {}),
191 2cc673a3 Iustin Pop
    (constants.ISPECS_MAX, {}),
192 2cc673a3 Iustin Pop
    (constants.ISPECS_STD, {}),
193 918eb80b Agata Murawska
    ])
194 918eb80b Agata Murawska
195 918eb80b Agata Murawska
196 473d87a3 Iustin Pop
class ConfigObject(outils.ValidatedSlots):
197 a8083063 Iustin Pop
  """A generic config object.
198 a8083063 Iustin Pop

199 a8083063 Iustin Pop
  It has the following properties:
200 a8083063 Iustin Pop

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

205 a8083063 Iustin Pop
  Classes derived from this must always declare __slots__ (we use many
206 55224070 Guido Trotter
  config objects and the memory reduction is useful)
207 a8083063 Iustin Pop

208 a8083063 Iustin Pop
  """
209 a8083063 Iustin Pop
  __slots__ = []
210 a8083063 Iustin Pop
211 a8083063 Iustin Pop
  def __getattr__(self, name):
212 32683096 René Nussbaumer
    if name not in self.GetAllSlots():
213 3ecf6786 Iustin Pop
      raise AttributeError("Invalid object attribute %s.%s" %
214 3ecf6786 Iustin Pop
                           (type(self).__name__, name))
215 a8083063 Iustin Pop
    return None
216 a8083063 Iustin Pop
217 a8083063 Iustin Pop
  def __setstate__(self, state):
218 32683096 René Nussbaumer
    slots = self.GetAllSlots()
219 a8083063 Iustin Pop
    for name in state:
220 adf385c7 Iustin Pop
      if name in slots:
221 a8083063 Iustin Pop
        setattr(self, name, state[name])
222 a8083063 Iustin Pop
223 32683096 René Nussbaumer
  def Validate(self):
224 32683096 René Nussbaumer
    """Validates the slots.
225 adf385c7 Iustin Pop

226 adf385c7 Iustin Pop
    """
227 415feb2e René Nussbaumer
228 ff9c047c Iustin Pop
  def ToDict(self):
229 ff9c047c Iustin Pop
    """Convert to a dict holding only standard python types.
230 ff9c047c Iustin Pop

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

237 ff9c047c Iustin Pop
    """
238 4c14965f Guido Trotter
    result = {}
239 32683096 René Nussbaumer
    for name in self.GetAllSlots():
240 4c14965f Guido Trotter
      value = getattr(self, name, None)
241 4c14965f Guido Trotter
      if value is not None:
242 4c14965f Guido Trotter
        result[name] = value
243 4c14965f Guido Trotter
    return result
244 4c14965f Guido Trotter
245 4c14965f Guido Trotter
  __getstate__ = ToDict
246 ff9c047c Iustin Pop
247 ff9c047c Iustin Pop
  @classmethod
248 ff9c047c Iustin Pop
  def FromDict(cls, val):
249 ff9c047c Iustin Pop
    """Create an object from a dictionary.
250 ff9c047c Iustin Pop

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

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

259 ff9c047c Iustin Pop
    """
260 ff9c047c Iustin Pop
    if not isinstance(val, dict):
261 ff9c047c Iustin Pop
      raise errors.ConfigurationError("Invalid object passed to FromDict:"
262 ff9c047c Iustin Pop
                                      " expected dict, got %s" % type(val))
263 319856a9 Michael Hanselmann
    val_str = dict([(str(k), v) for k, v in val.iteritems()])
264 b459a848 Andrea Spadaccini
    obj = cls(**val_str) # pylint: disable=W0142
265 ff9c047c Iustin Pop
    return obj
266 ff9c047c Iustin Pop
267 e8d563f3 Iustin Pop
  def Copy(self):
268 e8d563f3 Iustin Pop
    """Makes a deep copy of the current object and its children.
269 e8d563f3 Iustin Pop

270 e8d563f3 Iustin Pop
    """
271 e8d563f3 Iustin Pop
    dict_form = self.ToDict()
272 e8d563f3 Iustin Pop
    clone_obj = self.__class__.FromDict(dict_form)
273 e8d563f3 Iustin Pop
    return clone_obj
274 e8d563f3 Iustin Pop
275 ff9c047c Iustin Pop
  def __repr__(self):
276 ff9c047c Iustin Pop
    """Implement __repr__ for ConfigObjects."""
277 ff9c047c Iustin Pop
    return repr(self.ToDict())
278 ff9c047c Iustin Pop
279 560428be Guido Trotter
  def UpgradeConfig(self):
280 560428be Guido Trotter
    """Fill defaults for missing configuration values.
281 560428be Guido Trotter

282 90d726a8 Iustin Pop
    This method will be called at configuration load time, and its
283 90d726a8 Iustin Pop
    implementation will be object dependent.
284 560428be Guido Trotter

285 560428be Guido Trotter
    """
286 560428be Guido Trotter
    pass
287 560428be Guido Trotter
288 a8083063 Iustin Pop
289 ec29fe40 Iustin Pop
class TaggableObject(ConfigObject):
290 5c947f38 Iustin Pop
  """An generic class supporting tags.
291 5c947f38 Iustin Pop

292 5c947f38 Iustin Pop
  """
293 154b9580 Balazs Lecz
  __slots__ = ["tags"]
294 b5e5632e Iustin Pop
  VALID_TAG_RE = re.compile("^[\w.+*/:@-]+$")
295 2057f6c7 Iustin Pop
296 b5e5632e Iustin Pop
  @classmethod
297 b5e5632e Iustin Pop
  def ValidateTag(cls, tag):
298 5c947f38 Iustin Pop
    """Check if a tag is valid.
299 5c947f38 Iustin Pop

300 5c947f38 Iustin Pop
    If the tag is invalid, an errors.TagError will be raised. The
301 5c947f38 Iustin Pop
    function has no return value.
302 5c947f38 Iustin Pop

303 5c947f38 Iustin Pop
    """
304 5c947f38 Iustin Pop
    if not isinstance(tag, basestring):
305 3ecf6786 Iustin Pop
      raise errors.TagError("Invalid tag type (not a string)")
306 5c947f38 Iustin Pop
    if len(tag) > constants.MAX_TAG_LEN:
307 319856a9 Michael Hanselmann
      raise errors.TagError("Tag too long (>%d characters)" %
308 319856a9 Michael Hanselmann
                            constants.MAX_TAG_LEN)
309 5c947f38 Iustin Pop
    if not tag:
310 3ecf6786 Iustin Pop
      raise errors.TagError("Tags cannot be empty")
311 b5e5632e Iustin Pop
    if not cls.VALID_TAG_RE.match(tag):
312 3ecf6786 Iustin Pop
      raise errors.TagError("Tag contains invalid characters")
313 5c947f38 Iustin Pop
314 5c947f38 Iustin Pop
  def GetTags(self):
315 5c947f38 Iustin Pop
    """Return the tags list.
316 5c947f38 Iustin Pop

317 5c947f38 Iustin Pop
    """
318 5c947f38 Iustin Pop
    tags = getattr(self, "tags", None)
319 5c947f38 Iustin Pop
    if tags is None:
320 5c947f38 Iustin Pop
      tags = self.tags = set()
321 5c947f38 Iustin Pop
    return tags
322 5c947f38 Iustin Pop
323 5c947f38 Iustin Pop
  def AddTag(self, tag):
324 5c947f38 Iustin Pop
    """Add a new tag.
325 5c947f38 Iustin Pop

326 5c947f38 Iustin Pop
    """
327 5c947f38 Iustin Pop
    self.ValidateTag(tag)
328 5c947f38 Iustin Pop
    tags = self.GetTags()
329 5c947f38 Iustin Pop
    if len(tags) >= constants.MAX_TAGS_PER_OBJ:
330 3ecf6786 Iustin Pop
      raise errors.TagError("Too many tags")
331 5c947f38 Iustin Pop
    self.GetTags().add(tag)
332 5c947f38 Iustin Pop
333 5c947f38 Iustin Pop
  def RemoveTag(self, tag):
334 5c947f38 Iustin Pop
    """Remove a tag.
335 5c947f38 Iustin Pop

336 5c947f38 Iustin Pop
    """
337 5c947f38 Iustin Pop
    self.ValidateTag(tag)
338 5c947f38 Iustin Pop
    tags = self.GetTags()
339 5c947f38 Iustin Pop
    try:
340 5c947f38 Iustin Pop
      tags.remove(tag)
341 5c947f38 Iustin Pop
    except KeyError:
342 3ecf6786 Iustin Pop
      raise errors.TagError("Tag not found")
343 5c947f38 Iustin Pop
344 ff9c047c Iustin Pop
  def ToDict(self):
345 ff9c047c Iustin Pop
    """Taggable-object-specific conversion to standard python types.
346 ff9c047c Iustin Pop

347 ff9c047c Iustin Pop
    This replaces the tags set with a list.
348 ff9c047c Iustin Pop

349 ff9c047c Iustin Pop
    """
350 ff9c047c Iustin Pop
    bo = super(TaggableObject, self).ToDict()
351 ff9c047c Iustin Pop
352 ff9c047c Iustin Pop
    tags = bo.get("tags", None)
353 ff9c047c Iustin Pop
    if isinstance(tags, set):
354 ff9c047c Iustin Pop
      bo["tags"] = list(tags)
355 ff9c047c Iustin Pop
    return bo
356 ff9c047c Iustin Pop
357 ff9c047c Iustin Pop
  @classmethod
358 ff9c047c Iustin Pop
  def FromDict(cls, val):
359 ff9c047c Iustin Pop
    """Custom function for instances.
360 ff9c047c Iustin Pop

361 ff9c047c Iustin Pop
    """
362 ff9c047c Iustin Pop
    obj = super(TaggableObject, cls).FromDict(val)
363 ff9c047c Iustin Pop
    if hasattr(obj, "tags") and isinstance(obj.tags, list):
364 ff9c047c Iustin Pop
      obj.tags = set(obj.tags)
365 ff9c047c Iustin Pop
    return obj
366 ff9c047c Iustin Pop
367 5c947f38 Iustin Pop
368 061af273 Andrea Spadaccini
class MasterNetworkParameters(ConfigObject):
369 061af273 Andrea Spadaccini
  """Network configuration parameters for the master
370 061af273 Andrea Spadaccini

371 061af273 Andrea Spadaccini
  @ivar name: master name
372 061af273 Andrea Spadaccini
  @ivar ip: master IP
373 061af273 Andrea Spadaccini
  @ivar netmask: master netmask
374 061af273 Andrea Spadaccini
  @ivar netdev: master network device
375 061af273 Andrea Spadaccini
  @ivar ip_family: master IP family
376 061af273 Andrea Spadaccini

377 061af273 Andrea Spadaccini
  """
378 061af273 Andrea Spadaccini
  __slots__ = [
379 061af273 Andrea Spadaccini
    "name",
380 061af273 Andrea Spadaccini
    "ip",
381 061af273 Andrea Spadaccini
    "netmask",
382 061af273 Andrea Spadaccini
    "netdev",
383 3c286190 Dimitris Aragiorgis
    "ip_family",
384 061af273 Andrea Spadaccini
    ]
385 061af273 Andrea Spadaccini
386 061af273 Andrea Spadaccini
387 a8083063 Iustin Pop
class ConfigData(ConfigObject):
388 a8083063 Iustin Pop
  """Top-level config object."""
389 3df43542 Guido Trotter
  __slots__ = [
390 3df43542 Guido Trotter
    "version",
391 3df43542 Guido Trotter
    "cluster",
392 3df43542 Guido Trotter
    "nodes",
393 3df43542 Guido Trotter
    "nodegroups",
394 3df43542 Guido Trotter
    "instances",
395 eaa4c57c Dimitris Aragiorgis
    "networks",
396 3df43542 Guido Trotter
    "serial_no",
397 3df43542 Guido Trotter
    ] + _TIMESTAMPS
398 a8083063 Iustin Pop
399 ff9c047c Iustin Pop
  def ToDict(self):
400 ff9c047c Iustin Pop
    """Custom function for top-level config data.
401 ff9c047c Iustin Pop

402 ff9c047c Iustin Pop
    This just replaces the list of instances, nodes and the cluster
403 ff9c047c Iustin Pop
    with standard python types.
404 ff9c047c Iustin Pop

405 ff9c047c Iustin Pop
    """
406 ff9c047c Iustin Pop
    mydict = super(ConfigData, self).ToDict()
407 ff9c047c Iustin Pop
    mydict["cluster"] = mydict["cluster"].ToDict()
408 eaa4c57c Dimitris Aragiorgis
    for key in "nodes", "instances", "nodegroups", "networks":
409 fe502d25 Iustin Pop
      mydict[key] = outils.ContainerToDicts(mydict[key])
410 ff9c047c Iustin Pop
411 ff9c047c Iustin Pop
    return mydict
412 ff9c047c Iustin Pop
413 ff9c047c Iustin Pop
  @classmethod
414 ff9c047c Iustin Pop
  def FromDict(cls, val):
415 ff9c047c Iustin Pop
    """Custom function for top-level config data
416 ff9c047c Iustin Pop

417 ff9c047c Iustin Pop
    """
418 ff9c047c Iustin Pop
    obj = super(ConfigData, cls).FromDict(val)
419 ff9c047c Iustin Pop
    obj.cluster = Cluster.FromDict(obj.cluster)
420 fe502d25 Iustin Pop
    obj.nodes = outils.ContainerFromDicts(obj.nodes, dict, Node)
421 473ab806 Michael Hanselmann
    obj.instances = \
422 fe502d25 Iustin Pop
      outils.ContainerFromDicts(obj.instances, dict, Instance)
423 473ab806 Michael Hanselmann
    obj.nodegroups = \
424 fe502d25 Iustin Pop
      outils.ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
425 fe502d25 Iustin Pop
    obj.networks = outils.ContainerFromDicts(obj.networks, dict, Network)
426 ff9c047c Iustin Pop
    return obj
427 ff9c047c Iustin Pop
428 51cb1581 Luca Bigliardi
  def HasAnyDiskOfType(self, dev_type):
429 51cb1581 Luca Bigliardi
    """Check if in there is at disk of the given type in the configuration.
430 51cb1581 Luca Bigliardi

431 51cb1581 Luca Bigliardi
    @type dev_type: L{constants.LDS_BLOCK}
432 51cb1581 Luca Bigliardi
    @param dev_type: the type to look for
433 51cb1581 Luca Bigliardi
    @rtype: boolean
434 51cb1581 Luca Bigliardi
    @return: boolean indicating if a disk of the given type was found or not
435 51cb1581 Luca Bigliardi

436 51cb1581 Luca Bigliardi
    """
437 51cb1581 Luca Bigliardi
    for instance in self.instances.values():
438 51cb1581 Luca Bigliardi
      for disk in instance.disks:
439 51cb1581 Luca Bigliardi
        if disk.IsBasedOnDiskType(dev_type):
440 51cb1581 Luca Bigliardi
          return True
441 51cb1581 Luca Bigliardi
    return False
442 51cb1581 Luca Bigliardi
443 90d726a8 Iustin Pop
  def UpgradeConfig(self):
444 90d726a8 Iustin Pop
    """Fill defaults for missing configuration values.
445 90d726a8 Iustin Pop

446 90d726a8 Iustin Pop
    """
447 90d726a8 Iustin Pop
    self.cluster.UpgradeConfig()
448 90d726a8 Iustin Pop
    for node in self.nodes.values():
449 90d726a8 Iustin Pop
      node.UpgradeConfig()
450 90d726a8 Iustin Pop
    for instance in self.instances.values():
451 90d726a8 Iustin Pop
      instance.UpgradeConfig()
452 3df43542 Guido Trotter
    if self.nodegroups is None:
453 3df43542 Guido Trotter
      self.nodegroups = {}
454 3df43542 Guido Trotter
    for nodegroup in self.nodegroups.values():
455 3df43542 Guido Trotter
      nodegroup.UpgradeConfig()
456 ee2f0ed4 Luca Bigliardi
    if self.cluster.drbd_usermode_helper is None:
457 ee2f0ed4 Luca Bigliardi
      # To decide if we set an helper let's check if at least one instance has
458 ee2f0ed4 Luca Bigliardi
      # a DRBD disk. This does not cover all the possible scenarios but it
459 ee2f0ed4 Luca Bigliardi
      # gives a good approximation.
460 ee2f0ed4 Luca Bigliardi
      if self.HasAnyDiskOfType(constants.LD_DRBD8):
461 ee2f0ed4 Luca Bigliardi
        self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
462 eaa4c57c Dimitris Aragiorgis
    if self.networks is None:
463 eaa4c57c Dimitris Aragiorgis
      self.networks = {}
464 ee9516c8 Guido Trotter
    for network in self.networks.values():
465 ee9516c8 Guido Trotter
      network.UpgradeConfig()
466 90d726a8 Iustin Pop
467 a8083063 Iustin Pop
468 a8083063 Iustin Pop
class NIC(ConfigObject):
469 a8083063 Iustin Pop
  """Config object representing a network card."""
470 cbe4a0a5 Dimitris Aragiorgis
  __slots__ = ["mac", "ip", "network", "nicparams", "netinfo"]
471 a8083063 Iustin Pop
472 255e19d4 Guido Trotter
  @classmethod
473 255e19d4 Guido Trotter
  def CheckParameterSyntax(cls, nicparams):
474 255e19d4 Guido Trotter
    """Check the given parameters for validity.
475 255e19d4 Guido Trotter

476 255e19d4 Guido Trotter
    @type nicparams:  dict
477 255e19d4 Guido Trotter
    @param nicparams: dictionary with parameter names/value
478 255e19d4 Guido Trotter
    @raise errors.ConfigurationError: when a parameter is not valid
479 255e19d4 Guido Trotter

480 255e19d4 Guido Trotter
    """
481 53258324 Michael Hanselmann
    mode = nicparams[constants.NIC_MODE]
482 53258324 Michael Hanselmann
    if (mode not in constants.NIC_VALID_MODES and
483 53258324 Michael Hanselmann
        mode != constants.VALUE_AUTO):
484 53258324 Michael Hanselmann
      raise errors.ConfigurationError("Invalid NIC mode '%s'" % mode)
485 255e19d4 Guido Trotter
486 53258324 Michael Hanselmann
    if (mode == constants.NIC_MODE_BRIDGED and
487 255e19d4 Guido Trotter
        not nicparams[constants.NIC_LINK]):
488 53258324 Michael Hanselmann
      raise errors.ConfigurationError("Missing bridged NIC link")
489 255e19d4 Guido Trotter
490 a8083063 Iustin Pop
491 a8083063 Iustin Pop
class Disk(ConfigObject):
492 a8083063 Iustin Pop
  """Config object representing a block device."""
493 a8083063 Iustin Pop
  __slots__ = ["dev_type", "logical_id", "physical_id",
494 bc5d0215 Andrea Spadaccini
               "children", "iv_name", "size", "mode", "params"]
495 a8083063 Iustin Pop
496 a8083063 Iustin Pop
  def CreateOnSecondary(self):
497 a8083063 Iustin Pop
    """Test if this device needs to be created on a secondary node."""
498 00fb8246 Michael Hanselmann
    return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
499 a8083063 Iustin Pop
500 a8083063 Iustin Pop
  def AssembleOnSecondary(self):
501 a8083063 Iustin Pop
    """Test if this device needs to be assembled on a secondary node."""
502 00fb8246 Michael Hanselmann
    return self.dev_type in (constants.LD_DRBD8, constants.LD_LV)
503 a8083063 Iustin Pop
504 a8083063 Iustin Pop
  def OpenOnSecondary(self):
505 a8083063 Iustin Pop
    """Test if this device needs to be opened on a secondary node."""
506 fe96220b Iustin Pop
    return self.dev_type in (constants.LD_LV,)
507 a8083063 Iustin Pop
508 222f2dd5 Iustin Pop
  def StaticDevPath(self):
509 222f2dd5 Iustin Pop
    """Return the device path if this device type has a static one.
510 222f2dd5 Iustin Pop

511 222f2dd5 Iustin Pop
    Some devices (LVM for example) live always at the same /dev/ path,
512 222f2dd5 Iustin Pop
    irrespective of their status. For such devices, we return this
513 222f2dd5 Iustin Pop
    path, for others we return None.
514 222f2dd5 Iustin Pop

515 e51db2a6 Iustin Pop
    @warning: The path returned is not a normalized pathname; callers
516 e51db2a6 Iustin Pop
        should check that it is a valid path.
517 e51db2a6 Iustin Pop

518 222f2dd5 Iustin Pop
    """
519 222f2dd5 Iustin Pop
    if self.dev_type == constants.LD_LV:
520 222f2dd5 Iustin Pop
      return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
521 b6135bbc Apollon Oikonomopoulos
    elif self.dev_type == constants.LD_BLOCKDEV:
522 b6135bbc Apollon Oikonomopoulos
      return self.logical_id[1]
523 7181fba0 Constantinos Venetsanopoulos
    elif self.dev_type == constants.LD_RBD:
524 7181fba0 Constantinos Venetsanopoulos
      return "/dev/%s/%s" % (self.logical_id[0], self.logical_id[1])
525 222f2dd5 Iustin Pop
    return None
526 222f2dd5 Iustin Pop
527 fc1dc9d7 Iustin Pop
  def ChildrenNeeded(self):
528 fc1dc9d7 Iustin Pop
    """Compute the needed number of children for activation.
529 fc1dc9d7 Iustin Pop

530 fc1dc9d7 Iustin Pop
    This method will return either -1 (all children) or a positive
531 fc1dc9d7 Iustin Pop
    number denoting the minimum number of children needed for
532 fc1dc9d7 Iustin Pop
    activation (only mirrored devices will usually return >=0).
533 fc1dc9d7 Iustin Pop

534 fc1dc9d7 Iustin Pop
    Currently, only DRBD8 supports diskless activation (therefore we
535 fc1dc9d7 Iustin Pop
    return 0), for all other we keep the previous semantics and return
536 fc1dc9d7 Iustin Pop
    -1.
537 fc1dc9d7 Iustin Pop

538 fc1dc9d7 Iustin Pop
    """
539 fc1dc9d7 Iustin Pop
    if self.dev_type == constants.LD_DRBD8:
540 fc1dc9d7 Iustin Pop
      return 0
541 fc1dc9d7 Iustin Pop
    return -1
542 fc1dc9d7 Iustin Pop
543 51cb1581 Luca Bigliardi
  def IsBasedOnDiskType(self, dev_type):
544 51cb1581 Luca Bigliardi
    """Check if the disk or its children are based on the given type.
545 51cb1581 Luca Bigliardi

546 51cb1581 Luca Bigliardi
    @type dev_type: L{constants.LDS_BLOCK}
547 51cb1581 Luca Bigliardi
    @param dev_type: the type to look for
548 51cb1581 Luca Bigliardi
    @rtype: boolean
549 51cb1581 Luca Bigliardi
    @return: boolean indicating if a device of the given type was found or not
550 51cb1581 Luca Bigliardi

551 51cb1581 Luca Bigliardi
    """
552 51cb1581 Luca Bigliardi
    if self.children:
553 51cb1581 Luca Bigliardi
      for child in self.children:
554 51cb1581 Luca Bigliardi
        if child.IsBasedOnDiskType(dev_type):
555 51cb1581 Luca Bigliardi
          return True
556 51cb1581 Luca Bigliardi
    return self.dev_type == dev_type
557 51cb1581 Luca Bigliardi
558 a8083063 Iustin Pop
  def GetNodes(self, node):
559 a8083063 Iustin Pop
    """This function returns the nodes this device lives on.
560 a8083063 Iustin Pop

561 a8083063 Iustin Pop
    Given the node on which the parent of the device lives on (or, in
562 a8083063 Iustin Pop
    case of a top-level device, the primary node of the devices'
563 a8083063 Iustin Pop
    instance), this function will return a list of nodes on which this
564 a8083063 Iustin Pop
    devices needs to (or can) be assembled.
565 a8083063 Iustin Pop

566 a8083063 Iustin Pop
    """
567 b6135bbc Apollon Oikonomopoulos
    if self.dev_type in [constants.LD_LV, constants.LD_FILE,
568 376631d1 Constantinos Venetsanopoulos
                         constants.LD_BLOCKDEV, constants.LD_RBD,
569 376631d1 Constantinos Venetsanopoulos
                         constants.LD_EXT]:
570 a8083063 Iustin Pop
      result = [node]
571 a1f445d3 Iustin Pop
    elif self.dev_type in constants.LDS_DRBD:
572 a8083063 Iustin Pop
      result = [self.logical_id[0], self.logical_id[1]]
573 a8083063 Iustin Pop
      if node not in result:
574 3ecf6786 Iustin Pop
        raise errors.ConfigurationError("DRBD device passed unknown node")
575 a8083063 Iustin Pop
    else:
576 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Unhandled device type %s" % self.dev_type)
577 a8083063 Iustin Pop
    return result
578 a8083063 Iustin Pop
579 a8083063 Iustin Pop
  def ComputeNodeTree(self, parent_node):
580 a8083063 Iustin Pop
    """Compute the node/disk tree for this disk and its children.
581 a8083063 Iustin Pop

582 a8083063 Iustin Pop
    This method, given the node on which the parent disk lives, will
583 a8083063 Iustin Pop
    return the list of all (node, disk) pairs which describe the disk
584 abdf0113 Iustin Pop
    tree in the most compact way. For example, a drbd/lvm stack
585 abdf0113 Iustin Pop
    will be returned as (primary_node, drbd) and (secondary_node, drbd)
586 abdf0113 Iustin Pop
    which represents all the top-level devices on the nodes.
587 a8083063 Iustin Pop

588 a8083063 Iustin Pop
    """
589 a8083063 Iustin Pop
    my_nodes = self.GetNodes(parent_node)
590 a8083063 Iustin Pop
    result = [(node, self) for node in my_nodes]
591 a8083063 Iustin Pop
    if not self.children:
592 a8083063 Iustin Pop
      # leaf device
593 a8083063 Iustin Pop
      return result
594 a8083063 Iustin Pop
    for node in my_nodes:
595 a8083063 Iustin Pop
      for child in self.children:
596 a8083063 Iustin Pop
        child_result = child.ComputeNodeTree(node)
597 a8083063 Iustin Pop
        if len(child_result) == 1:
598 a8083063 Iustin Pop
          # child (and all its descendants) is simple, doesn't split
599 a8083063 Iustin Pop
          # over multiple hosts, so we don't need to describe it, our
600 a8083063 Iustin Pop
          # own entry for this node describes it completely
601 a8083063 Iustin Pop
          continue
602 a8083063 Iustin Pop
        else:
603 a8083063 Iustin Pop
          # check if child nodes differ from my nodes; note that
604 a8083063 Iustin Pop
          # subdisk can differ from the child itself, and be instead
605 a8083063 Iustin Pop
          # one of its descendants
606 a8083063 Iustin Pop
          for subnode, subdisk in child_result:
607 a8083063 Iustin Pop
            if subnode not in my_nodes:
608 a8083063 Iustin Pop
              result.append((subnode, subdisk))
609 a8083063 Iustin Pop
            # otherwise child is under our own node, so we ignore this
610 a8083063 Iustin Pop
            # entry (but probably the other results in the list will
611 a8083063 Iustin Pop
            # be different)
612 a8083063 Iustin Pop
    return result
613 a8083063 Iustin Pop
614 6d33a6eb Iustin Pop
  def ComputeGrowth(self, amount):
615 6d33a6eb Iustin Pop
    """Compute the per-VG growth requirements.
616 6d33a6eb Iustin Pop

617 6d33a6eb Iustin Pop
    This only works for VG-based disks.
618 6d33a6eb Iustin Pop

619 6d33a6eb Iustin Pop
    @type amount: integer
620 6d33a6eb Iustin Pop
    @param amount: the desired increase in (user-visible) disk space
621 6d33a6eb Iustin Pop
    @rtype: dict
622 6d33a6eb Iustin Pop
    @return: a dictionary of volume-groups and the required size
623 6d33a6eb Iustin Pop

624 6d33a6eb Iustin Pop
    """
625 6d33a6eb Iustin Pop
    if self.dev_type == constants.LD_LV:
626 6d33a6eb Iustin Pop
      return {self.logical_id[0]: amount}
627 6d33a6eb Iustin Pop
    elif self.dev_type == constants.LD_DRBD8:
628 6d33a6eb Iustin Pop
      if self.children:
629 6d33a6eb Iustin Pop
        return self.children[0].ComputeGrowth(amount)
630 6d33a6eb Iustin Pop
      else:
631 6d33a6eb Iustin Pop
        return {}
632 6d33a6eb Iustin Pop
    else:
633 6d33a6eb Iustin Pop
      # Other disk types do not require VG space
634 6d33a6eb Iustin Pop
      return {}
635 6d33a6eb Iustin Pop
636 acec9d51 Iustin Pop
  def RecordGrow(self, amount):
637 acec9d51 Iustin Pop
    """Update the size of this disk after growth.
638 acec9d51 Iustin Pop

639 acec9d51 Iustin Pop
    This method recurses over the disks's children and updates their
640 acec9d51 Iustin Pop
    size correspondigly. The method needs to be kept in sync with the
641 acec9d51 Iustin Pop
    actual algorithms from bdev.
642 acec9d51 Iustin Pop

643 acec9d51 Iustin Pop
    """
644 7181fba0 Constantinos Venetsanopoulos
    if self.dev_type in (constants.LD_LV, constants.LD_FILE,
645 376631d1 Constantinos Venetsanopoulos
                         constants.LD_RBD, constants.LD_EXT):
646 acec9d51 Iustin Pop
      self.size += amount
647 acec9d51 Iustin Pop
    elif self.dev_type == constants.LD_DRBD8:
648 acec9d51 Iustin Pop
      if self.children:
649 acec9d51 Iustin Pop
        self.children[0].RecordGrow(amount)
650 acec9d51 Iustin Pop
      self.size += amount
651 acec9d51 Iustin Pop
    else:
652 acec9d51 Iustin Pop
      raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
653 acec9d51 Iustin Pop
                                   " disk type %s" % self.dev_type)
654 acec9d51 Iustin Pop
655 735e1318 Michael Hanselmann
  def Update(self, size=None, mode=None):
656 735e1318 Michael Hanselmann
    """Apply changes to size and mode.
657 735e1318 Michael Hanselmann

658 735e1318 Michael Hanselmann
    """
659 735e1318 Michael Hanselmann
    if self.dev_type == constants.LD_DRBD8:
660 735e1318 Michael Hanselmann
      if self.children:
661 735e1318 Michael Hanselmann
        self.children[0].Update(size=size, mode=mode)
662 735e1318 Michael Hanselmann
    else:
663 735e1318 Michael Hanselmann
      assert not self.children
664 735e1318 Michael Hanselmann
665 735e1318 Michael Hanselmann
    if size is not None:
666 735e1318 Michael Hanselmann
      self.size = size
667 735e1318 Michael Hanselmann
    if mode is not None:
668 735e1318 Michael Hanselmann
      self.mode = mode
669 735e1318 Michael Hanselmann
670 a805ec18 Iustin Pop
  def UnsetSize(self):
671 a805ec18 Iustin Pop
    """Sets recursively the size to zero for the disk and its children.
672 a805ec18 Iustin Pop

673 a805ec18 Iustin Pop
    """
674 a805ec18 Iustin Pop
    if self.children:
675 a805ec18 Iustin Pop
      for child in self.children:
676 a805ec18 Iustin Pop
        child.UnsetSize()
677 a805ec18 Iustin Pop
    self.size = 0
678 a805ec18 Iustin Pop
679 0402302c Iustin Pop
  def SetPhysicalID(self, target_node, nodes_ip):
680 0402302c Iustin Pop
    """Convert the logical ID to the physical ID.
681 0402302c Iustin Pop

682 0402302c Iustin Pop
    This is used only for drbd, which needs ip/port configuration.
683 0402302c Iustin Pop

684 0402302c Iustin Pop
    The routine descends down and updates its children also, because
685 0402302c Iustin Pop
    this helps when the only the top device is passed to the remote
686 0402302c Iustin Pop
    node.
687 0402302c Iustin Pop

688 0402302c Iustin Pop
    Arguments:
689 0402302c Iustin Pop
      - target_node: the node we wish to configure for
690 0402302c Iustin Pop
      - nodes_ip: a mapping of node name to ip
691 0402302c Iustin Pop

692 0402302c Iustin Pop
    The target_node must exist in in nodes_ip, and must be one of the
693 0402302c Iustin Pop
    nodes in the logical ID for each of the DRBD devices encountered
694 0402302c Iustin Pop
    in the disk tree.
695 0402302c Iustin Pop

696 0402302c Iustin Pop
    """
697 0402302c Iustin Pop
    if self.children:
698 0402302c Iustin Pop
      for child in self.children:
699 0402302c Iustin Pop
        child.SetPhysicalID(target_node, nodes_ip)
700 0402302c Iustin Pop
701 0402302c Iustin Pop
    if self.logical_id is None and self.physical_id is not None:
702 0402302c Iustin Pop
      return
703 0402302c Iustin Pop
    if self.dev_type in constants.LDS_DRBD:
704 f9518d38 Iustin Pop
      pnode, snode, port, pminor, sminor, secret = self.logical_id
705 0402302c Iustin Pop
      if target_node not in (pnode, snode):
706 0402302c Iustin Pop
        raise errors.ConfigurationError("DRBD device not knowing node %s" %
707 0402302c Iustin Pop
                                        target_node)
708 0402302c Iustin Pop
      pnode_ip = nodes_ip.get(pnode, None)
709 0402302c Iustin Pop
      snode_ip = nodes_ip.get(snode, None)
710 0402302c Iustin Pop
      if pnode_ip is None or snode_ip is None:
711 0402302c Iustin Pop
        raise errors.ConfigurationError("Can't find primary or secondary node"
712 0402302c Iustin Pop
                                        " for %s" % str(self))
713 ffa1c0dc Iustin Pop
      p_data = (pnode_ip, port)
714 ffa1c0dc Iustin Pop
      s_data = (snode_ip, port)
715 0402302c Iustin Pop
      if pnode == target_node:
716 f9518d38 Iustin Pop
        self.physical_id = p_data + s_data + (pminor, secret)
717 0402302c Iustin Pop
      else: # it must be secondary, we tested above
718 f9518d38 Iustin Pop
        self.physical_id = s_data + p_data + (sminor, secret)
719 0402302c Iustin Pop
    else:
720 0402302c Iustin Pop
      self.physical_id = self.logical_id
721 0402302c Iustin Pop
    return
722 0402302c Iustin Pop
723 ff9c047c Iustin Pop
  def ToDict(self):
724 ff9c047c Iustin Pop
    """Disk-specific conversion to standard python types.
725 ff9c047c Iustin Pop

726 ff9c047c Iustin Pop
    This replaces the children lists of objects with lists of
727 ff9c047c Iustin Pop
    standard python types.
728 ff9c047c Iustin Pop

729 ff9c047c Iustin Pop
    """
730 ff9c047c Iustin Pop
    bo = super(Disk, self).ToDict()
731 ff9c047c Iustin Pop
732 ff9c047c Iustin Pop
    for attr in ("children",):
733 ff9c047c Iustin Pop
      alist = bo.get(attr, None)
734 ff9c047c Iustin Pop
      if alist:
735 fe502d25 Iustin Pop
        bo[attr] = outils.ContainerToDicts(alist)
736 ff9c047c Iustin Pop
    return bo
737 ff9c047c Iustin Pop
738 ff9c047c Iustin Pop
  @classmethod
739 ff9c047c Iustin Pop
  def FromDict(cls, val):
740 ff9c047c Iustin Pop
    """Custom function for Disks
741 ff9c047c Iustin Pop

742 ff9c047c Iustin Pop
    """
743 ff9c047c Iustin Pop
    obj = super(Disk, cls).FromDict(val)
744 ff9c047c Iustin Pop
    if obj.children:
745 fe502d25 Iustin Pop
      obj.children = outils.ContainerFromDicts(obj.children, list, Disk)
746 ff9c047c Iustin Pop
    if obj.logical_id and isinstance(obj.logical_id, list):
747 ff9c047c Iustin Pop
      obj.logical_id = tuple(obj.logical_id)
748 ff9c047c Iustin Pop
    if obj.physical_id and isinstance(obj.physical_id, list):
749 ff9c047c Iustin Pop
      obj.physical_id = tuple(obj.physical_id)
750 f9518d38 Iustin Pop
    if obj.dev_type in constants.LDS_DRBD:
751 f9518d38 Iustin Pop
      # we need a tuple of length six here
752 f9518d38 Iustin Pop
      if len(obj.logical_id) < 6:
753 f9518d38 Iustin Pop
        obj.logical_id += (None,) * (6 - len(obj.logical_id))
754 ff9c047c Iustin Pop
    return obj
755 ff9c047c Iustin Pop
756 65a15336 Iustin Pop
  def __str__(self):
757 65a15336 Iustin Pop
    """Custom str() formatter for disks.
758 65a15336 Iustin Pop

759 65a15336 Iustin Pop
    """
760 65a15336 Iustin Pop
    if self.dev_type == constants.LD_LV:
761 e687ec01 Michael Hanselmann
      val = "<LogicalVolume(/dev/%s/%s" % self.logical_id
762 65a15336 Iustin Pop
    elif self.dev_type in constants.LDS_DRBD:
763 89f28b76 Iustin Pop
      node_a, node_b, port, minor_a, minor_b = self.logical_id[:5]
764 00fb8246 Michael Hanselmann
      val = "<DRBD8("
765 073ca59e Iustin Pop
      if self.physical_id is None:
766 073ca59e Iustin Pop
        phy = "unconfigured"
767 073ca59e Iustin Pop
      else:
768 073ca59e Iustin Pop
        phy = ("configured as %s:%s %s:%s" %
769 25a915d0 Iustin Pop
               (self.physical_id[0], self.physical_id[1],
770 25a915d0 Iustin Pop
                self.physical_id[2], self.physical_id[3]))
771 073ca59e Iustin Pop
772 89f28b76 Iustin Pop
      val += ("hosts=%s/%d-%s/%d, port=%s, %s, " %
773 89f28b76 Iustin Pop
              (node_a, minor_a, node_b, minor_b, port, phy))
774 65a15336 Iustin Pop
      if self.children and self.children.count(None) == 0:
775 65a15336 Iustin Pop
        val += "backend=%s, metadev=%s" % (self.children[0], self.children[1])
776 65a15336 Iustin Pop
      else:
777 65a15336 Iustin Pop
        val += "no local storage"
778 65a15336 Iustin Pop
    else:
779 65a15336 Iustin Pop
      val = ("<Disk(type=%s, logical_id=%s, physical_id=%s, children=%s" %
780 65a15336 Iustin Pop
             (self.dev_type, self.logical_id, self.physical_id, self.children))
781 65a15336 Iustin Pop
    if self.iv_name is None:
782 65a15336 Iustin Pop
      val += ", not visible"
783 65a15336 Iustin Pop
    else:
784 65a15336 Iustin Pop
      val += ", visible as /dev/%s" % self.iv_name
785 fd965830 Iustin Pop
    if isinstance(self.size, int):
786 fd965830 Iustin Pop
      val += ", size=%dm)>" % self.size
787 fd965830 Iustin Pop
    else:
788 fd965830 Iustin Pop
      val += ", size='%s')>" % (self.size,)
789 65a15336 Iustin Pop
    return val
790 65a15336 Iustin Pop
791 332d0e37 Iustin Pop
  def Verify(self):
792 332d0e37 Iustin Pop
    """Checks that this disk is correctly configured.
793 332d0e37 Iustin Pop

794 332d0e37 Iustin Pop
    """
795 7c4d6c7b Michael Hanselmann
    all_errors = []
796 332d0e37 Iustin Pop
    if self.mode not in constants.DISK_ACCESS_SET:
797 7c4d6c7b Michael Hanselmann
      all_errors.append("Disk access mode '%s' is invalid" % (self.mode, ))
798 7c4d6c7b Michael Hanselmann
    return all_errors
799 332d0e37 Iustin Pop
800 90d726a8 Iustin Pop
  def UpgradeConfig(self):
801 90d726a8 Iustin Pop
    """Fill defaults for missing configuration values.
802 90d726a8 Iustin Pop

803 90d726a8 Iustin Pop
    """
804 90d726a8 Iustin Pop
    if self.children:
805 90d726a8 Iustin Pop
      for child in self.children:
806 90d726a8 Iustin Pop
        child.UpgradeConfig()
807 bc5d0215 Andrea Spadaccini
808 cce46164 René Nussbaumer
    # FIXME: Make this configurable in Ganeti 2.7
809 5dbee5ea Iustin Pop
    self.params = {}
810 90d726a8 Iustin Pop
    # add here config upgrade for this disk
811 90d726a8 Iustin Pop
812 cd46491f René Nussbaumer
  @staticmethod
813 cd46491f René Nussbaumer
  def ComputeLDParams(disk_template, disk_params):
814 cd46491f René Nussbaumer
    """Computes Logical Disk parameters from Disk Template parameters.
815 cd46491f René Nussbaumer

816 cd46491f René Nussbaumer
    @type disk_template: string
817 cd46491f René Nussbaumer
    @param disk_template: disk template, one of L{constants.DISK_TEMPLATES}
818 cd46491f René Nussbaumer
    @type disk_params: dict
819 cd46491f René Nussbaumer
    @param disk_params: disk template parameters;
820 cd46491f René Nussbaumer
                        dict(template_name -> parameters
821 cd46491f René Nussbaumer
    @rtype: list(dict)
822 cd46491f René Nussbaumer
    @return: a list of dicts, one for each node of the disk hierarchy. Each dict
823 cd46491f René Nussbaumer
      contains the LD parameters of the node. The tree is flattened in-order.
824 cd46491f René Nussbaumer

825 cd46491f René Nussbaumer
    """
826 cd46491f René Nussbaumer
    if disk_template not in constants.DISK_TEMPLATES:
827 cd46491f René Nussbaumer
      raise errors.ProgrammerError("Unknown disk template %s" % disk_template)
828 cd46491f René Nussbaumer
829 cd46491f René Nussbaumer
    assert disk_template in disk_params
830 cd46491f René Nussbaumer
831 cd46491f René Nussbaumer
    result = list()
832 cd46491f René Nussbaumer
    dt_params = disk_params[disk_template]
833 cd46491f René Nussbaumer
    if disk_template == constants.DT_DRBD8:
834 52f93ffd Michael Hanselmann
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_DRBD8], {
835 cd46491f René Nussbaumer
        constants.LDP_RESYNC_RATE: dt_params[constants.DRBD_RESYNC_RATE],
836 cd46491f René Nussbaumer
        constants.LDP_BARRIERS: dt_params[constants.DRBD_DISK_BARRIERS],
837 cd46491f René Nussbaumer
        constants.LDP_NO_META_FLUSH: dt_params[constants.DRBD_META_BARRIERS],
838 cd46491f René Nussbaumer
        constants.LDP_DEFAULT_METAVG: dt_params[constants.DRBD_DEFAULT_METAVG],
839 cd46491f René Nussbaumer
        constants.LDP_DISK_CUSTOM: dt_params[constants.DRBD_DISK_CUSTOM],
840 cd46491f René Nussbaumer
        constants.LDP_NET_CUSTOM: dt_params[constants.DRBD_NET_CUSTOM],
841 cd46491f René Nussbaumer
        constants.LDP_DYNAMIC_RESYNC: dt_params[constants.DRBD_DYNAMIC_RESYNC],
842 cd46491f René Nussbaumer
        constants.LDP_PLAN_AHEAD: dt_params[constants.DRBD_PLAN_AHEAD],
843 cd46491f René Nussbaumer
        constants.LDP_FILL_TARGET: dt_params[constants.DRBD_FILL_TARGET],
844 cd46491f René Nussbaumer
        constants.LDP_DELAY_TARGET: dt_params[constants.DRBD_DELAY_TARGET],
845 cd46491f René Nussbaumer
        constants.LDP_MAX_RATE: dt_params[constants.DRBD_MAX_RATE],
846 cd46491f René Nussbaumer
        constants.LDP_MIN_RATE: dt_params[constants.DRBD_MIN_RATE],
847 52f93ffd Michael Hanselmann
        }))
848 cd46491f René Nussbaumer
849 cd46491f René Nussbaumer
      # data LV
850 52f93ffd Michael Hanselmann
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
851 cd46491f René Nussbaumer
        constants.LDP_STRIPES: dt_params[constants.DRBD_DATA_STRIPES],
852 52f93ffd Michael Hanselmann
        }))
853 cd46491f René Nussbaumer
854 cd46491f René Nussbaumer
      # metadata LV
855 52f93ffd Michael Hanselmann
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
856 cd46491f René Nussbaumer
        constants.LDP_STRIPES: dt_params[constants.DRBD_META_STRIPES],
857 52f93ffd Michael Hanselmann
        }))
858 52f93ffd Michael Hanselmann
859 52f93ffd Michael Hanselmann
    elif disk_template in (constants.DT_FILE, constants.DT_SHARED_FILE):
860 cd46491f René Nussbaumer
      result.append(constants.DISK_LD_DEFAULTS[constants.LD_FILE])
861 cd46491f René Nussbaumer
862 cd46491f René Nussbaumer
    elif disk_template == constants.DT_PLAIN:
863 52f93ffd Michael Hanselmann
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_LV], {
864 cd46491f René Nussbaumer
        constants.LDP_STRIPES: dt_params[constants.LV_STRIPES],
865 52f93ffd Michael Hanselmann
        }))
866 cd46491f René Nussbaumer
867 cd46491f René Nussbaumer
    elif disk_template == constants.DT_BLOCK:
868 cd46491f René Nussbaumer
      result.append(constants.DISK_LD_DEFAULTS[constants.LD_BLOCKDEV])
869 cd46491f René Nussbaumer
870 cd46491f René Nussbaumer
    elif disk_template == constants.DT_RBD:
871 52f93ffd Michael Hanselmann
      result.append(FillDict(constants.DISK_LD_DEFAULTS[constants.LD_RBD], {
872 3c286190 Dimitris Aragiorgis
        constants.LDP_POOL: dt_params[constants.RBD_POOL],
873 52f93ffd Michael Hanselmann
        }))
874 cd46491f René Nussbaumer
875 938adc87 Constantinos Venetsanopoulos
    elif disk_template == constants.DT_EXT:
876 938adc87 Constantinos Venetsanopoulos
      result.append(constants.DISK_LD_DEFAULTS[constants.LD_EXT])
877 938adc87 Constantinos Venetsanopoulos
878 cd46491f René Nussbaumer
    return result
879 cd46491f René Nussbaumer
880 a8083063 Iustin Pop
881 918eb80b Agata Murawska
class InstancePolicy(ConfigObject):
882 ffa339ca Iustin Pop
  """Config object representing instance policy limits dictionary.
883 918eb80b Agata Murawska

884 ffa339ca Iustin Pop

885 ffa339ca Iustin Pop
  Note that this object is not actually used in the config, it's just
886 ffa339ca Iustin Pop
  used as a placeholder for a few functions.
887 ffa339ca Iustin Pop

888 ffa339ca Iustin Pop
  """
889 918eb80b Agata Murawska
  @classmethod
890 8b057218 René Nussbaumer
  def CheckParameterSyntax(cls, ipolicy, check_std):
891 918eb80b Agata Murawska
    """ Check the instance policy for validity.
892 918eb80b Agata Murawska

893 918eb80b Agata Murawska
    """
894 918eb80b Agata Murawska
    for param in constants.ISPECS_PARAMETERS:
895 8b057218 René Nussbaumer
      InstancePolicy.CheckISpecSyntax(ipolicy, param, check_std)
896 d04c9d45 Iustin Pop
    if constants.IPOLICY_DTS in ipolicy:
897 d04c9d45 Iustin Pop
      InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
898 ff6c5e55 Iustin Pop
    for key in constants.IPOLICY_PARAMETERS:
899 ff6c5e55 Iustin Pop
      if key in ipolicy:
900 ff6c5e55 Iustin Pop
        InstancePolicy.CheckParameter(key, ipolicy[key])
901 57dc299a Iustin Pop
    wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
902 57dc299a Iustin Pop
    if wrong_keys:
903 57dc299a Iustin Pop
      raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
904 57dc299a Iustin Pop
                                      utils.CommaJoin(wrong_keys))
905 918eb80b Agata Murawska
906 918eb80b Agata Murawska
  @classmethod
907 8b057218 René Nussbaumer
  def CheckISpecSyntax(cls, ipolicy, name, check_std):
908 918eb80b Agata Murawska
    """Check the instance policy for validity on a given key.
909 918eb80b Agata Murawska

910 918eb80b Agata Murawska
    We check if the instance policy makes sense for a given key, that is
911 918eb80b Agata Murawska
    if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
912 918eb80b Agata Murawska

913 918eb80b Agata Murawska
    @type ipolicy: dict
914 918eb80b Agata Murawska
    @param ipolicy: dictionary with min, max, std specs
915 918eb80b Agata Murawska
    @type name: string
916 918eb80b Agata Murawska
    @param name: what are the limits for
917 8b057218 René Nussbaumer
    @type check_std: bool
918 8b057218 René Nussbaumer
    @param check_std: Whether to check std value or just assume compliance
919 918eb80b Agata Murawska
    @raise errors.ConfigureError: when specs for given name are not valid
920 918eb80b Agata Murawska

921 918eb80b Agata Murawska
    """
922 4f725341 Agata Murawska
    min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
923 8b057218 René Nussbaumer
924 8b057218 René Nussbaumer
    if check_std:
925 8b057218 René Nussbaumer
      std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
926 8b057218 René Nussbaumer
      std_msg = std_v
927 8b057218 René Nussbaumer
    else:
928 8b057218 René Nussbaumer
      std_v = min_v
929 8b057218 René Nussbaumer
      std_msg = "-"
930 8b057218 René Nussbaumer
931 4f725341 Agata Murawska
    max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
932 918eb80b Agata Murawska
    err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
933 918eb80b Agata Murawska
           (name,
934 4f725341 Agata Murawska
            ipolicy[constants.ISPECS_MIN].get(name, "-"),
935 4f725341 Agata Murawska
            ipolicy[constants.ISPECS_MAX].get(name, "-"),
936 8b057218 René Nussbaumer
            std_msg))
937 918eb80b Agata Murawska
    if min_v > std_v or std_v > max_v:
938 918eb80b Agata Murawska
      raise errors.ConfigurationError(err)
939 918eb80b Agata Murawska
940 2cc673a3 Iustin Pop
  @classmethod
941 2cc673a3 Iustin Pop
  def CheckDiskTemplates(cls, disk_templates):
942 2cc673a3 Iustin Pop
    """Checks the disk templates for validity.
943 2cc673a3 Iustin Pop

944 2cc673a3 Iustin Pop
    """
945 ba5c6c6b Bernardo Dal Seno
    if not disk_templates:
946 ba5c6c6b Bernardo Dal Seno
      raise errors.ConfigurationError("Instance policy must contain" +
947 ba5c6c6b Bernardo Dal Seno
                                      " at least one disk template")
948 2cc673a3 Iustin Pop
    wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
949 2cc673a3 Iustin Pop
    if wrong:
950 2cc673a3 Iustin Pop
      raise errors.ConfigurationError("Invalid disk template(s) %s" %
951 2cc673a3 Iustin Pop
                                      utils.CommaJoin(wrong))
952 2cc673a3 Iustin Pop
953 ff6c5e55 Iustin Pop
  @classmethod
954 ff6c5e55 Iustin Pop
  def CheckParameter(cls, key, value):
955 ff6c5e55 Iustin Pop
    """Checks a parameter.
956 ff6c5e55 Iustin Pop

957 ff6c5e55 Iustin Pop
    Currently we expect all parameters to be float values.
958 ff6c5e55 Iustin Pop

959 ff6c5e55 Iustin Pop
    """
960 ff6c5e55 Iustin Pop
    try:
961 ff6c5e55 Iustin Pop
      float(value)
962 ff6c5e55 Iustin Pop
    except (TypeError, ValueError), err:
963 ff6c5e55 Iustin Pop
      raise errors.ConfigurationError("Invalid value for key" " '%s':"
964 ff6c5e55 Iustin Pop
                                      " '%s', error: %s" % (key, value, err))
965 ff6c5e55 Iustin Pop
966 918eb80b Agata Murawska
967 ec29fe40 Iustin Pop
class Instance(TaggableObject):
968 a8083063 Iustin Pop
  """Config object representing an instance."""
969 154b9580 Balazs Lecz
  __slots__ = [
970 a8083063 Iustin Pop
    "name",
971 a8083063 Iustin Pop
    "primary_node",
972 a8083063 Iustin Pop
    "os",
973 e69d05fd Iustin Pop
    "hypervisor",
974 5bf7b5cf Iustin Pop
    "hvparams",
975 5bf7b5cf Iustin Pop
    "beparams",
976 1bdcbbab Iustin Pop
    "osparams",
977 9ca8a7c5 Agata Murawska
    "admin_state",
978 a8083063 Iustin Pop
    "nics",
979 a8083063 Iustin Pop
    "disks",
980 a8083063 Iustin Pop
    "disk_template",
981 58acb49d Alexander Schreiber
    "network_port",
982 be1fa613 Iustin Pop
    "serial_no",
983 e1dcc53a Iustin Pop
    ] + _TIMESTAMPS + _UUID
984 a8083063 Iustin Pop
985 a8083063 Iustin Pop
  def _ComputeSecondaryNodes(self):
986 a8083063 Iustin Pop
    """Compute the list of secondary nodes.
987 a8083063 Iustin Pop

988 cfcc5c6d Iustin Pop
    This is a simple wrapper over _ComputeAllNodes.
989 cfcc5c6d Iustin Pop

990 cfcc5c6d Iustin Pop
    """
991 cfcc5c6d Iustin Pop
    all_nodes = set(self._ComputeAllNodes())
992 cfcc5c6d Iustin Pop
    all_nodes.discard(self.primary_node)
993 cfcc5c6d Iustin Pop
    return tuple(all_nodes)
994 cfcc5c6d Iustin Pop
995 cfcc5c6d Iustin Pop
  secondary_nodes = property(_ComputeSecondaryNodes, None, None,
996 05325a35 Bernardo Dal Seno
                             "List of names of secondary nodes")
997 cfcc5c6d Iustin Pop
998 cfcc5c6d Iustin Pop
  def _ComputeAllNodes(self):
999 cfcc5c6d Iustin Pop
    """Compute the list of all nodes.
1000 cfcc5c6d Iustin Pop

1001 a8083063 Iustin Pop
    Since the data is already there (in the drbd disks), keeping it as
1002 a8083063 Iustin Pop
    a separate normal attribute is redundant and if not properly
1003 a8083063 Iustin Pop
    synchronised can cause problems. Thus it's better to compute it
1004 a8083063 Iustin Pop
    dynamically.
1005 a8083063 Iustin Pop

1006 a8083063 Iustin Pop
    """
1007 cfcc5c6d Iustin Pop
    def _Helper(nodes, device):
1008 cfcc5c6d Iustin Pop
      """Recursively computes nodes given a top device."""
1009 a1f445d3 Iustin Pop
      if device.dev_type in constants.LDS_DRBD:
1010 cfcc5c6d Iustin Pop
        nodea, nodeb = device.logical_id[:2]
1011 cfcc5c6d Iustin Pop
        nodes.add(nodea)
1012 cfcc5c6d Iustin Pop
        nodes.add(nodeb)
1013 a8083063 Iustin Pop
      if device.children:
1014 a8083063 Iustin Pop
        for child in device.children:
1015 cfcc5c6d Iustin Pop
          _Helper(nodes, child)
1016 a8083063 Iustin Pop
1017 cfcc5c6d Iustin Pop
    all_nodes = set()
1018 99c7b2a1 Iustin Pop
    all_nodes.add(self.primary_node)
1019 a8083063 Iustin Pop
    for device in self.disks:
1020 cfcc5c6d Iustin Pop
      _Helper(all_nodes, device)
1021 cfcc5c6d Iustin Pop
    return tuple(all_nodes)
1022 a8083063 Iustin Pop
1023 cfcc5c6d Iustin Pop
  all_nodes = property(_ComputeAllNodes, None, None,
1024 05325a35 Bernardo Dal Seno
                       "List of names of all the nodes of the instance")
1025 a8083063 Iustin Pop
1026 a8083063 Iustin Pop
  def MapLVsByNode(self, lvmap=None, devs=None, node=None):
1027 a8083063 Iustin Pop
    """Provide a mapping of nodes to LVs this instance owns.
1028 a8083063 Iustin Pop

1029 c41eea6e Iustin Pop
    This function figures out what logical volumes should belong on
1030 c41eea6e Iustin Pop
    which nodes, recursing through a device tree.
1031 a8083063 Iustin Pop

1032 c41eea6e Iustin Pop
    @param lvmap: optional dictionary to receive the
1033 c41eea6e Iustin Pop
        'node' : ['lv', ...] data.
1034 a8083063 Iustin Pop

1035 84d7e26b Dmitry Chernyak
    @return: None if lvmap arg is given, otherwise, a dictionary of
1036 84d7e26b Dmitry Chernyak
        the form { 'nodename' : ['volume1', 'volume2', ...], ... };
1037 84d7e26b Dmitry Chernyak
        volumeN is of the form "vg_name/lv_name", compatible with
1038 84d7e26b Dmitry Chernyak
        GetVolumeList()
1039 a8083063 Iustin Pop

1040 a8083063 Iustin Pop
    """
1041 5ae4945a Iustin Pop
    if node is None:
1042 a8083063 Iustin Pop
      node = self.primary_node
1043 a8083063 Iustin Pop
1044 a8083063 Iustin Pop
    if lvmap is None:
1045 e687ec01 Michael Hanselmann
      lvmap = {
1046 e687ec01 Michael Hanselmann
        node: [],
1047 e687ec01 Michael Hanselmann
        }
1048 a8083063 Iustin Pop
      ret = lvmap
1049 a8083063 Iustin Pop
    else:
1050 a8083063 Iustin Pop
      if not node in lvmap:
1051 a8083063 Iustin Pop
        lvmap[node] = []
1052 a8083063 Iustin Pop
      ret = None
1053 a8083063 Iustin Pop
1054 a8083063 Iustin Pop
    if not devs:
1055 a8083063 Iustin Pop
      devs = self.disks
1056 a8083063 Iustin Pop
1057 a8083063 Iustin Pop
    for dev in devs:
1058 fe96220b Iustin Pop
      if dev.dev_type == constants.LD_LV:
1059 e687ec01 Michael Hanselmann
        lvmap[node].append(dev.logical_id[0] + "/" + dev.logical_id[1])
1060 a8083063 Iustin Pop
1061 a1f445d3 Iustin Pop
      elif dev.dev_type in constants.LDS_DRBD:
1062 a8083063 Iustin Pop
        if dev.children:
1063 a8083063 Iustin Pop
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
1064 a8083063 Iustin Pop
          self.MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
1065 a8083063 Iustin Pop
1066 a8083063 Iustin Pop
      elif dev.children:
1067 a8083063 Iustin Pop
        self.MapLVsByNode(lvmap, dev.children, node)
1068 a8083063 Iustin Pop
1069 a8083063 Iustin Pop
    return ret
1070 a8083063 Iustin Pop
1071 ad24e046 Iustin Pop
  def FindDisk(self, idx):
1072 ad24e046 Iustin Pop
    """Find a disk given having a specified index.
1073 644eeef9 Iustin Pop

1074 ad24e046 Iustin Pop
    This is just a wrapper that does validation of the index.
1075 644eeef9 Iustin Pop

1076 ad24e046 Iustin Pop
    @type idx: int
1077 ad24e046 Iustin Pop
    @param idx: the disk index
1078 ad24e046 Iustin Pop
    @rtype: L{Disk}
1079 ad24e046 Iustin Pop
    @return: the corresponding disk
1080 ad24e046 Iustin Pop
    @raise errors.OpPrereqError: when the given index is not valid
1081 644eeef9 Iustin Pop

1082 ad24e046 Iustin Pop
    """
1083 ad24e046 Iustin Pop
    try:
1084 ad24e046 Iustin Pop
      idx = int(idx)
1085 ad24e046 Iustin Pop
      return self.disks[idx]
1086 691744c4 Iustin Pop
    except (TypeError, ValueError), err:
1087 debac808 Iustin Pop
      raise errors.OpPrereqError("Invalid disk index: '%s'" % str(err),
1088 debac808 Iustin Pop
                                 errors.ECODE_INVAL)
1089 ad24e046 Iustin Pop
    except IndexError:
1090 ad24e046 Iustin Pop
      raise errors.OpPrereqError("Invalid disk index: %d (instace has disks"
1091 daa55b04 Michael Hanselmann
                                 " 0 to %d" % (idx, len(self.disks) - 1),
1092 debac808 Iustin Pop
                                 errors.ECODE_INVAL)
1093 644eeef9 Iustin Pop
1094 ff9c047c Iustin Pop
  def ToDict(self):
1095 ff9c047c Iustin Pop
    """Instance-specific conversion to standard python types.
1096 ff9c047c Iustin Pop

1097 ff9c047c Iustin Pop
    This replaces the children lists of objects with lists of standard
1098 ff9c047c Iustin Pop
    python types.
1099 ff9c047c Iustin Pop

1100 ff9c047c Iustin Pop
    """
1101 ff9c047c Iustin Pop
    bo = super(Instance, self).ToDict()
1102 ff9c047c Iustin Pop
1103 ff9c047c Iustin Pop
    for attr in "nics", "disks":
1104 ff9c047c Iustin Pop
      alist = bo.get(attr, None)
1105 ff9c047c Iustin Pop
      if alist:
1106 fe502d25 Iustin Pop
        nlist = outils.ContainerToDicts(alist)
1107 ff9c047c Iustin Pop
      else:
1108 ff9c047c Iustin Pop
        nlist = []
1109 ff9c047c Iustin Pop
      bo[attr] = nlist
1110 ff9c047c Iustin Pop
    return bo
1111 ff9c047c Iustin Pop
1112 ff9c047c Iustin Pop
  @classmethod
1113 ff9c047c Iustin Pop
  def FromDict(cls, val):
1114 ff9c047c Iustin Pop
    """Custom function for instances.
1115 ff9c047c Iustin Pop

1116 ff9c047c Iustin Pop
    """
1117 9ca8a7c5 Agata Murawska
    if "admin_state" not in val:
1118 9ca8a7c5 Agata Murawska
      if val.get("admin_up", False):
1119 9ca8a7c5 Agata Murawska
        val["admin_state"] = constants.ADMINST_UP
1120 9ca8a7c5 Agata Murawska
      else:
1121 9ca8a7c5 Agata Murawska
        val["admin_state"] = constants.ADMINST_DOWN
1122 9ca8a7c5 Agata Murawska
    if "admin_up" in val:
1123 9ca8a7c5 Agata Murawska
      del val["admin_up"]
1124 ff9c047c Iustin Pop
    obj = super(Instance, cls).FromDict(val)
1125 fe502d25 Iustin Pop
    obj.nics = outils.ContainerFromDicts(obj.nics, list, NIC)
1126 fe502d25 Iustin Pop
    obj.disks = outils.ContainerFromDicts(obj.disks, list, Disk)
1127 ff9c047c Iustin Pop
    return obj
1128 ff9c047c Iustin Pop
1129 90d726a8 Iustin Pop
  def UpgradeConfig(self):
1130 90d726a8 Iustin Pop
    """Fill defaults for missing configuration values.
1131 90d726a8 Iustin Pop

1132 90d726a8 Iustin Pop
    """
1133 90d726a8 Iustin Pop
    for nic in self.nics:
1134 90d726a8 Iustin Pop
      nic.UpgradeConfig()
1135 90d726a8 Iustin Pop
    for disk in self.disks:
1136 90d726a8 Iustin Pop
      disk.UpgradeConfig()
1137 7736a5f2 Iustin Pop
    if self.hvparams:
1138 7736a5f2 Iustin Pop
      for key in constants.HVC_GLOBALS:
1139 7736a5f2 Iustin Pop
        try:
1140 7736a5f2 Iustin Pop
          del self.hvparams[key]
1141 7736a5f2 Iustin Pop
        except KeyError:
1142 7736a5f2 Iustin Pop
          pass
1143 1bdcbbab Iustin Pop
    if self.osparams is None:
1144 1bdcbbab Iustin Pop
      self.osparams = {}
1145 8c72ab2b Guido Trotter
    UpgradeBeParams(self.beparams)
1146 90d726a8 Iustin Pop
1147 a8083063 Iustin Pop
1148 a8083063 Iustin Pop
class OS(ConfigObject):
1149 b41b3516 Iustin Pop
  """Config object representing an operating system.
1150 b41b3516 Iustin Pop

1151 b41b3516 Iustin Pop
  @type supported_parameters: list
1152 b41b3516 Iustin Pop
  @ivar supported_parameters: a list of tuples, name and description,
1153 b41b3516 Iustin Pop
      containing the supported parameters by this OS
1154 b41b3516 Iustin Pop

1155 870dc44c Iustin Pop
  @type VARIANT_DELIM: string
1156 870dc44c Iustin Pop
  @cvar VARIANT_DELIM: the variant delimiter
1157 870dc44c Iustin Pop

1158 b41b3516 Iustin Pop
  """
1159 a8083063 Iustin Pop
  __slots__ = [
1160 a8083063 Iustin Pop
    "name",
1161 a8083063 Iustin Pop
    "path",
1162 082a7f91 Guido Trotter
    "api_versions",
1163 a8083063 Iustin Pop
    "create_script",
1164 a8083063 Iustin Pop
    "export_script",
1165 386b57af Iustin Pop
    "import_script",
1166 386b57af Iustin Pop
    "rename_script",
1167 b41b3516 Iustin Pop
    "verify_script",
1168 6d79896b Guido Trotter
    "supported_variants",
1169 b41b3516 Iustin Pop
    "supported_parameters",
1170 a8083063 Iustin Pop
    ]
1171 a8083063 Iustin Pop
1172 870dc44c Iustin Pop
  VARIANT_DELIM = "+"
1173 870dc44c Iustin Pop
1174 870dc44c Iustin Pop
  @classmethod
1175 870dc44c Iustin Pop
  def SplitNameVariant(cls, name):
1176 870dc44c Iustin Pop
    """Splits the name into the proper name and variant.
1177 870dc44c Iustin Pop

1178 870dc44c Iustin Pop
    @param name: the OS (unprocessed) name
1179 870dc44c Iustin Pop
    @rtype: list
1180 870dc44c Iustin Pop
    @return: a list of two elements; if the original name didn't
1181 870dc44c Iustin Pop
        contain a variant, it's returned as an empty string
1182 870dc44c Iustin Pop

1183 870dc44c Iustin Pop
    """
1184 870dc44c Iustin Pop
    nv = name.split(cls.VARIANT_DELIM, 1)
1185 870dc44c Iustin Pop
    if len(nv) == 1:
1186 870dc44c Iustin Pop
      nv.append("")
1187 870dc44c Iustin Pop
    return nv
1188 870dc44c Iustin Pop
1189 870dc44c Iustin Pop
  @classmethod
1190 870dc44c Iustin Pop
  def GetName(cls, name):
1191 870dc44c Iustin Pop
    """Returns the proper name of the os (without the variant).
1192 870dc44c Iustin Pop

1193 870dc44c Iustin Pop
    @param name: the OS (unprocessed) name
1194 870dc44c Iustin Pop

1195 870dc44c Iustin Pop
    """
1196 870dc44c Iustin Pop
    return cls.SplitNameVariant(name)[0]
1197 870dc44c Iustin Pop
1198 870dc44c Iustin Pop
  @classmethod
1199 870dc44c Iustin Pop
  def GetVariant(cls, name):
1200 870dc44c Iustin Pop
    """Returns the variant the os (without the base name).
1201 870dc44c Iustin Pop

1202 870dc44c Iustin Pop
    @param name: the OS (unprocessed) name
1203 870dc44c Iustin Pop

1204 870dc44c Iustin Pop
    """
1205 870dc44c Iustin Pop
    return cls.SplitNameVariant(name)[1]
1206 870dc44c Iustin Pop
1207 7c0d6283 Michael Hanselmann
1208 376631d1 Constantinos Venetsanopoulos
class ExtStorage(ConfigObject):
1209 376631d1 Constantinos Venetsanopoulos
  """Config object representing an External Storage Provider.
1210 376631d1 Constantinos Venetsanopoulos

1211 376631d1 Constantinos Venetsanopoulos
  """
1212 376631d1 Constantinos Venetsanopoulos
  __slots__ = [
1213 376631d1 Constantinos Venetsanopoulos
    "name",
1214 376631d1 Constantinos Venetsanopoulos
    "path",
1215 376631d1 Constantinos Venetsanopoulos
    "create_script",
1216 376631d1 Constantinos Venetsanopoulos
    "remove_script",
1217 376631d1 Constantinos Venetsanopoulos
    "grow_script",
1218 376631d1 Constantinos Venetsanopoulos
    "attach_script",
1219 376631d1 Constantinos Venetsanopoulos
    "detach_script",
1220 376631d1 Constantinos Venetsanopoulos
    "setinfo_script",
1221 938adc87 Constantinos Venetsanopoulos
    "verify_script",
1222 938adc87 Constantinos Venetsanopoulos
    "supported_parameters",
1223 376631d1 Constantinos Venetsanopoulos
    ]
1224 376631d1 Constantinos Venetsanopoulos
1225 376631d1 Constantinos Venetsanopoulos
1226 5f06ce5e Michael Hanselmann
class NodeHvState(ConfigObject):
1227 5f06ce5e Michael Hanselmann
  """Hypvervisor state on a node.
1228 5f06ce5e Michael Hanselmann

1229 5f06ce5e Michael Hanselmann
  @ivar mem_total: Total amount of memory
1230 5f06ce5e Michael Hanselmann
  @ivar mem_node: Memory used by, or reserved for, the node itself (not always
1231 5f06ce5e Michael Hanselmann
    available)
1232 5f06ce5e Michael Hanselmann
  @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
1233 5f06ce5e Michael Hanselmann
    rounding
1234 5f06ce5e Michael Hanselmann
  @ivar mem_inst: Memory used by instances living on node
1235 5f06ce5e Michael Hanselmann
  @ivar cpu_total: Total node CPU core count
1236 5f06ce5e Michael Hanselmann
  @ivar cpu_node: Number of CPU cores reserved for the node itself
1237 5f06ce5e Michael Hanselmann

1238 5f06ce5e Michael Hanselmann
  """
1239 5f06ce5e Michael Hanselmann
  __slots__ = [
1240 5f06ce5e Michael Hanselmann
    "mem_total",
1241 5f06ce5e Michael Hanselmann
    "mem_node",
1242 5f06ce5e Michael Hanselmann
    "mem_hv",
1243 5f06ce5e Michael Hanselmann
    "mem_inst",
1244 5f06ce5e Michael Hanselmann
    "cpu_total",
1245 5f06ce5e Michael Hanselmann
    "cpu_node",
1246 5f06ce5e Michael Hanselmann
    ] + _TIMESTAMPS
1247 5f06ce5e Michael Hanselmann
1248 5f06ce5e Michael Hanselmann
1249 5f06ce5e Michael Hanselmann
class NodeDiskState(ConfigObject):
1250 5f06ce5e Michael Hanselmann
  """Disk state on a node.
1251 5f06ce5e Michael Hanselmann

1252 5f06ce5e Michael Hanselmann
  """
1253 5f06ce5e Michael Hanselmann
  __slots__ = [
1254 5f06ce5e Michael Hanselmann
    "total",
1255 5f06ce5e Michael Hanselmann
    "reserved",
1256 5f06ce5e Michael Hanselmann
    "overhead",
1257 5f06ce5e Michael Hanselmann
    ] + _TIMESTAMPS
1258 5f06ce5e Michael Hanselmann
1259 5f06ce5e Michael Hanselmann
1260 ec29fe40 Iustin Pop
class Node(TaggableObject):
1261 634d30f4 Michael Hanselmann
  """Config object representing a node.
1262 634d30f4 Michael Hanselmann

1263 634d30f4 Michael Hanselmann
  @ivar hv_state: Hypervisor state (e.g. number of CPUs)
1264 634d30f4 Michael Hanselmann
  @ivar hv_state_static: Hypervisor state overriden by user
1265 634d30f4 Michael Hanselmann
  @ivar disk_state: Disk state (e.g. free space)
1266 634d30f4 Michael Hanselmann
  @ivar disk_state_static: Disk state overriden by user
1267 634d30f4 Michael Hanselmann

1268 634d30f4 Michael Hanselmann
  """
1269 154b9580 Balazs Lecz
  __slots__ = [
1270 ec29fe40 Iustin Pop
    "name",
1271 ec29fe40 Iustin Pop
    "primary_ip",
1272 ec29fe40 Iustin Pop
    "secondary_ip",
1273 be1fa613 Iustin Pop
    "serial_no",
1274 8b8b8b81 Iustin Pop
    "master_candidate",
1275 fc0fe88c Iustin Pop
    "offline",
1276 af64c0ea Iustin Pop
    "drained",
1277 f936c153 Iustin Pop
    "group",
1278 490acd18 Iustin Pop
    "master_capable",
1279 490acd18 Iustin Pop
    "vm_capable",
1280 095e71aa René Nussbaumer
    "ndparams",
1281 25124d4a René Nussbaumer
    "powered",
1282 5b49ed09 René Nussbaumer
    "hv_state",
1283 634d30f4 Michael Hanselmann
    "hv_state_static",
1284 5b49ed09 René Nussbaumer
    "disk_state",
1285 634d30f4 Michael Hanselmann
    "disk_state_static",
1286 e1dcc53a Iustin Pop
    ] + _TIMESTAMPS + _UUID
1287 a8083063 Iustin Pop
1288 490acd18 Iustin Pop
  def UpgradeConfig(self):
1289 490acd18 Iustin Pop
    """Fill defaults for missing configuration values.
1290 490acd18 Iustin Pop

1291 490acd18 Iustin Pop
    """
1292 b459a848 Andrea Spadaccini
    # pylint: disable=E0203
1293 490acd18 Iustin Pop
    # because these are "defined" via slots, not manually
1294 490acd18 Iustin Pop
    if self.master_capable is None:
1295 490acd18 Iustin Pop
      self.master_capable = True
1296 490acd18 Iustin Pop
1297 490acd18 Iustin Pop
    if self.vm_capable is None:
1298 490acd18 Iustin Pop
      self.vm_capable = True
1299 490acd18 Iustin Pop
1300 095e71aa René Nussbaumer
    if self.ndparams is None:
1301 095e71aa René Nussbaumer
      self.ndparams = {}
1302 250a9404 Bernardo Dal Seno
    # And remove any global parameter
1303 250a9404 Bernardo Dal Seno
    for key in constants.NDC_GLOBALS:
1304 250a9404 Bernardo Dal Seno
      if key in self.ndparams:
1305 250a9404 Bernardo Dal Seno
        logging.warning("Ignoring %s node parameter for node %s",
1306 250a9404 Bernardo Dal Seno
                        key, self.name)
1307 250a9404 Bernardo Dal Seno
        del self.ndparams[key]
1308 095e71aa René Nussbaumer
1309 25124d4a René Nussbaumer
    if self.powered is None:
1310 25124d4a René Nussbaumer
      self.powered = True
1311 25124d4a René Nussbaumer
1312 5f06ce5e Michael Hanselmann
  def ToDict(self):
1313 5f06ce5e Michael Hanselmann
    """Custom function for serializing.
1314 5f06ce5e Michael Hanselmann

1315 5f06ce5e Michael Hanselmann
    """
1316 5f06ce5e Michael Hanselmann
    data = super(Node, self).ToDict()
1317 5f06ce5e Michael Hanselmann
1318 5f06ce5e Michael Hanselmann
    hv_state = data.get("hv_state", None)
1319 5f06ce5e Michael Hanselmann
    if hv_state is not None:
1320 fe502d25 Iustin Pop
      data["hv_state"] = outils.ContainerToDicts(hv_state)
1321 5f06ce5e Michael Hanselmann
1322 5f06ce5e Michael Hanselmann
    disk_state = data.get("disk_state", None)
1323 5f06ce5e Michael Hanselmann
    if disk_state is not None:
1324 5f06ce5e Michael Hanselmann
      data["disk_state"] = \
1325 fe502d25 Iustin Pop
        dict((key, outils.ContainerToDicts(value))
1326 5f06ce5e Michael Hanselmann
             for (key, value) in disk_state.items())
1327 5f06ce5e Michael Hanselmann
1328 5f06ce5e Michael Hanselmann
    return data
1329 5f06ce5e Michael Hanselmann
1330 5f06ce5e Michael Hanselmann
  @classmethod
1331 5f06ce5e Michael Hanselmann
  def FromDict(cls, val):
1332 5f06ce5e Michael Hanselmann
    """Custom function for deserializing.
1333 5f06ce5e Michael Hanselmann

1334 5f06ce5e Michael Hanselmann
    """
1335 5f06ce5e Michael Hanselmann
    obj = super(Node, cls).FromDict(val)
1336 5f06ce5e Michael Hanselmann
1337 5f06ce5e Michael Hanselmann
    if obj.hv_state is not None:
1338 473ab806 Michael Hanselmann
      obj.hv_state = \
1339 fe502d25 Iustin Pop
        outils.ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1340 5f06ce5e Michael Hanselmann
1341 5f06ce5e Michael Hanselmann
    if obj.disk_state is not None:
1342 5f06ce5e Michael Hanselmann
      obj.disk_state = \
1343 fe502d25 Iustin Pop
        dict((key, outils.ContainerFromDicts(value, dict, NodeDiskState))
1344 5f06ce5e Michael Hanselmann
             for (key, value) in obj.disk_state.items())
1345 5f06ce5e Michael Hanselmann
1346 5f06ce5e Michael Hanselmann
    return obj
1347 5f06ce5e Michael Hanselmann
1348 a8083063 Iustin Pop
1349 1ffd2673 Michael Hanselmann
class NodeGroup(TaggableObject):
1350 24a3707f Guido Trotter
  """Config object representing a node group."""
1351 24a3707f Guido Trotter
  __slots__ = [
1352 24a3707f Guido Trotter
    "name",
1353 24a3707f Guido Trotter
    "members",
1354 095e71aa René Nussbaumer
    "ndparams",
1355 bc5d0215 Andrea Spadaccini
    "diskparams",
1356 81e3ab4f Agata Murawska
    "ipolicy",
1357 e11a1b77 Adeodato Simo
    "serial_no",
1358 a8282327 René Nussbaumer
    "hv_state_static",
1359 a8282327 René Nussbaumer
    "disk_state_static",
1360 90e99856 Adeodato Simo
    "alloc_policy",
1361 eaa4c57c Dimitris Aragiorgis
    "networks",
1362 24a3707f Guido Trotter
    ] + _TIMESTAMPS + _UUID
1363 24a3707f Guido Trotter
1364 24a3707f Guido Trotter
  def ToDict(self):
1365 24a3707f Guido Trotter
    """Custom function for nodegroup.
1366 24a3707f Guido Trotter

1367 c60abd62 Guido Trotter
    This discards the members object, which gets recalculated and is only kept
1368 c60abd62 Guido Trotter
    in memory.
1369 24a3707f Guido Trotter

1370 24a3707f Guido Trotter
    """
1371 24a3707f Guido Trotter
    mydict = super(NodeGroup, self).ToDict()
1372 24a3707f Guido Trotter
    del mydict["members"]
1373 24a3707f Guido Trotter
    return mydict
1374 24a3707f Guido Trotter
1375 24a3707f Guido Trotter
  @classmethod
1376 24a3707f Guido Trotter
  def FromDict(cls, val):
1377 24a3707f Guido Trotter
    """Custom function for nodegroup.
1378 24a3707f Guido Trotter

1379 24a3707f Guido Trotter
    The members slot is initialized to an empty list, upon deserialization.
1380 24a3707f Guido Trotter

1381 24a3707f Guido Trotter
    """
1382 24a3707f Guido Trotter
    obj = super(NodeGroup, cls).FromDict(val)
1383 24a3707f Guido Trotter
    obj.members = []
1384 24a3707f Guido Trotter
    return obj
1385 24a3707f Guido Trotter
1386 095e71aa René Nussbaumer
  def UpgradeConfig(self):
1387 095e71aa René Nussbaumer
    """Fill defaults for missing configuration values.
1388 095e71aa René Nussbaumer

1389 095e71aa René Nussbaumer
    """
1390 095e71aa René Nussbaumer
    if self.ndparams is None:
1391 095e71aa René Nussbaumer
      self.ndparams = {}
1392 095e71aa René Nussbaumer
1393 e11a1b77 Adeodato Simo
    if self.serial_no is None:
1394 e11a1b77 Adeodato Simo
      self.serial_no = 1
1395 e11a1b77 Adeodato Simo
1396 90e99856 Adeodato Simo
    if self.alloc_policy is None:
1397 90e99856 Adeodato Simo
      self.alloc_policy = constants.ALLOC_POLICY_PREFERRED
1398 90e99856 Adeodato Simo
1399 4b97458c Iustin Pop
    # We only update mtime, and not ctime, since we would not be able
1400 4b97458c Iustin Pop
    # to provide a correct value for creation time.
1401 e11a1b77 Adeodato Simo
    if self.mtime is None:
1402 e11a1b77 Adeodato Simo
      self.mtime = time.time()
1403 e11a1b77 Adeodato Simo
1404 7228ca91 René Nussbaumer
    if self.diskparams is None:
1405 7228ca91 René Nussbaumer
      self.diskparams = {}
1406 81e3ab4f Agata Murawska
    if self.ipolicy is None:
1407 81e3ab4f Agata Murawska
      self.ipolicy = MakeEmptyIPolicy()
1408 bc5d0215 Andrea Spadaccini
1409 eaa4c57c Dimitris Aragiorgis
    if self.networks is None:
1410 eaa4c57c Dimitris Aragiorgis
      self.networks = {}
1411 eaa4c57c Dimitris Aragiorgis
1412 095e71aa René Nussbaumer
  def FillND(self, node):
1413 ce523de1 Michael Hanselmann
    """Return filled out ndparams for L{objects.Node}
1414 095e71aa René Nussbaumer

1415 095e71aa René Nussbaumer
    @type node: L{objects.Node}
1416 095e71aa René Nussbaumer
    @param node: A Node object to fill
1417 095e71aa René Nussbaumer
    @return a copy of the node's ndparams with defaults filled
1418 095e71aa René Nussbaumer

1419 095e71aa René Nussbaumer
    """
1420 095e71aa René Nussbaumer
    return self.SimpleFillND(node.ndparams)
1421 095e71aa René Nussbaumer
1422 095e71aa René Nussbaumer
  def SimpleFillND(self, ndparams):
1423 095e71aa René Nussbaumer
    """Fill a given ndparams dict with defaults.
1424 095e71aa René Nussbaumer

1425 095e71aa René Nussbaumer
    @type ndparams: dict
1426 095e71aa René Nussbaumer
    @param ndparams: the dict to fill
1427 095e71aa René Nussbaumer
    @rtype: dict
1428 095e71aa René Nussbaumer
    @return: a copy of the passed in ndparams with missing keys filled
1429 e6e88de6 Adeodato Simo
        from the node group defaults
1430 095e71aa René Nussbaumer

1431 095e71aa René Nussbaumer
    """
1432 095e71aa René Nussbaumer
    return FillDict(self.ndparams, ndparams)
1433 095e71aa René Nussbaumer
1434 24a3707f Guido Trotter
1435 ec29fe40 Iustin Pop
class Cluster(TaggableObject):
1436 a8083063 Iustin Pop
  """Config object representing the cluster."""
1437 154b9580 Balazs Lecz
  __slots__ = [
1438 a8083063 Iustin Pop
    "serial_no",
1439 a8083063 Iustin Pop
    "rsahostkeypub",
1440 a8083063 Iustin Pop
    "highest_used_port",
1441 b2fddf63 Iustin Pop
    "tcpudp_port_pool",
1442 a8083063 Iustin Pop
    "mac_prefix",
1443 a8083063 Iustin Pop
    "volume_group_name",
1444 999b183c Iustin Pop
    "reserved_lvs",
1445 9e33896b Luca Bigliardi
    "drbd_usermode_helper",
1446 a8083063 Iustin Pop
    "default_bridge",
1447 02691904 Alexander Schreiber
    "default_hypervisor",
1448 f6bd6e98 Michael Hanselmann
    "master_node",
1449 f6bd6e98 Michael Hanselmann
    "master_ip",
1450 f6bd6e98 Michael Hanselmann
    "master_netdev",
1451 5a8648eb Andrea Spadaccini
    "master_netmask",
1452 33be7576 Andrea Spadaccini
    "use_external_mip_script",
1453 f6bd6e98 Michael Hanselmann
    "cluster_name",
1454 f6bd6e98 Michael Hanselmann
    "file_storage_dir",
1455 4b97f902 Apollon Oikonomopoulos
    "shared_file_storage_dir",
1456 e69d05fd Iustin Pop
    "enabled_hypervisors",
1457 5bf7b5cf Iustin Pop
    "hvparams",
1458 918eb80b Agata Murawska
    "ipolicy",
1459 17463d22 René Nussbaumer
    "os_hvp",
1460 5bf7b5cf Iustin Pop
    "beparams",
1461 1bdcbbab Iustin Pop
    "osparams",
1462 c8fcde47 Guido Trotter
    "nicparams",
1463 095e71aa René Nussbaumer
    "ndparams",
1464 bc5d0215 Andrea Spadaccini
    "diskparams",
1465 4b7735f9 Iustin Pop
    "candidate_pool_size",
1466 b86a6bcd Guido Trotter
    "modify_etc_hosts",
1467 b989b9d9 Ken Wehr
    "modify_ssh_setup",
1468 3953242f Iustin Pop
    "maintain_node_health",
1469 4437d889 Balazs Lecz
    "uid_pool",
1470 bf4af505 Apollon Oikonomopoulos
    "default_iallocator",
1471 87b2cd45 Iustin Pop
    "hidden_os",
1472 87b2cd45 Iustin Pop
    "blacklisted_os",
1473 2f20d07b Manuel Franceschini
    "primary_ip_family",
1474 3d914585 René Nussbaumer
    "prealloc_wipe_disks",
1475 2da9f556 René Nussbaumer
    "hv_state_static",
1476 2da9f556 René Nussbaumer
    "disk_state_static",
1477 d0de443e Helga Velroyen
    "enabled_storage_types",
1478 e1dcc53a Iustin Pop
    ] + _TIMESTAMPS + _UUID
1479 a8083063 Iustin Pop
1480 b86a6bcd Guido Trotter
  def UpgradeConfig(self):
1481 b86a6bcd Guido Trotter
    """Fill defaults for missing configuration values.
1482 b86a6bcd Guido Trotter

1483 b86a6bcd Guido Trotter
    """
1484 b459a848 Andrea Spadaccini
    # pylint: disable=E0203
1485 fe267188 Iustin Pop
    # because these are "defined" via slots, not manually
1486 c1b42c18 Guido Trotter
    if self.hvparams is None:
1487 c1b42c18 Guido Trotter
      self.hvparams = constants.HVC_DEFAULTS
1488 c1b42c18 Guido Trotter
    else:
1489 c1b42c18 Guido Trotter
      for hypervisor in self.hvparams:
1490 abe609b2 Guido Trotter
        self.hvparams[hypervisor] = FillDict(
1491 c1b42c18 Guido Trotter
            constants.HVC_DEFAULTS[hypervisor], self.hvparams[hypervisor])
1492 c1b42c18 Guido Trotter
1493 17463d22 René Nussbaumer
    if self.os_hvp is None:
1494 17463d22 René Nussbaumer
      self.os_hvp = {}
1495 17463d22 René Nussbaumer
1496 1bdcbbab Iustin Pop
    # osparams added before 2.2
1497 1bdcbbab Iustin Pop
    if self.osparams is None:
1498 1bdcbbab Iustin Pop
      self.osparams = {}
1499 1bdcbbab Iustin Pop
1500 2a27dac3 Iustin Pop
    self.ndparams = UpgradeNDParams(self.ndparams)
1501 095e71aa René Nussbaumer
1502 6e34b628 Guido Trotter
    self.beparams = UpgradeGroupedParams(self.beparams,
1503 6e34b628 Guido Trotter
                                         constants.BEC_DEFAULTS)
1504 8c72ab2b Guido Trotter
    for beparams_group in self.beparams:
1505 8c72ab2b Guido Trotter
      UpgradeBeParams(self.beparams[beparams_group])
1506 8c72ab2b Guido Trotter
1507 c8fcde47 Guido Trotter
    migrate_default_bridge = not self.nicparams
1508 c8fcde47 Guido Trotter
    self.nicparams = UpgradeGroupedParams(self.nicparams,
1509 c8fcde47 Guido Trotter
                                          constants.NICC_DEFAULTS)
1510 c8fcde47 Guido Trotter
    if migrate_default_bridge:
1511 c8fcde47 Guido Trotter
      self.nicparams[constants.PP_DEFAULT][constants.NIC_LINK] = \
1512 c8fcde47 Guido Trotter
        self.default_bridge
1513 c1b42c18 Guido Trotter
1514 b86a6bcd Guido Trotter
    if self.modify_etc_hosts is None:
1515 b86a6bcd Guido Trotter
      self.modify_etc_hosts = True
1516 b86a6bcd Guido Trotter
1517 b989b9d9 Ken Wehr
    if self.modify_ssh_setup is None:
1518 b989b9d9 Ken Wehr
      self.modify_ssh_setup = True
1519 b989b9d9 Ken Wehr
1520 73f1d185 Stephen Shirley
    # default_bridge is no longer used in 2.1. The slot is left there to
1521 90d118fd Guido Trotter
    # support auto-upgrading. It can be removed once we decide to deprecate
1522 90d118fd Guido Trotter
    # upgrading straight from 2.0.
1523 9b31ca85 Guido Trotter
    if self.default_bridge is not None:
1524 9b31ca85 Guido Trotter
      self.default_bridge = None
1525 9b31ca85 Guido Trotter
1526 90d118fd Guido Trotter
    # default_hypervisor is just the first enabled one in 2.1. This slot and
1527 90d118fd Guido Trotter
    # code can be removed once upgrading straight from 2.0 is deprecated.
1528 066f465d Guido Trotter
    if self.default_hypervisor is not None:
1529 016d04b3 Michael Hanselmann
      self.enabled_hypervisors = ([self.default_hypervisor] +
1530 5ae4945a Iustin Pop
                                  [hvname for hvname in self.enabled_hypervisors
1531 5ae4945a Iustin Pop
                                   if hvname != self.default_hypervisor])
1532 066f465d Guido Trotter
      self.default_hypervisor = None
1533 066f465d Guido Trotter
1534 3953242f Iustin Pop
    # maintain_node_health added after 2.1.1
1535 3953242f Iustin Pop
    if self.maintain_node_health is None:
1536 3953242f Iustin Pop
      self.maintain_node_health = False
1537 3953242f Iustin Pop
1538 4437d889 Balazs Lecz
    if self.uid_pool is None:
1539 4437d889 Balazs Lecz
      self.uid_pool = []
1540 4437d889 Balazs Lecz
1541 bf4af505 Apollon Oikonomopoulos
    if self.default_iallocator is None:
1542 bf4af505 Apollon Oikonomopoulos
      self.default_iallocator = ""
1543 bf4af505 Apollon Oikonomopoulos
1544 999b183c Iustin Pop
    # reserved_lvs added before 2.2
1545 999b183c Iustin Pop
    if self.reserved_lvs is None:
1546 999b183c Iustin Pop
      self.reserved_lvs = []
1547 999b183c Iustin Pop
1548 546b1111 Iustin Pop
    # hidden and blacklisted operating systems added before 2.2.1
1549 87b2cd45 Iustin Pop
    if self.hidden_os is None:
1550 87b2cd45 Iustin Pop
      self.hidden_os = []
1551 546b1111 Iustin Pop
1552 87b2cd45 Iustin Pop
    if self.blacklisted_os is None:
1553 87b2cd45 Iustin Pop
      self.blacklisted_os = []
1554 546b1111 Iustin Pop
1555 f4c9af7a Guido Trotter
    # primary_ip_family added before 2.3
1556 f4c9af7a Guido Trotter
    if self.primary_ip_family is None:
1557 f4c9af7a Guido Trotter
      self.primary_ip_family = AF_INET
1558 f4c9af7a Guido Trotter
1559 0007f3ab Andrea Spadaccini
    if self.master_netmask is None:
1560 0007f3ab Andrea Spadaccini
      ipcls = netutils.IPAddress.GetClassFromIpFamily(self.primary_ip_family)
1561 0007f3ab Andrea Spadaccini
      self.master_netmask = ipcls.iplen
1562 0007f3ab Andrea Spadaccini
1563 3d914585 René Nussbaumer
    if self.prealloc_wipe_disks is None:
1564 3d914585 René Nussbaumer
      self.prealloc_wipe_disks = False
1565 3d914585 René Nussbaumer
1566 e8f472d1 Iustin Pop
    # shared_file_storage_dir added before 2.5
1567 e8f472d1 Iustin Pop
    if self.shared_file_storage_dir is None:
1568 e8f472d1 Iustin Pop
      self.shared_file_storage_dir = ""
1569 e8f472d1 Iustin Pop
1570 33be7576 Andrea Spadaccini
    if self.use_external_mip_script is None:
1571 33be7576 Andrea Spadaccini
      self.use_external_mip_script = False
1572 33be7576 Andrea Spadaccini
1573 99ccf8b9 René Nussbaumer
    if self.diskparams:
1574 99ccf8b9 René Nussbaumer
      self.diskparams = UpgradeDiskParams(self.diskparams)
1575 99ccf8b9 René Nussbaumer
    else:
1576 99ccf8b9 René Nussbaumer
      self.diskparams = constants.DISK_DT_DEFAULTS.copy()
1577 bc5d0215 Andrea Spadaccini
1578 918eb80b Agata Murawska
    # instance policy added before 2.6
1579 918eb80b Agata Murawska
    if self.ipolicy is None:
1580 2cc673a3 Iustin Pop
      self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, {})
1581 38a6e2e1 Iustin Pop
    else:
1582 38a6e2e1 Iustin Pop
      # we can either make sure to upgrade the ipolicy always, or only
1583 38a6e2e1 Iustin Pop
      # do it in some corner cases (e.g. missing keys); note that this
1584 38a6e2e1 Iustin Pop
      # will break any removal of keys from the ipolicy dict
1585 4f7e5a1d Bernardo Dal Seno
      wrongkeys = frozenset(self.ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
1586 4f7e5a1d Bernardo Dal Seno
      if wrongkeys:
1587 4f7e5a1d Bernardo Dal Seno
        # These keys would be silently removed by FillIPolicy()
1588 4f7e5a1d Bernardo Dal Seno
        msg = ("Cluster instance policy contains spourious keys: %s" %
1589 4f7e5a1d Bernardo Dal Seno
               utils.CommaJoin(wrongkeys))
1590 4f7e5a1d Bernardo Dal Seno
        raise errors.ConfigurationError(msg)
1591 38a6e2e1 Iustin Pop
      self.ipolicy = FillIPolicy(constants.IPOLICY_DEFAULTS, self.ipolicy)
1592 918eb80b Agata Murawska
1593 0fbedb7a Michael Hanselmann
  @property
1594 0fbedb7a Michael Hanselmann
  def primary_hypervisor(self):
1595 0fbedb7a Michael Hanselmann
    """The first hypervisor is the primary.
1596 0fbedb7a Michael Hanselmann

1597 0fbedb7a Michael Hanselmann
    Useful, for example, for L{Node}'s hv/disk state.
1598 0fbedb7a Michael Hanselmann

1599 0fbedb7a Michael Hanselmann
    """
1600 0fbedb7a Michael Hanselmann
    return self.enabled_hypervisors[0]
1601 0fbedb7a Michael Hanselmann
1602 319856a9 Michael Hanselmann
  def ToDict(self):
1603 319856a9 Michael Hanselmann
    """Custom function for cluster.
1604 319856a9 Michael Hanselmann

1605 319856a9 Michael Hanselmann
    """
1606 b60ae2ca Iustin Pop
    mydict = super(Cluster, self).ToDict()
1607 4d36fbf4 Michael Hanselmann
1608 4d36fbf4 Michael Hanselmann
    if self.tcpudp_port_pool is None:
1609 4d36fbf4 Michael Hanselmann
      tcpudp_port_pool = []
1610 4d36fbf4 Michael Hanselmann
    else:
1611 4d36fbf4 Michael Hanselmann
      tcpudp_port_pool = list(self.tcpudp_port_pool)
1612 4d36fbf4 Michael Hanselmann
1613 4d36fbf4 Michael Hanselmann
    mydict["tcpudp_port_pool"] = tcpudp_port_pool
1614 4d36fbf4 Michael Hanselmann
1615 319856a9 Michael Hanselmann
    return mydict
1616 319856a9 Michael Hanselmann
1617 319856a9 Michael Hanselmann
  @classmethod
1618 319856a9 Michael Hanselmann
  def FromDict(cls, val):
1619 319856a9 Michael Hanselmann
    """Custom function for cluster.
1620 319856a9 Michael Hanselmann

1621 319856a9 Michael Hanselmann
    """
1622 b60ae2ca Iustin Pop
    obj = super(Cluster, cls).FromDict(val)
1623 4d36fbf4 Michael Hanselmann
1624 4d36fbf4 Michael Hanselmann
    if obj.tcpudp_port_pool is None:
1625 4d36fbf4 Michael Hanselmann
      obj.tcpudp_port_pool = set()
1626 4d36fbf4 Michael Hanselmann
    elif not isinstance(obj.tcpudp_port_pool, set):
1627 319856a9 Michael Hanselmann
      obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
1628 4d36fbf4 Michael Hanselmann
1629 319856a9 Michael Hanselmann
    return obj
1630 319856a9 Michael Hanselmann
1631 8a147bba René Nussbaumer
  def SimpleFillDP(self, diskparams):
1632 8a147bba René Nussbaumer
    """Fill a given diskparams dict with cluster defaults.
1633 8a147bba René Nussbaumer

1634 8a147bba René Nussbaumer
    @param diskparams: The diskparams
1635 8a147bba René Nussbaumer
    @return: The defaults dict
1636 8a147bba René Nussbaumer

1637 8a147bba René Nussbaumer
    """
1638 8a147bba René Nussbaumer
    return FillDiskParams(self.diskparams, diskparams)
1639 8a147bba René Nussbaumer
1640 d63479b5 Iustin Pop
  def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
1641 d63479b5 Iustin Pop
    """Get the default hypervisor parameters for the cluster.
1642 d63479b5 Iustin Pop

1643 d63479b5 Iustin Pop
    @param hypervisor: the hypervisor name
1644 d63479b5 Iustin Pop
    @param os_name: if specified, we'll also update the defaults for this OS
1645 d63479b5 Iustin Pop
    @param skip_keys: if passed, list of keys not to use
1646 d63479b5 Iustin Pop
    @return: the defaults dict
1647 d63479b5 Iustin Pop

1648 d63479b5 Iustin Pop
    """
1649 d63479b5 Iustin Pop
    if skip_keys is None:
1650 d63479b5 Iustin Pop
      skip_keys = []
1651 d63479b5 Iustin Pop
1652 d63479b5 Iustin Pop
    fill_stack = [self.hvparams.get(hypervisor, {})]
1653 d63479b5 Iustin Pop
    if os_name is not None:
1654 d63479b5 Iustin Pop
      os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
1655 d63479b5 Iustin Pop
      fill_stack.append(os_hvp)
1656 d63479b5 Iustin Pop
1657 d63479b5 Iustin Pop
    ret_dict = {}
1658 d63479b5 Iustin Pop
    for o_dict in fill_stack:
1659 d63479b5 Iustin Pop
      ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
1660 d63479b5 Iustin Pop
1661 d63479b5 Iustin Pop
    return ret_dict
1662 d63479b5 Iustin Pop
1663 73e0328b Iustin Pop
  def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False):
1664 73e0328b Iustin Pop
    """Fill a given hvparams dict with cluster defaults.
1665 73e0328b Iustin Pop

1666 73e0328b Iustin Pop
    @type hv_name: string
1667 73e0328b Iustin Pop
    @param hv_name: the hypervisor to use
1668 73e0328b Iustin Pop
    @type os_name: string
1669 73e0328b Iustin Pop
    @param os_name: the OS to use for overriding the hypervisor defaults
1670 73e0328b Iustin Pop
    @type skip_globals: boolean
1671 73e0328b Iustin Pop
    @param skip_globals: if True, the global hypervisor parameters will
1672 73e0328b Iustin Pop
        not be filled
1673 73e0328b Iustin Pop
    @rtype: dict
1674 73e0328b Iustin Pop
    @return: a copy of the given hvparams with missing keys filled from
1675 73e0328b Iustin Pop
        the cluster defaults
1676 73e0328b Iustin Pop

1677 73e0328b Iustin Pop
    """
1678 73e0328b Iustin Pop
    if skip_globals:
1679 73e0328b Iustin Pop
      skip_keys = constants.HVC_GLOBALS
1680 73e0328b Iustin Pop
    else:
1681 73e0328b Iustin Pop
      skip_keys = []
1682 73e0328b Iustin Pop
1683 73e0328b Iustin Pop
    def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys)
1684 73e0328b Iustin Pop
    return FillDict(def_dict, hvparams, skip_keys=skip_keys)
1685 d63479b5 Iustin Pop
1686 7736a5f2 Iustin Pop
  def FillHV(self, instance, skip_globals=False):
1687 73e0328b Iustin Pop
    """Fill an instance's hvparams dict with cluster defaults.
1688 5bf7b5cf Iustin Pop

1689 a2a24f4c Guido Trotter
    @type instance: L{objects.Instance}
1690 5bf7b5cf Iustin Pop
    @param instance: the instance parameter to fill
1691 7736a5f2 Iustin Pop
    @type skip_globals: boolean
1692 7736a5f2 Iustin Pop
    @param skip_globals: if True, the global hypervisor parameters will
1693 7736a5f2 Iustin Pop
        not be filled
1694 5bf7b5cf Iustin Pop
    @rtype: dict
1695 5bf7b5cf Iustin Pop
    @return: a copy of the instance's hvparams with missing keys filled from
1696 5bf7b5cf Iustin Pop
        the cluster defaults
1697 5bf7b5cf Iustin Pop

1698 5bf7b5cf Iustin Pop
    """
1699 73e0328b Iustin Pop
    return self.SimpleFillHV(instance.hypervisor, instance.os,
1700 73e0328b Iustin Pop
                             instance.hvparams, skip_globals)
1701 17463d22 René Nussbaumer
1702 73e0328b Iustin Pop
  def SimpleFillBE(self, beparams):
1703 73e0328b Iustin Pop
    """Fill a given beparams dict with cluster defaults.
1704 73e0328b Iustin Pop

1705 06596a60 Guido Trotter
    @type beparams: dict
1706 06596a60 Guido Trotter
    @param beparams: the dict to fill
1707 73e0328b Iustin Pop
    @rtype: dict
1708 73e0328b Iustin Pop
    @return: a copy of the passed in beparams with missing keys filled
1709 73e0328b Iustin Pop
        from the cluster defaults
1710 73e0328b Iustin Pop

1711 73e0328b Iustin Pop
    """
1712 73e0328b Iustin Pop
    return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams)
1713 5bf7b5cf Iustin Pop
1714 5bf7b5cf Iustin Pop
  def FillBE(self, instance):
1715 73e0328b Iustin Pop
    """Fill an instance's beparams dict with cluster defaults.
1716 5bf7b5cf Iustin Pop

1717 a2a24f4c Guido Trotter
    @type instance: L{objects.Instance}
1718 5bf7b5cf Iustin Pop
    @param instance: the instance parameter to fill
1719 5bf7b5cf Iustin Pop
    @rtype: dict
1720 5bf7b5cf Iustin Pop
    @return: a copy of the instance's beparams with missing keys filled from
1721 5bf7b5cf Iustin Pop
        the cluster defaults
1722 5bf7b5cf Iustin Pop

1723 5bf7b5cf Iustin Pop
    """
1724 73e0328b Iustin Pop
    return self.SimpleFillBE(instance.beparams)
1725 73e0328b Iustin Pop
1726 73e0328b Iustin Pop
  def SimpleFillNIC(self, nicparams):
1727 73e0328b Iustin Pop
    """Fill a given nicparams dict with cluster defaults.
1728 73e0328b Iustin Pop

1729 06596a60 Guido Trotter
    @type nicparams: dict
1730 06596a60 Guido Trotter
    @param nicparams: the dict to fill
1731 73e0328b Iustin Pop
    @rtype: dict
1732 73e0328b Iustin Pop
    @return: a copy of the passed in nicparams with missing keys filled
1733 73e0328b Iustin Pop
        from the cluster defaults
1734 73e0328b Iustin Pop

1735 73e0328b Iustin Pop
    """
1736 73e0328b Iustin Pop
    return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams)
1737 5bf7b5cf Iustin Pop
1738 1bdcbbab Iustin Pop
  def SimpleFillOS(self, os_name, os_params):
1739 1bdcbbab Iustin Pop
    """Fill an instance's osparams dict with cluster defaults.
1740 1bdcbbab Iustin Pop

1741 1bdcbbab Iustin Pop
    @type os_name: string
1742 1bdcbbab Iustin Pop
    @param os_name: the OS name to use
1743 1bdcbbab Iustin Pop
    @type os_params: dict
1744 1bdcbbab Iustin Pop
    @param os_params: the dict to fill with default values
1745 1bdcbbab Iustin Pop
    @rtype: dict
1746 1bdcbbab Iustin Pop
    @return: a copy of the instance's osparams with missing keys filled from
1747 1bdcbbab Iustin Pop
        the cluster defaults
1748 1bdcbbab Iustin Pop

1749 1bdcbbab Iustin Pop
    """
1750 1bdcbbab Iustin Pop
    name_only = os_name.split("+", 1)[0]
1751 1bdcbbab Iustin Pop
    # base OS
1752 1bdcbbab Iustin Pop
    result = self.osparams.get(name_only, {})
1753 1bdcbbab Iustin Pop
    # OS with variant
1754 1bdcbbab Iustin Pop
    result = FillDict(result, self.osparams.get(os_name, {}))
1755 1bdcbbab Iustin Pop
    # specified params
1756 1bdcbbab Iustin Pop
    return FillDict(result, os_params)
1757 1bdcbbab Iustin Pop
1758 2da9f556 René Nussbaumer
  @staticmethod
1759 2da9f556 René Nussbaumer
  def SimpleFillHvState(hv_state):
1760 2da9f556 René Nussbaumer
    """Fill an hv_state sub dict with cluster defaults.
1761 2da9f556 René Nussbaumer

1762 2da9f556 René Nussbaumer
    """
1763 2da9f556 René Nussbaumer
    return FillDict(constants.HVST_DEFAULTS, hv_state)
1764 2da9f556 René Nussbaumer
1765 2da9f556 René Nussbaumer
  @staticmethod
1766 2da9f556 René Nussbaumer
  def SimpleFillDiskState(disk_state):
1767 2da9f556 René Nussbaumer
    """Fill an disk_state sub dict with cluster defaults.
1768 2da9f556 René Nussbaumer

1769 2da9f556 René Nussbaumer
    """
1770 2da9f556 René Nussbaumer
    return FillDict(constants.DS_DEFAULTS, disk_state)
1771 2da9f556 René Nussbaumer
1772 095e71aa René Nussbaumer
  def FillND(self, node, nodegroup):
1773 ce523de1 Michael Hanselmann
    """Return filled out ndparams for L{objects.NodeGroup} and L{objects.Node}
1774 095e71aa René Nussbaumer

1775 095e71aa René Nussbaumer
    @type node: L{objects.Node}
1776 095e71aa René Nussbaumer
    @param node: A Node object to fill
1777 095e71aa René Nussbaumer
    @type nodegroup: L{objects.NodeGroup}
1778 095e71aa René Nussbaumer
    @param nodegroup: A Node object to fill
1779 095e71aa René Nussbaumer
    @return a copy of the node's ndparams with defaults filled
1780 095e71aa René Nussbaumer

1781 095e71aa René Nussbaumer
    """
1782 095e71aa René Nussbaumer
    return self.SimpleFillND(nodegroup.FillND(node))
1783 095e71aa René Nussbaumer
1784 095e71aa René Nussbaumer
  def SimpleFillND(self, ndparams):
1785 095e71aa René Nussbaumer
    """Fill a given ndparams dict with defaults.
1786 095e71aa René Nussbaumer

1787 095e71aa René Nussbaumer
    @type ndparams: dict
1788 095e71aa René Nussbaumer
    @param ndparams: the dict to fill
1789 095e71aa René Nussbaumer
    @rtype: dict
1790 095e71aa René Nussbaumer
    @return: a copy of the passed in ndparams with missing keys filled
1791 095e71aa René Nussbaumer
        from the cluster defaults
1792 095e71aa René Nussbaumer

1793 095e71aa René Nussbaumer
    """
1794 095e71aa René Nussbaumer
    return FillDict(self.ndparams, ndparams)
1795 095e71aa René Nussbaumer
1796 918eb80b Agata Murawska
  def SimpleFillIPolicy(self, ipolicy):
1797 918eb80b Agata Murawska
    """ Fill instance policy dict with defaults.
1798 918eb80b Agata Murawska

1799 918eb80b Agata Murawska
    @type ipolicy: dict
1800 918eb80b Agata Murawska
    @param ipolicy: the dict to fill
1801 918eb80b Agata Murawska
    @rtype: dict
1802 918eb80b Agata Murawska
    @return: a copy of passed ipolicy with missing keys filled from
1803 918eb80b Agata Murawska
      the cluster defaults
1804 918eb80b Agata Murawska

1805 918eb80b Agata Murawska
    """
1806 2cc673a3 Iustin Pop
    return FillIPolicy(self.ipolicy, ipolicy)
1807 918eb80b Agata Murawska
1808 5c947f38 Iustin Pop
1809 96acbc09 Michael Hanselmann
class BlockDevStatus(ConfigObject):
1810 96acbc09 Michael Hanselmann
  """Config object representing the status of a block device."""
1811 96acbc09 Michael Hanselmann
  __slots__ = [
1812 96acbc09 Michael Hanselmann
    "dev_path",
1813 96acbc09 Michael Hanselmann
    "major",
1814 96acbc09 Michael Hanselmann
    "minor",
1815 96acbc09 Michael Hanselmann
    "sync_percent",
1816 96acbc09 Michael Hanselmann
    "estimated_time",
1817 96acbc09 Michael Hanselmann
    "is_degraded",
1818 f208978a Michael Hanselmann
    "ldisk_status",
1819 96acbc09 Michael Hanselmann
    ]
1820 96acbc09 Michael Hanselmann
1821 96acbc09 Michael Hanselmann
1822 2d76b580 Michael Hanselmann
class ImportExportStatus(ConfigObject):
1823 2d76b580 Michael Hanselmann
  """Config object representing the status of an import or export."""
1824 2d76b580 Michael Hanselmann
  __slots__ = [
1825 2d76b580 Michael Hanselmann
    "recent_output",
1826 2d76b580 Michael Hanselmann
    "listen_port",
1827 2d76b580 Michael Hanselmann
    "connected",
1828 c08d76f5 Michael Hanselmann
    "progress_mbytes",
1829 c08d76f5 Michael Hanselmann
    "progress_throughput",
1830 c08d76f5 Michael Hanselmann
    "progress_eta",
1831 c08d76f5 Michael Hanselmann
    "progress_percent",
1832 2d76b580 Michael Hanselmann
    "exit_status",
1833 2d76b580 Michael Hanselmann
    "error_message",
1834 2d76b580 Michael Hanselmann
    ] + _TIMESTAMPS
1835 2d76b580 Michael Hanselmann
1836 2d76b580 Michael Hanselmann
1837 eb630f50 Michael Hanselmann
class ImportExportOptions(ConfigObject):
1838 eb630f50 Michael Hanselmann
  """Options for import/export daemon
1839 eb630f50 Michael Hanselmann

1840 eb630f50 Michael Hanselmann
  @ivar key_name: X509 key name (None for cluster certificate)
1841 eb630f50 Michael Hanselmann
  @ivar ca_pem: Remote peer CA in PEM format (None for cluster certificate)
1842 a5310c2a Michael Hanselmann
  @ivar compress: Compression method (one of L{constants.IEC_ALL})
1843 af1d39b1 Michael Hanselmann
  @ivar magic: Used to ensure the connection goes to the right disk
1844 855d2fc7 Michael Hanselmann
  @ivar ipv6: Whether to use IPv6
1845 4478301b Michael Hanselmann
  @ivar connect_timeout: Number of seconds for establishing connection
1846 eb630f50 Michael Hanselmann

1847 eb630f50 Michael Hanselmann
  """
1848 eb630f50 Michael Hanselmann
  __slots__ = [
1849 eb630f50 Michael Hanselmann
    "key_name",
1850 eb630f50 Michael Hanselmann
    "ca_pem",
1851 a5310c2a Michael Hanselmann
    "compress",
1852 af1d39b1 Michael Hanselmann
    "magic",
1853 855d2fc7 Michael Hanselmann
    "ipv6",
1854 4478301b Michael Hanselmann
    "connect_timeout",
1855 eb630f50 Michael Hanselmann
    ]
1856 eb630f50 Michael Hanselmann
1857 eb630f50 Michael Hanselmann
1858 18d750b9 Guido Trotter
class ConfdRequest(ConfigObject):
1859 18d750b9 Guido Trotter
  """Object holding a confd request.
1860 18d750b9 Guido Trotter

1861 18d750b9 Guido Trotter
  @ivar protocol: confd protocol version
1862 18d750b9 Guido Trotter
  @ivar type: confd query type
1863 18d750b9 Guido Trotter
  @ivar query: query request
1864 18d750b9 Guido Trotter
  @ivar rsalt: requested reply salt
1865 18d750b9 Guido Trotter

1866 18d750b9 Guido Trotter
  """
1867 18d750b9 Guido Trotter
  __slots__ = [
1868 18d750b9 Guido Trotter
    "protocol",
1869 18d750b9 Guido Trotter
    "type",
1870 18d750b9 Guido Trotter
    "query",
1871 18d750b9 Guido Trotter
    "rsalt",
1872 18d750b9 Guido Trotter
    ]
1873 18d750b9 Guido Trotter
1874 18d750b9 Guido Trotter
1875 18d750b9 Guido Trotter
class ConfdReply(ConfigObject):
1876 18d750b9 Guido Trotter
  """Object holding a confd reply.
1877 18d750b9 Guido Trotter

1878 18d750b9 Guido Trotter
  @ivar protocol: confd protocol version
1879 18d750b9 Guido Trotter
  @ivar status: reply status code (ok, error)
1880 18d750b9 Guido Trotter
  @ivar answer: confd query reply
1881 18d750b9 Guido Trotter
  @ivar serial: configuration serial number
1882 18d750b9 Guido Trotter

1883 18d750b9 Guido Trotter
  """
1884 18d750b9 Guido Trotter
  __slots__ = [
1885 18d750b9 Guido Trotter
    "protocol",
1886 18d750b9 Guido Trotter
    "status",
1887 18d750b9 Guido Trotter
    "answer",
1888 18d750b9 Guido Trotter
    "serial",
1889 18d750b9 Guido Trotter
    ]
1890 18d750b9 Guido Trotter
1891 18d750b9 Guido Trotter
1892 707f23b5 Michael Hanselmann
class QueryFieldDefinition(ConfigObject):
1893 707f23b5 Michael Hanselmann
  """Object holding a query field definition.
1894 707f23b5 Michael Hanselmann

1895 24d6d3e2 Michael Hanselmann
  @ivar name: Field name
1896 707f23b5 Michael Hanselmann
  @ivar title: Human-readable title
1897 707f23b5 Michael Hanselmann
  @ivar kind: Field type
1898 1ae17369 Michael Hanselmann
  @ivar doc: Human-readable description
1899 707f23b5 Michael Hanselmann

1900 707f23b5 Michael Hanselmann
  """
1901 707f23b5 Michael Hanselmann
  __slots__ = [
1902 707f23b5 Michael Hanselmann
    "name",
1903 707f23b5 Michael Hanselmann
    "title",
1904 707f23b5 Michael Hanselmann
    "kind",
1905 1ae17369 Michael Hanselmann
    "doc",
1906 707f23b5 Michael Hanselmann
    ]
1907 707f23b5 Michael Hanselmann
1908 707f23b5 Michael Hanselmann
1909 0538c375 Michael Hanselmann
class _QueryResponseBase(ConfigObject):
1910 0538c375 Michael Hanselmann
  __slots__ = [
1911 0538c375 Michael Hanselmann
    "fields",
1912 0538c375 Michael Hanselmann
    ]
1913 0538c375 Michael Hanselmann
1914 0538c375 Michael Hanselmann
  def ToDict(self):
1915 0538c375 Michael Hanselmann
    """Custom function for serializing.
1916 0538c375 Michael Hanselmann

1917 0538c375 Michael Hanselmann
    """
1918 0538c375 Michael Hanselmann
    mydict = super(_QueryResponseBase, self).ToDict()
1919 fe502d25 Iustin Pop
    mydict["fields"] = outils.ContainerToDicts(mydict["fields"])
1920 0538c375 Michael Hanselmann
    return mydict
1921 0538c375 Michael Hanselmann
1922 0538c375 Michael Hanselmann
  @classmethod
1923 0538c375 Michael Hanselmann
  def FromDict(cls, val):
1924 0538c375 Michael Hanselmann
    """Custom function for de-serializing.
1925 0538c375 Michael Hanselmann

1926 0538c375 Michael Hanselmann
    """
1927 0538c375 Michael Hanselmann
    obj = super(_QueryResponseBase, cls).FromDict(val)
1928 473ab806 Michael Hanselmann
    obj.fields = \
1929 fe502d25 Iustin Pop
      outils.ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
1930 0538c375 Michael Hanselmann
    return obj
1931 0538c375 Michael Hanselmann
1932 0538c375 Michael Hanselmann
1933 0538c375 Michael Hanselmann
class QueryResponse(_QueryResponseBase):
1934 24d6d3e2 Michael Hanselmann
  """Object holding the response to a query.
1935 24d6d3e2 Michael Hanselmann

1936 24d6d3e2 Michael Hanselmann
  @ivar fields: List of L{QueryFieldDefinition} objects
1937 24d6d3e2 Michael Hanselmann
  @ivar data: Requested data
1938 24d6d3e2 Michael Hanselmann

1939 24d6d3e2 Michael Hanselmann
  """
1940 24d6d3e2 Michael Hanselmann
  __slots__ = [
1941 24d6d3e2 Michael Hanselmann
    "data",
1942 24d6d3e2 Michael Hanselmann
    ]
1943 24d6d3e2 Michael Hanselmann
1944 24d6d3e2 Michael Hanselmann
1945 24d6d3e2 Michael Hanselmann
class QueryFieldsRequest(ConfigObject):
1946 24d6d3e2 Michael Hanselmann
  """Object holding a request for querying available fields.
1947 24d6d3e2 Michael Hanselmann

1948 24d6d3e2 Michael Hanselmann
  """
1949 24d6d3e2 Michael Hanselmann
  __slots__ = [
1950 24d6d3e2 Michael Hanselmann
    "what",
1951 24d6d3e2 Michael Hanselmann
    "fields",
1952 24d6d3e2 Michael Hanselmann
    ]
1953 24d6d3e2 Michael Hanselmann
1954 24d6d3e2 Michael Hanselmann
1955 0538c375 Michael Hanselmann
class QueryFieldsResponse(_QueryResponseBase):
1956 24d6d3e2 Michael Hanselmann
  """Object holding the response to a query for fields.
1957 24d6d3e2 Michael Hanselmann

1958 24d6d3e2 Michael Hanselmann
  @ivar fields: List of L{QueryFieldDefinition} objects
1959 24d6d3e2 Michael Hanselmann

1960 24d6d3e2 Michael Hanselmann
  """
1961 5ae4945a Iustin Pop
  __slots__ = []
1962 24d6d3e2 Michael Hanselmann
1963 24d6d3e2 Michael Hanselmann
1964 6a1434d7 Andrea Spadaccini
class MigrationStatus(ConfigObject):
1965 6a1434d7 Andrea Spadaccini
  """Object holding the status of a migration.
1966 6a1434d7 Andrea Spadaccini

1967 6a1434d7 Andrea Spadaccini
  """
1968 6a1434d7 Andrea Spadaccini
  __slots__ = [
1969 6a1434d7 Andrea Spadaccini
    "status",
1970 6a1434d7 Andrea Spadaccini
    "transferred_ram",
1971 6a1434d7 Andrea Spadaccini
    "total_ram",
1972 6a1434d7 Andrea Spadaccini
    ]
1973 6a1434d7 Andrea Spadaccini
1974 6a1434d7 Andrea Spadaccini
1975 25ce3ec4 Michael Hanselmann
class InstanceConsole(ConfigObject):
1976 25ce3ec4 Michael Hanselmann
  """Object describing how to access the console of an instance.
1977 25ce3ec4 Michael Hanselmann

1978 25ce3ec4 Michael Hanselmann
  """
1979 25ce3ec4 Michael Hanselmann
  __slots__ = [
1980 25ce3ec4 Michael Hanselmann
    "instance",
1981 25ce3ec4 Michael Hanselmann
    "kind",
1982 25ce3ec4 Michael Hanselmann
    "message",
1983 25ce3ec4 Michael Hanselmann
    "host",
1984 25ce3ec4 Michael Hanselmann
    "port",
1985 25ce3ec4 Michael Hanselmann
    "user",
1986 25ce3ec4 Michael Hanselmann
    "command",
1987 25ce3ec4 Michael Hanselmann
    "display",
1988 25ce3ec4 Michael Hanselmann
    ]
1989 25ce3ec4 Michael Hanselmann
1990 25ce3ec4 Michael Hanselmann
  def Validate(self):
1991 25ce3ec4 Michael Hanselmann
    """Validates contents of this object.
1992 25ce3ec4 Michael Hanselmann

1993 25ce3ec4 Michael Hanselmann
    """
1994 25ce3ec4 Michael Hanselmann
    assert self.kind in constants.CONS_ALL, "Unknown console type"
1995 25ce3ec4 Michael Hanselmann
    assert self.instance, "Missing instance name"
1996 4d2cdb5a Andrea Spadaccini
    assert self.message or self.kind in [constants.CONS_SSH,
1997 4d2cdb5a Andrea Spadaccini
                                         constants.CONS_SPICE,
1998 4d2cdb5a Andrea Spadaccini
                                         constants.CONS_VNC]
1999 25ce3ec4 Michael Hanselmann
    assert self.host or self.kind == constants.CONS_MESSAGE
2000 25ce3ec4 Michael Hanselmann
    assert self.port or self.kind in [constants.CONS_MESSAGE,
2001 25ce3ec4 Michael Hanselmann
                                      constants.CONS_SSH]
2002 25ce3ec4 Michael Hanselmann
    assert self.user or self.kind in [constants.CONS_MESSAGE,
2003 4d2cdb5a Andrea Spadaccini
                                      constants.CONS_SPICE,
2004 25ce3ec4 Michael Hanselmann
                                      constants.CONS_VNC]
2005 25ce3ec4 Michael Hanselmann
    assert self.command or self.kind in [constants.CONS_MESSAGE,
2006 4d2cdb5a Andrea Spadaccini
                                         constants.CONS_SPICE,
2007 25ce3ec4 Michael Hanselmann
                                         constants.CONS_VNC]
2008 25ce3ec4 Michael Hanselmann
    assert self.display or self.kind in [constants.CONS_MESSAGE,
2009 4d2cdb5a Andrea Spadaccini
                                         constants.CONS_SPICE,
2010 25ce3ec4 Michael Hanselmann
                                         constants.CONS_SSH]
2011 25ce3ec4 Michael Hanselmann
    return True
2012 25ce3ec4 Michael Hanselmann
2013 25ce3ec4 Michael Hanselmann
2014 8140e24f Dimitris Aragiorgis
class Network(TaggableObject):
2015 eaa4c57c Dimitris Aragiorgis
  """Object representing a network definition for ganeti.
2016 eaa4c57c Dimitris Aragiorgis

2017 eaa4c57c Dimitris Aragiorgis
  """
2018 eaa4c57c Dimitris Aragiorgis
  __slots__ = [
2019 eaa4c57c Dimitris Aragiorgis
    "name",
2020 eaa4c57c Dimitris Aragiorgis
    "serial_no",
2021 eaa4c57c Dimitris Aragiorgis
    "mac_prefix",
2022 eaa4c57c Dimitris Aragiorgis
    "network",
2023 eaa4c57c Dimitris Aragiorgis
    "network6",
2024 eaa4c57c Dimitris Aragiorgis
    "gateway",
2025 eaa4c57c Dimitris Aragiorgis
    "gateway6",
2026 eaa4c57c Dimitris Aragiorgis
    "reservations",
2027 eaa4c57c Dimitris Aragiorgis
    "ext_reservations",
2028 eaa4c57c Dimitris Aragiorgis
    ] + _TIMESTAMPS + _UUID
2029 eaa4c57c Dimitris Aragiorgis
2030 7e8f03e3 Dimitris Aragiorgis
  def HooksDict(self, prefix=""):
2031 d89168ff Guido Trotter
    """Export a dictionary used by hooks with a network's information.
2032 d89168ff Guido Trotter

2033 d89168ff Guido Trotter
    @type prefix: String
2034 d89168ff Guido Trotter
    @param prefix: Prefix to prepend to the dict entries
2035 d89168ff Guido Trotter

2036 d89168ff Guido Trotter
    """
2037 d89168ff Guido Trotter
    result = {
2038 7e8f03e3 Dimitris Aragiorgis
      "%sNETWORK_NAME" % prefix: self.name,
2039 d89168ff Guido Trotter
      "%sNETWORK_UUID" % prefix: self.uuid,
2040 5a76adf7 Dimitris Aragiorgis
      "%sNETWORK_TAGS" % prefix: " ".join(self.GetTags()),
2041 d89168ff Guido Trotter
    }
2042 d89168ff Guido Trotter
    if self.network:
2043 d89168ff Guido Trotter
      result["%sNETWORK_SUBNET" % prefix] = self.network
2044 d89168ff Guido Trotter
    if self.gateway:
2045 d89168ff Guido Trotter
      result["%sNETWORK_GATEWAY" % prefix] = self.gateway
2046 d89168ff Guido Trotter
    if self.network6:
2047 d89168ff Guido Trotter
      result["%sNETWORK_SUBNET6" % prefix] = self.network6
2048 d89168ff Guido Trotter
    if self.gateway6:
2049 d89168ff Guido Trotter
      result["%sNETWORK_GATEWAY6" % prefix] = self.gateway6
2050 d89168ff Guido Trotter
    if self.mac_prefix:
2051 d89168ff Guido Trotter
      result["%sNETWORK_MAC_PREFIX" % prefix] = self.mac_prefix
2052 d89168ff Guido Trotter
2053 d89168ff Guido Trotter
    return result
2054 d89168ff Guido Trotter
2055 5cfa6c37 Dimitris Aragiorgis
  @classmethod
2056 5cfa6c37 Dimitris Aragiorgis
  def FromDict(cls, val):
2057 5cfa6c37 Dimitris Aragiorgis
    """Custom function for networks.
2058 5cfa6c37 Dimitris Aragiorgis

2059 48616625 Dimitris Aragiorgis
    Remove deprecated network_type and family.
2060 5cfa6c37 Dimitris Aragiorgis

2061 5cfa6c37 Dimitris Aragiorgis
    """
2062 5cfa6c37 Dimitris Aragiorgis
    if "network_type" in val:
2063 5cfa6c37 Dimitris Aragiorgis
      del val["network_type"]
2064 48616625 Dimitris Aragiorgis
    if "family" in val:
2065 48616625 Dimitris Aragiorgis
      del val["family"]
2066 5cfa6c37 Dimitris Aragiorgis
    obj = super(Network, cls).FromDict(val)
2067 5cfa6c37 Dimitris Aragiorgis
    return obj
2068 5cfa6c37 Dimitris Aragiorgis
2069 eaa4c57c Dimitris Aragiorgis
2070 a8083063 Iustin Pop
class SerializableConfigParser(ConfigParser.SafeConfigParser):
2071 a8083063 Iustin Pop
  """Simple wrapper over ConfigParse that allows serialization.
2072 a8083063 Iustin Pop

2073 a8083063 Iustin Pop
  This class is basically ConfigParser.SafeConfigParser with two
2074 a8083063 Iustin Pop
  additional methods that allow it to serialize/unserialize to/from a
2075 a8083063 Iustin Pop
  buffer.
2076 a8083063 Iustin Pop

2077 a8083063 Iustin Pop
  """
2078 a8083063 Iustin Pop
  def Dumps(self):
2079 a8083063 Iustin Pop
    """Dump this instance and return the string representation."""
2080 a8083063 Iustin Pop
    buf = StringIO()
2081 a8083063 Iustin Pop
    self.write(buf)
2082 a8083063 Iustin Pop
    return buf.getvalue()
2083 a8083063 Iustin Pop
2084 b39bf4bb Guido Trotter
  @classmethod
2085 b39bf4bb Guido Trotter
  def Loads(cls, data):
2086 a8083063 Iustin Pop
    """Load data from a string."""
2087 a8083063 Iustin Pop
    buf = StringIO(data)
2088 b39bf4bb Guido Trotter
    cfp = cls()
2089 a8083063 Iustin Pop
    cfp.readfp(buf)
2090 a8083063 Iustin Pop
    return cfp
2091 59726e15 Bernardo Dal Seno
2092 59726e15 Bernardo Dal Seno
2093 59726e15 Bernardo Dal Seno
class LvmPvInfo(ConfigObject):
2094 59726e15 Bernardo Dal Seno
  """Information about an LVM physical volume (PV).
2095 59726e15 Bernardo Dal Seno

2096 59726e15 Bernardo Dal Seno
  @type name: string
2097 59726e15 Bernardo Dal Seno
  @ivar name: name of the PV
2098 59726e15 Bernardo Dal Seno
  @type vg_name: string
2099 59726e15 Bernardo Dal Seno
  @ivar vg_name: name of the volume group containing the PV
2100 59726e15 Bernardo Dal Seno
  @type size: float
2101 59726e15 Bernardo Dal Seno
  @ivar size: size of the PV in MiB
2102 59726e15 Bernardo Dal Seno
  @type free: float
2103 59726e15 Bernardo Dal Seno
  @ivar free: free space in the PV, in MiB
2104 59726e15 Bernardo Dal Seno
  @type attributes: string
2105 59726e15 Bernardo Dal Seno
  @ivar attributes: PV attributes
2106 b496abdb Bernardo Dal Seno
  @type lv_list: list of strings
2107 b496abdb Bernardo Dal Seno
  @ivar lv_list: names of the LVs hosted on the PV
2108 59726e15 Bernardo Dal Seno
  """
2109 59726e15 Bernardo Dal Seno
  __slots__ = [
2110 59726e15 Bernardo Dal Seno
    "name",
2111 59726e15 Bernardo Dal Seno
    "vg_name",
2112 59726e15 Bernardo Dal Seno
    "size",
2113 59726e15 Bernardo Dal Seno
    "free",
2114 59726e15 Bernardo Dal Seno
    "attributes",
2115 b496abdb Bernardo Dal Seno
    "lv_list"
2116 59726e15 Bernardo Dal Seno
    ]
2117 59726e15 Bernardo Dal Seno
2118 59726e15 Bernardo Dal Seno
  def IsEmpty(self):
2119 59726e15 Bernardo Dal Seno
    """Is this PV empty?
2120 59726e15 Bernardo Dal Seno

2121 59726e15 Bernardo Dal Seno
    """
2122 59726e15 Bernardo Dal Seno
    return self.size <= (self.free + 1)
2123 59726e15 Bernardo Dal Seno
2124 59726e15 Bernardo Dal Seno
  def IsAllocatable(self):
2125 59726e15 Bernardo Dal Seno
    """Is this PV allocatable?
2126 59726e15 Bernardo Dal Seno

2127 59726e15 Bernardo Dal Seno
    """
2128 59726e15 Bernardo Dal Seno
    return ("a" in self.attributes)