Statistics
| Branch: | Tag: | Revision:

root / lib / config.py @ 34f9be6d

History | View | Annotate | Download (84.5 kB)

1
#
2
#
3

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

    
21

    
22
"""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
# pylint: disable=R0904
35
# R0904: Too many public methods
36

    
37
import os
38
import random
39
import logging
40
import time
41
import itertools
42
from functools import wraps
43

    
44
from ganeti import errors
45
from ganeti import locking
46
from ganeti import utils
47
from ganeti import constants
48
from ganeti import rpc
49
from ganeti import objects
50
from ganeti import serializer
51
from ganeti import uidpool
52
from ganeti import netutils
53
from ganeti import runtime
54
from ganeti import network
55

    
56

    
57
_config_lock = locking.SharedLock("ConfigWriter")
58

    
59
# job id used for resource management at config upgrade time
60
_UPGRADE_CONFIG_JID = "jid-cfg-upgrade"
61

    
62

    
63
def _ValidateConfig(data):
64
  """Verifies that a configuration objects looks valid.
65

66
  This only verifies the version of the configuration.
67

68
  @raise errors.ConfigurationError: if the version differs from what
69
      we expect
70

71
  """
72
  if data.version != constants.CONFIG_VERSION:
73
    raise errors.ConfigVersionMismatch(constants.CONFIG_VERSION, data.version)
74

    
75

    
76
class TemporaryReservationManager:
77
  """A temporary resource reservation manager.
78

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

82
  """
83
  def __init__(self):
84
    self._ec_reserved = {}
85

    
86
  def Reserved(self, resource):
87
    for holder_reserved in self._ec_reserved.values():
88
      if resource in holder_reserved:
89
        return True
90
    return False
91

    
92
  def Reserve(self, ec_id, resource):
93
    if self.Reserved(resource):
94
      raise errors.ReservationError("Duplicate reservation for resource '%s'"
95
                                    % str(resource))
96
    if ec_id not in self._ec_reserved:
97
      self._ec_reserved[ec_id] = set([resource])
98
    else:
99
      self._ec_reserved[ec_id].add(resource)
100

    
101
  def DropECReservations(self, ec_id):
102
    if ec_id in self._ec_reserved:
103
      del self._ec_reserved[ec_id]
104

    
105
  def GetReserved(self):
106
    all_reserved = set()
107
    for holder_reserved in self._ec_reserved.values():
108
      all_reserved.update(holder_reserved)
109
    return all_reserved
110

    
111
  def GetECReserved(self, ec_id):
112
    ec_reserved = set()
113
    if ec_id in self._ec_reserved:
114
      ec_reserved.update(self._ec_reserved[ec_id])
115
    return ec_reserved
116

    
117

    
118
  def Generate(self, existing, generate_one_fn, ec_id):
119
    """Generate a new resource of this type
120

121
    """
122
    assert callable(generate_one_fn)
123

    
124
    all_elems = self.GetReserved()
125
    all_elems.update(existing)
126
    retries = 64
127
    while retries > 0:
128
      new_resource = generate_one_fn()
129
      if new_resource is not None and new_resource not in all_elems:
130
        break
131
    else:
132
      raise errors.ConfigurationError("Not able generate new resource"
133
                                      " (last tried: %s)" % new_resource)
134
    self.Reserve(ec_id, new_resource)
135
    return new_resource
136

    
137

    
138
def _MatchNameComponentIgnoreCase(short_name, names):
139
  """Wrapper around L{utils.text.MatchNameComponent}.
140

141
  """
142
  return utils.MatchNameComponent(short_name, names, case_sensitive=False)
143

    
144

    
145
def _CheckInstanceDiskIvNames(disks):
146
  """Checks if instance's disks' C{iv_name} attributes are in order.
147

148
  @type disks: list of L{objects.Disk}
149
  @param disks: List of disks
150
  @rtype: list of tuples; (int, string, string)
151
  @return: List of wrongly named disks, each tuple contains disk index,
152
    expected and actual name
153

154
  """
155
  result = []
156

    
157
  for (idx, disk) in enumerate(disks):
158
    exp_iv_name = "disk/%s" % idx
159
    if disk.iv_name != exp_iv_name:
160
      result.append((idx, exp_iv_name, disk.iv_name))
161

    
162
  return result
163

    
164

    
165
class ConfigWriter:
166
  """The interface to the cluster configuration.
167

168
  @ivar _temporary_lvs: reservation manager for temporary LVs
169
  @ivar _all_rms: a list of all temporary reservation managers
170

171
  """
172
  def __init__(self, cfg_file=None, offline=False, _getents=runtime.GetEnts,
173
               accept_foreign=False):
174
    self.write_count = 0
175
    self._lock = _config_lock
176
    self._config_data = None
177
    self._offline = offline
178
    if cfg_file is None:
179
      self._cfg_file = constants.CLUSTER_CONF_FILE
180
    else:
181
      self._cfg_file = cfg_file
182
    self._getents = _getents
183
    self._temporary_ids = TemporaryReservationManager()
184
    self._temporary_drbds = {}
185
    self._temporary_macs = TemporaryReservationManager()
186
    self._temporary_secrets = TemporaryReservationManager()
187
    self._temporary_lvs = TemporaryReservationManager()
188
    self._temporary_ips = TemporaryReservationManager()
189
    self._all_rms = [self._temporary_ids, self._temporary_macs,
190
                     self._temporary_secrets, self._temporary_lvs,
191
                     self._temporary_ips]
192
    # Note: in order to prevent errors when resolving our name in
193
    # _DistributeConfig, we compute it here once and reuse it; it's
194
    # better to raise an error before starting to modify the config
195
    # file than after it was modified
196
    self._my_hostname = netutils.Hostname.GetSysName()
197
    self._last_cluster_serial = -1
198
    self._cfg_id = None
199
    self._context = None
200
    self._OpenConfig(accept_foreign)
201

    
202
  def _GetRpc(self, address_list):
203
    """Returns RPC runner for configuration.
204

205
    """
206
    return rpc.ConfigRunner(self._context, address_list)
207

    
208
  def SetContext(self, context):
209
    """Sets Ganeti context.
210

211
    """
212
    self._context = context
213

    
214
  # this method needs to be static, so that we can call it on the class
215
  @staticmethod
216
  def IsCluster():
217
    """Check if the cluster is configured.
218

219
    """
220
    return os.path.exists(constants.CLUSTER_CONF_FILE)
221

    
222
  def _GenerateMACPrefix(self, net=None):
223
    def _get_mac_prefix(view_func):
224
      def _decorator(*args, **kwargs):
225
        prefix = self._config_data.cluster.mac_prefix
226
        if net:
227
          net_uuid = self._UnlockedLookupNetwork(net)
228
          if net_uuid:
229
            nobj = self._UnlockedGetNetwork(net_uuid)
230
            if nobj.mac_prefix:
231
              prefix = nobj.mac_prefix
232
        suffix = view_func(*args, **kwargs)
233
        return prefix+':'+suffix
234
      return wraps(view_func)(_decorator)
235
    return _get_mac_prefix
236

    
237
  def _GenerateMACSuffix(self):
238
    """Generate one mac address
239

240
    """
241
    byte1 = random.randrange(0, 256)
242
    byte2 = random.randrange(0, 256)
243
    byte3 = random.randrange(0, 256)
244
    suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
245
    return suffix
246

    
247
  @locking.ssynchronized(_config_lock, shared=1)
248
  def GetNdParams(self, node):
249
    """Get the node params populated with cluster defaults.
250

251
    @type node: L{objects.Node}
252
    @param node: The node we want to know the params for
253
    @return: A dict with the filled in node params
254

255
    """
256
    nodegroup = self._UnlockedGetNodeGroup(node.group)
257
    return self._config_data.cluster.FillND(node, nodegroup)
258

    
259
  @locking.ssynchronized(_config_lock, shared=1)
260
  def GetInstanceDiskParams(self, instance):
261
    """Get the disk params populated with inherit chain.
262

263
    @type instance: L{objects.Instance}
264
    @param instance: The instance we want to know the params for
265
    @return: A dict with the filled in disk params
266

267
    """
268
    node = self._UnlockedGetNodeInfo(instance.primary_node)
269
    nodegroup = self._UnlockedGetNodeGroup(node.group)
270
    return self._UnlockedGetGroupDiskParams(nodegroup)
271

    
272
  @locking.ssynchronized(_config_lock, shared=1)
273
  def GetGroupDiskParams(self, group):
274
    """Get the disk params populated with inherit chain.
275

276
    @type group: L{objects.NodeGroup}
277
    @param group: The group we want to know the params for
278
    @return: A dict with the filled in disk params
279

280
    """
281
    return self._UnlockedGetGroupDiskParams(group)
282

    
283
  def _UnlockedGetGroupDiskParams(self, group):
284
    """Get the disk params populated with inherit chain down to node-group.
285

286
    @type group: L{objects.NodeGroup}
287
    @param group: The group we want to know the params for
288
    @return: A dict with the filled in disk params
289

290
    """
291
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
292

    
293
  @locking.ssynchronized(_config_lock, shared=1)
294
  def GenerateMAC(self, net, ec_id):
295
    """Generate a MAC for an instance.
296

297
    This should check the current instances for duplicates.
298

299
    """
300
    existing = self._AllMACs()
301
    gen_mac = self._GenerateMACPrefix(net)(self._GenerateMACSuffix)
302
    return self._temporary_ids.Generate(existing, gen_mac, ec_id)
303

    
304
  @locking.ssynchronized(_config_lock, shared=1)
305
  def ReserveMAC(self, mac, ec_id):
306
    """Reserve a MAC for an instance.
307

308
    This only checks instances managed by this cluster, it does not
309
    check for potential collisions elsewhere.
310

311
    """
312
    all_macs = self._AllMACs()
313
    if mac in all_macs:
314
      raise errors.ReservationError("mac already in use")
315
    else:
316
      self._temporary_macs.Reserve(ec_id, mac)
317

    
318
  def _UnlockedCommitTemporaryIps(self, ec_id):
319
    """Commit all reserved IP address to their respective pools
320

321
    """
322
    for action, address, net_uuid in self._temporary_ips.GetECReserved(ec_id):
323
      self._UnlockedCommitIp(action, net_uuid, address)
324

    
325
  def _UnlockedCommitIp(self, action, net_uuid, address):
326
    """Commit a reserved IP address to an IP pool.
327

328
    The IP address is taken from the network's IP pool and marked as reserved.
329

330
    """
331
    nobj = self._UnlockedGetNetwork(net_uuid)
332
    pool = network.AddressPool(nobj)
333
    if action == 'reserve':
334
      pool.Reserve(address)
335
    elif action == 'release':
336
      pool.Release(address)
337

    
338
  def _UnlockedReleaseIp(self, net_uuid, address, ec_id):
339
    """Give a specific IP address back to an IP pool.
340

341
    The IP address is returned to the IP pool designated by pool_id and marked
342
    as reserved.
343

344
    """
345
    nobj = self._UnlockedGetNetwork(net_uuid)
346
    pool = network.AddressPool(nobj)
347
    self._temporary_ips.Reserve(ec_id, ('release', address, net_uuid))
348

    
349
  @locking.ssynchronized(_config_lock, shared=1)
350
  def ReleaseIp(self, network, address, ec_id):
351
    """Give a specified IP address back to an IP pool.
352

353
    This is just a wrapper around _UnlockedReleaseIp.
354

355
    """
356
    net_uuid = self._UnlockedLookupNetwork(network)
357
    if net_uuid:
358
      self._UnlockedReleaseIp(net_uuid, address, ec_id)
359

    
360
  @locking.ssynchronized(_config_lock, shared=1)
361
  def GenerateIp(self, net, ec_id):
362
    """Find a free IPv4 address for an instance.
363

364
    """
365
    net_uuid = self._UnlockedLookupNetwork(net)
366
    nobj = self._UnlockedGetNetwork(net_uuid)
367
    pool = network.AddressPool(nobj)
368
    gen_free = pool.GenerateFree()
369

    
370
    def gen_one():
371
      try:
372
        ip = gen_free()
373
      except StopIteration:
374
        raise errors.ReservationError("Cannot generate IP. Network is full")
375
      return ("reserve", ip, net_uuid)
376

    
377
    _ ,address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
378
    return address
379

    
380
  def _UnlockedReserveIp(self, net_uuid, address, ec_id):
381
    """Reserve a given IPv4 address for use by an instance.
