Statistics
| Branch: | Tag: | Revision:

root / lib / config.py @ 5c6672e9

History | View | Annotate | Download (83.4 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
  def _GenerateOneMAC(self):
222
    """Generate one mac address
223

224
    """
225
    prefix = self._config_data.cluster.mac_prefix
226
    byte1 = random.randrange(0, 256)
227
    byte2 = random.randrange(0, 256)
228
    byte3 = random.randrange(0, 256)
229
    mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
230
    return mac
231

    
232
  @locking.ssynchronized(_config_lock, shared=1)
233
  def GetNdParams(self, node):
234
    """Get the node params populated with cluster defaults.
235

236
    @type node: L{objects.Node}
237
    @param node: The node we want to know the params for
238
    @return: A dict with the filled in node params
239

240
    """
241
    nodegroup = self._UnlockedGetNodeGroup(node.group)
242
    return self._config_data.cluster.FillND(node, nodegroup)
243

    
244
  @locking.ssynchronized(_config_lock, shared=1)
245
  def GetInstanceDiskParams(self, instance):
246
    """Get the disk params populated with inherit chain.
247

248
    @type instance: L{objects.Instance}
249
    @param instance: The instance we want to know the params for
250
    @return: A dict with the filled in disk params
251

252
    """
253
    node = self._UnlockedGetNodeInfo(instance.primary_node)
254
    nodegroup = self._UnlockedGetNodeGroup(node.group)
255
    return self._UnlockedGetGroupDiskParams(nodegroup)
256

    
257
  @locking.ssynchronized(_config_lock, shared=1)
258
  def GetGroupDiskParams(self, group):
259
    """Get the disk params populated with inherit chain.
260

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

265
    """
266
    return self._UnlockedGetGroupDiskParams(group)
267

    
268
  def _UnlockedGetGroupDiskParams(self, group):
269
    """Get the disk params populated with inherit chain down to node-group.
270

271
    @type group: L{objects.NodeGroup}
272
    @param group: The group we want to know the params for
273
    @return: A dict with the filled in disk params
274

275
    """
276
    return self._config_data.cluster.SimpleFillDP(group.diskparams)
277

    
278
  @locking.ssynchronized(_config_lock, shared=1)
279
  def GenerateMAC(self, ec_id):
280
    """Generate a MAC for an instance.
281

282
    This should check the current instances for duplicates.
283

284
    """
285
    existing = self._AllMACs()
286
    return self._temporary_ids.Generate(existing, self._GenerateOneMAC, ec_id)
287

    
288
  @locking.ssynchronized(_config_lock, shared=1)
289
  def ReserveMAC(self, mac, ec_id):
290
    """Reserve a MAC for an instance.
291

292
    This only checks instances managed by this cluster, it does not
293
    check for potential collisions elsewhere.
294

295
    """
296
    all_macs = self._AllMACs()
297
    if mac in all_macs:
298
      raise errors.ReservationError("mac already in use")
299
    else:
300
      self._temporary_macs.Reserve(ec_id, mac)
301

    
302
  def _UnlockedCommitTemporaryIps(self, ec_id):
303
    """Commit all reserved IP address to their respective pools
304

305
    """
306
    for action, address, net_uuid in self._temporary_ips.GetECReserved(ec_id):
307
      self._UnlockedCommitIp(action, net_uuid, address)
308

    
309
  def _UnlockedCommitIp(self, action, net_uuid, address):
310
    """Commit a reserved IP address to an IP pool.
311

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

314
    """
315
    nobj = self._UnlockedGetNetwork(net_uuid)
316
    pool = network.AddressPool(nobj)
317
    if action == 'reserve':
318
      pool.Reserve(address)
319
    elif action == 'release':
320
      pool.Release(address)
321

    
322
  def _UnlockedReleaseIp(self, net_uuid, address, ec_id):
323
    """Give a specific IP address back to an IP pool.
324

325
    The IP address is returned to the IP pool designated by pool_id and marked
326
    as reserved.
327

328
    """
329
    nobj = self._UnlockedGetNetwork(net_uuid)
330
    pool = network.AddressPool(nobj)
331
    self._temporary_ips.Reserve(ec_id, ('release', address, net_uuid))
332

    
333
  @locking.ssynchronized(_config_lock, shared=1)
334
  def ReleaseIp(self, network, address, ec_id):
335
    """Give a specified IP address back to an IP pool.
336

337
    This is just a wrapper around _UnlockedReleaseIp.
338

339
    """
340
    net_uuid = self._UnlockedLookupNetwork(network)
341
    if net_uuid:
342
      self._UnlockedReleaseIp(net_uuid, address, ec_id)
343

    
344
  @locking.ssynchronized(_config_lock, shared=1)
345
  def GenerateIp(self, net, ec_id):
346
    """Find a free IPv4 address for an instance.
347

348
    """
349
    net_uuid = self._UnlockedLookupNetwork(net)
350
    nobj = self._UnlockedGetNetwork(net_uuid)
351
    pool = network.AddressPool(nobj)
352
    gen_free = pool.GenerateFree()
353

    
354
    def gen_one():
355
      try:
356
        ip = gen_free()
357
      except StopIteration:
358
        return None
359
      return ("reserve", ip, net_uuid)
360

    
361
    _ ,address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
362
    return address
363

    
364
  def _UnlockedReserveIp(self, net_uuid, address, ec_id):
365
    """Reserve a given IPv4 address for use by an instance.
366

367
    """
368
    nobj = self._UnlockedGetNetwork(net_uuid)
369
    pool = network.AddressPool(nobj)
370
    try:
371
      isreserved = pool.IsReserved(address)
372
    except errors.AddressPoolError:
373
      raise errors.ReservationError("IP address not in network")
374
    if isreserved:
375
      raise errors.ReservationError("IP address already in use")
376

    
377
    return self._temporary_ips.Reserve(ec_id, ('reserve', address, net_uuid))
378

    
379

    
380
  @locking.ssynchronized(_config_lock, shared=1)
381
  def ReserveIp(self, net, address, ec_id):
382
    """Reserve a given IPv4 address for use by an instance.
383

384
    """
385
    net_uuid = self._UnlockedLookupNetwork(net)
386
    if net_uuid:
387
      return self._UnlockedReserveIp(net_uuid, address, ec_id)
388

    
389
  @locking.ssynchronized(_config_lock, shared=1)
390
  def ReserveLV(self, lv_name, ec_id):
391
    """Reserve an VG/LV pair for an instance.
392

393
    @type lv_name: string
394
    @param lv_name: the logical volume name to reserve
395

396
    """
397
    all_lvs = self._AllLVs()
398
    if lv_name in all_lvs:
399
      raise errors.ReservationError("LV already in use")
400
    else:
401
      self._temporary_lvs.Reserve(ec_id, lv_name)
402

    
403
  @locking.ssynchronized(_config_lock, shared=1)
404
  def GenerateDRBDSecret(self, ec_id):
405
    """Generate a DRBD secret.
406

407
    This checks the current disks for duplicates.
408

409
    """
410
    return self._temporary_secrets.Generate(self._AllDRBDSecrets(),
411
                                            utils.GenerateSecret,
412
                                            ec_id)
413

    
414
  def _AllLVs(self):
415
    """Compute the list of all LVs.
416

417
    """
418
    lvnames = set()
419
    for instance in self._config_data.instances.values():
420
      node_data = instance.MapLVsByNode()
421
      for lv_list in node_data.values():
422
        lvnames.update(lv_list)
423
    return lvnames
424

    
425
  def _AllIDs(self, include_temporary):
426
    """Compute the list of all UUIDs and names we have.
427

428
    @type include_temporary: boolean
429
    @param include_temporary: whether to include the _temporary_ids set
430
    @rtype: set
431
    @return: a set of IDs
432

433
    """
434
    existing = set()
435
    if include_temporary:
436
      existing.update(self._temporary_ids.GetReserved())
437
    existing.update(self._AllLVs())
438
    existing.update(self._config_data.instances.keys())
439
    existing.update(self._config_data.nodes.keys())
440
    existing.update([i.uuid for i in self._AllUUIDObjects() if i.uuid])
441
    return existing
442

    
443
  def _GenerateUniqueID(self, ec_id):
444
    """Generate an unique UUID.
445

446
    This checks the current node, instances and disk names for
447
    duplicates.
448

449
    @rtype: string
450
    @return: the unique id
451

452
    """
453
    existing = self._AllIDs(include_temporary=False)
454
    return self._temporary_ids.Generate(existing, utils.NewUUID, ec_id)
455

    
456
  @locking.ssynchronized(_config_lock, shared=1)
457
  def GenerateUniqueID(self, ec_id):
458
    """Generate an unique ID.
459

460
    This is just a wrapper over the unlocked version.
461

462
    @type ec_id: string
463
    @param ec_id: unique id for the job to reserve the id to
464

465
    """
466
    return self._GenerateUniqueID(ec_id)
467

    
468
  def _AllMACs(self):
469
    """Return all MACs present in the config.
470

471
    @rtype: list
472
    @return: the list of all MACs
473

474
    """
475
    result = []
476
    for instance in self._config_data.instances.values():
477
      for nic in instance.nics:
478
        result.append(nic.mac)
479

    
480
    return result
481

    
482
  def _AllDRBDSecrets(self):
483
    """Return all DRBD secrets present in the config.
484

485
    @rtype: list
486
    @return: the list of all DRBD secrets
487

488
    """
489
    def helper(disk, result):
490
      """Recursively gather secrets from this disk."""
491
      if disk.dev_type == constants.DT_DRBD8:
492
        result.append(disk.logical_id[5])
493
      if disk.children:
494
        for child in disk.children:
495
          helper(child, result)
496

    
497
    result = []
498
    for instance in self._config_data.instances.values():
499
      for disk in instance.disks:
500
        helper(disk, result)
501

    
502
    return result
503

    
504
  def _CheckDiskIDs(self, disk, l_ids, p_ids):
505
    """Compute duplicate disk IDs
506

507
    @type disk: L{objects.Disk}
508
    @param disk: the disk at which to start searching
509
    @type l_ids: list
510
    @param l_ids: list of current logical ids
511
    @type p_ids: list
512
    @param p_ids: list of current physical ids
513
    @rtype: list
514
    @return: a list of error messages
515

516
    """
517
    result = []
518
    if disk.logical_id is not None:
519
      if disk.logical_id in l_ids:
520
        result.append("duplicate logical id %s" % str(disk.logical_id))
521
      else:
522
        l_ids.append(disk.logical_id)
523
    if disk.physical_id is not None:
524
      if disk.physical_id in p_ids:
525
        result.append("duplicate physical id %s" % str(disk.physical_id))
526
      else:
527
        p_ids.append(disk.physical_id)
528

    
529
    if disk.children:
530
      for child in disk.children:
531
        result.extend(self._CheckDiskIDs(child, l_ids, p_ids))
532
    return result
533

    
534
  def _UnlockedVerifyConfig(self):
535
    """Verify function.
536

537
    @rtype: list
538
    @return: a list of error messages; a non-empty list signifies
539
        configuration errors
540

541
    """
542
    # pylint: disable=R0914
543
    result = []
544
    seen_macs = []
545
    ports = {}
546
    data = self._config_data
547
    cluster = data.cluster
548
    seen_lids = []
549
    seen_pids = []
550

    
551
    # global cluster checks
552
    if not cluster.enabled_hypervisors:
553
      result.append("enabled hypervisors list doesn't have any entries")
554
    invalid_hvs = set(cluster.enabled_hypervisors) - constants.HYPER_TYPES
555
    if invalid_hvs:
556
      result.append("enabled hypervisors contains invalid entries: %s" %
557
                    invalid_hvs)
558
    missing_hvp = (set(cluster.enabled_hypervisors) -
559
                   set(cluster.hvparams.keys()))
560
    if missing_hvp:
561
      result.append("hypervisor parameters missing for the enabled"
562
                    " hypervisor(s) %s" % utils.CommaJoin(missing_hvp))
563

    
564
    if cluster.master_node not in data.nodes:
565
      result.append("cluster has invalid primary node '%s'" %
566
                    cluster.master_node)
567

    
568
    def _helper(owner, attr, value, template):
569
      try:
570
        utils.ForceDictType(value, template)
571
      except errors.GenericError, err:
572
        result.append("%s has invalid %s: %s" % (owner, attr, err))
573

    
574
    def _helper_nic(owner, params):
575
      try:
576
        objects.NIC.CheckParameterSyntax(params)
577
      except errors.ConfigurationError, err:
578
        result.append("%s has invalid nicparams: %s" % (owner, err))
579

    
580
    def _helper_ipolicy(owner, params):
581
      try:
582
        objects.InstancePolicy.CheckParameterSyntax(params)
583
      except errors.ConfigurationError, err:
584
        result.append("%s has invalid instance policy: %s" % (owner, err))
585

    
586
    def _helper_ispecs(owner, params):
587
      for key, value in params.items():
588
        if key in constants.IPOLICY_ISPECS:
589
          fullkey = "ipolicy/" + key
590
          _helper(owner, fullkey, value, constants.ISPECS_PARAMETER_TYPES)
591
        else:
592
          # FIXME: assuming list type
593
          if key in constants.IPOLICY_PARAMETERS:
594
            exp_type = float
595
          else:
596
            exp_type = list
597
          if not isinstance(value, exp_type):
598
            result.append("%s has invalid instance policy: for %s,"
599
                          " expecting %s, got %s" %
600
                          (owner, key, exp_type.__name__, type(value)))
601

    
602
    # check cluster parameters
603
    _helper("cluster", "beparams", cluster.SimpleFillBE({}),
604
            constants.BES_PARAMETER_TYPES)
605
    _helper("cluster", "nicparams", cluster.SimpleFillNIC({}),
606
            constants.NICS_PARAMETER_TYPES)
607
    _helper_nic("cluster", cluster.SimpleFillNIC({}))
608
    _helper("cluster", "ndparams", cluster.SimpleFillND({}),
609
            constants.NDS_PARAMETER_TYPES)
610
    _helper_ipolicy("cluster", cluster.SimpleFillIPolicy({}))
611
    _helper_ispecs("cluster", cluster.SimpleFillIPolicy({}))
612

    
613
    # per-instance checks
614
    for instance_name in data.instances:
615
      instance = data.instances[instance_name]
616
      if instance.name != instance_name:
617
        result.append("instance '%s' is indexed by wrong name '%s'" %
618
                      (instance.name, instance_name))
619
      if instance.primary_node not in data.nodes:
620
        result.append("instance '%s' has invalid primary node '%s'" %
621
                      (instance_name, instance.primary_node))
622
      for snode in instance.secondary_nodes:
623
        if snode not in data.nodes:
624
          result.append("instance '%s' has invalid secondary node '%s'" %
625
                        (instance_name, snode))
626
      for idx, nic in enumerate(instance.nics):
627
        if nic.mac in seen_macs:
628
          result.append("instance '%s' has NIC %d mac %s duplicate" %
629
                        (instance_name, idx, nic.mac))
630
        else:
631
          seen_macs.append(nic.mac)
632
        if nic.nicparams:
633
          filled = cluster.SimpleFillNIC(nic.nicparams)
634
          owner = "instance %s nic %d" % (instance.name, idx)
635
          _helper(owner, "nicparams",
636
                  filled, constants.NICS_PARAMETER_TYPES)
637
          _helper_nic(owner, filled)
638

    
639
      # parameter checks
640
      if instance.beparams:
641
        _helper("instance %s" % instance.name, "beparams",
642
                cluster.FillBE(instance), constants.BES_PARAMETER_TYPES)
643

    
644
      # gather the drbd ports for duplicate checks
645
      for (idx, dsk) in enumerate(instance.disks):
646
        if dsk.dev_type in constants.LDS_DRBD:
647
          tcp_port = dsk.logical_id[2]
648
          if tcp_port not in ports:
649
            ports[tcp_port] = []
650
          ports[tcp_port].append((instance.name, "drbd disk %s" % idx))
651
      # gather network port reservation
652
      net_port = getattr(instance, "network_port", None)
653
      if net_port is not None:
654
        if net_port not in ports:
655
          ports[net_port] = []
656
        ports[net_port].append((instance.name, "network port"))
657

    
658
      # instance disk verify
659
      for idx, disk in enumerate(instance.disks):
660
        result.extend(["instance '%s' disk %d error: %s" %
661
                       (instance.name, idx, msg) for msg in disk.Verify()])
662
        result.extend(self._CheckDiskIDs(disk, seen_lids, seen_pids))
663

    
664
      wrong_names = _CheckInstanceDiskIvNames(instance.disks)
665
      if wrong_names:
666
        tmp = "; ".join(("name of disk %s should be '%s', but is '%s'" %
667
                         (idx, exp_name, actual_name))
668
                        for (idx, exp_name, actual_name) in wrong_names)
669

    
670
        result.append("Instance '%s' has wrongly named disks: %s" %
671
                      (instance.name, tmp))
672

    
673
    # cluster-wide pool of free ports
674
    for free_port in cluster.tcpudp_port_pool:
675
      if free_port not in ports:
676
        ports[free_port] = []
677
      ports[free_port].append(("cluster", "port marked as free"))
678

    
679
    # compute tcp/udp duplicate ports
680
    keys = ports.keys()
681
    keys.sort()
682
    for pnum in keys:
683
      pdata = ports[pnum]
684
      if len(pdata) > 1:
685
        txt = utils.CommaJoin(["%s/%s" % val for val in pdata])
686
        result.append("tcp/udp port %s has duplicates: %s" % (pnum, txt))
687

    
688
    # highest used tcp port check
689
    if keys:
690
      if keys[-1] > cluster.highest_used_port:
691
        result.append("Highest used port mismatch, saved %s, computed %s" %
692
                      (cluster.highest_used_port, keys[-1]))
693

    
694
    if not data.nodes[cluster.master_node].master_candidate:
695
      result.append("Master node is not a master candidate")
696

    
697
    # master candidate checks
698
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats()
699
    if mc_now < mc_max:
700
      result.append("Not enough master candidates: actual %d, target %d" %
701
                    (mc_now, mc_max))
702

    
703
    # node checks
704
    for node_name, node in data.nodes.items():
705
      if node.name != node_name:
706
        result.append("Node '%s' is indexed by wrong name '%s'" %
707
                      (node.name, node_name))
708
      if [node.master_candidate, node.drained, node.offline].count(True) > 1:
709
        result.append("Node %s state is invalid: master_candidate=%s,"
710
                      " drain=%s, offline=%s" %
711
                      (node.name, node.master_candidate, node.drained,
712
                       node.offline))
713
      if node.group not in data.nodegroups:
714
        result.append("Node '%s' has invalid group '%s'" %
715
                      (node.name, node.group))
716
      else:
717
        _helper("node %s" % node.name, "ndparams",
718
                cluster.FillND(node, data.nodegroups[node.group]),
719
                constants.NDS_PARAMETER_TYPES)
720

    
721
    # nodegroups checks
722
    nodegroups_names = set()
723
    for nodegroup_uuid in data.nodegroups:
724
      nodegroup = data.nodegroups[nodegroup_uuid]
725
      if nodegroup.uuid != nodegroup_uuid:
726
        result.append("node group '%s' (uuid: '%s') indexed by wrong uuid '%s'"
727
                      % (nodegroup.name, nodegroup.uuid, nodegroup_uuid))
728
      if utils.UUID_RE.match(nodegroup.name.lower()):
729
        result.append("node group '%s' (uuid: '%s') has uuid-like name" %
730
                      (nodegroup.name, nodegroup.uuid))
731
      if nodegroup.name in nodegroups_names:
732
        result.append("duplicate node group name '%s'" % nodegroup.name)
733
      else:
734
        nodegroups_names.add(nodegroup.name)
735
      group_name = "group %s" % nodegroup.name
736
      _helper_ipolicy(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy))
