Statistics
| Branch: | Tag: | Revision:

root / lib / config.py @ d367b66c

History | View | Annotate | Download (49.5 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 7f93570a Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010 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
"""Configuration management for Ganeti
23 a8083063 Iustin Pop

24 319856a9 Michael Hanselmann
This module provides the interface to the Ganeti cluster configuration.
25 a8083063 Iustin Pop

26 319856a9 Michael Hanselmann
The configuration data is stored on every node but is updated on the master
27 319856a9 Michael Hanselmann
only. After each update, the master distributes the data to the other nodes.
28 a8083063 Iustin Pop

29 319856a9 Michael Hanselmann
Currently, the data storage format is JSON. YAML was slow and consuming too
30 319856a9 Michael Hanselmann
much memory.
31 a8083063 Iustin Pop

32 a8083063 Iustin Pop
"""
33 a8083063 Iustin Pop
34 d367b66c Manuel Franceschini
# pylint: disable-msg=R0904
35 d367b66c Manuel Franceschini
# R0904: Too many public methods
36 d367b66c Manuel Franceschini
37 a8083063 Iustin Pop
import os
38 a8083063 Iustin Pop
import random
39 d8470559 Michael Hanselmann
import logging
40 d693c864 Iustin Pop
import time
41 a8083063 Iustin Pop
42 a8083063 Iustin Pop
from ganeti import errors
43 f78ede4e Guido Trotter
from ganeti import locking
44 a8083063 Iustin Pop
from ganeti import utils
45 a8083063 Iustin Pop
from ganeti import constants
46 a8083063 Iustin Pop
from ganeti import rpc
47 a8083063 Iustin Pop
from ganeti import objects
48 8d14b30d Iustin Pop
from ganeti import serializer
49 0fbae49a Balazs Lecz
from ganeti import uidpool
50 a744b676 Manuel Franceschini
from ganeti import netutils
51 243cdbcc Michael Hanselmann
52 243cdbcc Michael Hanselmann
53 7f93570a Iustin Pop
_config_lock = locking.SharedLock("ConfigWriter")
54 f78ede4e Guido Trotter
55 4fae38c5 Guido Trotter
# job id used for resource management at config upgrade time
56 8d9c3bef Michael Hanselmann
_UPGRADE_CONFIG_JID = "jid-cfg-upgrade"
57 4fae38c5 Guido Trotter
58 f78ede4e Guido Trotter
59 5b263ed7 Michael Hanselmann
def _ValidateConfig(data):
60 c41eea6e Iustin Pop
  """Verifies that a configuration objects looks valid.
61 c41eea6e Iustin Pop

62 c41eea6e Iustin Pop
  This only verifies the version of the configuration.
63 c41eea6e Iustin Pop

64 c41eea6e Iustin Pop
  @raise errors.ConfigurationError: if the version differs from what
65 c41eea6e Iustin Pop
      we expect
66 c41eea6e Iustin Pop

67 c41eea6e Iustin Pop
  """
68 5b263ed7 Michael Hanselmann
  if data.version != constants.CONFIG_VERSION:
69 243cdbcc Michael Hanselmann
    raise errors.ConfigurationError("Cluster configuration version"
70 243cdbcc Michael Hanselmann
                                    " mismatch, got %s instead of %s" %
71 5b263ed7 Michael Hanselmann
                                    (data.version,
72 243cdbcc Michael Hanselmann
                                     constants.CONFIG_VERSION))
73 a8083063 Iustin Pop
74 319856a9 Michael Hanselmann
75 013da361 Guido Trotter
class TemporaryReservationManager:
76 013da361 Guido Trotter
  """A temporary resource reservation manager.
77 013da361 Guido Trotter

78 013da361 Guido Trotter
  This is used to reserve resources in a job, before using them, making sure
79 013da361 Guido Trotter
  other jobs cannot get them in the meantime.
80 013da361 Guido Trotter

81 013da361 Guido Trotter
  """
82 013da361 Guido Trotter
  def __init__(self):
83 013da361 Guido Trotter
    self._ec_reserved = {}
84 013da361 Guido Trotter
85 013da361 Guido Trotter
  def Reserved(self, resource):
86 013da361 Guido Trotter
    for holder_reserved in self._ec_reserved.items():
87 013da361 Guido Trotter
      if resource in holder_reserved:
88 013da361 Guido Trotter
        return True
89 013da361 Guido Trotter
    return False
90 013da361 Guido Trotter
91 013da361 Guido Trotter
  def Reserve(self, ec_id, resource):
92 013da361 Guido Trotter
    if self.Reserved(resource):
93 013da361 Guido Trotter
      raise errors.ReservationError("Duplicate reservation for resource: %s." %
94 013da361 Guido Trotter
                                    (resource))
95 013da361 Guido Trotter
    if ec_id not in self._ec_reserved:
96 013da361 Guido Trotter
      self._ec_reserved[ec_id] = set([resource])
97 013da361 Guido Trotter
    else:
98 013da361 Guido Trotter
      self._ec_reserved[ec_id].add(resource)
99 013da361 Guido Trotter
100 013da361 Guido Trotter
  def DropECReservations(self, ec_id):
101 013da361 Guido Trotter
    if ec_id in self._ec_reserved:
102 013da361 Guido Trotter
      del self._ec_reserved[ec_id]
103 013da361 Guido Trotter
104 013da361 Guido Trotter
  def GetReserved(self):
105 013da361 Guido Trotter
    all_reserved = set()
106 013da361 Guido Trotter
    for holder_reserved in self._ec_reserved.values():
107 013da361 Guido Trotter
      all_reserved.update(holder_reserved)
108 013da361 Guido Trotter
    return all_reserved
109 013da361 Guido Trotter
110 013da361 Guido Trotter
  def Generate(self, existing, generate_one_fn, ec_id):
111 013da361 Guido Trotter
    """Generate a new resource of this type
112 013da361 Guido Trotter

113 013da361 Guido Trotter
    """
114 013da361 Guido Trotter
    assert callable(generate_one_fn)
115 013da361 Guido Trotter
116 013da361 Guido Trotter
    all_elems = self.GetReserved()
117 013da361 Guido Trotter
    all_elems.update(existing)
118 013da361 Guido Trotter
    retries = 64
119 013da361 Guido Trotter
    while retries > 0:
120 013da361 Guido Trotter
      new_resource = generate_one_fn()
121 013da361 Guido Trotter
      if new_resource is not None and new_resource not in all_elems:
122 013da361 Guido Trotter
        break
123 013da361 Guido Trotter
    else:
124 013da361 Guido Trotter
      raise errors.ConfigurationError("Not able generate new resource"
125 013da361 Guido Trotter
                                      " (last tried: %s)" % new_resource)
126 013da361 Guido Trotter
    self.Reserve(ec_id, new_resource)
127 013da361 Guido Trotter
    return new_resource
128 013da361 Guido Trotter
129 013da361 Guido Trotter
130 a8083063 Iustin Pop
class ConfigWriter:
131 098c0958 Michael Hanselmann
  """The interface to the cluster configuration.
132 a8083063 Iustin Pop

133 d8aee57e Iustin Pop
  @ivar _temporary_lvs: reservation manager for temporary LVs
134 d8aee57e Iustin Pop
  @ivar _all_rms: a list of all temporary reservation managers
135 d8aee57e Iustin Pop

136 098c0958 Michael Hanselmann
  """
137 a8083063 Iustin Pop
  def __init__(self, cfg_file=None, offline=False):
138 14e15659 Iustin Pop
    self.write_count = 0
139 f78ede4e Guido Trotter
    self._lock = _config_lock
140 a8083063 Iustin Pop
    self._config_data = None
141 a8083063 Iustin Pop
    self._offline = offline
142 a8083063 Iustin Pop
    if cfg_file is None:
143 a8083063 Iustin Pop
      self._cfg_file = constants.CLUSTER_CONF_FILE
144 a8083063 Iustin Pop
    else:
145 a8083063 Iustin Pop
      self._cfg_file = cfg_file
146 4fae38c5 Guido Trotter
    self._temporary_ids = TemporaryReservationManager()
147 a81c53c9 Iustin Pop
    self._temporary_drbds = {}
148 36b66e6e Guido Trotter
    self._temporary_macs = TemporaryReservationManager()
149 afa1386e Guido Trotter
    self._temporary_secrets = TemporaryReservationManager()
150 d8aee57e Iustin Pop
    self._temporary_lvs = TemporaryReservationManager()
151 d8aee57e Iustin Pop
    self._all_rms = [self._temporary_ids, self._temporary_macs,
152 d8aee57e Iustin Pop
                     self._temporary_secrets, self._temporary_lvs]
153 89e1fc26 Iustin Pop
    # Note: in order to prevent errors when resolving our name in
154 89e1fc26 Iustin Pop
    # _DistributeConfig, we compute it here once and reuse it; it's
155 89e1fc26 Iustin Pop
    # better to raise an error before starting to modify the config
156 89e1fc26 Iustin Pop
    # file than after it was modified
157 a744b676 Manuel Franceschini
    self._my_hostname = netutils.HostInfo().name
158 3c7f6c44 Iustin Pop
    self._last_cluster_serial = -1
159 3d3a04bc Iustin Pop
    self._OpenConfig()
160 a8083063 Iustin Pop
161 a8083063 Iustin Pop
  # this method needs to be static, so that we can call it on the class
162 a8083063 Iustin Pop
  @staticmethod
163 a8083063 Iustin Pop
  def IsCluster():
164 a8083063 Iustin Pop
    """Check if the cluster is configured.
165 a8083063 Iustin Pop

166 a8083063 Iustin Pop
    """
167 a8083063 Iustin Pop
    return os.path.exists(constants.CLUSTER_CONF_FILE)
168 a8083063 Iustin Pop
169 36b66e6e Guido Trotter
  def _GenerateOneMAC(self):
170 36b66e6e Guido Trotter
    """Generate one mac address
171 36b66e6e Guido Trotter

172 36b66e6e Guido Trotter
    """
173 36b66e6e Guido Trotter
    prefix = self._config_data.cluster.mac_prefix
174 36b66e6e Guido Trotter
    byte1 = random.randrange(0, 256)
175 36b66e6e Guido Trotter
    byte2 = random.randrange(0, 256)
176 36b66e6e Guido Trotter
    byte3 = random.randrange(0, 256)
177 36b66e6e Guido Trotter
    mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
178 36b66e6e Guido Trotter
    return mac
179 36b66e6e Guido Trotter
180 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
181 36b66e6e Guido Trotter
  def GenerateMAC(self, ec_id):
182 a8083063 Iustin Pop
    """Generate a MAC for an instance.
183 a8083063 Iustin Pop

184 a8083063 Iustin Pop
    This should check the current instances for duplicates.
185 a8083063 Iustin Pop

186 a8083063 Iustin Pop
    """
187 36b66e6e Guido Trotter
    existing = self._AllMACs()
188 36b66e6e Guido Trotter
    return self._temporary_ids.Generate(existing, self._GenerateOneMAC, ec_id)
189 a8083063 Iustin Pop
190 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
191 36b66e6e Guido Trotter
  def ReserveMAC(self, mac, ec_id):
192 36b66e6e Guido Trotter
    """Reserve a MAC for an instance.
193 1862d460 Alexander Schreiber

194 1862d460 Alexander Schreiber
    This only checks instances managed by this cluster, it does not
195 1862d460 Alexander Schreiber
    check for potential collisions elsewhere.
196 1862d460 Alexander Schreiber

197 1862d460 Alexander Schreiber
    """
198 1862d460 Alexander Schreiber
    all_macs = self._AllMACs()
199 36b66e6e Guido Trotter
    if mac in all_macs:
200 36b66e6e Guido Trotter
      raise errors.ReservationError("mac already in use")
201 36b66e6e Guido Trotter
    else:
202 36b66e6e Guido Trotter
      self._temporary_macs.Reserve(mac, ec_id)
203 1862d460 Alexander Schreiber
204 f9518d38 Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
205 d8aee57e Iustin Pop
  def ReserveLV(self, lv_name, ec_id):
206 d8aee57e Iustin Pop
    """Reserve an VG/LV pair for an instance.
207 d8aee57e Iustin Pop

208 d8aee57e Iustin Pop
    @type lv_name: string
209 d8aee57e Iustin Pop
    @param lv_name: the logical volume name to reserve
210 d8aee57e Iustin Pop

211 d8aee57e Iustin Pop
    """
212 d8aee57e Iustin Pop
    all_lvs = self._AllLVs()
213 d8aee57e Iustin Pop
    if lv_name in all_lvs:
214 d8aee57e Iustin Pop
      raise errors.ReservationError("LV already in use")
215 d8aee57e Iustin Pop
    else:
216 d8aee57e Iustin Pop
      self._temporary_lvs.Reserve(lv_name, ec_id)
217 d8aee57e Iustin Pop
218 d8aee57e Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
219 afa1386e Guido Trotter
  def GenerateDRBDSecret(self, ec_id):
220 f9518d38 Iustin Pop
    """Generate a DRBD secret.
221 f9518d38 Iustin Pop

222 f9518d38 Iustin Pop
    This checks the current disks for duplicates.
223 f9518d38 Iustin Pop

224 f9518d38 Iustin Pop
    """
225 afa1386e Guido Trotter
    return self._temporary_secrets.Generate(self._AllDRBDSecrets(),
226 afa1386e Guido Trotter
                                            utils.GenerateSecret,
227 afa1386e Guido Trotter
                                            ec_id)
228 8d9c3bef Michael Hanselmann
229 34e54ebc Iustin Pop
  def _AllLVs(self):
230 923b1523 Iustin Pop
    """Compute the list of all LVs.
231 923b1523 Iustin Pop

232 923b1523 Iustin Pop
    """
233 923b1523 Iustin Pop
    lvnames = set()
234 923b1523 Iustin Pop
    for instance in self._config_data.instances.values():
235 923b1523 Iustin Pop
      node_data = instance.MapLVsByNode()
236 923b1523 Iustin Pop
      for lv_list in node_data.values():
237 923b1523 Iustin Pop
        lvnames.update(lv_list)
238 923b1523 Iustin Pop
    return lvnames
239 923b1523 Iustin Pop
240 34e54ebc Iustin Pop
  def _AllIDs(self, include_temporary):
241 34e54ebc Iustin Pop
    """Compute the list of all UUIDs and names we have.
242 34e54ebc Iustin Pop

243 34e54ebc Iustin Pop
    @type include_temporary: boolean
244 34e54ebc Iustin Pop
    @param include_temporary: whether to include the _temporary_ids set
245 34e54ebc Iustin Pop
    @rtype: set
246 34e54ebc Iustin Pop
    @return: a set of IDs
247 34e54ebc Iustin Pop

248 34e54ebc Iustin Pop
    """
249 34e54ebc Iustin Pop
    existing = set()
250 34e54ebc Iustin Pop
    if include_temporary:
251 4fae38c5 Guido Trotter
      existing.update(self._temporary_ids.GetReserved())
252 34e54ebc Iustin Pop
    existing.update(self._AllLVs())
253 34e54ebc Iustin Pop
    existing.update(self._config_data.instances.keys())
254 34e54ebc Iustin Pop
    existing.update(self._config_data.nodes.keys())
255 76d5d3a3 Iustin Pop
    existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid])
