Statistics
| Branch: | Tag: | Revision:

root / lib / config.py @ 326d8273

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

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

    
55

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

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

    
61

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

65
  This only verifies the version of the configuration.
66

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

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

    
74

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

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

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

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

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

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

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

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

    
116

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

120
    """
121
    assert callable(generate_one_fn)
122

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

    
136

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

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

    
143

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

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

153
  """
154
  result = []
155

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

    
161
  return result
162

    
163

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

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

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

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

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

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

210
    """
211
    self._context = context
212

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

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

    
221
  @locking.ssynchronized(_config_lock, shared=1)
222
  def GetNdParams(self, node):
223
    """Get the node params populated with cluster defaults.
224

225
    @type node: L{objects.Node}
226
    @param node: The node we want to know the params for
227
    @return: A dict with the filled in node params
228

229
    """
230
    nodegroup = self._UnlockedGetNodeGroup(node.group)
231
    return self._config_data.cluster.FillND(node, nodegroup)
232

    
233
  @locking.ssynchronized(_config_lock, shared=1)
234
  def GetInstanceDiskParams(self, instance):
235
    """Get the disk params populated with inherit chain.
236

237
    @type instance: L{objects.Instance}
238
    @param instance: The instance we want to know the params for
239
    @return: A dict with the filled in disk params
240

241
    """
242
    node = self._UnlockedGetNodeInfo(instance.primary_node)
243
    nodegroup = self._UnlockedGetNodeGroup(node.group)
244
    return self._UnlockedGetGroupDiskParams(nodegroup)
245

    
246
  @locking.ssynchronized(_config_lock, shared=1)
247
  def GetGroupDiskParams(self, group):
248
    """Get the disk params populated with inherit chain.
249

250
    @type group: L{objects.NodeGroup}
251
    @param group: The group we want to know the params for
252
    @return: A dict with the filled in disk params
253

254
    """
255
    return self._UnlockedGetGroupDiskParams(group)
256

    
257
  def _UnlockedGetGroupDiskParams(self, group):
258
    """Get the disk params populated with inherit chain down to node-group.
259

260
    @type group: L{objects.NodeGroup}
261
    @param group: The group we want to know the params for
262
    @return: A dict with the filled in disk params
263

264
    """
265
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
266

    
267
  def _UnlockedGetNetworkMACPrefix(self, net):
268
    """Return the network mac prefix if it exists or the cluster level default.
269

270
    """
271
    prefix = None
272
    if net:
273
      net_uuid = self._UnlockedLookupNetwork(net)
274
      if net_uuid:
275
        nobj = self._UnlockedGetNetwork(net_uuid)
276
        if nobj.mac_prefix:
277
          prefix = nobj.mac_prefix
278

    
279
    return prefix
280

    
281
  def _GenerateOneMAC(self, prefix=None):
282
    """Return a function that randomly generates a MAC suffic
283
       and appends it to the given prefix. If prefix is not given get
284
       the cluster level default.
285

286
    """
287
    if not prefix:
288
      prefix = self._config_data.cluster.mac_prefix
289
    def GenMac():
290
      byte1 = random.randrange(0, 256)
291
      byte2 = random.randrange(0, 256)
292
      byte3 = random.randrange(0, 256)
293
      mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
294
      return mac
295
    return GenMac
296

    
297
  @locking.ssynchronized(_config_lock, shared=1)
298
  def GenerateMAC(self, net, ec_id):
299
    """Generate a MAC for an instance.
300

301
    This should check the current instances for duplicates.
302

303
    """
304
    existing = self._AllMACs()
305
    prefix = self._UnlockedGetNetworkMACPrefix(net)
306
    gen_mac = self._GenerateOneMAC(prefix)
307
    return self._temporary_ids.Generate(existing, gen_mac, ec_id)
308

    
309
  @locking.ssynchronized(_config_lock, shared=1)
310
  def ReserveMAC(self, mac, ec_id):
311
    """Reserve a MAC for an instance.
312

313
    This only checks instances managed by this cluster, it does not
314
    check for potential collisions elsewhere.
315

316
    """
317
    all_macs = self._AllMACs()
318
    if mac in all_macs:
319
      raise errors.ReservationError("mac already in use")
320
    else:
321
      self._temporary_macs.Reserve(ec_id, mac)
322

    
323
  def _UnlockedCommitTemporaryIps(self, ec_id):
324
    """Commit all reserved IP address to their respective pools
325

326
    """
327
    for action, address, net_uuid in self._temporary_ips.GetECReserved(ec_id):
328
      self._UnlockedCommitIp(action, net_uuid, address)
329

    
330
  def _UnlockedCommitIp(self, action, net_uuid, address):
331
    """Commit a reserved IP address to an IP pool.
332

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

335
    """
336
    nobj = self._UnlockedGetNetwork(net_uuid)
337
    pool = network.AddressPool(nobj)
338
    if action == constants.RESERVE_ACTION:
339
      pool.Reserve(address)
340
    elif action == constants.RELEASE_ACTION:
341
      pool.Release(address)
342

    
343
  def _UnlockedReleaseIp(self, net_uuid, address, ec_id):
344
    """Give a specific IP address back to an IP pool.
345

346
    The IP address is returned to the IP pool designated by pool_id and marked
347
    as reserved.
348

349
    """
350
    self._temporary_ips.Reserve(ec_id,
351
                                (constants.RELEASE_ACTION, address, net_uuid))
352

    
353
  @locking.ssynchronized(_config_lock, shared=1)
354
  def ReleaseIp(self, network, address, ec_id):
355
    """Give a specified IP address back to an IP pool.
356

357
    This is just a wrapper around _UnlockedReleaseIp.
358

359
    """
360
    net_uuid = self._UnlockedLookupNetwork(network)
361
    if net_uuid:
362
      self._UnlockedReleaseIp(net_uuid, address, ec_id)
363

    
364
  @locking.ssynchronized(_config_lock, shared=1)
365
  def GenerateIp(self, net, ec_id):
366
    """Find a free IPv4 address for an instance.
367

368
    """
369
    net_uuid = self._UnlockedLookupNetwork(net)
370
    nobj = self._UnlockedGetNetwork(net_uuid)
371
    pool = network.AddressPool(nobj)
372

    
373
    def gen_one():
374
      try:
375
        ip = pool.GenerateFree()
376
      except errors.AddressPoolError:
377
        raise errors.ReservationError("Cannot generate IP. Network is full")
378
      return (constants.RESERVE_ACTION, ip, net_uuid)
379

    
380
    _ ,address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
381
    return address
382

    
383
  def _UnlockedReserveIp(self, net_uuid, address, ec_id):
384
    """Reserve a given IPv4 address for use by an instance.
385

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

    
396
    return self._temporary_ips.Reserve(ec_id,
397
                                       (constants.RESERVE_ACTION,
398
                                        address, net_uuid))
399

    
400

    
401
  @locking.ssynchronized(_config_lock, shared=1)
402
  def ReserveIp(self, net, address, ec_id):
403
    """Reserve a given IPv4 address for use by an instance.
404

405
    """
406
    net_uuid = self._UnlockedLookupNetwork(net)
407
    if net_uuid:
408
      return self._UnlockedReserveIp(net_uuid, address, ec_id)
409

    
410
  @locking.ssynchronized(_config_lock, shared=1)
411
  def ReserveLV(self, lv_name, ec_id):
412
    """Reserve an VG/LV pair for an instance.
413

414
    @type lv_name: string
415
    @param lv_name: the logical volume name to reserve
416

417
    """
418
    all_lvs = self._AllLVs()
419
    if lv_name in all_lvs:
420
      raise errors.ReservationError("LV already in use")
421
    else:
422
      self._temporary_lvs.Reserve(ec_id, lv_name)
423

    
424
  @locking.ssynchronized(_config_lock, shared=1)
425
  def GenerateDRBDSecret(self, ec_id):
426
    """Generate a DRBD secret.
427

428
    This checks the current disks for duplicates.
429

430
    """
431
    return self._temporary_secrets.Generate(self._AllDRBDSecrets(),
432
                                            utils.GenerateSecret,
433
                                            ec_id)
434

    
435
  def _AllLVs(self):
