Statistics
| Branch: | Tag: | Revision:

root / lib / config.py @ fee9556c

History | View | Annotate | Download (18.2 kB)

1
#!/usr/bin/python
2
#
3

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

    
21

    
22
"""Configuration management for Ganeti
23

24
This module provides the interface to the ganeti cluster configuration.
25

26

27
The configuration data is stored on every node but is updated on the
28
master only. After each update, the master distributes the data to the
29
other nodes.
30

31
Currently the data storage format is pickle as yaml was initially not
32
available, then we used it but it was a memory-eating slow beast, so
33
we reverted to pickle using custom Unpicklers.
34

35
"""
36

    
37
import os
38
import socket
39
import tempfile
40
import random
41

    
42
from ganeti import errors
43
from ganeti import logger
44
from ganeti import utils
45
from ganeti import constants
46
from ganeti import rpc
47
from ganeti import objects
48

    
49
def _my_uuidgen():
50
  """Poor-man's uuidgen using the uuidgen binary.
51

52
  """
53
  result = utils.RunCmd(["uuidgen", "-r"])
54
  if result.failed:
55
    return None
56
  return result.stdout.rstrip('\n')
57

    
58

    
59
try:
60
  import uuid
61
  _uuidgen = uuid.uuid4
62
except ImportError:
63
  _uuidgen = _my_uuidgen
64

    
65

    
66
class ConfigWriter:
67
  """The interface to the cluster configuration.
68

69
  """
70
  def __init__(self, cfg_file=None, offline=False):
71
    self._config_data = None
72
    self._config_time = None
73
    self._config_size = None
74
    self._config_inode = None
75
    self._offline = offline
76
    if cfg_file is None:
77
      self._cfg_file = constants.CLUSTER_CONF_FILE
78
    else:
79
      self._cfg_file = cfg_file
80
    self._temporary_ids = set()
81

    
82
  # this method needs to be static, so that we can call it on the class
83
  @staticmethod
84
  def IsCluster():
85
    """Check if the cluster is configured.
86

87
    """
88
    return os.path.exists(constants.CLUSTER_CONF_FILE)
89

    
90
  def GenerateMAC(self):
91
    """Generate a MAC for an instance.
92

93
    This should check the current instances for duplicates.
94

95
    """
96
    self._OpenConfig()
97
    self._ReleaseLock()
98
    prefix = self._config_data.cluster.mac_prefix
99
    all_macs = self._AllMACs()
100
    retries = 64
101
    while retries > 0:
102
      byte1 = random.randrange(0, 256)
103
      byte2 = random.randrange(0, 256)
104
      byte3 = random.randrange(0, 256)
105
      mac = "%s:%02x:%02x:%02x" % (prefix, byte1, byte2, byte3)
106
      if mac not in all_macs:
107
        break
108
      retries -= 1
109
    else:
110
      raise errors.ConfigurationError, ("Can't generate unique MAC")
111
    return mac
112

    
113
  def _ComputeAllLVs(self):
114
    """Compute the list of all LVs.
115

116
    """
117
    self._OpenConfig()
118
    self._ReleaseLock()
119
    lvnames = set()
120
    for instance in self._config_data.instances.values():
121
      node_data = instance.MapLVsByNode()
122
      for lv_list in node_data.values():
123
        lvnames.update(lv_list)
124
    return lvnames
125

    
126
  def GenerateUniqueID(self, exceptions=None):
127
    """Generate an unique disk name.
128

129
    This checks the current node, instances and disk names for
130
    duplicates.
131

132
    Args:
133
      - exceptions: a list with some other names which should be checked
134
                    for uniqueness (used for example when you want to get
135
                    more than one id at one time without adding each one in
136
                    turn to the config file
137

138
    Returns: the unique id as a string
139

140
    """
141
    existing = set()
142
    existing.update(self._temporary_ids)
143
    existing.update(self._ComputeAllLVs())
144
    existing.update(self._config_data.instances.keys())
145
    existing.update(self._config_data.nodes.keys())