256 34e54ebc Iustin Pop
    return existing
257 34e54ebc Iustin Pop
258 4fae38c5 Guido Trotter
  def _GenerateUniqueID(self, ec_id):
259 430b923c Iustin Pop
    """Generate an unique UUID.
260 923b1523 Iustin Pop

261 923b1523 Iustin Pop
    This checks the current node, instances and disk names for
262 923b1523 Iustin Pop
    duplicates.
263 923b1523 Iustin Pop

264 c41eea6e Iustin Pop
    @rtype: string
265 c41eea6e Iustin Pop
    @return: the unique id
266 923b1523 Iustin Pop

267 923b1523 Iustin Pop
    """
268 4fae38c5 Guido Trotter
    existing = self._AllIDs(include_temporary=False)
269 4fae38c5 Guido Trotter
    return self._temporary_ids.Generate(existing, utils.NewUUID, ec_id)
270 923b1523 Iustin Pop
271 430b923c Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
272 4fae38c5 Guido Trotter
  def GenerateUniqueID(self, ec_id):
273 430b923c Iustin Pop
    """Generate an unique ID.
274 430b923c Iustin Pop

275 430b923c Iustin Pop
    This is just a wrapper over the unlocked version.
276 430b923c Iustin Pop

277 4fae38c5 Guido Trotter
    @type ec_id: string
278 4fae38c5 Guido Trotter
    @param ec_id: unique id for the job to reserve the id to
279 34d657ba Iustin Pop

280 34d657ba Iustin Pop
    """
281 4fae38c5 Guido Trotter
    return self._GenerateUniqueID(ec_id)
282 34d657ba Iustin Pop
283 a8083063 Iustin Pop
  def _AllMACs(self):
284 a8083063 Iustin Pop
    """Return all MACs present in the config.
285 a8083063 Iustin Pop

286 c41eea6e Iustin Pop
    @rtype: list
287 c41eea6e Iustin Pop
    @return: the list of all MACs
288 c41eea6e Iustin Pop

289 a8083063 Iustin Pop
    """
290 a8083063 Iustin Pop
    result = []
291 a8083063 Iustin Pop
    for instance in self._config_data.instances.values():
292 a8083063 Iustin Pop
      for nic in instance.nics:
293 a8083063 Iustin Pop
        result.append(nic.mac)
294 a8083063 Iustin Pop
295 a8083063 Iustin Pop
    return result
296 a8083063 Iustin Pop
297 f9518d38 Iustin Pop
  def _AllDRBDSecrets(self):
298 f9518d38 Iustin Pop
    """Return all DRBD secrets present in the config.
299 f9518d38 Iustin Pop

300 c41eea6e Iustin Pop
    @rtype: list
301 c41eea6e Iustin Pop
    @return: the list of all DRBD secrets
302 c41eea6e Iustin Pop

303 f9518d38 Iustin Pop
    """
304 f9518d38 Iustin Pop
    def helper(disk, result):
305 f9518d38 Iustin Pop
      """Recursively gather secrets from this disk."""
306 f9518d38 Iustin Pop
      if disk.dev_type == constants.DT_DRBD8:
307 f9518d38 Iustin Pop
        result.append(disk.logical_id[5])
308 f9518d38 Iustin Pop
      if disk.children:
309 f9518d38 Iustin Pop
        for child in disk.children:
310 f9518d38 Iustin Pop
          helper(child, result)
311 f9518d38 Iustin Pop
312 f9518d38 Iustin Pop
    result = []
313 f9518d38 Iustin Pop
    for instance in self._config_data.instances.values():
314 f9518d38 Iustin Pop
      for disk in instance.disks:
315 f9518d38 Iustin Pop
        helper(disk, result)
316 f9518d38 Iustin Pop
317 f9518d38 Iustin Pop
    return result
318 f9518d38 Iustin Pop
319 4b98ac29 Iustin Pop
  def _CheckDiskIDs(self, disk, l_ids, p_ids):
320 4b98ac29 Iustin Pop
    """Compute duplicate disk IDs
321 4b98ac29 Iustin Pop

322 4b98ac29 Iustin Pop
    @type disk: L{objects.Disk}
323 4b98ac29 Iustin Pop
    @param disk: the disk at which to start searching
324 4b98ac29 Iustin Pop
    @type l_ids: list
325 4b98ac29 Iustin Pop
    @param l_ids: list of current logical ids
326 4b98ac29 Iustin Pop
    @type p_ids: list
327 4b98ac29 Iustin Pop
    @param p_ids: list of current physical ids
328 4b98ac29 Iustin Pop
    @rtype: list
329 4b98ac29 Iustin Pop
    @return: a list of error messages
330 4b98ac29 Iustin Pop

331 4b98ac29 Iustin Pop
    """
332 4b98ac29 Iustin Pop
    result = []
333 25ae22e4 Iustin Pop
    if disk.logical_id is not None:
334 25ae22e4 Iustin Pop
      if disk.logical_id in l_ids:
335 25ae22e4 Iustin Pop
        result.append("duplicate logical id %s" % str(disk.logical_id))
336 25ae22e4 Iustin Pop
      else:
337 25ae22e4 Iustin Pop
        l_ids.append(disk.logical_id)
338 25ae22e4 Iustin Pop
    if disk.physical_id is not None:
339 25ae22e4 Iustin Pop
      if disk.physical_id in p_ids:
340 25ae22e4 Iustin Pop
        result.append("duplicate physical id %s" % str(disk.physical_id))
341 25ae22e4 Iustin Pop
      else:
342 25ae22e4 Iustin Pop
        p_ids.append(disk.physical_id)
343 4b98ac29 Iustin Pop
344 4b98ac29 Iustin Pop
    if disk.children:
345 4b98ac29 Iustin Pop
      for child in disk.children:
346 4b98ac29 Iustin Pop
        result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
347 4b98ac29 Iustin Pop
    return result
348 4b98ac29 Iustin Pop
349 4a89c54a Iustin Pop
  def _UnlockedVerifyConfig(self):
350 a8efbb40 Iustin Pop
    """Verify function.
351 a8efbb40 Iustin Pop

352 4a89c54a Iustin Pop
    @rtype: list
353 4a89c54a Iustin Pop
    @return: a list of error messages; a non-empty list signifies
354 4a89c54a Iustin Pop
        configuration errors
355 4a89c54a Iustin Pop

356 a8083063 Iustin Pop
    """
357 a8083063 Iustin Pop
    result = []
358 a8083063 Iustin Pop
    seen_macs = []
359 48ce9fd9 Iustin Pop
    ports = {}
360 a8083063 Iustin Pop
    data = self._config_data
361 4b98ac29 Iustin Pop
    seen_lids = []
362 4b98ac29 Iustin Pop
    seen_pids = []
363 9a5fba23 Guido Trotter
364 9a5fba23 Guido Trotter
    # global cluster checks
365 9a5fba23 Guido Trotter
    if not data.cluster.enabled_hypervisors:
366 9a5fba23 Guido Trotter
      result.append("enabled hypervisors list doesn't have any entries")
367 9a5fba23 Guido Trotter
    invalid_hvs = set(data.cluster.enabled_hypervisors) - constants.HYPER_TYPES
368 9a5fba23 Guido Trotter
    if invalid_hvs:
369 9a5fba23 Guido Trotter
      result.append("enabled hypervisors contains invalid entries: %s" %
370 9a5fba23 Guido Trotter
                    invalid_hvs)
371 9f3ac970 Iustin Pop
    missing_hvp = (set(data.cluster.enabled_hypervisors) -
372 9f3ac970 Iustin Pop
                   set(data.cluster.hvparams.keys()))
373 9f3ac970 Iustin Pop
    if missing_hvp:
374 9f3ac970 Iustin Pop
      result.append("hypervisor parameters missing for the enabled"
375 9f3ac970 Iustin Pop
                    " hypervisor(s) %s" % utils.CommaJoin(missing_hvp))
376 9a5fba23 Guido Trotter
377 9a5fba23 Guido Trotter
    if data.cluster.master_node not in data.nodes:
378 9a5fba23 Guido Trotter
      result.append("cluster has invalid primary node '%s'" %
379 9a5fba23 Guido Trotter
                    data.cluster.master_node)
380 9a5fba23 Guido Trotter
381 9a5fba23 Guido Trotter
    # per-instance checks
382 a8083063 Iustin Pop
    for instance_name in data.instances:
383 a8083063 Iustin Pop
      instance = data.instances[instance_name]
384 81196341 Iustin Pop
      if instance.name != instance_name:
385 81196341 Iustin Pop
        result.append("instance '%s' is indexed by wrong name '%s'" %
386 81196341 Iustin Pop
                      (instance.name, instance_name))
387 a8083063 Iustin Pop
      if instance.primary_node not in data.nodes:
388 8522ceeb Iustin Pop
        result.append("instance '%s' has invalid primary node '%s'" %
389 a8083063 Iustin Pop
                      (instance_name, instance.primary_node))
390 a8083063 Iustin Pop
      for snode in instance.secondary_nodes:
391 a8083063 Iustin Pop
        if snode not in data.nodes:
392 8522ceeb Iustin Pop
          result.append("instance '%s' has invalid secondary node '%s'" %
393 a8083063 Iustin Pop
                        (instance_name, snode))
394 a8083063 Iustin Pop
      for idx, nic in enumerate(instance.nics):
395 a8083063 Iustin Pop
        if nic.mac in seen_macs:
396 8522ceeb Iustin Pop
          result.append("instance '%s' has NIC %d mac %s duplicate" %
397 a8083063 Iustin Pop
                        (instance_name, idx, nic.mac))
398 a8083063 Iustin Pop
        else:
399 a8083063 Iustin Pop
          seen_macs.append(nic.mac)
400 48ce9fd9 Iustin Pop
401 48ce9fd9 Iustin Pop
      # gather the drbd ports for duplicate checks
402 48ce9fd9 Iustin Pop
      for dsk in instance.disks:
403 48ce9fd9 Iustin Pop
        if dsk.dev_type in constants.LDS_DRBD:
404 48ce9fd9 Iustin Pop
          tcp_port = dsk.logical_id[2]
405 48ce9fd9 Iustin Pop
          if tcp_port not in ports:
406 48ce9fd9 Iustin Pop
            ports[tcp_port] = []
407 48ce9fd9 Iustin Pop
          ports[tcp_port].append((instance.name, "drbd disk %s" % dsk.iv_name))
408 48ce9fd9 Iustin Pop
      # gather network port reservation
409 48ce9fd9 Iustin Pop
      net_port = getattr(instance, "network_port", None)
410 48ce9fd9 Iustin Pop
      if net_port is not None:
411 48ce9fd9 Iustin Pop
        if net_port not in ports:
412 48ce9fd9 Iustin Pop
          ports[net_port] = []
