4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
22 """Global Configuration data for Ganeti.
24 This module provides the interface to a special case of cluster
25 configuration data, which is mostly static and available to all nodes.
34 from ganeti import errors
35 from ganeti import constants
39 """Interface to static cluster data.
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
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
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)
68 def __init__(self, cfg_location=None):
69 if cfg_location is None:
70 self._cfg_dir = constants.DATA_DIR
72 self._cfg_dir = cfg_location
75 def KeyToFilename(self, key):
76 """Convert a given key into filename.
79 if key not in self._VALID_KEYS:
80 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
83 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
86 def _ReadFile(self, key):
87 """Generic routine to read keys.
89 This will read the file which holds the value requested. Errors
90 will be changed into ConfigurationErrors.
93 if key in self._cache:
94 return self._cache[key]
95 filename = self.KeyToFilename(key)
97 fh = file(filename, 'r')
99 data = fh.readline(self._MAX_SIZE)
100 data = data.rstrip('\n')
103 except EnvironmentError, err:
104 raise errors.ConfigurationError("Can't read from the ssconf file:"
106 self._cache[key] = data
109 def GetNodeDaemonPort(self):
110 """Get the node daemon port for this cluster.
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.
118 port = socket.getservbyname("ganeti-noded", "tcp")
120 port = constants.DEFAULT_NODED_PORT
124 def GetHypervisorType(self):
125 """Get the hypervisor type for this cluster.
128 return self._ReadFile(self.SS_HYPERVISOR)
130 def GetNodeDaemonPassword(self):
131 """Get the node password for this cluster.
134 return self._ReadFile(self.SS_NODED_PASS)
136 def GetMasterNode(self):
137 """Get the hostname of the master node for this cluster.
140 return self._ReadFile(self.SS_MASTER_NODE)
142 def GetMasterIP(self):
143 """Get the IP of the master node for this cluster.
146 return self._ReadFile(self.SS_MASTER_IP)
148 def GetMasterNetdev(self):
149 """Get the netdev to which we'll add the master ip.
152 return self._ReadFile(self.SS_MASTER_NETDEV)
154 def GetClusterName(self):
155 """Get the cluster name.
158 return self._ReadFile(self.SS_CLUSTER_NAME)
160 def SetKey(self, key, value):
161 """Set the value of a key.
163 This should be used only when adding a node to a cluster.
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
172 os.chown(new_name, 0, 0)
173 os.chmod(new_name, 0400)
174 os.write(fd, "%s\n" % str(value))
176 os.rename(new_name, file_name)
177 self._cache[key] = value
183 if err.errno != errno.ENOENT:
186 def GetFileList(self):
187 """Return the lis of all config files.
189 This is used for computing node replication data.
192 return [self.KeyToFilename(key) for key in self._VALID_KEYS]