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.
31 from ganeti import errors
32 from ganeti import constants
33 from ganeti import utils
37 """Interface to static cluster data.
39 This is different that the config.ConfigWriter class in that it
40 holds data that is (mostly) constant after the cluster
41 initialization. Its purpose is to allow limited customization of
42 things which would otherwise normally live in constants.py. Note
43 that this data cannot live in ConfigWriter as that is available only
44 on the master node, and our data must be readable by both the master
47 Other particularities of the datastore:
48 - keys are restricted to predefined values
49 - values are small (<4k)
50 - since the data is practically static, read keys are cached in memory
51 - some keys are handled specially (read from the system, so
55 _SS_FILEPREFIX = "ssconf_"
56 SS_HYPERVISOR = "hypervisor"
57 SS_NODED_PASS = "node_pass"
58 SS_MASTER_NODE = "master_node"
59 SS_MASTER_IP = "master_ip"
60 SS_MASTER_NETDEV = "master_netdev"
61 SS_CLUSTER_NAME = "cluster_name"
62 SS_FILE_STORAGE_DIR = "file_storage_dir"
63 _VALID_KEYS = (SS_HYPERVISOR, SS_NODED_PASS, SS_MASTER_NODE, SS_MASTER_IP,
64 SS_MASTER_NETDEV, SS_CLUSTER_NAME, SS_FILE_STORAGE_DIR)
67 def __init__(self, cfg_location=None):
68 if cfg_location is None:
69 self._cfg_dir = constants.DATA_DIR
71 self._cfg_dir = cfg_location
74 def KeyToFilename(self, key):
75 """Convert a given key into filename.
78 if key not in self._VALID_KEYS:
79 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
82 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
85 def _ReadFile(self, key):
86 """Generic routine to read keys.
88 This will read the file which holds the value requested. Errors
89 will be changed into ConfigurationErrors.
92 if key in self._cache:
93 return self._cache[key]
94 filename = self.KeyToFilename(key)
96 fh = file(filename, 'r')
98 data = fh.readline(self._MAX_SIZE)
99 data = data.rstrip('\n')
102 except EnvironmentError, err:
103 raise errors.ConfigurationError("Can't read from the ssconf file:"
105 self._cache[key] = data
108 def GetNodeDaemonPort(self):
109 """Get the node daemon port for this cluster.
111 Note that this routine does not read a ganeti-specific file, but
112 instead uses socket.getservbyname to allow pre-customization of
113 this parameter outside of ganeti.
117 port = socket.getservbyname("ganeti-noded", "tcp")
119 port = constants.DEFAULT_NODED_PORT
123 def GetHypervisorType(self):
124 """Get the hypervisor type for this cluster.
127 return self._ReadFile(self.SS_HYPERVISOR)
129 def GetNodeDaemonPassword(self):
130 """Get the node password for this cluster.
133 return self._ReadFile(self.SS_NODED_PASS)
135 def GetMasterNode(self):
136 """Get the hostname of the master node for this cluster.
139 return self._ReadFile(self.SS_MASTER_NODE)
141 def GetMasterIP(self):
142 """Get the IP of the master node for this cluster.
145 return self._ReadFile(self.SS_MASTER_IP)
147 def GetMasterNetdev(self):
148 """Get the netdev to which we'll add the master ip.
151 return self._ReadFile(self.SS_MASTER_NETDEV)
153 def GetClusterName(self):
154 """Get the cluster name.
157 return self._ReadFile(self.SS_CLUSTER_NAME)
159 def GetFileStorageDir(self):
160 """Get the file storage dir.
163 return self._ReadFile(self.SS_FILE_STORAGE_DIR)
165 def SetKey(self, key, value):
166 """Set the value of a key.
168 This should be used only when adding a node to a cluster.
171 file_name = self.KeyToFilename(key)
172 utils.WriteFile(file_name, data="%s\n" % str(value),
173 uid=0, gid=0, mode=0400)
174 self._cache[key] = value
176 def GetFileList(self):
177 """Return the lis of all config files.
179 This is used for computing node replication data.
182 return [self.KeyToFilename(key) for key in self._VALID_KEYS]