4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
22 """Configuration management for Ganeti
24 This module provides the interface to the Ganeti cluster configuration.
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.
29 Currently, the data storage format is JSON. YAML was slow and consuming too
34 # pylint: disable=R0904
35 # R0904: Too many public methods
43 from ganeti import errors
44 from ganeti import locking
45 from ganeti import utils
46 from ganeti import constants
47 from ganeti import rpc
48 from ganeti import objects
49 from ganeti import serializer
50 from ganeti import uidpool
51 from ganeti import netutils
52 from ganeti import runtime
53 from ganeti import pathutils
54 from ganeti import network
57 _config_lock = locking.SharedLock("ConfigWriter")
59 # job id used for resource management at config upgrade time
60 _UPGRADE_CONFIG_JID = "jid-cfg-upgrade"
63 def _ValidateConfig(data):
64 """Verifies that a configuration objects looks valid.
66 This only verifies the version of the configuration.
68 @raise errors.ConfigurationError: if the version differs from what
72 if data.version != constants.CONFIG_VERSION:
73 raise errors.ConfigVersionMismatch(constants.CONFIG_VERSION, data.version)
76 class TemporaryReservationManager:
77 """A temporary resource reservation manager.
79 This is used to reserve resources in a job, before using them, making sure
80 other jobs cannot get them in the meantime.
84 self._ec_reserved = {}
86 def Reserved(self, resource):
87 for holder_reserved in self._ec_reserved.values():
88 if resource in holder_reserved:
92 def Reserve(self, ec_id, resource):
93 if self.Reserved(resource):
94 raise errors.ReservationError("Duplicate reservation for resource '%s'"
96 if ec_id not in self._ec_reserved:
97 self._ec_reserved[ec_id] = set([resource])
99 self._ec_reserved[ec_id].add(resource)
101 def DropECReservations(self, ec_id):
102 if ec_id in self._ec_reserved:
103 del self._ec_reserved[ec_id]
105 def GetReserved(self):
107 for holder_reserved in self._ec_reserved.values():
108 all_reserved.update(holder_reserved)
111 def GetECReserved(self, ec_id):
112 """ Used when you want to retrieve all reservations for a specific
113 execution context. E.g when commiting reserved IPs for a specific
118 if ec_id in self._ec_reserved:
119 ec_reserved.update(self._ec_reserved[ec_id])
122 def Generate(self, existing, generate_one_fn, ec_id):
123 """Generate a new resource of this type
126 assert callable(generate_one_fn)
128 all_elems = self.GetReserved()
129 all_elems.update(existing)
132 new_resource = generate_one_fn()
133 if new_resource is not None and new_resource not in all_elems:
136 raise errors.ConfigurationError("Not able generate new resource"
137 " (last tried: %s)" % new_resource)
138 self.Reserve(ec_id, new_resource)
142 def _MatchNameComponentIgnoreCase(short_name, names):
143 """Wrapper around L{utils.text.MatchNameComponent}.
146 return utils.MatchNameComponent(short_name, names, case_sensitive=False)
149 def _CheckInstanceDiskIvNames(disks):
150 """Checks if instance's disks' C{iv_name} attributes are in order.
152 @type disks: list of L{objects.Disk}
153 @param disks: List of disks
154 @rtype: list of tuples; (int, string, string)
155 @return: List of wrongly named disks, each tuple contains disk index,
156 expected and actual name
161 for (idx, disk) in enumerate(disks):
162 exp_iv_name = "disk/%s" % idx
163 if disk.iv_name != exp_iv_name:
164 result.append((idx, exp_iv_name, disk.iv_name))
170 """The interface to the cluster configuration.
172 @ivar _temporary_lvs: reservation manager for temporary LVs
173 @ivar _all_rms: a list of all temporary reservation managers
176 def __init__(self, cfg_file=None, offline=False, _getents=runtime.GetEnts,
177 accept_foreign=False):
179 self._lock = _config_lock
180 self._config_data = None
181 self._offline = offline
183 self._cfg_file = pathutils.CLUSTER_CONF_FILE
185 self._cfg_file = cfg_file
186 self._getents = _getents
187 self._temporary_ids = TemporaryReservationManager()
188 self._temporary_drbds = {}
189 self._temporary_macs = TemporaryReservationManager()
190 self._temporary_secrets = TemporaryReservationManager()
191 self._temporary_lvs = TemporaryReservationManager()
192 self._temporary_ips = TemporaryReservationManager()
193 self._all_rms = [self._temporary_ids, self._temporary_macs,
194 self._temporary_secrets, self._temporary_lvs,
196 # Note: in order to prevent errors when resolving our name in
197 # _DistributeConfig, we compute it here once and reuse it; it's
198 # better to raise an error before starting to modify the config
199 # file than after it was modified
200 self._my_hostname = netutils.Hostname.GetSysName()
201 self._last_cluster_serial = -1
204 self._OpenConfig(accept_foreign)
206 def _GetRpc(self, address_list):
207 """Returns RPC runner for configuration.
210 return rpc.ConfigRunner(self._context, address_list)
212 def SetContext(self, context):
213 """Sets Ganeti context.
216 self._context = context
218 # this method needs to be static, so that we can call it on the class
221 """Check if the cluster is configured.
224 return os.path.exists(pathutils.CLUSTER_CONF_FILE)
226 @locking.ssynchronized(_config_lock, shared=1)
227 def GetNdParams(self, node):
228 """Get the node params populated with cluster defaults.
230 @type node: L{objects.Node}
231 @param node: The node we want to know the params for
232 @return: A dict with the filled in node params
235 nodegroup = self._UnlockedGetNodeGroup(node.group)
236 return self._config_data.cluster.FillND(node, nodegroup)
238 @locking.ssynchronized(_config_lock, shared=1)
239 def GetInstanceDiskParams(self, instance):
240 """Get the disk params populated with inherit chain.
242 @type instance: L{objects.Instance}
243 @param instance: The instance we want to know the params for
244 @return: A dict with the filled in disk params
247 node = self._UnlockedGetNodeInfo(instance.primary_node)
248 nodegroup = self._UnlockedGetNodeGroup(node.group)
249 return self._UnlockedGetGroupDiskParams(nodegroup)
251 @locking.ssynchronized(_config_lock, shared=1)
252 def GetGroupDiskParams(self, group):
253 """Get the disk params populated with inherit chain.
255 @type group: L{objects.NodeGroup}
256 @param group: The group we want to know the params for
257 @return: A dict with the filled in disk params
260 return self._UnlockedGetGroupDiskParams(group)
262 def _UnlockedGetGroupDiskParams(self, group):
263 """Get the disk params populated with inherit chain down to node-group.
265 @type group: L{objects.NodeGroup}
266 @param group: The group we want to know the params for
267 @return: A dict with the filled in disk params
270 return self._config_data.cluster.SimpleFillDP(group.diskparams)
272 def _UnlockedGetNetworkMACPrefix(self, net):
273 """Return the network mac prefix if it exists or the cluster level default.
278 net_uuid = self._UnlockedLookupNetwork(net)
280 nobj = self._UnlockedGetNetwork(net_uuid)
282 prefix = nobj.mac_prefix
286 def _GenerateOneMAC(self, prefix=None):
287 """Return a function that randomly generates a MAC suffic
288 and appends it to the given prefix. If prefix is not given get
289 the cluster level default.
293 prefix = self._config_data.cluster.mac_prefix
296 byte1 = random.randrange(0, 256)
297 byte2 = random.randrange(0, 256)
298 byte3 = random.randrange(0, 256)
299 mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
304 @locking.ssynchronized(_config_lock, shared=1)
305 def GenerateMAC(self, net, ec_id):
306 """Generate a MAC for an instance.
308 This should check the current instances for duplicates.
311 existing = self._AllMACs()
312 prefix = self._UnlockedGetNetworkMACPrefix(net)
313 gen_mac = self._GenerateOneMAC(prefix)
314 return self._temporary_ids.Generate(existing, gen_mac, ec_id)
316 @locking.ssynchronized(_config_lock, shared=1)
317 def ReserveMAC(self, mac, ec_id):
318 """Reserve a MAC for an instance.
320 This only checks instances managed by this cluster, it does not
321 check for potential collisions elsewhere.
324 all_macs = self._AllMACs()
326 raise errors.ReservationError("mac already in use")
328 self._temporary_macs.Reserve(ec_id, mac)
330 def _UnlockedCommitTemporaryIps(self, ec_id):
331 """Commit all reserved IP address to their respective pools
334 for action, address, net_uuid in self._temporary_ips.GetECReserved(ec_id):
335 self._UnlockedCommitIp(action, net_uuid, address)
337 def _UnlockedCommitIp(self, action, net_uuid, address):
338 """Commit a reserved IP address to an IP pool.
340 The IP address is taken from the network's IP pool and marked as reserved.
343 nobj = self._UnlockedGetNetwork(net_uuid)
344 pool = network.AddressPool(nobj)
345 if action == constants.RESERVE_ACTION:
346 pool.Reserve(address)
347 elif action == constants.RELEASE_ACTION:
348 pool.Release(address)
350 def _UnlockedReleaseIp(self, net_uuid, address, ec_id):
351 """Give a specific IP address back to an IP pool.
353 The IP address is returned to the IP pool designated by pool_id and marked
357 self._temporary_ips.Reserve(ec_id,
358 (constants.RELEASE_ACTION, address, net_uuid))
360 @locking.ssynchronized(_config_lock, shared=1)
361 def ReleaseIp(self, net, address, ec_id):
362 """Give a specified IP address back to an IP pool.
364 This is just a wrapper around _UnlockedReleaseIp.
367 net_uuid = self._UnlockedLookupNetwork(net)
369 self._UnlockedReleaseIp(net_uuid, address, ec_id)
371 @locking.ssynchronized(_config_lock, shared=1)
372 def GenerateIp(self, net, ec_id):
373 """Find a free IPv4 address for an instance.
376 net_uuid = self._UnlockedLookupNetwork(net)
377 nobj = self._UnlockedGetNetwork(net_uuid)
378 pool = network.AddressPool(nobj)
379 gen_free = pool.GenerateFree()
384 except StopIteration:
385 raise errors.ReservationError("Cannot generate IP. Network is full")
386 return (constants.RESERVE_ACTION, ip, net_uuid)
388 _, address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
391 def _UnlockedReserveIp(self, net_uuid, address, ec_id):
392 """Reserve a given IPv4 address for use by an instance.
395 nobj = self._UnlockedGetNetwork(net_uuid)
396 pool = network.AddressPool(nobj)
398 isreserved = pool.IsReserved(address)
399 except errors.AddressPoolError:
400 raise errors.ReservationError("IP address not in network")
402 raise errors.ReservationError("IP address already in use")
404 return self._temporary_ips.Reserve(ec_id,
405 (constants.RESERVE_ACTION,
408 @locking.ssynchronized(_config_lock, shared=1)
409 def ReserveIp(self, net, address, ec_id):
410 """Reserve a given IPv4 address for use by an instance.
413 net_uuid = self._UnlockedLookupNetwork(net)
415 return self._UnlockedReserveIp(net_uuid, address, ec_id)
417 @locking.ssynchronized(_config_lock, shared=1)
418 def ReserveLV(self, lv_name, ec_id):
419 """Reserve an VG/LV pair for an instance.
421 @type lv_name: string
422 @param lv_name: the logical volume name to reserve
425 all_lvs = self._AllLVs()
426 if lv_name in all_lvs:
427 raise errors.ReservationError("LV already in use")
429 self._temporary_lvs.Reserve(ec_id, lv_name)
431 @locking.ssynchronized(_config_lock, shared=1)
432 def GenerateDRBDSecret(self, ec_id):
433 """Generate a DRBD secret.
435 This checks the current disks for duplicates.
438 return self._temporary_secrets.Generate(self._AllDRBDSecrets(),
439 utils.GenerateSecret,
443 """Compute the list of all LVs.
447 for instance in self._config_data.instances.values():
448 node_data = instance.MapLVsByNode()
449 for lv_list in node_data.values():
450 lvnames.update(lv_list)
453 def _AllIDs(self, include_temporary):
454 """Compute the list of all UUIDs and names we have.
456 @type include_temporary: boolean
457 @param include_temporary: whether to include the _temporary_ids set
459 @return: a set of IDs
463 if include_temporary:
464 existing.update(self._temporary_ids.GetReserved())
465 existing.update(self._AllLVs())
466 existing.update(self._config_data.instances.keys())
467 existing.update(self._config_data.nodes.keys())
468 existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid])
471 def _GenerateUniqueID(self, ec_id):
472 """Generate an unique UUID.
474 This checks the current node, instances and disk names for
478 @return: the unique id
481 existing = self._AllIDs(include_temporary=False)
482 return self._temporary_ids.Generate(existing, utils.NewUUID, ec_id)
484 @locking.ssynchronized(_config_lock, shared=1)
485 def GenerateUniqueID(self, ec_id):
486 """Generate an unique ID.
488 This is just a wrapper over the unlocked version.
491 @param ec_id: unique id for the job to reserve the id to
494 return self._GenerateUniqueID(ec_id)
497 """Return all MACs present in the config.
500 @return: the list of all MACs
504 for instance in self._config_data.instances.values():
505 for nic in instance.nics:
506 result.append(nic.mac)
510 def _AllDRBDSecrets(self):
511 """Return all DRBD secrets present in the config.
514 @return: the list of all DRBD secrets
517 def helper(disk, result):
518 """Recursively gather secrets from this disk."""
519 if disk.dev_type == constants.DT_DRBD8:
520 result.append(disk.logical_id[5])
522 for child in disk.children:
523 helper(child, result)
526 for instance in self._config_data.instances.values():
527 for disk in instance.disks:
532 def _CheckDiskIDs(self, disk, l_ids, p_ids):
533 """Compute duplicate disk IDs
535 @type disk: L{objects.Disk}
536 @param disk: the disk at which to start searching
538 @param l_ids: list of current logical ids
540 @param p_ids: list of current physical ids
542 @return: a list of error messages
546 if disk.logical_id is not None:
547 if disk.logical_id in l_ids:
548 result.append("duplicate logical id %s" % str(disk.logical_id))
550 l_ids.append(disk.logical_id)
551 if disk.physical_id is not None:
552 if disk.physical_id in p_ids:
553 result.append("duplicate physical id %s" % str(disk.physical_id))
555 p_ids.append(disk.physical_id)
558 for child in disk.children:
559 result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
562 def _UnlockedVerifyConfig(self):
566 @return: a list of error messages; a non-empty list signifies
570 # pylint: disable=R0914
574 data = self._config_data
575 cluster = data.cluster
579 # global cluster checks
580 if not cluster.enabled_hypervisors:
581 result.append("enabled hypervisors list doesn't have any entries")
582 invalid_hvs = set(cluster.enabled_hypervisors) - constants.HYPER_TYPES
584 result.append("enabled hypervisors contains invalid entries: %s" %
586 missing_hvp = (set(cluster.enabled_hypervisors) -
587 set(cluster.hvparams.keys()))
589 result.append("hypervisor parameters missing for the enabled"
590 " hypervisor(s) %s" % utils.CommaJoin(missing_hvp))
592 if cluster.master_node not in data.nodes:
593 result.append("cluster has invalid primary node '%s'" %
596 def _helper(owner, attr, value, template):
598 utils.ForceDictType(value, template)
599 except errors.GenericError, err:
600 result.append("%s has invalid %s: %s" % (owner, attr, err))
602 def _helper_nic(owner, params):
604 objects.NIC.CheckParameterSyntax(params)
605 except errors.ConfigurationError, err:
606 result.append("%s has invalid nicparams: %s" % (owner, err))
608 def _helper_ipolicy(owner, params, check_std):
610 objects.InstancePolicy.CheckParameterSyntax(params, check_std)
611 except errors.ConfigurationError, err:
612 result.append("%s has invalid instance policy: %s" % (owner, err))
614 def _helper_ispecs(owner, params):
615 for key, value in params.items():
616 if key in constants.IPOLICY_ISPECS:
617 fullkey = "ipolicy/" + key
618 _helper(owner, fullkey, value, constants.ISPECS_PARAMETER_TYPES)
620 # FIXME: assuming list type
621 if key in constants.IPOLICY_PARAMETERS:
625 if not isinstance(value, exp_type):
626 result.append("%s has invalid instance policy: for %s,"
627 " expecting %s, got %s" %
628 (owner, key, exp_type.__name__, type(value)))
630 # check cluster parameters
631 _helper("cluster", "beparams", cluster.SimpleFillBE({}),
632 constants.BES_PARAMETER_TYPES)
633 _helper("cluster", "nicparams", cluster.SimpleFillNIC({}),
634 constants.NICS_PARAMETER_TYPES)
635 _helper_nic("cluster", cluster.SimpleFillNIC({}))
636 _helper("cluster", "ndparams", cluster.SimpleFillND({}),
637 constants.NDS_PARAMETER_TYPES)
638 _helper_ipolicy("cluster", cluster.SimpleFillIPolicy({}), True)
639 _helper_ispecs("cluster", cluster.SimpleFillIPolicy({}))
641 # per-instance checks
642 for instance_name in data.instances:
643 instance = data.instances[instance_name]
644 if instance.name != instance_name:
645 result.append("instance '%s' is indexed by wrong name '%s'" %
646 (instance.name, instance_name))
647 if instance.primary_node not in data.nodes:
648 result.append("instance '%s' has invalid primary node '%s'" %
649 (instance_name, instance.primary_node))
650 for snode in instance.secondary_nodes:
651 if snode not in data.nodes:
652 result.append("instance '%s' has invalid secondary node '%s'" %
653 (instance_name, snode))
654 for idx, nic in enumerate(instance.nics):
655 if nic.mac in seen_macs:
656 result.append("instance '%s' has NIC %d mac %s duplicate" %
657 (instance_name, idx, nic.mac))
659 seen_macs.append(nic.mac)
661 filled = cluster.SimpleFillNIC(nic.nicparams)
662 owner = "instance %s nic %d" % (instance.name, idx)
663 _helper(owner, "nicparams",
664 filled, constants.NICS_PARAMETER_TYPES)
665 _helper_nic(owner, filled)
668 if instance.beparams:
669 _helper("instance %s" % instance.name, "beparams",
670 cluster.FillBE(instance), constants.BES_PARAMETER_TYPES)
672 # gather the drbd ports for duplicate checks
673 for (idx, dsk) in enumerate(instance.disks):
674 if dsk.dev_type in constants.LDS_DRBD:
675 tcp_port = dsk.logical_id[2]
676 if tcp_port not in ports:
678 ports[tcp_port].append((instance.name, "drbd disk %s" % idx))
679 # gather network port reservation
680 net_port = getattr(instance, "network_port", None)
681 if net_port is not None:
682 if net_port not in ports:
684 ports[net_port].append((instance.name, "network port"))
686 # instance disk verify
687 for idx, disk in enumerate(instance.disks):
688 result.extend(["instance '%s' disk %d error: %s" %
689 (instance.name, idx, msg) for msg in disk.Verify()])
690 result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
692 wrong_names = _CheckInstanceDiskIvNames(instance.disks)
694 tmp = "; ".join(("name of disk %s should be '%s', but is '%s'" %
695 (idx, exp_name, actual_name))
696 for (idx, exp_name, actual_name) in wrong_names)
698 result.append("Instance '%s' has wrongly named disks: %s" %
699 (instance.name, tmp))
701 # cluster-wide pool of free ports
702 for free_port in cluster.tcpudp_port_pool:
703 if free_port not in ports:
704 ports[free_port] = []
705 ports[free_port].append(("cluster", "port marked as free"))
707 # compute tcp/udp duplicate ports
713 txt = utils.CommaJoin(["%s/%s" % val for val in pdata])
714 result.append("tcp/udp port %s has duplicates: %s" % (pnum, txt))
716 # highest used tcp port check
718 if keys[-1] > cluster.highest_used_port:
719 result.append("Highest used port mismatch, saved %s, computed %s" %
720 (cluster.highest_used_port, keys[-1]))
722 if not data.nodes[cluster.master_node].master_candidate:
723 result.append("Master node is not a master candidate")
725 # master candidate checks
726 mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
728 result.append("Not enough master candidates: actual %d, target %d" %
732 for node_name, node in data.nodes.items():
733 if node.name != node_name:
734 result.append("Node '%s' is indexed by wrong name '%s'" %
735 (node.name, node_name))
736 if [node.master_candidate, node.drained, node.offline].count(True) > 1:
737 result.append("Node %s state is invalid: master_candidate=%s,"
738 " drain=%s, offline=%s" %
739 (node.name, node.master_candidate, node.drained,
741 if node.group not in data.nodegroups:
742 result.append("Node '%s' has invalid group '%s'" %
743 (node.name, node.group))
745 _helper("node %s" % node.name, "ndparams",
746 cluster.FillND(node, data.nodegroups[node.group]),
747 constants.NDS_PARAMETER_TYPES)
750 nodegroups_names = set()
751 for nodegroup_uuid in data.nodegroups:
752 nodegroup = data.nodegroups[nodegroup_uuid]
753 if nodegroup.uuid != nodegroup_uuid:
754 result.append("node group '%s' (uuid: '%s') indexed by wrong uuid '%s'"
755 % (nodegroup.name, nodegroup.uuid, nodegroup_uuid))
756 if utils.UUID_RE.match(nodegroup.name.lower()):
757 result.append("node group '%s' (uuid: '%s') has uuid-like name" %
758 (nodegroup.name, nodegroup.uuid))
759 if nodegroup.name in nodegroups_names:
760 result.append("duplicate node group name '%s'" % nodegroup.name)
762 nodegroups_names.add(nodegroup.name)
763 group_name = "group %s" % nodegroup.name
764 _helper_ipolicy(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy),
766 _helper_ispecs(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy))
767 if nodegroup.ndparams:
768 _helper(group_name, "ndparams",
769 cluster.SimpleFillND(nodegroup.ndparams),
770 constants.NDS_PARAMETER_TYPES)
773 _, duplicates = self._UnlockedComputeDRBDMap()
774 for node, minor, instance_a, instance_b in duplicates:
775 result.append("DRBD minor %d on node %s is assigned twice to instances"
776 " %s and %s" % (minor, node, instance_a, instance_b))
779 default_nicparams = cluster.nicparams[constants.PP_DEFAULT]
782 def _AddIpAddress(ip, name):
783 ips.setdefault(ip, []).append(name)
785 _AddIpAddress(cluster.master_ip, "cluster_ip")
787 for node in data.nodes.values():
788 _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
789 if node.secondary_ip != node.primary_ip:
790 _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
792 for instance in data.instances.values():
793 for idx, nic in enumerate(instance.nics):
797 nicparams = objects.FillDict(default_nicparams, nic.nicparams)
798 nic_mode = nicparams[constants.NIC_MODE]
799 nic_link = nicparams[constants.NIC_LINK]
801 if nic_mode == constants.NIC_MODE_BRIDGED:
802 link = "bridge:%s" % nic_link
803 elif nic_mode == constants.NIC_MODE_ROUTED:
804 link = "route:%s" % nic_link
806 raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
808 _AddIpAddress("%s/%s/%s" % (link, nic.ip, nic.network),
809 "instance:%s/nic:%d" % (instance.name, idx))
811 for ip, owners in ips.items():
813 result.append("IP address %s is used by multiple owners: %s" %
814 (ip, utils.CommaJoin(owners)))
818 @locking.ssynchronized(_config_lock, shared=1)
819 def VerifyConfig(self):
822 This is just a wrapper over L{_UnlockedVerifyConfig}.
825 @return: a list of error messages; a non-empty list signifies
829 return self._UnlockedVerifyConfig()
831 def _UnlockedSetDiskID(self, disk, node_name):
832 """Convert the unique ID to the ID needed on the target nodes.
834 This is used only for drbd, which needs ip/port configuration.
836 The routine descends down and updates its children also, because
837 this helps when the only the top device is passed to the remote
840 This function is for internal use, when the config lock is already held.
844 for child in disk.children:
845 self._UnlockedSetDiskID(child, node_name)
847 if disk.logical_id is None and disk.physical_id is not None:
849 if disk.dev_type == constants.LD_DRBD8:
850 pnode, snode, port, pminor, sminor, secret = disk.logical_id
851 if node_name not in (pnode, snode):
852 raise errors.ConfigurationError("DRBD device not knowing node %s" %
854 pnode_info = self._UnlockedGetNodeInfo(pnode)
855 snode_info = self._UnlockedGetNodeInfo(snode)
856 if pnode_info is None or snode_info is None:
857 raise errors.ConfigurationError("Can't find primary or secondary node"
858 " for %s" % str(disk))
859 p_data = (pnode_info.secondary_ip, port)
860 s_data = (snode_info.secondary_ip, port)
861 if pnode == node_name:
862 disk.physical_id = p_data + s_data + (pminor, secret)
863 else: # it must be secondary, we tested above
864 disk.physical_id = s_data + p_data + (sminor, secret)
866 disk.physical_id = disk.logical_id
869 @locking.ssynchronized(_config_lock)
870 def SetDiskID(self, disk, node_name):
871 """Convert the unique ID to the ID needed on the target nodes.
873 This is used only for drbd, which needs ip/port configuration.
875 The routine descends down and updates its children also, because
876 this helps when the only the top device is passed to the remote
880 return self._UnlockedSetDiskID(disk, node_name)
882 @locking.ssynchronized(_config_lock)
883 def AddTcpUdpPort(self, port):
884 """Adds a new port to the available port pool.
886 @warning: this method does not "flush" the configuration (via
887 L{_WriteConfig}); callers should do that themselves once the
888 configuration is stable
891 if not isinstance(port, int):
892 raise errors.ProgrammerError("Invalid type passed for port")
894 self._config_data.cluster.tcpudp_port_pool.add(port)
896 @locking.ssynchronized(_config_lock, shared=1)
897 def GetPortList(self):
898 """Returns a copy of the current port list.
901 return self._config_data.cluster.tcpudp_port_pool.copy()
903 @locking.ssynchronized(_config_lock)
904 def AllocatePort(self):
907 The port will be taken from the available port pool or from the
908 default port range (and in this case we increase
912 # If there are TCP/IP ports configured, we use them first.
913 if self._config_data.cluster.tcpudp_port_pool:
914 port = self._config_data.cluster.tcpudp_port_pool.pop()
916 port = self._config_data.cluster.highest_used_port + 1
917 if port >= constants.LAST_DRBD_PORT:
918 raise errors.ConfigurationError("The highest used port is greater"
919 " than %s. Aborting." %
920 constants.LAST_DRBD_PORT)
921 self._config_data.cluster.highest_used_port = port
926 def _UnlockedComputeDRBDMap(self):
927 """Compute the used DRBD minor/nodes.
930 @return: dictionary of node_name: dict of minor: instance_name;
931 the returned dict will have all the nodes in it (even if with
932 an empty list), and a list of duplicates; if the duplicates
933 list is not empty, the configuration is corrupted and its caller
934 should raise an exception
937 def _AppendUsedPorts(instance_name, disk, used):
939 if disk.dev_type == constants.LD_DRBD8 and len(disk.logical_id) >= 5:
940 node_a, node_b, _, minor_a, minor_b = disk.logical_id[:5]
941 for node, port in ((node_a, minor_a), (node_b, minor_b)):
942 assert node in used, ("Node '%s' of instance '%s' not found"
943 " in node list" % (node, instance_name))
944 if port in used[node]:
945 duplicates.append((node, port, instance_name, used[node][port]))
947 used[node][port] = instance_name
949 for child in disk.children:
950 duplicates.extend(_AppendUsedPorts(instance_name, child, used))
954 my_dict = dict((node, {}) for node in self._config_data.nodes)
955 for instance in self._config_data.instances.itervalues():
956 for disk in instance.disks:
957 duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict))
958 for (node, minor), instance in self._temporary_drbds.iteritems():
959 if minor in my_dict[node] and my_dict[node][minor] != instance:
960 duplicates.append((node, minor, instance, my_dict[node][minor]))
962 my_dict[node][minor] = instance
963 return my_dict, duplicates
965 @locking.ssynchronized(_config_lock)
966 def ComputeDRBDMap(self):
967 """Compute the used DRBD minor/nodes.
969 This is just a wrapper over L{_UnlockedComputeDRBDMap}.
971 @return: dictionary of node_name: dict of minor: instance_name;
972 the returned dict will have all the nodes in it (even if with
976 d_map, duplicates = self._UnlockedComputeDRBDMap()
978 raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
982 @locking.ssynchronized(_config_lock)
983 def AllocateDRBDMinor(self, nodes, instance):
984 """Allocate a drbd minor.
986 The free minor will be automatically computed from the existing
987 devices. A node can be given multiple times in order to allocate
988 multiple minors. The result is the list of minors, in the same
989 order as the passed nodes.
991 @type instance: string
992 @param instance: the instance for which we allocate minors
995 assert isinstance(instance, basestring), \
996 "Invalid argument '%s' passed to AllocateDRBDMinor" % instance
998 d_map, duplicates = self._UnlockedComputeDRBDMap()
1000 raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
1004 ndata = d_map[nname]
1006 # no minors used, we can start at 0
1009 self._temporary_drbds[(nname, 0)] = instance
1013 ffree = utils.FirstFree(keys)
1015 # return the next minor
1016 # TODO: implement high-limit check
1017 minor = keys[-1] + 1
1020 # double-check minor against current instances
1021 assert minor not in d_map[nname], \
1022 ("Attempt to reuse allocated DRBD minor %d on node %s,"
1023 " already allocated to instance %s" %
1024 (minor, nname, d_map[nname][minor]))
1025 ndata[minor] = instance
1026 # double-check minor against reservation
1027 r_key = (nname, minor)
1028 assert r_key not in self._temporary_drbds, \
1029 ("Attempt to reuse reserved DRBD minor %d on node %s,"
1030 " reserved for instance %s" %
1031 (minor, nname, self._temporary_drbds[r_key]))
1032 self._temporary_drbds[r_key] = instance
1033 result.append(minor)
1034 logging.debug("Request to allocate drbd minors, input: %s, returning %s",
1038 def _UnlockedReleaseDRBDMinors(self, instance):
1039 """Release temporary drbd minors allocated for a given instance.
1041 @type instance: string
1042 @param instance: the instance for which temporary minors should be
1046 assert isinstance(instance, basestring), \
1047 "Invalid argument passed to ReleaseDRBDMinors"
1048 for key, name in self._temporary_drbds.items():
1049 if name == instance:
1050 del self._temporary_drbds[key]
1052 @locking.ssynchronized(_config_lock)
1053 def ReleaseDRBDMinors(self, instance):
1054 """Release temporary drbd minors allocated for a given instance.
1056 This should be called on the error paths, on the success paths
1057 it's automatically called by the ConfigWriter add and update
1060 This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
1062 @type instance: string
1063 @param instance: the instance for which temporary minors should be
1067 self._UnlockedReleaseDRBDMinors(instance)
1069 @locking.ssynchronized(_config_lock, shared=1)
1070 def GetConfigVersion(self):
1071 """Get the configuration version.
1073 @return: Config version
1076 return self._config_data.version
1078 @locking.ssynchronized(_config_lock, shared=1)
1079 def GetClusterName(self):
1080 """Get cluster name.
1082 @return: Cluster name
1085 return self._config_data.cluster.cluster_name
1087 @locking.ssynchronized(_config_lock, shared=1)
1088 def GetMasterNode(self):
1089 """Get the hostname of the master node for this cluster.
1091 @return: Master hostname
1094 return self._config_data.cluster.master_node
1096 @locking.ssynchronized(_config_lock, shared=1)
1097 def GetMasterIP(self):
1098 """Get the IP of the master node for this cluster.
1103 return self._config_data.cluster.master_ip
1105 @locking.ssynchronized(_config_lock, shared=1)
1106 def GetMasterNetdev(self):
1107 """Get the master network device for this cluster.
1110 return self._config_data.cluster.master_netdev
1112 @locking.ssynchronized(_config_lock, shared=1)
1113 def GetMasterNetmask(self):
1114 """Get the netmask of the master node for this cluster.
1117 return self._config_data.cluster.master_netmask
1119 @locking.ssynchronized(_config_lock, shared=1)
1120 def GetUseExternalMipScript(self):
1121 """Get flag representing whether to use the external master IP setup script.
1124 return self._config_data.cluster.use_external_mip_script
1126 @locking.ssynchronized(_config_lock, shared=1)
1127 def GetFileStorageDir(self):
1128 """Get the file storage dir for this cluster.
1131 return self._config_data.cluster.file_storage_dir
1133 @locking.ssynchronized(_config_lock, shared=1)
1134 def GetSharedFileStorageDir(self):
1135 """Get the shared file storage dir for this cluster.
1138 return self._config_data.cluster.shared_file_storage_dir
1140 @locking.ssynchronized(_config_lock, shared=1)
1141 def GetHypervisorType(self):
1142 """Get the hypervisor type for this cluster.
1145 return self._config_data.cluster.enabled_hypervisors[0]
1147 @locking.ssynchronized(_config_lock, shared=1)
1148 def GetHostKey(self):
1149 """Return the rsa hostkey from the config.
1152 @return: the rsa hostkey
1155 return self._config_data.cluster.rsahostkeypub
1157 @locking.ssynchronized(_config_lock, shared=1)
1158 def GetDefaultIAllocator(self):
1159 """Get the default instance allocator for this cluster.
1162 return self._config_data.cluster.default_iallocator
1164 @locking.ssynchronized(_config_lock, shared=1)
1165 def GetPrimaryIPFamily(self):
1166 """Get cluster primary ip family.
1168 @return: primary ip family
1171 return self._config_data.cluster.primary_ip_family
1173 @locking.ssynchronized(_config_lock, shared=1)
1174 def GetMasterNetworkParameters(self):
1175 """Get network parameters of the master node.
1177 @rtype: L{object.MasterNetworkParameters}
1178 @return: network parameters of the master node
1181 cluster = self._config_data.cluster
1182 result = objects.MasterNetworkParameters(
1183 name=cluster.master_node, ip=cluster.master_ip,
1184 netmask=cluster.master_netmask, netdev=cluster.master_netdev,
1185 ip_family=cluster.primary_ip_family)
1189 @locking.ssynchronized(_config_lock)
1190 def AddNodeGroup(self, group, ec_id, check_uuid=True):
1191 """Add a node group to the configuration.
1193 This method calls group.UpgradeConfig() to fill any missing attributes
1194 according to their default values.
1196 @type group: L{objects.NodeGroup}
1197 @param group: the NodeGroup object to add
1199 @param ec_id: unique id for the job to use when creating a missing UUID
1200 @type check_uuid: bool
1201 @param check_uuid: add an UUID to the group if it doesn't have one or, if
1202 it does, ensure that it does not exist in the
1203 configuration already
1206 self._UnlockedAddNodeGroup(group, ec_id, check_uuid)
1209 def _UnlockedAddNodeGroup(self, group, ec_id, check_uuid):
1210 """Add a node group to the configuration.
1213 logging.info("Adding node group %s to configuration", group.name)
1215 # Some code might need to add a node group with a pre-populated UUID
1216 # generated with ConfigWriter.GenerateUniqueID(). We allow them to bypass
1217 # the "does this UUID" exist already check.
1219 self._EnsureUUID(group, ec_id)
1222 existing_uuid = self._UnlockedLookupNodeGroup(group.name)
1223 except errors.OpPrereqError:
1226 raise errors.OpPrereqError("Desired group name '%s' already exists as a"
1227 " node group (UUID: %s)" %
1228 (group.name, existing_uuid),
1229 errors.ECODE_EXISTS)
1232 group.ctime = group.mtime = time.time()
1233 group.UpgradeConfig()
1235 self._config_data.nodegroups[group.uuid] = group
1236 self._config_data.cluster.serial_no += 1
1238 @locking.ssynchronized(_config_lock)
1239 def RemoveNodeGroup(self, group_uuid):
1240 """Remove a node group from the configuration.
1242 @type group_uuid: string
1243 @param group_uuid: the UUID of the node group to remove
1246 logging.info("Removing node group %s from configuration", group_uuid)
1248 if group_uuid not in self._config_data.nodegroups:
1249 raise errors.ConfigurationError("Unknown node group '%s'" % group_uuid)
1251 assert len(self._config_data.nodegroups) != 1, \
1252 "Group '%s' is the only group, cannot be removed" % group_uuid
1254 del self._config_data.nodegroups[group_uuid]
1255 self._config_data.cluster.serial_no += 1
1258 def _UnlockedLookupNodeGroup(self, target):
1259 """Lookup a node group's UUID.
1261 @type target: string or None
1262 @param target: group name or UUID or None to look for the default
1264 @return: nodegroup UUID
1265 @raises errors.OpPrereqError: when the target group cannot be found
1269 if len(self._config_data.nodegroups) != 1:
1270 raise errors.OpPrereqError("More than one node group exists. Target"
1271 " group must be specified explicitly.")
1273 return self._config_data.nodegroups.keys()[0]
1274 if target in self._config_data.nodegroups:
1276 for nodegroup in self._config_data.nodegroups.values():
1277 if nodegroup.name == target:
1278 return nodegroup.uuid
1279 raise errors.OpPrereqError("Node group '%s' not found" % target,
1282 @locking.ssynchronized(_config_lock, shared=1)
1283 def LookupNodeGroup(self, target):
1284 """Lookup a node group's UUID.
1286 This function is just a wrapper over L{_UnlockedLookupNodeGroup}.
1288 @type target: string or None
1289 @param target: group name or UUID or None to look for the default
1291 @return: nodegroup UUID
1294 return self._UnlockedLookupNodeGroup(target)
1296 def _UnlockedGetNodeGroup(self, uuid):
1297 """Lookup a node group.
1300 @param uuid: group UUID
1301 @rtype: L{objects.NodeGroup} or None
1302 @return: nodegroup object, or None if not found
1305 if uuid not in self._config_data.nodegroups:
1308 return self._config_data.nodegroups[uuid]
1310 @locking.ssynchronized(_config_lock, shared=1)
1311 def GetNodeGroup(self, uuid):
1312 """Lookup a node group.
1315 @param uuid: group UUID
1316 @rtype: L{objects.NodeGroup} or None
1317 @return: nodegroup object, or None if not found
1320 return self._UnlockedGetNodeGroup(uuid)
1322 @locking.ssynchronized(_config_lock, shared=1)
1323 def GetAllNodeGroupsInfo(self):
1324 """Get the configuration of all node groups.
1327 return dict(self._config_data.nodegroups)
1329 @locking.ssynchronized(_config_lock, shared=1)
1330 def GetNodeGroupList(self):
1331 """Get a list of node groups.
1334 return self._config_data.nodegroups.keys()
1336 @locking.ssynchronized(_config_lock, shared=1)
1337 def GetNodeGroupMembersByNodes(self, nodes):
1338 """Get nodes which are member in the same nodegroups as the given nodes.
1341 ngfn = lambda node_name: self._UnlockedGetNodeInfo(node_name).group
1342 return frozenset(member_name
1343 for node_name in nodes
1345 self._UnlockedGetNodeGroup(ngfn(node_name)).members)
1347 @locking.ssynchronized(_config_lock, shared=1)
1348 def GetMultiNodeGroupInfo(self, group_uuids):
1349 """Get the configuration of multiple node groups.
1351 @param group_uuids: List of node group UUIDs
1353 @return: List of tuples of (group_uuid, group_info)
1356 return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]
1358 @locking.ssynchronized(_config_lock)
1359 def AddInstance(self, instance, ec_id):
1360 """Add an instance to the config.
1362 This should be used after creating a new instance.
1364 @type instance: L{objects.Instance}
1365 @param instance: the instance object
1368 if not isinstance(instance, objects.Instance):
1369 raise errors.ProgrammerError("Invalid type passed to AddInstance")
1371 if instance.disk_template != constants.DT_DISKLESS:
1372 all_lvs = instance.MapLVsByNode()
1373 logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
1375 all_macs = self._AllMACs()
1376 for nic in instance.nics:
1377 if nic.mac in all_macs:
1378 raise errors.ConfigurationError("Cannot add instance %s:"
1379 " MAC address '%s' already in use." %
1380 (instance.name, nic.mac))
1382 self._EnsureUUID(instance, ec_id)
1384 instance.serial_no = 1
1385 instance.ctime = instance.mtime = time.time()
1386 self._config_data.instances[instance.name] = instance
1387 self._config_data.cluster.serial_no += 1
1388 self._UnlockedReleaseDRBDMinors(instance.name)
1389 self._UnlockedCommitTemporaryIps(ec_id)
1392 def _EnsureUUID(self, item, ec_id):
1393 """Ensures a given object has a valid UUID.
1395 @param item: the instance or node to be checked
1396 @param ec_id: the execution context id for the uuid reservation
1400 item.uuid = self._GenerateUniqueID(ec_id)
1401 elif item.uuid in self._AllIDs(include_temporary=True):
1402 raise errors.ConfigurationError("Cannot add '%s': UUID %s already"
1403 " in use" % (item.name, item.uuid))
1405 def _SetInstanceStatus(self, instance_name, status):
1406 """Set the instance's status to a given value.
1409 assert status in constants.ADMINST_ALL, \
1410 "Invalid status '%s' passed to SetInstanceStatus" % (status,)
1412 if instance_name not in self._config_data.instances:
1413 raise errors.ConfigurationError("Unknown instance '%s'" %
1415 instance = self._config_data.instances[instance_name]
1416 if instance.admin_state != status:
1417 instance.admin_state = status
1418 instance.serial_no += 1
1419 instance.mtime = time.time()
1422 @locking.ssynchronized(_config_lock)
1423 def MarkInstanceUp(self, instance_name):
1424 """Mark the instance status to up in the config.
1427 self._SetInstanceStatus(instance_name, constants.ADMINST_UP)
1429 @locking.ssynchronized(_config_lock)
1430 def MarkInstanceOffline(self, instance_name):
1431 """Mark the instance status to down in the config.
1434 self._SetInstanceStatus(instance_name, constants.ADMINST_OFFLINE)
1436 @locking.ssynchronized(_config_lock)
1437 def RemoveInstance(self, instance_name):
1438 """Remove the instance from the configuration.
1441 if instance_name not in self._config_data.instances:
1442 raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1444 # If a network port has been allocated to the instance,
1445 # return it to the pool of free ports.
1446 inst = self._config_data.instances[instance_name]
1447 network_port = getattr(inst, "network_port", None)
1448 if network_port is not None:
1449 self._config_data.cluster.tcpudp_port_pool.add(network_port)
1451 instance = self._UnlockedGetInstanceInfo(instance_name)
1453 for nic in instance.nics:
1454 if nic.network is not None and nic.ip is not None:
1455 net_uuid = self._UnlockedLookupNetwork(nic.network)
1457 # Return all IP addresses to the respective address pools
1458 self._UnlockedCommitIp(constants.RELEASE_ACTION, net_uuid, nic.ip)
1460 del self._config_data.instances[instance_name]
1461 self._config_data.cluster.serial_no += 1
1464 @locking.ssynchronized(_config_lock)
1465 def RenameInstance(self, old_name, new_name):
1466 """Rename an instance.
1468 This needs to be done in ConfigWriter and not by RemoveInstance
1469 combined with AddInstance as only we can guarantee an atomic
1473 if old_name not in self._config_data.instances:
1474 raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
1476 # Operate on a copy to not loose instance object in case of a failure
1477 inst = self._config_data.instances[old_name].Copy()
1478 inst.name = new_name
1480 for (idx, disk) in enumerate(inst.disks):
1481 if disk.dev_type == constants.LD_FILE:
1482 # rename the file paths in logical and physical id
1483 file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
1484 disk.logical_id = (disk.logical_id[0],
1485 utils.PathJoin(file_storage_dir, inst.name,
1487 disk.physical_id = disk.logical_id
1489 # Actually replace instance object
1490 del self._config_data.instances[old_name]
1491 self._config_data.instances[inst.name] = inst
1493 # Force update of ssconf files
1494 self._config_data.cluster.serial_no += 1
1498 @locking.ssynchronized(_config_lock)
1499 def MarkInstanceDown(self, instance_name):
1500 """Mark the status of an instance to down in the configuration.
1503 self._SetInstanceStatus(instance_name, constants.ADMINST_DOWN)
1505 def _UnlockedGetInstanceList(self):
1506 """Get the list of instances.
1508 This function is for internal use, when the config lock is already held.
1511 return self._config_data.instances.keys()
1513 @locking.ssynchronized(_config_lock, shared=1)
1514 def GetInstanceList(self):
1515 """Get the list of instances.
1517 @return: array of instances, ex. ['instance2.example.com',
1518 'instance1.example.com']
1521 return self._UnlockedGetInstanceList()
1523 def ExpandInstanceName(self, short_name):
1524 """Attempt to expand an incomplete instance name.
1527 # Locking is done in L{ConfigWriter.GetInstanceList}
1528 return _MatchNameComponentIgnoreCase(short_name, self.GetInstanceList())
1530 def _UnlockedGetInstanceInfo(self, instance_name):
1531 """Returns information about an instance.
1533 This function is for internal use, when the config lock is already held.
1536 if instance_name not in self._config_data.instances:
1539 return self._config_data.instances[instance_name]
1541 @locking.ssynchronized(_config_lock, shared=1)
1542 def GetInstanceInfo(self, instance_name):
1543 """Returns information about an instance.
1545 It takes the information from the configuration file. Other information of
1546 an instance are taken from the live systems.
1548 @param instance_name: name of the instance, e.g.
1549 I{instance1.example.com}
1551 @rtype: L{objects.Instance}
1552 @return: the instance object
1555 return self._UnlockedGetInstanceInfo(instance_name)
1557 @locking.ssynchronized(_config_lock, shared=1)
1558 def GetInstanceNodeGroups(self, instance_name, primary_only=False):
1559 """Returns set of node group UUIDs for instance's nodes.
1564 instance = self._UnlockedGetInstanceInfo(instance_name)
1566 raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1569 nodes = [instance.primary_node]
1571 nodes = instance.all_nodes
1573 return frozenset(self._UnlockedGetNodeInfo(node_name).group
1574 for node_name in nodes)
1576 @locking.ssynchronized(_config_lock, shared=1)
1577 def GetMultiInstanceInfo(self, instances):
1578 """Get the configuration of multiple instances.
1580 @param instances: list of instance names
1582 @return: list of tuples (instance, instance_info), where
1583 instance_info is what would GetInstanceInfo return for the
1584 node, while keeping the original order
1587 return [(name, self._UnlockedGetInstanceInfo(name)) for name in instances]
1589 @locking.ssynchronized(_config_lock, shared=1)
1590 def GetAllInstancesInfo(self):
1591 """Get the configuration of all instances.
1594 @return: dict of (instance, instance_info), where instance_info is what
1595 would GetInstanceInfo return for the node
1598 my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance))
1599 for instance in self._UnlockedGetInstanceList()])
1602 @locking.ssynchronized(_config_lock, shared=1)
1603 def GetInstancesInfoByFilter(self, filter_fn):
1604 """Get instance configuration with a filter.
1606 @type filter_fn: callable
1607 @param filter_fn: Filter function receiving instance object as parameter,
1608 returning boolean. Important: this function is called while the
1609 configuration locks is held. It must not do any complex work or call
1610 functions potentially leading to a deadlock. Ideally it doesn't call any
1611 other functions and just compares instance attributes.
1614 return dict((name, inst)
1615 for (name, inst) in self._config_data.instances.items()
1618 @locking.ssynchronized(_config_lock)
1619 def AddNode(self, node, ec_id):
1620 """Add a node to the configuration.
1622 @type node: L{objects.Node}
1623 @param node: a Node instance
1626 logging.info("Adding node %s to configuration", node.name)
1628 self._EnsureUUID(node, ec_id)
1631 node.ctime = node.mtime = time.time()
1632 self._UnlockedAddNodeToGroup(node.name, node.group)
1633 self._config_data.nodes[node.name] = node
1634 self._config_data.cluster.serial_no += 1
1637 @locking.ssynchronized(_config_lock)
1638 def RemoveNode(self, node_name):
1639 """Remove a node from the configuration.
1642 logging.info("Removing node %s from configuration", node_name)
1644 if node_name not in self._config_data.nodes:
1645 raise errors.ConfigurationError("Unknown node '%s'" % node_name)
1647 self._UnlockedRemoveNodeFromGroup(self._config_data.nodes[node_name])
1648 del self._config_data.nodes[node_name]
1649 self._config_data.cluster.serial_no += 1
1652 def ExpandNodeName(self, short_name):
1653 """Attempt to expand an incomplete node name.
1656 # Locking is done in L{ConfigWriter.GetNodeList}
1657 return _MatchNameComponentIgnoreCase(short_name, self.GetNodeList())
1659 def _UnlockedGetNodeInfo(self, node_name):
1660 """Get the configuration of a node, as stored in the config.
1662 This function is for internal use, when the config lock is already
1665 @param node_name: the node name, e.g. I{node1.example.com}
1667 @rtype: L{objects.Node}
1668 @return: the node object
1671 if node_name not in self._config_data.nodes:
1674 return self._config_data.nodes[node_name]
1676 @locking.ssynchronized(_config_lock, shared=1)
1677 def GetNodeInfo(self, node_name):
1678 """Get the configuration of a node, as stored in the config.
1680 This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
1682 @param node_name: the node name, e.g. I{node1.example.com}
1684 @rtype: L{objects.Node}
1685 @return: the node object
1688 return self._UnlockedGetNodeInfo(node_name)
1690 @locking.ssynchronized(_config_lock, shared=1)
1691 def GetNodeInstances(self, node_name):
1692 """Get the instances of a node, as stored in the config.
1694 @param node_name: the node name, e.g. I{node1.example.com}
1696 @rtype: (list, list)
1697 @return: a tuple with two lists: the primary and the secondary instances
1702 for inst in self._config_data.instances.values():
1703 if inst.primary_node == node_name:
1704 pri.append(inst.name)
1705 if node_name in inst.secondary_nodes:
1706 sec.append(inst.name)
1709 @locking.ssynchronized(_config_lock, shared=1)
1710 def GetNodeGroupInstances(self, uuid, primary_only=False):
1711 """Get the instances of a node group.
1713 @param uuid: Node group UUID
1714 @param primary_only: Whether to only consider primary nodes
1716 @return: List of instance names in node group
1720 nodes_fn = lambda inst: [inst.primary_node]
1722 nodes_fn = lambda inst: inst.all_nodes
1724 return frozenset(inst.name
1725 for inst in self._config_data.instances.values()
1726 for node_name in nodes_fn(inst)
1727 if self._UnlockedGetNodeInfo(node_name).group == uuid)
1729 def _UnlockedGetNodeList(self):
1730 """Return the list of nodes which are in the configuration.
1732 This function is for internal use, when the config lock is already
1738 return self._config_data.nodes.keys()
1740 @locking.ssynchronized(_config_lock, shared=1)
1741 def GetNodeList(self):
1742 """Return the list of nodes which are in the configuration.
1745 return self._UnlockedGetNodeList()
1747 def _UnlockedGetOnlineNodeList(self):
1748 """Return the list of nodes which are online.
1751 all_nodes = [self._UnlockedGetNodeInfo(node)
1752 for node in self._UnlockedGetNodeList()]
1753 return [node.name for node in all_nodes if not node.offline]
1755 @locking.ssynchronized(_config_lock, shared=1)
1756 def GetOnlineNodeList(self):
1757 """Return the list of nodes which are online.
1760 return self._UnlockedGetOnlineNodeList()
1762 @locking.ssynchronized(_config_lock, shared=1)
1763 def GetVmCapableNodeList(self):
1764 """Return the list of nodes which are not vm capable.
1767 all_nodes = [self._UnlockedGetNodeInfo(node)
1768 for node in self._UnlockedGetNodeList()]
1769 return [node.name for node in all_nodes if node.vm_capable]
1771 @locking.ssynchronized(_config_lock, shared=1)
1772 def GetNonVmCapableNodeList(self):
1773 """Return the list of nodes which are not vm capable.
1776 all_nodes = [self._UnlockedGetNodeInfo(node)
1777 for node in self._UnlockedGetNodeList()]
1778 return [node.name for node in all_nodes if not node.vm_capable]
1780 @locking.ssynchronized(_config_lock, shared=1)
1781 def GetMultiNodeInfo(self, nodes):
1782 """Get the configuration of multiple nodes.
1784 @param nodes: list of node names
1786 @return: list of tuples of (node, node_info), where node_info is
1787 what would GetNodeInfo return for the node, in the original
1791 return [(name, self._UnlockedGetNodeInfo(name)) for name in nodes]
1793 @locking.ssynchronized(_config_lock, shared=1)
1794 def GetAllNodesInfo(self):
1795 """Get the configuration of all nodes.
1798 @return: dict of (node, node_info), where node_info is what
1799 would GetNodeInfo return for the node
1802 return self._UnlockedGetAllNodesInfo()
1804 def _UnlockedGetAllNodesInfo(self):
1805 """Gets configuration of all nodes.
1807 @note: See L{GetAllNodesInfo}
1810 return dict([(node, self._UnlockedGetNodeInfo(node))
1811 for node in self._UnlockedGetNodeList()])
1813 @locking.ssynchronized(_config_lock, shared=1)
1814 def GetNodeGroupsFromNodes(self, nodes):
1815 """Returns groups for a list of nodes.
1817 @type nodes: list of string
1818 @param nodes: List of node names
1822 return frozenset(self._UnlockedGetNodeInfo(name).group for name in nodes)
1824 def _UnlockedGetMasterCandidateStats(self, exceptions=None):
1825 """Get the number of current and maximum desired and possible candidates.
1827 @type exceptions: list
1828 @param exceptions: if passed, list of nodes that should be ignored
1830 @return: tuple of (current, desired and possible, possible)
1833 mc_now = mc_should = mc_max = 0
1834 for node in self._config_data.nodes.values():
1835 if exceptions and node.name in exceptions:
1837 if not (node.offline or node.drained) and node.master_capable:
1839 if node.master_candidate:
1841 mc_should = min(mc_max, self._config_data.cluster.candidate_pool_size)
1842 return (mc_now, mc_should, mc_max)
1844 @locking.ssynchronized(_config_lock, shared=1)
1845 def GetMasterCandidateStats(self, exceptions=None):
1846 """Get the number of current and maximum possible candidates.
1848 This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
1850 @type exceptions: list
1851 @param exceptions: if passed, list of nodes that should be ignored
1853 @return: tuple of (current, max)
1856 return self._UnlockedGetMasterCandidateStats(exceptions)
1858 @locking.ssynchronized(_config_lock)
1859 def MaintainCandidatePool(self, exceptions):
1860 """Try to grow the candidate pool to the desired size.
1862 @type exceptions: list
1863 @param exceptions: if passed, list of nodes that should be ignored
1865 @return: list with the adjusted nodes (L{objects.Node} instances)
1868 mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats(exceptions)
1871 node_list = self._config_data.nodes.keys()
1872 random.shuffle(node_list)
1873 for name in node_list:
1874 if mc_now >= mc_max:
1876 node = self._config_data.nodes[name]
1877 if (node.master_candidate or node.offline or node.drained or
1878 node.name in exceptions or not node.master_capable):
1880 mod_list.append(node)
1881 node.master_candidate = True
1884 if mc_now != mc_max:
1885 # this should not happen
1886 logging.warning("Warning: MaintainCandidatePool didn't manage to"
1887 " fill the candidate pool (%d/%d)", mc_now, mc_max)
1889 self._config_data.cluster.serial_no += 1
1894 def _UnlockedAddNodeToGroup(self, node_name, nodegroup_uuid):
1895 """Add a given node to the specified group.
1898 if nodegroup_uuid not in self._config_data.nodegroups:
1899 # This can happen if a node group gets deleted between its lookup and
1900 # when we're adding the first node to it, since we don't keep a lock in
1901 # the meantime. It's ok though, as we'll fail cleanly if the node group
1902 # is not found anymore.
1903 raise errors.OpExecError("Unknown node group: %s" % nodegroup_uuid)
1904 if node_name not in self._config_data.nodegroups[nodegroup_uuid].members:
1905 self._config_data.nodegroups[nodegroup_uuid].members.append(node_name)
1907 def _UnlockedRemoveNodeFromGroup(self, node):
1908 """Remove a given node from its group.
1911 nodegroup = node.group
1912 if nodegroup not in self._config_data.nodegroups:
1913 logging.warning("Warning: node '%s' has unknown node group '%s'"
1914 " (while being removed from it)", node.name, nodegroup)
1915 nodegroup_obj = self._config_data.nodegroups[nodegroup]
1916 if node.name not in nodegroup_obj.members:
1917 logging.warning("Warning: node '%s' not a member of its node group '%s'"
1918 " (while being removed from it)", node.name, nodegroup)
1920 nodegroup_obj.members.remove(node.name)
1922 @locking.ssynchronized(_config_lock)
1923 def AssignGroupNodes(self, mods):
1924 """Changes the group of a number of nodes.
1926 @type mods: list of tuples; (node name, new group UUID)
1927 @param mods: Node membership modifications
1930 groups = self._config_data.nodegroups
1931 nodes = self._config_data.nodes
1935 # Try to resolve names/UUIDs first
1936 for (node_name, new_group_uuid) in mods:
1938 node = nodes[node_name]
1940 raise errors.ConfigurationError("Unable to find node '%s'" % node_name)
1942 if node.group == new_group_uuid:
1943 # Node is being assigned to its current group
1944 logging.debug("Node '%s' was assigned to its current group (%s)",
1945 node_name, node.group)
1948 # Try to find current group of node
1950 old_group = groups[node.group]
1952 raise errors.ConfigurationError("Unable to find old group '%s'" %
1955 # Try to find new group for node
1957 new_group = groups[new_group_uuid]
1959 raise errors.ConfigurationError("Unable to find new group '%s'" %
1962 assert node.name in old_group.members, \
1963 ("Inconsistent configuration: node '%s' not listed in members for its"
1964 " old group '%s'" % (node.name, old_group.uuid))
1965 assert node.name not in new_group.members, \
1966 ("Inconsistent configuration: node '%s' already listed in members for"
1967 " its new group '%s'" % (node.name, new_group.uuid))
1969 resmod.append((node, old_group, new_group))
1972 for (node, old_group, new_group) in resmod:
1973 assert node.uuid != new_group.uuid and old_group.uuid != new_group.uuid, \
1974 "Assigning to current group is not possible"
1976 node.group = new_group.uuid
1978 # Update members of involved groups
1979 if node.name in old_group.members:
1980 old_group.members.remove(node.name)
1981 if node.name not in new_group.members:
1982 new_group.members.append(node.name)
1984 # Update timestamps and serials (only once per node/group object)
1986 for obj in frozenset(itertools.chain(*resmod)): # pylint: disable=W0142
1990 # Force ssconf update
1991 self._config_data.cluster.serial_no += 1
1995 def _BumpSerialNo(self):
1996 """Bump up the serial number of the config.
1999 self._config_data.serial_no += 1
2000 self._config_data.mtime = time.time()
2002 def _AllUUIDObjects(self):
2003 """Returns all objects with uuid attributes.
2006 return (self._config_data.instances.values() +
2007 self._config_data.nodes.values() +
2008 self._config_data.nodegroups.values() +
2009 [self._config_data.cluster])
2011 def _OpenConfig(self, accept_foreign):
2012 """Read the config data from disk.
2015 raw_data = utils.ReadFile(self._cfg_file)
2018 data = objects.ConfigData.FromDict(serializer.Load(raw_data))
2019 except Exception, err:
2020 raise errors.ConfigurationError(err)
2022 # Make sure the configuration has the right version
2023 _ValidateConfig(data)
2025 if (not hasattr(data, "cluster") or
2026 not hasattr(data.cluster, "rsahostkeypub")):
2027 raise errors.ConfigurationError("Incomplete configuration"
2028 " (missing cluster.rsahostkeypub)")
2030 if data.cluster.master_node != self._my_hostname and not accept_foreign:
2031 msg = ("The configuration denotes node %s as master, while my"
2032 " hostname is %s; opening a foreign configuration is only"
2033 " possible in accept_foreign mode" %
2034 (data.cluster.master_node, self._my_hostname))
2035 raise errors.ConfigurationError(msg)
2037 # Upgrade configuration if needed
2038 data.UpgradeConfig()
2040 self._config_data = data
2041 # reset the last serial as -1 so that the next write will cause
2043 self._last_cluster_serial = -1
2045 # And finally run our (custom) config upgrade sequence
2046 self._UpgradeConfig()
2048 self._cfg_id = utils.GetFileID(path=self._cfg_file)
2050 def _UpgradeConfig(self):
2051 """Run upgrade steps that cannot be done purely in the objects.
2053 This is because some data elements need uniqueness across the
2054 whole configuration, etc.
2056 @warning: this function will call L{_WriteConfig()}, but also
2057 L{DropECReservations} so it needs to be called only from a
2058 "safe" place (the constructor). If one wanted to call it with
2059 the lock held, a DropECReservationUnlocked would need to be
2060 created first, to avoid causing deadlock.
2064 for item in self._AllUUIDObjects():
2065 if item.uuid is None:
2066 item.uuid = self._GenerateUniqueID(_UPGRADE_CONFIG_JID)
2068 if not self._config_data.nodegroups:
2069 default_nodegroup_name = constants.INITIAL_NODE_GROUP_NAME
2070 default_nodegroup = objects.NodeGroup(name=default_nodegroup_name,
2072 self._UnlockedAddNodeGroup(default_nodegroup, _UPGRADE_CONFIG_JID, True)
2074 for node in self._config_data.nodes.values():
2076 node.group = self.LookupNodeGroup(None)
2078 # This is technically *not* an upgrade, but needs to be done both when
2079 # nodegroups are being added, and upon normally loading the config,
2080 # because the members list of a node group is discarded upon
2081 # serializing/deserializing the object.
2082 self._UnlockedAddNodeToGroup(node.name, node.group)
2085 # This is ok even if it acquires the internal lock, as _UpgradeConfig is
2086 # only called at config init time, without the lock held
2087 self.DropECReservations(_UPGRADE_CONFIG_JID)
2089 def _DistributeConfig(self, feedback_fn):
2090 """Distribute the configuration to the other nodes.
2092 Currently, this only copies the configuration file. In the future,
2093 it could be used to encapsulate the 2/3-phase update mechanism.
2103 myhostname = self._my_hostname
2104 # we can skip checking whether _UnlockedGetNodeInfo returns None
2105 # since the node list comes from _UnlocketGetNodeList, and we are
2106 # called with the lock held, so no modifications should take place
2108 for node_name in self._UnlockedGetNodeList():
2109 if node_name == myhostname:
2111 node_info = self._UnlockedGetNodeInfo(node_name)
2112 if not node_info.master_candidate:
2114 node_list.append(node_info.name)
2115 addr_list.append(node_info.primary_ip)
2117 # TODO: Use dedicated resolver talking to config writer for name resolution
2119 self._GetRpc(addr_list).call_upload_file(node_list, self._cfg_file)
2120 for to_node, to_result in result.items():
2121 msg = to_result.fail_msg
2123 msg = ("Copy of file %s to node %s failed: %s" %
2124 (self._cfg_file, to_node, msg))
2134 def _WriteConfig(self, destination=None, feedback_fn=None):
2135 """Write the configuration data to persistent storage.
2138 assert feedback_fn is None or callable(feedback_fn)
2140 # Warn on config errors, but don't abort the save - the
2141 # configuration has already been modified, and we can't revert;
2142 # the best we can do is to warn the user and save as is, leaving
2143 # recovery to the user
2144 config_errors = self._UnlockedVerifyConfig()
2146 errmsg = ("Configuration data is not consistent: %s" %
2147 (utils.CommaJoin(config_errors)))
2148 logging.critical(errmsg)
2152 if destination is None:
2153 destination = self._cfg_file
2154 self._BumpSerialNo()
2155 txt = serializer.Dump(self._config_data.ToDict())
2157 getents = self._getents()
2159 fd = utils.SafeWriteFile(destination, self._cfg_id, data=txt,
2160 close=False, gid=getents.confd_gid, mode=0640)
2161 except errors.LockError:
2162 raise errors.ConfigurationError("The configuration file has been"
2163 " modified since the last write, cannot"
2166 self._cfg_id = utils.GetFileID(fd=fd)
2170 self.write_count += 1
2172 # and redistribute the config file to master candidates
2173 self._DistributeConfig(feedback_fn)
2175 # Write ssconf files on all nodes (including locally)
2176 if self._last_cluster_serial < self._config_data.cluster.serial_no:
2177 if not self._offline:
2178 result = self._GetRpc(None).call_write_ssconf_files(
2179 self._UnlockedGetOnlineNodeList(),
2180 self._UnlockedGetSsconfValues())
2182 for nname, nresu in result.items():
2183 msg = nresu.fail_msg
2185 errmsg = ("Error while uploading ssconf files to"
2186 " node %s: %s" % (nname, msg))
2187 logging.warning(errmsg)
2192 self._last_cluster_serial = self._config_data.cluster.serial_no
2194 def _UnlockedGetSsconfValues(self):
2195 """Return the values needed by ssconf.
2198 @return: a dictionary with keys the ssconf names and values their
2203 instance_names = utils.NiceSort(self._UnlockedGetInstanceList())
2204 node_names = utils.NiceSort(self._UnlockedGetNodeList())
2205 node_info = [self._UnlockedGetNodeInfo(name) for name in node_names]
2206 node_pri_ips = ["%s %s" % (ninfo.name, ninfo.primary_ip)
2207 for ninfo in node_info]
2208 node_snd_ips = ["%s %s" % (ninfo.name, ninfo.secondary_ip)
2209 for ninfo in node_info]
2211 instance_data = fn(instance_names)
2212 off_data = fn(node.name for node in node_info if node.offline)
2213 on_data = fn(node.name for node in node_info if not node.offline)
2214 mc_data = fn(node.name for node in node_info if node.master_candidate)
2215 mc_ips_data = fn(node.primary_ip for node in node_info
2216 if node.master_candidate)
2217 node_data = fn(node_names)
2218 node_pri_ips_data = fn(node_pri_ips)
2219 node_snd_ips_data = fn(node_snd_ips)
2221 cluster = self._config_data.cluster
2222 cluster_tags = fn(cluster.GetTags())
2224 hypervisor_list = fn(cluster.enabled_hypervisors)
2226 uid_pool = uidpool.FormatUidPool(cluster.uid_pool, separator="\n")
2228 nodegroups = ["%s %s" % (nodegroup.uuid, nodegroup.name) for nodegroup in
2229 self._config_data.nodegroups.values()]
2230 nodegroups_data = fn(utils.NiceSort(nodegroups))
2231 networks = ["%s %s" % (net.uuid, net.name) for net in
2232 self._config_data.networks.values()]
2233 networks_data = fn(utils.NiceSort(networks))
2236 constants.SS_CLUSTER_NAME: cluster.cluster_name,
2237 constants.SS_CLUSTER_TAGS: cluster_tags,
2238 constants.SS_FILE_STORAGE_DIR: cluster.file_storage_dir,
2239 constants.SS_SHARED_FILE_STORAGE_DIR: cluster.shared_file_storage_dir,
2240 constants.SS_MASTER_CANDIDATES: mc_data,
2241 constants.SS_MASTER_CANDIDATES_IPS: mc_ips_data,
2242 constants.SS_MASTER_IP: cluster.master_ip,
2243 constants.SS_MASTER_NETDEV: cluster.master_netdev,
2244 constants.SS_MASTER_NETMASK: str(cluster.master_netmask),
2245 constants.SS_MASTER_NODE: cluster.master_node,
2246 constants.SS_NODE_LIST: node_data,
2247 constants.SS_NODE_PRIMARY_IPS: node_pri_ips_data,
2248 constants.SS_NODE_SECONDARY_IPS: node_snd_ips_data,
2249 constants.SS_OFFLINE_NODES: off_data,
2250 constants.SS_ONLINE_NODES: on_data,
2251 constants.SS_PRIMARY_IP_FAMILY: str(cluster.primary_ip_family),
2252 constants.SS_INSTANCE_LIST: instance_data,
2253 constants.SS_RELEASE_VERSION: constants.RELEASE_VERSION,
2254 constants.SS_HYPERVISOR_LIST: hypervisor_list,
2255 constants.SS_MAINTAIN_NODE_HEALTH: str(cluster.maintain_node_health),
2256 constants.SS_UID_POOL: uid_pool,
2257 constants.SS_NODEGROUPS: nodegroups_data,
2258 constants.SS_NETWORKS: networks_data,
2260 bad_values = [(k, v) for k, v in ssconf_values.items()
2261 if not isinstance(v, (str, basestring))]
2263 err = utils.CommaJoin("%s=%s" % (k, v) for k, v in bad_values)
2264 raise errors.ConfigurationError("Some ssconf key(s) have non-string"
2265 " values: %s" % err)
2266 return ssconf_values
2268 @locking.ssynchronized(_config_lock, shared=1)
2269 def GetSsconfValues(self):
2270 """Wrapper using lock around _UnlockedGetSsconf().
2273 return self._UnlockedGetSsconfValues()
2275 @locking.ssynchronized(_config_lock, shared=1)
2276 def GetVGName(self):
2277 """Return the volume group name.
2280 return self._config_data.cluster.volume_group_name
2282 @locking.ssynchronized(_config_lock)
2283 def SetVGName(self, vg_name):
2284 """Set the volume group name.
2287 self._config_data.cluster.volume_group_name = vg_name
2288 self._config_data.cluster.serial_no += 1
2291 @locking.ssynchronized(_config_lock, shared=1)
2292 def GetDRBDHelper(self):
2293 """Return DRBD usermode helper.
2296 return self._config_data.cluster.drbd_usermode_helper
2298 @locking.ssynchronized(_config_lock)
2299 def SetDRBDHelper(self, drbd_helper):
2300 """Set DRBD usermode helper.
2303 self._config_data.cluster.drbd_usermode_helper = drbd_helper
2304 self._config_data.cluster.serial_no += 1
2307 @locking.ssynchronized(_config_lock, shared=1)
2308 def GetMACPrefix(self):
2309 """Return the mac prefix.
2312 return self._config_data.cluster.mac_prefix
2314 @locking.ssynchronized(_config_lock, shared=1)
2315 def GetClusterInfo(self):
2316 """Returns information about the cluster
2318 @rtype: L{objects.Cluster}
2319 @return: the cluster object
2322 return self._config_data.cluster
2324 @locking.ssynchronized(_config_lock, shared=1)
2325 def HasAnyDiskOfType(self, dev_type):
2326 """Check if in there is at disk of the given type in the configuration.
2329 return self._config_data.HasAnyDiskOfType(dev_type)
2331 @locking.ssynchronized(_config_lock)
2332 def Update(self, target, feedback_fn, ec_id=None):
2333 """Notify function to be called after updates.
2335 This function must be called when an object (as returned by
2336 GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
2337 caller wants the modifications saved to the backing store. Note
2338 that all modified objects will be saved, but the target argument
2339 is the one the caller wants to ensure that it's saved.
2341 @param target: an instance of either L{objects.Cluster},
2342 L{objects.Node} or L{objects.Instance} which is existing in
2344 @param feedback_fn: Callable feedback function
2347 if self._config_data is None:
2348 raise errors.ProgrammerError("Configuration file not read,"
2350 update_serial = False
2351 if isinstance(target, objects.Cluster):
2352 test = target == self._config_data.cluster
2353 elif isinstance(target, objects.Node):
2354 test = target in self._config_data.nodes.values()
2355 update_serial = True
2356 elif isinstance(target, objects.Instance):
2357 test = target in self._config_data.instances.values()
2358 elif isinstance(target, objects.NodeGroup):
2359 test = target in self._config_data.nodegroups.values()
2360 elif isinstance(target, objects.Network):
2361 test = target in self._config_data.networks.values()
2363 raise errors.ProgrammerError("Invalid object type (%s) passed to"
2364 " ConfigWriter.Update" % type(target))
2366 raise errors.ConfigurationError("Configuration updated since object"
2367 " has been read or unknown object")
2368 target.serial_no += 1
2369 target.mtime = now = time.time()
2372 # for node updates, we need to increase the cluster serial too
2373 self._config_data.cluster.serial_no += 1
2374 self._config_data.cluster.mtime = now
2376 if isinstance(target, objects.Instance):
2377 self._UnlockedReleaseDRBDMinors(target.name)
2379 if ec_id is not None:
2380 # Commit all ips reserved by OpInstanceSetParams and OpGroupSetParams
2381 self._UnlockedCommitTemporaryIps(ec_id)
2383 self._WriteConfig(feedback_fn=feedback_fn)
2385 @locking.ssynchronized(_config_lock)
2386 def DropECReservations(self, ec_id):
2387 """Drop per-execution-context reservations
2390 for rm in self._all_rms:
2391 rm.DropECReservations(ec_id)
2393 @locking.ssynchronized(_config_lock, shared=1)
2394 def GetAllNetworksInfo(self):
2395 """Get the configuration of all networks
2398 return dict(self._config_data.networks)
2400 def _UnlockedGetNetworkList(self):
2401 """Get the list of networks.
2403 This function is for internal use, when the config lock is already held.
2406 return self._config_data.networks.keys()
2408 @locking.ssynchronized(_config_lock, shared=1)
2409 def GetNetworkList(self):
2410 """Get the list of networks.
2412 @return: array of networks, ex. ["main", "vlan100", "200]
2415 return self._UnlockedGetNetworkList()
2417 @locking.ssynchronized(_config_lock, shared=1)
2418 def GetNetworkNames(self):
2419 """Get a list of network names
2423 for net in self._config_data.networks.values()]
2426 def _UnlockedGetNetwork(self, uuid):
2427 """Returns information about a network.
2429 This function is for internal use, when the config lock is already held.
2432 if uuid not in self._config_data.networks:
2435 return self._config_data.networks[uuid]
2437 @locking.ssynchronized(_config_lock, shared=1)
2438 def GetNetwork(self, uuid):
2439 """Returns information about a network.
2441 It takes the information from the configuration file.
2443 @param uuid: UUID of the network
2445 @rtype: L{objects.Network}
2446 @return: the network object
2449 return self._UnlockedGetNetwork(uuid)
2451 @locking.ssynchronized(_config_lock)
2452 def AddNetwork(self, net, ec_id, check_uuid=True):
2453 """Add a network to the configuration.
2455 @type net: L{objects.Network}
2456 @param net: the Network object to add
2458 @param ec_id: unique id for the job to use when creating a missing UUID
2461 self._UnlockedAddNetwork(net, ec_id, check_uuid)
2464 def _UnlockedAddNetwork(self, net, ec_id, check_uuid):
2465 """Add a network to the configuration.
2468 logging.info("Adding network %s to configuration", net.name)
2471 self._EnsureUUID(net, ec_id)
2473 existing_uuid = self._UnlockedLookupNetwork(net.name)
2475 raise errors.OpPrereqError("Desired network name '%s' already"
2476 " exists as a network (UUID: %s)" %
2477 (net.name, existing_uuid),
2478 errors.ECODE_EXISTS)
2480 self._config_data.networks[net.uuid] = net
2481 self._config_data.cluster.serial_no += 1
2483 def _UnlockedLookupNetwork(self, target):
2484 """Lookup a network's UUID.
2486 @type target: string
2487 @param target: network name or UUID
2489 @return: network UUID
2490 @raises errors.OpPrereqError: when the target network cannot be found
2493 if target in self._config_data.networks:
2495 for net in self._config_data.networks.values():
2496 if net.name == target:
2500 @locking.ssynchronized(_config_lock, shared=1)
2501 def LookupNetwork(self, target):
2502 """Lookup a network's UUID.
2504 This function is just a wrapper over L{_UnlockedLookupNetwork}.
2506 @type target: string
2507 @param target: network name or UUID
2509 @return: network UUID
2512 return self._UnlockedLookupNetwork(target)
2514 @locking.ssynchronized(_config_lock)
2515 def RemoveNetwork(self, network_uuid):
2516 """Remove a network from the configuration.
2518 @type network_uuid: string
2519 @param network_uuid: the UUID of the network to remove
2522 logging.info("Removing network %s from configuration", network_uuid)
2524 if network_uuid not in self._config_data.networks:
2525 raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
2527 del self._config_data.networks[network_uuid]
2528 self._config_data.cluster.serial_no += 1
2531 def _UnlockedGetGroupNetParams(self, net, node):
2532 """Get the netparams (mode, link) of a network.
2534 Get a network's netparams for a given node.
2537 @param net: network name
2539 @param node: node name
2540 @rtype: dict or None
2544 net_uuid = self._UnlockedLookupNetwork(net)
2545 if net_uuid is None:
2548 node_info = self._UnlockedGetNodeInfo(node)
2549 nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2550 netparams = nodegroup_info.networks.get(net_uuid, None)
2554 @locking.ssynchronized(_config_lock, shared=1)
2555 def GetGroupNetParams(self, net, node):
2556 """Locking wrapper of _UnlockedGetGroupNetParams()
2559 return self._UnlockedGetGroupNetParams(net, node)
2561 @locking.ssynchronized(_config_lock, shared=1)
2562 def CheckIPInNodeGroup(self, ip, node):
2563 """Check for conflictig IP.
2566 @param ip: ip address
2568 @param node: node name
2569 @rtype: (string, dict) or (None, None)
2570 @return: (network name, netparams)
2575 node_info = self._UnlockedGetNodeInfo(node)
2576 nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2577 for net_uuid in nodegroup_info.networks.keys():
2578 net_info = self._UnlockedGetNetwork(net_uuid)
2579 pool = network.AddressPool(net_info)
2580 if pool.Contains(ip):
2581 return (net_info.name, nodegroup_info.networks[net_uuid])