737
      _helper_ispecs(group_name, cluster.SimpleFillIPolicy(nodegroup.ipolicy))
738
      if nodegroup.ndparams:
739
        _helper(group_name, "ndparams",
740
                cluster.SimpleFillND(nodegroup.ndparams),
741
                constants.NDS_PARAMETER_TYPES)
742

    
743
    # drbd minors check
744
    _, duplicates = self._UnlockedComputeDRBDMap()
745
    for node, minor, instance_a, instance_b in duplicates:
746
      result.append("DRBD minor %d on node %s is assigned twice to instances"
747
                    " %s and %s" % (minor, node, instance_a, instance_b))
748

    
749
    # IP checks
750
    default_nicparams = cluster.nicparams[constants.PP_DEFAULT]
751
    ips = {}
752

    
753
    def _AddIpAddress(ip, name):
754
      ips.setdefault(ip, []).append(name)
755

    
756
    _AddIpAddress(cluster.master_ip, "cluster_ip")
757

    
758
    for node in data.nodes.values():
759
      _AddIpAddress(node.primary_ip, "node:%s/primary" % node.name)
760
      if node.secondary_ip != node.primary_ip:
761
        _AddIpAddress(node.secondary_ip, "node:%s/secondary" % node.name)
762

    
763
    for instance in data.instances.values():
764
      for idx, nic in enumerate(instance.nics):
765
        if nic.ip is None:
766
          continue
767

    
768
        nicparams = objects.FillDict(default_nicparams, nic.nicparams)
