root / lib / ssconf.py @ b24b52d9
History | View | Annotate | Download (12.6 kB)
1 | 2f31098c | Iustin Pop | #
|
---|---|---|---|
2 | a8083063 | Iustin Pop | #
|
3 | a8083063 | Iustin Pop | |
4 | c09254c2 | Iustin Pop | # Copyright (C) 2006, 2007, 2008, 2010, 2011, 2012 Google Inc.
|
5 | a8083063 | Iustin Pop | #
|
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | a8083063 | Iustin Pop | # (at your option) any later version.
|
10 | a8083063 | Iustin Pop | #
|
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | a8083063 | Iustin Pop | # General Public License for more details.
|
15 | a8083063 | Iustin Pop | #
|
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | a8083063 | Iustin Pop | # 02110-1301, USA.
|
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | """Global Configuration data for Ganeti.
|
23 | a8083063 | Iustin Pop |
|
24 | a8083063 | Iustin Pop | This module provides the interface to a special case of cluster
|
25 | a8083063 | Iustin Pop | configuration data, which is mostly static and available to all nodes.
|
26 | a8083063 | Iustin Pop |
|
27 | a8083063 | Iustin Pop | """
|
28 | a8083063 | Iustin Pop | |
29 | 5675cd1f | Iustin Pop | import sys |
30 | 7dd999fc | Manuel Franceschini | import errno |
31 | dffa96d6 | Michael Hanselmann | import logging |
32 | a8083063 | Iustin Pop | |
33 | b8028dcf | Michael Hanselmann | from ganeti import compat |
34 | a8083063 | Iustin Pop | from ganeti import errors |
35 | a8083063 | Iustin Pop | from ganeti import constants |
36 | 41a57aab | Michael Hanselmann | from ganeti import utils |
37 | a744b676 | Manuel Franceschini | from ganeti import netutils |
38 | f45c5c09 | Michael Hanselmann | from ganeti import pathutils |
39 | a8083063 | Iustin Pop | |
40 | a8083063 | Iustin Pop | |
41 | 0c223ea9 | Michael Hanselmann | SSCONF_LOCK_TIMEOUT = 10
|
42 | 0c223ea9 | Michael Hanselmann | |
43 | fb486969 | Michael Hanselmann | #: Valid ssconf keys
|
44 | b8028dcf | Michael Hanselmann | _VALID_KEYS = compat.UniqueFrozenset([ |
45 | fb486969 | Michael Hanselmann | constants.SS_CLUSTER_NAME, |
46 | fb486969 | Michael Hanselmann | constants.SS_CLUSTER_TAGS, |
47 | fb486969 | Michael Hanselmann | constants.SS_FILE_STORAGE_DIR, |
48 | fb486969 | Michael Hanselmann | constants.SS_SHARED_FILE_STORAGE_DIR, |
49 | fb486969 | Michael Hanselmann | constants.SS_MASTER_CANDIDATES, |
50 | fb486969 | Michael Hanselmann | constants.SS_MASTER_CANDIDATES_IPS, |
51 | fb486969 | Michael Hanselmann | constants.SS_MASTER_IP, |
52 | fb486969 | Michael Hanselmann | constants.SS_MASTER_NETDEV, |
53 | fb486969 | Michael Hanselmann | constants.SS_MASTER_NETMASK, |
54 | fb486969 | Michael Hanselmann | constants.SS_MASTER_NODE, |
55 | fb486969 | Michael Hanselmann | constants.SS_NODE_LIST, |
56 | fb486969 | Michael Hanselmann | constants.SS_NODE_PRIMARY_IPS, |
57 | fb486969 | Michael Hanselmann | constants.SS_NODE_SECONDARY_IPS, |
58 | fb486969 | Michael Hanselmann | constants.SS_OFFLINE_NODES, |
59 | fb486969 | Michael Hanselmann | constants.SS_ONLINE_NODES, |
60 | fb486969 | Michael Hanselmann | constants.SS_PRIMARY_IP_FAMILY, |
61 | fb486969 | Michael Hanselmann | constants.SS_INSTANCE_LIST, |
62 | fb486969 | Michael Hanselmann | constants.SS_RELEASE_VERSION, |
63 | fb486969 | Michael Hanselmann | constants.SS_HYPERVISOR_LIST, |
64 | fb486969 | Michael Hanselmann | constants.SS_MAINTAIN_NODE_HEALTH, |
65 | fb486969 | Michael Hanselmann | constants.SS_UID_POOL, |
66 | fb486969 | Michael Hanselmann | constants.SS_NODEGROUPS, |
67 | fb486969 | Michael Hanselmann | constants.SS_NETWORKS, |
68 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_PVM, |
69 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_FAKE, |
70 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_HVM, |
71 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_KVM, |
72 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_CHROOT, |
73 | def6577f | Helga Velroyen | constants.SS_HVPARAMS_XEN_LXC, |
74 | fb486969 | Michael Hanselmann | ]) |
75 | fb486969 | Michael Hanselmann | |
76 | fb486969 | Michael Hanselmann | #: Maximum size for ssconf files
|
77 | fb486969 | Michael Hanselmann | _MAX_SIZE = 128 * 1024 |
78 | fb486969 | Michael Hanselmann | |
79 | 0c223ea9 | Michael Hanselmann | |
80 | 911dfc49 | Michael Hanselmann | def ReadSsconfFile(filename): |
81 | 911dfc49 | Michael Hanselmann | """Reads an ssconf file and verifies its size.
|
82 | 911dfc49 | Michael Hanselmann |
|
83 | 911dfc49 | Michael Hanselmann | @type filename: string
|
84 | 911dfc49 | Michael Hanselmann | @param filename: Path to file
|
85 | 911dfc49 | Michael Hanselmann | @rtype: string
|
86 | 911dfc49 | Michael Hanselmann | @return: File contents without newlines at the end
|
87 | 911dfc49 | Michael Hanselmann | @raise RuntimeError: When the file size exceeds L{_MAX_SIZE}
|
88 | 911dfc49 | Michael Hanselmann |
|
89 | 911dfc49 | Michael Hanselmann | """
|
90 | 911dfc49 | Michael Hanselmann | statcb = utils.FileStatHelper() |
91 | 911dfc49 | Michael Hanselmann | |
92 | 911dfc49 | Michael Hanselmann | data = utils.ReadFile(filename, size=_MAX_SIZE, preread=statcb) |
93 | 911dfc49 | Michael Hanselmann | |
94 | 911dfc49 | Michael Hanselmann | if statcb.st.st_size > _MAX_SIZE:
|
95 | 911dfc49 | Michael Hanselmann | msg = ("File '%s' has a size of %s bytes (up to %s allowed)" %
|
96 | 911dfc49 | Michael Hanselmann | (filename, statcb.st.st_size, _MAX_SIZE)) |
97 | 911dfc49 | Michael Hanselmann | raise RuntimeError(msg) |
98 | 911dfc49 | Michael Hanselmann | |
99 | 911dfc49 | Michael Hanselmann | return data.rstrip("\n") |
100 | 911dfc49 | Michael Hanselmann | |
101 | 911dfc49 | Michael Hanselmann | |
102 | 93384844 | Iustin Pop | class SimpleStore(object): |
103 | 93384844 | Iustin Pop | """Interface to static cluster data.
|
104 | 93384844 | Iustin Pop |
|
105 | 93384844 | Iustin Pop | This is different that the config.ConfigWriter and
|
106 | 93384844 | Iustin Pop | SimpleConfigReader classes in that it holds data that will always be
|
107 | 93384844 | Iustin Pop | present, even on nodes which don't have all the cluster data.
|
108 | 93384844 | Iustin Pop |
|
109 | 93384844 | Iustin Pop | Other particularities of the datastore:
|
110 | 93384844 | Iustin Pop | - keys are restricted to predefined values
|
111 | 93384844 | Iustin Pop |
|
112 | 93384844 | Iustin Pop | """
|
113 | cc7f5bfc | Michael Hanselmann | def __init__(self, cfg_location=None, _lockfile=pathutils.SSCONF_LOCK_FILE): |
114 | 93384844 | Iustin Pop | if cfg_location is None: |
115 | f45c5c09 | Michael Hanselmann | self._cfg_dir = pathutils.DATA_DIR
|
116 | 93384844 | Iustin Pop | else:
|
117 | 93384844 | Iustin Pop | self._cfg_dir = cfg_location
|
118 | 93384844 | Iustin Pop | |
119 | cc7f5bfc | Michael Hanselmann | self._lockfile = _lockfile
|
120 | cc7f5bfc | Michael Hanselmann | |
121 | 93384844 | Iustin Pop | def KeyToFilename(self, key): |
122 | 93384844 | Iustin Pop | """Convert a given key into filename.
|
123 | 93384844 | Iustin Pop |
|
124 | 93384844 | Iustin Pop | """
|
125 | fb486969 | Michael Hanselmann | if key not in _VALID_KEYS: |
126 | 93384844 | Iustin Pop | raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'" |
127 | 93384844 | Iustin Pop | % str(key))
|
128 | 93384844 | Iustin Pop | |
129 | c09254c2 | Iustin Pop | filename = self._cfg_dir + "/" + constants.SSCONF_FILEPREFIX + key |
130 | 93384844 | Iustin Pop | return filename
|
131 | 93384844 | Iustin Pop | |
132 | 7dd999fc | Manuel Franceschini | def _ReadFile(self, key, default=None): |
133 | 93384844 | Iustin Pop | """Generic routine to read keys.
|
134 | 93384844 | Iustin Pop |
|
135 | 93384844 | Iustin Pop | This will read the file which holds the value requested. Errors
|
136 | 93384844 | Iustin Pop | will be changed into ConfigurationErrors.
|
137 | 93384844 | Iustin Pop |
|
138 | 93384844 | Iustin Pop | """
|
139 | 93384844 | Iustin Pop | filename = self.KeyToFilename(key)
|
140 | 93384844 | Iustin Pop | try:
|
141 | 911dfc49 | Michael Hanselmann | return ReadSsconfFile(filename)
|
142 | 93384844 | Iustin Pop | except EnvironmentError, err: |
143 | 7dd999fc | Manuel Franceschini | if err.errno == errno.ENOENT and default is not None: |
144 | 7dd999fc | Manuel Franceschini | return default
|
145 | c5ac51bb | Michael Hanselmann | raise errors.ConfigurationError("Can't read ssconf file %s: %s" % |
146 | c5ac51bb | Michael Hanselmann | (filename, str(err)))
|
147 | c5ac51bb | Michael Hanselmann | |
148 | 9b4329e9 | Michael Hanselmann | def ReadAll(self): |
149 | 9b4329e9 | Michael Hanselmann | """Reads all keys and returns their values.
|
150 | 9b4329e9 | Michael Hanselmann |
|
151 | 9b4329e9 | Michael Hanselmann | @rtype: dict
|
152 | 9b4329e9 | Michael Hanselmann | @return: Dictionary, ssconf key as key, value as value
|
153 | 9b4329e9 | Michael Hanselmann |
|
154 | 9b4329e9 | Michael Hanselmann | """
|
155 | 9b4329e9 | Michael Hanselmann | result = [] |
156 | 9b4329e9 | Michael Hanselmann | |
157 | 9b4329e9 | Michael Hanselmann | for key in _VALID_KEYS: |
158 | 9b4329e9 | Michael Hanselmann | try:
|
159 | 9b4329e9 | Michael Hanselmann | value = self._ReadFile(key)
|
160 | 9b4329e9 | Michael Hanselmann | except errors.ConfigurationError:
|
161 | 9b4329e9 | Michael Hanselmann | # Ignore non-existing files
|
162 | 9b4329e9 | Michael Hanselmann | pass
|
163 | 9b4329e9 | Michael Hanselmann | else:
|
164 | 9b4329e9 | Michael Hanselmann | result.append((key, value)) |
165 | 9b4329e9 | Michael Hanselmann | |
166 | 9b4329e9 | Michael Hanselmann | return dict(result) |
167 | 9b4329e9 | Michael Hanselmann | |
168 | cc7f5bfc | Michael Hanselmann | def WriteFiles(self, values, dry_run=False): |
169 | 89b14f05 | Iustin Pop | """Writes ssconf files used by external scripts.
|
170 | 89b14f05 | Iustin Pop |
|
171 | 89b14f05 | Iustin Pop | @type values: dict
|
172 | 89b14f05 | Iustin Pop | @param values: Dictionary of (name, value)
|
173 | cc7f5bfc | Michael Hanselmann | @type dry_run boolean
|
174 | cc7f5bfc | Michael Hanselmann | @param dry_run: Whether to perform a dry run
|
175 | 89b14f05 | Iustin Pop |
|
176 | 89b14f05 | Iustin Pop | """
|
177 | cc7f5bfc | Michael Hanselmann | ssconf_lock = utils.FileLock.Open(self._lockfile)
|
178 | 89b14f05 | Iustin Pop | |
179 | 89b14f05 | Iustin Pop | # Get lock while writing files
|
180 | 89b14f05 | Iustin Pop | ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
|
181 | 89b14f05 | Iustin Pop | try:
|
182 | 89b14f05 | Iustin Pop | for name, value in values.iteritems(): |
183 | 02b31f32 | Iustin Pop | if value and not value.endswith("\n"): |
184 | 89b14f05 | Iustin Pop | value += "\n"
|
185 | cc7f5bfc | Michael Hanselmann | |
186 | fb486969 | Michael Hanselmann | if len(value) > _MAX_SIZE: |
187 | cc7f5bfc | Michael Hanselmann | msg = ("Value '%s' has a length of %s bytes, but only up to %s are"
|
188 | cc7f5bfc | Michael Hanselmann | " allowed" % (name, len(value), _MAX_SIZE)) |
189 | cc7f5bfc | Michael Hanselmann | raise errors.ConfigurationError(msg)
|
190 | cc7f5bfc | Michael Hanselmann | |
191 | cd57bab6 | Michael Hanselmann | utils.WriteFile(self.KeyToFilename(name), data=value,
|
192 | cc7f5bfc | Michael Hanselmann | mode=constants.SS_FILE_PERMS, |
193 | cc7f5bfc | Michael Hanselmann | dry_run=dry_run) |
194 | 89b14f05 | Iustin Pop | finally:
|
195 | 89b14f05 | Iustin Pop | ssconf_lock.Unlock() |
196 | 89b14f05 | Iustin Pop | |
197 | 93384844 | Iustin Pop | def GetFileList(self): |
198 | 93384844 | Iustin Pop | """Return the list of all config files.
|
199 | 93384844 | Iustin Pop |
|
200 | 93384844 | Iustin Pop | This is used for computing node replication data.
|
201 | 93384844 | Iustin Pop |
|
202 | 93384844 | Iustin Pop | """
|
203 | fb486969 | Michael Hanselmann | return [self.KeyToFilename(key) for key in _VALID_KEYS] |
204 | 93384844 | Iustin Pop | |
205 | 93384844 | Iustin Pop | def GetClusterName(self): |
206 | 93384844 | Iustin Pop | """Get the cluster name.
|
207 | 93384844 | Iustin Pop |
|
208 | 93384844 | Iustin Pop | """
|
209 | 93384844 | Iustin Pop | return self._ReadFile(constants.SS_CLUSTER_NAME) |
210 | 93384844 | Iustin Pop | |
211 | 93384844 | Iustin Pop | def GetFileStorageDir(self): |
212 | 93384844 | Iustin Pop | """Get the file storage dir.
|
213 | 93384844 | Iustin Pop |
|
214 | 93384844 | Iustin Pop | """
|
215 | 93384844 | Iustin Pop | return self._ReadFile(constants.SS_FILE_STORAGE_DIR) |
216 | 93384844 | Iustin Pop | |
217 | 4b97f902 | Apollon Oikonomopoulos | def GetSharedFileStorageDir(self): |
218 | 4b97f902 | Apollon Oikonomopoulos | """Get the shared file storage dir.
|
219 | 4b97f902 | Apollon Oikonomopoulos |
|
220 | 4b97f902 | Apollon Oikonomopoulos | """
|
221 | 4b97f902 | Apollon Oikonomopoulos | return self._ReadFile(constants.SS_SHARED_FILE_STORAGE_DIR) |
222 | 4b97f902 | Apollon Oikonomopoulos | |
223 | f56618e0 | Iustin Pop | def GetMasterCandidates(self): |
224 | f56618e0 | Iustin Pop | """Return the list of master candidates.
|
225 | f56618e0 | Iustin Pop |
|
226 | f56618e0 | Iustin Pop | """
|
227 | f56618e0 | Iustin Pop | data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
|
228 | f56618e0 | Iustin Pop | nl = data.splitlines(False)
|
229 | f56618e0 | Iustin Pop | return nl
|
230 | f56618e0 | Iustin Pop | |
231 | 8113a52e | Luca Bigliardi | def GetMasterCandidatesIPList(self): |
232 | 8113a52e | Luca Bigliardi | """Return the list of master candidates' primary IP.
|
233 | 8113a52e | Luca Bigliardi |
|
234 | 8113a52e | Luca Bigliardi | """
|
235 | 8113a52e | Luca Bigliardi | data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
|
236 | 8113a52e | Luca Bigliardi | nl = data.splitlines(False)
|
237 | 8113a52e | Luca Bigliardi | return nl
|
238 | 8113a52e | Luca Bigliardi | |
239 | 93384844 | Iustin Pop | def GetMasterIP(self): |
240 | 93384844 | Iustin Pop | """Get the IP of the master node for this cluster.
|
241 | 93384844 | Iustin Pop |
|
242 | 93384844 | Iustin Pop | """
|
243 | 93384844 | Iustin Pop | return self._ReadFile(constants.SS_MASTER_IP) |
244 | 93384844 | Iustin Pop | |
245 | 93384844 | Iustin Pop | def GetMasterNetdev(self): |
246 | 93384844 | Iustin Pop | """Get the netdev to which we'll add the master ip.
|
247 | 93384844 | Iustin Pop |
|
248 | 93384844 | Iustin Pop | """
|
249 | 93384844 | Iustin Pop | return self._ReadFile(constants.SS_MASTER_NETDEV) |
250 | 93384844 | Iustin Pop | |
251 | 5a8648eb | Andrea Spadaccini | def GetMasterNetmask(self): |
252 | 186c03b3 | Andrea Spadaccini | """Get the master netmask.
|
253 | 5a8648eb | Andrea Spadaccini |
|
254 | 5a8648eb | Andrea Spadaccini | """
|
255 | 186c03b3 | Andrea Spadaccini | try:
|
256 | 186c03b3 | Andrea Spadaccini | return self._ReadFile(constants.SS_MASTER_NETMASK) |
257 | 186c03b3 | Andrea Spadaccini | except errors.ConfigurationError:
|
258 | 186c03b3 | Andrea Spadaccini | family = self.GetPrimaryIPFamily()
|
259 | 186c03b3 | Andrea Spadaccini | ipcls = netutils.IPAddress.GetClassFromIpFamily(family) |
260 | 186c03b3 | Andrea Spadaccini | return ipcls.iplen
|
261 | 5a8648eb | Andrea Spadaccini | |
262 | 93384844 | Iustin Pop | def GetMasterNode(self): |
263 | 93384844 | Iustin Pop | """Get the hostname of the master node for this cluster.
|
264 | 93384844 | Iustin Pop |
|
265 | 93384844 | Iustin Pop | """
|
266 | 93384844 | Iustin Pop | return self._ReadFile(constants.SS_MASTER_NODE) |
267 | 93384844 | Iustin Pop | |
268 | 93384844 | Iustin Pop | def GetNodeList(self): |
269 | 93384844 | Iustin Pop | """Return the list of cluster nodes.
|
270 | 93384844 | Iustin Pop |
|
271 | 93384844 | Iustin Pop | """
|
272 | 93384844 | Iustin Pop | data = self._ReadFile(constants.SS_NODE_LIST)
|
273 | 93384844 | Iustin Pop | nl = data.splitlines(False)
|
274 | 93384844 | Iustin Pop | return nl
|
275 | 93384844 | Iustin Pop | |
276 | f9780ccd | Luca Bigliardi | def GetNodePrimaryIPList(self): |
277 | f9780ccd | Luca Bigliardi | """Return the list of cluster nodes' primary IP.
|
278 | f9780ccd | Luca Bigliardi |
|
279 | f9780ccd | Luca Bigliardi | """
|
280 | f9780ccd | Luca Bigliardi | data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
|
281 | f9780ccd | Luca Bigliardi | nl = data.splitlines(False)
|
282 | f9780ccd | Luca Bigliardi | return nl
|
283 | f9780ccd | Luca Bigliardi | |
284 | f9780ccd | Luca Bigliardi | def GetNodeSecondaryIPList(self): |
285 | f9780ccd | Luca Bigliardi | """Return the list of cluster nodes' secondary IP.
|
286 | f9780ccd | Luca Bigliardi |
|
287 | f9780ccd | Luca Bigliardi | """
|
288 | f9780ccd | Luca Bigliardi | data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
|
289 | f9780ccd | Luca Bigliardi | nl = data.splitlines(False)
|
290 | f9780ccd | Luca Bigliardi | return nl
|
291 | f9780ccd | Luca Bigliardi | |
292 | 6f076453 | Guido Trotter | def GetNodegroupList(self): |
293 | 6f076453 | Guido Trotter | """Return the list of nodegroups.
|
294 | 6f076453 | Guido Trotter |
|
295 | 6f076453 | Guido Trotter | """
|
296 | 6f076453 | Guido Trotter | data = self._ReadFile(constants.SS_NODEGROUPS)
|
297 | 6f076453 | Guido Trotter | nl = data.splitlines(False)
|
298 | 6f076453 | Guido Trotter | return nl
|
299 | 6f076453 | Guido Trotter | |
300 | f2bd89b3 | Apollon Oikonomopoulos | def GetNetworkList(self): |
301 | f2bd89b3 | Apollon Oikonomopoulos | """Return the list of networks.
|
302 | f2bd89b3 | Apollon Oikonomopoulos |
|
303 | f2bd89b3 | Apollon Oikonomopoulos | """
|
304 | f2bd89b3 | Apollon Oikonomopoulos | data = self._ReadFile(constants.SS_NETWORKS)
|
305 | f2bd89b3 | Apollon Oikonomopoulos | nl = data.splitlines(False)
|
306 | f2bd89b3 | Apollon Oikonomopoulos | return nl
|
307 | f2bd89b3 | Apollon Oikonomopoulos | |
308 | 25e39bfa | Iustin Pop | def GetClusterTags(self): |
309 | 25e39bfa | Iustin Pop | """Return the cluster tags.
|
310 | 25e39bfa | Iustin Pop |
|
311 | 25e39bfa | Iustin Pop | """
|
312 | 25e39bfa | Iustin Pop | data = self._ReadFile(constants.SS_CLUSTER_TAGS)
|
313 | 25e39bfa | Iustin Pop | nl = data.splitlines(False)
|
314 | 25e39bfa | Iustin Pop | return nl
|
315 | 25e39bfa | Iustin Pop | |
316 | 4f7a6a10 | Iustin Pop | def GetHypervisorList(self): |
317 | 4f7a6a10 | Iustin Pop | """Return the list of enabled hypervisors.
|
318 | 4f7a6a10 | Iustin Pop |
|
319 | 4f7a6a10 | Iustin Pop | """
|
320 | 4f7a6a10 | Iustin Pop | data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
|
321 | 4f7a6a10 | Iustin Pop | nl = data.splitlines(False)
|
322 | 4f7a6a10 | Iustin Pop | return nl
|
323 | 4f7a6a10 | Iustin Pop | |
324 | def6577f | Helga Velroyen | def GetHvparamsForHypervisor(self, hvname): |
325 | def6577f | Helga Velroyen | """Return the hypervisor parameters of the given hypervisor.
|
326 | def6577f | Helga Velroyen |
|
327 | def6577f | Helga Velroyen | @type hvname: string
|
328 | def6577f | Helga Velroyen | @param hvname: name of the hypervisor, must be in C{constants.HYPER_TYPES}
|
329 | def6577f | Helga Velroyen | @rtype: dict of strings
|
330 | def6577f | Helga Velroyen | @returns: dictionary with hypervisor parameters
|
331 | def6577f | Helga Velroyen |
|
332 | def6577f | Helga Velroyen | """
|
333 | def6577f | Helga Velroyen | data = self._ReadFile(constants.SS_HVPARAMS_PREF + hvname)
|
334 | def6577f | Helga Velroyen | lines = data.splitlines(False)
|
335 | def6577f | Helga Velroyen | hvparams = {} |
336 | def6577f | Helga Velroyen | for line in lines: |
337 | def6577f | Helga Velroyen | (key, value) = line.split("=")
|
338 | def6577f | Helga Velroyen | hvparams[key] = value |
339 | def6577f | Helga Velroyen | return hvparams
|
340 | def6577f | Helga Velroyen | |
341 | def6577f | Helga Velroyen | def GetHvparams(self): |
342 | def6577f | Helga Velroyen | """Return the hypervisor parameters of all hypervisors.
|
343 | def6577f | Helga Velroyen |
|
344 | def6577f | Helga Velroyen | @rtype: dict of dict of strings
|
345 | def6577f | Helga Velroyen | @returns: dictionary mapping hypervisor names to hvparams
|
346 | def6577f | Helga Velroyen |
|
347 | def6577f | Helga Velroyen | """
|
348 | def6577f | Helga Velroyen | all_hvparams = {} |
349 | def6577f | Helga Velroyen | for hv in constants.HYPER_TYPES: |
350 | def6577f | Helga Velroyen | all_hvparams[hv] = self.GetHvparamsForHypervisor(hv)
|
351 | def6577f | Helga Velroyen | return all_hvparams
|
352 | def6577f | Helga Velroyen | |
353 | 5c465a95 | Iustin Pop | def GetMaintainNodeHealth(self): |
354 | 5c465a95 | Iustin Pop | """Return the value of the maintain_node_health option.
|
355 | 5c465a95 | Iustin Pop |
|
356 | 5c465a95 | Iustin Pop | """
|
357 | 5c465a95 | Iustin Pop | data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
|
358 | 5c465a95 | Iustin Pop | # we rely on the bool serialization here
|
359 | 5c465a95 | Iustin Pop | return data == "True" |
360 | 5c465a95 | Iustin Pop | |
361 | 0fbae49a | Balazs Lecz | def GetUidPool(self): |
362 | 0fbae49a | Balazs Lecz | """Return the user-id pool definition string.
|
363 | 0fbae49a | Balazs Lecz |
|
364 | 0fbae49a | Balazs Lecz | The separator character is a newline.
|
365 | 0fbae49a | Balazs Lecz |
|
366 | d3b790bb | Balazs Lecz | The return value can be parsed using uidpool.ParseUidPool()::
|
367 | d3b790bb | Balazs Lecz |
|
368 | 0fbae49a | Balazs Lecz | ss = ssconf.SimpleStore()
|
369 | d3b790bb | Balazs Lecz | uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
|
370 | 0fbae49a | Balazs Lecz |
|
371 | 0fbae49a | Balazs Lecz | """
|
372 | 0fbae49a | Balazs Lecz | data = self._ReadFile(constants.SS_UID_POOL)
|
373 | 0fbae49a | Balazs Lecz | return data
|
374 | 0fbae49a | Balazs Lecz | |
375 | 868a98ca | Manuel Franceschini | def GetPrimaryIPFamily(self): |
376 | 868a98ca | Manuel Franceschini | """Return the cluster-wide primary address family.
|
377 | 868a98ca | Manuel Franceschini |
|
378 | 868a98ca | Manuel Franceschini | """
|
379 | 868a98ca | Manuel Franceschini | try:
|
380 | 7dd999fc | Manuel Franceschini | return int(self._ReadFile(constants.SS_PRIMARY_IP_FAMILY, |
381 | 7dd999fc | Manuel Franceschini | default=netutils.IP4Address.family)) |
382 | 868a98ca | Manuel Franceschini | except (ValueError, TypeError), err: |
383 | c5ac51bb | Michael Hanselmann | raise errors.ConfigurationError("Error while trying to parse primary IP" |
384 | 868a98ca | Manuel Franceschini | " family: %s" % err)
|
385 | 868a98ca | Manuel Franceschini | |
386 | 93384844 | Iustin Pop | |
387 | cc7f5bfc | Michael Hanselmann | def WriteSsconfFiles(values, dry_run=False): |
388 | ee501db1 | Michael Hanselmann | """Update all ssconf files.
|
389 | ee501db1 | Michael Hanselmann |
|
390 | ee501db1 | Michael Hanselmann | Wrapper around L{SimpleStore.WriteFiles}.
|
391 | ee501db1 | Michael Hanselmann |
|
392 | ee501db1 | Michael Hanselmann | """
|
393 | cc7f5bfc | Michael Hanselmann | SimpleStore().WriteFiles(values, dry_run=dry_run) |
394 | ee501db1 | Michael Hanselmann | |
395 | ee501db1 | Michael Hanselmann | |
396 | b33e986b | Iustin Pop | def GetMasterAndMyself(ss=None): |
397 | b33e986b | Iustin Pop | """Get the master node and my own hostname.
|
398 | b33e986b | Iustin Pop |
|
399 | b33e986b | Iustin Pop | This can be either used for a 'soft' check (compared to CheckMaster,
|
400 | b33e986b | Iustin Pop | which exits) or just for computing both at the same time.
|
401 | b33e986b | Iustin Pop |
|
402 | b33e986b | Iustin Pop | The function does not handle any errors, these should be handled in
|
403 | b33e986b | Iustin Pop | the caller (errors.ConfigurationError, errors.ResolverError).
|
404 | b33e986b | Iustin Pop |
|
405 | 8135a2db | Iustin Pop | @param ss: either a sstore.SimpleConfigReader or a
|
406 | 8135a2db | Iustin Pop | sstore.SimpleStore instance
|
407 | 8135a2db | Iustin Pop | @rtype: tuple
|
408 | 8135a2db | Iustin Pop | @return: a tuple (master node name, my own name)
|
409 | 8135a2db | Iustin Pop |
|
410 | b33e986b | Iustin Pop | """
|
411 | b33e986b | Iustin Pop | if ss is None: |
412 | 93384844 | Iustin Pop | ss = SimpleStore() |
413 | b705c7a6 | Manuel Franceschini | return ss.GetMasterNode(), netutils.Hostname.GetSysName()
|
414 | b33e986b | Iustin Pop | |
415 | 06dc5b44 | Iustin Pop | |
416 | b33e986b | Iustin Pop | def CheckMaster(debug, ss=None): |
417 | 5675cd1f | Iustin Pop | """Checks the node setup.
|
418 | 5675cd1f | Iustin Pop |
|
419 | 5675cd1f | Iustin Pop | If this is the master, the function will return. Otherwise it will
|
420 | 5675cd1f | Iustin Pop | exit with an exit code based on the node status.
|
421 | 5675cd1f | Iustin Pop |
|
422 | 5675cd1f | Iustin Pop | """
|
423 | 5675cd1f | Iustin Pop | try:
|
424 | b33e986b | Iustin Pop | master_name, myself = GetMasterAndMyself(ss) |
425 | 5675cd1f | Iustin Pop | except errors.ConfigurationError, err:
|
426 | 5675cd1f | Iustin Pop | print "Cluster configuration incomplete: '%s'" % str(err) |
427 | 5675cd1f | Iustin Pop | sys.exit(constants.EXIT_NODESETUP_ERROR) |
428 | 5675cd1f | Iustin Pop | except errors.ResolverError, err:
|
429 | 5675cd1f | Iustin Pop | sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0]) |
430 | 5675cd1f | Iustin Pop | sys.exit(constants.EXIT_NODESETUP_ERROR) |
431 | 5675cd1f | Iustin Pop | |
432 | b33e986b | Iustin Pop | if myself != master_name:
|
433 | 5675cd1f | Iustin Pop | if debug:
|
434 | 5675cd1f | Iustin Pop | sys.stderr.write("Not master, exiting.\n")
|
435 | 5675cd1f | Iustin Pop | sys.exit(constants.EXIT_NOTMASTER) |
436 | dffa96d6 | Michael Hanselmann | |
437 | dffa96d6 | Michael Hanselmann | |
438 | dffa96d6 | Michael Hanselmann | def VerifyClusterName(name, _cfg_location=None): |
439 | dffa96d6 | Michael Hanselmann | """Verifies cluster name against a local cluster name.
|
440 | dffa96d6 | Michael Hanselmann |
|
441 | dffa96d6 | Michael Hanselmann | @type name: string
|
442 | dffa96d6 | Michael Hanselmann | @param name: Cluster name
|
443 | dffa96d6 | Michael Hanselmann |
|
444 | dffa96d6 | Michael Hanselmann | """
|
445 | dffa96d6 | Michael Hanselmann | sstore = SimpleStore(cfg_location=_cfg_location) |
446 | dffa96d6 | Michael Hanselmann | |
447 | dffa96d6 | Michael Hanselmann | try:
|
448 | dffa96d6 | Michael Hanselmann | local_name = sstore.GetClusterName() |
449 | dffa96d6 | Michael Hanselmann | except errors.ConfigurationError, err:
|
450 | dffa96d6 | Michael Hanselmann | logging.debug("Can't get local cluster name: %s", err)
|
451 | dffa96d6 | Michael Hanselmann | else:
|
452 | dffa96d6 | Michael Hanselmann | if name != local_name:
|
453 | dffa96d6 | Michael Hanselmann | raise errors.GenericError("Current cluster name is '%s'" % local_name) |
454 | 29a32ce5 | Michael Hanselmann | |
455 | 29a32ce5 | Michael Hanselmann | |
456 | 29a32ce5 | Michael Hanselmann | def VerifyKeys(keys): |
457 | 29a32ce5 | Michael Hanselmann | """Raises an exception if unknown ssconf keys are given.
|
458 | 29a32ce5 | Michael Hanselmann |
|
459 | 29a32ce5 | Michael Hanselmann | @type keys: sequence
|
460 | 29a32ce5 | Michael Hanselmann | @param keys: Key names to verify
|
461 | 29a32ce5 | Michael Hanselmann | @raise errors.GenericError: When invalid keys were found
|
462 | 29a32ce5 | Michael Hanselmann |
|
463 | 29a32ce5 | Michael Hanselmann | """
|
464 | 29a32ce5 | Michael Hanselmann | invalid = frozenset(keys) - _VALID_KEYS
|
465 | 29a32ce5 | Michael Hanselmann | if invalid:
|
466 | 29a32ce5 | Michael Hanselmann | raise errors.GenericError("Invalid ssconf keys: %s" % |
467 | 29a32ce5 | Michael Hanselmann | utils.CommaJoin(sorted(invalid))) |