root / lib / config.py @ 0ce8f948
History | View | Annotate | Download (44.7 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Configuration management for Ganeti
|
23 |
|
24 |
This module provides the interface to the Ganeti cluster configuration.
|
25 |
|
26 |
The configuration data is stored on every node but is updated on the master
|
27 |
only. After each update, the master distributes the data to the other nodes.
|
28 |
|
29 |
Currently, the data storage format is JSON. YAML was slow and consuming too
|
30 |
much memory.
|
31 |
|
32 |
"""
|
33 |
|
34 |
import os |
35 |
import random |
36 |
import logging |
37 |
import time |
38 |
|
39 |
from ganeti import errors |
40 |
from ganeti import locking |
41 |
from ganeti import utils |
42 |
from ganeti import constants |
43 |
from ganeti import rpc |
44 |
from ganeti import objects |
45 |
from ganeti import serializer |
46 |
|
47 |
|
48 |
_config_lock = locking.SharedLock() |
49 |
|
50 |
|
51 |
def _ValidateConfig(data): |
52 |
"""Verifies that a configuration objects looks valid.
|
53 |
|
54 |
This only verifies the version of the configuration.
|
55 |
|
56 |
@raise errors.ConfigurationError: if the version differs from what
|
57 |
we expect
|
58 |
|
59 |
"""
|
60 |
if data.version != constants.CONFIG_VERSION:
|
61 |
raise errors.ConfigurationError("Cluster configuration version" |
62 |
" mismatch, got %s instead of %s" %
|
63 |
(data.version, |
64 |
constants.CONFIG_VERSION)) |
65 |
|
66 |
|
67 |
class ConfigWriter: |
68 |
"""The interface to the cluster configuration.
|
69 |
|
70 |
"""
|
71 |
def __init__(self, cfg_file=None, offline=False): |
72 |
self.write_count = 0 |
73 |
self._lock = _config_lock
|
74 |
self._config_data = None |
75 |
self._offline = offline
|
76 |
if cfg_file is None: |
77 |
self._cfg_file = constants.CLUSTER_CONF_FILE
|
78 |
else:
|
79 |
self._cfg_file = cfg_file
|
80 |
self._temporary_ids = set() |
81 |
self._temporary_drbds = {}
|
82 |
self._temporary_macs = set() |
83 |
# Note: in order to prevent errors when resolving our name in
|
84 |
# _DistributeConfig, we compute it here once and reuse it; it's
|
85 |
# better to raise an error before starting to modify the config
|
86 |
# file than after it was modified
|
87 |
self._my_hostname = utils.HostInfo().name
|
88 |
self._last_cluster_serial = -1 |
89 |
self._OpenConfig()
|
90 |
|
91 |
# this method needs to be static, so that we can call it on the class
|
92 |
@staticmethod
|
93 |
def IsCluster(): |
94 |
"""Check if the cluster is configured.
|
95 |
|
96 |
"""
|
97 |
return os.path.exists(constants.CLUSTER_CONF_FILE)
|
98 |
|
99 |
@locking.ssynchronized(_config_lock, shared=1) |
100 |
def GenerateMAC(self): |
101 |
"""Generate a MAC for an instance.
|
102 |
|
103 |
This should check the current instances for duplicates.
|
104 |
|
105 |
"""
|
106 |
prefix = self._config_data.cluster.mac_prefix
|
107 |
all_macs = self._AllMACs()
|
108 |
retries = 64
|
109 |
while retries > 0: |
110 |
byte1 = random.randrange(0, 256) |
111 |
byte2 = random.randrange(0, 256) |
112 |
byte3 = random.randrange(0, 256) |
113 |
mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
|
114 |
if mac not in all_macs and mac not in self._temporary_macs: |
115 |
break
|
116 |
retries -= 1
|
117 |
else:
|
118 |
raise errors.ConfigurationError("Can't generate unique MAC") |
119 |
self._temporary_macs.add(mac)
|
120 |
return mac
|
121 |
|
122 |
@locking.ssynchronized(_config_lock, shared=1) |
123 |
def IsMacInUse(self, mac): |
124 |
"""Predicate: check if the specified MAC is in use in the Ganeti cluster.
|
125 |
|
126 |
This only checks instances managed by this cluster, it does not
|
127 |
check for potential collisions elsewhere.
|
128 |
|
129 |
"""
|
130 |
all_macs = self._AllMACs()
|
131 |
return mac in all_macs or mac in self._temporary_macs |
132 |
|
133 |
@locking.ssynchronized(_config_lock, shared=1) |
134 |
def GenerateDRBDSecret(self): |
135 |
"""Generate a DRBD secret.
|
136 |
|
137 |
This checks the current disks for duplicates.
|
138 |
|
139 |
"""
|
140 |
all_secrets = self._AllDRBDSecrets()
|
141 |
retries = 64
|
142 |
while retries > 0: |
143 |
secret = utils.GenerateSecret() |
144 |
if secret not in all_secrets: |
145 |
break
|
146 |
retries -= 1
|
147 |
else:
|
148 |
raise errors.ConfigurationError("Can't generate unique DRBD secret") |
149 |
return secret
|
150 |
|
151 |
def _AllLVs(self): |
152 |
"""Compute the list of all LVs.
|
153 |
|
154 |
"""
|
155 |
lvnames = set()
|
156 |
for instance in self._config_data.instances.values(): |
157 |
node_data = instance.MapLVsByNode() |
158 |
for lv_list in node_data.values(): |
159 |
lvnames.update(lv_list) |
160 |
return lvnames
|
161 |
|
162 |
def _AllIDs(self, include_temporary): |
163 |
"""Compute the list of all UUIDs and names we have.
|
164 |
|
165 |
@type include_temporary: boolean
|
166 |
@param include_temporary: whether to include the _temporary_ids set
|
167 |
@rtype: set
|
168 |
@return: a set of IDs
|
169 |
|
170 |
"""
|
171 |
existing = set()
|
172 |
if include_temporary:
|
173 |
existing.update(self._temporary_ids)
|
174 |
existing.update(self._AllLVs())
|
175 |
existing.update(self._config_data.instances.keys())
|
176 |
existing.update(self._config_data.nodes.keys())
|
177 |
existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid]) |
178 |
return existing
|
179 |
|
180 |
def _GenerateUniqueID(self, exceptions=None): |
181 |
"""Generate an unique UUID.
|
182 |
|
183 |
This checks the current node, instances and disk names for
|
184 |
duplicates.
|
185 |
|
186 |
@param exceptions: a list with some other names which should be
|
187 |
checked for uniqueness (used for example when you want to get
|
188 |
more than one id at one time without adding each one in turn
|
189 |
to the config file)
|
190 |
|
191 |
@rtype: string
|
192 |
@return: the unique id
|
193 |
|
194 |
"""
|
195 |
existing = self._AllIDs(include_temporary=True) |
196 |
if exceptions is not None: |
197 |
existing.update(exceptions) |
198 |
retries = 64
|
199 |
while retries > 0: |
200 |
unique_id = utils.NewUUID() |
201 |
if unique_id not in existing and unique_id is not None: |
202 |
break
|
203 |
else:
|
204 |
raise errors.ConfigurationError("Not able generate an unique ID" |
205 |
" (last tried ID: %s" % unique_id)
|
206 |
self._temporary_ids.add(unique_id)
|
207 |
return unique_id
|
208 |
|
209 |
@locking.ssynchronized(_config_lock, shared=1) |
210 |
def GenerateUniqueID(self, exceptions=None): |
211 |
"""Generate an unique ID.
|
212 |
|
213 |
This is just a wrapper over the unlocked version.
|
214 |
|
215 |
"""
|
216 |
return self._GenerateUniqueID(exceptions=exceptions) |
217 |
|
218 |
def _CleanupTemporaryIDs(self): |
219 |
"""Cleanups the _temporary_ids structure.
|
220 |
|
221 |
"""
|
222 |
existing = self._AllIDs(include_temporary=False) |
223 |
self._temporary_ids = self._temporary_ids - existing |
224 |
|
225 |
def _AllMACs(self): |
226 |
"""Return all MACs present in the config.
|
227 |
|
228 |
@rtype: list
|
229 |
@return: the list of all MACs
|
230 |
|
231 |
"""
|
232 |
result = [] |
233 |
for instance in self._config_data.instances.values(): |
234 |
for nic in instance.nics: |
235 |
result.append(nic.mac) |
236 |
|
237 |
return result
|
238 |
|
239 |
def _AllDRBDSecrets(self): |
240 |
"""Return all DRBD secrets present in the config.
|
241 |
|
242 |
@rtype: list
|
243 |
@return: the list of all DRBD secrets
|
244 |
|
245 |
"""
|
246 |
def helper(disk, result): |
247 |
"""Recursively gather secrets from this disk."""
|
248 |
if disk.dev_type == constants.DT_DRBD8:
|
249 |
result.append(disk.logical_id[5])
|
250 |
if disk.children:
|
251 |
for child in disk.children: |
252 |
helper(child, result) |
253 |
|
254 |
result = [] |
255 |
for instance in self._config_data.instances.values(): |
256 |
for disk in instance.disks: |
257 |
helper(disk, result) |
258 |
|
259 |
return result
|
260 |
|
261 |
def _CheckDiskIDs(self, disk, l_ids, p_ids): |
262 |
"""Compute duplicate disk IDs
|
263 |
|
264 |
@type disk: L{objects.Disk}
|
265 |
@param disk: the disk at which to start searching
|
266 |
@type l_ids: list
|
267 |
@param l_ids: list of current logical ids
|
268 |
@type p_ids: list
|
269 |
@param p_ids: list of current physical ids
|
270 |
@rtype: list
|
271 |
@return: a list of error messages
|
272 |
|
273 |
"""
|
274 |
result = [] |
275 |
if disk.logical_id is not None: |
276 |
if disk.logical_id in l_ids: |
277 |
result.append("duplicate logical id %s" % str(disk.logical_id)) |
278 |
else:
|
279 |
l_ids.append(disk.logical_id) |
280 |
if disk.physical_id is not None: |
281 |
if disk.physical_id in p_ids: |
282 |
result.append("duplicate physical id %s" % str(disk.physical_id)) |
283 |
else:
|
284 |
p_ids.append(disk.physical_id) |
285 |
|
286 |
if disk.children:
|
287 |
for child in disk.children: |
288 |
result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
|
289 |
return result
|
290 |
|
291 |
def _UnlockedVerifyConfig(self): |
292 |
"""Verify function.
|
293 |
|
294 |
@rtype: list
|
295 |
@return: a list of error messages; a non-empty list signifies
|
296 |
configuration errors
|
297 |
|
298 |
"""
|
299 |
result = [] |
300 |
seen_macs = [] |
301 |
ports = {} |
302 |
data = self._config_data
|
303 |
seen_lids = [] |
304 |
seen_pids = [] |
305 |
|
306 |
# global cluster checks
|
307 |
if not data.cluster.enabled_hypervisors: |
308 |
result.append("enabled hypervisors list doesn't have any entries")
|
309 |
invalid_hvs = set(data.cluster.enabled_hypervisors) - constants.HYPER_TYPES
|
310 |
if invalid_hvs:
|
311 |
result.append("enabled hypervisors contains invalid entries: %s" %
|
312 |
invalid_hvs) |
313 |
|
314 |
if data.cluster.master_node not in data.nodes: |
315 |
result.append("cluster has invalid primary node '%s'" %
|
316 |
data.cluster.master_node) |
317 |
|
318 |
# per-instance checks
|
319 |
for instance_name in data.instances: |
320 |
instance = data.instances[instance_name] |
321 |
if instance.primary_node not in data.nodes: |
322 |
result.append("instance '%s' has invalid primary node '%s'" %
|
323 |
(instance_name, instance.primary_node)) |
324 |
for snode in instance.secondary_nodes: |
325 |
if snode not in data.nodes: |
326 |
result.append("instance '%s' has invalid secondary node '%s'" %
|
327 |
(instance_name, snode)) |
328 |
for idx, nic in enumerate(instance.nics): |
329 |
if nic.mac in seen_macs: |
330 |
result.append("instance '%s' has NIC %d mac %s duplicate" %
|
331 |
(instance_name, idx, nic.mac)) |
332 |
else:
|
333 |
seen_macs.append(nic.mac) |
334 |
|
335 |
# gather the drbd ports for duplicate checks
|
336 |
for dsk in instance.disks: |
337 |
if dsk.dev_type in constants.LDS_DRBD: |
338 |
tcp_port = dsk.logical_id[2]
|
339 |
if tcp_port not in ports: |
340 |
ports[tcp_port] = [] |
341 |
ports[tcp_port].append((instance.name, "drbd disk %s" % dsk.iv_name))
|
342 |
# gather network port reservation
|
343 |
net_port = getattr(instance, "network_port", None) |
344 |
if net_port is not None: |
345 |
if net_port not in ports: |
346 |
ports[net_port] = [] |
347 |
ports[net_port].append((instance.name, "network port"))
|
348 |
|
349 |
# instance disk verify
|
350 |
for idx, disk in enumerate(instance.disks): |
351 |
result.extend(["instance '%s' disk %d error: %s" %
|
352 |
(instance.name, idx, msg) for msg in disk.Verify()]) |
353 |
result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
|
354 |
|
355 |
# cluster-wide pool of free ports
|
356 |
for free_port in data.cluster.tcpudp_port_pool: |
357 |
if free_port not in ports: |
358 |
ports[free_port] = [] |
359 |
ports[free_port].append(("cluster", "port marked as free")) |
360 |
|
361 |
# compute tcp/udp duplicate ports
|
362 |
keys = ports.keys() |
363 |
keys.sort() |
364 |
for pnum in keys: |
365 |
pdata = ports[pnum] |
366 |
if len(pdata) > 1: |
367 |
txt = ", ".join(["%s/%s" % val for val in pdata]) |
368 |
result.append("tcp/udp port %s has duplicates: %s" % (pnum, txt))
|
369 |
|
370 |
# highest used tcp port check
|
371 |
if keys:
|
372 |
if keys[-1] > data.cluster.highest_used_port: |
373 |
result.append("Highest used port mismatch, saved %s, computed %s" %
|
374 |
(data.cluster.highest_used_port, keys[-1]))
|
375 |
|
376 |
if not data.nodes[data.cluster.master_node].master_candidate: |
377 |
result.append("Master node is not a master candidate")
|
378 |
|
379 |
# master candidate checks
|
380 |
mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
|
381 |
if mc_now < mc_max:
|
382 |
result.append("Not enough master candidates: actual %d, target %d" %
|
383 |
(mc_now, mc_max)) |
384 |
|
385 |
# node checks
|
386 |
for node in data.nodes.values(): |
387 |
if [node.master_candidate, node.drained, node.offline].count(True) > 1: |
388 |
result.append("Node %s state is invalid: master_candidate=%s,"
|
389 |
" drain=%s, offline=%s" %
|
390 |
(node.name, node.master_candidate, node.drain, |
391 |
node.offline)) |
392 |
|
393 |
# drbd minors check
|
394 |
d_map, duplicates = self._UnlockedComputeDRBDMap()
|
395 |
for node, minor, instance_a, instance_b in duplicates: |
396 |
result.append("DRBD minor %d on node %s is assigned twice to instances"
|
397 |
" %s and %s" % (minor, node, instance_a, instance_b))
|
398 |
|
399 |
# IP checks
|
400 |
ips = { data.cluster.master_ip: ["cluster_ip"] }
|
401 |
def _helper(ip, name): |
402 |
if ip in ips: |
403 |
ips[ip].append(name) |
404 |
else:
|
405 |
ips[ip] = [name] |
406 |
|
407 |
for node in data.nodes.values(): |
408 |
_helper(node.primary_ip, "node:%s/primary" % node.name)
|
409 |
if node.secondary_ip != node.primary_ip:
|
410 |
_helper(node.secondary_ip, "node:%s/secondary" % node.name)
|
411 |
for instance in data.instances.values(): |
412 |
for idx, nic in enumerate(instance.nics): |
413 |
if nic.ip is not None: |
414 |
_helper(nic.ip, "instance:%s/nic:%d" % (instance.name, idx))
|
415 |
|
416 |
for ip, owners in ips.items(): |
417 |
if len(owners) > 1: |
418 |
result.append("IP address %s is used by multiple owners: %s" %
|
419 |
(ip, ", ".join(owners)))
|
420 |
return result
|
421 |
|
422 |
@locking.ssynchronized(_config_lock, shared=1) |
423 |
def VerifyConfig(self): |
424 |
"""Verify function.
|
425 |
|
426 |
This is just a wrapper over L{_UnlockedVerifyConfig}.
|
427 |
|
428 |
@rtype: list
|
429 |
@return: a list of error messages; a non-empty list signifies
|
430 |
configuration errors
|
431 |
|
432 |
"""
|
433 |
return self._UnlockedVerifyConfig() |
434 |
|
435 |
def _UnlockedSetDiskID(self, disk, node_name): |
436 |
"""Convert the unique ID to the ID needed on the target nodes.
|
437 |
|
438 |
This is used only for drbd, which needs ip/port configuration.
|
439 |
|
440 |
The routine descends down and updates its children also, because
|
441 |
this helps when the only the top device is passed to the remote
|
442 |
node.
|
443 |
|
444 |
This function is for internal use, when the config lock is already held.
|
445 |
|
446 |
"""
|
447 |
if disk.children:
|
448 |
for child in disk.children: |
449 |
self._UnlockedSetDiskID(child, node_name)
|
450 |
|
451 |
if disk.logical_id is None and disk.physical_id is not None: |
452 |
return
|
453 |
if disk.dev_type == constants.LD_DRBD8:
|
454 |
pnode, snode, port, pminor, sminor, secret = disk.logical_id |
455 |
if node_name not in (pnode, snode): |
456 |
raise errors.ConfigurationError("DRBD device not knowing node %s" % |
457 |
node_name) |
458 |
pnode_info = self._UnlockedGetNodeInfo(pnode)
|
459 |
snode_info = self._UnlockedGetNodeInfo(snode)
|
460 |
if pnode_info is None or snode_info is None: |
461 |
raise errors.ConfigurationError("Can't find primary or secondary node" |
462 |
" for %s" % str(disk)) |
463 |
p_data = (pnode_info.secondary_ip, port) |
464 |
s_data = (snode_info.secondary_ip, port) |
465 |
if pnode == node_name:
|
466 |
disk.physical_id = p_data + s_data + (pminor, secret) |
467 |
else: # it must be secondary, we tested above |
468 |
disk.physical_id = s_data + p_data + (sminor, secret) |
469 |
else:
|
470 |
disk.physical_id = disk.logical_id |
471 |
return
|
472 |
|
473 |
@locking.ssynchronized(_config_lock)
|
474 |
def SetDiskID(self, disk, node_name): |
475 |
"""Convert the unique ID to the ID needed on the target nodes.
|
476 |
|
477 |
This is used only for drbd, which needs ip/port configuration.
|
478 |
|
479 |
The routine descends down and updates its children also, because
|
480 |
this helps when the only the top device is passed to the remote
|
481 |
node.
|
482 |
|
483 |
"""
|
484 |
return self._UnlockedSetDiskID(disk, node_name) |
485 |
|
486 |
@locking.ssynchronized(_config_lock)
|
487 |
def AddTcpUdpPort(self, port): |
488 |
"""Adds a new port to the available port pool.
|
489 |
|
490 |
"""
|
491 |
if not isinstance(port, int): |
492 |
raise errors.ProgrammerError("Invalid type passed for port") |
493 |
|
494 |
self._config_data.cluster.tcpudp_port_pool.add(port)
|
495 |
self._WriteConfig()
|
496 |
|
497 |
@locking.ssynchronized(_config_lock, shared=1) |
498 |
def GetPortList(self): |
499 |
"""Returns a copy of the current port list.
|
500 |
|
501 |
"""
|
502 |
return self._config_data.cluster.tcpudp_port_pool.copy() |
503 |
|
504 |
@locking.ssynchronized(_config_lock)
|
505 |
def AllocatePort(self): |
506 |
"""Allocate a port.
|
507 |
|
508 |
The port will be taken from the available port pool or from the
|
509 |
default port range (and in this case we increase
|
510 |
highest_used_port).
|
511 |
|
512 |
"""
|
513 |
# If there are TCP/IP ports configured, we use them first.
|
514 |
if self._config_data.cluster.tcpudp_port_pool: |
515 |
port = self._config_data.cluster.tcpudp_port_pool.pop()
|
516 |
else:
|
517 |
port = self._config_data.cluster.highest_used_port + 1 |
518 |
if port >= constants.LAST_DRBD_PORT:
|
519 |
raise errors.ConfigurationError("The highest used port is greater" |
520 |
" than %s. Aborting." %
|
521 |
constants.LAST_DRBD_PORT) |
522 |
self._config_data.cluster.highest_used_port = port
|
523 |
|
524 |
self._WriteConfig()
|
525 |
return port
|
526 |
|
527 |
def _UnlockedComputeDRBDMap(self): |
528 |
"""Compute the used DRBD minor/nodes.
|
529 |
|
530 |
@rtype: (dict, list)
|
531 |
@return: dictionary of node_name: dict of minor: instance_name;
|
532 |
the returned dict will have all the nodes in it (even if with
|
533 |
an empty list), and a list of duplicates; if the duplicates
|
534 |
list is not empty, the configuration is corrupted and its caller
|
535 |
should raise an exception
|
536 |
|
537 |
"""
|
538 |
def _AppendUsedPorts(instance_name, disk, used): |
539 |
duplicates = [] |
540 |
if disk.dev_type == constants.LD_DRBD8 and len(disk.logical_id) >= 5: |
541 |
node_a, node_b, _, minor_a, minor_b = disk.logical_id[:5]
|
542 |
for node, port in ((node_a, minor_a), (node_b, minor_b)): |
543 |
assert node in used, ("Node '%s' of instance '%s' not found" |
544 |
" in node list" % (node, instance_name))
|
545 |
if port in used[node]: |
546 |
duplicates.append((node, port, instance_name, used[node][port])) |
547 |
else:
|
548 |
used[node][port] = instance_name |
549 |
if disk.children:
|
550 |
for child in disk.children: |
551 |
duplicates.extend(_AppendUsedPorts(instance_name, child, used)) |
552 |
return duplicates
|
553 |
|
554 |
duplicates = [] |
555 |
my_dict = dict((node, {}) for node in self._config_data.nodes) |
556 |
for instance in self._config_data.instances.itervalues(): |
557 |
for disk in instance.disks: |
558 |
duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict)) |
559 |
for (node, minor), instance in self._temporary_drbds.iteritems(): |
560 |
if minor in my_dict[node] and my_dict[node][minor] != instance: |
561 |
duplicates.append((node, minor, instance, my_dict[node][minor])) |
562 |
else:
|
563 |
my_dict[node][minor] = instance |
564 |
return my_dict, duplicates
|
565 |
|
566 |
@locking.ssynchronized(_config_lock)
|
567 |
def ComputeDRBDMap(self): |
568 |
"""Compute the used DRBD minor/nodes.
|
569 |
|
570 |
This is just a wrapper over L{_UnlockedComputeDRBDMap}.
|
571 |
|
572 |
@return: dictionary of node_name: dict of minor: instance_name;
|
573 |
the returned dict will have all the nodes in it (even if with
|
574 |
an empty list).
|
575 |
|
576 |
"""
|
577 |
d_map, duplicates = self._UnlockedComputeDRBDMap()
|
578 |
if duplicates:
|
579 |
raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" % |
580 |
str(duplicates))
|
581 |
return d_map
|
582 |
|
583 |
@locking.ssynchronized(_config_lock)
|
584 |
def AllocateDRBDMinor(self, nodes, instance): |
585 |
"""Allocate a drbd minor.
|
586 |
|
587 |
The free minor will be automatically computed from the existing
|
588 |
devices. A node can be given multiple times in order to allocate
|
589 |
multiple minors. The result is the list of minors, in the same
|
590 |
order as the passed nodes.
|
591 |
|
592 |
@type instance: string
|
593 |
@param instance: the instance for which we allocate minors
|
594 |
|
595 |
"""
|
596 |
assert isinstance(instance, basestring), \ |
597 |
"Invalid argument '%s' passed to AllocateDRBDMinor" % instance
|
598 |
|
599 |
d_map, duplicates = self._UnlockedComputeDRBDMap()
|
600 |
if duplicates:
|
601 |
raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" % |
602 |
str(duplicates))
|
603 |
result = [] |
604 |
for nname in nodes: |
605 |
ndata = d_map[nname] |
606 |
if not ndata: |
607 |
# no minors used, we can start at 0
|
608 |
result.append(0)
|
609 |
ndata[0] = instance
|
610 |
self._temporary_drbds[(nname, 0)] = instance |
611 |
continue
|
612 |
keys = ndata.keys() |
613 |
keys.sort() |
614 |
ffree = utils.FirstFree(keys) |
615 |
if ffree is None: |
616 |
# return the next minor
|
617 |
# TODO: implement high-limit check
|
618 |
minor = keys[-1] + 1 |
619 |
else:
|
620 |
minor = ffree |
621 |
# double-check minor against current instances
|
622 |
assert minor not in d_map[nname], \ |
623 |
("Attempt to reuse allocated DRBD minor %d on node %s,"
|
624 |
" already allocated to instance %s" %
|
625 |
(minor, nname, d_map[nname][minor])) |
626 |
ndata[minor] = instance |
627 |
# double-check minor against reservation
|
628 |
r_key = (nname, minor) |
629 |
assert r_key not in self._temporary_drbds, \ |
630 |
("Attempt to reuse reserved DRBD minor %d on node %s,"
|
631 |
" reserved for instance %s" %
|
632 |
(minor, nname, self._temporary_drbds[r_key]))
|
633 |
self._temporary_drbds[r_key] = instance
|
634 |
result.append(minor) |
635 |
logging.debug("Request to allocate drbd minors, input: %s, returning %s",
|
636 |
nodes, result) |
637 |
return result
|
638 |
|
639 |
def _UnlockedReleaseDRBDMinors(self, instance): |
640 |
"""Release temporary drbd minors allocated for a given instance.
|
641 |
|
642 |
@type instance: string
|
643 |
@param instance: the instance for which temporary minors should be
|
644 |
released
|
645 |
|
646 |
"""
|
647 |
assert isinstance(instance, basestring), \ |
648 |
"Invalid argument passed to ReleaseDRBDMinors"
|
649 |
for key, name in self._temporary_drbds.items(): |
650 |
if name == instance:
|
651 |
del self._temporary_drbds[key] |
652 |
|
653 |
@locking.ssynchronized(_config_lock)
|
654 |
def ReleaseDRBDMinors(self, instance): |
655 |
"""Release temporary drbd minors allocated for a given instance.
|
656 |
|
657 |
This should be called on the error paths, on the success paths
|
658 |
it's automatically called by the ConfigWriter add and update
|
659 |
functions.
|
660 |
|
661 |
This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
|
662 |
|
663 |
@type instance: string
|
664 |
@param instance: the instance for which temporary minors should be
|
665 |
released
|
666 |
|
667 |
"""
|
668 |
self._UnlockedReleaseDRBDMinors(instance)
|
669 |
|
670 |
@locking.ssynchronized(_config_lock, shared=1) |
671 |
def GetConfigVersion(self): |
672 |
"""Get the configuration version.
|
673 |
|
674 |
@return: Config version
|
675 |
|
676 |
"""
|
677 |
return self._config_data.version |
678 |
|
679 |
@locking.ssynchronized(_config_lock, shared=1) |
680 |
def GetClusterName(self): |
681 |
"""Get cluster name.
|
682 |
|
683 |
@return: Cluster name
|
684 |
|
685 |
"""
|
686 |
return self._config_data.cluster.cluster_name |
687 |
|
688 |
@locking.ssynchronized(_config_lock, shared=1) |
689 |
def GetMasterNode(self): |
690 |
"""Get the hostname of the master node for this cluster.
|
691 |
|
692 |
@return: Master hostname
|
693 |
|
694 |
"""
|
695 |
return self._config_data.cluster.master_node |
696 |
|
697 |
@locking.ssynchronized(_config_lock, shared=1) |
698 |
def GetMasterIP(self): |
699 |
"""Get the IP of the master node for this cluster.
|
700 |
|
701 |
@return: Master IP
|
702 |
|
703 |
"""
|
704 |
return self._config_data.cluster.master_ip |
705 |
|
706 |
@locking.ssynchronized(_config_lock, shared=1) |
707 |
def GetMasterNetdev(self): |
708 |
"""Get the master network device for this cluster.
|
709 |
|
710 |
"""
|
711 |
return self._config_data.cluster.master_netdev |
712 |
|
713 |
@locking.ssynchronized(_config_lock, shared=1) |
714 |
def GetFileStorageDir(self): |
715 |
"""Get the file storage dir for this cluster.
|
716 |
|
717 |
"""
|
718 |
return self._config_data.cluster.file_storage_dir |
719 |
|
720 |
@locking.ssynchronized(_config_lock, shared=1) |
721 |
def GetHypervisorType(self): |
722 |
"""Get the hypervisor type for this cluster.
|
723 |
|
724 |
"""
|
725 |
return self._config_data.cluster.enabled_hypervisors[0] |
726 |
|
727 |
@locking.ssynchronized(_config_lock, shared=1) |
728 |
def GetHostKey(self): |
729 |
"""Return the rsa hostkey from the config.
|
730 |
|
731 |
@rtype: string
|
732 |
@return: the rsa hostkey
|
733 |
|
734 |
"""
|
735 |
return self._config_data.cluster.rsahostkeypub |
736 |
|
737 |
@locking.ssynchronized(_config_lock)
|
738 |
def AddInstance(self, instance): |
739 |
"""Add an instance to the config.
|
740 |
|
741 |
This should be used after creating a new instance.
|
742 |
|
743 |
@type instance: L{objects.Instance}
|
744 |
@param instance: the instance object
|
745 |
|
746 |
"""
|
747 |
if not isinstance(instance, objects.Instance): |
748 |
raise errors.ProgrammerError("Invalid type passed to AddInstance") |
749 |
|
750 |
if instance.disk_template != constants.DT_DISKLESS:
|
751 |
all_lvs = instance.MapLVsByNode() |
752 |
logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
|
753 |
|
754 |
all_macs = self._AllMACs()
|
755 |
for nic in instance.nics: |
756 |
if nic.mac in all_macs: |
757 |
raise errors.ConfigurationError("Cannot add instance %s:" |
758 |
" MAC address '%s' already in use." %
|
759 |
(instance.name, nic.mac)) |
760 |
|
761 |
self._EnsureUUID(instance)
|
762 |
|
763 |
instance.serial_no = 1
|
764 |
instance.ctime = instance.mtime = time.time() |
765 |
self._config_data.instances[instance.name] = instance
|
766 |
self._config_data.cluster.serial_no += 1 |
767 |
self._UnlockedReleaseDRBDMinors(instance.name)
|
768 |
for nic in instance.nics: |
769 |
self._temporary_macs.discard(nic.mac)
|
770 |
self._WriteConfig()
|
771 |
|
772 |
def _EnsureUUID(self, item): |
773 |
"""Ensures a given object has a valid UUID.
|
774 |
|
775 |
@param item: the instance or node to be checked
|
776 |
|
777 |
"""
|
778 |
if not item.uuid: |
779 |
item.uuid = self._GenerateUniqueID()
|
780 |
elif item.uuid in self._AllIDs(temporary=True): |
781 |
raise errors.ConfigurationError("Cannot add '%s': UUID already in use" % |
782 |
(item.name, item.uuid)) |
783 |
|
784 |
def _SetInstanceStatus(self, instance_name, status): |
785 |
"""Set the instance's status to a given value.
|
786 |
|
787 |
"""
|
788 |
assert isinstance(status, bool), \ |
789 |
"Invalid status '%s' passed to SetInstanceStatus" % (status,)
|
790 |
|
791 |
if instance_name not in self._config_data.instances: |
792 |
raise errors.ConfigurationError("Unknown instance '%s'" % |
793 |
instance_name) |
794 |
instance = self._config_data.instances[instance_name]
|
795 |
if instance.admin_up != status:
|
796 |
instance.admin_up = status |
797 |
instance.serial_no += 1
|
798 |
instance.mtime = time.time() |
799 |
self._WriteConfig()
|
800 |
|
801 |
@locking.ssynchronized(_config_lock)
|
802 |
def MarkInstanceUp(self, instance_name): |
803 |
"""Mark the instance status to up in the config.
|
804 |
|
805 |
"""
|
806 |
self._SetInstanceStatus(instance_name, True) |
807 |
|
808 |
@locking.ssynchronized(_config_lock)
|
809 |
def RemoveInstance(self, instance_name): |
810 |
"""Remove the instance from the configuration.
|
811 |
|
812 |
"""
|
813 |
if instance_name not in self._config_data.instances: |
814 |
raise errors.ConfigurationError("Unknown instance '%s'" % instance_name) |
815 |
del self._config_data.instances[instance_name] |
816 |
self._config_data.cluster.serial_no += 1 |
817 |
self._WriteConfig()
|
818 |
|
819 |
@locking.ssynchronized(_config_lock)
|
820 |
def RenameInstance(self, old_name, new_name): |
821 |
"""Rename an instance.
|
822 |
|
823 |
This needs to be done in ConfigWriter and not by RemoveInstance
|
824 |
combined with AddInstance as only we can guarantee an atomic
|
825 |
rename.
|
826 |
|
827 |
"""
|
828 |
if old_name not in self._config_data.instances: |
829 |
raise errors.ConfigurationError("Unknown instance '%s'" % old_name) |
830 |
inst = self._config_data.instances[old_name]
|
831 |
del self._config_data.instances[old_name] |
832 |
inst.name = new_name |
833 |
|
834 |
for disk in inst.disks: |
835 |
if disk.dev_type == constants.LD_FILE:
|
836 |
# rename the file paths in logical and physical id
|
837 |
file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
|
838 |
disk.physical_id = disk.logical_id = (disk.logical_id[0],
|
839 |
os.path.join(file_storage_dir, |
840 |
inst.name, |
841 |
disk.iv_name)) |
842 |
|
843 |
self._config_data.instances[inst.name] = inst
|
844 |
self._WriteConfig()
|
845 |
|
846 |
@locking.ssynchronized(_config_lock)
|
847 |
def MarkInstanceDown(self, instance_name): |
848 |
"""Mark the status of an instance to down in the configuration.
|
849 |
|
850 |
"""
|
851 |
self._SetInstanceStatus(instance_name, False) |
852 |
|
853 |
def _UnlockedGetInstanceList(self): |
854 |
"""Get the list of instances.
|
855 |
|
856 |
This function is for internal use, when the config lock is already held.
|
857 |
|
858 |
"""
|
859 |
return self._config_data.instances.keys() |
860 |
|
861 |
@locking.ssynchronized(_config_lock, shared=1) |
862 |
def GetInstanceList(self): |
863 |
"""Get the list of instances.
|
864 |
|
865 |
@return: array of instances, ex. ['instance2.example.com',
|
866 |
'instance1.example.com']
|
867 |
|
868 |
"""
|
869 |
return self._UnlockedGetInstanceList() |
870 |
|
871 |
@locking.ssynchronized(_config_lock, shared=1) |
872 |
def ExpandInstanceName(self, short_name): |
873 |
"""Attempt to expand an incomplete instance name.
|
874 |
|
875 |
"""
|
876 |
return utils.MatchNameComponent(short_name,
|
877 |
self._config_data.instances.keys(),
|
878 |
case_sensitive=False)
|
879 |
|
880 |
def _UnlockedGetInstanceInfo(self, instance_name): |
881 |
"""Returns information about an instance.
|
882 |
|
883 |
This function is for internal use, when the config lock is already held.
|
884 |
|
885 |
"""
|
886 |
if instance_name not in self._config_data.instances: |
887 |
return None |
888 |
|
889 |
return self._config_data.instances[instance_name] |
890 |
|
891 |
@locking.ssynchronized(_config_lock, shared=1) |
892 |
def GetInstanceInfo(self, instance_name): |
893 |
"""Returns information about an instance.
|
894 |
|
895 |
It takes the information from the configuration file. Other information of
|
896 |
an instance are taken from the live systems.
|
897 |
|
898 |
@param instance_name: name of the instance, e.g.
|
899 |
I{instance1.example.com}
|
900 |
|
901 |
@rtype: L{objects.Instance}
|
902 |
@return: the instance object
|
903 |
|
904 |
"""
|
905 |
return self._UnlockedGetInstanceInfo(instance_name) |
906 |
|
907 |
@locking.ssynchronized(_config_lock, shared=1) |
908 |
def GetAllInstancesInfo(self): |
909 |
"""Get the configuration of all instances.
|
910 |
|
911 |
@rtype: dict
|
912 |
@return: dict of (instance, instance_info), where instance_info is what
|
913 |
would GetInstanceInfo return for the node
|
914 |
|
915 |
"""
|
916 |
my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance)) |
917 |
for instance in self._UnlockedGetInstanceList()]) |
918 |
return my_dict
|
919 |
|
920 |
@locking.ssynchronized(_config_lock)
|
921 |
def AddNode(self, node): |
922 |
"""Add a node to the configuration.
|
923 |
|
924 |
@type node: L{objects.Node}
|
925 |
@param node: a Node instance
|
926 |
|
927 |
"""
|
928 |
logging.info("Adding node %s to configuration", node.name)
|
929 |
|
930 |
self._EnsureUUID(node)
|
931 |
|
932 |
node.serial_no = 1
|
933 |
node.ctime = node.mtime = time.time() |
934 |
self._config_data.nodes[node.name] = node
|
935 |
self._config_data.cluster.serial_no += 1 |
936 |
self._WriteConfig()
|
937 |
|
938 |
@locking.ssynchronized(_config_lock)
|
939 |
def RemoveNode(self, node_name): |
940 |
"""Remove a node from the configuration.
|
941 |
|
942 |
"""
|
943 |
logging.info("Removing node %s from configuration", node_name)
|
944 |
|
945 |
if node_name not in self._config_data.nodes: |
946 |
raise errors.ConfigurationError("Unknown node '%s'" % node_name) |
947 |
|
948 |
del self._config_data.nodes[node_name] |
949 |
self._config_data.cluster.serial_no += 1 |
950 |
self._WriteConfig()
|
951 |
|
952 |
@locking.ssynchronized(_config_lock, shared=1) |
953 |
def ExpandNodeName(self, short_name): |
954 |
"""Attempt to expand an incomplete instance name.
|
955 |
|
956 |
"""
|
957 |
return utils.MatchNameComponent(short_name,
|
958 |
self._config_data.nodes.keys(),
|
959 |
case_sensitive=False)
|
960 |
|
961 |
def _UnlockedGetNodeInfo(self, node_name): |
962 |
"""Get the configuration of a node, as stored in the config.
|
963 |
|
964 |
This function is for internal use, when the config lock is already
|
965 |
held.
|
966 |
|
967 |
@param node_name: the node name, e.g. I{node1.example.com}
|
968 |
|
969 |
@rtype: L{objects.Node}
|
970 |
@return: the node object
|
971 |
|
972 |
"""
|
973 |
if node_name not in self._config_data.nodes: |
974 |
return None |
975 |
|
976 |
return self._config_data.nodes[node_name] |
977 |
|
978 |
|
979 |
@locking.ssynchronized(_config_lock, shared=1) |
980 |
def GetNodeInfo(self, node_name): |
981 |
"""Get the configuration of a node, as stored in the config.
|
982 |
|
983 |
This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
|
984 |
|
985 |
@param node_name: the node name, e.g. I{node1.example.com}
|
986 |
|
987 |
@rtype: L{objects.Node}
|
988 |
@return: the node object
|
989 |
|
990 |
"""
|
991 |
return self._UnlockedGetNodeInfo(node_name) |
992 |
|
993 |
def _UnlockedGetNodeList(self): |
994 |
"""Return the list of nodes which are in the configuration.
|
995 |
|
996 |
This function is for internal use, when the config lock is already
|
997 |
held.
|
998 |
|
999 |
@rtype: list
|
1000 |
|
1001 |
"""
|
1002 |
return self._config_data.nodes.keys() |
1003 |
|
1004 |
|
1005 |
@locking.ssynchronized(_config_lock, shared=1) |
1006 |
def GetNodeList(self): |
1007 |
"""Return the list of nodes which are in the configuration.
|
1008 |
|
1009 |
"""
|
1010 |
return self._UnlockedGetNodeList() |
1011 |
|
1012 |
@locking.ssynchronized(_config_lock, shared=1) |
1013 |
def GetOnlineNodeList(self): |
1014 |
"""Return the list of nodes which are online.
|
1015 |
|
1016 |
"""
|
1017 |
all_nodes = [self._UnlockedGetNodeInfo(node)
|
1018 |
for node in self._UnlockedGetNodeList()] |
1019 |
return [node.name for node in all_nodes if not node.offline] |
1020 |
|
1021 |
@locking.ssynchronized(_config_lock, shared=1) |
1022 |
def GetAllNodesInfo(self): |
1023 |
"""Get the configuration of all nodes.
|
1024 |
|
1025 |
@rtype: dict
|
1026 |
@return: dict of (node, node_info), where node_info is what
|
1027 |
would GetNodeInfo return for the node
|
1028 |
|
1029 |
"""
|
1030 |
my_dict = dict([(node, self._UnlockedGetNodeInfo(node)) |
1031 |
for node in self._UnlockedGetNodeList()]) |
1032 |
return my_dict
|
1033 |
|
1034 |
def _UnlockedGetMasterCandidateStats(self, exceptions=None): |
1035 |
"""Get the number of current and maximum desired and possible candidates.
|
1036 |
|
1037 |
@type exceptions: list
|
1038 |
@param exceptions: if passed, list of nodes that should be ignored
|
1039 |
@rtype: tuple
|
1040 |
@return: tuple of (current, desired and possible, possible)
|
1041 |
|
1042 |
"""
|
1043 |
mc_now = mc_should = mc_max = 0
|
1044 |
for node in self._config_data.nodes.values(): |
1045 |
if exceptions and node.name in exceptions: |
1046 |
continue
|
1047 |
if not (node.offline or node.drained): |
1048 |
mc_max += 1
|
1049 |
if node.master_candidate:
|
1050 |
mc_now += 1
|
1051 |
mc_should = min(mc_max, self._config_data.cluster.candidate_pool_size) |
1052 |
return (mc_now, mc_should, mc_max)
|
1053 |
|
1054 |
@locking.ssynchronized(_config_lock, shared=1) |
1055 |
def GetMasterCandidateStats(self, exceptions=None): |
1056 |
"""Get the number of current and maximum possible candidates.
|
1057 |
|
1058 |
This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
|
1059 |
|
1060 |
@type exceptions: list
|
1061 |
@param exceptions: if passed, list of nodes that should be ignored
|
1062 |
@rtype: tuple
|
1063 |
@return: tuple of (current, max)
|
1064 |
|
1065 |
"""
|
1066 |
return self._UnlockedGetMasterCandidateStats(exceptions) |
1067 |
|
1068 |
@locking.ssynchronized(_config_lock)
|
1069 |
def MaintainCandidatePool(self, exceptions): |
1070 |
"""Try to grow the candidate pool to the desired size.
|
1071 |
|
1072 |
@type exceptions: list
|
1073 |
@param exceptions: if passed, list of nodes that should be ignored
|
1074 |
@rtype: list
|
1075 |
@return: list with the adjusted nodes (L{objects.Node} instances)
|
1076 |
|
1077 |
"""
|
1078 |
mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats(exceptions)
|
1079 |
mod_list = [] |
1080 |
if mc_now < mc_max:
|
1081 |
node_list = self._config_data.nodes.keys()
|
1082 |
random.shuffle(node_list) |
1083 |
for name in node_list: |
1084 |
if mc_now >= mc_max:
|
1085 |
break
|
1086 |
node = self._config_data.nodes[name]
|
1087 |
if (node.master_candidate or node.offline or node.drained or |
1088 |
node.name in exceptions):
|
1089 |
continue
|
1090 |
mod_list.append(node) |
1091 |
node.master_candidate = True
|
1092 |
node.serial_no += 1
|
1093 |
mc_now += 1
|
1094 |
if mc_now != mc_max:
|
1095 |
# this should not happen
|
1096 |
logging.warning("Warning: MaintainCandidatePool didn't manage to"
|
1097 |
" fill the candidate pool (%d/%d)", mc_now, mc_max)
|
1098 |
if mod_list:
|
1099 |
self._config_data.cluster.serial_no += 1 |
1100 |
self._WriteConfig()
|
1101 |
|
1102 |
return mod_list
|
1103 |
|
1104 |
def _BumpSerialNo(self): |
1105 |
"""Bump up the serial number of the config.
|
1106 |
|
1107 |
"""
|
1108 |
self._config_data.serial_no += 1 |
1109 |
self._config_data.mtime = time.time()
|
1110 |
|
1111 |
def _AllUUIDObjects(self): |
1112 |
"""Returns all objects with uuid attributes.
|
1113 |
|
1114 |
"""
|
1115 |
return (self._config_data.instances.values() + |
1116 |
self._config_data.nodes.values() +
|
1117 |
[self._config_data.cluster])
|
1118 |
|
1119 |
def _OpenConfig(self): |
1120 |
"""Read the config data from disk.
|
1121 |
|
1122 |
"""
|
1123 |
raw_data = utils.ReadFile(self._cfg_file)
|
1124 |
|
1125 |
try:
|
1126 |
data = objects.ConfigData.FromDict(serializer.Load(raw_data)) |
1127 |
except Exception, err: |
1128 |
raise errors.ConfigurationError(err)
|
1129 |
|
1130 |
# Make sure the configuration has the right version
|
1131 |
_ValidateConfig(data) |
1132 |
|
1133 |
if (not hasattr(data, 'cluster') or |
1134 |
not hasattr(data.cluster, 'rsahostkeypub')): |
1135 |
raise errors.ConfigurationError("Incomplete configuration" |
1136 |
" (missing cluster.rsahostkeypub)")
|
1137 |
|
1138 |
# Upgrade configuration if needed
|
1139 |
data.UpgradeConfig() |
1140 |
|
1141 |
self._config_data = data
|
1142 |
# reset the last serial as -1 so that the next write will cause
|
1143 |
# ssconf update
|
1144 |
self._last_cluster_serial = -1 |
1145 |
|
1146 |
# And finally run our (custom) config upgrade sequence
|
1147 |
self._UpgradeConfig()
|
1148 |
|
1149 |
def _UpgradeConfig(self): |
1150 |
"""Run upgrade steps that cannot be done purely in the objects.
|
1151 |
|
1152 |
This is because some data elements need uniqueness across the
|
1153 |
whole configuration, etc.
|
1154 |
|
1155 |
@warning: this function will call L{_WriteConfig()}, so it needs
|
1156 |
to either be called with the lock held or from a safe place
|
1157 |
(the constructor)
|
1158 |
|
1159 |
"""
|
1160 |
modified = False
|
1161 |
for item in self._AllUUIDObjects(): |
1162 |
if item.uuid is None: |
1163 |
item.uuid = self._GenerateUniqueID()
|
1164 |
modified = True
|
1165 |
if modified:
|
1166 |
self._WriteConfig()
|
1167 |
|
1168 |
def _DistributeConfig(self, feedback_fn): |
1169 |
"""Distribute the configuration to the other nodes.
|
1170 |
|
1171 |
Currently, this only copies the configuration file. In the future,
|
1172 |
it could be used to encapsulate the 2/3-phase update mechanism.
|
1173 |
|
1174 |
"""
|
1175 |
if self._offline: |
1176 |
return True |
1177 |
|
1178 |
bad = False
|
1179 |
|
1180 |
node_list = [] |
1181 |
addr_list = [] |
1182 |
myhostname = self._my_hostname
|
1183 |
# we can skip checking whether _UnlockedGetNodeInfo returns None
|
1184 |
# since the node list comes from _UnlocketGetNodeList, and we are
|
1185 |
# called with the lock held, so no modifications should take place
|
1186 |
# in between
|
1187 |
for node_name in self._UnlockedGetNodeList(): |
1188 |
if node_name == myhostname:
|
1189 |
continue
|
1190 |
node_info = self._UnlockedGetNodeInfo(node_name)
|
1191 |
if not node_info.master_candidate: |
1192 |
continue
|
1193 |
node_list.append(node_info.name) |
1194 |
addr_list.append(node_info.primary_ip) |
1195 |
|
1196 |
result = rpc.RpcRunner.call_upload_file(node_list, self._cfg_file,
|
1197 |
address_list=addr_list) |
1198 |
for to_node, to_result in result.items(): |
1199 |
msg = to_result.fail_msg |
1200 |
if msg:
|
1201 |
msg = ("Copy of file %s to node %s failed: %s" %
|
1202 |
(self._cfg_file, to_node, msg))
|
1203 |
logging.error(msg) |
1204 |
|
1205 |
if feedback_fn:
|
1206 |
feedback_fn(msg) |
1207 |
|
1208 |
bad = True
|
1209 |
|
1210 |
return not bad |
1211 |
|
1212 |
def _WriteConfig(self, destination=None, feedback_fn=None): |
1213 |
"""Write the configuration data to persistent storage.
|
1214 |
|
1215 |
"""
|
1216 |
assert feedback_fn is None or callable(feedback_fn) |
1217 |
|
1218 |
# First, cleanup the _temporary_ids set, if an ID is now in the
|
1219 |
# other objects it should be discarded to prevent unbounded growth
|
1220 |
# of that structure
|
1221 |
self._CleanupTemporaryIDs()
|
1222 |
|
1223 |
# Warn on config errors, but don't abort the save - the
|
1224 |
# configuration has already been modified, and we can't revert;
|
1225 |
# the best we can do is to warn the user and save as is, leaving
|
1226 |
# recovery to the user
|
1227 |
config_errors = self._UnlockedVerifyConfig()
|
1228 |
if config_errors:
|
1229 |
errmsg = ("Configuration data is not consistent: %s" %
|
1230 |
(", ".join(config_errors)))
|
1231 |
logging.critical(errmsg) |
1232 |
if feedback_fn:
|
1233 |
feedback_fn(errmsg) |
1234 |
|
1235 |
if destination is None: |
1236 |
destination = self._cfg_file
|
1237 |
self._BumpSerialNo()
|
1238 |
txt = serializer.Dump(self._config_data.ToDict())
|
1239 |
|
1240 |
utils.WriteFile(destination, data=txt) |
1241 |
|
1242 |
self.write_count += 1 |
1243 |
|
1244 |
# and redistribute the config file to master candidates
|
1245 |
self._DistributeConfig(feedback_fn)
|
1246 |
|
1247 |
# Write ssconf files on all nodes (including locally)
|
1248 |
if self._last_cluster_serial < self._config_data.cluster.serial_no: |
1249 |
if not self._offline: |
1250 |
result = rpc.RpcRunner.call_write_ssconf_files( |
1251 |
self._UnlockedGetNodeList(),
|
1252 |
self._UnlockedGetSsconfValues())
|
1253 |
|
1254 |
for nname, nresu in result.items(): |
1255 |
msg = nresu.fail_msg |
1256 |
if msg:
|
1257 |
errmsg = ("Error while uploading ssconf files to"
|
1258 |
" node %s: %s" % (nname, msg))
|
1259 |
logging.warning(errmsg) |
1260 |
|
1261 |
if feedback_fn:
|
1262 |
feedback_fn(errmsg) |
1263 |
|
1264 |
self._last_cluster_serial = self._config_data.cluster.serial_no |
1265 |
|
1266 |
def _UnlockedGetSsconfValues(self): |
1267 |
"""Return the values needed by ssconf.
|
1268 |
|
1269 |
@rtype: dict
|
1270 |
@return: a dictionary with keys the ssconf names and values their
|
1271 |
associated value
|
1272 |
|
1273 |
"""
|
1274 |
fn = "\n".join
|
1275 |
instance_names = utils.NiceSort(self._UnlockedGetInstanceList())
|
1276 |
node_names = utils.NiceSort(self._UnlockedGetNodeList())
|
1277 |
node_info = [self._UnlockedGetNodeInfo(name) for name in node_names] |
1278 |
node_pri_ips = ["%s %s" % (ninfo.name, ninfo.primary_ip)
|
1279 |
for ninfo in node_info] |
1280 |
node_snd_ips = ["%s %s" % (ninfo.name, ninfo.secondary_ip)
|
1281 |
for ninfo in node_info] |
1282 |
|
1283 |
instance_data = fn(instance_names) |
1284 |
off_data = fn(node.name for node in node_info if node.offline) |
1285 |
on_data = fn(node.name for node in node_info if not node.offline) |
1286 |
mc_data = fn(node.name for node in node_info if node.master_candidate) |
1287 |
mc_ips_data = fn(node.primary_ip for node in node_info |
1288 |
if node.master_candidate)
|
1289 |
node_data = fn(node_names) |
1290 |
node_pri_ips_data = fn(node_pri_ips) |
1291 |
node_snd_ips_data = fn(node_snd_ips) |
1292 |
|
1293 |
cluster = self._config_data.cluster
|
1294 |
cluster_tags = fn(cluster.GetTags()) |
1295 |
return {
|
1296 |
constants.SS_CLUSTER_NAME: cluster.cluster_name, |
1297 |
constants.SS_CLUSTER_TAGS: cluster_tags, |
1298 |
constants.SS_FILE_STORAGE_DIR: cluster.file_storage_dir, |
1299 |
constants.SS_MASTER_CANDIDATES: mc_data, |
1300 |
constants.SS_MASTER_CANDIDATES_IPS: mc_ips_data, |
1301 |
constants.SS_MASTER_IP: cluster.master_ip, |
1302 |
constants.SS_MASTER_NETDEV: cluster.master_netdev, |
1303 |
constants.SS_MASTER_NODE: cluster.master_node, |
1304 |
constants.SS_NODE_LIST: node_data, |
1305 |
constants.SS_NODE_PRIMARY_IPS: node_pri_ips_data, |
1306 |
constants.SS_NODE_SECONDARY_IPS: node_snd_ips_data, |
1307 |
constants.SS_OFFLINE_NODES: off_data, |
1308 |
constants.SS_ONLINE_NODES: on_data, |
1309 |
constants.SS_INSTANCE_LIST: instance_data, |
1310 |
constants.SS_RELEASE_VERSION: constants.RELEASE_VERSION, |
1311 |
} |
1312 |
|
1313 |
@locking.ssynchronized(_config_lock, shared=1) |
1314 |
def GetVGName(self): |
1315 |
"""Return the volume group name.
|
1316 |
|
1317 |
"""
|
1318 |
return self._config_data.cluster.volume_group_name |
1319 |
|
1320 |
@locking.ssynchronized(_config_lock)
|
1321 |
def SetVGName(self, vg_name): |
1322 |
"""Set the volume group name.
|
1323 |
|
1324 |
"""
|
1325 |
self._config_data.cluster.volume_group_name = vg_name
|
1326 |
self._config_data.cluster.serial_no += 1 |
1327 |
self._WriteConfig()
|
1328 |
|
1329 |
@locking.ssynchronized(_config_lock, shared=1) |
1330 |
def GetMACPrefix(self): |
1331 |
"""Return the mac prefix.
|
1332 |
|
1333 |
"""
|
1334 |
return self._config_data.cluster.mac_prefix |
1335 |
|
1336 |
@locking.ssynchronized(_config_lock, shared=1) |
1337 |
def GetClusterInfo(self): |
1338 |
"""Returns information about the cluster
|
1339 |
|
1340 |
@rtype: L{objects.Cluster}
|
1341 |
@return: the cluster object
|
1342 |
|
1343 |
"""
|
1344 |
return self._config_data.cluster |
1345 |
|
1346 |
@locking.ssynchronized(_config_lock)
|
1347 |
def Update(self, target, feedback_fn): |
1348 |
"""Notify function to be called after updates.
|
1349 |
|
1350 |
This function must be called when an object (as returned by
|
1351 |
GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
|
1352 |
caller wants the modifications saved to the backing store. Note
|
1353 |
that all modified objects will be saved, but the target argument
|
1354 |
is the one the caller wants to ensure that it's saved.
|
1355 |
|
1356 |
@param target: an instance of either L{objects.Cluster},
|
1357 |
L{objects.Node} or L{objects.Instance} which is existing in
|
1358 |
the cluster
|
1359 |
@param feedback_fn: Callable feedback function
|
1360 |
|
1361 |
"""
|
1362 |
if self._config_data is None: |
1363 |
raise errors.ProgrammerError("Configuration file not read," |
1364 |
" cannot save.")
|
1365 |
update_serial = False
|
1366 |
if isinstance(target, objects.Cluster): |
1367 |
test = target == self._config_data.cluster
|
1368 |
elif isinstance(target, objects.Node): |
1369 |
test = target in self._config_data.nodes.values() |
1370 |
update_serial = True
|
1371 |
elif isinstance(target, objects.Instance): |
1372 |
test = target in self._config_data.instances.values() |
1373 |
else:
|
1374 |
raise errors.ProgrammerError("Invalid object type (%s) passed to" |
1375 |
" ConfigWriter.Update" % type(target)) |
1376 |
if not test: |
1377 |
raise errors.ConfigurationError("Configuration updated since object" |
1378 |
" has been read or unknown object")
|
1379 |
target.serial_no += 1
|
1380 |
target.mtime = now = time.time() |
1381 |
|
1382 |
if update_serial:
|
1383 |
# for node updates, we need to increase the cluster serial too
|
1384 |
self._config_data.cluster.serial_no += 1 |
1385 |
self._config_data.cluster.mtime = now
|
1386 |
|
1387 |
if isinstance(target, objects.Instance): |
1388 |
self._UnlockedReleaseDRBDMinors(target.name)
|
1389 |
for nic in target.nics: |
1390 |
self._temporary_macs.discard(nic.mac)
|
1391 |
|
1392 |
self._WriteConfig(feedback_fn=feedback_fn)
|