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
109 self._nodes_primary_ips = []
110 self._mc_primary_ips = []
111 for node_name in self._config_data["nodes"]:
112 node = self._config_data["nodes"][node_name]
113 self._nodes_primary_ips.append(node["primary_ip"])
114 if node["master_candidate"]:
115 self._mc_primary_ips.append(node["primary_ip"])
119 # Clients can request a reload of the config file, so we export our internal
120 # _Load function as Reload.
123 def GetClusterName(self):
124 return self._config_data["cluster"]["cluster_name"]
126 def GetHostKey(self):
127 return self._config_data["cluster"]["rsahostkeypub"]
129 def GetMasterNode(self):
130 return self._config_data["cluster"]["master_node"]
132 def GetMasterIP(self):
133 return self._config_data["cluster"]["master_ip"]
135 def GetMasterNetdev(self):
136 return self._config_data["cluster"]["master_netdev"]
138 def GetFileStorageDir(self):
139 return self._config_data["cluster"]["file_storage_dir"]
141 def GetNodeList(self):
142 return self._config_data["nodes"].keys()
144 def GetConfigSerialNo(self):
145 return self._config_data["serial_no"]
147 def GetClusterSerialNo(self):
148 return self._config_data["cluster"]["serial_no"]
150 def GetNodeStatusFlags(self, node):
151 """Get a node's status flags
154 @param node: node name
155 @rtype: (bool, bool, bool)
156 @return: (master_candidate, drained, offline) (or None if no such node)
159 if node not in self._config_data["nodes"]:
162 master_candidate = self._config_data["nodes"][node]["master_candidate"]
163 drained = self._config_data["nodes"][node]["drained"]
164 offline = self._config_data["nodes"][node]["offline"]
165 return master_candidate, drained, offline
167 def GetInstanceByIp(self, ip):
168 if ip not in self._ip_to_instance:
170 return self._ip_to_instance[ip]
172 def GetNodePrimaryIp(self, node):
173 """Get a node's primary ip
176 @param node: node name
177 @rtype: string, or None
178 @return: node's primary ip, or None if no such node
181 if node not in self._config_data["nodes"]:
183 return self._config_data["nodes"][node]["primary_ip"]
185 def GetInstancePrimaryNode(self, instance):
186 """Get an instance's primary node
188 @type instance: string
189 @param instance: instance name
190 @rtype: string, or None
191 @return: primary node, or None if no such instance
194 if instance not in self._config_data["instances"]:
196 return self._config_data["instances"][instance]["primary_node"]
198 def GetNodesPrimaryIps(self):
199 return self._nodes_primary_ips
201 def GetMasterCandidatesPrimaryIps(self):
202 return self._mc_primary_ips
205 class SimpleStore(object):
206 """Interface to static cluster data.
208 This is different that the config.ConfigWriter and
209 SimpleConfigReader classes in that it holds data that will always be
210 present, even on nodes which don't have all the cluster data.
212 Other particularities of the datastore:
213 - keys are restricted to predefined values
216 _SS_FILEPREFIX = "ssconf_"
218 constants.SS_CLUSTER_NAME,
219 constants.SS_CLUSTER_TAGS,
220 constants.SS_FILE_STORAGE_DIR,
221 constants.SS_MASTER_CANDIDATES,
222 constants.SS_MASTER_CANDIDATES_IPS,
223 constants.SS_MASTER_IP,
224 constants.SS_MASTER_NETDEV,
225 constants.SS_MASTER_NODE,
226 constants.SS_NODE_LIST,
227 constants.SS_NODE_PRIMARY_IPS,
228 constants.SS_NODE_SECONDARY_IPS,
229 constants.SS_OFFLINE_NODES,
230 constants.SS_ONLINE_NODES,
231 constants.SS_INSTANCE_LIST,
232 constants.SS_RELEASE_VERSION,
236 def __init__(self, cfg_location=None):
237 if cfg_location is None:
238 self._cfg_dir = constants.DATA_DIR
240 self._cfg_dir = cfg_location
242 def KeyToFilename(self, key):
243 """Convert a given key into filename.
246 if key not in self._VALID_KEYS:
247 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
250 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
253 def _ReadFile(self, key):
254 """Generic routine to read keys.
256 This will read the file which holds the value requested. Errors
257 will be changed into ConfigurationErrors.
260 filename = self.KeyToFilename(key)
262 data = utils.ReadFile(filename, size=self._MAX_SIZE)
263 except EnvironmentError, err:
264 raise errors.ConfigurationError("Can't read from the ssconf file:"
266 data = data.rstrip('\n')
269 def WriteFiles(self, values):
270 """Writes ssconf files used by external scripts.
273 @param values: Dictionary of (name, value)
276 ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE)
278 # Get lock while writing files
279 ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
281 for name, value in values.iteritems():
282 if value and not value.endswith("\n"):
284 utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444)
288 def GetFileList(self):
289 """Return the list of all config files.
291 This is used for computing node replication data.
294 return [self.KeyToFilename(key) for key in self._VALID_KEYS]
296 def GetClusterName(self):
297 """Get the cluster name.
300 return self._ReadFile(constants.SS_CLUSTER_NAME)
302 def GetFileStorageDir(self):
303 """Get the file storage dir.
306 return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
308 def GetMasterCandidates(self):
309 """Return the list of master candidates.
312 data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
313 nl = data.splitlines(False)
316 def GetMasterCandidatesIPList(self):
317 """Return the list of master candidates' primary IP.
320 data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
321 nl = data.splitlines(False)
324 def GetMasterIP(self):
325 """Get the IP of the master node for this cluster.
328 return self._ReadFile(constants.SS_MASTER_IP)
330 def GetMasterNetdev(self):
331 """Get the netdev to which we'll add the master ip.
334 return self._ReadFile(constants.SS_MASTER_NETDEV)
336 def GetMasterNode(self):
337 """Get the hostname of the master node for this cluster.
340 return self._ReadFile(constants.SS_MASTER_NODE)
342 def GetNodeList(self):
343 """Return the list of cluster nodes.
346 data = self._ReadFile(constants.SS_NODE_LIST)
347 nl = data.splitlines(False)
350 def GetNodePrimaryIPList(self):
351 """Return the list of cluster nodes' primary IP.
354 data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
355 nl = data.splitlines(False)
358 def GetNodeSecondaryIPList(self):
359 """Return the list of cluster nodes' secondary IP.
362 data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
363 nl = data.splitlines(False)
366 def GetClusterTags(self):
367 """Return the cluster tags.
370 data = self._ReadFile(constants.SS_CLUSTER_TAGS)
371 nl = data.splitlines(False)
375 def GetMasterAndMyself(ss=None):
376 """Get the master node and my own hostname.
378 This can be either used for a 'soft' check (compared to CheckMaster,
379 which exits) or just for computing both at the same time.
381 The function does not handle any errors, these should be handled in
382 the caller (errors.ConfigurationError, errors.ResolverError).
384 @param ss: either a sstore.SimpleConfigReader or a
385 sstore.SimpleStore instance
387 @return: a tuple (master node name, my own name)
392 return ss.GetMasterNode(), utils.HostInfo().name
395 def CheckMaster(debug, ss=None):
396 """Checks the node setup.
398 If this is the master, the function will return. Otherwise it will
399 exit with an exit code based on the node status.
403 master_name, myself = GetMasterAndMyself(ss)
404 except errors.ConfigurationError, err:
405 print "Cluster configuration incomplete: '%s'" % str(err)
406 sys.exit(constants.EXIT_NODESETUP_ERROR)
407 except errors.ResolverError, err:
408 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
409 sys.exit(constants.EXIT_NODESETUP_ERROR)
411 if myself != master_name:
413 sys.stderr.write("Not master, exiting.\n")
414 sys.exit(constants.EXIT_NOTMASTER)
417 def CheckMasterCandidate(debug, ss=None):
418 """Checks the node setup.
420 If this is a master candidate, the function will return. Otherwise it will
421 exit with an exit code based on the node status.
427 myself = utils.HostInfo().name
428 candidates = ss.GetMasterCandidates()
429 except errors.ConfigurationError, err:
430 print "Cluster configuration incomplete: '%s'" % str(err)
431 sys.exit(constants.EXIT_NODESETUP_ERROR)
432 except errors.ResolverError, err:
433 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
434 sys.exit(constants.EXIT_NODESETUP_ERROR)
436 if myself not in candidates:
438 sys.stderr.write("Not master candidate, exiting.\n")
439 sys.exit(constants.EXIT_NOTCANDIDATE)