146
    if exceptions is not None:
147
      existing.update(exceptions)
148
    retries = 64
149
    while retries > 0:
150
      unique_id = _uuidgen()
151
      if unique_id not in existing and unique_id is not None:
152
        break
153
    else:
154
      raise errors.ConfigurationError, ("Not able generate an unique ID"
155
                                        " (last tried ID: %s" % unique_id)
156
    self._temporary_ids.add(unique_id)
157
    return unique_id
158

    
159
  def _AllMACs(self):
160
    """Return all MACs present in the config.
161

162
    """
163
    self._OpenConfig()
164
    self._ReleaseLock()
165

    
166
    result = []
167
    for instance in self._config_data.instances.values():
168
      for nic in instance.nics:
169
        result.append(nic.mac)
170

    
171
    return result
172

    
173
  def VerifyConfig(self):
174
    """Stub verify function.
175
    """
176
    self._OpenConfig()
177
    self._ReleaseLock()
178

    
179
    result = []
180
    seen_macs = []
181
    data = self._config_data
182
    for instance_name in data.instances:
183
      instance = data.instances[instance_name]
184
      if instance.primary_node not in data.nodes:
185
        result.append("Instance '%s' has invalid primary node '%s'" %
186
                      (instance_name, instance.primary_node))
187
      for snode in instance.secondary_nodes:
188
        if snode not in data.nodes:
189
          result.append("Instance '%s' has invalid secondary node '%s'" %
190
                        (instance_name, snode))
191
      for idx, nic in enumerate(instance.nics):
192
        if nic.mac in seen_macs:
193
          result.append("Instance '%s' has NIC %d mac %s duplicate" %
194
                        (instance_name, idx, nic.mac))
195
        else:
196
          seen_macs.append(nic.mac)
197
    return result
198

    
199
  def SetDiskID(self, disk, node_name):
200
    """Convert the unique ID to the ID needed on the target nodes.
201

202
    This is used only for drbd, which needs ip/port configuration.
203

204
    The routine descends down and updates its children also, because
205
    this helps when the only the top device is passed to the remote
206
    node.
207

208
    """
209
    if disk.children:
210
      for child in disk.children:
211
        self.SetDiskID(child, node_name)
212

    
213
    if disk.logical_id is None and disk.physical_id is not None:
214
      return
215
    if disk.dev_type == "drbd":
216
      pnode, snode, port = disk.logical_id
217
      if node_name not in (pnode, snode):
218
        raise errors.ConfigurationError, ("DRBD device not knowing node %s" %
219
                                          node_name)
220
      pnode_info = self.GetNodeInfo(pnode)
221
      snode_info = self.GetNodeInfo(snode)
222
      if pnode_info is None or snode_info is None:
223
        raise errors.ConfigurationError("Can't find primary or secondary node"
224
                                        " for %s" % str(disk))
225
      if pnode == node_name:
226
        disk.physical_id = (pnode_info.secondary_ip, port,
227
                            snode_info.secondary_ip, port)
228
      else: # it must be secondary, we tested above
229
        disk.physical_id = (snode_info.secondary_ip, port,
230
                            pnode_info.secondary_ip, port)
231
    else:
232
      disk.physical_id = disk.logical_id
233
    return
234

    
235
  def AddTcpUdpPort(self, port):
236
    """Adds a new port to the available port pool.
237

238
    """
239
    if not isinstance(port, int):
240
      raise errors.ProgrammerError, ("Invalid type passed for port")
241

    
242
    self._OpenConfig()
243
    self._config_data.cluster.tcpudp_port_pool.add(port)
244
    self._WriteConfig()
245

    
246
  def GetPortList(self):
247
    """Returns a copy of the current port list.
248

249
    """
250
    self._OpenConfig()
251
    self._ReleaseLock()
252
    return self._config_data.cluster.tcpudp_port_pool.copy()
253

    
254
  def AllocatePort(self):