436
    """Compute the list of all LVs.
437

438
    """
439
    lvnames = set()
440
    for instance in self._config_data.instances.values():
441
      node_data = instance.MapLVsByNode()
442
      for lv_list in node_data.values():
443
        lvnames.update(lv_list)
444
    return lvnames
445

    
446
  def _AllIDs(self, include_temporary):
447
    """Compute the list of all UUIDs and names we have.
448

449
    @type include_temporary: boolean
450
    @param include_temporary: whether to include the _temporary_ids set
451
    @rtype: set
452
    @return: a set of IDs
453

454
    """
455
    existing = set()
456
    if include_temporary:
457
      existing.update(self._temporary_ids.GetReserved())
458
    existing.update(self._AllLVs())
459
    existing.update(self._config_data.instances.keys())
460
    existing.update(self._config_data.nodes.keys())
461
    existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid])
462
    return existing
463

    
464
  def _GenerateUniqueID(self, ec_id):
465
    """Generate an unique UUID.
466

467
    This checks the current node, instances and disk names for
468
    duplicates.
469

470
    @rtype: string
471
    @return: the unique id
472

473
    """
474
    existing = self._AllIDs(include_temporary=False)
475
    return self._temporary_ids.Generate(existing, utils.NewUUID, ec_id)
476

    
477
  @locking.ssynchronized(_config_lock, shared=1)
478
  def GenerateUniqueID(self, ec_id):
479
    """Generate an unique ID.
480

481
    This is just a wrapper over the unlocked version.
482

483
    @type ec_id: string
484
    @param ec_id: unique id for the job to reserve the id to
485

486
    """
487
    return self._GenerateUniqueID(ec_id)
488

    
489
  def _AllMACs(self):
490
    """Return all MACs present in the config.
491

492
    @rtype: list
493
    @return: the list of all MACs
494

495
    """
496
    result = []
497
    for instance in self._config_data.instances.values():
498
      for nic in instance.nics:
499
        result.append(nic.mac)
500

    
501
    return result
502

    
503
  def _AllDRBDSecrets(self):
504
    """Return all DRBD secrets present in the config.
505

506
    @rtype: list
507
    @return: the list of all DRBD secrets
508

509
    """
510
    def helper(disk, result):
511
      """Recursively gather secrets from this disk."""
512
      if disk.dev_type == constants.DT_DRBD8:
513
        result.append(disk.logical_id[5])
514
      if disk.children:
515
        for child in disk.children:
516
          helper(child, result)
517

    
518
    result = []
519
    for instance in self._config_data.instances.values():
520
      for disk in instance.disks:
521
        helper(disk, result)
522

    
523
    return result
524

    
525
  def _CheckDiskIDs(self, disk, l_ids, p_ids):
526
    """Compute duplicate disk IDs
527

528
    @type disk: L{objects.Disk}
529
    @param disk: the disk at which to start searching
530
    @type l_ids: list
531
    @param l_ids: list of current logical ids
532
    @type p_ids: list
533
    @param p_ids: list of current physical ids
534
    @rtype: list
535
    @return: a list of error messages
536

537
    """
538
    result = []
539
    if disk.logical_id is not None:
540
      if disk.logical_id in l_ids:
541
        result.append("duplicate logical id %s" % str(disk.logical_id))
542
      else:
543
        l_ids.append(disk.logical_id)
544
    if disk.physical_id is not None:
545
      if disk.physical_id in p_ids:
546
        result.append("duplicate physical id %s" % str(disk.physical_id))
547
      else:
548
        p_ids.append(disk.physical_id)
549

    
550
    if disk.children:
551
      for child in disk.children:
552
        result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
553
    return result
554

    
555
  def _UnlockedVerifyConfig(self):
556
    """Verify function.
557

558
    @rtype: list
559
    @return: a list of error messages; a non-empty list signifies
560
        configuration errors
561

562
    """
563
    # pylint: disable=R0914
564
    result = []
565
    seen_macs = []
566
    ports = {}
567
    data = self._config_data
568
    cluster = data.cluster
569
    seen_lids = []
570
    seen_pids = []
571

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

    
585
    if cluster.master_node not in data.nodes:
586
      result.append("cluster has invalid primary node '%s'" %
587
                    cluster.master_node)
588

    
589
    def _helper(owner, attr, value, template):
590
      try:
591
        utils.ForceDictType(value, template)
592
      except errors.GenericError, err:
593
        result.append("%s has invalid %s: %s" % (owner, attr, err))
594

    
595
    def _helper_nic(owner, params):
596
      try:
597
        objects.NIC.CheckParameterSyntax(params)
598
      except errors.ConfigurationError, err:
599
        result.append("%s has invalid nicparams: %s" % (owner, err))
600

    
601
    def _helper_ipolicy(owner, params, check_std):
602
      try:
603
        objects.InstancePolicy.CheckParameterSyntax(params, check_std)
604
      except errors.ConfigurationError, err:
605
        result.append("%s has invalid instance policy: %s" % (owner, err))
606

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

    
623
    # check cluster parameters
624
    _helper("cluster", "beparams", cluster.SimpleFillBE({}),
625
            constants.BES_PARAMETER_TYPES)
626
    _helper("cluster", "nicparams", cluster.SimpleFillNIC({}),
627
            constants.NICS_PARAMETER_TYPES)
628
    _helper_nic("cluster", cluster.SimpleFillNIC({}))
629
    _helper("cluster", "ndparams", cluster.SimpleFillND({}),
630
            constants.NDS_PARAMETER_TYPES)
631
    _helper_ipolicy("cluster", cluster.SimpleFillIPolicy({}), True)
632
    _helper_ispecs("cluster", cluster.SimpleFillIPolicy({}))
633

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

    
660
      # parameter checks
661
      if instance.beparams:
662
        _helper("instance %s" % instance.name, "beparams",
663
                cluster.FillBE(instance), constants.BES_PARAMETER_TYPES)
664

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

    
679
      # instance disk verify
680
      for idx, disk in enumerate(instance.disks):
681
        result.extend(["instance '%s' disk %d error: %s" %
682
                       (instance.name, idx, msg) for msg in disk.Verify()])
683
        result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
684

    
685
      wrong_names = _CheckInstanceDiskIvNames(instance.disks)
686
      if wrong_names:
687
        tmp = "; ".join(("name of disk %s should be '%s', but is '%s'" %
688
                         (idx, exp_name, actual_name))
689
                        for (idx, exp_name, actual_name) in wrong_names)
690

    
691
        result.append("Instance '%s' has wrongly named disks: %s" %
692
                      (instance.name, tmp))
693

    
694
    # cluster-wide pool of free ports
695
    for free_port in cluster.tcpudp_port_pool:
696
      if free_port not in ports:
697
        ports[free_port] = []
698
      ports[free_port].append(("cluster", "port marked as free"))
699

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

    
709
    # highest used tcp port check
710
    if keys:
711
      if keys[-1] > cluster.highest_used_port:
712
        result.append("Highest used port mismatch, saved %s, computed %s" %
713
                      (cluster.highest_used_port, keys[-1]))
714

    
715
    if not data.nodes[cluster.master_node].master_candidate:
716
      result.append("Master node is not a master candidate")
717

    
718
    # master candidate checks
719
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
720
    if mc_now < mc_max:
721
      result.append("Not enough master candidates: actual %d, target %d" %
722
                    (mc_now, mc_max))
723

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

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

    
765
    # drbd minors check
766
    _, duplicates = self._UnlockedComputeDRBDMap()
767
    for node, minor, instance_a, instance_b in duplicates:
768
      result.append("DRBD minor %d on node %s is assigned twice to instances"
769
                    " %s and %s" % (minor, node, instance_a, instance_b))
770

    
771
    # IP checks
772
    default_nicparams = cluster.nicparams[constants.PP_DEFAULT]
773
    ips = {}
774

    
775
    def _AddIpAddress(ip, name):
776
      ips.setdefault(ip, []).append(name)
777

    
778
    _AddIpAddress(cluster.master_ip, "cluster_ip")
779

    
780
    for node in data.nodes.values():
781
      _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
782
      if node.secondary_ip != node.primary_ip:
783
        _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