382

383
    """
384
    nobj = self._UnlockedGetNetwork(net_uuid)
385
    pool = network.AddressPool(nobj)
386
    try:
387
      isreserved = pool.IsReserved(address)
388
    except errors.AddressPoolError:
389
      raise errors.ReservationError("IP address not in network")
390
    if isreserved:
391
      raise errors.ReservationError("IP address already in use")
392

    
393
    return self._temporary_ips.Reserve(ec_id, ('reserve', address, net_uuid))
394

    
395

    
396
  @locking.ssynchronized(_config_lock, shared=1)
397
  def ReserveIp(self, net, address, ec_id):
398
    """Reserve a given IPv4 address for use by an instance.
399

400
    """
401
    net_uuid = self._UnlockedLookupNetwork(net)
402
    if net_uuid:
403
      return self._UnlockedReserveIp(net_uuid, address, ec_id)
404

    
405
  @locking.ssynchronized(_config_lock, shared=1)
406
  def ReserveLV(self, lv_name, ec_id):
407
    """Reserve an VG/LV pair for an instance.
408

409
    @type lv_name: string
410
    @param lv_name: the logical volume name to reserve
411

412
    """
413
    all_lvs = self._AllLVs()
414
    if lv_name in all_lvs:
415
      raise errors.ReservationError("LV already in use")
416
    else:
417
      self._temporary_lvs.Reserve(ec_id, lv_name)
418

    
419
  @locking.ssynchronized(_config_lock, shared=1)
420
  def GenerateDRBDSecret(self, ec_id):
421
    """Generate a DRBD secret.
422

423
    This checks the current disks for duplicates.
424

425
    """
426
    return self._temporary_secrets.Generate(self._AllDRBDSecrets(),
427
                                            utils.GenerateSecret,
428
                                            ec_id)
429

    
430
  def _AllLVs(self):
431
    """Compute the list of all LVs.
432

433
    """
434
    lvnames = set()
435
    for instance in self._config_data.instances.values():
436
      node_data = instance.MapLVsByNode()
437
      for lv_list in node_data.values():
438
        lvnames.update(lv_list)
439
    return lvnames
440

    
441
  def _AllIDs(self, include_temporary):
442
    """Compute the list of all UUIDs and names we have.
443

444
    @type include_temporary: boolean
445
    @param include_temporary: whether to include the _temporary_ids set
446
    @rtype: set
447
    @return: a set of IDs
448

449
    """
450
    existing = set()
451
    if include_temporary:
452
      existing.update(self._temporary_ids.GetReserved())
453
    existing.update(self._AllLVs())
454
    existing.update(self._config_data.instances.keys())
455
    existing.update(self._config_data.nodes.keys())
456
    existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid])
457
    return existing
458

    
459
  def _GenerateUniqueID(self, ec_id):
460
    """Generate an unique UUID.
461

462
    This checks the current node, instances and disk names for
463
    duplicates.
464

465
    @rtype: string
466
    @return: the unique id
467

468
    """
469
    existing = self._AllIDs(include_temporary=False)
470
    return self._temporary_ids.Generate(existing, utils.NewUUID, ec_id)
471

    
472
  @locking.ssynchronized(_config_lock, shared=1)
473
  def GenerateUniqueID(self, ec_id):
474
    """Generate an unique ID.
475

476
    This is just a wrapper over the unlocked version.
477

478
    @type ec_id: string
479
    @param ec_id: unique id for the job to reserve the id to
480

481
    """
482
    return self._GenerateUniqueID(ec_id)
483

    
484
  def _AllMACs(self):
485
    """Return all MACs present in the config.
486

487
    @rtype: list
488
    @return: the list of all MACs
489

490
    """
491
    result = []
492
    for instance in self._config_data.instances.values():
493
      for nic in instance.nics:
494
        result.append(nic.mac)
495

    
496
    return result
497

    
498
  def _AllDRBDSecrets(self):
499
    """Return all DRBD secrets present in the config.
500

501
    @rtype: list
502
    @return: the list of all DRBD secrets
503

504
    """
505
    def helper(disk, result):
506
      """Recursively gather secrets from this disk."""
507
      if disk.dev_type == constants.DT_DRBD8:
508
        result.append(disk.logical_id[5])
509
      if disk.children:
510
        for child in disk.children:
511
          helper(child, result)
512

    
513
    result = []
514
    for instance in self._config_data.instances.values():
515
      for disk in instance.disks:
516
        helper(disk, result)
517

    
518
    return result
519

    
520
  def _CheckDiskIDs(self, disk, l_ids, p_ids):
521
    """Compute duplicate disk IDs
522

523
    @type disk: L{objects.Disk}
524
    @param disk: the disk at which to start searching
525
    @type l_ids: list
526
    @param l_ids: list of current logical ids
527
    @type p_ids: list
528
    @param p_ids: list of current physical ids
529
    @rtype: list
530
    @return: a list of error messages
531

532
    """
533
    result = []
534
    if disk.logical_id is not None:
535
      if disk.logical_id in l_ids:
536
        result.append("duplicate logical id %s" % str(disk.logical_id))
537
      else:
538
        l_ids.append(disk.logical_id)
539
    if disk.physical_id is not None:
540
      if disk.physical_id in p_ids:
541
        result.append("duplicate physical id %s" % str(disk.physical_id))
542
      else:
543
        p_ids.append(disk.physical_id)
544

    
545
    if disk.children:
546
      for child in disk.children:
547
        result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
548
    return result
549

    
550
  def _UnlockedVerifyConfig(self):
551
    """Verify function.
552

553
    @rtype: list
554
    @return: a list of error messages; a non-empty list signifies
555
        configuration errors
556

557
    """
558
    # pylint: disable=R0914
559
    result = []
560
    seen_macs = []
561
    ports = {}
562
    data = self._config_data
563
    cluster = data.cluster
564
    seen_lids = []
565
    seen_pids = []
566

    
567
    # global cluster checks
568
    if not cluster.enabled_hypervisors:
569
      result.append("enabled hypervisors list doesn't have any entries")
570
    invalid_hvs = set(cluster.enabled_hypervisors) - constants.HYPER_TYPES
571
    if invalid_hvs:
572
      result.append("enabled hypervisors contains invalid entries: %s" %
573
                    invalid_hvs)
574
    missing_hvp = (set(cluster.enabled_hypervisors) -
575
                   set(cluster.hvparams.keys()))
576
    if missing_hvp:
577
      result.append("hypervisor parameters missing for the enabled"
578
                    " hypervisor(s) %s" % utils.CommaJoin(missing_hvp))
579

    
580
    if cluster.master_node not in data.nodes:
581
      result.append("cluster has invalid primary node '%s'" %
582
                    cluster.master_node)
583

    
584
    def _helper(owner, attr, value, template):
585
      try:
586
        utils.ForceDictType(value, template)
587
      except errors.GenericError, err:
588
        result.append("%s has invalid %s: %s" % (owner, attr, err))
589

    
590
    def _helper_nic(owner, params):
591
      try:
592
        objects.NIC.CheckParameterSyntax(params)
593
      except errors.ConfigurationError, err:
594
        result.append("%s has invalid nicparams: %s" % (owner, err))
595

    
596
    def _helper_ipolicy(owner, params):
597
      try:
598
        objects.InstancePolicy.CheckParameterSyntax(params)
599
      except errors.ConfigurationError, err:
600
        result.append("%s has invalid instance policy: %s" % (owner, err))
601

    
602
    def _helper_ispecs(owner, params):
603
      for key, value in params.items():
604
        if key in constants.IPOLICY_ISPECS:
605
          fullkey = "ipolicy/" + key
606
          _helper(owner, fullkey, value, constants.ISPECS_PARAMETER_TYPES)
607
        else:
608
          # FIXME: assuming list type
609
          if key in constants.IPOLICY_PARAMETERS:
610
            exp_type = float
611
          else:
612
            exp_type = list
613
          if not isinstance(value, exp_type):
614
            result.append("%s has invalid instance policy: for %s,"
615
                          " expecting %s, got %s" %
616
                          (owner, key, exp_type.__name__, type(value)))
617

    
618
    # check cluster parameters
619
    _helper("cluster", "beparams", cluster.SimpleFillBE({}),
620
            constants.BES_PARAMETER_TYPES)
621
    _helper("cluster", "nicparams", cluster.SimpleFillNIC({}),
622
            constants.NICS_PARAMETER_TYPES)
623
    _helper_nic("cluster", cluster.SimpleFillNIC({}))
624
    _helper("cluster", "ndparams", cluster.SimpleFillND({}),
625
            constants.NDS_PARAMETER_TYPES)
626
    _helper_ipolicy("cluster", cluster.SimpleFillIPolicy({}))
627
    _helper_ispecs("cluster", cluster.SimpleFillIPolicy({}))
628

    
629
    # per-instance checks
630
    for instance_name in data.instances:
631
      instance = data.instances[instance_name]
632
      if instance.name != instance_name:
633
        result.append("instance '%s' is indexed by wrong name '%s'" %
634
                      (instance.name, instance_name))
635
      if instance.primary_node not in data.nodes:
636
        result.append("instance '%s' has invalid primary node '%s'" %
637
                      (instance_name, instance.primary_node))
638
      for snode in instance.secondary_nodes:
639
        if snode not in data.nodes:
640
          result.append("instance '%s' has invalid secondary node '%s'" %
641
                        (instance_name, snode))
642
      for idx, nic in enumerate(instance.nics):
643
        if nic.mac in seen_macs:
644
          result.append("instance '%s' has NIC %d mac %s duplicate" %
645
                        (instance_name, idx, nic.mac))
646
        else:
647
          seen_macs.append(nic.mac)
648
        if nic.nicparams:
649
          filled = cluster.SimpleFillNIC(nic.nicparams)
650
          owner = "instance %s nic %d" % (instance.name, idx)
651
          _helper(owner, "nicparams",
652
                  filled, constants.NICS_PARAMETER_TYPES)
653
          _helper_nic(owner, filled)
654

    
655
      # parameter checks
656
      if instance.beparams:
657
        _helper("instance %s" % instance.name, "beparams",
658
                cluster.FillBE(instance), constants.BES_PARAMETER_TYPES)
659

    
660
      # gather the drbd ports for duplicate checks
661
      for (idx, dsk) in enumerate(instance.disks):
662
        if dsk.dev_type in constants.LDS_DRBD:
663
          tcp_port = dsk.logical_id[2]
664
          if tcp_port not in ports:
665
            ports[tcp_port] = []
666
          ports[tcp_port].append((instance.name, "drbd disk %s" % idx))
667
      # gather network port reservation
668
      net_port = getattr(instance, "network_port", None)
669
      if net_port is not None:
670
        if net_port not in ports:
671
          ports[net_port] = []
672
        ports[net_port].append((instance.name, "network port"))
673

    
674
      # instance disk verify
675
      for idx, disk in enumerate(instance.disks):
676
        result.extend(["instance '%s' disk %d error: %s" %
677
                       (instance.name, idx, msg) for msg in disk.Verify()])
678
        result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
679

    
680
      wrong_names = _CheckInstanceDiskIvNames(instance.disks)
681
      if wrong_names:
682
        tmp = "; ".join(("name of disk %s should be '%s', but is '%s'" %
683
                         (idx, exp_name, actual_name))
684
                        for (idx, exp_name, actual_name) in wrong_names)
685

    
686
        result.append("Instance '%s' has wrongly named disks: %s" %
687
                      (instance.name, tmp))
688

    
689
    # cluster-wide pool of free ports
690
    for free_port in cluster.tcpudp_port_pool:
691
      if free_port not in ports:
692
        ports[free_port] = []
693
      ports[free_port].append(("cluster", "port marked as free"))
694

    
695
    # compute tcp/udp duplicate ports
696
    keys = ports.keys()
697
    keys.sort()
698
    for pnum in keys:
699
      pdata = ports[pnum]
700
      if len(pdata) > 1:
701
        txt = utils.CommaJoin(["%s/%s" % val for val in pdata])
702
        result.append("tcp/udp port %s has duplicates: %s" % (pnum, txt))
703

    
704
    # highest used tcp port check
705
    if keys:
706
      if keys[-1] > cluster.highest_used_port:
707
        result.append("Highest used port mismatch, saved %s, computed %s" %
708
                      (cluster.highest_used_port, keys[-1]))
709

    
710
    if not data.nodes[cluster.master_node].master_candidate:
711
      result.append("Master node is not a master candidate")
712

    
713
    # master candidate checks
714
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
715
    if mc_now < mc_max:
716
      result.append("Not enough master candidates: actual %d, target %d" %
717
                    (mc_now, mc_max))
718

    
719
    # node checks
720
    for node_name, node in data.nodes.items():
721
      if node.name != node_name:
722
        result.append("Node '%s' is indexed by wrong name '%s'" %
723
                      (node.name, node_name))
724
      if [node.master_candidate, node.drained, node.offline].count(True) > 1:
725
        result.append("Node %s state is invalid: master_candidate=%s,"
726
                      " drain=%s, offline=%s" %
727
                      (node.name, node.master_candidate, node.drained,
728
                       node.offline))
729
      if node.group not in data.nodegroups:
730
        result.append("Node '%s' has invalid group '%s'" %
731
                      (node.name, node.group))
732
      else:
733
        _helper("node %s" % node.name, "ndparams",
734
                cluster.FillND(node, data.nodegroups[node.group]),
735
                constants.NDS_PARAMETER_TYPES)
736

    
737
    # nodegroups checks
738
    nodegroups_names = set()
739
    for nodegroup_uuid in data.nodegroups:
740
      nodegroup = data.nodegroups[nodegroup_uuid]
741
      if nodegroup.uuid != nodegroup_uuid:
742
        result.append("node group '%s' (uuid: '%s') indexed by wrong uuid '%s'"
743
                      % (nodegroup.name, nodegroup.uuid, nodegroup_uuid))
744
      if utils.UUID_RE.match(nodegroup.name.lower()):
745
        result.append("node group '%s' (uuid: '%s') has uuid-like name" %
746
                      (nodegroup.name, nodegroup.uuid))
747
      if nodegroup.name in nodegroups_names:
748
        result.append("duplicate node group name '%s'" % nodegroup.name)
749
      else:
750
        nodegroups_names.add(nodegroup.name)
751
      group_name = "group %s" % nodegroup.name
752
      _helper_ipolicy(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy))
753
      _helper_ispecs(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy))
754
      if nodegroup.ndparams:
755
        _helper(group_name, "ndparams",
756
                cluster.SimpleFillND(nodegroup.ndparams),
757
                constants.NDS_PARAMETER_TYPES)
758

    
759
    # drbd minors check
760
    _, duplicates = self._UnlockedComputeDRBDMap()
761
    for node, minor, instance_a, instance_b in duplicates:
762
      result.append("DRBD minor %d on node %s is assigned twice to instances"
763
                    " %s and %s" % (minor, node, instance_a, instance_b))
764

    
765
    # IP checks
766
    default_nicparams = cluster.nicparams[constants.PP_DEFAULT]
767
    ips = {}
768

    
769
    def _AddIpAddress(ip, name):
770
      ips.setdefault(ip, []).append(name)
771

    
772
    _AddIpAddress(cluster.master_ip, "cluster_ip")
773

    
774
    for node in data.nodes.values():
775
      _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
776
      if node.secondary_ip != node.primary_ip:
777
        _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
778

    
779
    for instance in data.instances.values():
780
      for idx, nic in enumerate(instance.nics):
781
        if nic.ip is None:
782
          continue
783

    
784
        nicparams = objects.FillDict(default_nicparams, nic.nicparams)
785
        nic_mode = nicparams[constants.NIC_MODE]
786
        nic_link = nicparams[constants.NIC_LINK]
787

    
788
        if nic_mode == constants.NIC_MODE_BRIDGED:
789
          link = "bridge:%s" % nic_link
790
        elif nic_mode == constants.NIC_MODE_ROUTED:
791
          link = "route:%s" % nic_link
792
        else:
793
          raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
794

    
795
        _AddIpAddress("%s/%s/%s" % (link, nic.ip, nic.network),
796
                      "instance:%s/nic:%d" % (instance.name, idx))
797

    
798
    for ip, owners in ips.items():
799
      if len(owners) > 1:
800
        result.append("IP address %s is used by multiple owners: %s" %
801
                      (ip, utils.CommaJoin(owners)))
802

    
803
    return result
804

    
805
  @locking.ssynchronized(_config_lock, shared=1)
806
  def VerifyConfig(self):
807
    """Verify function.
