Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ d6646186

History | View | Annotate | Download (5.4 kB)

1
#
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
  SS_MASTER_NODE = "master_node"
61
  SS_MASTER_IP = "master_ip"
62
  SS_MASTER_NETDEV = "master_netdev"
63
  SS_CLUSTER_NAME = "cluster_name"
64
  _VALID_KEYS = (SS_HYPERVISOR, SS_NODED_PASS, SS_MASTER_NODE, SS_MASTER_IP,
65
                 SS_MASTER_NETDEV, SS_CLUSTER_NAME)
66
  _MAX_SIZE = 4096
67

    
68
  def __init__(self, cfg_location=None):
69
    if cfg_location is None:
70
      self._cfg_dir = constants.DATA_DIR
71
    else:
72
      self._cfg_dir = cfg_location
73
    self._cache = {}
74

    
75
  def KeyToFilename(self, key):
76
    """Convert a given key into filename.
77

78
    """
79
    if key not in self._VALID_KEYS:
80
      raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
81
                                   % str(key))
82

    
83
    filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
84
    return filename
85

    
86
  def _ReadFile(self, key):
87
    """Generic routine to read keys.
88

89
    This will read the file which holds the value requested. Errors
90
    will be changed into ConfigurationErrors.
91

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

    
109
  def GetNodeDaemonPort(self):
110
    """Get the node daemon port for this cluster.
111

112
    Note that this routine does not read a ganeti-specific file, but
113
    instead uses socket.getservbyname to allow pre-customization of
114
    this parameter outside of ganeti.
115

116
    """
117
    try:
118
      port = socket.getservbyname("ganeti-noded", "tcp")
119
    except socket.error:
120
      port = constants.DEFAULT_NODED_PORT
121

    
122
    return port
123

    
124
  def GetHypervisorType(self):
125
    """Get the hypervisor type for this cluster.
126

127
    """
128
    return self._ReadFile(self.SS_HYPERVISOR)
129

    
130
  def GetNodeDaemonPassword(self):
131
    """Get the node password for this cluster.
132

133
    """
134
    return self._ReadFile(self.SS_NODED_PASS)
135

    
136
  def GetMasterNode(self):
137
    """Get the hostname of the master node for this cluster.
138

139
    """
140
    return self._ReadFile(self.SS_MASTER_NODE)
141

    
142
  def GetMasterIP(self):
143
    """Get the IP of the master node for this cluster.
144

145
    """
146
    return self._ReadFile(self.SS_MASTER_IP)
147

    
148
  def GetMasterNetdev(self):
149
    """Get the netdev to which we'll add the master ip.
150

151
    """
152
    return self._ReadFile(self.SS_MASTER_NETDEV)
153

    
154
  def GetClusterName(self):
155
    """Get the cluster name.
156

157
    """
158
    return self._ReadFile(self.SS_CLUSTER_NAME)
159

    
160
  def SetKey(self, key, value):
161
    """Set the value of a key.
162

163
    This should be used only when adding a node to a cluster.
164

165
    """
166
    file_name = self.KeyToFilename(key)
167
    dir_name, small_name = os.path.split(file_name)
168
    fd, new_name = tempfile.mkstemp('.new', small_name, dir_name)
169
    # here we need to make sure we remove the temp file, if any error
170
    # leaves it in place
171
    try:
172
      os.chown(new_name, 0, 0)
173
      os.chmod(new_name, 0400)
174
      os.write(fd, "%s\n" % str(value))
175
      os.fsync(fd)
176
      os.rename(new_name, file_name)
177
      self._cache[key] = value
178
    finally:
179
      os.close(fd)
180
      try:
181
        os.unlink(new_name)
182
      except OSError, err:
183
        if err.errno != errno.ENOENT:
184
          raise
185

    
186
  def GetFileList(self):
187
    """Return the lis of all config files.
188

189
    This is used for computing node replication data.
190

191
    """
192
    return [self.KeyToFilename(key) for key in self._VALID_KEYS]