769
        nic_mode = nicparams[constants.NIC_MODE]
770
        nic_link = nicparams[constants.NIC_LINK]
771

    
772
        if nic_mode == constants.NIC_MODE_BRIDGED:
773
          link = "bridge:%s" % nic_link
774
        elif nic_mode == constants.NIC_MODE_ROUTED:
775
          link = "route:%s" % nic_link
776
        else:
777
          raise errors.ProgrammerError("NIC mode '%s' not handled" % nic_mode)
778

    
779
        _AddIpAddress("%s/%s/%s" % (link, nic.ip, nic.network),
780
                      "instance:%s/nic:%d" % (instance.name, idx))
781

    
782
    for ip, owners in ips.items():
783
      if len(owners) > 1:
784
        result.append("IP address %s is used by multiple owners: %s" %
785
                      (ip, utils.CommaJoin(owners)))
786

    
787
    return result
788

    
789
  @locking.ssynchronized(_config_lock, shared=1)
790
  def VerifyConfig(self):
791
    """Verify function.
792

793
    This is just a wrapper over L{_UnlockedVerifyConfig}.
794

795
    @rtype: list
796
    @return: a list of error messages; a non-empty list signifies
797
        configuration errors
798

799
    """
800
    return self._UnlockedVerifyConfig()
801

    
802
  def _UnlockedSetDiskID(self, disk, node_name):
803
    """Convert the unique ID to the ID needed on the target nodes.
804

805
    This is used only for drbd, which needs ip/port configuration.
806

807
    The routine descends down and updates its children also, because
808
    this helps when the only the top device is passed to the remote
809
    node.
810

811
    This function is for internal use, when the config lock is already held.
812

813
    """
814
    if disk.children:
815
      for child in disk.children:
816
        self._UnlockedSetDiskID(child, node_name)
817

    
818
    if disk.logical_id is None and disk.physical_id is not None:
819
      return
820
    if disk.dev_type == constants.LD_DRBD8:
821
      pnode, snode, port, pminor, sminor, secret = disk.logical_id
822
      if node_name not in (pnode, snode):
823
        raise errors.ConfigurationError("DRBD device not knowing node %s" %
824
                                        node_name)
825
      pnode_info = self._UnlockedGetNodeInfo(pnode)
826
      snode_info = self._UnlockedGetNodeInfo(snode)
827
      if pnode_info is None or snode_info is None:
828
        raise errors.ConfigurationError("Can't find primary or secondary node"
829
                                        " for %s" % str(disk))
830
      p_data = (pnode_info.secondary_ip, port)
831
      s_data = (snode_info.secondary_ip, port)
832
      if pnode == node_name:
833
        disk.physical_id = p_data + s_data + (pminor, secret)
834
      else: # it must be secondary, we tested above
835
        disk.physical_id = s_data + p_data + (sminor, secret)
836
    else:
837
      disk.physical_id = disk.logical_id
838
    return
839

    
840
  @locking.ssynchronized(_config_lock)
841
  def SetDiskID(self, disk, node_name):
842
    """Convert the unique ID to the ID needed on the target nodes.
843

844
    This is used only for drbd, which needs ip/port configuration.
845

846
    The routine descends down and updates its children also, because
847
    this helps when the only the top device is passed to the remote
848
    node.
849

850
    """
851
    return self._UnlockedSetDiskID(disk, node_name)
852

    
853
  @locking.ssynchronized(_config_lock)
854
  def AddTcpUdpPort(self, port):
855
    """Adds a new port to the available port pool.
856

857
    @warning: this method does not "flush" the configuration (via
858
        L{_WriteConfig}); callers should do that themselves once the
859
        configuration is stable
860

861
    """
862
    if not isinstance(port, int):
863
      raise errors.ProgrammerError("Invalid type passed for port")
864

    
865
    self._config_data.cluster.tcpudp_port_pool.add(port)
866

    
867
  @locking.ssynchronized(_config_lock, shared=1)
868
  def GetPortList(self):
869
    """Returns a copy of the current port list.
870

871
    """
872
    return self._config_data.cluster.tcpudp_port_pool.copy()
873

    
874
  @locking.ssynchronized(_config_lock)
875
  def AllocatePort(self):
876
    """Allocate a port.
877

878
    The port will be taken from the available port pool or from the
879
    default port range (and in this case we increase
880
    highest_used_port).
881

882
    """
883
    # If there are TCP/IP ports configured, we use them first.
884
    if self._config_data.cluster.tcpudp_port_pool:
885
      port = self._config_data.cluster.tcpudp_port_pool.pop()
886
    else:
887
      port = self._config_data.cluster.highest_used_port + 1
888
      if port >= constants.LAST_DRBD_PORT:
889
        raise errors.ConfigurationError("The highest used port is greater"
890
                                        " than %s. Aborting." %
891
                                        constants.LAST_DRBD_PORT)
892
      self._config_data.cluster.highest_used_port = port
893

    
894
    self._WriteConfig()
895
    return port
896

    
897
  def _UnlockedComputeDRBDMap(self):
898
    """Compute the used DRBD minor/nodes.
899

900
    @rtype: (dict, list)
901
    @return: dictionary of node_name: dict of minor: instance_name;
902
        the returned dict will have all the nodes in it (even if with
903
        an empty list), and a list of duplicates; if the duplicates
904
        list is not empty, the configuration is corrupted and its caller
905
        should raise an exception
906

907
    """
908
    def _AppendUsedPorts(instance_name, disk, used):
909
      duplicates = []
910
      if disk.dev_type == constants.LD_DRBD8 and len(disk.logical_id) >= 5:
911
        node_a, node_b, _, minor_a, minor_b = disk.logical_id[:5]
912
        for node, port in ((node_a, minor_a), (node_b, minor_b)):
913
          assert node in used, ("Node '%s' of instance '%s' not found"
914
                                " in node list" % (node, instance_name))
915
          if port in used[node]:
916
            duplicates.append((node, port, instance_name, used[node][port]))
917
          else:
918
            used[node][port] = instance_name
919
      if disk.children:
920
        for child in disk.children:
921
          duplicates.extend(_AppendUsedPorts(instance_name, child, used))
922
      return duplicates
923

    
924
    duplicates = []
925
    my_dict = dict((node, {}) for node in self._config_data.nodes)
926
    for instance in self._config_data.instances.itervalues():
927
      for disk in instance.disks:
928
        duplicates.extend(_AppendUsedPorts(instance.name, disk, my_dict))
929
    for (node, minor), instance in self._temporary_drbds.iteritems():
930
      if minor in my_dict[node] and my_dict[node][minor] != instance:
931
        duplicates.append((node, minor, instance, my_dict[node][minor]))
932
      else:
933
        my_dict[node][minor] = instance
934
    return my_dict, duplicates
935

    
936
  @locking.ssynchronized(_config_lock)
937
  def ComputeDRBDMap(self):
938
    """Compute the used DRBD minor/nodes.
939

940
    This is just a wrapper over L{_UnlockedComputeDRBDMap}.
941

942
    @return: dictionary of node_name: dict of minor: instance_name;
943
        the returned dict will have all the nodes in it (even if with
944
        an empty list).
945

946
    """
947
    d_map, duplicates = self._UnlockedComputeDRBDMap()
948
    if duplicates:
949
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
950
                                      str(duplicates))
951
    return d_map
952

    
953
  @locking.ssynchronized(_config_lock)
954
  def AllocateDRBDMinor(self, nodes, instance):
955
    """Allocate a drbd minor.
956

957
    The free minor will be automatically computed from the existing
958
    devices. A node can be given multiple times in order to allocate
959
    multiple minors. The result is the list of minors, in the same
960
    order as the passed nodes.
961

962
    @type instance: string
963
    @param instance: the instance for which we allocate minors
964

965
    """
966
    assert isinstance(instance, basestring), \
967
           "Invalid argument '%s' passed to AllocateDRBDMinor" % instance
968

    
969
    d_map, duplicates = self._UnlockedComputeDRBDMap()
970
    if duplicates:
971
      raise errors.ConfigurationError("Duplicate DRBD ports detected: %s" %
972
                                      str(duplicates))
973
    result = []
974
    for nname in nodes:
975
      ndata = d_map[nname]
976
      if not ndata:
977
        # no minors used, we can start at 0
978
        result.append(0)
979
        ndata[0] = instance
980
        self._temporary_drbds[(nname, 0)] = instance
981
        continue
982
      keys = ndata.keys()
983
      keys.sort()
984
      ffree = utils.FirstFree(keys)
985
      if ffree is None:
986
        # return the next minor
987
        # TODO: implement high-limit check
988
        minor = keys[-1] + 1
989
      else:
990
        minor = ffree
991
      # double-check minor against current instances
992
      assert minor not in d_map[nname], \
993
             ("Attempt to reuse allocated DRBD minor %d on node %s,"
994
              " already allocated to instance %s" %
995
              (minor, nname, d_map[nname][minor]))
996
      ndata[minor] = instance
997
      # double-check minor against reservation
998
      r_key = (nname, minor)
999
      assert r_key not in self._temporary_drbds, \
1000
             ("Attempt to reuse reserved DRBD minor %d on node %s,"
1001
              " reserved for instance %s" %
1002
              (minor, nname, self._temporary_drbds[r_key]))
1003
      self._temporary_drbds[r_key] = instance
1004
      result.append(minor)
1005
    logging.debug("Request to allocate drbd minors, input: %s, returning %s",
1006
                  nodes, result)
1007
    return result
1008

    
1009
  def _UnlockedReleaseDRBDMinors(self, instance):
1010
    """Release temporary drbd minors allocated for a given instance.
1011

1012
    @type instance: string
1013
    @param instance: the instance for which temporary minors should be
1014
                     released
1015

1016
    """
1017
    assert isinstance(instance, basestring), \
1018
           "Invalid argument passed to ReleaseDRBDMinors"
1019
    for key, name in self._temporary_drbds.items():
1020
      if name == instance:
1021
        del self._temporary_drbds[key]
1022

    
1023
  @locking.ssynchronized(_config_lock)
1024
  def ReleaseDRBDMinors(self, instance):
1025
    """Release temporary drbd minors allocated for a given instance.
1026

1027
    This should be called on the error paths, on the success paths
1028
    it's automatically called by the ConfigWriter add and update
1029
    functions.
1030

1031
    This function is just a wrapper over L{_UnlockedReleaseDRBDMinors}.
1032

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

1037
    """
1038
    self._UnlockedReleaseDRBDMinors(instance)
1039

    
1040
  @locking.ssynchronized(_config_lock, shared=1)
1041
  def GetConfigVersion(self):
1042
    """Get the configuration version.
1043

1044
    @return: Config version
1045

1046
    """
1047
    return self._config_data.version