255
    """Allocate a port.
256

257
    The port will be taken from the available port pool or from the
258
    default port range (and in this case we increase
259
    highest_used_port).
260

261
    """
262
    self._OpenConfig()
263

    
264
    # If there are TCP/IP ports configured, we use them first.
265
    if self._config_data.cluster.tcpudp_port_pool:
266
      port = self._config_data.cluster.tcpudp_port_pool.pop()
267
    else:
268
      port = self._config_data.cluster.highest_used_port + 1
269
      if port >= constants.LAST_DRBD_PORT:
270
        raise errors.ConfigurationError, ("The highest used port is greater"
271
                                          " than %s. Aborting." %
272
                                          constants.LAST_DRBD_PORT)
273
      self._config_data.cluster.highest_used_port = port
274

    
275
    self._WriteConfig()
276
    return port
277

    
278
  def GetHostKey(self):
279
    """Return the rsa hostkey from the config.
280

281
    Args: None
282

283
    Returns: rsa hostkey
284
    """
285
    self._OpenConfig()
286
    self._ReleaseLock()
287
    return self._config_data.cluster.rsahostkeypub
288

    
289
  def AddInstance(self, instance):
290
    """Add an instance to the config.
291

292
    This should be used after creating a new instance.
293

294
    Args:
295
      instance: the instance object
296
    """
297
    if not isinstance(instance, objects.Instance):
298
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
299

    
300
    all_lvs = instance.MapLVsByNode()
301
    logger.Info("Instance '%s' DISK_LAYOUT: %s" % (instance.name, all_lvs))
302

    
303
    self._OpenConfig()
304
    self._config_data.instances[instance.name] = instance
305
    self._WriteConfig()
306

    
307
  def MarkInstanceUp(self, instance_name):
308
    """Mark the instance status to up in the config.
309

310
    """
311
    self._OpenConfig()
312

    
313
    if instance_name not in self._config_data.instances:
314
      raise errors.ConfigurationError, ("Unknown instance '%s'" %
315
                                        instance_name)
316
    instance = self._config_data.instances[instance_name]
317
    instance.status = "up"
318
    self._WriteConfig()
319

    
320
  def RemoveInstance(self, instance_name):
321
    """Remove the instance from the configuration.
322

323
    """
324
    self._OpenConfig()
325

    
326
    if instance_name not in self._config_data.instances:
327
      raise errors.ConfigurationError, ("Unknown instance '%s'" %
328
                                        instance_name)
329
    del self._config_data.instances[instance_name]
330
    self._WriteConfig()
331

    
332
  def MarkInstanceDown(self, instance_name):
333
    """Mark the status of an instance to down in the configuration.
334

335
    """
336
    self._OpenConfig()
337

    
338
    if instance_name not in self._config_data.instances:
339
      raise errors.ConfigurationError, ("Unknown instance '%s'" %
340
                                        instance_name)
341
    instance = self._config_data.instances[instance_name]
342
    instance.status = "down"
343
    self._WriteConfig()
344

    
345
  def GetInstanceList(self):
346
    """Get the list of instances.
347

348
    Returns:
349
      array of instances, ex. ['instance2.example.com','instance1.example.com']
350
      these contains all the instances, also the ones in Admin_down state
351

352
    """
353
    self._OpenConfig()
354
    self._ReleaseLock()
355

    
356
    return self._config_data.instances.keys()
357

    
358
  def ExpandInstanceName(self, short_name):
359
    """Attempt to expand an incomplete instance name.
360

361
    """
362
    self._OpenConfig()
363
    self._ReleaseLock()
364

    
365
    return utils.MatchNameComponent(short_name,
366
                                    self._config_data.instances.keys())
367

    
368
  def GetInstanceInfo(self, instance_name):
369
    """Returns informations about an instance.
370

371
    It takes the information from the configuration file. Other informations of
372
    an instance are taken from the live systems.
373

374
    Args:
375
      instance: name of the instance, ex instance1.example.com
376

377
    Returns:
378
      the instance object
379

380
    """
381
    self._OpenConfig()