808

809
    This is just a wrapper over L{_UnlockedVerifyConfig}.
810

811
    @rtype: list
812
    @return: a list of error messages; a non-empty list signifies
813
        configuration errors
814

815
    """
816
    return self._UnlockedVerifyConfig()
817

    
818
  def _UnlockedSetDiskID(self, disk, node_name):
819
    """Convert the unique ID to the ID needed on the target nodes.
820

821
    This is used only for drbd, which needs ip/port configuration.
822

823
    The routine descends down and updates its children also, because
824
    this helps when the only the top device is passed to the remote
825
    node.
826

827
    This function is for internal use, when the config lock is already held.
828

829
    """
830
    if disk.children:
831
      for child in disk.children:
832
        self._UnlockedSetDiskID(child, node_name)
833

    
834
    if disk.logical_id is None and disk.physical_id is not None:
835
      return
836
    if disk.dev_type == constants.LD_DRBD8:
837
      pnode, snode, port, pminor, sminor, secret = disk.logical_id
838
      if node_name not in (pnode, snode):
839
        raise errors.ConfigurationError("DRBD device not knowing node %s" %
840
                                        node_name)
841
      pnode_info = self._UnlockedGetNodeInfo(pnode)
842
      snode_info = self._UnlockedGetNodeInfo(snode)
843
      if pnode_info is None or snode_info is None:
844
        raise errors.ConfigurationError("Can't find primary or secondary node"
845
                                        " for %s" % str(disk))
846
      p_data = (pnode_info.secondary_ip, port)
847
      s_data = (snode_info.secondary_ip, port)
848
      if pnode == node_name:
849
        disk.physical_id = p_data + s_data + (pminor, secret)
850
      else: # it must be secondary, we tested above
851
        disk.physical_id = s_data + p_data + (sminor, secret)
852
    else:
853
      disk.physical_id = disk.logical_id
854
    return
855

    
856
  @locking.ssynchronized(_config_lock)
857
  def SetDiskID(self, disk, node_name):
858
    """Convert the unique ID to the ID needed on the target nodes.
859

860
    This is used only for drbd, which needs ip/port configuration.
861

862
    The routine descends down and updates its children also, because
863
    this helps when the only the top device is passed to the remote
864
    node.
865

866
    """
867
    return self._UnlockedSetDiskID(disk, node_name)
868

    
869
  @locking.ssynchronized(_config_lock)
870
  def AddTcpUdpPort(self, port):
871
    """Adds a new port to the available port pool.
872

873
    @warning: this method does not "flush" the configuration (via
874
        L{_WriteConfig}); callers should do that themselves once the
875
        configuration is stable
876

877
    """
878
    if not isinstance(port, int):
879
      raise errors.ProgrammerError("Invalid type passed for port")
880

    
881
    self._config_data.cluster.tcpudp_port_pool.add(port)
882

    
883
  @locking.ssynchronized(_config_lock, shared=1)
884
  def GetPortList(self):
885
    """Returns a copy of the current port list.
886

887
    """
888
    return self._config_data.cluster.tcpudp_port_pool.copy()
889

    
890
  @locking.ssynchronized(_config_lock)
891
  def AllocatePort(self):
892
    """Allocate a port.
893

894
    The port will be taken from the available port pool or from the
895
    default port range (and in this case we increase
896
    highest_used_port).
897

898
    """
899
    # If there are TCP/IP ports configured, we use them first.
900
    if self._config_data.cluster.tcpudp_port_pool:
901
      port = self._config_data.cluster.tcpudp_port_pool.pop()
902
    else:
903
      port = self._config_data.cluster.highest_used_port + 1
904
      if port >= constants.LAST_DRBD_PORT:
905
        raise errors.ConfigurationError("The highest used port is greater"
906
                                        " than %s. Aborting." %
907
                                        constants.LAST_DRBD_PORT)
908
      self._config_data.cluster.highest_used_port = port
909

    
910
    self._WriteConfig()
911
    return port
912

    
913
  def _UnlockedComputeDRBDMap(self):
914
    """Compute the used DRBD minor/nodes.
915

916
    @rtype: (dict, list)
917
    @return: dictionary of node_name: dict of minor: instance_name;
918
        the returned dict will have all the nodes in it (even if with
919
        an empty list), and a list of duplicates; if the duplicates
920
        list is not empty, the configuration is corrupted and its caller
921
        should raise an exception
922

923
    """
924
    def _AppendUsedPorts(instance_name, disk, used):
925
      duplicates = []
926
      if disk.dev_type == constants.LD_DRBD8 and len(disk.logical_id) >= 5:
927
        node_a, node_b, _, minor_a, minor_b = disk.logical_id[:5]
928
        for node, port in ((node_a, minor_a), (node_b, minor_b)):
929
          assert node in used, ("Node '%s' of instance '%s' not found"
930
                                " in node list" % (node, instance_name))
931
          if port in used[node]:
932
            duplicates.append((node, port, instance_name, used[node][port]))
933
          else:
934
            used[node][port] = instance_name
935
      if disk.children:
936
        for child in disk.children:
937
          duplicates.extend(_AppendUsedPorts(instance_name, child, used))
938
      return duplicates
939

    
940
    duplicates = []
941
    my_dict = dict((node, {}) for node in self._config_data.nodes)
942
    for instance in self._config_data.instances.itervalues():
943
      for disk in instance.disks:
944
        duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict))
945
    for (node, minor), instance in self._temporary_drbds.iteritems():
946
      if minor in my_dict[node] and my_dict[node][minor] != instance:
947
        duplicates.append((node, minor, instance, my_dict[node][minor]))
948
      else:
949
        my_dict[node][minor] = instance
950
    return my_dict, duplicates
951

    
952
  @locking.ssynchronized(_config_lock)
953
  def ComputeDRBDMap(self):
954
    """Compute the used DRBD minor/nodes.
955

956
    This is just a wrapper over L{_UnlockedComputeDRBDMap}.
957

958
    @return: dictionary of node_name: dict of minor: instance_name;
959
        the returned dict will have all the nodes in it (even if with
960
        an empty list).
961

962
    """
963
    d_map, duplicates = self._UnlockedComputeDRBDMap()
964
    if duplicates:
965
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
966
                                      str(duplicates))
967
    return d_map
968

    
969
  @locking.ssynchronized(_config_lock)
970
  def AllocateDRBDMinor(self, nodes, instance):
971
    """Allocate a drbd minor.
972

973
    The free minor will be automatically computed from the existing
974
    devices. A node can be given multiple times in order to allocate
975
    multiple minors. The result is the list of minors, in the same
976
    order as the passed nodes.
977

978
    @type instance: string
979
    @param instance: the instance for which we allocate minors
980