1048

    
1049
  @locking.ssynchronized(_config_lock, shared=1)
1050
  def GetClusterName(self):
1051
    """Get cluster name.
1052

1053
    @return: Cluster name
1054

1055
    """
1056
    return self._config_data.cluster.cluster_name
1057

    
1058
  @locking.ssynchronized(_config_lock, shared=1)
1059
  def GetMasterNode(self):
1060
    """Get the hostname of the master node for this cluster.
1061

1062
    @return: Master hostname
1063

1064
    """
1065
    return self._config_data.cluster.master_node
1066

    
1067
  @locking.ssynchronized(_config_lock, shared=1)
1068
  def GetMasterIP(self):
1069
    """Get the IP of the master node for this cluster.
1070

1071
    @return: Master IP
1072

1073
    """
1074
    return self._config_data.cluster.master_ip
1075

    
1076
  @locking.ssynchronized(_config_lock, shared=1)
1077
  def GetMasterNetdev(self):
1078
    """Get the master network device for this cluster.
1079

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

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

1087
    """
1088
    return self._config_data.cluster.master_netmask
1089

    
1090
  @locking.ssynchronized(_config_lock, shared=1)
1091
  def GetUseExternalMipScript(self):
1092
    """Get flag representing whether to use the external master IP setup script.
1093

1094
    """
1095
    return self._config_data.cluster.use_external_mip_script
1096

    
1097
  @locking.ssynchronized(_config_lock, shared=1)
1098
  def GetFileStorageDir(self):
1099
    """Get the file storage dir for this cluster.
1100

1101
    """
1102
    return self._config_data.cluster.file_storage_dir
1103

    
1104
  @locking.ssynchronized(_config_lock, shared=1)
1105
  def GetSharedFileStorageDir(self):
1106
    """Get the shared file storage dir for this cluster.
1107

1108
    """
1109
    return self._config_data.cluster.shared_file_storage_dir
1110

    
1111
  @locking.ssynchronized(_config_lock, shared=1)
1112
  def GetHypervisorType(self):
1113
    """Get the hypervisor type for this cluster.
1114

1115
    """
1116
    return self._config_data.cluster.enabled_hypervisors[0]
1117

    
1118
  @locking.ssynchronized(_config_lock, shared=1)
1119
  def GetHostKey(self):
1120
    """Return the rsa hostkey from the config.
1121

1122
    @rtype: string
1123
    @return: the rsa hostkey
1124

1125
    """
1126
    return self._config_data.cluster.rsahostkeypub
1127

    
1128
  @locking.ssynchronized(_config_lock, shared=1)
1129
  def GetDefaultIAllocator(self):
1130
    """Get the default instance allocator for this cluster.
1131

1132
    """
1133
    return self._config_data.cluster.default_iallocator
1134

    
1135
  @locking.ssynchronized(_config_lock, shared=1)
1136
  def GetPrimaryIPFamily(self):
1137
    """Get cluster primary ip family.
1138

1139
    @return: primary ip family
1140

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

    
1144
  @locking.ssynchronized(_config_lock, shared=1)
1145
  def GetMasterNetworkParameters(self):
1146
    """Get network parameters of the master node.
1147

1148
    @rtype: L{object.MasterNetworkParameters}
1149
    @return: network parameters of the master node
1150

1151
    """
1152
    cluster = self._config_data.cluster
1153
    result = objects.MasterNetworkParameters(name=cluster.master_node,
1154
      ip=cluster.master_ip,
1155
      netmask=cluster.master_netmask,
1156
      netdev=cluster.master_netdev,
1157
      ip_family=cluster.primary_ip_family)
1158

    
1159
    return result
1160

    
1161
  @locking.ssynchronized(_config_lock)
1162
  def AddNodeGroup(self, group, ec_id, check_uuid=True):
1163
    """Add a node group to the configuration.
1164

1165
    This method calls group.UpgradeConfig() to fill any missing attributes
1166
    according to their default values.
1167

1168
    @type group: L{objects.NodeGroup}
1169
    @param group: the NodeGroup object to add
1170
    @type ec_id: string
1171
    @param ec_id: unique id for the job to use when creating a missing UUID
1172
    @type check_uuid: bool
1173
    @param check_uuid: add an UUID to the group if it doesn't have one or, if
1174
                       it does, ensure that it does not exist in the
1175
                       configuration already
1176

1177
    """
1178
    self._UnlockedAddNodeGroup(group, ec_id, check_uuid)
1179
    self._WriteConfig()
1180

    
1181
  def _UnlockedAddNodeGroup(self, group, ec_id, check_uuid):
1182
    """Add a node group to the configuration.
1183

1184
    """
1185
    logging.info("Adding node group %s to configuration", group.name)
1186

    
1187
    # Some code might need to add a node group with a pre-populated UUID
1188
    # generated with ConfigWriter.GenerateUniqueID(). We allow them to bypass
1189
    # the "does this UUID" exist already check.
1190
    if check_uuid:
1191
      self._EnsureUUID(group, ec_id)
1192

    
1193
    try:
1194
      existing_uuid = self._UnlockedLookupNodeGroup(group.name)
1195
    except errors.OpPrereqError:
1196
      pass
1197
    else:
1198
      raise errors.OpPrereqError("Desired group name '%s' already exists as a"
1199
                                 " node group (UUID: %s)" %
1200
                                 (group.name, existing_uuid),
1201
                                 errors.ECODE_EXISTS)
1202

    
1203
    group.serial_no = 1
1204
    group.ctime = group.mtime = time.time()
1205
    group.UpgradeConfig()
1206

    
1207
    self._config_data.nodegroups[group.uuid] = group
1208
    self._config_data.cluster.serial_no += 1
1209

    
1210
  @locking.ssynchronized(_config_lock)
1211
  def RemoveNodeGroup(self, group_uuid):
1212
    """Remove a node group from the configuration.
1213

1214
    @type group_uuid: string
1215
    @param group_uuid: the UUID of the node group to remove
1216

1217
    """
1218
    logging.info("Removing node group %s from configuration", group_uuid)
1219

    
1220
    if group_uuid not in self._config_data.nodegroups:
1221
      raise errors.ConfigurationError("Unknown node group '%s'" % group_uuid)
1222

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

    
1226
    del self._config_data.nodegroups[group_uuid]
1227
    self._config_data.cluster.serial_no += 1
1228
    self._WriteConfig()
1229

    
1230
  def _UnlockedLookupNodeGroup(self, target):
1231
    """Lookup a node group's UUID.
1232

1233
    @type target: string or None
1234
    @param target: group name or UUID or None to look for the default
1235
    @rtype: string
1236
    @return: nodegroup UUID
1237
    @raises errors.OpPrereqError: when the target group cannot be found
1238

1239
    """
1240
    if target is None:
1241
      if len(self._config_data.nodegroups) != 1:
1242
        raise errors.OpPrereqError("More than one node group exists. Target"
1243
                                   " group must be specified explicitely.")
1244
      else:
1245
        return self._config_data.nodegroups.keys()[0]
1246
    if target in self._config_data.nodegroups:
1247
      return target
1248
    for nodegroup in self._config_data.nodegroups.values():
1249
      if nodegroup.name == target:
1250
        return nodegroup.uuid
1251
    raise errors.OpPrereqError("Node group '%s' not found" % target,
1252
                               errors.ECODE_NOENT)
1253

    
1254
  @locking.ssynchronized(_config_lock, shared=1)
1255
  def LookupNodeGroup(self, target):
1256
    """Lookup a node group's UUID.
1257

1258
    This function is just a wrapper over L{_UnlockedLookupNodeGroup}.
1259

1260
    @type target: string or None
1261
    @param target: group name or UUID or None to look for the default
1262
    @rtype: string
1263
    @return: nodegroup UUID
1264

1265
    """
1266
    return self._UnlockedLookupNodeGroup(target)
1267

    
1268
  def _UnlockedGetNodeGroup(self, uuid):
1269
    """Lookup a node group.
1270

1271
    @type uuid: string
1272
    @param uuid: group UUID
1273
    @rtype: L{objects.NodeGroup} or None
1274
    @return: nodegroup object, or None if not found
1275

1276
    """
1277
    if uuid not in self._config_data.nodegroups:
1278
      return None
1279

    
1280
    return self._config_data.nodegroups[uuid]
1281

    
1282
  @locking.ssynchronized(_config_lock, shared=1)
1283
  def GetNodeGroup(self, uuid):
1284
    """Lookup a node group.
1285

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

1291
    """
1292
    return self._UnlockedGetNodeGroup(uuid)
1293

    
1294
  @locking.ssynchronized(_config_lock, shared=1)
1295
  def GetAllNodeGroupsInfo(self):
1296
    """Get the configuration of all node groups.
1297

1298
    """
1299
    return dict(self._config_data.nodegroups)
1300

    
1301
  @locking.ssynchronized(_config_lock, shared=1)
1302
  def GetNodeGroupList(self):
1303
    """Get a list of node groups.
1304

1305
    """
1306
    return self._config_data.nodegroups.keys()
1307

    
1308
  @locking.ssynchronized(_config_lock, shared=1)
1309
  def GetNodeGroupNames(self):
1310
    """Get a list of node group names
1311

1312
    """
1313
    names = [group.name
1314
             for group in self._config_data.nodegroups.values()]
1315
    return names
1316

    
1317

    
1318
  @locking.ssynchronized(_config_lock, shared=1)
1319
  def GetNodeGroupMembersByNodes(self, nodes):
1320
    """Get nodes which are member in the same nodegroups as the given nodes.
1321

1322
    """
1323
    ngfn = lambda node_name: self._UnlockedGetNodeInfo(node_name).group
1324
    return frozenset(member_name
1325
                     for node_name in nodes
1326
                     for member_name in
1327
                       self._UnlockedGetNodeGroup(ngfn(node_name)).members)
1328

    
1329
  @locking.ssynchronized(_config_lock, shared=1)
1330
  def GetMultiNodeGroupInfo(self, group_uuids):
1331
    """Get the configuration of multiple node groups.
1332

1333
    @param group_uuids: List of node group UUIDs
1334
    @rtype: list
1335
    @return: List of tuples of (group_uuid, group_info)
1336

1337
    """
1338
    return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]
1339

    
1340
  @locking.ssynchronized(_config_lock)
1341
  def AddInstance(self, instance, ec_id):
1342
    """Add an instance to the config.
1343

1344
    This should be used after creating a new instance.
1345

1346
    @type instance: L{objects.Instance}
1347
    @param instance: the instance object
1348