784

    
785
    for instance in data.instances.values():
786
      for idx, nic in enumerate(instance.nics):
787
        if nic.ip is None:
788
          continue
789

    
790
        nicparams = objects.FillDict(default_nicparams, nic.nicparams)
791
        nic_mode = nicparams[constants.NIC_MODE]
792
        nic_link = nicparams[constants.NIC_LINK]
793

    
794
        if nic_mode == constants.NIC_MODE_BRIDGED:
795
          link = "bridge:%s" % nic_link
796
        elif nic_mode == constants.NIC_MODE_ROUTED:
797
          link = "route:%s" % nic_link
798
        else:
799
          raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
800

    
801
        _AddIpAddress("%s/%s/%s" % (link, nic.ip, nic.network),
802
                      "instance:%s/nic:%d" % (instance.name, idx))
803

    
804
    for ip, owners in ips.items():
805
      if len(owners) > 1:
806
        result.append("IP address %s is used by multiple owners: %s" %
807
                      (ip, utils.CommaJoin(owners)))
808

    
809
    return result
810

    
811
  @locking.ssynchronized(_config_lock, shared=1)
812
  def VerifyConfig(self):
813
    """Verify function.
814

815
    This is just a wrapper over L{_UnlockedVerifyConfig}.
816

817
    @rtype: list
818
    @return: a list of error messages; a non-empty list signifies
819
        configuration errors
820

821
    """
822
    return self._UnlockedVerifyConfig()
823

    
824
  def _UnlockedSetDiskID(self, disk, node_name):
825
    """Convert the unique ID to the ID needed on the target nodes.
826

827
    This is used only for drbd, which needs ip/port configuration.
828

829
    The routine descends down and updates its children also, because
830
    this helps when the only the top device is passed to the remote
831
    node.
832

833
    This function is for internal use, when the config lock is already held.
834

835
    """
836
    if disk.children:
837
      for child in disk.children:
838
        self._UnlockedSetDiskID(child, node_name)
839

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

    
862
  @locking.ssynchronized(_config_lock)
863
  def SetDiskID(self, disk, node_name):
864
    """Convert the unique ID to the ID needed on the target nodes.
865

866
    This is used only for drbd, which needs ip/port configuration.
867

868
    The routine descends down and updates its children also, because
869
    this helps when the only the top device is passed to the remote
870
    node.
871

872
    """
873
    return self._UnlockedSetDiskID(disk, node_name)
874

    
875
  @locking.ssynchronized(_config_lock)
876
  def AddTcpUdpPort(self, port):
877
    """Adds a new port to the available port pool.
878

879
    @warning: this method does not "flush" the configuration (via
880
        L{_WriteConfig}); callers should do that themselves once the
881
        configuration is stable
882

883
    """
884
    if not isinstance(port, int):
885
      raise errors.ProgrammerError("Invalid type passed for port")
886

    
887
    self._config_data.cluster.tcpudp_port_pool.add(port)
888

    
889
  @locking.ssynchronized(_config_lock, shared=1)
890
  def GetPortList(self):
891
    """Returns a copy of the current port list.
892

893
    """
894
    return self._config_data.cluster.tcpudp_port_pool.copy()
895

    
896
  @locking.ssynchronized(_config_lock)
897
  def AllocatePort(self):
898
    """Allocate a port.
899

900
    The port will be taken from the available port pool or from the
901
    default port range (and in this case we increase
902
    highest_used_port).
903

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

    
916
    self._WriteConfig()
917
    return port
918

    
919
  def _UnlockedComputeDRBDMap(self):
920
    """Compute the used DRBD minor/nodes.
921

922
    @rtype: (dict, list)
923
    @return: dictionary of node_name: dict of minor: instance_name;
924
        the returned dict will have all the nodes in it (even if with
925
        an empty list), and a list of duplicates; if the duplicates
926
        list is not empty, the configuration is corrupted and its caller
927
        should raise an exception
928

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

    
946
    duplicates = []
947
    my_dict = dict((node, {}) for node in self._config_data.nodes)
948
    for instance in self._config_data.instances.itervalues():
949
      for disk in instance.disks:
950
        duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict))
951
    for (node, minor), instance in self._temporary_drbds.iteritems():
952
      if minor in my_dict[node] and my_dict[node][minor] != instance:
953
        duplicates.append((node, minor, instance, my_dict[node][minor]))
954
      else:
955
        my_dict[node][minor] = instance
956
    return my_dict, duplicates
957

    
958
  @locking.ssynchronized(_config_lock)
959
  def ComputeDRBDMap(self):
960
    """Compute the used DRBD minor/nodes.
961

962
    This is just a wrapper over L{_UnlockedComputeDRBDMap}.
963

964
    @return: dictionary of node_name: dict of minor: instance_name;
965
        the returned dict will have all the nodes in it (even if with
966
        an empty list).
967

968
    """
969
    d_map, duplicates = self._UnlockedComputeDRBDMap()
970
    if duplicates:
971
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
972
                                      str(duplicates))
973
    return d_map
974

    
975
  @locking.ssynchronized(_config_lock)
976
  def AllocateDRBDMinor(self, nodes, instance):
977
    """Allocate a drbd minor.
978

979
    The free minor will be automatically computed from the existing
980
    devices. A node can be given multiple times in order to allocate
981
    multiple minors. The result is the list of minors, in the same
982
    order as the passed nodes.
983

984
    @type instance: string
985
    @param instance: the instance for which we allocate minors
986

987
    """
988
    assert isinstance(instance, basestring), \
989
           "Invalid argument '%s' passed to AllocateDRBDMinor" % instance
990

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

    
1031
  def _UnlockedReleaseDRBDMinors(self, instance):
1032
    """Release temporary drbd minors allocated for a given instance.
1033

1034
    @type instance: string
1035
    @param instance: the instance for which temporary minors should be
1036
                     released
1037

1038
    """
1039
    assert isinstance(instance, basestring), \
1040
           "Invalid argument passed to ReleaseDRBDMinors"
1041
    for key, name in self._temporary_drbds.items():
1042
      if name == instance:
1043
        del self._temporary_drbds[key]
1044

    
1045
  @locking.ssynchronized(_config_lock)
1046
  def ReleaseDRBDMinors(self, instance):
1047
    """Release temporary drbd minors allocated for a given instance.
1048

1049
    This should be called on the error paths, on the success paths
1050
    it's automatically called by the ConfigWriter add and update
1051
    functions.
1052

1053
    This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
1054

1055
    @type instance: string
1056
    @param instance: the instance for which temporary minors should be
1057
                     released
1058

1059
    """
1060
    self._UnlockedReleaseDRBDMinors(instance)
1061

    
1062
  @locking.ssynchronized(_config_lock, shared=1)
1063
  def GetConfigVersion(self):
1064
    """Get the configuration version.
1065

1066
    @return: Config version
1067

1068
    """
1069
    return self._config_data.version
1070

    
1071
  @locking.ssynchronized(_config_lock, shared=1)
1072
  def GetClusterName(self):
1073
    """Get cluster name.
1074

1075
    @return: Cluster name
1076

1077
    """
1078
    return self._config_data.cluster.cluster_name
1079

    
1080
  @locking.ssynchronized(_config_lock, shared=1)
1081
  def GetMasterNode(self):
1082
    """Get the hostname of the master node for this cluster.
1083

1084
    @return: Master hostname
1085

1086
    """
1087
    return self._config_data.cluster.master_node
1088

    
1089
  @locking.ssynchronized(_config_lock, shared=1)
1090
  def GetMasterIP(self):
1091
    """Get the IP of the master node for this cluster.
1092

1093
    @return: Master IP
1094

1095
    """
1096
    return self._config_data.cluster.master_ip
1097

    
1098
  @locking.ssynchronized(_config_lock, shared=1)
1099
  def GetMasterNetdev(self):
1100
    """Get the master network device for this cluster.
1101

1102
    """
1103
    return self._config_data.cluster.master_netdev
1104

    
1105
  @locking.ssynchronized(_config_lock, shared=1)
1106
  def GetMasterNetmask(self):
1107
    """Get the netmask of the master node for this cluster.
1108

1109
    """
1110
    return self._config_data.cluster.master_netmask