981
    """
982
    assert isinstance(instance, basestring), \
983
           "Invalid argument '%s' passed to AllocateDRBDMinor" % instance
984

    
985
    d_map, duplicates = self._UnlockedComputeDRBDMap()
986
    if duplicates:
987
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
988
                                      str(duplicates))
989
    result = []
990
    for nname in nodes:
991
      ndata = d_map[nname]
992
      if not ndata:
993
        # no minors used, we can start at 0
994
        result.append(0)
995
        ndata[0] = instance
996
        self._temporary_drbds[(nname, 0)] = instance
997
        continue
998
      keys = ndata.keys()
999
      keys.sort()
1000
      ffree = utils.FirstFree(keys)
1001
      if ffree is None:
1002
        # return the next minor
1003
        # TODO: implement high-limit check
1004
        minor = keys[-1] + 1
1005
      else:
1006
        minor = ffree
1007
      # double-check minor against current instances
1008
      assert minor not in d_map[nname], \
1009
             ("Attempt to reuse allocated DRBD minor %d on node %s,"
1010
              " already allocated to instance %s" %
1011
              (minor, nname, d_map[nname][minor]))
1012
      ndata[minor] = instance
1013
      # double-check minor against reservation
1014
      r_key = (nname, minor)
1015
      assert r_key not in self._temporary_drbds, \
1016
             ("Attempt to reuse reserved DRBD minor %d on node %s,"
1017
              " reserved for instance %s" %
1018
              (minor, nname, self._temporary_drbds[r_key]))
1019
      self._temporary_drbds[r_key] = instance
1020
      result.append(minor)
1021
    logging.debug("Request to allocate drbd minors, input: %s, returning %s",
1022
                  nodes, result)
1023
    return result
1024

    
1025
  def _UnlockedReleaseDRBDMinors(self, instance):
1026
    """Release temporary drbd minors allocated for a given instance.
1027

1028
    @type instance: string
1029
    @param instance: the instance for which temporary minors should be
1030
                     released
1031

1032
    """
1033
    assert isinstance(instance, basestring), \
1034
           "Invalid argument passed to ReleaseDRBDMinors"
1035
    for key, name in self._temporary_drbds.items():
1036
      if name == instance:
1037
        del self._temporary_drbds[key]
1038

    
1039
  @locking.ssynchronized(_config_lock)
1040
  def ReleaseDRBDMinors(self, instance):
1041
    """Release temporary drbd minors allocated for a given instance.
1042

1043
    This should be called on the error paths, on the success paths
1044
    it's automatically called by the ConfigWriter add and update
1045
    functions.
1046

1047
    This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
1048

1049
    @type instance: string
1050
    @param instance: the instance for which temporary minors should be
1051
                     released
1052

1053
    """
1054
    self._UnlockedReleaseDRBDMinors(instance)
1055

    
1056
  @locking.ssynchronized(_config_lock, shared=1)
1057
  def GetConfigVersion(self):
1058
    """Get the configuration version.
1059

1060
    @return: Config version
1061

1062
    """
1063
    return self._config_data.version
1064

    
1065
  @locking.ssynchronized(_config_lock, shared=1)
1066
  def GetClusterName(self):
1067
    """Get cluster name.
1068

1069
    @return: Cluster name
1070

1071
    """
1072
    return self._config_data.cluster.cluster_name
1073

    
1074
  @locking.ssynchronized(_config_lock, shared=1)
1075
  def GetMasterNode(self):
1076
    """Get the hostname of the master node for this cluster.
1077

1078
    @return: Master hostname
1079

1080
    """
1081
    return self._config_data.cluster.master_node
1082

    
1083
  @locking.ssynchronized(_config_lock, shared=1)
1084
  def GetMasterIP(self):
1085
    """Get the IP of the master node for this cluster.
1086

1087
    @return: Master IP
1088

1089
    """
1090
    return self._config_data.cluster.master_ip
1091

    
1092
  @locking.ssynchronized(_config_lock, shared=1)
1093
  def GetMasterNetdev(self):
1094
    """Get the master network device for this cluster.
1095

1096
    """
1097
    return self._config_data.cluster.master_netdev
1098

    
1099
  @locking.ssynchronized(_config_lock, shared=1)
1100
  def GetMasterNetmask(self):
1101
    """Get the netmask of the master node for this cluster.
1102

1103
    """
1104
    return self._config_data.cluster.master_netmask
1105

    
1106
  @locking.ssynchronized(_config_lock, shared=1)
1107
  def GetUseExternalMipScript(self):
1108
    """Get flag representing whether to use the external master IP setup script.
1109

1110
    """
1111
    return self._config_data.cluster.use_external_mip_script
1112

    
1113
  @locking.ssynchronized(_config_lock, shared=1)
1114
  def GetFileStorageDir(self):
1115
    """Get the file storage dir for this cluster.
1116

1117
    """
1118
    return self._config_data.cluster.file_storage_dir
1119

    
1120
  @locking.ssynchronized(_config_lock, shared=1)
1121
  def GetSharedFileStorageDir(self):
1122
    """Get the shared file storage dir for this cluster.
1123

1124
    """
1125
    return self._config_data.cluster.shared_file_storage_dir
1126

    
1127
  @locking.ssynchronized(_config_lock, shared=1)
1128
  def GetHypervisorType(self):
1129
    """Get the hypervisor type for this cluster.
1130

1131
    """
1132
    return self._config_data.cluster.enabled_hypervisors[0]
1133

    
1134
  @locking.ssynchronized(_config_lock, shared=1)
1135
  def GetHostKey(self):
1136
    """Return the rsa hostkey from the config.
1137

1138
    @rtype: string
1139
    @return: the rsa hostkey
1140

1141
    """
1142
    return self._config_data.cluster.rsahostkeypub
1143

    
1144
  @locking.ssynchronized(_config_lock, shared=1)
1145
  def GetDefaultIAllocator(self):
1146
    """Get the default instance allocator for this cluster.
1147

1148
    """
1149
    return self._config_data.cluster.default_iallocator
1150

    
1151
  @locking.ssynchronized(_config_lock, shared=1)
1152
  def GetPrimaryIPFamily(self):
1153
    """Get cluster primary ip family.
1154

1155
    @return: primary ip family
1156

1157
    """
1158
    return self._config_data.cluster.primary_ip_family
1159

    
1160
  @locking.ssynchronized(_config_lock, shared=1)
1161
  def GetMasterNetworkParameters(self):
1162
    """Get network parameters of the master node.
1163

1164
    @rtype: L{object.MasterNetworkParameters}
1165
    @return: network parameters of the master node
1166

1167
    """
1168
    cluster = self._config_data.cluster
1169
    result = objects.MasterNetworkParameters(name=cluster.master_node,
1170
      ip=cluster.master_ip,
1171
      netmask=cluster.master_netmask,
1172
      netdev=cluster.master_netdev,
1173
      ip_family=cluster.primary_ip_family)
1174

    
1175
    return result
1176

    
1177
  @locking.ssynchronized(_config_lock)
1178
  def AddNodeGroup(self, group, ec_id, check_uuid=True):
1179
    """Add a node group to the configuration.
1180

1181
    This method calls group.UpgradeConfig() to fill any missing attributes
1182
    according to their default values.
1183

1184
    @type group: L{objects.NodeGroup}
1185
    @param group: the NodeGroup object to add
1186
    @type ec_id: string
1187
    @param ec_id: unique id for the job to use when creating a missing UUID
1188
    @type check_uuid: bool
1189
    @param check_uuid: add an UUID to the group if it doesn't have one or, if
1190
                       it does, ensure that it does not exist in the
1191
                       configuration already
1192

1193
    """
1194
    self._UnlockedAddNodeGroup(group, ec_id, check_uuid)
1195
    self._WriteConfig()
1196

    
1197
  def _UnlockedAddNodeGroup(self, group, ec_id, check_uuid):
1198
    """Add a node group to the configuration.
1199

1200
    """
1201
    logging.info("Adding node group %s to configuration", group.name)
1202

    
1203
    # Some code might need to add a node group with a pre-populated UUID
1204
    # generated with ConfigWriter.GenerateUniqueID(). We allow them to bypass
1205
    # the "does this UUID" exist already check.
1206
    if check_uuid:
1207
      self._EnsureUUID(group, ec_id)
1208

    
1209
    try:
1210
      existing_uuid = self._UnlockedLookupNodeGroup(group.name)
1211
    except errors.OpPrereqError:
1212
      pass
1213
    else:
1214
      raise errors.OpPrereqError("Desired group name '%s' already exists as a"
1215
                                 " node group (UUID: %s)" %
1216
                                 (group.name, existing_uuid),
1217
                                 errors.ECODE_EXISTS)
1218

    
1219
    group.serial_no = 1
1220
    group.ctime = group.mtime = time.time()
1221
    group.UpgradeConfig()
1222

    
1223
    self._config_data.nodegroups[group.uuid] = group
1224
    self._config_data.cluster.serial_no += 1
1225

    
1226
  @locking.ssynchronized(_config_lock)
1227
  def RemoveNodeGroup(self, group_uuid):
1228
    """Remove a node group from the configuration.
1229

1230
    @type group_uuid: string
1231
    @param group_uuid: the UUID of the node group to remove
1232

1233
    """
1234
    logging.info("Removing node group %s from configuration", group_uuid)
1235

    
1236
    if group_uuid not in self._config_data.nodegroups:
1237
      raise errors.ConfigurationError("Unknown node group '%s'" % group_uuid)
1238

    
1239
    assert len(self._config_data.nodegroups) != 1, \
1240
            "Group '%s' is the only group, cannot be removed" % group_uuid
1241

    
1242
    del self._config_data.nodegroups[group_uuid]
1243
    self._config_data.cluster.serial_no += 1
1244
    self._WriteConfig()
1245

    
1246
  def _UnlockedLookupNodeGroup(self, target):
1247
    """Lookup a node group's UUID.
1248

1249
    @type target: string or None
1250
    @param target: group name or UUID or None to look for the default
1251
    @rtype: string
1252
    @return: nodegroup UUID
1253
    @raises errors.OpPrereqError: when the target group cannot be found
1254

1255
    """
1256
    if target is None:
1257
      if len(self._config_data.nodegroups) != 1:
1258
        raise errors.OpPrereqError("More than one node group exists. Target"
1259
                                   " group must be specified explicitely.")
1260
      else:
1261
        return self._config_data.nodegroups.keys()[0]
1262
    if target in self._config_data.nodegroups:
1263
      return target
1264
    for nodegroup in self._config_data.nodegroups.values():
1265
      if nodegroup.name == target:
1266
        return nodegroup.uuid
1267
    raise errors.OpPrereqError("Node group '%s' not found" % target,
1268
                               errors.ECODE_NOENT)
1269

    
1270
  @locking.ssynchronized(_config_lock, shared=1)
1271
  def LookupNodeGroup(self, target):
1272
    """Lookup a node group's UUID.
1273

1274
    This function is just a wrapper over L{_UnlockedLookupNodeGroup}.
1275

1276
    @type target: string or None
1277
    @param target: group name or UUID or None to look for the default
1278
    @rtype: string
1279
    @return: nodegroup UUID
1280

1281
    """
1282
    return self._UnlockedLookupNodeGroup(target)
1283

    
1284
  def _UnlockedGetNodeGroup(self, uuid):
1285
    """Lookup a node group.
1286

1287
    @type uuid: string
1288
    @param uuid: group UUID
1289
    @rtype: L{objects.NodeGroup} or None
1290
    @return: nodegroup object, or None if not found
1291

1292
    """
1293
    if uuid not in self._config_data.nodegroups:
1294
      return None
1295

    
1296
    return self._config_data.nodegroups[uuid]
1297

    
1298
  @locking.ssynchronized(_config_lock, shared=1)
1299
  def GetNodeGroup(self, uuid):
1300
    """Lookup a node group.
1301

1302
    @type uuid: string
1303
    @param uuid: group UUID
1304
    @rtype: L{objects.NodeGroup} or None
1305
    @return: nodegroup object, or None if not found
1306

1307
    """
1308
    return self._UnlockedGetNodeGroup(uuid)
1309

    
1310
  @locking.ssynchronized(_config_lock, shared=1)
1311
  def GetAllNodeGroupsInfo(self):
1312
    """Get the configuration of all node groups.
1313

1314
    """
1315
    return dict(self._config_data.nodegroups)
1316

    
1317
  @locking.ssynchronized(_config_lock, shared=1)
1318
  def GetNodeGroupList(self):
1319
    """Get a list of node groups.
1320

1321
    """
1322
    return self._config_data.nodegroups.keys()
1323

    
1324
  @locking.ssynchronized(_config_lock, shared=1)
1325
  def GetNodeGroupNames(self):
1326
    """Get a list of node group names
1327

1328
    """
1329
    names = [group.name
1330
             for group in self._config_data.nodegroups.values()]
1331
    return names
1332

    
1333

    
1334
  @locking.ssynchronized(_config_lock, shared=1)
1335
  def GetNodeGroupMembersByNodes(self, nodes):
1336
    """Get nodes which are member in the same nodegroups as the given nodes.
