1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 from ganeti import objects
38 from ganeti import netutils
39
40
41 SSCONF_LOCK_TIMEOUT = 10
42
43 RE_VALID_SSCONF_NAME = re.compile(r'^[-_a-z0-9]+$')
44
45
47 """Simple class to read configuration file.
48
49 """
51 """Initializes this class.
52
53 @type file_name: string
54 @param file_name: Configuration file path
55
56 """
57 self._file_name = file_name
58 self._last_inode = None
59 self._last_mtime = None
60 self._last_size = None
61
62 self._config_data = None
63 self._inst_ips_by_link = None
64 self._ip_to_inst_by_link = None
65 self._instances_ips = None
66 self._mc_primary_ips = None
67 self._nodes_primary_ips = None
68
69
70 self._Load(force=True)
71
72 - def _Load(self, force=False):
73 """Loads (or reloads) the config file.
74
75 @type force: boolean
76 @param force: whether to force the reload without checking the mtime
77 @rtype: boolean
78 @return: boolean value that says whether we reloaded the configuration or
79 not (because we decided it was already up-to-date)
80
81 """
82 try:
83 cfg_stat = os.stat(self._file_name)
84 except EnvironmentError, err:
85 raise errors.ConfigurationError("Cannot stat config file %s: %s" %
86 (self._file_name, err))
87 inode = cfg_stat.st_ino
88 mtime = cfg_stat.st_mtime
89 size = cfg_stat.st_size
90
91 if (force or inode != self._last_inode or
92 mtime > self._last_mtime or
93 size != self._last_size):
94 self._last_inode = inode
95 self._last_mtime = mtime
96 self._last_size = size
97 else:
98
99 return False
100
101 try:
102 self._config_data = serializer.Load(utils.ReadFile(self._file_name))
103 except EnvironmentError, err:
104 raise errors.ConfigurationError("Cannot read config file %s: %s" %
105 (self._file_name, err))
106 except ValueError, err:
107 raise errors.ConfigurationError("Cannot load config file %s: %s" %
108 (self._file_name, err))
109
110 self._ip_to_inst_by_link = {}
111 self._instances_ips = []
112 self._inst_ips_by_link = {}
113 c_nparams = self._config_data['cluster']['nicparams'][constants.PP_DEFAULT]
114 for iname in self._config_data['instances']:
115 instance = self._config_data['instances'][iname]
116 for nic in instance['nics']:
117 if 'ip' in nic and nic['ip']:
118 params = objects.FillDict(c_nparams, nic['nicparams'])
119 if not params['link'] in self._inst_ips_by_link:
120 self._inst_ips_by_link[params['link']] = []
121 self._ip_to_inst_by_link[params['link']] = {}
122 self._ip_to_inst_by_link[params['link']][nic['ip']] = iname
123 self._inst_ips_by_link[params['link']].append(nic['ip'])
124
125 self._nodes_primary_ips = []
126 self._mc_primary_ips = []
127 for node_name in self._config_data["nodes"]:
128 node = self._config_data["nodes"][node_name]
129 self._nodes_primary_ips.append(node["primary_ip"])
130 if node["master_candidate"]:
131 self._mc_primary_ips.append(node["primary_ip"])
132
133 return True
134
135
136
137 Reload = _Load
138
140 return self._config_data["cluster"]["cluster_name"]
141
143 return self._config_data["cluster"]["rsahostkeypub"]
144
146 return self._config_data["cluster"]["master_node"]
147
149 return self._config_data["cluster"]["master_ip"]
150
152 return self._config_data["cluster"]["master_netdev"]
153
155 return self._config_data["cluster"]["file_storage_dir"]
156
158 return self._config_data["nodes"].keys()
159
161 return self._config_data["serial_no"]
162
164 return self._config_data["cluster"]["serial_no"]
165
168
171
173 """Get a node's status flags
174
175 @type node: string
176 @param node: node name
177 @rtype: (bool, bool, bool)
178 @return: (master_candidate, drained, offline) (or None if no such node)
179
180 """
181 if node not in self._config_data["nodes"]:
182 return None
183
184 master_candidate = self._config_data["nodes"][node]["master_candidate"]
185 drained = self._config_data["nodes"][node]["drained"]
186 offline = self._config_data["nodes"][node]["offline"]
187 return master_candidate, drained, offline
188
190 """Get instance name from its link and ip address.
191
192 @type ip: string
193 @param ip: ip address
194 @type link: string
195 @param link: nic link
196 @rtype: string
197 @return: instance name
198
199 """
200 if not link:
201 link = self.GetDefaultNicLink()
202 if not link in self._ip_to_inst_by_link:
203 return None
204 if not ip in self._ip_to_inst_by_link[link]:
205 return None
206 return self._ip_to_inst_by_link[link][ip]
207
209 """Get a node's primary ip
210
211 @type node: string
212 @param node: node name
213 @rtype: string, or None
214 @return: node's primary ip, or None if no such node
215
216 """
217 if node not in self._config_data["nodes"]:
218 return None
219 return self._config_data["nodes"][node]["primary_ip"]
220
222 """Get an instance's primary node
223
224 @type instance: string
225 @param instance: instance name
226 @rtype: string, or None
227 @return: primary node, or None if no such instance
228
229 """
230 if instance not in self._config_data["instances"]:
231 return None
232 return self._config_data["instances"][instance]["primary_node"]
233
235 return self._nodes_primary_ips
236
238 return self._mc_primary_ips
239
241 """Get list of nic ips connected to a certain link.
242
243 @type link: string
244 @param link: nic link
245 @rtype: list
246 @return: list of ips connected to that link
247
248 """
249 if not link:
250 link = self.GetDefaultNicLink()
251
252 if link in self._inst_ips_by_link:
253 return self._inst_ips_by_link[link]
254 else:
255 return []
256
257
259 """Interface to static cluster data.
260
261 This is different that the config.ConfigWriter and
262 SimpleConfigReader classes in that it holds data that will always be
263 present, even on nodes which don't have all the cluster data.
264
265 Other particularities of the datastore:
266 - keys are restricted to predefined values
267
268 """
269 _SS_FILEPREFIX = "ssconf_"
270 _VALID_KEYS = (
271 constants.SS_CLUSTER_NAME,
272 constants.SS_CLUSTER_TAGS,
273 constants.SS_FILE_STORAGE_DIR,
274 constants.SS_MASTER_CANDIDATES,
275 constants.SS_MASTER_CANDIDATES_IPS,
276 constants.SS_MASTER_IP,
277 constants.SS_MASTER_NETDEV,
278 constants.SS_MASTER_NODE,
279 constants.SS_NODE_LIST,
280 constants.SS_NODE_PRIMARY_IPS,
281 constants.SS_NODE_SECONDARY_IPS,
282 constants.SS_OFFLINE_NODES,
283 constants.SS_ONLINE_NODES,
284 constants.SS_INSTANCE_LIST,
285 constants.SS_RELEASE_VERSION,
286 constants.SS_HYPERVISOR_LIST,
287 constants.SS_MAINTAIN_NODE_HEALTH,
288 constants.SS_UID_POOL,
289 )
290 _MAX_SIZE = 131072
291
293 if cfg_location is None:
294 self._cfg_dir = constants.DATA_DIR
295 else:
296 self._cfg_dir = cfg_location
297
299 """Convert a given key into filename.
300
301 """
302 if key not in self._VALID_KEYS:
303 raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
304 % str(key))
305
306 filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
307 return filename
308
310 """Generic routine to read keys.
311
312 This will read the file which holds the value requested. Errors
313 will be changed into ConfigurationErrors.
314
315 """
316 filename = self.KeyToFilename(key)
317 try:
318 data = utils.ReadFile(filename, size=self._MAX_SIZE)
319 except EnvironmentError, err:
320 raise errors.ConfigurationError("Can't read from the ssconf file:"
321 " '%s'" % str(err))
322 data = data.rstrip('\n')
323 return data
324
346
348 """Return the list of all config files.
349
350 This is used for computing node replication data.
351
352 """
353 return [self.KeyToFilename(key) for key in self._VALID_KEYS]
354
360
366
374
382
388
394
400
408
416
424
432
440
442 """Return the value of the maintain_node_health option.
443
444 """
445 data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
446
447 return data == "True"
448
450 """Return the user-id pool definition string.
451
452 The separator character is a newline.
453
454 The return value can be parsed using uidpool.ParseUidPool()::
455
456 ss = ssconf.SimpleStore()
457 uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
458
459 """
460 data = self._ReadFile(constants.SS_UID_POOL)
461 return data
462
463
465 """Get the master node and my own hostname.
466
467 This can be either used for a 'soft' check (compared to CheckMaster,
468 which exits) or just for computing both at the same time.
469
470 The function does not handle any errors, these should be handled in
471 the caller (errors.ConfigurationError, errors.ResolverError).
472
473 @param ss: either a sstore.SimpleConfigReader or a
474 sstore.SimpleStore instance
475 @rtype: tuple
476 @return: a tuple (master node name, my own name)
477
478 """
479 if ss is None:
480 ss = SimpleStore()
481 return ss.GetMasterNode(), netutils.HostInfo().name
482
483
504