413 48ce9fd9 Iustin Pop
        ports[net_port].append((instance.name, "network port"))
414 48ce9fd9 Iustin Pop
415 332d0e37 Iustin Pop
      # instance disk verify
416 332d0e37 Iustin Pop
      for idx, disk in enumerate(instance.disks):
417 332d0e37 Iustin Pop
        result.extend(["instance '%s' disk %d error: %s" %
418 332d0e37 Iustin Pop
                       (instance.name, idx, msg) for msg in disk.Verify()])
419 4b98ac29 Iustin Pop
        result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
420 332d0e37 Iustin Pop
421 48ce9fd9 Iustin Pop
    # cluster-wide pool of free ports
422 a8efbb40 Iustin Pop
    for free_port in data.cluster.tcpudp_port_pool:
423 48ce9fd9 Iustin Pop
      if free_port not in ports:
424 48ce9fd9 Iustin Pop
        ports[free_port] = []
425 48ce9fd9 Iustin Pop
      ports[free_port].append(("cluster", "port marked as free"))
426 48ce9fd9 Iustin Pop
427 48ce9fd9 Iustin Pop
    # compute tcp/udp duplicate ports
428 48ce9fd9 Iustin Pop
    keys = ports.keys()
429 48ce9fd9 Iustin Pop
    keys.sort()
430 48ce9fd9 Iustin Pop
    for pnum in keys:
431 48ce9fd9 Iustin Pop
      pdata = ports[pnum]
432 48ce9fd9 Iustin Pop
      if len(pdata) > 1:
433 1f864b60 Iustin Pop
        txt = utils.CommaJoin(["%s/%s" % val for val in pdata])
434 48ce9fd9 Iustin Pop
        result.append("tcp/udp port %s has duplicates: %s" % (pnum, txt))
435 48ce9fd9 Iustin Pop
436 48ce9fd9 Iustin Pop
    # highest used tcp port check
437 48ce9fd9 Iustin Pop
    if keys:
438 a8efbb40 Iustin Pop
      if keys[-1] > data.cluster.highest_used_port:
439 48ce9fd9 Iustin Pop
        result.append("Highest used port mismatch, saved %s, computed %s" %
440 a8efbb40 Iustin Pop
                      (data.cluster.highest_used_port, keys[-1]))
441 a8efbb40 Iustin Pop
442 3a26773f Iustin Pop
    if not data.nodes[data.cluster.master_node].master_candidate:
443 3a26773f Iustin Pop
      result.append("Master node is not a master candidate")
444 3a26773f Iustin Pop
445 4a89c54a Iustin Pop
    # master candidate checks
446 e623dbe3 Guido Trotter
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
447 ec0292f1 Iustin Pop
    if mc_now < mc_max:
448 ec0292f1 Iustin Pop
      result.append("Not enough master candidates: actual %d, target %d" %
449 ec0292f1 Iustin Pop
                    (mc_now, mc_max))
450 48ce9fd9 Iustin Pop
451 5bf07049 Iustin Pop
    # node checks
452 81196341 Iustin Pop
    for node_name, node in data.nodes.items():
453 81196341 Iustin Pop
      if node.name != node_name:
454 81196341 Iustin Pop
        result.append("Node '%s' is indexed by wrong name '%s'" %
455 81196341 Iustin Pop
                      (node.name, node_name))
456 5bf07049 Iustin Pop
      if [node.master_candidate, node.drained, node.offline].count(True) > 1:
457 5bf07049 Iustin Pop
        result.append("Node %s state is invalid: master_candidate=%s,"
458 5bf07049 Iustin Pop
                      " drain=%s, offline=%s" %
459 5bf07049 Iustin Pop
                      (node.name, node.master_candidate, node.drain,
460 5bf07049 Iustin Pop
                       node.offline))
461 5bf07049 Iustin Pop
462 4a89c54a Iustin Pop
    # drbd minors check
463 1122eb25 Iustin Pop
    _, duplicates = self._UnlockedComputeDRBDMap()
464 4a89c54a Iustin Pop
    for node, minor, instance_a, instance_b in duplicates:
465 4a89c54a Iustin Pop
      result.append("DRBD minor %d on node %s is assigned twice to instances"
466 4a89c54a Iustin Pop
                    " %s and %s" % (minor, node, instance_a, instance_b))
467 4a89c54a Iustin Pop
468 0ce8f948 Iustin Pop
    # IP checks
469 b8716596 Michael Hanselmann
    default_nicparams = data.cluster.nicparams[constants.PP_DEFAULT]
470 b8716596 Michael Hanselmann
    ips = {}
471 b8716596 Michael Hanselmann
472 b8716596 Michael Hanselmann
    def _AddIpAddress(ip, name):
473 b8716596 Michael Hanselmann
      ips.setdefault(ip, []).append(name)
474 b8716596 Michael Hanselmann
475 b8716596 Michael Hanselmann
    _AddIpAddress(data.cluster.master_ip, "cluster_ip")
476 0ce8f948 Iustin Pop
477 0ce8f948 Iustin Pop
    for node in data.nodes.values():
478 b8716596 Michael Hanselmann
      _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
479 0ce8f948 Iustin Pop
      if node.secondary_ip != node.primary_ip:
480 b8716596 Michael Hanselmann
        _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
481 b8716596 Michael Hanselmann
482 b8716596 Michael Hanselmann
    for instance in data.instances.values():
483 b8716596 Michael Hanselmann
      for idx, nic in enumerate(instance.nics):
484 b8716596 Michael Hanselmann
        if nic.ip is None:
485 b8716596 Michael Hanselmann
          continue
486 b8716596 Michael Hanselmann
487 b8716596 Michael Hanselmann
        nicparams = objects.FillDict(default_nicparams, nic.nicparams)
488 b8716596 Michael Hanselmann
        nic_mode = nicparams[constants.NIC_MODE]
489 b8716596 Michael Hanselmann
        nic_link = nicparams[constants.NIC_LINK]
490 b8716596 Michael Hanselmann
491 b8716596 Michael Hanselmann
        if nic_mode == constants.NIC_MODE_BRIDGED:
492 b8716596 Michael Hanselmann
          link = "bridge:%s" % nic_link
493 b8716596 Michael Hanselmann
        elif nic_mode == constants.NIC_MODE_ROUTED:
494 b8716596 Michael Hanselmann
          link = "route:%s" % nic_link
495 b8716596 Michael Hanselmann
        else:
496 b8716596 Michael Hanselmann
          raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
497 b8716596 Michael Hanselmann
498 b8716596 Michael Hanselmann
        _AddIpAddress("%s/%s" % (link, nic.ip),
499 b8716596 Michael Hanselmann
                      "instance:%s/nic:%d" % (instance.name, idx))
500 0ce8f948 Iustin Pop
501 0ce8f948 Iustin Pop
    for ip, owners in ips.items():
502 0ce8f948 Iustin Pop
      if len(owners) > 1:
503 0ce8f948 Iustin Pop
        result.append("IP address %s is used by multiple owners: %s" %
504 1f864b60 Iustin Pop
                      (ip, utils.CommaJoin(owners)))
505 b8716596 Michael Hanselmann
506 a8083063 Iustin Pop
    return result
507 a8083063 Iustin Pop
508 4a89c54a Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
509 4a89c54a Iustin Pop
  def VerifyConfig(self):
510 4a89c54a Iustin Pop
    """Verify function.
511 4a89c54a Iustin Pop

512 4a89c54a Iustin Pop
    This is just a wrapper over L{_UnlockedVerifyConfig}.
513 4a89c54a Iustin Pop

514 4a89c54a Iustin Pop
    @rtype: list
515 4a89c54a Iustin Pop
    @return: a list of error messages; a non-empty list signifies
516 4a89c54a Iustin Pop
        configuration errors
517 4a89c54a Iustin Pop

518 4a89c54a Iustin Pop
    """
519 4a89c54a Iustin Pop
    return self._UnlockedVerifyConfig()
520 4a89c54a Iustin Pop
521 f78ede4e Guido Trotter
  def _UnlockedSetDiskID(self, disk, node_name):
522 a8083063 Iustin Pop
    """Convert the unique ID to the ID needed on the target nodes.
523 a8083063 Iustin Pop

524 a8083063 Iustin Pop
    This is used only for drbd, which needs ip/port configuration.
525 a8083063 Iustin Pop

526 a8083063 Iustin Pop
    The routine descends down and updates its children also, because
527 a8083063 Iustin Pop
    this helps when the only the top device is passed to the remote
528 a8083063 Iustin Pop
    node.
529 a8083063 Iustin Pop

530 f78ede4e Guido Trotter
    This function is for internal use, when the config lock is already held.
531 f78ede4e Guido Trotter

532 a8083063 Iustin Pop
    """
533 a8083063 Iustin Pop
    if disk.children:
534 a8083063 Iustin Pop
      for child in disk.children:
535 f78ede4e Guido Trotter
        self._UnlockedSetDiskID(child, node_name)
536 a8083063 Iustin Pop
537 a8083063 Iustin Pop
    if disk.logical_id is None and disk.physical_id is not None:
538 a8083063 Iustin Pop
      return
539 ffa1c0dc Iustin Pop
    if disk.dev_type == constants.LD_DRBD8:
540 f9518d38 Iustin Pop
      pnode, snode, port, pminor, sminor, secret = disk.logical_id
541 a8083063 Iustin Pop
      if node_name not in (pnode, snode):
542 3ecf6786 Iustin Pop
        raise errors.ConfigurationError("DRBD device not knowing node %s" %
543 3ecf6786 Iustin Pop
                                        node_name)
544 f78ede4e Guido Trotter
      pnode_info = self._UnlockedGetNodeInfo(pnode)
545 f78ede4e Guido Trotter
      snode_info = self._UnlockedGetNodeInfo(snode)
546 a8083063 Iustin Pop
      if pnode_info is None or snode_info is None:
547 a8083063 Iustin Pop
        raise errors.ConfigurationError("Can't find primary or secondary node"
548 a8083063 Iustin Pop
                                        " for %s" % str(disk))
549 ffa1c0dc Iustin Pop
      p_data = (pnode_info.secondary_ip, port)
550 ffa1c0dc Iustin Pop
      s_data = (snode_info.secondary_ip, port)
551 a8083063 Iustin Pop
      if pnode == node_name:
552 f9518d38 Iustin Pop
        disk.physical_id = p_data + s_data + (pminor, secret)
553 a8083063 Iustin Pop
      else: # it must be secondary, we tested above
554 f9518d38 Iustin Pop
        disk.physical_id = s_data + p_data + (sminor, secret)
555 a8083063 Iustin Pop
    else:
556 a8083063 Iustin Pop
      disk.physical_id = disk.logical_id
557 a8083063 Iustin Pop
    return
558 a8083063 Iustin Pop
559 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
560 f78ede4e Guido Trotter
  def SetDiskID(self, disk, node_name):
561 f78ede4e Guido Trotter
    """Convert the unique ID to the ID needed on the target nodes.
562 f78ede4e Guido Trotter

563 f78ede4e Guido Trotter
    This is used only for drbd, which needs ip/port configuration.
564 f78ede4e Guido Trotter

565 f78ede4e Guido Trotter
    The routine descends down and updates its children also, because
566 f78ede4e Guido Trotter
    this helps when the only the top device is passed to the remote
567 f78ede4e Guido Trotter
    node.
568 f78ede4e Guido Trotter

569 f78ede4e Guido Trotter
    """
570 f78ede4e Guido Trotter
    return self._UnlockedSetDiskID(disk, node_name)
571 f78ede4e Guido Trotter
572 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
573 b2fddf63 Iustin Pop
  def AddTcpUdpPort(self, port):
574 b2fddf63 Iustin Pop
    """Adds a new port to the available port pool.
575 b2fddf63 Iustin Pop

576 b2fddf63 Iustin Pop
    """
577 264bb3c5 Michael Hanselmann
    if not isinstance(port, int):
578 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Invalid type passed for port")
579 264bb3c5 Michael Hanselmann
580 b2fddf63 Iustin Pop
    self._config_data.cluster.tcpudp_port_pool.add(port)
581 264bb3c5 Michael Hanselmann
    self._WriteConfig()
582 264bb3c5 Michael Hanselmann
583 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
584 b2fddf63 Iustin Pop
  def GetPortList(self):
585 264bb3c5 Michael Hanselmann
    """Returns a copy of the current port list.
586 264bb3c5 Michael Hanselmann

587 264bb3c5 Michael Hanselmann
    """
588 b2fddf63 Iustin Pop
    return self._config_data.cluster.tcpudp_port_pool.copy()
589 264bb3c5 Michael Hanselmann
590 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
591 a8083063 Iustin Pop
  def AllocatePort(self):
592 a8083063 Iustin Pop
    """Allocate a port.
593 a8083063 Iustin Pop

594 b2fddf63 Iustin Pop
    The port will be taken from the available port pool or from the
595 b2fddf63 Iustin Pop
    default port range (and in this case we increase
596 b2fddf63 Iustin Pop
    highest_used_port).
597 a8083063 Iustin Pop

598 a8083063 Iustin Pop
    """