1111

    
1112
  @locking.ssynchronized(_config_lock, shared=1)
1113
  def GetUseExternalMipScript(self):
1114
    """Get flag representing whether to use the external master IP setup script.
1115

1116
    """
1117
    return self._config_data.cluster.use_external_mip_script
1118

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

1123
    """
1124
    return self._config_data.cluster.file_storage_dir
1125

    
1126
  @locking.ssynchronized(_config_lock, shared=1)
1127
  def GetSharedFileStorageDir(self):
1128
    """Get the shared file storage dir for this cluster.
1129

1130
    """
1131
    return self._config_data.cluster.shared_file_storage_dir
1132

    
1133
  @locking.ssynchronized(_config_lock, shared=1)
1134
  def GetHypervisorType(self):
1135
    """Get the hypervisor type for this cluster.
1136

1137
    """
1138
    return self._config_data.cluster.enabled_hypervisors[0]
1139

    
1140
  @locking.ssynchronized(_config_lock, shared=1)
1141
  def GetHostKey(self):
1142
    """Return the rsa hostkey from the config.
1143

1144
    @rtype: string
1145
    @return: the rsa hostkey
1146

1147
    """
1148
    return self._config_data.cluster.rsahostkeypub
1149

    
1150
  @locking.ssynchronized(_config_lock, shared=1)
1151
  def GetDefaultIAllocator(self):
1152
    """Get the default instance allocator for this cluster.
1153

1154
    """
1155
    return self._config_data.cluster.default_iallocator
1156

    
1157
  @locking.ssynchronized(_config_lock, shared=1)
1158
  def GetPrimaryIPFamily(self):
1159
    """Get cluster primary ip family.
1160

1161
    @return: primary ip family
1162

1163
    """
1164
    return self._config_data.cluster.primary_ip_family
1165

    
1166
  @locking.ssynchronized(_config_lock, shared=1)
1167
  def GetMasterNetworkParameters(self):
1168
    """Get network parameters of the master node.
1169

1170
    @rtype: L{object.MasterNetworkParameters}
1171
    @return: network parameters of the master node
1172

1173
    """
1174
    cluster = self._config_data.cluster
1175
    result = objects.MasterNetworkParameters(name=cluster.master_node,
1176
      ip=cluster.master_ip,
1177
      netmask=cluster.master_netmask,
1178
      netdev=cluster.master_netdev,
1179
      ip_family=cluster.primary_ip_family)
1180

    
1181
    return result
1182

    
1183
  @locking.ssynchronized(_config_lock)
1184
  def AddNodeGroup(self, group, ec_id, check_uuid=True):
1185
    """Add a node group to the configuration.
1186

1187
    This method calls group.UpgradeConfig() to fill any missing attributes
1188
    according to their default values.
1189

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

1199
    """
1200
    self._UnlockedAddNodeGroup(group, ec_id, check_uuid)
1201
    self._WriteConfig()
1202

    
1203
  def _UnlockedAddNodeGroup(self, group, ec_id, check_uuid):
1204
    """Add a node group to the configuration.
1205

1206
    """
1207
    logging.info("Adding node group %s to configuration", group.name)
1208

    
1209
    # Some code might need to add a node group with a pre-populated UUID
1210
    # generated with ConfigWriter.GenerateUniqueID(). We allow them to bypass
1211
    # the "does this UUID" exist already check.
1212
    if check_uuid:
1213
      self._EnsureUUID(group, ec_id)
1214

    
1215
    try:
1216
      existing_uuid = self._UnlockedLookupNodeGroup(group.name)
1217
    except errors.OpPrereqError:
1218
      pass
1219
    else:
1220
      raise errors.OpPrereqError("Desired group name '%s' already exists as a"
1221
                                 " node group (UUID: %s)" %
1222
                                 (group.name, existing_uuid),
1223
                                 errors.ECODE_EXISTS)
1224

    
1225
    group.serial_no = 1
1226
    group.ctime = group.mtime = time.time()
1227
    group.UpgradeConfig()
1228

    
1229
    self._config_data.nodegroups[group.uuid] = group
1230
    self._config_data.cluster.serial_no += 1
1231

    
1232
  @locking.ssynchronized(_config_lock)
1233
  def RemoveNodeGroup(self, group_uuid):
1234
    """Remove a node group from the configuration.
1235

1236
    @type group_uuid: string
1237
    @param group_uuid: the UUID of the node group to remove
1238

1239
    """
1240
    logging.info("Removing node group %s from configuration", group_uuid)
1241

    
1242
    if group_uuid not in self._config_data.nodegroups:
1243
      raise errors.ConfigurationError("Unknown node group '%s'" % group_uuid)
1244

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

    
1248
    del self._config_data.nodegroups[group_uuid]
1249
    self._config_data.cluster.serial_no += 1
1250
    self._WriteConfig()
1251

    
1252
  def _UnlockedLookupNodeGroup(self, target):
1253
    """Lookup a node group's UUID.
1254

1255
    @type target: string or None
1256
    @param target: group name or UUID or None to look for the default
1257
    @rtype: string
1258
    @return: nodegroup UUID
1259
    @raises errors.OpPrereqError: when the target group cannot be found
1260

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

    
1276
  @locking.ssynchronized(_config_lock, shared=1)
1277
  def LookupNodeGroup(self, target):
1278
    """Lookup a node group's UUID.
1279

1280
    This function is just a wrapper over L{_UnlockedLookupNodeGroup}.
1281

1282
    @type target: string or None
1283
    @param target: group name or UUID or None to look for the default
1284
    @rtype: string
1285
    @return: nodegroup UUID
1286

1287
    """
1288
    return self._UnlockedLookupNodeGroup(target)
1289

    
1290
  def _UnlockedGetNodeGroup(self, uuid):
1291
    """Lookup a node group.
1292

1293
    @type uuid: string
1294
    @param uuid: group UUID
1295
    @rtype: L{objects.NodeGroup} or None
1296
    @return: nodegroup object, or None if not found
1297

1298
    """
1299
    if uuid not in self._config_data.nodegroups:
1300
      return None
1301

    
1302
    return self._config_data.nodegroups[uuid]
1303

    
1304
  @locking.ssynchronized(_config_lock, shared=1)
1305
  def GetNodeGroup(self, uuid):
1306
    """Lookup a node group.
1307

1308
    @type uuid: string
1309
    @param uuid: group UUID
1310
    @rtype: L{objects.NodeGroup} or None
1311
    @return: nodegroup object, or None if not found
1312

1313
    """
1314
    return self._UnlockedGetNodeGroup(uuid)
1315

    
1316
  @locking.ssynchronized(_config_lock, shared=1)
1317
  def GetAllNodeGroupsInfo(self):
1318
    """Get the configuration of all node groups.
1319

1320
    """
1321
    return dict(self._config_data.nodegroups)
1322

    
1323
  @locking.ssynchronized(_config_lock, shared=1)
1324
  def GetNodeGroupList(self):
1325
    """Get a list of node groups.
1326

1327
    """
1328
    return self._config_data.nodegroups.keys()
1329

    
1330
  @locking.ssynchronized(_config_lock, shared=1)
1331
  def GetNodeGroupMembersByNodes(self, nodes):
1332
    """Get nodes which are member in the same nodegroups as the given nodes.
1333

1334
    """
1335
    ngfn = lambda node_name: self._UnlockedGetNodeInfo(node_name).group
1336
    return frozenset(member_name
1337
                     for node_name in nodes
1338
                     for member_name in
1339
                       self._UnlockedGetNodeGroup(ngfn(node_name)).members)
1340

    
1341
  @locking.ssynchronized(_config_lock, shared=1)
1342
  def GetMultiNodeGroupInfo(self, group_uuids):
1343
    """Get the configuration of multiple node groups.
1344

1345
    @param group_uuids: List of node group UUIDs
1346
    @rtype: list
1347
    @return: List of tuples of (group_uuid, group_info)
1348

1349
    """
1350
    return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]
1351

    
1352
  @locking.ssynchronized(_config_lock)
1353
  def AddInstance(self, instance, ec_id):
