root / lib / ssconf.py @ 84c3ab28
History | View | Annotate | Download (11.9 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008 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 |
|
33 |
from ganeti import errors |
34 |
from ganeti import constants |
35 |
from ganeti import utils |
36 |
from ganeti import serializer |
37 |
|
38 |
|
39 |
SSCONF_LOCK_TIMEOUT = 10
|
40 |
|
41 |
RE_VALID_SSCONF_NAME = re.compile(r'^[-_a-z0-9]+$')
|
42 |
|
43 |
|
44 |
class SimpleConfigReader(object): |
45 |
"""Simple class to read configuration file.
|
46 |
|
47 |
"""
|
48 |
def __init__(self, file_name=constants.CLUSTER_CONF_FILE): |
49 |
"""Initializes this class.
|
50 |
|
51 |
@type file_name: string
|
52 |
@param file_name: Configuration file path
|
53 |
|
54 |
"""
|
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) |
61 |
|
62 |
def _Load(self, force=False): |
63 |
"""Loads (or reloads) the config file.
|
64 |
|
65 |
@type force: boolean
|
66 |
@param force: whether to force the reload without checking the mtime
|
67 |
@rtype: boolean
|
68 |
@return: boolean values that says whether we reloaded the configuration or not
|
69 |
(because we decided it was already up-to-date)
|
70 |
|
71 |
"""
|
72 |
try:
|
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 |
80 |
|
81 |
reload = False
|
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
|
88 |
reload = True
|
89 |
|
90 |
if not reload: |
91 |
return False |
92 |
|
93 |
try:
|
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))
|
101 |
|
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 |
108 |
|
109 |
return True |
110 |
|
111 |
# Clients can request a reload of the config file, so we export our internal
|
112 |
# _Load function as Reload.
|
113 |
Reload = _Load |
114 |
|
115 |
def GetClusterName(self): |
116 |
return self._config_data["cluster"]["cluster_name"] |
117 |
|
118 |
def GetHostKey(self): |
119 |
return self._config_data["cluster"]["rsahostkeypub"] |
120 |
|
121 |
def GetMasterNode(self): |
122 |
return self._config_data["cluster"]["master_node"] |
123 |
|
124 |
def GetMasterIP(self): |
125 |
return self._config_data["cluster"]["master_ip"] |
126 |
|
127 |
def GetMasterNetdev(self): |
128 |
return self._config_data["cluster"]["master_netdev"] |
129 |
|
130 |
def GetFileStorageDir(self): |
131 |
return self._config_data["cluster"]["file_storage_dir"] |
132 |
|
133 |
def GetHypervisorType(self): |
134 |
return self._config_data["cluster"]["hypervisor"] |
135 |
|
136 |
def GetNodeList(self): |
137 |
return self._config_data["nodes"].keys() |
138 |
|
139 |
def GetConfigSerialNo(self): |
140 |
return self._config_data["serial_no"] |
141 |
|
142 |
def GetClusterSerialNo(self): |
143 |
return self._config_data["cluster"]["serial_no"] |
144 |
|
145 |
def GetNodeStatusFlags(self, node): |
146 |
"""Get a node's status flags
|
147 |
|
148 |
@type node: string
|
149 |
@param node: node name
|
150 |
@rtype: (bool, bool, bool)
|
151 |
@return: (master_candidate, drained, offline) (or None if no such node)
|
152 |
|
153 |
"""
|
154 |
if node not in self._config_data["nodes"]: |
155 |
return None |
156 |
|
157 |
master_candidate = self._config_data["nodes"][node]["master_candidate"] |
158 |
drained = self._config_data["nodes"][node]["drained"] |
159 |
offline = self._config_data["nodes"][node]["offline"] |
160 |
return master_candidate, drained, offline
|
161 |
|
162 |
def GetInstanceByIp(self, ip): |
163 |
if ip not in self._ip_to_instance: |
164 |
return None |
165 |
return self._ip_to_instance[ip] |
166 |
|
167 |
def GetNodePrimaryIp(self, node): |
168 |
"""Get a node's primary ip
|
169 |
|
170 |
@type node: string
|
171 |
@param node: node name
|
172 |
@rtype: string, or None
|
173 |
@return: node's primary ip, or None if no such node
|
174 |
|
175 |
"""
|
176 |
if node not in self._config_data["nodes"]: |
177 |
return None |
178 |
return self._config_data["nodes"][node]["primary_ip"] |
179 |
|
180 |
def GetInstancePrimaryNode(self, instance): |
181 |
"""Get an instance's primary node
|
182 |
|
183 |
@type instance: string
|
184 |
@param instance: instance name
|
185 |
@rtype: string, or None
|
186 |
@return: primary node, or None if no such instance
|
187 |
|
188 |
"""
|
189 |
if instance not in self._config_data["instances"]: |
190 |
return None |
191 |
return self._config_data["instances"][instance]["primary_node"] |
192 |
|
193 |
|
194 |
class SimpleStore(object): |
195 |
"""Interface to static cluster data.
|
196 |
|
197 |
This is different that the config.ConfigWriter and
|
198 |
SimpleConfigReader classes in that it holds data that will always be
|
199 |
present, even on nodes which don't have all the cluster data.
|
200 |
|
201 |
Other particularities of the datastore:
|
202 |
- keys are restricted to predefined values
|
203 |
|
204 |
"""
|
205 |
_SS_FILEPREFIX = "ssconf_"
|
206 |
_VALID_KEYS = ( |
207 |
constants.SS_CLUSTER_NAME, |
208 |
constants.SS_CLUSTER_TAGS, |
209 |
constants.SS_FILE_STORAGE_DIR, |
210 |
constants.SS_MASTER_CANDIDATES, |
211 |
constants.SS_MASTER_CANDIDATES_IPS, |
212 |
constants.SS_MASTER_IP, |
213 |
constants.SS_MASTER_NETDEV, |
214 |
constants.SS_MASTER_NODE, |
215 |
constants.SS_NODE_LIST, |
216 |
constants.SS_NODE_PRIMARY_IPS, |
217 |
constants.SS_NODE_SECONDARY_IPS, |
218 |
constants.SS_OFFLINE_NODES, |
219 |
constants.SS_ONLINE_NODES, |
220 |
constants.SS_INSTANCE_LIST, |
221 |
constants.SS_RELEASE_VERSION, |
222 |
) |
223 |
_MAX_SIZE = 131072
|
224 |
|
225 |
def __init__(self, cfg_location=None): |
226 |
if cfg_location is None: |
227 |
self._cfg_dir = constants.DATA_DIR
|
228 |
else:
|
229 |
self._cfg_dir = cfg_location
|
230 |
|
231 |
def KeyToFilename(self, key): |
232 |
"""Convert a given key into filename.
|
233 |
|
234 |
"""
|
235 |
if key not in self._VALID_KEYS: |
236 |
raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'" |
237 |
% str(key))
|
238 |
|
239 |
filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key |
240 |
return filename
|
241 |
|
242 |
def _ReadFile(self, key): |
243 |
"""Generic routine to read keys.
|
244 |
|
245 |
This will read the file which holds the value requested. Errors
|
246 |
will be changed into ConfigurationErrors.
|
247 |
|
248 |
"""
|
249 |
filename = self.KeyToFilename(key)
|
250 |
try:
|
251 |
data = utils.ReadFile(filename, size=self._MAX_SIZE)
|
252 |
except EnvironmentError, err: |
253 |
raise errors.ConfigurationError("Can't read from the ssconf file:" |
254 |
" '%s'" % str(err)) |
255 |
data = data.rstrip('\n')
|
256 |
return data
|
257 |
|
258 |
def WriteFiles(self, values): |
259 |
"""Writes ssconf files used by external scripts.
|
260 |
|
261 |
@type values: dict
|
262 |
@param values: Dictionary of (name, value)
|
263 |
|
264 |
"""
|
265 |
ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE) |
266 |
|
267 |
# Get lock while writing files
|
268 |
ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
|
269 |
try:
|
270 |
for name, value in values.iteritems(): |
271 |
if value and not value.endswith("\n"): |
272 |
value += "\n"
|
273 |
utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444) |
274 |
finally:
|
275 |
ssconf_lock.Unlock() |
276 |
|
277 |
def GetFileList(self): |
278 |
"""Return the list of all config files.
|
279 |
|
280 |
This is used for computing node replication data.
|
281 |
|
282 |
"""
|
283 |
return [self.KeyToFilename(key) for key in self._VALID_KEYS] |
284 |
|
285 |
def GetClusterName(self): |
286 |
"""Get the cluster name.
|
287 |
|
288 |
"""
|
289 |
return self._ReadFile(constants.SS_CLUSTER_NAME) |
290 |
|
291 |
def GetFileStorageDir(self): |
292 |
"""Get the file storage dir.
|
293 |
|
294 |
"""
|
295 |
return self._ReadFile(constants.SS_FILE_STORAGE_DIR) |
296 |
|
297 |
def GetMasterCandidates(self): |
298 |
"""Return the list of master candidates.
|
299 |
|
300 |
"""
|
301 |
data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
|
302 |
nl = data.splitlines(False)
|
303 |
return nl
|
304 |
|
305 |
def GetMasterCandidatesIPList(self): |
306 |
"""Return the list of master candidates' primary IP.
|
307 |
|
308 |
"""
|
309 |
data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
|
310 |
nl = data.splitlines(False)
|
311 |
return nl
|
312 |
|
313 |
def GetMasterIP(self): |
314 |
"""Get the IP of the master node for this cluster.
|
315 |
|
316 |
"""
|
317 |
return self._ReadFile(constants.SS_MASTER_IP) |
318 |
|
319 |
def GetMasterNetdev(self): |
320 |
"""Get the netdev to which we'll add the master ip.
|
321 |
|
322 |
"""
|
323 |
return self._ReadFile(constants.SS_MASTER_NETDEV) |
324 |
|
325 |
def GetMasterNode(self): |
326 |
"""Get the hostname of the master node for this cluster.
|
327 |
|
328 |
"""
|
329 |
return self._ReadFile(constants.SS_MASTER_NODE) |
330 |
|
331 |
def GetNodeList(self): |
332 |
"""Return the list of cluster nodes.
|
333 |
|
334 |
"""
|
335 |
data = self._ReadFile(constants.SS_NODE_LIST)
|
336 |
nl = data.splitlines(False)
|
337 |
return nl
|
338 |
|
339 |
def GetNodePrimaryIPList(self): |
340 |
"""Return the list of cluster nodes' primary IP.
|
341 |
|
342 |
"""
|
343 |
data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
|
344 |
nl = data.splitlines(False)
|
345 |
return nl
|
346 |
|
347 |
def GetNodeSecondaryIPList(self): |
348 |
"""Return the list of cluster nodes' secondary IP.
|
349 |
|
350 |
"""
|
351 |
data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
|
352 |
nl = data.splitlines(False)
|
353 |
return nl
|
354 |
|
355 |
def GetClusterTags(self): |
356 |
"""Return the cluster tags.
|
357 |
|
358 |
"""
|
359 |
data = self._ReadFile(constants.SS_CLUSTER_TAGS)
|
360 |
nl = data.splitlines(False)
|
361 |
return nl
|
362 |
|
363 |
|
364 |
def GetMasterAndMyself(ss=None): |
365 |
"""Get the master node and my own hostname.
|
366 |
|
367 |
This can be either used for a 'soft' check (compared to CheckMaster,
|
368 |
which exits) or just for computing both at the same time.
|
369 |
|
370 |
The function does not handle any errors, these should be handled in
|
371 |
the caller (errors.ConfigurationError, errors.ResolverError).
|
372 |
|
373 |
@param ss: either a sstore.SimpleConfigReader or a
|
374 |
sstore.SimpleStore instance
|
375 |
@rtype: tuple
|
376 |
@return: a tuple (master node name, my own name)
|
377 |
|
378 |
"""
|
379 |
if ss is None: |
380 |
ss = SimpleStore() |
381 |
return ss.GetMasterNode(), utils.HostInfo().name
|
382 |
|
383 |
|
384 |
def CheckMaster(debug, ss=None): |
385 |
"""Checks the node setup.
|
386 |
|
387 |
If this is the master, the function will return. Otherwise it will
|
388 |
exit with an exit code based on the node status.
|
389 |
|
390 |
"""
|
391 |
try:
|
392 |
master_name, myself = GetMasterAndMyself(ss) |
393 |
except errors.ConfigurationError, err:
|
394 |
print "Cluster configuration incomplete: '%s'" % str(err) |
395 |
sys.exit(constants.EXIT_NODESETUP_ERROR) |
396 |
except errors.ResolverError, err:
|
397 |
sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0]) |
398 |
sys.exit(constants.EXIT_NODESETUP_ERROR) |
399 |
|
400 |
if myself != master_name:
|
401 |
if debug:
|
402 |
sys.stderr.write("Not master, exiting.\n")
|
403 |
sys.exit(constants.EXIT_NOTMASTER) |
404 |
|
405 |
|
406 |
def CheckMasterCandidate(debug, ss=None): |
407 |
"""Checks the node setup.
|
408 |
|
409 |
If this is a master candidate, the function will return. Otherwise it will
|
410 |
exit with an exit code based on the node status.
|
411 |
|
412 |
"""
|
413 |
try:
|
414 |
if ss is None: |
415 |
ss = SimpleStore() |
416 |
myself = utils.HostInfo().name |
417 |
candidates = ss.GetMasterCandidates() |
418 |
except errors.ConfigurationError, err:
|
419 |
print "Cluster configuration incomplete: '%s'" % str(err) |
420 |
sys.exit(constants.EXIT_NODESETUP_ERROR) |
421 |
except errors.ResolverError, err:
|
422 |
sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0]) |
423 |
sys.exit(constants.EXIT_NODESETUP_ERROR) |
424 |
|
425 |
if myself not in candidates: |
426 |
if debug:
|
427 |
sys.stderr.write("Not master candidate, exiting.\n")
|
428 |
sys.exit(constants.EXIT_NOTCANDIDATE) |
429 |
|