382
    self._ReleaseLock()
383

    
384
    if instance_name not in self._config_data.instances:
385
      return None
386

    
387
    return self._config_data.instances[instance_name]
388

    
389
  def AddNode(self, node):
390
    """Add a node to the configuration.
391

392
    Args:
393
      node: an object.Node instance
394

395
    """
396
    self._OpenConfig()
397
    self._config_data.nodes[node.name] = node
398
    self._WriteConfig()
399

    
400
  def RemoveNode(self, node_name):
401
    """Remove a node from the configuration.
402

403
    """
404
    self._OpenConfig()
405
    if node_name not in self._config_data.nodes:
406
      raise errors.ConfigurationError, ("Unknown node '%s'" % node_name)
407

    
408
    del self._config_data.nodes[node_name]
409
    self._WriteConfig()
410

    
411
  def ExpandNodeName(self, short_name):
412
    """Attempt to expand an incomplete instance name.
413

414
    """
415
    self._OpenConfig()
416
    self._ReleaseLock()
417

    
418
    return utils.MatchNameComponent(short_name,
419
                                    self._config_data.nodes.keys())
420

    
421
  def GetNodeInfo(self, node_name):
422
    """Get the configuration of a node, as stored in the config.
423

424
    Args: node: nodename (tuple) of the node
425

426
    Returns: the node object
427

428
    """
429
    self._OpenConfig()
430
    self._ReleaseLock()
431

    
432
    if node_name not in self._config_data.nodes:
433
      return None
434

    
435
    return self._config_data.nodes[node_name]
436

    
437
  def GetNodeList(self):
438
    """Return the list of nodes which are in the configuration.
439

440
    """
441
    self._OpenConfig()
442
    self._ReleaseLock()
443
    return self._config_data.nodes.keys()
444

    
445
  def DumpConfig(self):
446
    """Return the entire configuration of the cluster.
447
    """
448
    self._OpenConfig()
449
    self._ReleaseLock()
450
    return self._config_data
451

    
452
  def _BumpSerialNo(self):
453
    """Bump up the serial number of the config.
454

455
    """
456
    self._config_data.cluster.serial_no += 1
457

    
458
  def _OpenConfig(self):
459
    """Read the config data from disk.
460

461
    In case we already have configuration data and the config file has
462
    the same mtime as when we read it, we skip the parsing of the
463
    file, since de-serialisation could be slow.
464

465
    """
466
    try:
467
      st = os.stat(self._cfg_file)
468
    except OSError, err:
469
      raise errors.ConfigurationError, "Can't stat config file: %s" % err
470
    if (self._config_data is not None and
471
        self._config_time is not None and
472
        self._config_time == st.st_mtime and
473
        self._config_size == st.st_size and
474
        self._config_inode == st.st_ino):
475
      # data is current, so skip loading of config file
476
      return
477
    f = open(self._cfg_file, 'r')
478
    try:
479
      try:
480
        data = objects.ConfigObject.Load(f)
481
      except Exception, err:
482
        raise errors.ConfigurationError, err
483
    finally:
484
      f.close()
485
    if (not hasattr(data, 'cluster') or
486
        not hasattr(data.cluster, 'config_version')):
487
      raise errors.ConfigurationError, ("Incomplete configuration"
488
                                        " (missing cluster.config_version)")
489
    if data.cluster.config_version != constants.CONFIG_VERSION:
490
      raise errors.ConfigurationError, ("Cluster configuration version"
491
                                        " mismatch, got %s instead of %s" %
492
                                        (data.cluster.config_version,
493
                                         constants.CONFIG_VERSION))
494
    self._config_data = data
495
    self._config_time = st.st_mtime
496
    self._config_size = st.st_size
497
    self._config_inode = st.st_ino
498

    
499
  def _ReleaseLock(self):
500
    """xxxx
501
    """
502

    
503
  def _DistributeConfig(self):
504
    """Distribute the configuration to the other nodes.
505

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

509
    """
510
    if self._offline:
511
      return True