1354
    """Add an instance to the config.
1355

1356
    This should be used after creating a new instance.
1357

1358
    @type instance: L{objects.Instance}
1359
    @param instance: the instance object
1360

1361
    """
1362
    if not isinstance(instance, objects.Instance):
1363
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
1364

    
1365
    if instance.disk_template != constants.DT_DISKLESS:
1366
      all_lvs = instance.MapLVsByNode()
1367
      logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
1368

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

    
1376
    self._EnsureUUID(instance, ec_id)
1377

    
1378
    instance.serial_no = 1
1379
    instance.ctime = instance.mtime = time.time()
1380
    self._config_data.instances[instance.name] = instance
1381
    self._config_data.cluster.serial_no += 1
1382
    self._UnlockedReleaseDRBDMinors(instance.name)
1383
    self._UnlockedCommitTemporaryIps(ec_id)
1384
    self._WriteConfig()
1385

    
1386
  def _EnsureUUID(self, item, ec_id):
1387
    """Ensures a given object has a valid UUID.
1388

1389
    @param item: the instance or node to be checked
1390
    @param ec_id: the execution context id for the uuid reservation
1391

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

    
1399
  def _SetInstanceStatus(self, instance_name, status):
1400
    """Set the instance's status to a given value.
1401

1402
    """
1403
    assert status in constants.ADMINST_ALL, \
1404
           "Invalid status '%s' passed to SetInstanceStatus" % (status,)
1405

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

    
1416
  @locking.ssynchronized(_config_lock)
1417
  def MarkInstanceUp(self, instance_name):
1418
    """Mark the instance status to up in the config.
1419

1420
    """
1421
    self._SetInstanceStatus(instance_name, constants.ADMINST_UP)
1422

    
1423
  @locking.ssynchronized(_config_lock)
1424
  def MarkInstanceOffline(self, instance_name):
1425
    """Mark the instance status to down in the config.
1426

1427
    """
1428
    self._SetInstanceStatus(instance_name, constants.ADMINST_OFFLINE)
1429

    
1430
  @locking.ssynchronized(_config_lock)
1431
  def RemoveInstance(self, instance_name):
1432
    """Remove the instance from the configuration.
1433

1434
    """
1435
    if instance_name not in self._config_data.instances:
1436
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1437

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

    
1445
    instance = self._UnlockedGetInstanceInfo(instance_name)
1446

    
1447
    for nic in instance.nics:
1448
      if nic.network is not None and nic.ip is not None:
1449
        net_uuid = self._UnlockedLookupNetwork(nic.network)
1450
        if net_uuid:
1451
          # Return all IP addresses to the respective address pools
1452
          self._UnlockedCommitIp(constants.RELEASE_ACTION, net_uuid, nic.ip)
1453

    
1454

    
1455
    del self._config_data.instances[instance_name]
1456
    self._config_data.cluster.serial_no += 1
1457
    self._WriteConfig()
1458

    
1459
  @locking.ssynchronized(_config_lock)
1460
  def RenameInstance(self, old_name, new_name):
1461
    """Rename an instance.
1462

1463
    This needs to be done in ConfigWriter and not by RemoveInstance
1464
    combined with AddInstance as only we can guarantee an atomic
1465
    rename.
1466

1467
    """
1468
    if old_name not in self._config_data.instances:
1469
      raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
1470

    
1471
    # Operate on a copy to not loose instance object in case of a failure
1472
    inst = self._config_data.instances[old_name].Copy()
1473
    inst.name = new_name
1474

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

    
1484
    # Actually replace instance object
1485
    del self._config_data.instances[old_name]
1486
    self._config_data.instances[inst.name] = inst
1487

    
1488
    # Force update of ssconf files
1489
    self._config_data.cluster.serial_no += 1
1490

    
1491
    self._WriteConfig()
1492

    
1493
  @locking.ssynchronized(_config_lock)
1494
  def MarkInstanceDown(self, instance_name):
1495
    """Mark the status of an instance to down in the configuration.
1496

1497
    """
1498
    self._SetInstanceStatus(instance_name, constants.ADMINST_DOWN)
1499

    
1500
  def _UnlockedGetInstanceList(self):
1501
    """Get the list of instances.
1502

1503
    This function is for internal use, when the config lock is already held.
1504

1505
    """
1506
    return self._config_data.instances.keys()
1507

    
1508
  @locking.ssynchronized(_config_lock, shared=1)
1509
  def GetInstanceList(self):
1510
    """Get the list of instances.
1511

1512
    @return: array of instances, ex. ['instance2.example.com',
1513
        'instance1.example.com']
1514

1515
    """
1516
    return self._UnlockedGetInstanceList()
1517

    
1518
  def ExpandInstanceName(self, short_name):
1519
    """Attempt to expand an incomplete instance name.
1520

1521
    """
1522
    # Locking is done in L{ConfigWriter.GetInstanceList}
1523
    return _MatchNameComponentIgnoreCase(short_name, self.GetInstanceList())
1524

    
1525
  def _UnlockedGetInstanceInfo(self, instance_name):
1526
    """Returns information about an instance.
1527

1528
    This function is for internal use, when the config lock is already held.
1529

1530
    """
1531
    if instance_name not in self._config_data.instances:
1532
      return None
1533

    
1534
    return self._config_data.instances[instance_name]
1535

    
1536
  @locking.ssynchronized(_config_lock, shared=1)
1537
  def GetInstanceInfo(self, instance_name):
1538
    """Returns information about an instance.
1539

1540
    It takes the information from the configuration file. Other information of
1541
    an instance are taken from the live systems.
1542

1543
    @param instance_name: name of the instance, e.g.
1544
        I{instance1.example.com}
1545

1546
    @rtype: L{objects.Instance}
1547
    @return: the instance object
1548

1549
    """
1550
    return self._UnlockedGetInstanceInfo(instance_name)
1551

    
1552
  @locking.ssynchronized(_config_lock, shared=1)
1553
  def GetInstanceNodeGroups(self, instance_name, primary_only=False):
1554
    """Returns set of node group UUIDs for instance's nodes.
1555

1556
    @rtype: frozenset
1557

1558
    """
1559
    instance = self._UnlockedGetInstanceInfo(instance_name)
1560
    if not instance:
1561
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1562

    
1563
    if primary_only:
1564
      nodes = [instance.primary_node]
1565
    else:
1566
      nodes = instance.all_nodes
1567

    
1568
    return frozenset(self._UnlockedGetNodeInfo(node_name).group
1569
                     for node_name in nodes)
1570

    
1571
  @locking.ssynchronized(_config_lock, shared=1)
1572
  def GetMultiInstanceInfo(self, instances):
1573
    """Get the configuration of multiple instances.
1574

1575
    @param instances: list of instance names
1576
    @rtype: list
1577
    @return: list of tuples (instance, instance_info), where
1578
        instance_info is what would GetInstanceInfo return for the
1579
        node, while keeping the original order
1580

1581
    """
1582
    return [(name, self._UnlockedGetInstanceInfo(name)) for name in instances]
1583

    
1584
  @locking.ssynchronized(_config_lock, shared=1)
1585
  def GetAllInstancesInfo(self):
1586
    """Get the configuration of all instances.
1587

1588
    @rtype: dict
1589
    @return: dict of (instance, instance_info), where instance_info is what
1590
              would GetInstanceInfo return for the node
1591

1592
    """
1593
    my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance))
1594
                    for instance in self._UnlockedGetInstanceList()])
1595
    return my_dict
1596

    
1597
  @locking.ssynchronized(_config_lock, shared=1)
1598
  def GetInstancesInfoByFilter(self, filter_fn):
1599
    """Get instance configuration with a filter.
1600

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

1608
    """
1609
    return dict((name, inst)
1610
                for (name, inst) in self._config_data.instances.items()
1611
                if filter_fn(inst))
1612

    
1613
  @locking.ssynchronized(_config_lock)
1614
  def AddNode(self, node, ec_id):
1615
    """Add a node to the configuration.
1616

1617
    @type node: L{objects.Node}
1618
    @param node: a Node instance
1619

