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
37 from ganeti import objects
40 SSCONF_LOCK_TIMEOUT = 10
42 RE_VALID_SSCONF_NAME = re.compile(r'^[-_a-z0-9]+$')
45 class SimpleConfigReader(object):
46 """Simple class to read configuration file.
49 def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
50 """Initializes this class.
52 @type file_name: string
53 @param file_name: Configuration file path
56 self._file_name = file_name
57 self._last_inode = None
58 self._last_mtime = None
59 self._last_size = None
61 self._config_data = None
62 self._inst_ips_by_link = None
63 self._ip_to_inst_by_link = None
64 self._mc_primary_ips = None
65 self._nodes_primary_ips = None
67 # we need a forced reload at class init time, to initialize _last_*
68 self._Load(force=True)
70 def _Load(self, force=False):
71 """Loads (or reloads) the config file.
74 @param force: whether to force the reload without checking the mtime
76 @return: boolean value that says whether we reloaded the configuration or
77 not (because we decided it was already up-to-date)
81 cfg_stat = os.stat(self._file_name)
82 except EnvironmentError, err:
83 raise errors.ConfigurationError("Cannot stat config file %s: %s" %
84 (self._file_name, err))
85 inode = cfg_stat.st_ino
86 mtime = cfg_stat.st_mtime
87 size = cfg_stat.st_size
89 if (force or inode != self._last_inode or
90 mtime > self._last_mtime or
91 size != self._last_size):
92 self._last_inode = inode
93 self._last_mtime = mtime
94 self._last_size = size
100 self._config_data = serializer.Load(utils.ReadFile(self._file_name))
101 except EnvironmentError, err:
102 raise errors.ConfigurationError("Cannot read config file %s: %s" %
103 (self._file_name, err))
104 except ValueError, err:
105 raise errors.ConfigurationError("Cannot load config file %s: %s" %
106 (self._file_name, err))
108 self._ip_to_inst_by_link = {}
109 self._instances_ips = []
110 self._inst_ips_by_link = {}
111 c_nparams = self._config_data['cluster']['nicparams'][constants.PP_DEFAULT]
112 for iname in self._config_data['instances']:
113 instance = self._config_data['instances'][iname]
114 for nic in instance['nics']:
115 if 'ip' in nic and nic['ip']:
116 params = objects.FillDict(c_nparams, nic['nicparams'])
117 if not params['link'] in self._inst_ips_by_link:
118 self._inst_ips_by_link[params['link']] = []
119 self._ip_to_inst_by_link[params['link']] = {}
120 self._ip_to_inst_by_link[params['link']][nic['ip']] = iname
121 self._inst_ips_by_link[params['link']].append(nic['ip'])
123 self._nodes_primary_ips = []
124 self._mc_primary_ips = []
125 for node_name in self._config_data["nodes"]:
126 node = self._config_data["nodes"][node_name]
127 self._nodes_primary_ips.append(node["primary_ip"])
128 if node["master_candidate"]:
129 self._mc_primary_ips.append(node["primary_ip"])
133 # Clients can request a reload of the config file, so we export our internal
134 # _Load function as Reload.
137 def GetClusterName(self):
138 return self._config_data["cluster"]["cluster_name"]
140 def GetHostKey(self):
141 return self._config_data["cluster"]["rsahostkeypub"]
143 def GetMasterNode(self):
144 return self._config_data["cluster"]["master_node"]
146 def GetMasterIP(self):
147 return self._config_data["cluster"]["master_ip"]
149 def GetMasterNetdev(self):
150 return self._config_data["cluster"]["master_netdev"]
152 def GetFileStorageDir(self):
153 return self._config_data["cluster"]["file_storage_dir"]
155 def GetNodeList(self):
156 return self._config_data["nodes"].keys()
158 def GetConfigSerialNo(self):
159 return self._config_data["serial_no"]
161 def GetClusterSerialNo(self):
162 return self._config_data["cluster"]["serial_no"]
164 def GetDefaultNicParams(self):
165 return self._config_data["cluster"]["nicparams"][constants.PP_DEFAULT]
167 def GetDefaultNicLink(self):
168 return self.GetDefaultNicParams()[constants.NIC_LINK]
170 def GetNodeStatusFlags(self, node):
171 """Get a node's status flags
174 @param node: node name
175 @rtype: (bool, bool, bool)
176 @return: (master_candidate, drained, offline) (or None if no such node)
179 if node not in self._config_data["nodes"]:
182 master_candidate = self._config_data["nodes"][node]["master_candidate"]
183 drained = self._config_data["nodes"][node]["drained"]
184 offline = self._config_data["nodes"][node]["offline"]
185 return master_candidate, drained, offline
187 def GetInstanceByLinkIp(self, ip, link):
189 link = self.GetDefaultNicLink()
190 if not link in self._ip_to_inst_by_link:
192 if not ip in self._ip_to_inst_by_link[link]:
194 return self._ip_to_inst_by_link[link][ip]
196 def GetNodePrimaryIp(self, node):
197 """Get a node's primary ip
200 @param node: node name
201 @rtype: string, or None
202 @return: node's primary ip, or None if no such node
205 if node not in self._config_data["nodes"]:
207 return self._config_data["nodes"][node]["primary_ip"]
209 def GetInstancePrimaryNode(self, instance):
210 """Get an instance's primary node
212 @type instance: string
213 @param instance: instance name
214 @rtype: string, or None
215 @return: primary node, or None if no such instance
218 if instance not in self._config_data["instances"]:
220 return self._config_data["instances"][instance]["primary_node"]
222 def GetNodesPrimaryIps(self):
223 return self._nodes_primary_ips
225 def GetMasterCandidatesPrimaryIps(self):
226 return self._mc_primary_ips
228 def GetInstancesIps(self, link):
230 link = self.GetDefaultNicLink()
232 if link in self._inst_ips_by_link:
233 return self._inst_ips_by_link[link]
238 class SimpleStore(object):
239 """Interface to static cluster data.
241 This is different that the config.ConfigWriter and
242 SimpleConfigReader classes in that it holds data that will always be
243 present, even on nodes which don't have all the cluster data.
245 Other particularities of the datastore:
246 - keys are restricted to predefined values
249 _SS_FILEPREFIX = "ssconf_"
251 constants.SS_CLUSTER_NAME,
252 constants.SS_CLUSTER_TAGS,
253 constants.SS_FILE_STORAGE_DIR,
254 constants.SS_MASTER_CANDIDATES,
255 constants.SS_MASTER_CANDIDATES_IPS,
256 constants.SS_MASTER_IP,
257 constants.SS_MASTER_NETDEV,
258 constants.SS_MASTER_NODE,
259 constants.SS_NODE_LIST,
260 constants.SS_NODE_PRIMARY_IPS,
261 constants.SS_NODE_SECONDARY_IPS,
262 constants.SS_OFFLINE_NODES,
263 constants.SS_ONLINE_NODES,
264 constants.SS_INSTANCE_LIST,
265 constants.SS_RELEASE_VERSION,
269 def __init__(self, cfg_location=None):
270 if cfg_location is None:
271 self._cfg_dir = constants.DATA_DIR
273 self._cfg_dir = cfg_location
275 def KeyToFilename(self, key):
276 """Convert a given key into filename.
279 if key not in self._VALID_KEYS:
280 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
283 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
286 def _ReadFile(self, key):
287 """Generic routine to read keys.
289 This will read the file which holds the value requested. Errors
290 will be changed into ConfigurationErrors.
293 filename = self.KeyToFilename(key)
295 data = utils.ReadFile(filename, size=self._MAX_SIZE)
296 except EnvironmentError, err:
297 raise errors.ConfigurationError("Can't read from the ssconf file:"
299 data = data.rstrip('\n')
302 def WriteFiles(self, values):
303 """Writes ssconf files used by external scripts.
306 @param values: Dictionary of (name, value)
309 ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE)
311 # Get lock while writing files
312 ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
314 for name, value in values.iteritems():
315 if value and not value.endswith("\n"):
317 utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444)
321 def GetFileList(self):
322 """Return the list of all config files.
324 This is used for computing node replication data.
327 return [self.KeyToFilename(key) for key in self._VALID_KEYS]
329 def GetClusterName(self):
330 """Get the cluster name.
333 return self._ReadFile(constants.SS_CLUSTER_NAME)
335 def GetFileStorageDir(self):
336 """Get the file storage dir.
339 return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
341 def GetMasterCandidates(self):
342 """Return the list of master candidates.
345 data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
346 nl = data.splitlines(False)
349 def GetMasterCandidatesIPList(self):
350 """Return the list of master candidates' primary IP.
353 data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
354 nl = data.splitlines(False)
357 def GetMasterIP(self):
358 """Get the IP of the master node for this cluster.
361 return self._ReadFile(constants.SS_MASTER_IP)
363 def GetMasterNetdev(self):
364 """Get the netdev to which we'll add the master ip.
367 return self._ReadFile(constants.SS_MASTER_NETDEV)
369 def GetMasterNode(self):
370 """Get the hostname of the master node for this cluster.
373 return self._ReadFile(constants.SS_MASTER_NODE)
375 def GetNodeList(self):
376 """Return the list of cluster nodes.
379 data = self._ReadFile(constants.SS_NODE_LIST)
380 nl = data.splitlines(False)
383 def GetNodePrimaryIPList(self):
384 """Return the list of cluster nodes' primary IP.
387 data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
388 nl = data.splitlines(False)
391 def GetNodeSecondaryIPList(self):
392 """Return the list of cluster nodes' secondary IP.
395 data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
396 nl = data.splitlines(False)
399 def GetClusterTags(self):
400 """Return the cluster tags.
403 data = self._ReadFile(constants.SS_CLUSTER_TAGS)
404 nl = data.splitlines(False)
408 def GetMasterAndMyself(ss=None):
409 """Get the master node and my own hostname.
411 This can be either used for a 'soft' check (compared to CheckMaster,
412 which exits) or just for computing both at the same time.
414 The function does not handle any errors, these should be handled in
415 the caller (errors.ConfigurationError, errors.ResolverError).
417 @param ss: either a sstore.SimpleConfigReader or a
418 sstore.SimpleStore instance
420 @return: a tuple (master node name, my own name)
425 return ss.GetMasterNode(), utils.HostInfo().name
428 def CheckMaster(debug, ss=None):
429 """Checks the node setup.
431 If this is the master, the function will return. Otherwise it will
432 exit with an exit code based on the node status.
436 master_name, myself = GetMasterAndMyself(ss)
437 except errors.ConfigurationError, err:
438 print "Cluster configuration incomplete: '%s'" % str(err)
439 sys.exit(constants.EXIT_NODESETUP_ERROR)
440 except errors.ResolverError, err:
441 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
442 sys.exit(constants.EXIT_NODESETUP_ERROR)
444 if myself != master_name:
446 sys.stderr.write("Not master, exiting.\n")
447 sys.exit(constants.EXIT_NOTMASTER)
450 def CheckMasterCandidate(debug, ss=None):
451 """Checks the node setup.
453 If this is a master candidate, the function will return. Otherwise it will
454 exit with an exit code based on the node status.
460 myself = utils.HostInfo().name
461 candidates = ss.GetMasterCandidates()
462 except errors.ConfigurationError, err:
463 print "Cluster configuration incomplete: '%s'" % str(err)
464 sys.exit(constants.EXIT_NODESETUP_ERROR)
465 except errors.ResolverError, err:
466 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
467 sys.exit(constants.EXIT_NODESETUP_ERROR)
469 if myself not in candidates:
471 sys.stderr.write("Not master candidate, exiting.\n")
472 sys.exit(constants.EXIT_NOTCANDIDATE)