1337

1338
    """
1339
    ngfn = lambda node_name: self._UnlockedGetNodeInfo(node_name).group
1340
    return frozenset(member_name
1341
                     for node_name in nodes
1342
                     for member_name in
1343
                       self._UnlockedGetNodeGroup(ngfn(node_name)).members)
1344

    
1345
  @locking.ssynchronized(_config_lock, shared=1)
1346
  def GetMultiNodeGroupInfo(self, group_uuids):
1347
    """Get the configuration of multiple node groups.
1348

1349
    @param group_uuids: List of node group UUIDs
1350
    @rtype: list
1351
    @return: List of tuples of (group_uuid, group_info)
1352

1353
    """
1354
    return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]
1355

    
1356
  @locking.ssynchronized(_config_lock)
1357
  def AddInstance(self, instance, ec_id):
1358
    """Add an instance to the config.
1359

1360
    This should be used after creating a new instance.
1361

1362
    @type instance: L{objects.Instance}
1363
    @param instance: the instance object
1364

1365
    """
1366
    if not isinstance(instance, objects.Instance):
1367
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
1368

    
1369
    if instance.disk_template != constants.DT_DISKLESS:
1370
      all_lvs = instance.MapLVsByNode()
1371
      logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
1372

    
1373
    all_macs = self._AllMACs()
1374
    for nic in instance.nics:
1375
      if nic.mac in all_macs:
1376
        raise errors.ConfigurationError("Cannot add instance %s:"
1377
                                        " MAC address '%s' already in use." %
1378
                                        (instance.name, nic.mac))
1379

    
1380
    self._EnsureUUID(instance, ec_id)
1381

    
1382
    instance.serial_no = 1
1383
    instance.ctime = instance.mtime = time.time()
1384
    self._config_data.instances[instance.name] = instance
1385
    self._config_data.cluster.serial_no += 1
1386
    self._UnlockedReleaseDRBDMinors(instance.name)
1387
    self._UnlockedCommitTemporaryIps(ec_id)
1388
    self._WriteConfig()
1389

    
1390
  def _EnsureUUID(self, item, ec_id):
1391
    """Ensures a given object has a valid UUID.
1392

1393
    @param item: the instance or node to be checked
1394
    @param ec_id: the execution context id for the uuid reservation
1395

1396
    """
1397
    if not item.uuid:
1398
      item.uuid = self._GenerateUniqueID(ec_id)
1399
    elif item.uuid in self._AllIDs(include_temporary=True):
1400
      raise errors.ConfigurationError("Cannot add '%s': UUID %s already"
1401
                                      " in use" % (item.name, item.uuid))
1402

    
1403
  def _SetInstanceStatus(self, instance_name, status):
1404
    """Set the instance's status to a given value.
1405

1406
    """
1407
    assert status in constants.ADMINST_ALL, \
1408
           "Invalid status '%s' passed to SetInstanceStatus" % (status,)
1409

    
1410
    if instance_name not in self._config_data.instances:
1411
      raise errors.ConfigurationError("Unknown instance '%s'" %
1412
                                      instance_name)
1413
    instance = self._config_data.instances[instance_name]
1414
    if instance.admin_state != status:
1415
      instance.admin_state = status
1416
      instance.serial_no += 1
1417
      instance.mtime = time.time()
1418
      self._WriteConfig()
1419

    
1420
  @locking.ssynchronized(_config_lock)
1421
  def MarkInstanceUp(self, instance_name):
1422
    """Mark the instance status to up in the config.
1423

1424
    """
1425
    self._SetInstanceStatus(instance_name, constants.ADMINST_UP)
1426

    
1427
  @locking.ssynchronized(_config_lock)
1428
  def MarkInstanceOffline(self, instance_name):
1429
    """Mark the instance status to down in the config.
1430

1431
    """
1432
    self._SetInstanceStatus(instance_name, constants.ADMINST_OFFLINE)
1433

    
1434
  @locking.ssynchronized(_config_lock)
1435
  def RemoveInstance(self, instance_name):
1436
    """Remove the instance from the configuration.
1437

1438
    """
1439
    if instance_name not in self._config_data.instances:
1440
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1441

    
1442
    # If a network port has been allocated to the instance,
1443
    # return it to the pool of free ports.
1444
    inst = self._config_data.instances[instance_name]
1445
    network_port = getattr(inst, "network_port", None)
1446
    if network_port is not None:
1447
      self._config_data.cluster.tcpudp_port_pool.add(network_port)
1448

    
1449
    instance = self._UnlockedGetInstanceInfo(instance_name)
1450

    
1451
    for nic in instance.nics:
1452
      if nic.network is not None and nic.ip is not None:
1453
        net_uuid = self._UnlockedLookupNetwork(nic.network)
1454
        if net_uuid:
1455
          # Return all IP addresses to the respective address pools
1456
          self._UnlockedCommitIp('release', net_uuid, nic.ip)
1457

    
1458

    
1459
    del self._config_data.instances[instance_name]
1460
    self._config_data.cluster.serial_no += 1
1461
    self._WriteConfig()
1462

    
1463
  @locking.ssynchronized(_config_lock)
1464
  def RenameInstance(self, old_name, new_name):
1465
    """Rename an instance.
1466

1467
    This needs to be done in ConfigWriter and not by RemoveInstance
1468
    combined with AddInstance as only we can guarantee an atomic
1469
    rename.
1470

1471
    """
1472
    if old_name not in self._config_data.instances:
1473
      raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
1474

    
1475
    # Operate on a copy to not loose instance object in case of a failure
1476
    inst = self._config_data.instances[old_name].Copy()
1477
    inst.name = new_name
1478

    
1479
    for (idx, disk) in enumerate(inst.disks):
1480
      if disk.dev_type == constants.LD_FILE:
1481
        # rename the file paths in logical and physical id
1482
        file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
1483
        disk.logical_id = (disk.logical_id[0],
1484
                           utils.PathJoin(file_storage_dir, inst.name,
1485
                                          "disk%s" % idx))
1486
        disk.physical_id = disk.logical_id
1487

    
1488
    # Actually replace instance object
1489
    del self._config_data.instances[old_name]
1490
    self._config_data.instances[inst.name] = inst
1491

    
1492
    # Force update of ssconf files
1493
    self._config_data.cluster.serial_no += 1
1494

    
1495
    self._WriteConfig()
1496

    
1497
  @locking.ssynchronized(_config_lock)
1498
  def MarkInstanceDown(self, instance_name):
1499
    """Mark the status of an instance to down in the configuration.
1500

1501
    """
1502
    self._SetInstanceStatus(instance_name, constants.ADMINST_DOWN)
1503

    
1504
  def _UnlockedGetInstanceList(self):
1505
    """Get the list of instances.
1506

1507
    This function is for internal use, when the config lock is already held.
1508

1509
    """
1510
    return self._config_data.instances.keys()
1511

    
1512
  @locking.ssynchronized(_config_lock, shared=1)
1513
  def GetInstanceList(self):
1514
    """Get the list of instances.
1515

1516
    @return: array of instances, ex. ['instance2.example.com',
1517
        'instance1.example.com']
1518

1519
    """
1520
    return self._UnlockedGetInstanceList()
1521

    
1522
  def ExpandInstanceName(self, short_name):
1523
    """Attempt to expand an incomplete instance name.
1524

1525
    """
1526
    # Locking is done in L{ConfigWriter.GetInstanceList}
1527
    return _MatchNameComponentIgnoreCase(short_name, self.GetInstanceList())
1528

    
1529
  def _UnlockedGetInstanceInfo(self, instance_name):
1530
    """Returns information about an instance.
1531

1532
    This function is for internal use, when the config lock is already held.
1533

1534
    """
1535
    if instance_name not in self._config_data.instances:
1536
      return None
1537

    
1538
    return self._config_data.instances[instance_name]
1539

    
1540
  @locking.ssynchronized(_config_lock, shared=1)
1541
  def GetInstanceInfo(self, instance_name):
1542
    """Returns information about an instance.
1543

1544
    It takes the information from the configuration file. Other information of
1545
    an instance are taken from the live systems.
1546

1547
    @param instance_name: name of the instance, e.g.
1548
        I{instance1.example.com}
1549

1550
    @rtype: L{objects.Instance}
1551
    @return: the instance object
1552

1553
    """
1554
    return self._UnlockedGetInstanceInfo(instance_name)
1555

    
1556
  @locking.ssynchronized(_config_lock, shared=1)
1557
  def GetInstanceNodeGroups(self, instance_name, primary_only=False):
1558
    """Returns set of node group UUIDs for instance's nodes.
1559

1560
    @rtype: frozenset
1561

1562
    """
1563
    instance = self._UnlockedGetInstanceInfo(instance_name)
1564
    if not instance:
1565
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1566

    
1567
    if primary_only:
1568
      nodes = [instance.primary_node]
1569
    else:
1570
      nodes = instance.all_nodes
1571

    
1572
    return frozenset(self._UnlockedGetNodeInfo(node_name).group
1573
                     for node_name in nodes)
1574

    
1575
  @locking.ssynchronized(_config_lock, shared=1)
1576
  def GetMultiInstanceInfo(self, instances):
1577
    """Get the configuration of multiple instances.
1578

1579
    @param instances: list of instance names
1580
    @rtype: list
1581
    @return: list of tuples (instance, instance_info), where
1582
        instance_info is what would GetInstanceInfo return for the
1583
        node, while keeping the original order
1584

1585
    """
1586
    return [(name, self._UnlockedGetInstanceInfo(name)) for name in instances]
1587

    
1588
  @locking.ssynchronized(_config_lock, shared=1)
1589
  def GetAllInstancesInfo(self):
1590
    """Get the configuration of all instances.
1591

1592
    @rtype: dict
1593
    @return: dict of (instance, instance_info), where instance_info is what
1594
              would GetInstanceInfo return for the node
1595

1596
    """
1597
    my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance))
1598
                    for instance in self._UnlockedGetInstanceList()])
1599
    return my_dict
1600

    
1601
  @locking.ssynchronized(_config_lock, shared=1)
1602
  def GetInstancesInfoByFilter(self, filter_fn):
1603
    """Get instance configuration with a filter.
1604

1605
    @type filter_fn: callable
1606
    @param filter_fn: Filter function receiving instance object as parameter,
1607
      returning boolean. Important: this function is called while the
1608
      configuration locks is held. It must not do any complex work or call
1609
      functions potentially leading to a deadlock. Ideally it doesn't call any
1610
      other functions and just compares instance attributes.
1611

1612
    """
1613
    return dict((name, inst)
1614
                for (name, inst) in self._config_data.instances.items()
1615
                if filter_fn(inst))
1616

    
1617
  @locking.ssynchronized(_config_lock)
1618
  def AddNode(self, node, ec_id):
1619
    """Add a node to the configuration.
1620

1621
    @type node: L{objects.Node}
1622
    @param node: a Node instance
1623

1624
    """
1625
    logging.info("Adding node %s to configuration", node.name)
1626

    
1627
    self._EnsureUUID(node, ec_id)
1628

    
1629
    node.serial_no = 1
1630
    node.ctime = node.mtime = time.time()
1631
    self._UnlockedAddNodeToGroup(node.name, node.group)
1632
    self._config_data.nodes[node.name] = node
1633
    self._config_data.cluster.serial_no += 1
1634
    self._WriteConfig()
1635

    
1636
  @locking.ssynchronized(_config_lock)
1637
  def RemoveNode(self, node_name):
1638
    """Remove a node from the configuration.
1639

1640
    """
1641
    logging.info("Removing node %s from configuration", node_name)
1642

    
1643
    if node_name not in self._config_data.nodes:
1644
      raise errors.ConfigurationError("Unknown node '%s'" % node_name)
1645

    
1646
    self._UnlockedRemoveNodeFromGroup(self._config_data.nodes[node_name])
1647
    del self._config_data.nodes[node_name]
1648
    self._config_data.cluster.serial_no += 1
1649
    self._WriteConfig()
1650

    
1651
  def ExpandNodeName(self, short_name):
1652
    """Attempt to expand an incomplete node name.
1653