599 264bb3c5 Michael Hanselmann
    # If there are TCP/IP ports configured, we use them first.
600 b2fddf63 Iustin Pop
    if self._config_data.cluster.tcpudp_port_pool:
601 b2fddf63 Iustin Pop
      port = self._config_data.cluster.tcpudp_port_pool.pop()
602 264bb3c5 Michael Hanselmann
    else:
603 264bb3c5 Michael Hanselmann
      port = self._config_data.cluster.highest_used_port + 1
604 264bb3c5 Michael Hanselmann
      if port >= constants.LAST_DRBD_PORT:
605 3ecf6786 Iustin Pop
        raise errors.ConfigurationError("The highest used port is greater"
606 3ecf6786 Iustin Pop
                                        " than %s. Aborting." %
607 3ecf6786 Iustin Pop
                                        constants.LAST_DRBD_PORT)
608 264bb3c5 Michael Hanselmann
      self._config_data.cluster.highest_used_port = port
609 a8083063 Iustin Pop
610 a8083063 Iustin Pop
    self._WriteConfig()
611 a8083063 Iustin Pop
    return port
612 a8083063 Iustin Pop
613 6d2e83d5 Iustin Pop
  def _UnlockedComputeDRBDMap(self):
614 a81c53c9 Iustin Pop
    """Compute the used DRBD minor/nodes.
615 a81c53c9 Iustin Pop

616 4a89c54a Iustin Pop
    @rtype: (dict, list)
617 c41eea6e Iustin Pop
    @return: dictionary of node_name: dict of minor: instance_name;
618 c41eea6e Iustin Pop
        the returned dict will have all the nodes in it (even if with
619 4a89c54a Iustin Pop
        an empty list), and a list of duplicates; if the duplicates
620 4a89c54a Iustin Pop
        list is not empty, the configuration is corrupted and its caller
621 4a89c54a Iustin Pop
        should raise an exception
622 a81c53c9 Iustin Pop

623 a81c53c9 Iustin Pop
    """
624 a81c53c9 Iustin Pop
    def _AppendUsedPorts(instance_name, disk, used):
625 4a89c54a Iustin Pop
      duplicates = []
626 f9518d38 Iustin Pop
      if disk.dev_type == constants.LD_DRBD8 and len(disk.logical_id) >= 5:
627 7c4d6c7b Michael Hanselmann
        node_a, node_b, _, minor_a, minor_b = disk.logical_id[:5]
628 7c4d6c7b Michael Hanselmann
        for node, port in ((node_a, minor_a), (node_b, minor_b)):
629 4a89c54a Iustin Pop
          assert node in used, ("Node '%s' of instance '%s' not found"
630 4a89c54a Iustin Pop
                                " in node list" % (node, instance_name))
631 a81c53c9 Iustin Pop
          if port in used[node]:
632 4a89c54a Iustin Pop
            duplicates.append((node, port, instance_name, used[node][port]))
633 4a89c54a Iustin Pop
          else:
634 4a89c54a Iustin Pop
            used[node][port] = instance_name
635 a81c53c9 Iustin Pop
      if disk.children:
636 a81c53c9 Iustin Pop
        for child in disk.children:
637 4a89c54a Iustin Pop
          duplicates.extend(_AppendUsedPorts(instance_name, child, used))
638 4a89c54a Iustin Pop
      return duplicates
639 a81c53c9 Iustin Pop
640 4a89c54a Iustin Pop
    duplicates = []
641 a81c53c9 Iustin Pop
    my_dict = dict((node, {}) for node in self._config_data.nodes)
642 79b26a7a Iustin Pop
    for instance in self._config_data.instances.itervalues():
643 79b26a7a Iustin Pop
      for disk in instance.disks:
644 79b26a7a Iustin Pop
        duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict))
645 a81c53c9 Iustin Pop
    for (node, minor), instance in self._temporary_drbds.iteritems():
646 79b26a7a Iustin Pop
      if minor in my_dict[node] and my_dict[node][minor] != instance:
647 4a89c54a Iustin Pop
        duplicates.append((node, minor, instance, my_dict[node][minor]))
648 4a89c54a Iustin Pop
      else:
649 4a89c54a Iustin Pop
        my_dict[node][minor] = instance
650 4a89c54a Iustin Pop
    return my_dict, duplicates
651 a81c53c9 Iustin Pop
652 a81c53c9 Iustin Pop
  @locking.ssynchronized(_config_lock)
653 6d2e83d5 Iustin Pop
  def ComputeDRBDMap(self):
654 6d2e83d5 Iustin Pop
    """Compute the used DRBD minor/nodes.
655 6d2e83d5 Iustin Pop

656 6d2e83d5 Iustin Pop
    This is just a wrapper over L{_UnlockedComputeDRBDMap}.
657 6d2e83d5 Iustin Pop

658 6d2e83d5 Iustin Pop
    @return: dictionary of node_name: dict of minor: instance_name;
659 6d2e83d5 Iustin Pop
        the returned dict will have all the nodes in it (even if with
660 6d2e83d5 Iustin Pop
        an empty list).
661 6d2e83d5 Iustin Pop

662 6d2e83d5 Iustin Pop
    """
663 4a89c54a Iustin Pop
    d_map, duplicates = self._UnlockedComputeDRBDMap()
664 4a89c54a Iustin Pop
    if duplicates:
665 4a89c54a Iustin Pop
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
666 4a89c54a Iustin Pop
                                      str(duplicates))
667 4a89c54a Iustin Pop
    return d_map
668 6d2e83d5 Iustin Pop
669 6d2e83d5 Iustin Pop
  @locking.ssynchronized(_config_lock)
670 a81c53c9 Iustin Pop
  def AllocateDRBDMinor(self, nodes, instance):
671 a81c53c9 Iustin Pop
    """Allocate a drbd minor.
672 a81c53c9 Iustin Pop

673 a81c53c9 Iustin Pop
    The free minor will be automatically computed from the existing
674 a81c53c9 Iustin Pop
    devices. A node can be given multiple times in order to allocate
675 a81c53c9 Iustin Pop
    multiple minors. The result is the list of minors, in the same
676 a81c53c9 Iustin Pop
    order as the passed nodes.
677 a81c53c9 Iustin Pop

678 32388e6d Iustin Pop
    @type instance: string
679 32388e6d Iustin Pop
    @param instance: the instance for which we allocate minors
680 32388e6d Iustin Pop

681 a81c53c9 Iustin Pop
    """
682 32388e6d Iustin Pop
    assert isinstance(instance, basestring), \
683 4a89c54a Iustin Pop
           "Invalid argument '%s' passed to AllocateDRBDMinor" % instance
684 32388e6d Iustin Pop
685 4a89c54a Iustin Pop
    d_map, duplicates = self._UnlockedComputeDRBDMap()
686 4a89c54a Iustin Pop
    if duplicates:
687 4a89c54a Iustin Pop
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
688 4a89c54a Iustin Pop
                                      str(duplicates))
689 a81c53c9 Iustin Pop
    result = []
690 a81c53c9 Iustin Pop
    for nname in nodes:
691 a81c53c9 Iustin Pop
      ndata = d_map[nname]
692 a81c53c9 Iustin Pop
      if not ndata:
693 a81c53c9 Iustin Pop
        # no minors used, we can start at 0
694 a81c53c9 Iustin Pop
        result.append(0)
695 a81c53c9 Iustin Pop
        ndata[0] = instance
696 d48663e4 Iustin Pop
        self._temporary_drbds[(nname, 0)] = instance
697 a81c53c9 Iustin Pop
        continue
698 a81c53c9 Iustin Pop
      keys = ndata.keys()
699 a81c53c9 Iustin Pop
      keys.sort()
700 a81c53c9 Iustin Pop
      ffree = utils.FirstFree(keys)
701 a81c53c9 Iustin Pop
      if ffree is None:
702 a81c53c9 Iustin Pop
        # return the next minor
703 a81c53c9 Iustin Pop
        # TODO: implement high-limit check
704 a81c53c9 Iustin Pop
        minor = keys[-1] + 1
705 a81c53c9 Iustin Pop
      else:
706 a81c53c9 Iustin Pop
        minor = ffree
707 4a89c54a Iustin Pop
      # double-check minor against current instances
708 4a89c54a Iustin Pop
      assert minor not in d_map[nname], \
709 4a89c54a Iustin Pop
             ("Attempt to reuse allocated DRBD minor %d on node %s,"
710 4a89c54a Iustin Pop
              " already allocated to instance %s" %
711 4a89c54a Iustin Pop
              (minor, nname, d_map[nname][minor]))
712 a81c53c9 Iustin Pop
      ndata[minor] = instance
713 4a89c54a Iustin Pop
      # double-check minor against reservation
714 4a89c54a Iustin Pop
      r_key = (nname, minor)
715 4a89c54a Iustin Pop
      assert r_key not in self._temporary_drbds, \
716 4a89c54a Iustin Pop
             ("Attempt to reuse reserved DRBD minor %d on node %s,"
717 4a89c54a Iustin Pop
              " reserved for instance %s" %
718 4a89c54a Iustin Pop
              (minor, nname, self._temporary_drbds[r_key]))
719 4a89c54a Iustin Pop
      self._temporary_drbds[r_key] = instance
720 4a89c54a Iustin Pop
      result.append(minor)
721 a81c53c9 Iustin Pop
    logging.debug("Request to allocate drbd minors, input: %s, returning %s",
722 a81c53c9 Iustin Pop
                  nodes, result)
723 a81c53c9 Iustin Pop
    return result
724 a81c53c9 Iustin Pop
725 61cf6b5e Iustin Pop
  def _UnlockedReleaseDRBDMinors(self, instance):
726 a81c53c9 Iustin Pop
    """Release temporary drbd minors allocated for a given instance.
727 a81c53c9 Iustin Pop

728 a81c53c9 Iustin Pop
    @type instance: string
729 a81c53c9 Iustin Pop
    @param instance: the instance for which temporary minors should be
730 a81c53c9 Iustin Pop
                     released
731 a81c53c9 Iustin Pop

732 a81c53c9 Iustin Pop
    """
733 32388e6d Iustin Pop
    assert isinstance(instance, basestring), \
734 32388e6d Iustin Pop
           "Invalid argument passed to ReleaseDRBDMinors"
735 a81c53c9 Iustin Pop
    for key, name in self._temporary_drbds.items():
736 a81c53c9 Iustin Pop
      if name == instance:
737 a81c53c9 Iustin Pop
        del self._temporary_drbds[key]
738 a81c53c9 Iustin Pop
739 61cf6b5e Iustin Pop
  @locking.ssynchronized(_config_lock)
740 61cf6b5e Iustin Pop
  def ReleaseDRBDMinors(self, instance):
741 61cf6b5e Iustin Pop
    """Release temporary drbd minors allocated for a given instance.
742 61cf6b5e Iustin Pop

743 61cf6b5e Iustin Pop
    This should be called on the error paths, on the success paths
744 61cf6b5e Iustin Pop
    it's automatically called by the ConfigWriter add and update
745 61cf6b5e Iustin Pop
    functions.
746 61cf6b5e Iustin Pop

747 61cf6b5e Iustin Pop
    This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
748 61cf6b5e Iustin Pop

749 61cf6b5e Iustin Pop
    @type instance: string
750 61cf6b5e Iustin Pop
    @param instance: the instance for which temporary minors should be
751 61cf6b5e Iustin Pop
                     released
752 61cf6b5e Iustin Pop

753 61cf6b5e Iustin Pop
    """
754 61cf6b5e Iustin Pop
    self._UnlockedReleaseDRBDMinors(instance)
755 61cf6b5e Iustin Pop
756 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
757 4a8b186a Michael Hanselmann
  def GetConfigVersion(self):
758 4a8b186a Michael Hanselmann
    """Get the configuration version.
759 4a8b186a Michael Hanselmann

760 4a8b186a Michael Hanselmann
    @return: Config version
761 4a8b186a Michael Hanselmann

762 4a8b186a Michael Hanselmann
    """
763 4a8b186a Michael Hanselmann
    return self._config_data.version
764 4a8b186a Michael Hanselmann
765 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
766 4a8b186a Michael Hanselmann
  def GetClusterName(self):
767 4a8b186a Michael Hanselmann
    """Get cluster name.
768 4a8b186a Michael Hanselmann

769 4a8b186a Michael Hanselmann
    @return: Cluster name
770 4a8b186a Michael Hanselmann

771 4a8b186a Michael Hanselmann
    """
772 4a8b186a Michael Hanselmann
    return self._config_data.cluster.cluster_name
773 4a8b186a Michael Hanselmann
774 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
775 4a8b186a Michael Hanselmann
  def GetMasterNode(self):
776 4a8b186a Michael Hanselmann
    """Get the hostname of the master node for this cluster.
777 4a8b186a Michael Hanselmann

778 4a8b186a Michael Hanselmann
    @return: Master hostname
779 4a8b186a Michael Hanselmann

780 4a8b186a Michael Hanselmann
    """