512
    bad = False
513
    nodelist = self.GetNodeList()
514
    myhostname = socket.gethostname()
515

    
516
    tgt_list = []
517
    for node in nodelist:
518
      nodeinfo = self.GetNodeInfo(node)
519
      if nodeinfo.name == myhostname:
520
        continue
521
      tgt_list.append(node)
522

    
523
    result = rpc.call_upload_file(tgt_list, self._cfg_file)
524
    for node in tgt_list:
525
      if not result[node]:
526
        logger.Error("copy of file %s to node %s failed" %
527
                     (self._cfg_file, node))
528
        bad = True
529
    return not bad
530

    
531
  def _WriteConfig(self, destination=None):
532
    """Write the configuration data to persistent storage.
533

534
    """
535
    if destination is None:
536
      destination = self._cfg_file
537
    self._BumpSerialNo()
538
    dir_name, file_name = os.path.split(destination)
539
    fd, name = tempfile.mkstemp('.newconfig', file_name, dir_name)
540
    f = os.fdopen(fd, 'w')
541
    try:
542
      self._config_data.Dump(f)
543
      os.fsync(f.fileno())
544
    finally:
545
      f.close()
546
    # we don't need to do os.close(fd) as f.close() did it
547
    os.rename(name, destination)
548
    # re-set our cache as not to re-read the config file
549
    try:
550
      st = os.stat(destination)
551
    except OSError, err:
552
      raise errors.ConfigurationError, "Can't stat config file: %s" % err
553
    self._config_time = st.st_mtime
554
    self._config_size = st.st_size
555
    self._config_inode = st.st_ino
556
    # and redistribute the config file
557
    self._DistributeConfig()
558

    
559
  def InitConfig(self, node, primary_ip, secondary_ip,
560
                 hostkeypub, mac_prefix, vg_name, def_bridge):
561
    """Create the initial cluster configuration.
562

563
    It will contain the current node, which will also be the master
564
    node, and no instances or operating systmes.
565

566
    Args:
567
      node: the nodename of the initial node
568
      primary_ip: the IP address of the current host
569
      secondary_ip: the secondary IP of the current host or None
570
      hostkeypub: the public hostkey of this host
571

572
    """
573
    hu_port = constants.FIRST_DRBD_PORT - 1
574
    globalconfig = objects.Cluster(config_version=constants.CONFIG_VERSION,
575
                                   serial_no=1,
576
                                   rsahostkeypub=hostkeypub,
577
                                   highest_used_port=hu_port,
578
                                   mac_prefix=mac_prefix,
579
                                   volume_group_name=vg_name,
580
                                   default_bridge=def_bridge,
581
                                   tcpudp_port_pool=set())
582
    if secondary_ip is None:
583
      secondary_ip = primary_ip
584
    nodeconfig = objects.Node(name=node, primary_ip=primary_ip,
585
                              secondary_ip=secondary_ip)
586

    
587
    self._config_data = objects.ConfigData(nodes={node: nodeconfig},
588
                                           instances={},
589
                                           cluster=globalconfig)
590
    self._WriteConfig()
591

    
592
  def GetVGName(self):
593
    """Return the volume group name.
594

595
    """
596
    self._OpenConfig()
597
    self._ReleaseLock()
598
    return self._config_data.cluster.volume_group_name
599

    
600
  def GetDefBridge(self):
601
    """Return the default bridge.
602

603
    """
604
    self._OpenConfig()
605
    self._ReleaseLock()
606
    return self._config_data.cluster.default_bridge
607

    
608
  def GetMACPrefix(self):
609
    """Return the mac prefix.
610

611
    """
612
    self._OpenConfig()
613
    self._ReleaseLock()
614
    return self._config_data.cluster.mac_prefix
615

    
616
  def GetClusterInfo(self):
617
    """Returns informations about the cluster
618

619
    Returns:
620
      the cluster object
621

622
    """
623
    self._OpenConfig()
624
    self._ReleaseLock()
625

    
626
    return self._config_data.cluster