1654
    """
1655
    # Locking is done in L{ConfigWriter.GetNodeList}
1656
    return _MatchNameComponentIgnoreCase(short_name, self.GetNodeList())
1657

    
1658
  def _UnlockedGetNodeInfo(self, node_name):
1659
    """Get the configuration of a node, as stored in the config.
1660

1661
    This function is for internal use, when the config lock is already
1662
    held.
1663

1664
    @param node_name: the node name, e.g. I{node1.example.com}
1665

1666
    @rtype: L{objects.Node}
1667
    @return: the node object
1668

1669
    """
1670
    if node_name not in self._config_data.nodes:
1671
      return None
1672

    
1673
    return self._config_data.nodes[node_name]
1674

    
1675
  @locking.ssynchronized(_config_lock, shared=1)
1676
  def GetNodeInfo(self, node_name):
1677
    """Get the configuration of a node, as stored in the config.
1678

1679
    This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
1680

1681
    @param node_name: the node name, e.g. I{node1.example.com}
1682

1683
    @rtype: L{objects.Node}
1684
    @return: the node object
1685

1686
    """
1687
    return self._UnlockedGetNodeInfo(node_name)
1688

    
1689
  @locking.ssynchronized(_config_lock, shared=1)
1690
  def GetNodeInstances(self, node_name):
1691
    """Get the instances of a node, as stored in the config.
1692

1693
    @param node_name: the node name, e.g. I{node1.example.com}
1694

1695
    @rtype: (list, list)
1696
    @return: a tuple with two lists: the primary and the secondary instances
1697

1698
    """
1699
    pri = []
1700
    sec = []
1701
    for inst in self._config_data.instances.values():
1702
      if inst.primary_node == node_name:
1703
        pri.append(inst.name)
1704
      if node_name in inst.secondary_nodes:
1705
        sec.append(inst.name)
1706
    return (pri, sec)
1707

    
1708
  @locking.ssynchronized(_config_lock, shared=1)
1709
  def GetNodeGroupInstances(self, uuid, primary_only=False):
1710
    """Get the instances of a node group.
1711

1712
    @param uuid: Node group UUID
1713
    @param primary_only: Whether to only consider primary nodes
1714
    @rtype: frozenset
1715
    @return: List of instance names in node group
1716

1717
    """
1718
    if primary_only:
1719
      nodes_fn = lambda inst: [inst.primary_node]
1720
    else:
1721
      nodes_fn = lambda inst: inst.all_nodes
1722

    
1723
    return frozenset(inst.name
1724
                     for inst in self._config_data.instances.values()
1725
                     for node_name in nodes_fn(inst)
1726
                     if self._UnlockedGetNodeInfo(node_name).group == uuid)
1727

    
1728
  def _UnlockedGetNodeList(self):
1729
    """Return the list of nodes which are in the configuration.
1730

1731
    This function is for internal use, when the config lock is already
1732
    held.
1733

1734
    @rtype: list
1735

1736
    """
1737
    return self._config_data.nodes.keys()
1738

    
1739
  @locking.ssynchronized(_config_lock, shared=1)
1740
  def GetNodeList(self):
1741
    """Return the list of nodes which are in the configuration.
1742

1743
    """
1744
    return self._UnlockedGetNodeList()
1745

    
1746
  def _UnlockedGetOnlineNodeList(self):
1747
    """Return the list of nodes which are online.
1748

1749
    """
1750
    all_nodes = [self._UnlockedGetNodeInfo(node)
1751
                 for node in self._UnlockedGetNodeList()]
1752
    return [node.name for node in all_nodes if not node.offline]
1753

    
1754
  @locking.ssynchronized(_config_lock, shared=1)
1755
  def GetOnlineNodeList(self):
1756
    """Return the list of nodes which are online.
1757

1758
    """
1759
    return self._UnlockedGetOnlineNodeList()
1760

    
1761
  @locking.ssynchronized(_config_lock, shared=1)
1762
  def GetVmCapableNodeList(self):
1763
    """Return the list of nodes which are not vm capable.
1764

1765
    """
1766
    all_nodes = [self._UnlockedGetNodeInfo(node)
1767
                 for node in self._UnlockedGetNodeList()]
1768
    return [node.name for node in all_nodes if node.vm_capable]
1769

    
1770
  @locking.ssynchronized(_config_lock, shared=1)
1771
  def GetNonVmCapableNodeList(self):
1772
    """Return the list of nodes which are not vm capable.
1773

1774
    """
1775
    all_nodes = [self._UnlockedGetNodeInfo(node)
1776
                 for node in self._UnlockedGetNodeList()]
1777
    return [node.name for node in all_nodes if not node.vm_capable]
1778

    
1779
  @locking.ssynchronized(_config_lock, shared=1)
1780
  def GetMultiNodeInfo(self, nodes):
1781
    """Get the configuration of multiple nodes.
1782

1783
    @param nodes: list of node names
1784
    @rtype: list
1785
    @return: list of tuples of (node, node_info), where node_info is
1786
        what would GetNodeInfo return for the node, in the original
1787
        order
1788

1789
    """
1790
    return [(name, self._UnlockedGetNodeInfo(name)) for name in nodes]
1791

    
1792
  @locking.ssynchronized(_config_lock, shared=1)
1793
  def GetAllNodesInfo(self):
1794
    """Get the configuration of all nodes.
1795

1796
    @rtype: dict
1797
    @return: dict of (node, node_info), where node_info is what
1798
              would GetNodeInfo return for the node
1799

1800
    """
1801
    return self._UnlockedGetAllNodesInfo()
1802

    
1803
  def _UnlockedGetAllNodesInfo(self):
1804
    """Gets configuration of all nodes.
1805

1806
    @note: See L{GetAllNodesInfo}
1807

1808
    """
1809
    return dict([(node, self._UnlockedGetNodeInfo(node))
1810
                 for node in self._UnlockedGetNodeList()])
1811

    
1812
  @locking.ssynchronized(_config_lock, shared=1)
1813
  def GetNodeGroupsFromNodes(self, nodes):
1814
    """Returns groups for a list of nodes.
1815

1816
    @type nodes: list of string
1817
    @param nodes: List of node names
1818
    @rtype: frozenset
1819

1820
    """
1821
    return frozenset(self._UnlockedGetNodeInfo(name).group for name in nodes)
1822

    
1823
  def _UnlockedGetMasterCandidateStats(self, exceptions=None):
1824
    """Get the number of current and maximum desired and possible candidates.
1825

1826
    @type exceptions: list
1827
    @param exceptions: if passed, list of nodes that should be ignored
1828
    @rtype: tuple
1829
    @return: tuple of (current, desired and possible, possible)
1830

1831
    """
1832
    mc_now = mc_should = mc_max = 0
1833
    for node in self._config_data.nodes.values():
1834
      if exceptions and node.name in exceptions:
1835
        continue
1836
      if not (node.offline or node.drained) and node.master_capable:
1837
        mc_max += 1
1838
      if node.master_candidate:
1839
        mc_now += 1
1840
    mc_should = min(mc_max, self._config_data.cluster.candidate_pool_size)
1841
    return (mc_now, mc_should, mc_max)
1842

    
1843
  @locking.ssynchronized(_config_lock, shared=1)
1844
  def GetMasterCandidateStats(self, exceptions=None):
1845
    """Get the number of current and maximum possible candidates.
1846

1847
    This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
1848

1849
    @type exceptions: list
1850
    @param exceptions: if passed, list of nodes that should be ignored
1851
    @rtype: tuple
1852
    @return: tuple of (current, max)
1853

1854
    """
1855
    return self._UnlockedGetMasterCandidateStats(exceptions)
1856

    
1857
  @locking.ssynchronized(_config_lock)
1858
  def MaintainCandidatePool(self, exceptions):
1859
    """Try to grow the candidate pool to the desired size.
1860

1861
    @type exceptions: list
1862
    @param exceptions: if passed, list of nodes that should be ignored
1863
    @rtype: list
1864
    @return: list with the adjusted nodes (L{objects.Node} instances)
1865

1866
    """
1867
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats(exceptions)
1868
    mod_list = []
1869
    if mc_now < mc_max:
1870
      node_list = self._config_data.nodes.keys()
1871
      random.shuffle(node_list)
1872
      for name in node_list:
1873
        if mc_now >= mc_max:
1874
          break
1875
        node = self._config_data.nodes[name]
1876
        if (node.master_candidate or node.offline or node.drained or
1877
            node.name in exceptions or not node.master_capable):
1878
          continue
1879
        mod_list.append(node)
1880
        node.master_candidate = True
1881
        node.serial_no += 1
1882
        mc_now += 1
1883
      if mc_now != mc_max:
1884
        # this should not happen
1885
        logging.warning("Warning: MaintainCandidatePool didn't manage to"
1886
                        " fill the candidate pool (%d/%d)", mc_now, mc_max)
1887
      if mod_list:
1888
        self._config_data.cluster.serial_no += 1
1889
        self._WriteConfig()
1890

    
1891
    return mod_list
1892

    
1893
  def _UnlockedAddNodeToGroup(self, node_name, nodegroup_uuid):
1894
    """Add a given node to the specified group.
1895

1896
    """
1897
    if nodegroup_uuid not in self._config_data.nodegroups:
1898
      # This can happen if a node group gets deleted between its lookup and
1899
      # when we're adding the first node to it, since we don't keep a lock in
1900
      # the meantime. It's ok though, as we'll fail cleanly if the node group
1901
      # is not found anymore.
1902
      raise errors.OpExecError("Unknown node group: %s" % nodegroup_uuid)
1903
    if node_name not in self._config_data.nodegroups[nodegroup_uuid].members:
1904
      self._config_data.nodegroups[nodegroup_uuid].members.append(node_name)
1905

    
1906
  def _UnlockedRemoveNodeFromGroup(self, node):
1907
    """Remove a given node from its group.
1908

1909
    """
1910
    nodegroup = node.group
1911
    if nodegroup not in self._config_data.nodegroups:
1912
      logging.warning("Warning: node '%s' has unknown node group '%s'"
1913
                      " (while being removed from it)", node.name, nodegroup)
1914
    nodegroup_obj = self._config_data.nodegroups[nodegroup]
1915
    if node.name not in nodegroup_obj.members:
1916
      logging.warning("Warning: node '%s' not a member of its node group '%s'"
1917
                      " (while being removed from it)", node.name, nodegroup)
1918
    else:
1919
      nodegroup_obj.members.remove(node.name)
1920

    
1921
  @locking.ssynchronized(_config_lock)
1922
  def AssignGroupNodes(self, mods):
1923
    """Changes the group of a number of nodes.
1924

1925
    @type mods: list of tuples; (node name, new group UUID)
1926
    @param mods: Node membership modifications
1927

1928
    """
1929
    groups = self._config_data.nodegroups
1930
    nodes = self._config_data.nodes
1931

    
1932
    resmod = []
1933

    
1934
    # Try to resolve names/UUIDs first
1935
    for (node_name, new_group_uuid) in mods:
1936
      try:
1937
        node = nodes[node_name]
1938
      except KeyError:
1939
        raise errors.ConfigurationError("Unable to find node '%s'" % node_name)
1940

    
1941
      if node.group == new_group_uuid:
1942
        # Node is being assigned to its current group
1943
        logging.debug("Node '%s' was assigned to its current group (%s)",
1944
                      node_name, node.group)
1945
        continue
1946

    
1947
      # Try to find current group of node
1948
      try:
1949
        old_group = groups[node.group]
1950
      except KeyError:
1951
        raise errors.ConfigurationError("Unable to find old group '%s'" %
1952
                                        node.group)
1953

    
1954
      # Try to find new group for node
1955
      try:
1956
        new_group = groups[new_group_uuid]
1957
      except KeyError:
1958
        raise errors.ConfigurationError("Unable to find new group '%s'" %
1959
                                        new_group_uuid)
1960

    
1961
      assert node.name in old_group.members, \
1962
        ("Inconsistent configuration: node '%s' not listed in members for its"
1963
         " old group '%s'" % (node.name, old_group.uuid))
1964
      assert node.name not in new_group.members, \
1965
        ("Inconsistent configuration: node '%s' already listed in members for"
1966
         " its new group '%s'" % (node.name, new_group.uuid))
1967

    
1968
      resmod.append((node, old_group, new_group))
1969

    
1970
    # Apply changes
1971
    for (node, old_group, new_group) in resmod:
1972
      assert node.uuid != new_group.uuid and old_group.uuid != new_group.uuid, \
1973
        "Assigning to current group is not possible"
1974

    
1975
      node.group = new_group.uuid
1976

    
1977
      # Update members of involved groups
1978
      if node.name in old_group.members:
1979
        old_group.members.remove(node.name)
1980
      if node.name not in new_group.members:
1981
        new_group.members.append(node.name)
1982

    
1983
    # Update timestamps and serials (only once per node/group object)
1984
    now = time.time()
1985
    for obj in frozenset(itertools.chain(*resmod)): # pylint: disable=W0142
1986
      obj.serial_no += 1
1987
      obj.mtime = now
1988

    
1989
    # Force ssconf update
1990
    self._config_data.cluster.serial_no += 1
1991

    
1992
    self._WriteConfig()
1993

    
1994
  def _BumpSerialNo(self):
1995
    """Bump up the serial number of the config.