781 4a8b186a Michael Hanselmann
    return self._config_data.cluster.master_node
782 4a8b186a Michael Hanselmann
783 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
784 4a8b186a Michael Hanselmann
  def GetMasterIP(self):
785 4a8b186a Michael Hanselmann
    """Get the IP of the master node for this cluster.
786 4a8b186a Michael Hanselmann

787 4a8b186a Michael Hanselmann
    @return: Master IP
788 4a8b186a Michael Hanselmann

789 4a8b186a Michael Hanselmann
    """
790 4a8b186a Michael Hanselmann
    return self._config_data.cluster.master_ip
791 4a8b186a Michael Hanselmann
792 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
793 4a8b186a Michael Hanselmann
  def GetMasterNetdev(self):
794 4a8b186a Michael Hanselmann
    """Get the master network device for this cluster.
795 4a8b186a Michael Hanselmann

796 4a8b186a Michael Hanselmann
    """
797 4a8b186a Michael Hanselmann
    return self._config_data.cluster.master_netdev
798 4a8b186a Michael Hanselmann
799 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
800 4a8b186a Michael Hanselmann
  def GetFileStorageDir(self):
801 4a8b186a Michael Hanselmann
    """Get the file storage dir for this cluster.
802 4a8b186a Michael Hanselmann

803 4a8b186a Michael Hanselmann
    """
804 4a8b186a Michael Hanselmann
    return self._config_data.cluster.file_storage_dir
805 4a8b186a Michael Hanselmann
806 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
807 4a8b186a Michael Hanselmann
  def GetHypervisorType(self):
808 4a8b186a Michael Hanselmann
    """Get the hypervisor type for this cluster.
809 4a8b186a Michael Hanselmann

810 4a8b186a Michael Hanselmann
    """
811 066f465d Guido Trotter
    return self._config_data.cluster.enabled_hypervisors[0]
812 4a8b186a Michael Hanselmann
813 4a8b186a Michael Hanselmann
  @locking.ssynchronized(_config_lock, shared=1)
814 a8083063 Iustin Pop
  def GetHostKey(self):
815 a8083063 Iustin Pop
    """Return the rsa hostkey from the config.
816 a8083063 Iustin Pop

817 c41eea6e Iustin Pop
    @rtype: string
818 c41eea6e Iustin Pop
    @return: the rsa hostkey
819 a8083063 Iustin Pop

820 a8083063 Iustin Pop
    """
821 a8083063 Iustin Pop
    return self._config_data.cluster.rsahostkeypub
822 a8083063 Iustin Pop
823 bf4af505 Apollon Oikonomopoulos
  @locking.ssynchronized(_config_lock, shared=1)
824 bf4af505 Apollon Oikonomopoulos
  def GetDefaultIAllocator(self):
825 bf4af505 Apollon Oikonomopoulos
    """Get the default instance allocator for this cluster.
826 bf4af505 Apollon Oikonomopoulos

827 bf4af505 Apollon Oikonomopoulos
    """
828 bf4af505 Apollon Oikonomopoulos
    return self._config_data.cluster.default_iallocator
829 bf4af505 Apollon Oikonomopoulos
830 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
831 0debfb35 Guido Trotter
  def AddInstance(self, instance, ec_id):
832 a8083063 Iustin Pop
    """Add an instance to the config.
833 a8083063 Iustin Pop

834 a8083063 Iustin Pop
    This should be used after creating a new instance.
835 a8083063 Iustin Pop

836 c41eea6e Iustin Pop
    @type instance: L{objects.Instance}
837 c41eea6e Iustin Pop
    @param instance: the instance object
838 c41eea6e Iustin Pop

839 a8083063 Iustin Pop
    """
840 a8083063 Iustin Pop
    if not isinstance(instance, objects.Instance):
841 a8083063 Iustin Pop
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
842 a8083063 Iustin Pop
843 e00fb268 Iustin Pop
    if instance.disk_template != constants.DT_DISKLESS:
844 e00fb268 Iustin Pop
      all_lvs = instance.MapLVsByNode()
845 74a48621 Iustin Pop
      logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
846 923b1523 Iustin Pop
847 e4640214 Guido Trotter
    all_macs = self._AllMACs()
848 e4640214 Guido Trotter
    for nic in instance.nics:
849 e4640214 Guido Trotter
      if nic.mac in all_macs:
850 e4640214 Guido Trotter
        raise errors.ConfigurationError("Cannot add instance %s:"
851 430b923c Iustin Pop
                                        " MAC address '%s' already in use." %
852 430b923c Iustin Pop
                                        (instance.name, nic.mac))
853 430b923c Iustin Pop
854 0debfb35 Guido Trotter
    self._EnsureUUID(instance, ec_id)
855 e4640214 Guido Trotter
856 b989e85d Iustin Pop
    instance.serial_no = 1
857 d693c864 Iustin Pop
    instance.ctime = instance.mtime = time.time()
858 a8083063 Iustin Pop
    self._config_data.instances[instance.name] = instance
859 81a49123 Iustin Pop
    self._config_data.cluster.serial_no += 1
860 61cf6b5e Iustin Pop
    self._UnlockedReleaseDRBDMinors(instance.name)
861 a8083063 Iustin Pop
    self._WriteConfig()
862 a8083063 Iustin Pop
863 0debfb35 Guido Trotter
  def _EnsureUUID(self, item, ec_id):
864 430b923c Iustin Pop
    """Ensures a given object has a valid UUID.
865 430b923c Iustin Pop

866 430b923c Iustin Pop
    @param item: the instance or node to be checked
867 0debfb35 Guido Trotter
    @param ec_id: the execution context id for the uuid reservation
868 430b923c Iustin Pop

869 430b923c Iustin Pop
    """
870 430b923c Iustin Pop
    if not item.uuid:
871 4fae38c5 Guido Trotter
      item.uuid = self._GenerateUniqueID(ec_id)
872 be0fc05d Iustin Pop
    elif item.uuid in self._AllIDs(include_temporary=True):
873 be0fc05d Iustin Pop
      raise errors.ConfigurationError("Cannot add '%s': UUID %s already"
874 be0fc05d Iustin Pop
                                      " in use" % (item.name, item.uuid))
875 430b923c Iustin Pop
876 6a408fb2 Iustin Pop
  def _SetInstanceStatus(self, instance_name, status):
877 6a408fb2 Iustin Pop
    """Set the instance's status to a given value.
878 a8083063 Iustin Pop

879 a8083063 Iustin Pop
    """
880 0d68c45d Iustin Pop
    assert isinstance(status, bool), \
881 0d68c45d Iustin Pop
           "Invalid status '%s' passed to SetInstanceStatus" % (status,)
882 a8083063 Iustin Pop
883 a8083063 Iustin Pop
    if instance_name not in self._config_data.instances:
884 3ecf6786 Iustin Pop
      raise errors.ConfigurationError("Unknown instance '%s'" %
885 3ecf6786 Iustin Pop
                                      instance_name)
886 a8083063 Iustin Pop
    instance = self._config_data.instances[instance_name]
887 0d68c45d Iustin Pop
    if instance.admin_up != status:
888 0d68c45d Iustin Pop
      instance.admin_up = status
889 b989e85d Iustin Pop
      instance.serial_no += 1
890 d693c864 Iustin Pop
      instance.mtime = time.time()
891 455a3445 Iustin Pop
      self._WriteConfig()
892 a8083063 Iustin Pop
893 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
894 6a408fb2 Iustin Pop
  def MarkInstanceUp(self, instance_name):
895 6a408fb2 Iustin Pop
    """Mark the instance status to up in the config.
896 6a408fb2 Iustin Pop

897 6a408fb2 Iustin Pop
    """
898 0d68c45d Iustin Pop
    self._SetInstanceStatus(instance_name, True)
899 6a408fb2 Iustin Pop
900 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
901 a8083063 Iustin Pop
  def RemoveInstance(self, instance_name):
902 a8083063 Iustin Pop
    """Remove the instance from the configuration.
903 a8083063 Iustin Pop

904 a8083063 Iustin Pop
    """
905 a8083063 Iustin Pop
    if instance_name not in self._config_data.instances:
906 3ecf6786 Iustin Pop
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
907 a8083063 Iustin Pop
    del self._config_data.instances[instance_name]
908 81a49123 Iustin Pop
    self._config_data.cluster.serial_no += 1
909 a8083063 Iustin Pop
    self._WriteConfig()
910 a8083063 Iustin Pop
911 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
912 fc95f88f Iustin Pop
  def RenameInstance(self, old_name, new_name):
913 fc95f88f Iustin Pop
    """Rename an instance.
914 fc95f88f Iustin Pop

915 fc95f88f Iustin Pop
    This needs to be done in ConfigWriter and not by RemoveInstance
916 fc95f88f Iustin Pop
    combined with AddInstance as only we can guarantee an atomic
917 fc95f88f Iustin Pop
    rename.
918 fc95f88f Iustin Pop

919 fc95f88f Iustin Pop
    """
920 fc95f88f Iustin Pop
    if old_name not in self._config_data.instances:
921 fc95f88f Iustin Pop
      raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
922 fc95f88f Iustin Pop
    inst = self._config_data.instances[old_name]
923 fc95f88f Iustin Pop
    del self._config_data.instances[old_name]
924 fc95f88f Iustin Pop
    inst.name = new_name
925 b23c4333 Manuel Franceschini
926 b23c4333 Manuel Franceschini
    for disk in inst.disks:
927 b23c4333 Manuel Franceschini
      if disk.dev_type == constants.LD_FILE:
928 b23c4333 Manuel Franceschini
        # rename the file paths in logical and physical id
929 b23c4333 Manuel Franceschini
        file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
930 b23c4333 Manuel Franceschini
        disk.physical_id = disk.logical_id = (disk.logical_id[0],
931 c4feafe8 Iustin Pop
                                              utils.PathJoin(file_storage_dir,
932 c4feafe8 Iustin Pop
                                                             inst.name,
933 c4feafe8 Iustin Pop
                                                             disk.iv_name))
934 b23c4333 Manuel Franceschini
935 fc95f88f Iustin Pop
    self._config_data.instances[inst.name] = inst
936 fc95f88f Iustin Pop
    self._WriteConfig()
937 fc95f88f Iustin Pop
938 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
939 a8083063 Iustin Pop
  def MarkInstanceDown(self, instance_name):
940 a8083063 Iustin Pop
    """Mark the status of an instance to down in the configuration.
941 a8083063 Iustin Pop

942 a8083063 Iustin Pop
    """
943 0d68c45d Iustin Pop
    self._SetInstanceStatus(instance_name, False)
944 a8083063 Iustin Pop
945 94bbfece Iustin Pop
  def _UnlockedGetInstanceList(self):
946 94bbfece Iustin Pop
    """Get the list of instances.
947 94bbfece Iustin Pop

948 94bbfece Iustin Pop
    This function is for internal use, when the config lock is already held.
949 94bbfece Iustin Pop

950 94bbfece Iustin Pop
    """
951 94bbfece Iustin Pop
    return self._config_data.instances.keys()
952 94bbfece Iustin Pop
953 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
954 a8083063 Iustin Pop
  def GetInstanceList(self):
955 a8083063 Iustin Pop
    """Get the list of instances.
956 a8083063 Iustin Pop

957 c41eea6e Iustin Pop
    @return: array of instances, ex. ['instance2.example.com',
958 c41eea6e Iustin Pop
        'instance1.example.com']
959 a8083063 Iustin Pop

960 a8083063 Iustin Pop
    """
961 94bbfece Iustin Pop
    return self._UnlockedGetInstanceList()
962 a8083063 Iustin Pop
963 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
964 a8083063 Iustin Pop
  def ExpandInstanceName(self, short_name):
965 a8083063 Iustin Pop
    """Attempt to expand an incomplete instance name.
966 a8083063 Iustin Pop

967 a8083063 Iustin Pop
    """
968 a8083063 Iustin Pop
    return utils.MatchNameComponent(short_name,
969 bcdf16d7 Guido Trotter
                                    self._config_data.instances.keys(),
970 bcdf16d7 Guido Trotter
                                    case_sensitive=False)
971 a8083063 Iustin Pop
972 94bbfece Iustin Pop
  def _UnlockedGetInstanceInfo(self, instance_name):
973 5bbd3f7f Michael Hanselmann
    """Returns information about an instance.
974 94bbfece Iustin Pop

975 94bbfece Iustin Pop
    This function is for internal use, when the config lock is already held.
976 94bbfece Iustin Pop

977 94bbfece Iustin Pop
    """
978 94bbfece Iustin Pop
    if instance_name not in self._config_data.instances:
979 94bbfece Iustin Pop
      return None
980 94bbfece Iustin Pop
981 94bbfece Iustin Pop
    return self._config_data.instances[instance_name]
982 94bbfece Iustin Pop
983 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
984 a8083063 Iustin Pop
  def GetInstanceInfo(self, instance_name):
