Add a new NodeGroup config object
[ganeti-local] / lib / ssconf.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2010 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Global Configuration data for Ganeti.
23
24 This module provides the interface to a special case of cluster
25 configuration data, which is mostly static and available to all nodes.
26
27 """
28
29 import sys
30 import re
31 import os
32 import errno
33
34 from ganeti import errors
35 from ganeti import constants
36 from ganeti import utils
37 from ganeti import serializer
38 from ganeti import objects
39 from ganeti import netutils
40
41
42 SSCONF_LOCK_TIMEOUT = 10
43
44 RE_VALID_SSCONF_NAME = re.compile(r'^[-_a-z0-9]+$')
45
46
47 class SimpleConfigReader(object):
48   """Simple class to read configuration file.
49
50   """
51   def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
52     """Initializes this class.
53
54     @type file_name: string
55     @param file_name: Configuration file path
56
57     """
58     self._file_name = file_name
59     self._last_inode = None
60     self._last_mtime = None
61     self._last_size = None
62
63     self._config_data = None
64     self._inst_ips_by_link = None
65     self._ip_to_inst_by_link = None
66     self._instances_ips = None
67     self._mc_primary_ips = None
68     self._nodes_primary_ips = None
69
70     # we need a forced reload at class init time, to initialize _last_*
71     self._Load(force=True)
72
73   def _Load(self, force=False):
74     """Loads (or reloads) the config file.
75
76     @type force: boolean
77     @param force: whether to force the reload without checking the mtime
78     @rtype: boolean
79     @return: boolean value that says whether we reloaded the configuration or
80              not (because we decided it was already up-to-date)
81
82     """
83     try:
84       cfg_stat = os.stat(self._file_name)
85     except EnvironmentError, err:
86       raise errors.ConfigurationError("Cannot stat config file %s: %s" %
87                                       (self._file_name, err))
88     inode = cfg_stat.st_ino
89     mtime = cfg_stat.st_mtime
90     size = cfg_stat.st_size
91
92     if (force or inode != self._last_inode or
93         mtime > self._last_mtime or
94         size != self._last_size):
95       self._last_inode = inode
96       self._last_mtime = mtime
97       self._last_size = size
98     else:
99       # Don't reload
100       return False
101
102     try:
103       self._config_data = serializer.Load(utils.ReadFile(self._file_name))
104     except EnvironmentError, err:
105       raise errors.ConfigurationError("Cannot read config file %s: %s" %
106                                       (self._file_name, err))
107     except ValueError, err:
108       raise errors.ConfigurationError("Cannot load config file %s: %s" %
109                                       (self._file_name, err))
110
111     self._ip_to_inst_by_link = {}
112     self._instances_ips = []
113     self._inst_ips_by_link = {}
114     c_nparams = self._config_data['cluster']['nicparams'][constants.PP_DEFAULT]
115     for iname in self._config_data['instances']:
116       instance = self._config_data['instances'][iname]
117       for nic in instance['nics']:
118         if 'ip' in nic and nic['ip']:
119           params = objects.FillDict(c_nparams, nic['nicparams'])
120           if not params['link'] in self._inst_ips_by_link:
121             self._inst_ips_by_link[params['link']] = []
122             self._ip_to_inst_by_link[params['link']] = {}
123           self._ip_to_inst_by_link[params['link']][nic['ip']] = iname
124           self._inst_ips_by_link[params['link']].append(nic['ip'])
125
126     self._nodes_primary_ips = []
127     self._mc_primary_ips = []
128     for node_name in self._config_data["nodes"]:
129       node = self._config_data["nodes"][node_name]
130       self._nodes_primary_ips.append(node["primary_ip"])
131       if node["master_candidate"]:
132         self._mc_primary_ips.append(node["primary_ip"])
133
134     return True
135
136   # Clients can request a reload of the config file, so we export our internal
137   # _Load function as Reload.
138   Reload = _Load
139
140   def GetClusterName(self):
141     return self._config_data["cluster"]["cluster_name"]
142
143   def GetHostKey(self):
144     return self._config_data["cluster"]["rsahostkeypub"]
145
146   def GetMasterNode(self):
147     return self._config_data["cluster"]["master_node"]
148
149   def GetMasterIP(self):
150     return self._config_data["cluster"]["master_ip"]
151
152   def GetMasterNetdev(self):
153     return self._config_data["cluster"]["master_netdev"]
154
155   def GetFileStorageDir(self):
156     return self._config_data["cluster"]["file_storage_dir"]
157
158   def GetNodeList(self):
159     return self._config_data["nodes"].keys()
160
161   def GetConfigSerialNo(self):
162     return self._config_data["serial_no"]
163
164   def GetClusterSerialNo(self):
165     return self._config_data["cluster"]["serial_no"]
166
167   def GetDefaultNicParams(self):
168     return self._config_data["cluster"]["nicparams"][constants.PP_DEFAULT]
169
170   def GetDefaultNicLink(self):
171     return self.GetDefaultNicParams()[constants.NIC_LINK]
172
173   def GetNodeStatusFlags(self, node):
174     """Get a node's status flags
175
176     @type node: string
177     @param node: node name
178     @rtype: (bool, bool, bool)
179     @return: (master_candidate, drained, offline) (or None if no such node)
180
181     """
182     if node not in self._config_data["nodes"]:
183       return None
184
185     master_candidate = self._config_data["nodes"][node]["master_candidate"]
186     drained = self._config_data["nodes"][node]["drained"]
187     offline = self._config_data["nodes"][node]["offline"]
188     return master_candidate, drained, offline
189
190   def GetInstanceByLinkIp(self, ip, link):
191     """Get instance name from its link and ip address.
192
193     @type ip: string
194     @param ip: ip address
195     @type link: string
196     @param link: nic link
197     @rtype: string
198     @return: instance name
199
200     """
201     if not link:
202       link = self.GetDefaultNicLink()
203     if not link in self._ip_to_inst_by_link:
204       return None
205     if not ip in self._ip_to_inst_by_link[link]:
206       return None
207     return self._ip_to_inst_by_link[link][ip]
208
209   def GetNodePrimaryIp(self, node):
210     """Get a node's primary ip
211
212     @type node: string
213     @param node: node name
214     @rtype: string, or None
215     @return: node's primary ip, or None if no such node
216
217     """
218     if node not in self._config_data["nodes"]:
219       return None
220     return self._config_data["nodes"][node]["primary_ip"]
221
222   def GetInstancePrimaryNode(self, instance):
223     """Get an instance's primary node
224
225     @type instance: string
226     @param instance: instance name
227     @rtype: string, or None
228     @return: primary node, or None if no such instance
229
230     """
231     if instance not in self._config_data["instances"]:
232       return None
233     return self._config_data["instances"][instance]["primary_node"]
234
235   def GetNodesPrimaryIps(self):
236     return self._nodes_primary_ips
237
238   def GetMasterCandidatesPrimaryIps(self):
239     return self._mc_primary_ips
240
241   def GetInstancesIps(self, link):
242     """Get list of nic ips connected to a certain link.
243
244     @type link: string
245     @param link: nic link
246     @rtype: list
247     @return: list of ips connected to that link
248
249     """
250     if not link:
251       link = self.GetDefaultNicLink()
252
253     if link in self._inst_ips_by_link:
254       return self._inst_ips_by_link[link]
255     else:
256       return []
257
258
259 class SimpleStore(object):
260   """Interface to static cluster data.
261
262   This is different that the config.ConfigWriter and
263   SimpleConfigReader classes in that it holds data that will always be
264   present, even on nodes which don't have all the cluster data.
265
266   Other particularities of the datastore:
267     - keys are restricted to predefined values
268
269   """
270   _SS_FILEPREFIX = "ssconf_"
271   _VALID_KEYS = (
272     constants.SS_CLUSTER_NAME,
273     constants.SS_CLUSTER_TAGS,
274     constants.SS_FILE_STORAGE_DIR,
275     constants.SS_MASTER_CANDIDATES,
276     constants.SS_MASTER_CANDIDATES_IPS,
277     constants.SS_MASTER_IP,
278     constants.SS_MASTER_NETDEV,
279     constants.SS_MASTER_NODE,
280     constants.SS_NODE_LIST,
281     constants.SS_NODE_PRIMARY_IPS,
282     constants.SS_NODE_SECONDARY_IPS,
283     constants.SS_OFFLINE_NODES,
284     constants.SS_ONLINE_NODES,
285     constants.SS_PRIMARY_IP_FAMILY,
286     constants.SS_INSTANCE_LIST,
287     constants.SS_RELEASE_VERSION,
288     constants.SS_HYPERVISOR_LIST,
289     constants.SS_MAINTAIN_NODE_HEALTH,
290     constants.SS_UID_POOL,
291     )
292   _MAX_SIZE = 131072
293
294   def __init__(self, cfg_location=None):
295     if cfg_location is None:
296       self._cfg_dir = constants.DATA_DIR
297     else:
298       self._cfg_dir = cfg_location
299
300   def KeyToFilename(self, key):
301     """Convert a given key into filename.
302
303     """
304     if key not in self._VALID_KEYS:
305       raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
306                                    % str(key))
307
308     filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
309     return filename
310
311   def _ReadFile(self, key, default=None):
312     """Generic routine to read keys.
313
314     This will read the file which holds the value requested. Errors
315     will be changed into ConfigurationErrors.
316
317     """
318     filename = self.KeyToFilename(key)
319     try:
320       data = utils.ReadFile(filename, size=self._MAX_SIZE)
321     except EnvironmentError, err:
322       if err.errno == errno.ENOENT and default is not None:
323         return default
324       raise errors.ConfigurationError("Can't read from the ssconf file:"
325                                       " '%s'" % str(err))
326     data = data.rstrip('\n')
327     return data
328
329   def WriteFiles(self, values):
330     """Writes ssconf files used by external scripts.
331
332     @type values: dict
333     @param values: Dictionary of (name, value)
334
335     """
336     ssconf_lock = utils.FileLock.Open(constants.SSCONF_LOCK_FILE)
337
338     # Get lock while writing files
339     ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
340     try:
341       for name, value in values.iteritems():
342         if value and not value.endswith("\n"):
343           value += "\n"
344         if len(value) > self._MAX_SIZE:
345           raise errors.ConfigurationError("ssconf file %s above maximum size" %
346                                           name)
347         utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444)
348     finally:
349       ssconf_lock.Unlock()
350
351   def GetFileList(self):
352     """Return the list of all config files.
353
354     This is used for computing node replication data.
355
356     """
357     return [self.KeyToFilename(key) for key in self._VALID_KEYS]
358
359   def GetClusterName(self):
360     """Get the cluster name.
361
362     """
363     return self._ReadFile(constants.SS_CLUSTER_NAME)
364
365   def GetFileStorageDir(self):
366     """Get the file storage dir.
367
368     """
369     return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
370
371   def GetMasterCandidates(self):
372     """Return the list of master candidates.
373
374     """
375     data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
376     nl = data.splitlines(False)
377     return nl
378
379   def GetMasterCandidatesIPList(self):
380     """Return the list of master candidates' primary IP.
381
382     """
383     data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
384     nl = data.splitlines(False)
385     return nl
386
387   def GetMasterIP(self):
388     """Get the IP of the master node for this cluster.
389
390     """
391     return self._ReadFile(constants.SS_MASTER_IP)
392
393   def GetMasterNetdev(self):
394     """Get the netdev to which we'll add the master ip.
395
396     """
397     return self._ReadFile(constants.SS_MASTER_NETDEV)
398
399   def GetMasterNode(self):
400     """Get the hostname of the master node for this cluster.
401
402     """
403     return self._ReadFile(constants.SS_MASTER_NODE)
404
405   def GetNodeList(self):
406     """Return the list of cluster nodes.
407
408     """
409     data = self._ReadFile(constants.SS_NODE_LIST)
410     nl = data.splitlines(False)
411     return nl
412
413   def GetNodePrimaryIPList(self):
414     """Return the list of cluster nodes' primary IP.
415
416     """
417     data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
418     nl = data.splitlines(False)
419     return nl
420
421   def GetNodeSecondaryIPList(self):
422     """Return the list of cluster nodes' secondary IP.
423
424     """
425     data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
426     nl = data.splitlines(False)
427     return nl
428
429   def GetClusterTags(self):
430     """Return the cluster tags.
431
432     """
433     data = self._ReadFile(constants.SS_CLUSTER_TAGS)
434     nl = data.splitlines(False)
435     return nl
436
437   def GetHypervisorList(self):
438     """Return the list of enabled hypervisors.
439
440     """
441     data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
442     nl = data.splitlines(False)
443     return nl
444
445   def GetMaintainNodeHealth(self):
446     """Return the value of the maintain_node_health option.
447
448     """
449     data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
450     # we rely on the bool serialization here
451     return data == "True"
452
453   def GetUidPool(self):
454     """Return the user-id pool definition string.
455
456     The separator character is a newline.
457
458     The return value can be parsed using uidpool.ParseUidPool()::
459
460       ss = ssconf.SimpleStore()
461       uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
462
463     """
464     data = self._ReadFile(constants.SS_UID_POOL)
465     return data
466
467   def GetPrimaryIPFamily(self):
468     """Return the cluster-wide primary address family.
469
470     """
471     try:
472       return int(self._ReadFile(constants.SS_PRIMARY_IP_FAMILY,
473                                 default=netutils.IP4Address.family))
474     except (ValueError, TypeError), err:
475       raise errors.ConfigurationError("Error while trying to parse primary ip"
476                                       " family: %s" % err)
477
478
479 def GetMasterAndMyself(ss=None):
480   """Get the master node and my own hostname.
481
482   This can be either used for a 'soft' check (compared to CheckMaster,
483   which exits) or just for computing both at the same time.
484
485   The function does not handle any errors, these should be handled in
486   the caller (errors.ConfigurationError, errors.ResolverError).
487
488   @param ss: either a sstore.SimpleConfigReader or a
489       sstore.SimpleStore instance
490   @rtype: tuple
491   @return: a tuple (master node name, my own name)
492
493   """
494   if ss is None:
495     ss = SimpleStore()
496   return ss.GetMasterNode(), netutils.Hostname.GetSysName()
497
498
499 def CheckMaster(debug, ss=None):
500   """Checks the node setup.
501
502   If this is the master, the function will return. Otherwise it will
503   exit with an exit code based on the node status.
504
505   """
506   try:
507     master_name, myself = GetMasterAndMyself(ss)
508   except errors.ConfigurationError, err:
509     print "Cluster configuration incomplete: '%s'" % str(err)
510     sys.exit(constants.EXIT_NODESETUP_ERROR)
511   except errors.ResolverError, err:
512     sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
513     sys.exit(constants.EXIT_NODESETUP_ERROR)
514
515   if myself != master_name:
516     if debug:
517       sys.stderr.write("Not master, exiting.\n")
518     sys.exit(constants.EXIT_NOTMASTER)