1620
    """
1621
    logging.info("Adding node %s to configuration", node.name)
1622

    
1623
    self._EnsureUUID(node, ec_id)
1624

    
1625
    node.serial_no = 1
1626
    node.ctime = node.mtime = time.time()
1627
    self._UnlockedAddNodeToGroup(node.name, node.group)
1628
    self._config_data.nodes[node.name] = node
1629
    self._config_data.cluster.serial_no += 1
1630
    self._WriteConfig()
1631

    
1632
  @locking.ssynchronized(_config_lock)
1633
  def RemoveNode(self, node_name):
1634
    """Remove a node from the configuration.
1635

1636
    """
1637
    logging.info("Removing node %s from configuration", node_name)
1638

    
1639
    if node_name not in self._config_data.nodes:
1640
      raise errors.ConfigurationError("Unknown node '%s'" % node_name)
1641

    
1642
    self._UnlockedRemoveNodeFromGroup(self._config_data.nodes[node_name])
1643
    del self._config_data.nodes[node_name]
1644
    self._config_data.cluster.serial_no += 1
1645
    self._WriteConfig()
1646

    
1647
  def ExpandNodeName(self, short_name):
1648
    """Attempt to expand an incomplete node name.
1649

1650
    """
1651
    # Locking is done in L{ConfigWriter.GetNodeList}
1652
    return _MatchNameComponentIgnoreCase(short_name, self.GetNodeList())
1653

    
1654
  def _UnlockedGetNodeInfo(self, node_name):
1655
    """Get the configuration of a node, as stored in the config.
1656

1657
    This function is for internal use, when the config lock is already
1658
    held.
1659

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

1662
    @rtype: L{objects.Node}
1663
    @return: the node object
1664

1665
    """
1666
    if node_name not in self._config_data.nodes:
1667
      return None
1668

    
1669
    return self._config_data.nodes[node_name]
1670

    
1671
  @locking.ssynchronized(_config_lock, shared=1)
1672
  def GetNodeInfo(self, node_name):
1673
    """Get the configuration of a node, as stored in the config.
1674

1675
    This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
1676

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

1679
    @rtype: L{objects.Node}
1680
    @return: the node object
1681

1682
    """
1683
    return self._UnlockedGetNodeInfo(node_name)
1684

    
1685
  @locking.ssynchronized(_config_lock, shared=1)
1686
  def GetNodeInstances(self, node_name):
1687
    """Get the instances of a node, as stored in the config.
1688

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

1691
    @rtype: (list, list)
1692
    @return: a tuple with two lists: the primary and the secondary instances
1693

1694
    """
1695
    pri = []
1696
    sec = []
1697
    for inst in self._config_data.instances.values():
1698
      if inst.primary_node == node_name:
1699
        pri.append(inst.name)
1700
      if node_name in inst.secondary_nodes:
1701
        sec.append(inst.name)
1702
    return (pri, sec)
1703

    
1704
  @locking.ssynchronized(_config_lock, shared=1)
1705
  def GetNodeGroupInstances(self, uuid, primary_only=False):
1706
    """Get the instances of a node group.
1707

1708
    @param uuid: Node group UUID
1709
    @param primary_only: Whether to only consider primary nodes
1710
    @rtype: frozenset
1711
    @return: List of instance names in node group
1712

1713
    """
1714
    if primary_only:
1715
      nodes_fn = lambda inst: [inst.primary_node]
1716
    else:
1717
      nodes_fn = lambda inst: inst.all_nodes
1718

    
1719
    return frozenset(inst.name
1720
                     for inst in self._config_data.instances.values()
1721
                     for node_name in nodes_fn(inst)
1722
                     if self._UnlockedGetNodeInfo(node_name).group == uuid)
1723

    
1724
  def _UnlockedGetNodeList(self):
1725
    """Return the list of nodes which are in the configuration.
1726

1727
    This function is for internal use, when the config lock is already
1728
    held.
1729

1730
    @rtype: list
1731

1732
    """
1733
    return self._config_data.nodes.keys()
1734

    
1735
  @locking.ssynchronized(_config_lock, shared=1)
1736
  def GetNodeList(self):
1737
    """Return the list of nodes which are in the configuration.
1738

1739
    """
1740
    return self._UnlockedGetNodeList()
1741

    
1742
  def _UnlockedGetOnlineNodeList(self):
1743
    """Return the list of nodes which are online.
1744

1745
    """
1746
    all_nodes = [self._UnlockedGetNodeInfo(node)
1747
                 for node in self._UnlockedGetNodeList()]
1748
    return [node.name for node in all_nodes if not node.offline]
1749

    
1750
  @locking.ssynchronized(_config_lock, shared=1)
1751
  def GetOnlineNodeList(self):
1752
    """Return the list of nodes which are online.
1753

1754
    """
1755
    return self._UnlockedGetOnlineNodeList()
1756

    
1757
  @locking.ssynchronized(_config_lock, shared=1)
1758
  def GetVmCapableNodeList(self):
1759
    """Return the list of nodes which are not vm capable.
1760

1761
    """
1762
    all_nodes = [self._UnlockedGetNodeInfo(node)
1763
                 for node in self._UnlockedGetNodeList()]
1764
    return [node.name for node in all_nodes if node.vm_capable]
1765

    
1766
  @locking.ssynchronized(_config_lock, shared=1)
1767
  def GetNonVmCapableNodeList(self):
1768
    """Return the list of nodes which are not vm capable.
1769

1770
    """
1771
    all_nodes = [self._UnlockedGetNodeInfo(node)
1772
                 for node in self._UnlockedGetNodeList()]
1773
    return [node.name for node in all_nodes if not node.vm_capable]
1774

    
1775
  @locking.ssynchronized(_config_lock, shared=1)
1776
  def GetMultiNodeInfo(self, nodes):
1777
    """Get the configuration of multiple nodes.
1778

1779
    @param nodes: list of node names
1780
    @rtype: list
1781
    @return: list of tuples of (node, node_info), where node_info is
1782
        what would GetNodeInfo return for the node, in the original
1783
        order
1784

1785
    """
1786
    return [(name, self._UnlockedGetNodeInfo(name)) for name in nodes]
1787

    
1788
  @locking.ssynchronized(_config_lock, shared=1)
1789
  def GetAllNodesInfo(self):
1790
    """Get the configuration of all nodes.
1791

1792
    @rtype: dict
1793
    @return: dict of (node, node_info), where node_info is what
1794
              would GetNodeInfo return for the node
1795

1796
    """
1797
    return self._UnlockedGetAllNodesInfo()
1798

    
1799
  def _UnlockedGetAllNodesInfo(self):
1800
    """Gets configuration of all nodes.
1801

1802
    @note: See L{GetAllNodesInfo}
1803

1804
    """
1805
    return dict([(node, self._UnlockedGetNodeInfo(node))
1806
                 for node in self._UnlockedGetNodeList()])
1807

    
1808
  @locking.ssynchronized(_config_lock, shared=1)
1809
  def GetNodeGroupsFromNodes(self, nodes):
1810
    """Returns groups for a list of nodes.
1811

1812
    @type nodes: list of string
1813
    @param nodes: List of node names
1814
    @rtype: frozenset
1815

1816
    """
1817
    return frozenset(self._UnlockedGetNodeInfo(name).group for name in nodes)
1818

    
1819
  def _UnlockedGetMasterCandidateStats(self, exceptions=None):
1820
    """Get the number of current and maximum desired and possible candidates.
1821

1822
    @type exceptions: list
1823
    @param exceptions: if passed, list of nodes that should be ignored
1824
    @rtype: tuple
1825
    @return: tuple of (current, desired and possible, possible)
1826

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

    
1839
  @locking.ssynchronized(_config_lock, shared=1)
1840
  def GetMasterCandidateStats(self, exceptions=None):
1841
    """Get the number of current and maximum possible candidates.
1842

1843
    This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
1844

1845
    @type exceptions: list
1846
    @param exceptions: if passed, list of nodes that should be ignored
1847
    @rtype: tuple
1848
    @return: tuple of (current, max)
1849

1850
    """
1851
    return self._UnlockedGetMasterCandidateStats(exceptions)
1852

    
1853
  @locking.ssynchronized(_config_lock)
1854
  def MaintainCandidatePool(self, exceptions):