985 5bbd3f7f Michael Hanselmann
    """Returns information about an instance.
986 a8083063 Iustin Pop

987 5bbd3f7f Michael Hanselmann
    It takes the information from the configuration file. Other information of
988 a8083063 Iustin Pop
    an instance are taken from the live systems.
989 a8083063 Iustin Pop

990 c41eea6e Iustin Pop
    @param instance_name: name of the instance, e.g.
991 c41eea6e Iustin Pop
        I{instance1.example.com}
992 a8083063 Iustin Pop

993 c41eea6e Iustin Pop
    @rtype: L{objects.Instance}
994 c41eea6e Iustin Pop
    @return: the instance object
995 a8083063 Iustin Pop

996 a8083063 Iustin Pop
    """
997 94bbfece Iustin Pop
    return self._UnlockedGetInstanceInfo(instance_name)
998 a8083063 Iustin Pop
999 0b2de758 Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
1000 0b2de758 Iustin Pop
  def GetAllInstancesInfo(self):
1001 0b2de758 Iustin Pop
    """Get the configuration of all instances.
1002 0b2de758 Iustin Pop

1003 0b2de758 Iustin Pop
    @rtype: dict
1004 5fcc718f Iustin Pop
    @return: dict of (instance, instance_info), where instance_info is what
1005 0b2de758 Iustin Pop
              would GetInstanceInfo return for the node
1006 0b2de758 Iustin Pop

1007 0b2de758 Iustin Pop
    """
1008 64d3bd52 Guido Trotter
    my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance))
1009 64d3bd52 Guido Trotter
                    for instance in self._UnlockedGetInstanceList()])
1010 0b2de758 Iustin Pop
    return my_dict
1011 0b2de758 Iustin Pop
1012 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
1013 0debfb35 Guido Trotter
  def AddNode(self, node, ec_id):
1014 a8083063 Iustin Pop
    """Add a node to the configuration.
1015 a8083063 Iustin Pop

1016 c41eea6e Iustin Pop
    @type node: L{objects.Node}
1017 c41eea6e Iustin Pop
    @param node: a Node instance
1018 a8083063 Iustin Pop

1019 a8083063 Iustin Pop
    """
1020 099c52ad Iustin Pop
    logging.info("Adding node %s to configuration", node.name)
1021 d8470559 Michael Hanselmann
1022 0debfb35 Guido Trotter
    self._EnsureUUID(node, ec_id)
1023 430b923c Iustin Pop
1024 b989e85d Iustin Pop
    node.serial_no = 1
1025 d693c864 Iustin Pop
    node.ctime = node.mtime = time.time()
1026 a8083063 Iustin Pop
    self._config_data.nodes[node.name] = node
1027 b9f72b4e Iustin Pop
    self._config_data.cluster.serial_no += 1
1028 a8083063 Iustin Pop
    self._WriteConfig()
1029 a8083063 Iustin Pop
1030 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
1031 a8083063 Iustin Pop
  def RemoveNode(self, node_name):
1032 a8083063 Iustin Pop
    """Remove a node from the configuration.
1033 a8083063 Iustin Pop

1034 a8083063 Iustin Pop
    """
1035 099c52ad Iustin Pop
    logging.info("Removing node %s from configuration", node_name)
1036 d8470559 Michael Hanselmann
1037 a8083063 Iustin Pop
    if node_name not in self._config_data.nodes:
1038 3ecf6786 Iustin Pop
      raise errors.ConfigurationError("Unknown node '%s'" % node_name)
1039 a8083063 Iustin Pop
1040 a8083063 Iustin Pop
    del self._config_data.nodes[node_name]
1041 b9f72b4e Iustin Pop
    self._config_data.cluster.serial_no += 1
1042 a8083063 Iustin Pop
    self._WriteConfig()
1043 a8083063 Iustin Pop
1044 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1045 a8083063 Iustin Pop
  def ExpandNodeName(self, short_name):
1046 a8083063 Iustin Pop
    """Attempt to expand an incomplete instance name.
1047 a8083063 Iustin Pop

1048 a8083063 Iustin Pop
    """
1049 a8083063 Iustin Pop
    return utils.MatchNameComponent(short_name,
1050 bcdf16d7 Guido Trotter
                                    self._config_data.nodes.keys(),
1051 bcdf16d7 Guido Trotter
                                    case_sensitive=False)
1052 a8083063 Iustin Pop
1053 f78ede4e Guido Trotter
  def _UnlockedGetNodeInfo(self, node_name):
1054 a8083063 Iustin Pop
    """Get the configuration of a node, as stored in the config.
1055 a8083063 Iustin Pop

1056 c41eea6e Iustin Pop
    This function is for internal use, when the config lock is already
1057 c41eea6e Iustin Pop
    held.
1058 f78ede4e Guido Trotter

1059 c41eea6e Iustin Pop
    @param node_name: the node name, e.g. I{node1.example.com}
1060 a8083063 Iustin Pop

1061 c41eea6e Iustin Pop
    @rtype: L{objects.Node}
1062 c41eea6e Iustin Pop
    @return: the node object
1063 a8083063 Iustin Pop

1064 a8083063 Iustin Pop
    """
1065 a8083063 Iustin Pop
    if node_name not in self._config_data.nodes:
1066 a8083063 Iustin Pop
      return None
1067 a8083063 Iustin Pop
1068 a8083063 Iustin Pop
    return self._config_data.nodes[node_name]
1069 a8083063 Iustin Pop
1070 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1071 f78ede4e Guido Trotter
  def GetNodeInfo(self, node_name):
1072 f78ede4e Guido Trotter
    """Get the configuration of a node, as stored in the config.
1073 f78ede4e Guido Trotter

1074 c41eea6e Iustin Pop
    This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
1075 f78ede4e Guido Trotter

1076 c41eea6e Iustin Pop
    @param node_name: the node name, e.g. I{node1.example.com}
1077 c41eea6e Iustin Pop

1078 c41eea6e Iustin Pop
    @rtype: L{objects.Node}
1079 c41eea6e Iustin Pop
    @return: the node object
1080 f78ede4e Guido Trotter

1081 f78ede4e Guido Trotter
    """
1082 f78ede4e Guido Trotter
    return self._UnlockedGetNodeInfo(node_name)
1083 f78ede4e Guido Trotter
1084 f78ede4e Guido Trotter
  def _UnlockedGetNodeList(self):
1085 a8083063 Iustin Pop
    """Return the list of nodes which are in the configuration.
1086 a8083063 Iustin Pop

1087 c41eea6e Iustin Pop
    This function is for internal use, when the config lock is already
1088 c41eea6e Iustin Pop
    held.
1089 c41eea6e Iustin Pop

1090 c41eea6e Iustin Pop
    @rtype: list
1091 f78ede4e Guido Trotter

1092 a8083063 Iustin Pop
    """
1093 a8083063 Iustin Pop
    return self._config_data.nodes.keys()
1094 a8083063 Iustin Pop
1095 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1096 f78ede4e Guido Trotter
  def GetNodeList(self):
1097 f78ede4e Guido Trotter
    """Return the list of nodes which are in the configuration.
1098 f78ede4e Guido Trotter

1099 f78ede4e Guido Trotter
    """
1100 f78ede4e Guido Trotter
    return self._UnlockedGetNodeList()
1101 f78ede4e Guido Trotter
1102 6819dc49 Iustin Pop
  def _UnlockedGetOnlineNodeList(self):
1103 94a02bb5 Iustin Pop
    """Return the list of nodes which are online.
1104 94a02bb5 Iustin Pop

1105 94a02bb5 Iustin Pop
    """
1106 94a02bb5 Iustin Pop
    all_nodes = [self._UnlockedGetNodeInfo(node)
1107 94a02bb5 Iustin Pop
                 for node in self._UnlockedGetNodeList()]
1108 94a02bb5 Iustin Pop
    return [node.name for node in all_nodes if not node.offline]
1109 94a02bb5 Iustin Pop
1110 94a02bb5 Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
1111 6819dc49 Iustin Pop
  def GetOnlineNodeList(self):
1112 6819dc49 Iustin Pop
    """Return the list of nodes which are online.
1113 6819dc49 Iustin Pop

1114 6819dc49 Iustin Pop
    """
1115 6819dc49 Iustin Pop
    return self._UnlockedGetOnlineNodeList()
1116 6819dc49 Iustin Pop
1117 6819dc49 Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
1118 d65e5776 Iustin Pop
  def GetAllNodesInfo(self):
1119 d65e5776 Iustin Pop
    """Get the configuration of all nodes.
1120 d65e5776 Iustin Pop

1121 d65e5776 Iustin Pop
    @rtype: dict
1122 ec0292f1 Iustin Pop
    @return: dict of (node, node_info), where node_info is what
1123 d65e5776 Iustin Pop
              would GetNodeInfo return for the node
1124 d65e5776 Iustin Pop

1125 d65e5776 Iustin Pop
    """
1126 d65e5776 Iustin Pop
    my_dict = dict([(node, self._UnlockedGetNodeInfo(node))
1127 d65e5776 Iustin Pop
                    for node in self._UnlockedGetNodeList()])
1128 d65e5776 Iustin Pop
    return my_dict
1129 d65e5776 Iustin Pop
1130 23f06b2b Iustin Pop
  def _UnlockedGetMasterCandidateStats(self, exceptions=None):
1131 ec0292f1 Iustin Pop
    """Get the number of current and maximum desired and possible candidates.
1132 ec0292f1 Iustin Pop

1133 23f06b2b Iustin Pop
    @type exceptions: list
1134 23f06b2b Iustin Pop
    @param exceptions: if passed, list of nodes that should be ignored
1135 ec0292f1 Iustin Pop
    @rtype: tuple
1136 e623dbe3 Guido Trotter
    @return: tuple of (current, desired and possible, possible)
1137 ec0292f1 Iustin Pop

1138 ec0292f1 Iustin Pop
    """
1139 e623dbe3 Guido Trotter
    mc_now = mc_should = mc_max = 0
1140 23f06b2b Iustin Pop
    for node in self._config_data.nodes.values():
1141 23f06b2b Iustin Pop
      if exceptions and node.name in exceptions:
1142 23f06b2b Iustin Pop
        continue
1143 5bf07049 Iustin Pop
      if not (node.offline or node.drained):
1144 ec0292f1 Iustin Pop
        mc_max += 1
1145 ec0292f1 Iustin Pop
      if node.master_candidate:
1146 ec0292f1 Iustin Pop
        mc_now += 1
1147 e623dbe3 Guido Trotter
    mc_should = min(mc_max, self._config_data.cluster.candidate_pool_size)
1148 e623dbe3 Guido Trotter
    return (mc_now, mc_should, mc_max)
1149 ec0292f1 Iustin Pop
1150 ec0292f1 Iustin Pop
  @locking.ssynchronized(_config_lock, shared=1)
1151 23f06b2b Iustin Pop
  def GetMasterCandidateStats(self, exceptions=None):
1152 ec0292f1 Iustin Pop
    """Get the number of current and maximum possible candidates.
1153 ec0292f1 Iustin Pop

1154 ec0292f1 Iustin Pop
    This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
1155 ec0292f1 Iustin Pop

1156 23f06b2b Iustin Pop
    @type exceptions: list
1157 23f06b2b Iustin Pop
    @param exceptions: if passed, list of nodes that should be ignored
1158 ec0292f1 Iustin Pop
    @rtype: tuple
1159 ec0292f1 Iustin Pop
    @return: tuple of (current, max)
1160 ec0292f1 Iustin Pop

1161 ec0292f1 Iustin Pop
    """
1162 23f06b2b Iustin Pop
    return self._UnlockedGetMasterCandidateStats(exceptions)
1163 ec0292f1 Iustin Pop
1164 ec0292f1 Iustin Pop
  @locking.ssynchronized(_config_lock)
1165 44485f49 Guido Trotter
  def MaintainCandidatePool(self, exceptions):
1166 ec0292f1 Iustin Pop
    """Try to grow the candidate pool to the desired size.
1167 ec0292f1 Iustin Pop

1168 44485f49 Guido Trotter
    @type exceptions: list
1169 44485f49 Guido Trotter
    @param exceptions: if passed, list of nodes that should be ignored
1170 ec0292f1 Iustin Pop
    @rtype: list
1171 ee513a66 Iustin Pop
    @return: list with the adjusted nodes (L{objects.Node} instances)
1172 ec0292f1 Iustin Pop

1173 ec0292f1 Iustin Pop
    """
1174 44485f49 Guido Trotter
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats(exceptions)
1175 ec0292f1 Iustin Pop
    mod_list = []
1176 ec0292f1 Iustin Pop
    if mc_now < mc_max:
1177 ec0292f1 Iustin Pop
      node_list = self._config_data.nodes.keys()
1178 ec0292f1 Iustin Pop
      random.shuffle(node_list)
1179 ec0292f1 Iustin Pop
      for name in node_list:
1180 ec0292f1 Iustin Pop
        if mc_now >= mc_max:
1181 ec0292f1 Iustin Pop
          break
1182 ec0292f1 Iustin Pop
        node = self._config_data.nodes[name]
1183 44485f49 Guido Trotter
        if (node.master_candidate or node.offline or node.drained or
1184 44485f49 Guido Trotter
            node.name in exceptions):
