Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ a8083063

History | View | Annotate | Download (4.7 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
"""Global Configuration data for Ganeti.
23

24
This module provides the interface to a special case of cluster
25
configuration data, which is mostly static and available to all nodes.
26

27
"""
28

    
29
import os
30
import tempfile
31
import errno
32
import socket
33

    
34
from ganeti import errors
35
from ganeti import constants
36

    
37

    
38
class SimpleStore:
39
  """Interface to static cluster data.
40

41
  This is different that the config.ConfigWriter class in that it
42
  holds data that is (mostly) constant after the cluster
43
  initialization. Its purpose is to allow limited customization of
44
  things which would otherwise normally live in constants.py. Note
45
  that this data cannot live in ConfigWriter as that is available only
46
  on the master node, and our data must be readable by both the master
47
  and the nodes.
48

49
  Other particularities of the datastore:
50
    - keys are restricted to predefined values
51
    - values are small (<4k)
52
    - since the data is practically static, read keys are cached in memory
53
    - some keys are handled specially (read from the system, so
54
      we can't update them)
55

56
  """
57
  _SS_FILEPREFIX = "ssconf_"
58
  SS_HYPERVISOR = "hypervisor"
59
  SS_NODED_PASS = "node_pass"
60
  _VALID_KEYS = (SS_HYPERVISOR, SS_NODED_PASS,)
61
  _MAX_SIZE = 4096
62

    
63
  def __init__(self, cfg_location=None):
64
    if cfg_location is None:
65
      self._cfg_dir = constants.DATA_DIR
66
    else:
67
      self._cfg_dir = cfg_location
68
    self._cache = {}
69

    
70
  def KeyToFilename(self, key):
71
    """Convert a given key into filename.
72

73
    """
74
    if key not in self._VALID_KEYS:
75
      raise errors.ProgrammerError, ("Invalid key requested from SSConf: '%s'"
76
                                     % str(key))
77

    
78
    filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
79
    return filename
80

    
81
  def _ReadFile(self, key):
82
    """Generic routine to read keys.
83

84
    This will read the file which holds the value requested. Errors
85
    will be changed into ConfigurationErrors.
86

87
    """
88
    if key in self._cache:
89
      return self._cache[key]
90
    filename = self.KeyToFilename(key)
91
    try:
92
      fh = file(filename, 'r')
93
      try:
94
        data = fh.readline(self._MAX_SIZE)
95
        data = data.rstrip('\n')
96
      finally:
97
        fh.close()
98
    except EnvironmentError, err:
99
      raise errors.ConfigurationError, ("Can't read from the ssconf file:"
100
                                        " '%s'" % str(err))
101
    self._cache[key] = data
102
    return data
103

    
104
  def GetNodeDaemonPort(self):
105
    """Get the node daemon port for this cluster.
106

107
    Note that this routine does not read a ganeti-specific file, but
108
    instead uses socket.getservbyname to allow pre-customization of
109
    this parameter outside of ganeti.
110

111
    """
112
    try:
113
      port = socket.getservbyname("ganeti-noded", "tcp")
114
    except socket.error:
115
      port = constants.DEFAULT_NODED_PORT
116

    
117
    return port
118

    
119
  def GetHypervisorType(self):
120
    """Get the hypervisor type for this cluster.
121

122
    """
123
    return self._ReadFile(self.SS_HYPERVISOR)
124

    
125
  def GetNodeDaemonPassword(self):
126
    """Get the node password for this cluster.
127

128
    """
129
    return self._ReadFile(self.SS_NODED_PASS)
130

    
131
  def SetKey(self, key, value):
132
    """Set the value of a key.
133

134
    This should be used only when adding a node to a cluster.
135

136
    """
137
    file_name = self.KeyToFilename(key)
138
    dir_name, small_name = os.path.split(file_name)
139
    fd, new_name = tempfile.mkstemp('.new', small_name, dir_name)
140
    # here we need to make sure we remove the temp file, if any error
141
    # leaves it in place
142
    try:
143
      os.chown(new_name, 0, 0)
144
      os.chmod(new_name, 0400)
145
      os.write(fd, "%s\n" % str(value))
146
      os.fsync(fd)
147
      os.rename(new_name, file_name)
148
      self._cache[key] = value
149
    finally:
150
      os.close(fd)
151
      try:
152
        os.unlink(new_name)
153
      except OSError, err:
154
        if err.errno != errno.ENOENT:
155
          raise
156

    
157
  def GetFileList(self):
158
    """Return the lis of all config files.
159

160
    This is used for computing node replication data.
161

162
    """
163
    return [self.KeyToFilename(key) for key in self._VALID_KEYS]