1996

1997
    """
1998
    self._config_data.serial_no += 1
1999
    self._config_data.mtime = time.time()
2000

    
2001
  def _AllUUIDObjects(self):
2002
    """Returns all objects with uuid attributes.
2003

2004
    """
2005
    return (self._config_data.instances.values() +
2006
            self._config_data.nodes.values() +
2007
            self._config_data.nodegroups.values() +
2008
            [self._config_data.cluster])
2009

    
2010
  def _OpenConfig(self, accept_foreign):
2011
    """Read the config data from disk.
2012

2013
    """
2014
    raw_data = utils.ReadFile(self._cfg_file)
2015

    
2016
    try:
2017
      data = objects.ConfigData.FromDict(serializer.Load(raw_data))
2018
    except Exception, err:
2019
      raise errors.ConfigurationError(err)
2020

    
2021
    # Make sure the configuration has the right version
2022
    _ValidateConfig(data)
2023

    
2024
    if (not hasattr(data, "cluster") or
2025
        not hasattr(data.cluster, "rsahostkeypub")):
2026
      raise errors.ConfigurationError("Incomplete configuration"
2027
                                      " (missing cluster.rsahostkeypub)")
2028

    
2029
    if data.cluster.master_node != self._my_hostname and not accept_foreign:
2030
      msg = ("The configuration denotes node %s as master, while my"
2031
             " hostname is %s; opening a foreign configuration is only"
2032
             " possible in accept_foreign mode" %
2033
             (data.cluster.master_node, self._my_hostname))
2034
      raise errors.ConfigurationError(msg)
2035

    
2036
    # Upgrade configuration if needed
2037
    data.UpgradeConfig()
2038

    
2039
    self._config_data = data
2040
    # reset the last serial as -1 so that the next write will cause
2041
    # ssconf update
2042
    self._last_cluster_serial = -1
2043

    
2044
    # And finally run our (custom) config upgrade sequence
2045
    self._UpgradeConfig()
2046

    
2047
    self._cfg_id = utils.GetFileID(path=self._cfg_file)
2048

    
2049
  def _UpgradeConfig(self):
2050
    """Run upgrade steps that cannot be done purely in the objects.
2051

2052
    This is because some data elements need uniqueness across the
2053
    whole configuration, etc.
2054

2055
    @warning: this function will call L{_WriteConfig()}, but also
2056
        L{DropECReservations} so it needs to be called only from a
2057
        "safe" place (the constructor). If one wanted to call it with
2058
        the lock held, a DropECReservationUnlocked would need to be
2059
        created first, to avoid causing deadlock.
2060

2061
    """
2062
    modified = False
2063
    for item in self._AllUUIDObjects():
2064
      if item.uuid is None:
2065
        item.uuid = self._GenerateUniqueID(_UPGRADE_CONFIG_JID)
2066
        modified = True
2067
    if not self._config_data.nodegroups:
2068
      default_nodegroup_name = constants.INITIAL_NODE_GROUP_NAME
2069
      default_nodegroup = objects.NodeGroup(name=default_nodegroup_name,
2070
                                            members=[])
2071
      self._UnlockedAddNodeGroup(default_nodegroup, _UPGRADE_CONFIG_JID, True)
2072
      modified = True
2073
    for node in self._config_data.nodes.values():
2074
      if not node.group:
2075
        node.group = self.LookupNodeGroup(None)
2076
        modified = True
2077
      # This is technically *not* an upgrade, but needs to be done both when
2078
      # nodegroups are being added, and upon normally loading the config,
2079
      # because the members list of a node group is discarded upon
2080
      # serializing/deserializing the object.
2081
      self._UnlockedAddNodeToGroup(node.name, node.group)
2082
    if modified:
2083
      self._WriteConfig()
2084
      # This is ok even if it acquires the internal lock, as _UpgradeConfig is
2085
      # only called at config init time, without the lock held
2086
      self.DropECReservations(_UPGRADE_CONFIG_JID)
2087

    
2088
  def _DistributeConfig(self, feedback_fn):
2089
    """Distribute the configuration to the other nodes.
2090

2091
    Currently, this only copies the configuration file. In the future,
2092
    it could be used to encapsulate the 2/3-phase update mechanism.
2093

2094
    """
2095
    if self._offline:
2096
      return True
2097

    
2098
    bad = False
2099

    
2100
    node_list = []
2101
    addr_list = []
2102
    myhostname = self._my_hostname
2103
    # we can skip checking whether _UnlockedGetNodeInfo returns None
2104
    # since the node list comes from _UnlocketGetNodeList, and we are
2105
    # called with the lock held, so no modifications should take place
2106
    # in between
2107
    for node_name in self._UnlockedGetNodeList():
2108
      if node_name == myhostname:
2109
        continue
2110
      node_info = self._UnlockedGetNodeInfo(node_name)
2111
      if not node_info.master_candidate:
2112
        continue
2113
      node_list.append(node_info.name)
2114
      addr_list.append(node_info.primary_ip)
2115

    
2116
    # TODO: Use dedicated resolver talking to config writer for name resolution
2117
    result = \
2118
      self._GetRpc(addr_list).call_upload_file(node_list, self._cfg_file)
2119
    for to_node, to_result in result.items():
2120
      msg = to_result.fail_msg
2121
      if msg:
2122
        msg = ("Copy of file %s to node %s failed: %s" %
2123
               (self._cfg_file, to_node, msg))
2124
        logging.error(msg)
2125

    
2126
        if feedback_fn:
2127
          feedback_fn(msg)
2128

    
2129
        bad = True
2130

    
2131
    return not bad
2132

    
2133
  def _WriteConfig(self, destination=None, feedback_fn=None):
2134
    """Write the configuration data to persistent storage.
2135

2136
    """
2137
    assert feedback_fn is None or callable(feedback_fn)
2138

    
2139
    # Warn on config errors, but don't abort the save - the
2140
    # configuration has already been modified, and we can't revert;
2141
    # the best we can do is to warn the user and save as is, leaving
2142
    # recovery to the user
2143
    config_errors = self._UnlockedVerifyConfig()
2144
    if config_errors:
2145
      errmsg = ("Configuration data is not consistent: %s" %
2146
                (utils.CommaJoin(config_errors)))
2147
      logging.critical(errmsg)
2148
      if feedback_fn:
2149
        feedback_fn(errmsg)
2150

    
2151
    if destination is None:
2152
      destination = self._cfg_file
2153
    self._BumpSerialNo()
2154
    txt = serializer.Dump(self._config_data.ToDict())
2155

    
2156
    getents = self._getents()
2157
    try:
2158
      fd = utils.SafeWriteFile(destination, self._cfg_id, data=txt,
2159
                               close=False, gid=getents.confd_gid, mode=0640)
2160
    except errors.LockError:
2161
      raise errors.ConfigurationError("The configuration file has been"
2162
                                      " modified since the last write, cannot"
2163
                                      " update")
2164
    try:
2165
      self._cfg_id = utils.GetFileID(fd=fd)
2166
    finally:
2167
      os.close(fd)
2168

    
2169
    self.write_count += 1
2170

    
2171
    # and redistribute the config file to master candidates
2172
    self._DistributeConfig(feedback_fn)
2173

    
2174
    # Write ssconf files on all nodes (including locally)
2175
    if self._last_cluster_serial < self._config_data.cluster.serial_no:
2176
      if not self._offline:
2177
        result = self._GetRpc(None).call_write_ssconf_files(
2178
          self._UnlockedGetOnlineNodeList(),
2179
          self._UnlockedGetSsconfValues())
2180

    
2181
        for nname, nresu in result.items():
2182
          msg = nresu.fail_msg
2183
          if msg:
2184
            errmsg = ("Error while uploading ssconf files to"
2185
                      " node %s: %s" % (nname, msg))
2186
            logging.warning(errmsg)
2187

    
2188
            if feedback_fn:
2189
              feedback_fn(errmsg)
2190

    
2191
      self._last_cluster_serial = self._config_data.cluster.serial_no
2192

    
2193
  def _UnlockedGetSsconfValues(self):
2194
    """Return the values needed by ssconf.
2195

2196
    @rtype: dict
2197
    @return: a dictionary with keys the ssconf names and values their
2198
        associated value
2199

2200
    """
2201
    fn = "\n".join
2202
    instance_names = utils.NiceSort(self._UnlockedGetInstanceList())
2203
    node_names = utils.NiceSort(self._UnlockedGetNodeList())
2204
    node_info = [self._UnlockedGetNodeInfo(name) for name in node_names]
2205
    node_pri_ips = ["%s %s" % (ninfo.name, ninfo.primary_ip)
2206
                    for ninfo in node_info]
2207
    node_snd_ips = ["%s %s" % (ninfo.name, ninfo.secondary_ip)
2208
                    for ninfo in node_info]
2209

    
2210
    instance_data = fn(instance_names)
2211
    off_data = fn(node.name for node in node_info if node.offline)
2212
    on_data = fn(node.name for node in node_info if not node.offline)
2213
    mc_data = fn(node.name for node in node_info if node.master_candidate)
2214
    mc_ips_data = fn(node.primary_ip for node in node_info
2215
                     if node.master_candidate)
2216
    node_data = fn(node_names)
2217
    node_pri_ips_data = fn(node_pri_ips)
2218
    node_snd_ips_data = fn(node_snd_ips)
2219

    
2220
    cluster = self._config_data.cluster
2221
    cluster_tags = fn(cluster.GetTags())
2222

    
2223
    hypervisor_list = fn(cluster.enabled_hypervisors)
2224

    
2225
    uid_pool = uidpool.FormatUidPool(cluster.uid_pool, separator="\n")
2226

    
2227
    nodegroups = ["%s %s" % (nodegroup.uuid, nodegroup.name) for nodegroup in
2228
                  self._config_data.nodegroups.values()]
2229
    nodegroups_data = fn(utils.NiceSort(nodegroups))
2230
    networks = ["%s %s" % (net.uuid, net.name) for net in
2231
                self._config_data.networks.values()]
2232
    networks_data = fn(utils.NiceSort(networks))
2233

    
2234
    ssconf_values = {
2235
      constants.SS_CLUSTER_NAME: cluster.cluster_name,
2236
      constants.SS_CLUSTER_TAGS: cluster_tags,
2237
      constants.SS_FILE_STORAGE_DIR: cluster.file_storage_dir,
2238
      constants.SS_SHARED_FILE_STORAGE_DIR: cluster.shared_file_storage_dir,
2239
      constants.SS_MASTER_CANDIDATES: mc_data,
2240
      constants.SS_MASTER_CANDIDATES_IPS: mc_ips_data,
2241
      constants.SS_MASTER_IP: cluster.master_ip,
2242
      constants.SS_MASTER_NETDEV: cluster.master_netdev,
2243
      constants.SS_MASTER_NETMASK: str(cluster.master_netmask),
2244
      constants.SS_MASTER_NODE: cluster.master_node,
2245
      constants.SS_NODE_LIST: node_data,
2246
      constants.SS_NODE_PRIMARY_IPS: node_pri_ips_data,
2247
      constants.SS_NODE_SECONDARY_IPS: node_snd_ips_data,
2248
      constants.SS_OFFLINE_NODES: off_data,
2249
      constants.SS_ONLINE_NODES: on_data,
2250
      constants.SS_PRIMARY_IP_FAMILY: str(cluster.primary_ip_family),
2251
      constants.SS_INSTANCE_LIST: instance_data,
2252
      constants.SS_RELEASE_VERSION: constants.RELEASE_VERSION,
2253
      constants.SS_HYPERVISOR_LIST: hypervisor_list,
2254
      constants.SS_MAINTAIN_NODE_HEALTH: str(cluster.maintain_node_health),
2255
      constants.SS_UID_POOL: uid_pool,
2256
      constants.SS_NODEGROUPS: nodegroups_data,
2257
      constants.SS_NETWORKS: networks_data,
2258
      }
2259
    bad_values = [(k, v) for k, v in ssconf_values.items()
2260
                  if not isinstance(v, (str, basestring))]
2261
    if bad_values:
2262
      err = utils.CommaJoin("%s=%s" % (k, v) for k, v in bad_values)
2263
      raise errors.ConfigurationError("Some ssconf key(s) have non-string"
2264
                                      " values: %s" % err)
2265
    return ssconf_values
2266

    
2267
  @locking.ssynchronized(_config_lock, shared=1)
2268
  def GetSsconfValues(self):
2269
    """Wrapper using lock around _UnlockedGetSsconf().