1185 ec0292f1 Iustin Pop
          continue
1186 ee513a66 Iustin Pop
        mod_list.append(node)
1187 ec0292f1 Iustin Pop
        node.master_candidate = True
1188 ec0292f1 Iustin Pop
        node.serial_no += 1
1189 ec0292f1 Iustin Pop
        mc_now += 1
1190 ec0292f1 Iustin Pop
      if mc_now != mc_max:
1191 ec0292f1 Iustin Pop
        # this should not happen
1192 ec0292f1 Iustin Pop
        logging.warning("Warning: MaintainCandidatePool didn't manage to"
1193 ec0292f1 Iustin Pop
                        " fill the candidate pool (%d/%d)", mc_now, mc_max)
1194 ec0292f1 Iustin Pop
      if mod_list:
1195 ec0292f1 Iustin Pop
        self._config_data.cluster.serial_no += 1
1196 ec0292f1 Iustin Pop
        self._WriteConfig()
1197 ec0292f1 Iustin Pop
1198 ec0292f1 Iustin Pop
    return mod_list
1199 ec0292f1 Iustin Pop
1200 a8083063 Iustin Pop
  def _BumpSerialNo(self):
1201 a8083063 Iustin Pop
    """Bump up the serial number of the config.
1202 a8083063 Iustin Pop

1203 a8083063 Iustin Pop
    """
1204 9d38c6e1 Iustin Pop
    self._config_data.serial_no += 1
1205 d693c864 Iustin Pop
    self._config_data.mtime = time.time()
1206 a8083063 Iustin Pop
1207 76d5d3a3 Iustin Pop
  def _AllUUIDObjects(self):
1208 76d5d3a3 Iustin Pop
    """Returns all objects with uuid attributes.
1209 76d5d3a3 Iustin Pop

1210 76d5d3a3 Iustin Pop
    """
1211 76d5d3a3 Iustin Pop
    return (self._config_data.instances.values() +
1212 76d5d3a3 Iustin Pop
            self._config_data.nodes.values() +
1213 76d5d3a3 Iustin Pop
            [self._config_data.cluster])
1214 76d5d3a3 Iustin Pop
1215 a8083063 Iustin Pop
  def _OpenConfig(self):
1216 a8083063 Iustin Pop
    """Read the config data from disk.
1217 a8083063 Iustin Pop

1218 a8083063 Iustin Pop
    """
1219 13998ef2 Michael Hanselmann
    raw_data = utils.ReadFile(self._cfg_file)
1220 13998ef2 Michael Hanselmann
1221 a8083063 Iustin Pop
    try:
1222 13998ef2 Michael Hanselmann
      data = objects.ConfigData.FromDict(serializer.Load(raw_data))
1223 13998ef2 Michael Hanselmann
    except Exception, err:
1224 13998ef2 Michael Hanselmann
      raise errors.ConfigurationError(err)
1225 5b263ed7 Michael Hanselmann
1226 5b263ed7 Michael Hanselmann
    # Make sure the configuration has the right version
1227 5b263ed7 Michael Hanselmann
    _ValidateConfig(data)
1228 5b263ed7 Michael Hanselmann
1229 a8083063 Iustin Pop
    if (not hasattr(data, 'cluster') or
1230 243cdbcc Michael Hanselmann
        not hasattr(data.cluster, 'rsahostkeypub')):
1231 3ecf6786 Iustin Pop
      raise errors.ConfigurationError("Incomplete configuration"
1232 243cdbcc Michael Hanselmann
                                      " (missing cluster.rsahostkeypub)")
1233 90d726a8 Iustin Pop
1234 90d726a8 Iustin Pop
    # Upgrade configuration if needed
1235 90d726a8 Iustin Pop
    data.UpgradeConfig()
1236 90d726a8 Iustin Pop
1237 a8083063 Iustin Pop
    self._config_data = data
1238 3c7f6c44 Iustin Pop
    # reset the last serial as -1 so that the next write will cause
1239 0779e3aa Iustin Pop
    # ssconf update
1240 0779e3aa Iustin Pop
    self._last_cluster_serial = -1
1241 a8083063 Iustin Pop
1242 76d5d3a3 Iustin Pop
    # And finally run our (custom) config upgrade sequence
1243 76d5d3a3 Iustin Pop
    self._UpgradeConfig()
1244 76d5d3a3 Iustin Pop
1245 76d5d3a3 Iustin Pop
  def _UpgradeConfig(self):
1246 76d5d3a3 Iustin Pop
    """Run upgrade steps that cannot be done purely in the objects.
1247 76d5d3a3 Iustin Pop

1248 76d5d3a3 Iustin Pop
    This is because some data elements need uniqueness across the
1249 76d5d3a3 Iustin Pop
    whole configuration, etc.
1250 76d5d3a3 Iustin Pop

1251 111c4e2f Guido Trotter
    @warning: this function will call L{_WriteConfig()}, but also
1252 111c4e2f Guido Trotter
        L{DropECReservations} so it needs to be called only from a
1253 111c4e2f Guido Trotter
        "safe" place (the constructor). If one wanted to call it with
1254 111c4e2f Guido Trotter
        the lock held, a DropECReservationUnlocked would need to be
1255 111c4e2f Guido Trotter
        created first, to avoid causing deadlock.
1256 76d5d3a3 Iustin Pop

1257 76d5d3a3 Iustin Pop
    """
1258 76d5d3a3 Iustin Pop
    modified = False
1259 76d5d3a3 Iustin Pop
    for item in self._AllUUIDObjects():
1260 76d5d3a3 Iustin Pop
      if item.uuid is None:
1261 4fae38c5 Guido Trotter
        item.uuid = self._GenerateUniqueID(_UPGRADE_CONFIG_JID)
1262 76d5d3a3 Iustin Pop
        modified = True
1263 76d5d3a3 Iustin Pop
    if modified:
1264 76d5d3a3 Iustin Pop
      self._WriteConfig()
1265 4fae38c5 Guido Trotter
      # This is ok even if it acquires the internal lock, as _UpgradeConfig is
1266 4fae38c5 Guido Trotter
      # only called at config init time, without the lock held
1267 4fae38c5 Guido Trotter
      self.DropECReservations(_UPGRADE_CONFIG_JID)
1268 4fae38c5 Guido Trotter
1269 a4eae71f Michael Hanselmann
  def _DistributeConfig(self, feedback_fn):
1270 a8083063 Iustin Pop
    """Distribute the configuration to the other nodes.
1271 a8083063 Iustin Pop

1272 a8083063 Iustin Pop
    Currently, this only copies the configuration file. In the future,
1273 a8083063 Iustin Pop
    it could be used to encapsulate the 2/3-phase update mechanism.
1274 a8083063 Iustin Pop

1275 a8083063 Iustin Pop
    """
1276 a8083063 Iustin Pop
    if self._offline:
1277 a8083063 Iustin Pop
      return True
1278 a4eae71f Michael Hanselmann
1279 a8083063 Iustin Pop
    bad = False
1280 a8083063 Iustin Pop
1281 6a5b8b4b Iustin Pop
    node_list = []
1282 6a5b8b4b Iustin Pop
    addr_list = []
1283 6a5b8b4b Iustin Pop
    myhostname = self._my_hostname
1284 6b294c53 Iustin Pop
    # we can skip checking whether _UnlockedGetNodeInfo returns None
1285 6b294c53 Iustin Pop
    # since the node list comes from _UnlocketGetNodeList, and we are
1286 6b294c53 Iustin Pop
    # called with the lock held, so no modifications should take place
1287 6b294c53 Iustin Pop
    # in between
1288 6a5b8b4b Iustin Pop
    for node_name in self._UnlockedGetNodeList():
1289 6a5b8b4b Iustin Pop
      if node_name == myhostname:
1290 6a5b8b4b Iustin Pop
        continue
1291 6a5b8b4b Iustin Pop
      node_info = self._UnlockedGetNodeInfo(node_name)
1292 6a5b8b4b Iustin Pop
      if not node_info.master_candidate:
1293 6a5b8b4b Iustin Pop
        continue
1294 6a5b8b4b Iustin Pop
      node_list.append(node_info.name)
1295 6a5b8b4b Iustin Pop
      addr_list.append(node_info.primary_ip)
1296 6b294c53 Iustin Pop
1297 6a5b8b4b Iustin Pop
    result = rpc.RpcRunner.call_upload_file(node_list, self._cfg_file,
1298 6a5b8b4b Iustin Pop
                                            address_list=addr_list)
1299 1b54fc6c Guido Trotter
    for to_node, to_result in result.items():
1300 3cebe102 Michael Hanselmann
      msg = to_result.fail_msg
1301 1b54fc6c Guido Trotter
      if msg:
1302 1b54fc6c Guido Trotter
        msg = ("Copy of file %s to node %s failed: %s" %
1303 dd7db360 Iustin Pop
               (self._cfg_file, to_node, msg))
1304 1b54fc6c Guido Trotter
        logging.error(msg)
1305 a4eae71f Michael Hanselmann
1306 a4eae71f Michael Hanselmann
        if feedback_fn:
1307 a4eae71f Michael Hanselmann
          feedback_fn(msg)
1308 a4eae71f Michael Hanselmann
1309 a8083063 Iustin Pop
        bad = True
1310 a4eae71f Michael Hanselmann
1311 a8083063 Iustin Pop
    return not bad
1312 a8083063 Iustin Pop
1313 a4eae71f Michael Hanselmann
  def _WriteConfig(self, destination=None, feedback_fn=None):
1314 a8083063 Iustin Pop
    """Write the configuration data to persistent storage.
1315 a8083063 Iustin Pop

1316 a8083063 Iustin Pop
    """
1317 a4eae71f Michael Hanselmann
    assert feedback_fn is None or callable(feedback_fn)
1318 a4eae71f Michael Hanselmann
1319 d2231b8c Iustin Pop
    # Warn on config errors, but don't abort the save - the
1320 d2231b8c Iustin Pop
    # configuration has already been modified, and we can't revert;
1321 d2231b8c Iustin Pop
    # the best we can do is to warn the user and save as is, leaving
1322 d2231b8c Iustin Pop
    # recovery to the user
1323 4a89c54a Iustin Pop
    config_errors = self._UnlockedVerifyConfig()
1324 4a89c54a Iustin Pop
    if config_errors:
1325 d2231b8c Iustin Pop
      errmsg = ("Configuration data is not consistent: %s" %
1326 1f864b60 Iustin Pop
                (utils.CommaJoin(config_errors)))
1327 d2231b8c Iustin Pop
      logging.critical(errmsg)
1328 d2231b8c Iustin Pop
      if feedback_fn:
1329 d2231b8c Iustin Pop
        feedback_fn(errmsg)
1330 d2231b8c Iustin Pop
1331 a8083063 Iustin Pop
    if destination is None:
1332 a8083063 Iustin Pop
      destination = self._cfg_file
1333 a8083063 Iustin Pop
    self._BumpSerialNo()
1334 8d14b30d Iustin Pop
    txt = serializer.Dump(self._config_data.ToDict())
1335 13998ef2 Michael Hanselmann
1336 13998ef2 Michael Hanselmann
    utils.WriteFile(destination, data=txt)
1337 13998ef2 Michael Hanselmann
1338 14e15659 Iustin Pop
    self.write_count += 1
1339 3d3a04bc Iustin Pop
1340 f56618e0 Iustin Pop
    # and redistribute the config file to master candidates
1341 a4eae71f Michael Hanselmann
    self._DistributeConfig(feedback_fn)
1342 a8083063 Iustin Pop
1343 54d1a06e Michael Hanselmann
    # Write ssconf files on all nodes (including locally)
1344 0779e3aa Iustin Pop
    if self._last_cluster_serial < self._config_data.cluster.serial_no:
1345 d9a855f1 Michael Hanselmann
      if not self._offline:
1346 cd34faf2 Michael Hanselmann
        result = rpc.RpcRunner.call_write_ssconf_files(
1347 6819dc49 Iustin Pop
          self._UnlockedGetOnlineNodeList(),
1348 e1e75d00 Iustin Pop
          self._UnlockedGetSsconfValues())
1349 a4eae71f Michael Hanselmann
1350 e1e75d00 Iustin Pop
        for nname, nresu in result.items():
1351 3cebe102 Michael Hanselmann
          msg = nresu.fail_msg
1352 e1e75d00 Iustin Pop
          if msg:
1353 a4eae71f Michael Hanselmann
            errmsg = ("Error while uploading ssconf files to"
1354 a4eae71f Michael Hanselmann
                      " node %s: %s" % (nname, msg))
1355 a4eae71f Michael Hanselmann
            logging.warning(errmsg)
1356 a4eae71f Michael Hanselmann
1357 a4eae71f Michael Hanselmann
            if feedback_fn:
1358 a4eae71f Michael Hanselmann
              feedback_fn(errmsg)
1359 a4eae71f Michael Hanselmann
1360 0779e3aa Iustin Pop
      self._last_cluster_serial = self._config_data.cluster.serial_no