1349
    """
1350
    if not isinstance(instance, objects.Instance):
1351
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
1352

    
1353
    if instance.disk_template != constants.DT_DISKLESS:
1354
      all_lvs = instance.MapLVsByNode()
1355
      logging.info("Instance '%s' DISK_LAYOUT: %s", instance.name, all_lvs)
1356

    
1357
    all_macs = self._AllMACs()
1358
    for nic in instance.nics:
1359
      if nic.mac in all_macs:
1360
        raise errors.ConfigurationError("Cannot add instance %s:"
1361
                                        " MAC address '%s' already in use." %
1362
                                        (instance.name, nic.mac))
1363

    
1364
    self._EnsureUUID(instance, ec_id)
1365

    
1366
    instance.serial_no = 1
1367
    instance.ctime = instance.mtime = time.time()
1368
    self._config_data.instances[instance.name] = instance
1369
    self._config_data.cluster.serial_no += 1
1370
    self._UnlockedReleaseDRBDMinors(instance.name)
1371
    self._UnlockedCommitTemporaryIps(ec_id)
1372
    self._WriteConfig()
1373

    
1374
  def _EnsureUUID(self, item, ec_id):
1375
    """Ensures a given object has a valid UUID.
1376

1377
    @param item: the instance or node to be checked
1378
    @param ec_id: the execution context id for the uuid reservation
1379

1380
    """
1381
    if not item.uuid:
1382
      item.uuid = self._GenerateUniqueID(ec_id)
1383
    elif item.uuid in self._AllIDs(include_temporary=True):
1384
      raise errors.ConfigurationError("Cannot add '%s': UUID %s already"
1385
                                      " in use" % (item.name, item.uuid))
1386

    
1387
  def _SetInstanceStatus(self, instance_name, status):
1388
    """Set the instance's status to a given value.
1389

1390
    """
1391
    assert status in constants.ADMINST_ALL, \
1392
           "Invalid status '%s' passed to SetInstanceStatus" % (status,)
1393

    
1394
    if instance_name not in self._config_data.instances:
1395
      raise errors.ConfigurationError("Unknown instance '%s'" %
1396
                                      instance_name)
1397
    instance = self._config_data.instances[instance_name]
1398
    if instance.admin_state != status:
1399
      instance.admin_state = status
1400
      instance.serial_no += 1
1401
      instance.mtime = time.time()
1402
      self._WriteConfig()
1403

    
1404
  @locking.ssynchronized(_config_lock)
1405
  def MarkInstanceUp(self, instance_name):
1406
    """Mark the instance status to up in the config.
1407

1408
    """
1409
    self._SetInstanceStatus(instance_name, constants.ADMINST_UP)
1410

    
1411
  @locking.ssynchronized(_config_lock)
1412
  def MarkInstanceOffline(self, instance_name):
1413
    """Mark the instance status to down in the config.
1414

1415
    """
1416
    self._SetInstanceStatus(instance_name, constants.ADMINST_OFFLINE)
1417

    
1418
  @locking.ssynchronized(_config_lock)
1419
  def RemoveInstance(self, instance_name):
1420
    """Remove the instance from the configuration.
1421

1422
    """
1423
    if instance_name not in self._config_data.instances:
1424
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1425

    
1426
    # If a network port has been allocated to the instance,
1427
    # return it to the pool of free ports.
1428
    inst = self._config_data.instances[instance_name]
1429
    network_port = getattr(inst, "network_port", None)
1430
    if network_port is not None:
1431
      self._config_data.cluster.tcpudp_port_pool.add(network_port)
1432

    
1433
    instance = self._UnlockedGetInstanceInfo(instance_name)
1434

    
1435
    for nic in instance.nics:
1436
      if nic.network is not None and nic.ip is not None:
1437
        net_uuid = self._UnlockedLookupNetwork(nic.network)
1438
        if net_uuid:
1439
          # Return all IP addresses to the respective address pools
1440
          self._UnlockedCommitIp('release', net_uuid, nic.ip)
1441

    
1442

    
1443
    del self._config_data.instances[instance_name]
1444
    self._config_data.cluster.serial_no += 1
1445
    self._WriteConfig()
1446

    
1447
  @locking.ssynchronized(_config_lock)
1448
  def RenameInstance(self, old_name, new_name):
1449
    """Rename an instance.
1450

1451
    This needs to be done in ConfigWriter and not by RemoveInstance
1452
    combined with AddInstance as only we can guarantee an atomic
1453
    rename.
1454

1455
    """
1456
    if old_name not in self._config_data.instances:
1457
      raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
1458

    
1459
    # Operate on a copy to not loose instance object in case of a failure
1460
    inst = self._config_data.instances[old_name].Copy()
1461
    inst.name = new_name
1462

    
1463
    for (idx, disk) in enumerate(inst.disks):
1464
      if disk.dev_type == constants.LD_FILE:
1465
        # rename the file paths in logical and physical id
1466
        file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
1467
        disk.logical_id = (disk.logical_id[0],
1468
                           utils.PathJoin(file_storage_dir, inst.name,
1469
                                          "disk%s" % idx))
1470
        disk.physical_id = disk.logical_id
1471

    
1472
    # Actually replace instance object
1473
    del self._config_data.instances[old_name]
1474
    self._config_data.instances[inst.name] = inst
1475

    
1476
    # Force update of ssconf files
1477
    self._config_data.cluster.serial_no += 1
1478

    
1479
    self._WriteConfig()
1480

    
1481
  @locking.ssynchronized(_config_lock)
1482
  def MarkInstanceDown(self, instance_name):
1483
    """Mark the status of an instance to down in the configuration.
1484

1485
    """
1486
    self._SetInstanceStatus(instance_name, constants.ADMINST_DOWN)
1487

    
1488
  def _UnlockedGetInstanceList(self):
1489
    """Get the list of instances.
1490

1491
    This function is for internal use, when the config lock is already held.
1492

1493
    """
1494
    return self._config_data.instances.keys()
1495

    
1496
  @locking.ssynchronized(_config_lock, shared=1)
1497
  def GetInstanceList(self):
1498
    """Get the list of instances.
1499

1500
    @return: array of instances, ex. ['instance2.example.com',
1501
        'instance1.example.com']
1502

1503
    """
1504
    return self._UnlockedGetInstanceList()
1505

    
1506
  def ExpandInstanceName(self, short_name):
1507
    """Attempt to expand an incomplete instance name.
1508

1509
    """
1510
    # Locking is done in L{ConfigWriter.GetInstanceList}
1511
    return _MatchNameComponentIgnoreCase(short_name, self.GetInstanceList())
1512

    
1513
  def _UnlockedGetInstanceInfo(self, instance_name):
1514
    """Returns information about an instance.
1515

1516
    This function is for internal use, when the config lock is already held.
1517

1518
    """
1519
    if instance_name not in self._config_data.instances:
1520
      return None
1521

    
1522
    return self._config_data.instances[instance_name]
1523

    
1524
  @locking.ssynchronized(_config_lock, shared=1)
1525
  def GetInstanceInfo(self, instance_name):
1526
    """Returns information about an instance.
1527

1528
    It takes the information from the configuration file. Other information of
1529
    an instance are taken from the live systems.
1530

1531
    @param instance_name: name of the instance, e.g.
1532
        I{instance1.example.com}
1533

1534
    @rtype: L{objects.Instance}
1535
    @return: the instance object
1536

1537
    """
1538
    return self._UnlockedGetInstanceInfo(instance_name)
1539

    
1540
  @locking.ssynchronized(_config_lock, shared=1)
1541
  def GetInstanceNodeGroups(self, instance_name, primary_only=False):
1542
    """Returns set of node group UUIDs for instance's nodes.
1543

1544
    @rtype: frozenset
1545

1546
    """
1547
    instance = self._UnlockedGetInstanceInfo(instance_name)
1548
    if not instance:
1549
      raise errors.ConfigurationError("Unknown instance '%s'" % instance_name)
1550

    
1551
    if primary_only:
1552
      nodes = [instance.primary_node]
1553
    else:
1554
      nodes = instance.all_nodes
1555

    
1556
    return frozenset(self._UnlockedGetNodeInfo(node_name).group
1557
                     for node_name in nodes)
1558

    
1559
  @locking.ssynchronized(_config_lock, shared=1)
1560
  def GetMultiInstanceInfo(self, instances):
1561
    """Get the configuration of multiple instances.
1562

1563
    @param instances: list of instance names
1564
    @rtype: list
1565
    @return: list of tuples (instance, instance_info), where
1566
        instance_info is what would GetInstanceInfo return for the
1567
        node, while keeping the original order
1568

1569
    """
1570
    return [(name, self._UnlockedGetInstanceInfo(name)) for name in instances]
1571

    
1572
  @locking.ssynchronized(_config_lock, shared=1)
1573
  def GetAllInstancesInfo(self):
1574
    """Get the configuration of all instances.
1575

1576
    @rtype: dict
1577
    @return: dict of (instance, instance_info), where instance_info is what
1578
              would GetInstanceInfo return for the node
1579

1580
    """
1581
    my_dict = dict([(instance, self._UnlockedGetInstanceInfo(instance))
1582
                    for instance in self._UnlockedGetInstanceList()])
1583
    return my_dict
1584

    
1585
  @locking.ssynchronized(_config_lock, shared=1)
1586
  def GetInstancesInfoByFilter(self, filter_fn):
1587
    """Get instance configuration with a filter.
1588

1589
    @type filter_fn: callable
1590
    @param filter_fn: Filter function receiving instance object as parameter,
1591
      returning boolean. Important: this function is called while the
1592
      configuration locks is held. It must not do any complex work or call
1593
      functions potentially leading to a deadlock. Ideally it doesn't call any
1594
      other functions and just compares instance attributes.
1595

1596
    """
1597
    return dict((name, inst)
1598
                for (name, inst) in self._config_data.instances.items()
1599
                if filter_fn(inst))
1600

    
1601
  @locking.ssynchronized(_config_lock)
1602
  def AddNode(self, node, ec_id):
1603
    """Add a node to the configuration.
1604

1605
    @type node: L{objects.Node}
1606
    @param node: a Node instance
1607

1608
    """
1609
    logging.info("Adding node %s to configuration", node.name)
1610

    
1611
    self._EnsureUUID(node, ec_id)
1612

    
1613
    node.serial_no = 1
1614
    node.ctime = node.mtime = time.time()
1615
    self._UnlockedAddNodeToGroup(node.name, node.group)
1616
    self._config_data.nodes[node.name] = node
1617
    self._config_data.cluster.serial_no += 1
1618
    self._WriteConfig()
1619

    
1620
  @locking.ssynchronized(_config_lock)
1621
  def RemoveNode(self, node_name):
1622
    """Remove a node from the configuration.
1623