1855
    """Try to grow the candidate pool to the desired size.
1856

1857
    @type exceptions: list
1858
    @param exceptions: if passed, list of nodes that should be ignored
1859
    @rtype: list
1860
    @return: list with the adjusted nodes (L{objects.Node} instances)
1861

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

    
1887
    return mod_list
1888

    
1889
  def _UnlockedAddNodeToGroup(self, node_name, nodegroup_uuid):
1890
    """Add a given node to the specified group.
1891

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

    
1902
  def _UnlockedRemoveNodeFromGroup(self, node):
1903
    """Remove a given node from its group.
1904

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

    
1917
  @locking.ssynchronized(_config_lock)
1918
  def AssignGroupNodes(self, mods):
1919
    """Changes the group of a number of nodes.
1920

1921
    @type mods: list of tuples; (node name, new group UUID)
1922
    @param mods: Node membership modifications
1923

1924
    """
1925
    groups = self._config_data.nodegroups
1926
    nodes = self._config_data.nodes
1927

    
1928
    resmod = []
1929

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

    
1937
      if node.group == new_group_uuid:
1938
        # Node is being assigned to its current group
1939
        logging.debug("Node '%s' was assigned to its current group (%s)",
1940
                      node_name, node.group)
1941
        continue
1942

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

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

    
1957
      assert node.name in old_group.members, \
1958
        ("Inconsistent configuration: node '%s' not listed in members for its"
1959
         " old group '%s'" % (node.name, old_group.uuid))
1960
      assert node.name not in new_group.members, \
1961
        ("Inconsistent configuration: node '%s' already listed in members for"
1962
         " its new group '%s'" % (node.name, new_group.uuid))
1963

    
1964
      resmod.append((node, old_group, new_group))
1965

    
1966
    # Apply changes
1967
    for (node, old_group, new_group) in resmod:
1968
      assert node.uuid != new_group.uuid and old_group.uuid != new_group.uuid, \
1969
        "Assigning to current group is not possible"
1970

    
1971
      node.group = new_group.uuid
1972

    
1973
      # Update members of involved groups
1974
      if node.name in old_group.members:
1975
        old_group.members.remove(node.name)
1976
      if node.name not in new_group.members:
1977
        new_group.members.append(node.name)
1978

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

    
1985
    # Force ssconf update
1986
    self._config_data.cluster.serial_no += 1
1987

    
1988
    self._WriteConfig()
1989

    
1990
  def _BumpSerialNo(self):
1991
    """Bump up the serial number of the config.
1992

1993
    """
1994
    self._config_data.serial_no += 1
1995
    self._config_data.mtime = time.time()
1996

    
1997
  def _AllUUIDObjects(self):
1998
    """Returns all objects with uuid attributes.
1999

2000
    """
2001
    return (self._config_data.instances.values() +
2002
            self._config_data.nodes.values() +
2003
            self._config_data.nodegroups.values() +
2004
            [self._config_data.cluster])
2005

    
2006
  def _OpenConfig(self, accept_foreign):
2007
    """Read the config data from disk.
2008

2009
    """
2010
    raw_data = utils.ReadFile(self._cfg_file)
2011

    
2012
    try:
2013
      data = objects.ConfigData.FromDict(serializer.Load(raw_data))
2014
    except Exception, err:
2015
      raise errors.ConfigurationError(err)
2016

    
2017
    # Make sure the configuration has the right version
2018
    _ValidateConfig(data)
2019

    
2020
    if (not hasattr(data, "cluster") or
2021
        not hasattr(data.cluster, "rsahostkeypub")):
2022
      raise errors.ConfigurationError("Incomplete configuration"
2023
                                      " (missing cluster.rsahostkeypub)")
2024

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

    
2032
    # Upgrade configuration if needed
2033
    data.UpgradeConfig()
2034

    
2035
    self._config_data = data
2036
    # reset the last serial as -1 so that the next write will cause
2037
    # ssconf update
2038
    self._last_cluster_serial = -1
2039

    
2040
    # And finally run our (custom) config upgrade sequence
2041
    self._UpgradeConfig()
2042

    
2043
    self._cfg_id = utils.GetFileID(path=self._cfg_file)
2044

    
2045
  def _UpgradeConfig(self):
2046
    """Run upgrade steps that cannot be done purely in the objects.
2047

2048
    This is because some data elements need uniqueness across the
2049
    whole configuration, etc.
2050

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

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

    
2084
  def _DistributeConfig(self, feedback_fn):
2085
    """Distribute the configuration to the other nodes.
2086

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

2090
    """
2091
    if self._offline:
2092
      return True
2093

    
2094
    bad = False
2095

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

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

    
2122
        if feedback_fn:
2123
          feedback_fn(msg)
2124

    
2125
        bad = True
2126

    
2127
    return not bad
2128

    
2129
  def _WriteConfig(self, destination=None, feedback_fn=None):
2130
    """Write the configuration data to persistent storage.
2131

2132
    """
2133
    assert feedback_fn is None or callable(feedback_fn)
2134

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

    
2147
    if destination is None:
2148
      destination = self._cfg_file
2149
    self._BumpSerialNo()
2150
    txt = serializer.Dump(self._config_data.ToDict())
2151

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

    
2165
    self.write_count += 1
2166

    
2167
    # and redistribute the config file to master candidates
2168
    self._DistributeConfig(feedback_fn)
2169

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

    
2177
        for nname, nresu in result.items():
2178
          msg = nresu.fail_msg
2179
          if msg:
2180
            errmsg = ("Error while uploading ssconf files to"
2181
                      " node %s: %s" % (nname, msg))
2182
            logging.warning(errmsg)
2183

    
2184
            if feedback_fn:
2185
              feedback_fn(errmsg)
2186

    
2187
      self._last_cluster_serial = self._config_data.cluster.serial_no
2188

    
2189
  def _UnlockedGetSsconfValues(self):
2190
    """Return the values needed by ssconf.
2191

2192
    @rtype: dict
2193
    @return: a dictionary with keys the ssconf names and values their
2194
        associated value
2195

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

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

    
2216
    cluster = self._config_data.cluster
2217
    cluster_tags = fn(cluster.GetTags())
2218

    
2219
    hypervisor_list = fn(cluster.enabled_hypervisors)
2220

    
2221
    uid_pool = uidpool.FormatUidPool(cluster.uid_pool, separator="\n")
2222

    
2223
    nodegroups = ["%s %s" % (nodegroup.uuid, nodegroup.name) for nodegroup in
2224
                  self._config_data.nodegroups.values()]
2225
    nodegroups_data = fn(utils.NiceSort(nodegroups))
2226
    networks = ["%s %s" % (net.uuid, net.name) for net in
2227
                self._config_data.networks.values()]
2228
    networks_data = fn(utils.NiceSort(networks))
2229

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

    
2263
  @locking.ssynchronized(_config_lock, shared=1)
2264
  def GetSsconfValues(self):
2265
    """Wrapper using lock around _UnlockedGetSsconf().
2266

2267
    """
2268
    return self._UnlockedGetSsconfValues()
2269

    
2270
  @locking.ssynchronized(_config_lock, shared=1)
2271
  def GetVGName(self):
2272
    """Return the volume group name.
2273

2274
    """
2275
    return self._config_data.cluster.volume_group_name
2276

    
2277
  @locking.ssynchronized(_config_lock)
2278
  def SetVGName(self, vg_name):
2279
    """Set the volume group name.
2280

2281
    """
2282
    self._config_data.cluster.volume_group_name = vg_name
2283
    self._config_data.cluster.serial_no += 1
2284
    self._WriteConfig()
2285

    
2286
  @locking.ssynchronized(_config_lock, shared=1)
2287
  def GetDRBDHelper(self):
2288
    """Return DRBD usermode helper.
2289

2290
    """
2291
    return self._config_data.cluster.drbd_usermode_helper
2292

    
2293
  @locking.ssynchronized(_config_lock)
2294
  def SetDRBDHelper(self, drbd_helper):
2295
    """Set DRBD usermode helper.
2296

2297
    """
2298
    self._config_data.cluster.drbd_usermode_helper = drbd_helper
2299
    self._config_data.cluster.serial_no += 1
2300
    self._WriteConfig()
2301

    
2302
  @locking.ssynchronized(_config_lock, shared=1)
2303
  def GetMACPrefix(self):
2304
    """Return the mac prefix.
