4 # Copyright (C) 2006, 2007, 2008 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.
33 from ganeti import errors
34 from ganeti import constants
35 from ganeti import utils
36 from ganeti import serializer
39 SSCONF_LOCK_TIMEOUT = 10
41 RE_VALID_SSCONF_NAME = re.compile(r'^[-_a-z0-9]+$')
44 class SimpleConfigReader(object):
45 """Simple class to read configuration file.
48 def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
49 """Initializes this class.
51 @type file_name: string
52 @param file_name: Configuration file path
55 self._file_name = file_name
56 self._last_inode = None
57 self._last_mtime = None
58 self._last_size = None
59 # we need a forced reload at class init time, to initialize _last_*
60 self._Load(force=True)
62 def _Load(self, force=False):
63 """Loads (or reloads) the config file.
66 @param force: whether to force the reload without checking the mtime
68 @return: boolean value that says whether we reloaded the configuration or
69 not (because we decided it was already up-to-date)
73 cfg_stat = os.stat(self._file_name)
74 except EnvironmentError, err:
75 raise errors.ConfigurationError("Cannot stat config file %s: %s" %
76 (self._file_name, err))
77 inode = cfg_stat.st_ino
78 mtime = cfg_stat.st_mtime
79 size = cfg_stat.st_size
82 if force or inode != self._last_inode or \
83 mtime > self._last_mtime or \
84 size != self._last_size:
85 self._last_inode = inode
86 self._last_mtime = mtime
87 self._last_size = size
94 self._config_data = serializer.Load(utils.ReadFile(self._file_name))
95 except EnvironmentError, err:
96 raise errors.ConfigurationError("Cannot read config file %s: %s" %
97 (self._file_name, err))
98 except ValueError, err:
99 raise errors.ConfigurationError("Cannot load config file %s: %s" %
100 (self._file_name, err))
102 self._ip_to_instance = {}
103 for iname in self._config_data['instances']:
104 instance = self._config_data['instances'][iname]
105 for nic in instance['nics']:
106 if 'ip' in nic and nic['ip']:
107 self._ip_to_instance[nic['ip']] = iname
111 # Clients can request a reload of the config file, so we export our internal
112 # _Load function as Reload.
115 def GetClusterName(self):
116 return self._config_data["cluster"]["cluster_name"]
118 def GetHostKey(self):
119 return self._config_data["cluster"]["rsahostkeypub"]
121 def GetMasterNode(self):
122 return self._config_data["cluster"]["master_node"]
124 def GetMasterIP(self):
125 return self._config_data["cluster"]["master_ip"]
127 def GetMasterNetdev(self):
128 return self._config_data["cluster"]["master_netdev"]
130 def GetFileStorageDir(self):
131 return self._config_data["cluster"]["file_storage_dir"]
133 def GetNodeList(self):
134 return self._config_data["nodes"].keys()
136 def GetConfigSerialNo(self):
137 return self._config_data["serial_no"]
139 def GetClusterSerialNo(self):
140 return self._config_data["cluster"]["serial_no"]
142 def GetNodeStatusFlags(self, node):
143 """Get a node's status flags
146 @param node: node name
147 @rtype: (bool, bool, bool)
148 @return: (master_candidate, drained, offline) (or None if no such node)
151 if node not in self._config_data["nodes"]:
154 master_candidate = self._config_data["nodes"][node]["master_candidate"]
155 drained = self._config_data["nodes"][node]["drained"]
156 offline = self._config_data["nodes"][node]["offline"]
157 return master_candidate, drained, offline
159 def GetInstanceByIp(self, ip):
160 if ip not in self._ip_to_instance:
162 return self._ip_to_instance[ip]
164 def GetNodePrimaryIp(self, node):
165 """Get a node's primary ip
168 @param node: node name
169 @rtype: string, or None
170 @return: node's primary ip, or None if no such node
173 if node not in self._config_data["nodes"]:
175 return self._config_data["nodes"][node]["primary_ip"]
177 def GetInstancePrimaryNode(self, instance):
178 """Get an instance's primary node
180 @type instance: string
181 @param instance: instance name
182 @rtype: string, or None
183 @return: primary node, or None if no such instance
186 if instance not in self._config_data["instances"]:
188 return self._config_data["instances"][instance]["primary_node"]
191 class SimpleStore(object):
192 """Interface to static cluster data.
194 This is different that the config.ConfigWriter and
195 SimpleConfigReader classes in that it holds data that will always be
196 present, even on nodes which don't have all the cluster data.
198 Other particularities of the datastore:
199 - keys are restricted to predefined values
202 _SS_FILEPREFIX = "ssconf_"
204 constants.SS_CLUSTER_NAME,
205 constants.SS_CLUSTER_TAGS,
206 constants.SS_FILE_STORAGE_DIR,
207 constants.SS_MASTER_CANDIDATES,
208 constants.SS_MASTER_CANDIDATES_IPS,
209 constants.SS_MASTER_IP,
210 constants.SS_MASTER_NETDEV,
211 constants.SS_MASTER_NODE,
212 constants.SS_NODE_LIST,
213 constants.SS_NODE_PRIMARY_IPS,
214 constants.SS_NODE_SECONDARY_IPS,
215 constants.SS_OFFLINE_NODES,
216 constants.SS_ONLINE_NODES,
217 constants.SS_INSTANCE_LIST,
218 constants.SS_RELEASE_VERSION,
222 def __init__(self, cfg_location=None):
223 if cfg_location is None:
224 self._cfg_dir = constants.DATA_DIR
226 self._cfg_dir = cfg_location
228 def KeyToFilename(self, key):
229 """Convert a given key into filename.
232 if key not in self._VALID_KEYS:
233 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
236 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
239 def _ReadFile(self, key):
240 """Generic routine to read keys.
242 This will read the file which holds the value requested. Errors
243 will be changed into ConfigurationErrors.
246 filename = self.KeyToFilename(key)
248 data = utils.ReadFile(filename, size=self._MAX_SIZE)
249 except EnvironmentError, err:
250 raise errors.ConfigurationError("Can't read from the ssconf file:"
252 data = data.rstrip('\n')
255 def WriteFiles(self, values):
256 """Writes ssconf files used by external scripts.
259 @param values: Dictionary of (name, value)
262 ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE)
264 # Get lock while writing files
265 ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
267 for name, value in values.iteritems():
268 if value and not value.endswith("\n"):
270 utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444)
274 def GetFileList(self):
275 """Return the list of all config files.
277 This is used for computing node replication data.
280 return [self.KeyToFilename(key) for key in self._VALID_KEYS]
282 def GetClusterName(self):
283 """Get the cluster name.
286 return self._ReadFile(constants.SS_CLUSTER_NAME)
288 def GetFileStorageDir(self):
289 """Get the file storage dir.
292 return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
294 def GetMasterCandidates(self):
295 """Return the list of master candidates.
298 data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
299 nl = data.splitlines(False)
302 def GetMasterCandidatesIPList(self):
303 """Return the list of master candidates' primary IP.
306 data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
307 nl = data.splitlines(False)
310 def GetMasterIP(self):
311 """Get the IP of the master node for this cluster.
314 return self._ReadFile(constants.SS_MASTER_IP)
316 def GetMasterNetdev(self):
317 """Get the netdev to which we'll add the master ip.
320 return self._ReadFile(constants.SS_MASTER_NETDEV)
322 def GetMasterNode(self):
323 """Get the hostname of the master node for this cluster.
326 return self._ReadFile(constants.SS_MASTER_NODE)
328 def GetNodeList(self):
329 """Return the list of cluster nodes.
332 data = self._ReadFile(constants.SS_NODE_LIST)
333 nl = data.splitlines(False)
336 def GetNodePrimaryIPList(self):
337 """Return the list of cluster nodes' primary IP.
340 data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
341 nl = data.splitlines(False)
344 def GetNodeSecondaryIPList(self):
345 """Return the list of cluster nodes' secondary IP.
348 data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
349 nl = data.splitlines(False)
352 def GetClusterTags(self):
353 """Return the cluster tags.
356 data = self._ReadFile(constants.SS_CLUSTER_TAGS)
357 nl = data.splitlines(False)
361 def GetMasterAndMyself(ss=None):
362 """Get the master node and my own hostname.
364 This can be either used for a 'soft' check (compared to CheckMaster,
365 which exits) or just for computing both at the same time.
367 The function does not handle any errors, these should be handled in
368 the caller (errors.ConfigurationError, errors.ResolverError).
370 @param ss: either a sstore.SimpleConfigReader or a
371 sstore.SimpleStore instance
373 @return: a tuple (master node name, my own name)
378 return ss.GetMasterNode(), utils.HostInfo().name
381 def CheckMaster(debug, ss=None):
382 """Checks the node setup.
384 If this is the master, the function will return. Otherwise it will
385 exit with an exit code based on the node status.
389 master_name, myself = GetMasterAndMyself(ss)
390 except errors.ConfigurationError, err:
391 print "Cluster configuration incomplete: '%s'" % str(err)
392 sys.exit(constants.EXIT_NODESETUP_ERROR)
393 except errors.ResolverError, err:
394 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
395 sys.exit(constants.EXIT_NODESETUP_ERROR)
397 if myself != master_name:
399 sys.stderr.write("Not master, exiting.\n")
400 sys.exit(constants.EXIT_NOTMASTER)
403 def CheckMasterCandidate(debug, ss=None):
404 """Checks the node setup.
406 If this is a master candidate, the function will return. Otherwise it will
407 exit with an exit code based on the node status.
413 myself = utils.HostInfo().name
414 candidates = ss.GetMasterCandidates()
415 except errors.ConfigurationError, err:
416 print "Cluster configuration incomplete: '%s'" % str(err)
417 sys.exit(constants.EXIT_NODESETUP_ERROR)
418 except errors.ResolverError, err:
419 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
420 sys.exit(constants.EXIT_NODESETUP_ERROR)
422 if myself not in candidates:
424 sys.stderr.write("Not master candidate, exiting.\n")
425 sys.exit(constants.EXIT_NOTCANDIDATE)