1624
    """
1625
    logging.info("Removing node %s from configuration", node_name)
1626

    
1627
    if node_name not in self._config_data.nodes:
1628
      raise errors.ConfigurationError("Unknown node '%s'" % node_name)
1629

    
1630
    self._UnlockedRemoveNodeFromGroup(self._config_data.nodes[node_name])
1631
    del self._config_data.nodes[node_name]
1632
    self._config_data.cluster.serial_no += 1
1633
    self._WriteConfig()
1634

    
1635
  def ExpandNodeName(self, short_name):
1636
    """Attempt to expand an incomplete node name.
1637

1638
    """
1639
    # Locking is done in L{ConfigWriter.GetNodeList}
1640
    return _MatchNameComponentIgnoreCase(short_name, self.GetNodeList())
1641

    
1642
  def _UnlockedGetNodeInfo(self, node_name):
1643
    """Get the configuration of a node, as stored in the config.
1644

1645
    This function is for internal use, when the config lock is already
1646
    held.
1647

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

1650
    @rtype: L{objects.Node}
1651
    @return: the node object
1652

1653
    """
1654
    if node_name not in self._config_data.nodes:
1655
      return None
1656

    
1657
    return self._config_data.nodes[node_name]
1658

    
1659
  @locking.ssynchronized(_config_lock, shared=1)
1660
  def GetNodeInfo(self, node_name):
1661
    """Get the configuration of a node, as stored in the config.
1662

1663
    This is just a locked wrapper over L{_UnlockedGetNodeInfo}.
1664

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

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

1670
    """
1671
    return self._UnlockedGetNodeInfo(node_name)
1672

    
1673
  @locking.ssynchronized(_config_lock, shared=1)
1674
  def GetNodeInstances(self, node_name):
1675
    """Get the instances of a node, as stored in the config.
1676

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

1679
    @rtype: (list, list)
1680
    @return: a tuple with two lists: the primary and the secondary instances
1681

1682
    """
1683
    pri = []
1684
    sec = []
1685
    for inst in self._config_data.instances.values():
1686
      if inst.primary_node == node_name:
1687
        pri.append(inst.name)
1688
      if node_name in inst.secondary_nodes:
1689
        sec.append(inst.name)
1690
    return (pri, sec)
1691

    
1692
  @locking.ssynchronized(_config_lock, shared=1)
1693
  def GetNodeGroupInstances(self, uuid, primary_only=False):
1694
    """Get the instances of a node group.
1695

1696
    @param uuid: Node group UUID
1697
    @param primary_only: Whether to only consider primary nodes
1698
    @rtype: frozenset
1699
    @return: List of instance names in node group
1700

1701
    """
1702
    if primary_only:
1703
      nodes_fn = lambda inst: [inst.primary_node]
1704
    else:
1705
      nodes_fn = lambda inst: inst.all_nodes
1706

    
1707
    return frozenset(inst.name
1708
                     for inst in self._config_data.instances.values()
1709
                     for node_name in nodes_fn(inst)
1710
                     if self._UnlockedGetNodeInfo(node_name).group == uuid)
1711

    
1712
  def _UnlockedGetNodeList(self):
1713
    """Return the list of nodes which are in the configuration.
1714

1715
    This function is for internal use, when the config lock is already
1716
    held.
1717

1718
    @rtype: list
1719

1720
    """
1721
    return self._config_data.nodes.keys()
1722

    
1723
  @locking.ssynchronized(_config_lock, shared=1)
1724
  def GetNodeList(self):
1725
    """Return the list of nodes which are in the configuration.
1726

1727
    """
1728
    return self._UnlockedGetNodeList()
1729

    
1730
  def _UnlockedGetOnlineNodeList(self):
1731
    """Return the list of nodes which are online.
1732

1733
    """
1734
    all_nodes = [self._UnlockedGetNodeInfo(node)
1735
                 for node in self._UnlockedGetNodeList()]
1736
    return [node.name for node in all_nodes if not node.offline]
1737

    
1738
  @locking.ssynchronized(_config_lock, shared=1)
1739
  def GetOnlineNodeList(self):
1740
    """Return the list of nodes which are online.
1741

1742
    """
1743
    return self._UnlockedGetOnlineNodeList()
1744

    
1745
  @locking.ssynchronized(_config_lock, shared=1)
1746
  def GetVmCapableNodeList(self):
1747
    """Return the list of nodes which are not vm capable.
1748

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

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

1758
    """
1759
    all_nodes = [self._UnlockedGetNodeInfo(node)
1760
                 for node in self._UnlockedGetNodeList()]
1761
    return [node.name for node in all_nodes if not node.vm_capable]
1762

    
1763
  @locking.ssynchronized(_config_lock, shared=1)
1764
  def GetMultiNodeInfo(self, nodes):
1765
    """Get the configuration of multiple nodes.
1766

1767
    @param nodes: list of node names
1768
    @rtype: list
1769
    @return: list of tuples of (node, node_info), where node_info is
1770
        what would GetNodeInfo return for the node, in the original
1771
        order
1772

1773
    """
1774
    return [(name, self._UnlockedGetNodeInfo(name)) for name in nodes]
1775

    
1776
  @locking.ssynchronized(_config_lock, shared=1)
1777
  def GetAllNodesInfo(self):
1778
    """Get the configuration of all nodes.
1779

1780
    @rtype: dict
1781
    @return: dict of (node, node_info), where node_info is what
1782
              would GetNodeInfo return for the node
1783

1784
    """
1785
    return self._UnlockedGetAllNodesInfo()
1786

    
1787
  def _UnlockedGetAllNodesInfo(self):
1788
    """Gets configuration of all nodes.
1789

1790
    @note: See L{GetAllNodesInfo}
1791

1792
    """
1793
    return dict([(node, self._UnlockedGetNodeInfo(node))
1794
                 for node in self._UnlockedGetNodeList()])
1795

    
1796
  @locking.ssynchronized(_config_lock, shared=1)
1797
  def GetNodeGroupsFromNodes(self, nodes):
1798
    """Returns groups for a list of nodes.
1799

1800
    @type nodes: list of string
1801
    @param nodes: List of node names
1802
    @rtype: frozenset
1803

1804
    """
1805
    return frozenset(self._UnlockedGetNodeInfo(name).group for name in nodes)
1806

    
1807
  def _UnlockedGetMasterCandidateStats(self, exceptions=None):
1808
    """Get the number of current and maximum desired and possible candidates.
1809

1810
    @type exceptions: list
1811
    @param exceptions: if passed, list of nodes that should be ignored
1812
    @rtype: tuple
1813
    @return: tuple of (current, desired and possible, possible)
1814

1815
    """
1816
    mc_now = mc_should = mc_max = 0
1817
    for node in self._config_data.nodes.values():
1818
      if exceptions and node.name in exceptions:
1819
        continue
1820
      if not (node.offline or node.drained) and node.master_capable:
1821
        mc_max += 1
1822
      if node.master_candidate:
1823
        mc_now += 1
1824
    mc_should = min(mc_max, self._config_data.cluster.candidate_pool_size)
1825
    return (mc_now, mc_should, mc_max)
1826

    
1827
  @locking.ssynchronized(_config_lock, shared=1)
1828
  def GetMasterCandidateStats(self, exceptions=None):
1829
    """Get the number of current and maximum possible candidates.
1830

1831
    This is just a wrapper over L{_UnlockedGetMasterCandidateStats}.
1832

1833
    @type exceptions: list
1834
    @param exceptions: if passed, list of nodes that should be ignored
1835
    @rtype: tuple
1836
    @return: tuple of (current, max)
1837

1838
    """
1839
    return self._UnlockedGetMasterCandidateStats(exceptions)
1840

    
1841
  @locking.ssynchronized(_config_lock)
1842
  def MaintainCandidatePool(self, exceptions):
1843
    """Try to grow the candidate pool to the desired size.
1844

1845
    @type exceptions: list
1846
    @param exceptions: if passed, list of nodes that should be ignored
1847
    @rtype: list
1848
    @return: list with the adjusted nodes (L{objects.Node} instances)
1849

1850
    """
1851
    mc_now, mc_max, _ = self._UnlockedGetMasterCandidateStats(exceptions)
1852
    mod_list = []
1853
    if mc_now < mc_max:
1854
      node_list = self._config_data.nodes.keys()
1855
      random.shuffle(node_list)
1856
      for name in node_list:
1857
        if mc_now >= mc_max:
1858
          break
1859
        node = self._config_data.nodes[name]
1860
        if (node.master_candidate or node.offline or node.drained or
1861
            node.name in exceptions or not node.master_capable):
1862
          continue
1863
        mod_list.append(node)
1864
        node.master_candidate = True
1865
        node.serial_no += 1
1866
        mc_now += 1
1867
      if mc_now != mc_max:
1868
        # this should not happen
1869
        logging.warning("Warning: MaintainCandidatePool didn't manage to"
1870
                        " fill the candidate pool (%d/%d)", mc_now, mc_max)
1871
      if mod_list:
1872
        self._config_data.cluster.serial_no += 1
1873
        self._WriteConfig()
1874

    
1875
    return mod_list
1876

    
1877
  def _UnlockedAddNodeToGroup(self, node_name, nodegroup_uuid):
1878
    """Add a given node to the specified group.
1879

1880
    """
1881
    if nodegroup_uuid not in self._config_data.nodegroups:
1882
      # This can happen if a node group gets deleted between its lookup and
1883
      # when we're adding the first node to it, since we don't keep a lock in
1884
      # the meantime. It's ok though, as we'll fail cleanly if the node group
1885
      # is not found anymore.
1886
      raise errors.OpExecError("Unknown node group: %s" % nodegroup_uuid)
1887
    if node_name not in self._config_data.nodegroups[nodegroup_uuid].members:
1888
      self._config_data.nodegroups[nodegroup_uuid].members.append(node_name)
1889

    
1890
  def _UnlockedRemoveNodeFromGroup(self, node):
1891
    """Remove a given node from its group.
1892

1893
    """
1894
    nodegroup = node.group
1895
    if nodegroup not in self._config_data.nodegroups:
1896
      logging.warning("Warning: node '%s' has unknown node group '%s'"
1897
                      " (while being removed from it)", node.name, nodegroup)
1898
    nodegroup_obj = self._config_data.nodegroups[nodegroup]
1899
    if node.name not in nodegroup_obj.members:
1900
      logging.warning("Warning: node '%s' not a member of its node group '%s'"
1901
                      " (while being removed from it)", node.name, nodegroup)
1902
    else:
1903
      nodegroup_obj.members.remove(node.name)
1904

    
1905
  @locking.ssynchronized(_config_lock)
1906
  def AssignGroupNodes(self, mods):
1907
    """Changes the group of a number of nodes.
1908

1909
    @type mods: list of tuples; (node name, new group UUID)
1910
    @param mods: Node membership modifications
1911