1361 54d1a06e Michael Hanselmann
1362 03d1dba2 Michael Hanselmann
  def _UnlockedGetSsconfValues(self):
1363 054596f0 Iustin Pop
    """Return the values needed by ssconf.
1364 054596f0 Iustin Pop

1365 054596f0 Iustin Pop
    @rtype: dict
1366 054596f0 Iustin Pop
    @return: a dictionary with keys the ssconf names and values their
1367 054596f0 Iustin Pop
        associated value
1368 054596f0 Iustin Pop

1369 054596f0 Iustin Pop
    """
1370 a3316e4a Iustin Pop
    fn = "\n".join
1371 81a49123 Iustin Pop
    instance_names = utils.NiceSort(self._UnlockedGetInstanceList())
1372 a3316e4a Iustin Pop
    node_names = utils.NiceSort(self._UnlockedGetNodeList())
1373 a3316e4a Iustin Pop
    node_info = [self._UnlockedGetNodeInfo(name) for name in node_names]
1374 c3029d0a Luca Bigliardi
    node_pri_ips = ["%s %s" % (ninfo.name, ninfo.primary_ip)
1375 5909fb97 Luca Bigliardi
                    for ninfo in node_info]
1376 c3029d0a Luca Bigliardi
    node_snd_ips = ["%s %s" % (ninfo.name, ninfo.secondary_ip)
1377 5909fb97 Luca Bigliardi
                    for ninfo in node_info]
1378 a3316e4a Iustin Pop
1379 81a49123 Iustin Pop
    instance_data = fn(instance_names)
1380 a3316e4a Iustin Pop
    off_data = fn(node.name for node in node_info if node.offline)
1381 81a49123 Iustin Pop
    on_data = fn(node.name for node in node_info if not node.offline)
1382 a3316e4a Iustin Pop
    mc_data = fn(node.name for node in node_info if node.master_candidate)
1383 8113a52e Luca Bigliardi
    mc_ips_data = fn(node.primary_ip for node in node_info
1384 8113a52e Luca Bigliardi
                     if node.master_candidate)
1385 a3316e4a Iustin Pop
    node_data = fn(node_names)
1386 f9780ccd Luca Bigliardi
    node_pri_ips_data = fn(node_pri_ips)
1387 f9780ccd Luca Bigliardi
    node_snd_ips_data = fn(node_snd_ips)
1388 f56618e0 Iustin Pop
1389 054596f0 Iustin Pop
    cluster = self._config_data.cluster
1390 5d60b3bd Iustin Pop
    cluster_tags = fn(cluster.GetTags())
1391 4f7a6a10 Iustin Pop
1392 4f7a6a10 Iustin Pop
    hypervisor_list = fn(cluster.enabled_hypervisors)
1393 4f7a6a10 Iustin Pop
1394 0fbae49a Balazs Lecz
    uid_pool = uidpool.FormatUidPool(cluster.uid_pool, separator="\n")
1395 0fbae49a Balazs Lecz
1396 03d1dba2 Michael Hanselmann
    return {
1397 054596f0 Iustin Pop
      constants.SS_CLUSTER_NAME: cluster.cluster_name,
1398 5d60b3bd Iustin Pop
      constants.SS_CLUSTER_TAGS: cluster_tags,
1399 054596f0 Iustin Pop
      constants.SS_FILE_STORAGE_DIR: cluster.file_storage_dir,
1400 a3316e4a Iustin Pop
      constants.SS_MASTER_CANDIDATES: mc_data,
1401 8113a52e Luca Bigliardi
      constants.SS_MASTER_CANDIDATES_IPS: mc_ips_data,
1402 054596f0 Iustin Pop
      constants.SS_MASTER_IP: cluster.master_ip,
1403 054596f0 Iustin Pop
      constants.SS_MASTER_NETDEV: cluster.master_netdev,
1404 054596f0 Iustin Pop
      constants.SS_MASTER_NODE: cluster.master_node,
1405 a3316e4a Iustin Pop
      constants.SS_NODE_LIST: node_data,
1406 f9780ccd Luca Bigliardi
      constants.SS_NODE_PRIMARY_IPS: node_pri_ips_data,
1407 f9780ccd Luca Bigliardi
      constants.SS_NODE_SECONDARY_IPS: node_snd_ips_data,
1408 a3316e4a Iustin Pop
      constants.SS_OFFLINE_NODES: off_data,
1409 81a49123 Iustin Pop
      constants.SS_ONLINE_NODES: on_data,
1410 81a49123 Iustin Pop
      constants.SS_INSTANCE_LIST: instance_data,
1411 8a113c7a Iustin Pop
      constants.SS_RELEASE_VERSION: constants.RELEASE_VERSION,
1412 4f7a6a10 Iustin Pop
      constants.SS_HYPERVISOR_LIST: hypervisor_list,
1413 5c465a95 Iustin Pop
      constants.SS_MAINTAIN_NODE_HEALTH: str(cluster.maintain_node_health),
1414 0fbae49a Balazs Lecz
      constants.SS_UID_POOL: uid_pool,
1415 03d1dba2 Michael Hanselmann
      }
1416 03d1dba2 Michael Hanselmann
1417 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1418 d367b66c Manuel Franceschini
  def GetSsconfValues(self):
1419 d367b66c Manuel Franceschini
    """Wrapper using lock around _UnlockedGetSsconf().
1420 d367b66c Manuel Franceschini

1421 d367b66c Manuel Franceschini
    """
1422 d367b66c Manuel Franceschini
    return self._UnlockedGetSsconfValues()
1423 d367b66c Manuel Franceschini
1424 d367b66c Manuel Franceschini
  @locking.ssynchronized(_config_lock, shared=1)
1425 a8083063 Iustin Pop
  def GetVGName(self):
1426 a8083063 Iustin Pop
    """Return the volume group name.
1427 a8083063 Iustin Pop

1428 a8083063 Iustin Pop
    """
1429 a8083063 Iustin Pop
    return self._config_data.cluster.volume_group_name
1430 a8083063 Iustin Pop
1431 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
1432 89ff8e15 Manuel Franceschini
  def SetVGName(self, vg_name):
1433 89ff8e15 Manuel Franceschini
    """Set the volume group name.
1434 89ff8e15 Manuel Franceschini

1435 89ff8e15 Manuel Franceschini
    """
1436 2d4011cd Manuel Franceschini
    self._config_data.cluster.volume_group_name = vg_name
1437 b9f72b4e Iustin Pop
    self._config_data.cluster.serial_no += 1
1438 89ff8e15 Manuel Franceschini
    self._WriteConfig()
1439 89ff8e15 Manuel Franceschini
1440 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1441 9e33896b Luca Bigliardi
  def GetDRBDHelper(self):
1442 9e33896b Luca Bigliardi
    """Return DRBD usermode helper.
1443 9e33896b Luca Bigliardi

1444 9e33896b Luca Bigliardi
    """
1445 9e33896b Luca Bigliardi
    return self._config_data.cluster.drbd_usermode_helper
1446 9e33896b Luca Bigliardi
1447 9e33896b Luca Bigliardi
  @locking.ssynchronized(_config_lock)
1448 9e33896b Luca Bigliardi
  def SetDRBDHelper(self, drbd_helper):
1449 9e33896b Luca Bigliardi
    """Set DRBD usermode helper.
1450 9e33896b Luca Bigliardi

1451 9e33896b Luca Bigliardi
    """
1452 9e33896b Luca Bigliardi
    self._config_data.cluster.drbd_usermode_helper = drbd_helper
1453 9e33896b Luca Bigliardi
    self._config_data.cluster.serial_no += 1
1454 9e33896b Luca Bigliardi
    self._WriteConfig()
1455 9e33896b Luca Bigliardi
1456 9e33896b Luca Bigliardi
  @locking.ssynchronized(_config_lock, shared=1)
1457 a8083063 Iustin Pop
  def GetMACPrefix(self):
1458 a8083063 Iustin Pop
    """Return the mac prefix.
1459 a8083063 Iustin Pop

1460 a8083063 Iustin Pop
    """
1461 a8083063 Iustin Pop
    return self._config_data.cluster.mac_prefix
1462 62779dd0 Iustin Pop
1463 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock, shared=1)
1464 62779dd0 Iustin Pop
  def GetClusterInfo(self):
1465 5bbd3f7f Michael Hanselmann
    """Returns information about the cluster
1466 62779dd0 Iustin Pop

1467 c41eea6e Iustin Pop
    @rtype: L{objects.Cluster}
1468 c41eea6e Iustin Pop
    @return: the cluster object
1469 62779dd0 Iustin Pop

1470 62779dd0 Iustin Pop
    """
1471 62779dd0 Iustin Pop
    return self._config_data.cluster
1472 e00fb268 Iustin Pop
1473 51cb1581 Luca Bigliardi
  @locking.ssynchronized(_config_lock, shared=1)
1474 51cb1581 Luca Bigliardi
  def HasAnyDiskOfType(self, dev_type):
1475 51cb1581 Luca Bigliardi
    """Check if in there is at disk of the given type in the configuration.
1476 51cb1581 Luca Bigliardi

1477 51cb1581 Luca Bigliardi
    """
1478 51cb1581 Luca Bigliardi
    return self._config_data.HasAnyDiskOfType(dev_type)
1479 51cb1581 Luca Bigliardi
1480 f78ede4e Guido Trotter
  @locking.ssynchronized(_config_lock)
1481 a4eae71f Michael Hanselmann
  def Update(self, target, feedback_fn):
1482 e00fb268 Iustin Pop
    """Notify function to be called after updates.
1483 e00fb268 Iustin Pop

1484 e00fb268 Iustin Pop
    This function must be called when an object (as returned by
1485 e00fb268 Iustin Pop
    GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
1486 e00fb268 Iustin Pop
    caller wants the modifications saved to the backing store. Note
1487 e00fb268 Iustin Pop
    that all modified objects will be saved, but the target argument
1488 e00fb268 Iustin Pop
    is the one the caller wants to ensure that it's saved.
1489 e00fb268 Iustin Pop

1490 c41eea6e Iustin Pop
    @param target: an instance of either L{objects.Cluster},
1491 c41eea6e Iustin Pop
        L{objects.Node} or L{objects.Instance} which is existing in
1492 c41eea6e Iustin Pop
        the cluster
1493 a4eae71f Michael Hanselmann
    @param feedback_fn: Callable feedback function
1494 c41eea6e Iustin Pop

1495 e00fb268 Iustin Pop
    """
1496 e00fb268 Iustin Pop
    if self._config_data is None:
1497 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Configuration file not read,"
1498 3ecf6786 Iustin Pop
                                   " cannot save.")
1499 f34901f8 Iustin Pop
    update_serial = False
1500 e00fb268 Iustin Pop
    if isinstance(target, objects.Cluster):
1501 e00fb268 Iustin Pop
      test = target == self._config_data.cluster
1502 e00fb268 Iustin Pop
    elif isinstance(target, objects.Node):
1503 e00fb268 Iustin Pop
      test = target in self._config_data.nodes.values()
1504 f34901f8 Iustin Pop
      update_serial = True
1505 e00fb268 Iustin Pop
    elif isinstance(target, objects.Instance):
1506 e00fb268 Iustin Pop
      test = target in self._config_data.instances.values()
1507 e00fb268 Iustin Pop
    else:
1508 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Invalid object type (%s) passed to"
1509 3ecf6786 Iustin Pop
                                   " ConfigWriter.Update" % type(target))
1510 e00fb268 Iustin Pop
    if not test:
1511 3ecf6786 Iustin Pop
      raise errors.ConfigurationError("Configuration updated since object"
1512 3ecf6786 Iustin Pop
                                      " has been read or unknown object")
1513 f34901f8 Iustin Pop
    target.serial_no += 1
1514 d693c864 Iustin Pop
    target.mtime = now = time.time()
1515 f34901f8 Iustin Pop
1516 cff4c037 Iustin Pop
    if update_serial:
1517 f34901f8 Iustin Pop
      # for node updates, we need to increase the cluster serial too
1518 f34901f8 Iustin Pop
      self._config_data.cluster.serial_no += 1
1519 d693c864 Iustin Pop
      self._config_data.cluster.mtime = now
1520 b989e85d Iustin Pop
1521 61cf6b5e Iustin Pop
    if isinstance(target, objects.Instance):
1522 61cf6b5e Iustin Pop
      self._UnlockedReleaseDRBDMinors(target.name)
1523 61cf6b5e Iustin Pop
1524 a4eae71f Michael Hanselmann
    self._WriteConfig(feedback_fn=feedback_fn)
1525 73064714 Guido Trotter
1526 73064714 Guido Trotter
  @locking.ssynchronized(_config_lock)
1527 73064714 Guido Trotter
  def DropECReservations(self, ec_id):
1528 73064714 Guido Trotter
    """Drop per-execution-context reservations
1529 73064714 Guido Trotter

1530 73064714 Guido Trotter
    """
1531 d8aee57e Iustin Pop
    for rm in self._all_rms:
1532 d8aee57e Iustin Pop
      rm.DropECReservations(ec_id)