2305

2306
    """
2307
    return self._config_data.cluster.mac_prefix
2308

    
2309
  @locking.ssynchronized(_config_lock, shared=1)
2310
  def GetClusterInfo(self):
2311
    """Returns information about the cluster
2312

2313
    @rtype: L{objects.Cluster}
2314
    @return: the cluster object
2315

2316
    """
2317
    return self._config_data.cluster
2318

    
2319
  @locking.ssynchronized(_config_lock, shared=1)
2320
  def HasAnyDiskOfType(self, dev_type):
2321
    """Check if in there is at disk of the given type in the configuration.
2322

2323
    """
2324
    return self._config_data.HasAnyDiskOfType(dev_type)
2325

    
2326
  @locking.ssynchronized(_config_lock)
2327
  def Update(self, target, feedback_fn, ec_id=None):
2328
    """Notify function to be called after updates.
2329

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

2336
    @param target: an instance of either L{objects.Cluster},
2337
        L{objects.Node} or L{objects.Instance} which is existing in
2338
        the cluster
2339
    @param feedback_fn: Callable feedback function
2340

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

    
2366
    if update_serial:
2367
      # for node updates, we need to increase the cluster serial too
2368
      self._config_data.cluster.serial_no += 1
2369
      self._config_data.cluster.mtime = now
2370

    
2371
    if isinstance(target, objects.Instance):
2372
      self._UnlockedReleaseDRBDMinors(target.name)
2373

    
2374
    if ec_id is not None:
2375
      # Commit all ips reserved by OpInstanceSetParams and OpGroupSetParams
2376
      self._UnlockedCommitTemporaryIps(ec_id)
2377

    
2378
    self._WriteConfig(feedback_fn=feedback_fn)
2379

    
2380
  @locking.ssynchronized(_config_lock)
2381
  def DropECReservations(self, ec_id):
2382
    """Drop per-execution-context reservations
2383

2384
    """
2385
    for rm in self._all_rms:
2386
      rm.DropECReservations(ec_id)
2387

    
2388
  @locking.ssynchronized(_config_lock, shared=1)
2389
  def GetAllNetworksInfo(self):
2390
    """Get the configuration of all networks
2391

2392
    """
2393
    return dict(self._config_data.networks)
2394

    
2395
  def _UnlockedGetNetworkList(self):
2396
    """Get the list of networks.
2397

2398
    This function is for internal use, when the config lock is already held.
2399

2400
    """
2401
    return self._config_data.networks.keys()
2402

    
2403
  @locking.ssynchronized(_config_lock, shared=1)
2404
  def GetNetworkList(self):
2405
    """Get the list of networks.
2406

2407
    @return: array of networks, ex. ["main", "vlan100", "200]
2408

2409
    """
2410
    return self._UnlockedGetNetworkList()
2411

    
2412
  @locking.ssynchronized(_config_lock, shared=1)
2413
  def GetNetworkNames(self):
2414
    """Get a list of network names
2415

2416
    """
2417
    names = [network.name
2418
             for network in self._config_data.networks.values()]
2419
    return names
2420

    
2421
  def _UnlockedGetNetwork(self, uuid):
2422
    """Returns information about a network.
2423

2424
    This function is for internal use, when the config lock is already held.
2425

2426
    """
2427
    if uuid not in self._config_data.networks:
2428
      return None
2429

    
2430
    return self._config_data.networks[uuid]
2431

    
2432
  @locking.ssynchronized(_config_lock, shared=1)
2433
  def GetNetwork(self, uuid):
2434
    """Returns information about a network.
2435

2436
    It takes the information from the configuration file.
2437

2438
    @param uuid: UUID of the network
2439

2440
    @rtype: L{objects.Network}
2441
    @return: the network object
2442

2443
    """
2444
    return self._UnlockedGetNetwork(uuid)
2445

    
2446
  @locking.ssynchronized(_config_lock)
2447
  def AddNetwork(self, net, ec_id, check_uuid=True):
2448
    """Add a network to the configuration.
2449

2450
    @type net: L{objects.Network}
2451
    @param net: the Network object to add
2452
    @type ec_id: string
2453
    @param ec_id: unique id for the job to use when creating a missing UUID
2454

2455
    """
2456
    self._UnlockedAddNetwork(net, ec_id, check_uuid)
2457
    self._WriteConfig()
2458

    
2459
  def _UnlockedAddNetwork(self, net, ec_id, check_uuid):
2460
    """Add a network to the configuration.
2461

2462
    """
2463
    logging.info("Adding network %s to configuration", net.name)
2464

    
2465
    if check_uuid:
2466
      self._EnsureUUID(net, ec_id)
2467

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

    
2478
  def _UnlockedLookupNetwork(self, target):
2479
    """Lookup a network's UUID.
2480

2481
    @type target: string
2482
    @param target: network name or UUID
2483
    @rtype: string
2484
    @return: network UUID
2485
    @raises errors.OpPrereqError: when the target network cannot be found
2486

2487
    """
2488
    if target in self._config_data.networks:
2489
      return target
2490
    for net in self._config_data.networks.values():
2491
      if net.name == target:
2492
        return net.uuid
2493
    return None
2494

    
2495
  @locking.ssynchronized(_config_lock, shared=1)
2496
  def LookupNetwork(self, target):
2497
    """Lookup a network's UUID.
2498

2499
    This function is just a wrapper over L{_UnlockedLookupNetwork}.
2500

2501
    @type target: string
2502
    @param target: network name or UUID
2503
    @rtype: string
2504
    @return: network UUID
2505

2506
    """
2507
    return self._UnlockedLookupNetwork(target)
2508

    
2509
  @locking.ssynchronized(_config_lock)
2510
  def RemoveNetwork(self, network_uuid):
2511
    """Remove a network from the configuration.
2512

2513
    @type network_uuid: string
2514
    @param network_uuid: the UUID of the network to remove
2515

2516
    """
2517
    logging.info("Removing network %s from configuration", network_uuid)
2518

    
2519
    if network_uuid not in self._config_data.networks:
2520
      raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
2521

    
2522
    del self._config_data.networks[network_uuid]
2523
    self._config_data.cluster.serial_no += 1
2524
    self._WriteConfig()
2525

    
2526
  def _UnlockedGetGroupNetParams(self, net, node):
2527
    """Get the netparams (mode, link) of a network.
2528

2529
    Get a network's netparams for a given node.
2530

2531
    @type net: string
2532
    @param net: network name
2533
    @type node: string
2534
    @param node: node name
2535
    @rtype: dict or None
2536
    @return: netparams
2537

2538
    """
2539
    net_uuid = self._UnlockedLookupNetwork(net)
2540
    if net_uuid is None:
2541
      return None
2542

    
2543
    node_info = self._UnlockedGetNodeInfo(node)
2544
    nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2545
    netparams = nodegroup_info.networks.get(net_uuid, None)
2546

    
2547
    return netparams
2548

    
2549
  @locking.ssynchronized(_config_lock, shared=1)
2550
  def GetGroupNetParams(self, net, node):
2551
    """Locking wrapper of _UnlockedGetGroupNetParams()
2552

2553
    """
2554
    return self._UnlockedGetGroupNetParams(net, node)
2555

    
2556

    
2557
  @locking.ssynchronized(_config_lock, shared=1)
2558
  def CheckIPInNodeGroup(self, ip, node):
2559
    """Check for conflictig IP.
2560

2561
    @type ip: string
2562
    @param ip: ip address
2563
    @type node: string
2564
    @param node: node name
2565
    @rtype: (string, dict) or (None, None)
2566
    @return: (network name, netparams)
2567

2568
    """
2569
    if ip is None:
2570
      return (None, None)
2571
    node_info = self._UnlockedGetNodeInfo(node)
2572
    nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2573
    for net_uuid in nodegroup_info.networks.keys():
2574
      net_info = self._UnlockedGetNetwork(net_uuid)
2575
      pool = network.AddressPool(net_info)
2576
      if pool._Contains(ip):
2577
        return (net_info.name, nodegroup_info.networks[net_uuid])
2578

    
2579
    return (None, None)