1912
    """
1913
    groups = self._config_data.nodegroups
1914
    nodes = self._config_data.nodes
1915

    
1916
    resmod = []
1917

    
1918
    # Try to resolve names/UUIDs first
1919
    for (node_name, new_group_uuid) in mods:
1920
      try:
1921
        node = nodes[node_name]
1922
      except KeyError:
1923
        raise errors.ConfigurationError("Unable to find node '%s'" % node_name)
1924

    
1925
      if node.group == new_group_uuid:
1926
        # Node is being assigned to its current group
1927
        logging.debug("Node '%s' was assigned to its current group (%s)",
1928
                      node_name, node.group)
1929
        continue
1930

    
1931
      # Try to find current group of node
1932
      try:
1933
        old_group = groups[node.group]
1934
      except KeyError:
1935
        raise errors.ConfigurationError("Unable to find old group '%s'" %
1936
                                        node.group)
1937

    
1938
      # Try to find new group for node
1939
      try:
1940
        new_group = groups[new_group_uuid]
1941
      except KeyError:
1942
        raise errors.ConfigurationError("Unable to find new group '%s'" %
1943
                                        new_group_uuid)
1944

    
1945
      assert node.name in old_group.members, \
1946
        ("Inconsistent configuration: node '%s' not listed in members for its"
1947
         " old group '%s'" % (node.name, old_group.uuid))
1948
      assert node.name not in new_group.members, \
1949
        ("Inconsistent configuration: node '%s' already listed in members for"
1950
         " its new group '%s'" % (node.name, new_group.uuid))
1951

    
1952
      resmod.append((node, old_group, new_group))
1953

    
1954
    # Apply changes
1955
    for (node, old_group, new_group) in resmod:
1956
      assert node.uuid != new_group.uuid and old_group.uuid != new_group.uuid, \
1957
        "Assigning to current group is not possible"
1958

    
1959
      node.group = new_group.uuid
1960

    
1961
      # Update members of involved groups
1962
      if node.name in old_group.members:
1963
        old_group.members.remove(node.name)
1964
      if node.name not in new_group.members:
1965
        new_group.members.append(node.name)
1966

    
1967
    # Update timestamps and serials (only once per node/group object)
1968
    now = time.time()
1969
    for obj in frozenset(itertools.chain(*resmod)): # pylint: disable=W0142
1970
      obj.serial_no += 1
1971
      obj.mtime = now
1972

    
1973
    # Force ssconf update
1974
    self._config_data.cluster.serial_no += 1
1975

    
1976
    self._WriteConfig()
1977

    
1978
  def _BumpSerialNo(self):
1979
    """Bump up the serial number of the config.
1980

1981
    """
1982
    self._config_data.serial_no += 1
1983
    self._config_data.mtime = time.time()
1984

    
1985
  def _AllUUIDObjects(self):
1986
    """Returns all objects with uuid attributes.
1987

1988
    """
1989
    return (self._config_data.instances.values() +
1990
            self._config_data.nodes.values() +
1991
            self._config_data.nodegroups.values() +
1992
            [self._config_data.cluster])
1993

    
1994
  def _OpenConfig(self, accept_foreign):
1995
    """Read the config data from disk.
1996

1997
    """
1998
    raw_data = utils.ReadFile(self._cfg_file)
1999

    
2000
    try:
2001
      data = objects.ConfigData.FromDict(serializer.Load(raw_data))
2002
    except Exception, err:
2003
      raise errors.ConfigurationError(err)
2004

    
2005
    # Make sure the configuration has the right version
2006
    _ValidateConfig(data)
2007

    
2008
    if (not hasattr(data, "cluster") or
2009
        not hasattr(data.cluster, "rsahostkeypub")):
2010
      raise errors.ConfigurationError("Incomplete configuration"
2011
                                      " (missing cluster.rsahostkeypub)")
2012

    
2013
    if data.cluster.master_node != self._my_hostname and not accept_foreign:
2014
      msg = ("The configuration denotes node %s as master, while my"
2015
             " hostname is %s; opening a foreign configuration is only"
2016
             " possible in accept_foreign mode" %
2017
             (data.cluster.master_node, self._my_hostname))
2018
      raise errors.ConfigurationError(msg)
2019

    
2020
    # Upgrade configuration if needed
2021
    data.UpgradeConfig()
2022

    
2023
    self._config_data = data
2024
    # reset the last serial as -1 so that the next write will cause
2025
    # ssconf update
2026
    self._last_cluster_serial = -1
2027

    
2028
    # And finally run our (custom) config upgrade sequence
2029
    self._UpgradeConfig()
2030

    
2031
    self._cfg_id = utils.GetFileID(path=self._cfg_file)
2032

    
2033
  def _UpgradeConfig(self):
2034
    """Run upgrade steps that cannot be done purely in the objects.
2035

2036
    This is because some data elements need uniqueness across the
2037
    whole configuration, etc.
2038

2039
    @warning: this function will call L{_WriteConfig()}, but also
2040
        L{DropECReservations} so it needs to be called only from a
2041
        "safe" place (the constructor). If one wanted to call it with
2042
        the lock held, a DropECReservationUnlocked would need to be
2043
        created first, to avoid causing deadlock.
2044

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

    
2072
  def _DistributeConfig(self, feedback_fn):
2073
    """Distribute the configuration to the other nodes.
2074

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

2078
    """
2079
    if self._offline:
2080
      return True
2081

    
2082
    bad = False
2083

    
2084
    node_list = []
2085
    addr_list = []
2086
    myhostname = self._my_hostname
2087
    # we can skip checking whether _UnlockedGetNodeInfo returns None
2088
    # since the node list comes from _UnlocketGetNodeList, and we are
2089
    # called with the lock held, so no modifications should take place
2090
    # in between
2091
    for node_name in self._UnlockedGetNodeList():
2092
      if node_name == myhostname:
2093
        continue
2094
      node_info = self._UnlockedGetNodeInfo(node_name)
2095
      if not node_info.master_candidate:
2096
        continue
2097
      node_list.append(node_info.name)
2098
      addr_list.append(node_info.primary_ip)
2099

    
2100
    # TODO: Use dedicated resolver talking to config writer for name resolution
2101
    result = \
2102
      self._GetRpc(addr_list).call_upload_file(node_list, self._cfg_file)
2103
    for to_node, to_result in result.items():
2104
      msg = to_result.fail_msg
2105
      if msg:
2106
        msg = ("Copy of file %s to node %s failed: %s" %
2107
               (self._cfg_file, to_node, msg))
2108
        logging.error(msg)
2109

    
2110
        if feedback_fn:
2111
          feedback_fn(msg)
2112

    
2113
        bad = True
2114

    
2115
    return not bad
2116

    
2117
  def _WriteConfig(self, destination=None, feedback_fn=None):
2118
    """Write the configuration data to persistent storage.
2119

2120
    """
2121
    assert feedback_fn is None or callable(feedback_fn)
2122

    
2123
    # Warn on config errors, but don't abort the save - the
2124
    # configuration has already been modified, and we can't revert;
2125
    # the best we can do is to warn the user and save as is, leaving
2126
    # recovery to the user
2127
    config_errors = self._UnlockedVerifyConfig()
2128
    if config_errors:
2129
      errmsg = ("Configuration data is not consistent: %s" %
2130
                (utils.CommaJoin(config_errors)))
2131
      logging.critical(errmsg)
2132
      if feedback_fn:
2133
        feedback_fn(errmsg)
2134

    
2135
    if destination is None:
2136
      destination = self._cfg_file
2137
    self._BumpSerialNo()
2138
    txt = serializer.Dump(self._config_data.ToDict())
2139

    
2140
    getents = self._getents()
2141
    try:
2142
      fd = utils.SafeWriteFile(destination, self._cfg_id, data=txt,
2143
                               close=False, gid=getents.confd_gid, mode=0640)
2144
    except errors.LockError:
2145
      raise errors.ConfigurationError("The configuration file has been"
2146
                                      " modified since the last write, cannot"
2147
                                      " update")
2148
    try:
2149
      self._cfg_id = utils.GetFileID(fd=fd)
2150
    finally:
2151
      os.close(fd)
2152

    
2153
    self.write_count += 1
2154

    
2155
    # and redistribute the config file to master candidates
2156
    self._DistributeConfig(feedback_fn)
2157

    
2158
    # Write ssconf files on all nodes (including locally)
2159
    if self._last_cluster_serial < self._config_data.cluster.serial_no:
2160
      if not self._offline:
2161
        result = self._GetRpc(None).call_write_ssconf_files(
2162
          self._UnlockedGetOnlineNodeList(),
2163
          self._UnlockedGetSsconfValues())
2164

    
2165
        for nname, nresu in result.items():
2166
          msg = nresu.fail_msg
2167
          if msg:
2168
            errmsg = ("Error while uploading ssconf files to"
2169
                      " node %s: %s" % (nname, msg))
2170
            logging.warning(errmsg)
2171

    
2172
            if feedback_fn:
2173
              feedback_fn(errmsg)
2174

    
2175
      self._last_cluster_serial = self._config_data.cluster.serial_no
2176

    
2177
  def _UnlockedGetSsconfValues(self):
2178
    """Return the values needed by ssconf.
2179

2180
    @rtype: dict
2181
    @return: a dictionary with keys the ssconf names and values their
2182
        associated value
2183

2184
    """
2185
    fn = "\n".join
2186
    instance_names = utils.NiceSort(self._UnlockedGetInstanceList())
2187
    node_names = utils.NiceSort(self._UnlockedGetNodeList())
2188
    node_info = [self._UnlockedGetNodeInfo(name) for name in node_names]
2189
    node_pri_ips = ["%s %s" % (ninfo.name, ninfo.primary_ip)
2190
                    for ninfo in node_info]
2191
    node_snd_ips = ["%s %s" % (ninfo.name, ninfo.secondary_ip)
2192
                    for ninfo in node_info]
2193

    
2194
    instance_data = fn(instance_names)
2195
    off_data = fn(node.name for node in node_info if node.offline)
2196
    on_data = fn(node.name for node in node_info if not node.offline)
2197
    mc_data = fn(node.name for node in node_info if node.master_candidate)
2198
    mc_ips_data = fn(node.primary_ip for node in node_info
2199
                     if node.master_candidate)
2200
    node_data = fn(node_names)
2201
    node_pri_ips_data = fn(node_pri_ips)
2202
    node_snd_ips_data = fn(node_snd_ips)
2203

    
2204
    cluster = self._config_data.cluster
2205
    cluster_tags = fn(cluster.GetTags())
2206

    
2207
    hypervisor_list = fn(cluster.enabled_hypervisors)
2208

    
2209
    uid_pool = uidpool.FormatUidPool(cluster.uid_pool, separator="\n")
2210

    
2211
    nodegroups = ["%s %s" % (nodegroup.uuid, nodegroup.name) for nodegroup in
2212
                  self._config_data.nodegroups.values()]