2270

2271
    """
2272
    return self._UnlockedGetSsconfValues()
2273

    
2274
  @locking.ssynchronized(_config_lock, shared=1)
2275
  def GetVGName(self):
2276
    """Return the volume group name.
2277

2278
    """
2279
    return self._config_data.cluster.volume_group_name
2280

    
2281
  @locking.ssynchronized(_config_lock)
2282
  def SetVGName(self, vg_name):
2283
    """Set the volume group name.
2284

2285
    """
2286
    self._config_data.cluster.volume_group_name = vg_name
2287
    self._config_data.cluster.serial_no += 1
2288
    self._WriteConfig()
2289

    
2290
  @locking.ssynchronized(_config_lock, shared=1)
2291
  def GetDRBDHelper(self):
2292
    """Return DRBD usermode helper.
2293

2294
    """
2295
    return self._config_data.cluster.drbd_usermode_helper
2296

    
2297
  @locking.ssynchronized(_config_lock)
2298
  def SetDRBDHelper(self, drbd_helper):
2299
    """Set DRBD usermode helper.
2300

2301
    """
2302
    self._config_data.cluster.drbd_usermode_helper = drbd_helper
2303
    self._config_data.cluster.serial_no += 1
2304
    self._WriteConfig()
2305

    
2306
  @locking.ssynchronized(_config_lock, shared=1)
2307
  def GetMACPrefix(self):
2308
    """Return the mac prefix.
2309

2310
    """
2311
    return self._config_data.cluster.mac_prefix
2312

    
2313
  @locking.ssynchronized(_config_lock, shared=1)
2314
  def GetClusterInfo(self):
2315
    """Returns information about the cluster
2316

2317
    @rtype: L{objects.Cluster}
2318
    @return: the cluster object
2319

2320
    """
2321
    return self._config_data.cluster
2322

    
2323
  @locking.ssynchronized(_config_lock, shared=1)
2324
  def HasAnyDiskOfType(self, dev_type):
2325
    """Check if in there is at disk of the given type in the configuration.
2326

2327
    """
2328
    return self._config_data.HasAnyDiskOfType(dev_type)
2329

    
2330
  @locking.ssynchronized(_config_lock)
2331
  def Update(self, target, feedback_fn, ec_id=None):
2332
    """Notify function to be called after updates.
2333

2334
    This function must be called when an object (as returned by
2335
    GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
2336
    caller wants the modifications saved to the backing store. Note
2337
    that all modified objects will be saved, but the target argument
2338
    is the one the caller wants to ensure that it's saved.
2339

2340
    @param target: an instance of either L{objects.Cluster},
2341
        L{objects.Node} or L{objects.Instance} which is existing in
2342
        the cluster
2343
    @param feedback_fn: Callable feedback function
2344

2345
    """
2346
    if self._config_data is None:
2347
      raise errors.ProgrammerError("Configuration file not read,"
2348
                                   " cannot save.")
2349
    update_serial = False
2350
    if isinstance(target, objects.Cluster):
2351
      test = target == self._config_data.cluster
2352
    elif isinstance(target, objects.Node):
2353
      test = target in self._config_data.nodes.values()
2354
      update_serial = True
2355
    elif isinstance(target, objects.Instance):
2356
      test = target in self._config_data.instances.values()
2357
    elif isinstance(target, objects.NodeGroup):
2358
      test = target in self._config_data.nodegroups.values()
2359
    elif isinstance(target, objects.Network):
2360
      test = target in self._config_data.networks.values()
2361
    else:
2362
      raise errors.ProgrammerError("Invalid object type (%s) passed to"
2363
                                   " ConfigWriter.Update" % type(target))
2364
    if not test:
2365
      raise errors.ConfigurationError("Configuration updated since object"
2366
                                      " has been read or unknown object")
2367
    target.serial_no += 1
2368
    target.mtime = now = time.time()
2369

    
2370
    if update_serial:
2371
      # for node updates, we need to increase the cluster serial too
2372
      self._config_data.cluster.serial_no += 1
2373
      self._config_data.cluster.mtime = now
2374

    
2375
    if isinstance(target, objects.Instance):
2376
      self._UnlockedReleaseDRBDMinors(target.name)
2377

    
2378
    if ec_id is not None:
2379
      # Commit all ips reserved by OpInstanceSetParams and OpGroupSetParams
2380
      self._UnlockedCommitTemporaryIps(ec_id)
2381

    
2382
    self._WriteConfig(feedback_fn=feedback_fn)
2383

    
2384
  @locking.ssynchronized(_config_lock)
2385
  def DropECReservations(self, ec_id):
2386
    """Drop per-execution-context reservations
2387

2388
    """
2389
    for rm in self._all_rms:
2390
      rm.DropECReservations(ec_id)
2391

    
2392
  @locking.ssynchronized(_config_lock, shared=1)
2393
  def GetAllNetworksInfo(self):
2394
    """Get the configuration of all networks
2395

2396
    """
2397
    return dict(self._config_data.networks)
2398

    
2399
  def _UnlockedGetNetworkList(self):
2400
    """Get the list of networks.
2401

2402
    This function is for internal use, when the config lock is already held.
2403

2404
    """
2405
    return self._config_data.networks.keys()
2406

    
2407
  @locking.ssynchronized(_config_lock, shared=1)
2408
  def GetNetworkList(self):
2409
    """Get the list of networks.
2410

2411
    @return: array of networks, ex. ["main", "vlan100", "200]
2412

2413
    """
2414
    return self._UnlockedGetNetworkList()
2415

    
2416
  @locking.ssynchronized(_config_lock, shared=1)
2417
  def GetNetworkNames(self):
2418
    """Get a list of network names
2419

2420
    """
2421
    names = [network.name
2422
             for network in self._config_data.networks.values()]
2423
    return names
2424

    
2425
  def _UnlockedGetNetwork(self, uuid):
2426
    """Returns information about a network.
2427

2428
    This function is for internal use, when the config lock is already held.
2429

2430
    """
2431
    if uuid not in self._config_data.networks:
2432
      return None
2433

    
2434
    return self._config_data.networks[uuid]
2435

    
2436
  @locking.ssynchronized(_config_lock, shared=1)
2437
  def GetNetwork(self, uuid):
2438
    """Returns information about a network.
2439

2440
    It takes the information from the configuration file.
2441

2442
    @param uuid: UUID of the network
2443

2444
    @rtype: L{objects.Network}
2445
    @return: the network object
2446

2447
    """
2448
    return self._UnlockedGetNetwork(uuid)
2449

    
2450
  @locking.ssynchronized(_config_lock)
2451
  def AddNetwork(self, net, ec_id, check_uuid=True):
2452
    """Add a network to the configuration.
2453

2454
    @type net: L{objects.Network}
2455
    @param net: the Network object to add
2456
    @type ec_id: string
2457
    @param ec_id: unique id for the job to use when creating a missing UUID
2458

2459
    """
2460
    self._UnlockedAddNetwork(net, ec_id, check_uuid)
2461
    self._WriteConfig()
2462

    
2463
  def _UnlockedAddNetwork(self, net, ec_id, check_uuid):
2464
    """Add a network to the configuration.
2465

2466
    """
2467
    logging.info("Adding network %s to configuration", net.name)
2468

    
2469
    if check_uuid:
2470
      self._EnsureUUID(net, ec_id)
2471

    
2472
    existing_uuid = self._UnlockedLookupNetwork(net.name)
2473
    if existing_uuid:
2474
      raise errors.OpPrereqError("Desired network name '%s' already"
2475
                                 " exists as a network (UUID: %s)" %
2476
                                 (net.name, existing_uuid),
2477
                                 errors.ECODE_EXISTS)
2478
    net.serial_no = 1
2479
    self._config_data.networks[net.uuid] = net
2480
    self._config_data.cluster.serial_no += 1
2481

    
2482
  def _UnlockedLookupNetwork(self, target):
2483
    """Lookup a network's UUID.
2484

2485
    @type target: string
2486
    @param target: network name or UUID
2487
    @rtype: string
2488
    @return: network UUID
2489
    @raises errors.OpPrereqError: when the target network cannot be found
2490

2491
    """
2492
    if target in self._config_data.networks:
2493
      return target
2494
    for net in self._config_data.networks.values():
2495
      if net.name == target:
2496
        return net.uuid
2497
    return None
2498

    
2499
  @locking.ssynchronized(_config_lock, shared=1)
2500
  def LookupNetwork(self, target):
2501
    """Lookup a network's UUID.
2502

2503
    This function is just a wrapper over L{_UnlockedLookupNetwork}.
2504

2505
    @type target: string
2506
    @param target: network name or UUID
2507
    @rtype: string
2508
    @return: network UUID
2509

2510
    """
2511
    return self._UnlockedLookupNetwork(target)
2512

    
2513
  @locking.ssynchronized(_config_lock)
2514
  def RemoveNetwork(self, network_uuid):
2515
    """Remove a network from the configuration.
2516

2517
    @type network_uuid: string
2518
    @param network_uuid: the UUID of the network to remove
2519

2520
    """
2521
    logging.info("Removing network %s from configuration", network_uuid)
2522

    
2523
    if network_uuid not in self._config_data.networks:
2524
      raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
2525

    
2526
    del self._config_data.networks[network_uuid]
2527
    self._config_data.cluster.serial_no += 1
2528
    self._WriteConfig()
2529

    
2530
  def _UnlockedGetGroupNetParams(self, net, node):
2531
    """Get the netparams (mode, link) of a network.
2532

2533
    Get a network's netparams for a given node.
2534

2535
    @type net: string
2536
    @param net: network name
2537
    @type node: string
2538
    @param node: node name
2539
    @rtype: dict or None
2540
    @return: netparams
2541

2542
    """
2543
    net_uuid = self._UnlockedLookupNetwork(net)
2544
    if net_uuid is None:
2545
      return None
2546

    
2547
    node_info = self._UnlockedGetNodeInfo(node)
2548
    nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2549
    netparams = nodegroup_info.networks.get(net_uuid, None)
2550

    
2551
    return netparams
2552

    
2553
  @locking.ssynchronized(_config_lock, shared=1)
2554
  def GetGroupNetParams(self, net, node):
2555
    """Locking wrapper of _UnlockedGetGroupNetParams()
2556

2557
    """
2558
    return self._UnlockedGetGroupNetParams(net, node)
2559

    
2560

    
2561
  @locking.ssynchronized(_config_lock, shared=1)
2562
  def CheckIPInNodeGroup(self, ip, node):
2563
    """Check for conflictig IP.
2564

2565
    @type ip: string
2566
    @param ip: ip address
2567
    @type node: string
2568
    @param node: node name
2569
    @rtype: (string, dict) or (None, None)
2570
    @return: (network name, netparams)
2571

2572
    """
2573
    if ip is None:
2574
      return (None, None)
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])
2582

    
2583
    return (None, None)