2213
    nodegroups_data = fn(utils.NiceSort(nodegroups))
2214
    networks = ["%s %s" % (net.uuid, net.name) for net in
2215
                self._config_data.networks.values()]
2216
    networks_data = fn(utils.NiceSort(networks))
2217

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

    
2251
  @locking.ssynchronized(_config_lock, shared=1)
2252
  def GetSsconfValues(self):
2253
    """Wrapper using lock around _UnlockedGetSsconf().
2254

2255
    """
2256
    return self._UnlockedGetSsconfValues()
2257

    
2258
  @locking.ssynchronized(_config_lock, shared=1)
2259
  def GetVGName(self):
2260
    """Return the volume group name.
2261

2262
    """
2263
    return self._config_data.cluster.volume_group_name
2264

    
2265
  @locking.ssynchronized(_config_lock)
2266
  def SetVGName(self, vg_name):
2267
    """Set the volume group name.
2268

2269
    """
2270
    self._config_data.cluster.volume_group_name = vg_name
2271
    self._config_data.cluster.serial_no += 1
2272
    self._WriteConfig()
2273

    
2274
  @locking.ssynchronized(_config_lock, shared=1)
2275
  def GetDRBDHelper(self):
2276
    """Return DRBD usermode helper.
2277

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

    
2281
  @locking.ssynchronized(_config_lock)
2282
  def SetDRBDHelper(self, drbd_helper):
2283
    """Set DRBD usermode helper.
2284

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

    
2290
  @locking.ssynchronized(_config_lock, shared=1)
2291
  def GetMACPrefix(self):
2292
    """Return the mac prefix.
2293

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

    
2297
  @locking.ssynchronized(_config_lock, shared=1)
2298
  def GetClusterInfo(self):
2299
    """Returns information about the cluster
2300

2301
    @rtype: L{objects.Cluster}
2302
    @return: the cluster object
2303

2304
    """
2305
    return self._config_data.cluster
2306

    
2307
  @locking.ssynchronized(_config_lock, shared=1)
2308
  def HasAnyDiskOfType(self, dev_type):
2309
    """Check if in there is at disk of the given type in the configuration.
2310

2311
    """
2312
    return self._config_data.HasAnyDiskOfType(dev_type)
2313

    
2314
  @locking.ssynchronized(_config_lock)
2315
  def Update(self, target, feedback_fn):
2316
    """Notify function to be called after updates.
2317

2318
    This function must be called when an object (as returned by
2319
    GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
2320
    caller wants the modifications saved to the backing store. Note
2321
    that all modified objects will be saved, but the target argument
2322
    is the one the caller wants to ensure that it's saved.
2323

2324
    @param target: an instance of either L{objects.Cluster},
2325
        L{objects.Node} or L{objects.Instance} which is existing in
2326
        the cluster
2327
    @param feedback_fn: Callable feedback function
2328

2329
    """
2330
    if self._config_data is None:
2331
      raise errors.ProgrammerError("Configuration file not read,"
2332
                                   " cannot save.")
2333
    update_serial = False
2334
    if isinstance(target, objects.Cluster):
2335
      test = target == self._config_data.cluster
2336
    elif isinstance(target, objects.Node):
2337
      test = target in self._config_data.nodes.values()
2338
      update_serial = True
2339
    elif isinstance(target, objects.Instance):
2340
      test = target in self._config_data.instances.values()
2341
    elif isinstance(target, objects.NodeGroup):
2342
      test = target in self._config_data.nodegroups.values()
2343
    elif isinstance(target, objects.Network):
2344
      test = target in self._config_data.networks.values()
2345
    else:
2346
      raise errors.ProgrammerError("Invalid object type (%s) passed to"
2347
                                   " ConfigWriter.Update" % type(target))
2348
    if not test:
2349
      raise errors.ConfigurationError("Configuration updated since object"
2350
                                      " has been read or unknown object")
2351
    target.serial_no += 1
2352
    target.mtime = now = time.time()
2353

    
2354
    if update_serial:
2355
      # for node updates, we need to increase the cluster serial too
2356
      self._config_data.cluster.serial_no += 1
2357
      self._config_data.cluster.mtime = now
2358

    
2359
    if isinstance(target, objects.Instance):
2360
      self._UnlockedReleaseDRBDMinors(target.name)
2361

    
2362
    self._WriteConfig(feedback_fn=feedback_fn)
2363

    
2364
  @locking.ssynchronized(_config_lock)
2365
  def DropECReservations(self, ec_id):
2366
    """Drop per-execution-context reservations
2367

2368
    """
2369
    for rm in self._all_rms:
2370
      rm.DropECReservations(ec_id)
2371

    
2372
  @locking.ssynchronized(_config_lock, shared=1)
2373
  def GetAllNetworksInfo(self):
2374
    """Get the configuration of all networks
2375

2376
    """
2377
    return dict(self._config_data.networks)
2378

    
2379
  def _UnlockedGetNetworkList(self):
2380
    """Get the list of networks.
2381

2382
    This function is for internal use, when the config lock is already held.
2383

2384
    """
2385
    return self._config_data.networks.keys()
2386

    
2387
  @locking.ssynchronized(_config_lock, shared=1)
2388
  def GetNetworkList(self):
2389
    """Get the list of networks.
2390

2391
    @return: array of networks, ex. ["main", "vlan100", "200]
2392

2393
    """
2394
    return self._UnlockedGetNetworkList()
2395

    
2396
  def _UnlockedGetNetwork(self, uuid):
2397
    """Returns information about a network.
2398

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

2401
    """
2402
    if uuid not in self._config_data.networks:
2403
      return None
2404

    
2405
    return self._config_data.networks[uuid]
2406

    
2407
  @locking.ssynchronized(_config_lock, shared=1)
2408
  def GetNetwork(self, uuid):
2409
    """Returns information about a network.
2410

2411
    It takes the information from the configuration file.
2412

2413
    @param uuid: UUID of the network
2414

2415
    @rtype: L{objects.Network}
2416
    @return: the network object
2417

2418
    """
2419
    return self._UnlockedGetNetwork(uuid)
2420

    
2421
  @locking.ssynchronized(_config_lock)
2422
  def AddNetwork(self, net, ec_id):
2423
    """Add a network to the configuration.
2424

2425
    @type net: L{objects.Network}
2426
    @param net: the Network object to add
2427
    @type ec_id: string
2428
    @param ec_id: unique id for the job to use when creating a missing UUID
2429

2430
    """
2431
    self._UnlockedAddNetwork(net, ec_id)
2432
    self._WriteConfig()
2433

    
2434
  def _UnlockedAddNetwork(self, net, ec_id):
2435
    """Add a network to the configuration.
2436

2437
    """
2438
    logging.info("Adding network %s to configuration", net.name)
2439

    
2440
    self._EnsureUUID(net, ec_id)
2441

    
2442
    existing_uuid = self._UnlockedLookupNetwork(net.name)
2443
    if existing_uuid:
2444
      raise errors.OpPrereqError("Desired network name '%s' already"
2445
                                 " exists as a network (UUID: %s)" %
2446
                                 (net.name, existing_uuid),
2447
                                 errors.ECODE_EXISTS)
2448
    net.serial_no = 1
2449
    self._config_data.networks[net.uuid] = net
2450
    self._config_data.cluster.serial_no += 1
2451

    
2452
  def _UnlockedLookupNetwork(self, target):
2453
    """Lookup a network's UUID.
2454

2455
    @type target: string
2456
    @param target: network name or UUID
2457
    @rtype: string
2458
    @return: network UUID
2459
    @raises errors.OpPrereqError: when the target network cannot be found
2460

2461
    """
2462
    if target in self._config_data.networks:
2463
      return target
2464
    for net in self._config_data.networks.values():
2465
      if net.name == target:
2466
        return net.uuid
2467
    return None
2468

    
2469
  @locking.ssynchronized(_config_lock, shared=1)
2470
  def LookupNetwork(self, target):
2471
    """Lookup a network's UUID.
2472

2473
    This function is just a wrapper over L{_UnlockedLookupNetwork}.
2474

2475
    @type target: string
2476
    @param target: network name or UUID
2477
    @rtype: string
2478
    @return: network UUID
2479

2480
    """
2481
    return self._UnlockedLookupNetwork(target)
2482

    
2483
  @locking.ssynchronized(_config_lock)
2484
  def RemoveNetwork(self, network_uuid):
2485
    """Remove a network from the configuration.
2486

2487
    @type network_uuid: string
2488
    @param network_uuid: the UUID of the network to remove
2489

2490
    """
2491
    logging.info("Removing network %s from configuration", network_uuid)
2492

    
2493
    if network_uuid not in self._config_data.networks:
2494
      raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
2495

    
2496
    del self._config_data.networks[network_uuid]
2497
    self._config_data.cluster.serial_no += 1
2498
    self._WriteConfig()
2499

    
2500
  def _UnlockedGetGroupNetParams(self, net, node):
2501
    """Get the netparams (mode, link) of a network.
2502

2503
    Get a network's netparams for a given node.
2504

2505
    @type net: string
2506
    @param net: network name
2507
    @type node: string
2508
    @param node: node name
2509
    @rtype: dict or None
2510
    @return: netparams
2511

2512
    """
2513
    net_uuid = self._UnlockedLookupNetwork(net)
2514
    if net_uuid is None:
2515
      return None
2516

    
2517
    node_info = self._UnlockedGetNodeInfo(node)
2518
    nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2519
    netparams = nodegroup_info.networks.get(net_uuid, None)
2520

    
2521
    return netparams
2522

    
2523
  @locking.ssynchronized(_config_lock, shared=1)
2524
  def GetGroupNetParams(self, net, node):
2525
    """Locking wrapper of _UnlockedGetGroupNetParams()
2526

2527
    """
2528
    return self._UnlockedGetGroupNetParams(net, node)
2529

    
2530

    
2531
  @locking.ssynchronized(_config_lock, shared=1)
2532
  def CheckIPInNodeGroup(self, ip, node):
2533
    """Check for conflictig IP.
2534

2535
    @type ip: string
2536
    @param ip: ip address
2537
    @type node: string
2538
    @param node: node name
2539
    @rtype: (string, dict) or (None, None)
2540
    @return: (network name, netparams)
2541

2542
    """
2543
    if ip is None:
2544
      return (None, None)
2545
    node_info = self._UnlockedGetNodeInfo(node)
2546
    nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
2547
    for net_uuid in nodegroup_info.networks.keys():
2548
      net_info = self._UnlockedGetNetwork(net_uuid)
2549
      pool = network.AddressPool(net_info)
2550
      if pool._Contains(ip):
2551
        return (net_info.name, nodegroup_info.networks[net_uuid])
2552

    
2553
    return (None, None)