Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ c89622cd

History | View | Annotate | Download (13.5 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 d3e6fd0e Santi Raffa
  constants.SS_GLUSTER_STORAGE_DIR,
50 fb486969 Michael Hanselmann
  constants.SS_MASTER_CANDIDATES,
51 fb486969 Michael Hanselmann
  constants.SS_MASTER_CANDIDATES_IPS,
52 1059337d Helga Velroyen
  constants.SS_MASTER_CANDIDATES_CERTS,
53 fb486969 Michael Hanselmann
  constants.SS_MASTER_IP,
54 fb486969 Michael Hanselmann
  constants.SS_MASTER_NETDEV,
55 fb486969 Michael Hanselmann
  constants.SS_MASTER_NETMASK,
56 fb486969 Michael Hanselmann
  constants.SS_MASTER_NODE,
57 fb486969 Michael Hanselmann
  constants.SS_NODE_LIST,
58 fb486969 Michael Hanselmann
  constants.SS_NODE_PRIMARY_IPS,
59 fb486969 Michael Hanselmann
  constants.SS_NODE_SECONDARY_IPS,
60 fb486969 Michael Hanselmann
  constants.SS_OFFLINE_NODES,
61 fb486969 Michael Hanselmann
  constants.SS_ONLINE_NODES,
62 fb486969 Michael Hanselmann
  constants.SS_PRIMARY_IP_FAMILY,
63 fb486969 Michael Hanselmann
  constants.SS_INSTANCE_LIST,
64 fb486969 Michael Hanselmann
  constants.SS_RELEASE_VERSION,
65 fb486969 Michael Hanselmann
  constants.SS_HYPERVISOR_LIST,
66 fb486969 Michael Hanselmann
  constants.SS_MAINTAIN_NODE_HEALTH,
67 fb486969 Michael Hanselmann
  constants.SS_UID_POOL,
68 fb486969 Michael Hanselmann
  constants.SS_NODEGROUPS,
69 fb486969 Michael Hanselmann
  constants.SS_NETWORKS,
70 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_PVM,
71 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_FAKE,
72 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_HVM,
73 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_KVM,
74 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_CHROOT,
75 def6577f Helga Velroyen
  constants.SS_HVPARAMS_XEN_LXC,
76 fb486969 Michael Hanselmann
  ])
77 fb486969 Michael Hanselmann
78 fb486969 Michael Hanselmann
#: Maximum size for ssconf files
79 fb486969 Michael Hanselmann
_MAX_SIZE = 128 * 1024
80 fb486969 Michael Hanselmann
81 0c223ea9 Michael Hanselmann
82 911dfc49 Michael Hanselmann
def ReadSsconfFile(filename):
83 911dfc49 Michael Hanselmann
  """Reads an ssconf file and verifies its size.
84 911dfc49 Michael Hanselmann

85 911dfc49 Michael Hanselmann
  @type filename: string
86 911dfc49 Michael Hanselmann
  @param filename: Path to file
87 911dfc49 Michael Hanselmann
  @rtype: string
88 911dfc49 Michael Hanselmann
  @return: File contents without newlines at the end
89 911dfc49 Michael Hanselmann
  @raise RuntimeError: When the file size exceeds L{_MAX_SIZE}
90 911dfc49 Michael Hanselmann

91 911dfc49 Michael Hanselmann
  """
92 911dfc49 Michael Hanselmann
  statcb = utils.FileStatHelper()
93 911dfc49 Michael Hanselmann
94 911dfc49 Michael Hanselmann
  data = utils.ReadFile(filename, size=_MAX_SIZE, preread=statcb)
95 911dfc49 Michael Hanselmann
96 911dfc49 Michael Hanselmann
  if statcb.st.st_size > _MAX_SIZE:
97 911dfc49 Michael Hanselmann
    msg = ("File '%s' has a size of %s bytes (up to %s allowed)" %
98 911dfc49 Michael Hanselmann
           (filename, statcb.st.st_size, _MAX_SIZE))
99 911dfc49 Michael Hanselmann
    raise RuntimeError(msg)
100 911dfc49 Michael Hanselmann
101 911dfc49 Michael Hanselmann
  return data.rstrip("\n")
102 911dfc49 Michael Hanselmann
103 911dfc49 Michael Hanselmann
104 93384844 Iustin Pop
class SimpleStore(object):
105 93384844 Iustin Pop
  """Interface to static cluster data.
106 93384844 Iustin Pop

107 93384844 Iustin Pop
  This is different that the config.ConfigWriter and
108 93384844 Iustin Pop
  SimpleConfigReader classes in that it holds data that will always be
109 93384844 Iustin Pop
  present, even on nodes which don't have all the cluster data.
110 93384844 Iustin Pop

111 93384844 Iustin Pop
  Other particularities of the datastore:
112 93384844 Iustin Pop
    - keys are restricted to predefined values
113 93384844 Iustin Pop

114 93384844 Iustin Pop
  """
115 cc7f5bfc Michael Hanselmann
  def __init__(self, cfg_location=None, _lockfile=pathutils.SSCONF_LOCK_FILE):
116 93384844 Iustin Pop
    if cfg_location is None:
117 f45c5c09 Michael Hanselmann
      self._cfg_dir = pathutils.DATA_DIR
118 93384844 Iustin Pop
    else:
119 93384844 Iustin Pop
      self._cfg_dir = cfg_location
120 93384844 Iustin Pop
121 cc7f5bfc Michael Hanselmann
    self._lockfile = _lockfile
122 cc7f5bfc Michael Hanselmann
123 93384844 Iustin Pop
  def KeyToFilename(self, key):
124 93384844 Iustin Pop
    """Convert a given key into filename.
125 93384844 Iustin Pop

126 93384844 Iustin Pop
    """
127 fb486969 Michael Hanselmann
    if key not in _VALID_KEYS:
128 93384844 Iustin Pop
      raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
129 93384844 Iustin Pop
                                   % str(key))
130 93384844 Iustin Pop
131 c09254c2 Iustin Pop
    filename = self._cfg_dir + "/" + constants.SSCONF_FILEPREFIX + key
132 93384844 Iustin Pop
    return filename
133 93384844 Iustin Pop
134 7dd999fc Manuel Franceschini
  def _ReadFile(self, key, default=None):
135 93384844 Iustin Pop
    """Generic routine to read keys.
136 93384844 Iustin Pop

137 93384844 Iustin Pop
    This will read the file which holds the value requested. Errors
138 93384844 Iustin Pop
    will be changed into ConfigurationErrors.
139 93384844 Iustin Pop

140 93384844 Iustin Pop
    """
141 93384844 Iustin Pop
    filename = self.KeyToFilename(key)
142 93384844 Iustin Pop
    try:
143 911dfc49 Michael Hanselmann
      return ReadSsconfFile(filename)
144 93384844 Iustin Pop
    except EnvironmentError, err:
145 7dd999fc Manuel Franceschini
      if err.errno == errno.ENOENT and default is not None:
146 7dd999fc Manuel Franceschini
        return default
147 c5ac51bb Michael Hanselmann
      raise errors.ConfigurationError("Can't read ssconf file %s: %s" %
148 c5ac51bb Michael Hanselmann
                                      (filename, str(err)))
149 c5ac51bb Michael Hanselmann
150 9b4329e9 Michael Hanselmann
  def ReadAll(self):
151 9b4329e9 Michael Hanselmann
    """Reads all keys and returns their values.
152 9b4329e9 Michael Hanselmann

153 9b4329e9 Michael Hanselmann
    @rtype: dict
154 9b4329e9 Michael Hanselmann
    @return: Dictionary, ssconf key as key, value as value
155 9b4329e9 Michael Hanselmann

156 9b4329e9 Michael Hanselmann
    """
157 9b4329e9 Michael Hanselmann
    result = []
158 9b4329e9 Michael Hanselmann
159 9b4329e9 Michael Hanselmann
    for key in _VALID_KEYS:
160 9b4329e9 Michael Hanselmann
      try:
161 9b4329e9 Michael Hanselmann
        value = self._ReadFile(key)
162 9b4329e9 Michael Hanselmann
      except errors.ConfigurationError:
163 9b4329e9 Michael Hanselmann
        # Ignore non-existing files
164 9b4329e9 Michael Hanselmann
        pass
165 9b4329e9 Michael Hanselmann
      else:
166 9b4329e9 Michael Hanselmann
        result.append((key, value))
167 9b4329e9 Michael Hanselmann
168 9b4329e9 Michael Hanselmann
    return dict(result)
169 9b4329e9 Michael Hanselmann
170 cc7f5bfc Michael Hanselmann
  def WriteFiles(self, values, dry_run=False):
171 89b14f05 Iustin Pop
    """Writes ssconf files used by external scripts.
172 89b14f05 Iustin Pop

173 89b14f05 Iustin Pop
    @type values: dict
174 89b14f05 Iustin Pop
    @param values: Dictionary of (name, value)
175 cc7f5bfc Michael Hanselmann
    @type dry_run boolean
176 cc7f5bfc Michael Hanselmann
    @param dry_run: Whether to perform a dry run
177 89b14f05 Iustin Pop

178 89b14f05 Iustin Pop
    """
179 cc7f5bfc Michael Hanselmann
    ssconf_lock = utils.FileLock.Open(self._lockfile)
180 89b14f05 Iustin Pop
181 89b14f05 Iustin Pop
    # Get lock while writing files
182 89b14f05 Iustin Pop
    ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
183 89b14f05 Iustin Pop
    try:
184 89b14f05 Iustin Pop
      for name, value in values.iteritems():
185 3fb0d66f Petr Pudlak
        if isinstance(value, (list, tuple)):
186 3fb0d66f Petr Pudlak
          value = "\n".join(value)
187 02b31f32 Iustin Pop
        if value and not value.endswith("\n"):
188 89b14f05 Iustin Pop
          value += "\n"
189 cc7f5bfc Michael Hanselmann
190 fb486969 Michael Hanselmann
        if len(value) > _MAX_SIZE:
191 cc7f5bfc Michael Hanselmann
          msg = ("Value '%s' has a length of %s bytes, but only up to %s are"
192 cc7f5bfc Michael Hanselmann
                 " allowed" % (name, len(value), _MAX_SIZE))
193 cc7f5bfc Michael Hanselmann
          raise errors.ConfigurationError(msg)
194 cc7f5bfc Michael Hanselmann
195 cd57bab6 Michael Hanselmann
        utils.WriteFile(self.KeyToFilename(name), data=value,
196 cc7f5bfc Michael Hanselmann
                        mode=constants.SS_FILE_PERMS,
197 cc7f5bfc Michael Hanselmann
                        dry_run=dry_run)
198 89b14f05 Iustin Pop
    finally:
199 89b14f05 Iustin Pop
      ssconf_lock.Unlock()
200 89b14f05 Iustin Pop
201 93384844 Iustin Pop
  def GetFileList(self):
202 93384844 Iustin Pop
    """Return the list of all config files.
203 93384844 Iustin Pop

204 93384844 Iustin Pop
    This is used for computing node replication data.
205 93384844 Iustin Pop

206 93384844 Iustin Pop
    """
207 fb486969 Michael Hanselmann
    return [self.KeyToFilename(key) for key in _VALID_KEYS]
208 93384844 Iustin Pop
209 93384844 Iustin Pop
  def GetClusterName(self):
210 93384844 Iustin Pop
    """Get the cluster name.
211 93384844 Iustin Pop

212 93384844 Iustin Pop
    """
213 93384844 Iustin Pop
    return self._ReadFile(constants.SS_CLUSTER_NAME)
214 93384844 Iustin Pop
215 93384844 Iustin Pop
  def GetFileStorageDir(self):
216 93384844 Iustin Pop
    """Get the file storage dir.
217 93384844 Iustin Pop

218 93384844 Iustin Pop
    """
219 93384844 Iustin Pop
    return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
220 93384844 Iustin Pop
221 4b97f902 Apollon Oikonomopoulos
  def GetSharedFileStorageDir(self):
222 4b97f902 Apollon Oikonomopoulos
    """Get the shared file storage dir.
223 4b97f902 Apollon Oikonomopoulos

224 4b97f902 Apollon Oikonomopoulos
    """
225 4b97f902 Apollon Oikonomopoulos
    return self._ReadFile(constants.SS_SHARED_FILE_STORAGE_DIR)
226 4b97f902 Apollon Oikonomopoulos
227 d3e6fd0e Santi Raffa
  def GetGlusterStorageDir(self):
228 d3e6fd0e Santi Raffa
    """Get the Gluster storage dir.
229 d3e6fd0e Santi Raffa

230 d3e6fd0e Santi Raffa
    """
231 d3e6fd0e Santi Raffa
    return self._ReadFile(constants.SS_GLUSTER_STORAGE_DIR)
232 d3e6fd0e Santi Raffa
233 f56618e0 Iustin Pop
  def GetMasterCandidates(self):
234 f56618e0 Iustin Pop
    """Return the list of master candidates.
235 f56618e0 Iustin Pop

236 f56618e0 Iustin Pop
    """
237 f56618e0 Iustin Pop
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
238 f56618e0 Iustin Pop
    nl = data.splitlines(False)
239 f56618e0 Iustin Pop
    return nl
240 f56618e0 Iustin Pop
241 8113a52e Luca Bigliardi
  def GetMasterCandidatesIPList(self):
242 8113a52e Luca Bigliardi
    """Return the list of master candidates' primary IP.
243 8113a52e Luca Bigliardi

244 8113a52e Luca Bigliardi
    """
245 8113a52e Luca Bigliardi
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
246 8113a52e Luca Bigliardi
    nl = data.splitlines(False)
247 8113a52e Luca Bigliardi
    return nl
248 8113a52e Luca Bigliardi
249 1059337d Helga Velroyen
  def GetMasterCandidatesCertMap(self):
250 1059337d Helga Velroyen
    """Returns the map of master candidate UUIDs to ssl cert.
251 1059337d Helga Velroyen

252 1059337d Helga Velroyen
    @rtype: dict of string to string
253 1059337d Helga Velroyen
    @return: dictionary mapping the master candidates' UUIDs
254 1059337d Helga Velroyen
      to their SSL certificate digests
255 1059337d Helga Velroyen

256 1059337d Helga Velroyen
    """
257 1059337d Helga Velroyen
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES_CERTS)
258 1059337d Helga Velroyen
    lines = data.splitlines(False)
259 1059337d Helga Velroyen
    certs = {}
260 1059337d Helga Velroyen
    for line in lines:
261 1059337d Helga Velroyen
      (node_uuid, cert_digest) = line.split("=")
262 1059337d Helga Velroyen
      certs[node_uuid] = cert_digest
263 1059337d Helga Velroyen
    return certs
264 1059337d Helga Velroyen
265 93384844 Iustin Pop
  def GetMasterIP(self):
266 93384844 Iustin Pop
    """Get the IP of the master node for this cluster.
267 93384844 Iustin Pop

268 93384844 Iustin Pop
    """
269 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_IP)
270 93384844 Iustin Pop
271 93384844 Iustin Pop
  def GetMasterNetdev(self):
272 93384844 Iustin Pop
    """Get the netdev to which we'll add the master ip.
273 93384844 Iustin Pop

274 93384844 Iustin Pop
    """
275 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NETDEV)
276 93384844 Iustin Pop
277 5a8648eb Andrea Spadaccini
  def GetMasterNetmask(self):
278 186c03b3 Andrea Spadaccini
    """Get the master netmask.
279 5a8648eb Andrea Spadaccini

280 5a8648eb Andrea Spadaccini
    """
281 186c03b3 Andrea Spadaccini
    try:
282 186c03b3 Andrea Spadaccini
      return self._ReadFile(constants.SS_MASTER_NETMASK)
283 186c03b3 Andrea Spadaccini
    except errors.ConfigurationError:
284 186c03b3 Andrea Spadaccini
      family = self.GetPrimaryIPFamily()
285 186c03b3 Andrea Spadaccini
      ipcls = netutils.IPAddress.GetClassFromIpFamily(family)
286 186c03b3 Andrea Spadaccini
      return ipcls.iplen
287 5a8648eb Andrea Spadaccini
288 93384844 Iustin Pop
  def GetMasterNode(self):
289 93384844 Iustin Pop
    """Get the hostname of the master node for this cluster.
290 93384844 Iustin Pop

291 93384844 Iustin Pop
    """
292 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NODE)
293 93384844 Iustin Pop
294 93384844 Iustin Pop
  def GetNodeList(self):
295 93384844 Iustin Pop
    """Return the list of cluster nodes.
296 93384844 Iustin Pop

297 93384844 Iustin Pop
    """
298 93384844 Iustin Pop
    data = self._ReadFile(constants.SS_NODE_LIST)
299 93384844 Iustin Pop
    nl = data.splitlines(False)
300 93384844 Iustin Pop
    return nl
301 93384844 Iustin Pop
302 790fc19d Klaus Aehlig
  def GetOnlineNodeList(self):
303 790fc19d Klaus Aehlig
    """Return the list of online cluster nodes.
304 790fc19d Klaus Aehlig

305 790fc19d Klaus Aehlig
    """
306 790fc19d Klaus Aehlig
    data = self._ReadFile(constants.SS_ONLINE_NODES)
307 790fc19d Klaus Aehlig
    nl = data.splitlines(False)
308 790fc19d Klaus Aehlig
    return nl
309 790fc19d Klaus Aehlig
310 f9780ccd Luca Bigliardi
  def GetNodePrimaryIPList(self):
311 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' primary IP.
312 f9780ccd Luca Bigliardi

313 f9780ccd Luca Bigliardi
    """
314 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
315 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
316 f9780ccd Luca Bigliardi
    return nl
317 f9780ccd Luca Bigliardi
318 f9780ccd Luca Bigliardi
  def GetNodeSecondaryIPList(self):
319 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' secondary IP.
320 f9780ccd Luca Bigliardi

321 f9780ccd Luca Bigliardi
    """
322 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
323 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
324 f9780ccd Luca Bigliardi
    return nl
325 f9780ccd Luca Bigliardi
326 6f076453 Guido Trotter
  def GetNodegroupList(self):
327 6f076453 Guido Trotter
    """Return the list of nodegroups.
328 6f076453 Guido Trotter

329 6f076453 Guido Trotter
    """
330 6f076453 Guido Trotter
    data = self._ReadFile(constants.SS_NODEGROUPS)
331 6f076453 Guido Trotter
    nl = data.splitlines(False)
332 6f076453 Guido Trotter
    return nl
333 6f076453 Guido Trotter
334 f2bd89b3 Apollon Oikonomopoulos
  def GetNetworkList(self):
335 f2bd89b3 Apollon Oikonomopoulos
    """Return the list of networks.
336 f2bd89b3 Apollon Oikonomopoulos

337 f2bd89b3 Apollon Oikonomopoulos
    """
338 f2bd89b3 Apollon Oikonomopoulos
    data = self._ReadFile(constants.SS_NETWORKS)
339 f2bd89b3 Apollon Oikonomopoulos
    nl = data.splitlines(False)
340 f2bd89b3 Apollon Oikonomopoulos
    return nl
341 f2bd89b3 Apollon Oikonomopoulos
342 25e39bfa Iustin Pop
  def GetClusterTags(self):
343 25e39bfa Iustin Pop
    """Return the cluster tags.
344 25e39bfa Iustin Pop

345 25e39bfa Iustin Pop
    """
346 25e39bfa Iustin Pop
    data = self._ReadFile(constants.SS_CLUSTER_TAGS)
347 25e39bfa Iustin Pop
    nl = data.splitlines(False)
348 25e39bfa Iustin Pop
    return nl
349 25e39bfa Iustin Pop
350 4f7a6a10 Iustin Pop
  def GetHypervisorList(self):
351 4f7a6a10 Iustin Pop
    """Return the list of enabled hypervisors.
352 4f7a6a10 Iustin Pop

353 4f7a6a10 Iustin Pop
    """
354 4f7a6a10 Iustin Pop
    data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
355 4f7a6a10 Iustin Pop
    nl = data.splitlines(False)
356 4f7a6a10 Iustin Pop
    return nl
357 4f7a6a10 Iustin Pop
358 def6577f Helga Velroyen
  def GetHvparamsForHypervisor(self, hvname):
359 def6577f Helga Velroyen
    """Return the hypervisor parameters of the given hypervisor.
360 def6577f Helga Velroyen

361 def6577f Helga Velroyen
    @type hvname: string
362 def6577f Helga Velroyen
    @param hvname: name of the hypervisor, must be in C{constants.HYPER_TYPES}
363 def6577f Helga Velroyen
    @rtype: dict of strings
364 def6577f Helga Velroyen
    @returns: dictionary with hypervisor parameters
365 def6577f Helga Velroyen

366 def6577f Helga Velroyen
    """
367 def6577f Helga Velroyen
    data = self._ReadFile(constants.SS_HVPARAMS_PREF + hvname)
368 def6577f Helga Velroyen
    lines = data.splitlines(False)
369 def6577f Helga Velroyen
    hvparams = {}
370 def6577f Helga Velroyen
    for line in lines:
371 def6577f Helga Velroyen
      (key, value) = line.split("=")
372 def6577f Helga Velroyen
      hvparams[key] = value
373 def6577f Helga Velroyen
    return hvparams
374 def6577f Helga Velroyen
375 def6577f Helga Velroyen
  def GetHvparams(self):
376 def6577f Helga Velroyen
    """Return the hypervisor parameters of all hypervisors.
377 def6577f Helga Velroyen

378 def6577f Helga Velroyen
    @rtype: dict of dict of strings
379 def6577f Helga Velroyen
    @returns: dictionary mapping hypervisor names to hvparams
380 def6577f Helga Velroyen

381 def6577f Helga Velroyen
    """
382 def6577f Helga Velroyen
    all_hvparams = {}
383 def6577f Helga Velroyen
    for hv in constants.HYPER_TYPES:
384 def6577f Helga Velroyen
      all_hvparams[hv] = self.GetHvparamsForHypervisor(hv)
385 def6577f Helga Velroyen
    return all_hvparams
386 def6577f Helga Velroyen
387 5c465a95 Iustin Pop
  def GetMaintainNodeHealth(self):
388 5c465a95 Iustin Pop
    """Return the value of the maintain_node_health option.
389 5c465a95 Iustin Pop

390 5c465a95 Iustin Pop
    """
391 5c465a95 Iustin Pop
    data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
392 5c465a95 Iustin Pop
    # we rely on the bool serialization here
393 5c465a95 Iustin Pop
    return data == "True"
394 5c465a95 Iustin Pop
395 0fbae49a Balazs Lecz
  def GetUidPool(self):
396 0fbae49a Balazs Lecz
    """Return the user-id pool definition string.
397 0fbae49a Balazs Lecz

398 0fbae49a Balazs Lecz
    The separator character is a newline.
399 0fbae49a Balazs Lecz

400 d3b790bb Balazs Lecz
    The return value can be parsed using uidpool.ParseUidPool()::
401 d3b790bb Balazs Lecz

402 0fbae49a Balazs Lecz
      ss = ssconf.SimpleStore()
403 d3b790bb Balazs Lecz
      uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
404 0fbae49a Balazs Lecz

405 0fbae49a Balazs Lecz
    """
406 0fbae49a Balazs Lecz
    data = self._ReadFile(constants.SS_UID_POOL)
407 0fbae49a Balazs Lecz
    return data
408 0fbae49a Balazs Lecz
409 868a98ca Manuel Franceschini
  def GetPrimaryIPFamily(self):
410 868a98ca Manuel Franceschini
    """Return the cluster-wide primary address family.
411 868a98ca Manuel Franceschini

412 868a98ca Manuel Franceschini
    """
413 868a98ca Manuel Franceschini
    try:
414 7dd999fc Manuel Franceschini
      return int(self._ReadFile(constants.SS_PRIMARY_IP_FAMILY,
415 7dd999fc Manuel Franceschini
                                default=netutils.IP4Address.family))
416 868a98ca Manuel Franceschini
    except (ValueError, TypeError), err:
417 c5ac51bb Michael Hanselmann
      raise errors.ConfigurationError("Error while trying to parse primary IP"
418 868a98ca Manuel Franceschini
                                      " family: %s" % err)
419 868a98ca Manuel Franceschini
420 93384844 Iustin Pop
421 cc7f5bfc Michael Hanselmann
def WriteSsconfFiles(values, dry_run=False):
422 ee501db1 Michael Hanselmann
  """Update all ssconf files.
423 ee501db1 Michael Hanselmann

424 ee501db1 Michael Hanselmann
  Wrapper around L{SimpleStore.WriteFiles}.
425 ee501db1 Michael Hanselmann

426 ee501db1 Michael Hanselmann
  """
427 cc7f5bfc Michael Hanselmann
  SimpleStore().WriteFiles(values, dry_run=dry_run)
428 ee501db1 Michael Hanselmann
429 ee501db1 Michael Hanselmann
430 b33e986b Iustin Pop
def GetMasterAndMyself(ss=None):
431 b33e986b Iustin Pop
  """Get the master node and my own hostname.
432 b33e986b Iustin Pop

433 b33e986b Iustin Pop
  This can be either used for a 'soft' check (compared to CheckMaster,
434 b33e986b Iustin Pop
  which exits) or just for computing both at the same time.
435 b33e986b Iustin Pop

436 b33e986b Iustin Pop
  The function does not handle any errors, these should be handled in
437 b33e986b Iustin Pop
  the caller (errors.ConfigurationError, errors.ResolverError).
438 b33e986b Iustin Pop

439 8135a2db Iustin Pop
  @param ss: either a sstore.SimpleConfigReader or a
440 8135a2db Iustin Pop
      sstore.SimpleStore instance
441 8135a2db Iustin Pop
  @rtype: tuple
442 8135a2db Iustin Pop
  @return: a tuple (master node name, my own name)
443 8135a2db Iustin Pop

444 b33e986b Iustin Pop
  """
445 b33e986b Iustin Pop
  if ss is None:
446 93384844 Iustin Pop
    ss = SimpleStore()
447 b705c7a6 Manuel Franceschini
  return ss.GetMasterNode(), netutils.Hostname.GetSysName()
448 b33e986b Iustin Pop
449 06dc5b44 Iustin Pop
450 b33e986b Iustin Pop
def CheckMaster(debug, ss=None):
451 5675cd1f Iustin Pop
  """Checks the node setup.
452 5675cd1f Iustin Pop

453 5675cd1f Iustin Pop
  If this is the master, the function will return. Otherwise it will
454 5675cd1f Iustin Pop
  exit with an exit code based on the node status.
455 5675cd1f Iustin Pop

456 5675cd1f Iustin Pop
  """
457 5675cd1f Iustin Pop
  try:
458 b33e986b Iustin Pop
    master_name, myself = GetMasterAndMyself(ss)
459 5675cd1f Iustin Pop
  except errors.ConfigurationError, err:
460 5675cd1f Iustin Pop
    print "Cluster configuration incomplete: '%s'" % str(err)
461 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
462 5675cd1f Iustin Pop
  except errors.ResolverError, err:
463 5675cd1f Iustin Pop
    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
464 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
465 5675cd1f Iustin Pop
466 b33e986b Iustin Pop
  if myself != master_name:
467 5675cd1f Iustin Pop
    if debug:
468 5675cd1f Iustin Pop
      sys.stderr.write("Not master, exiting.\n")
469 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NOTMASTER)
470 dffa96d6 Michael Hanselmann
471 dffa96d6 Michael Hanselmann
472 dffa96d6 Michael Hanselmann
def VerifyClusterName(name, _cfg_location=None):
473 dffa96d6 Michael Hanselmann
  """Verifies cluster name against a local cluster name.
474 dffa96d6 Michael Hanselmann

475 dffa96d6 Michael Hanselmann
  @type name: string
476 dffa96d6 Michael Hanselmann
  @param name: Cluster name
477 dffa96d6 Michael Hanselmann

478 dffa96d6 Michael Hanselmann
  """
479 dffa96d6 Michael Hanselmann
  sstore = SimpleStore(cfg_location=_cfg_location)
480 dffa96d6 Michael Hanselmann
481 dffa96d6 Michael Hanselmann
  try:
482 dffa96d6 Michael Hanselmann
    local_name = sstore.GetClusterName()
483 dffa96d6 Michael Hanselmann
  except errors.ConfigurationError, err:
484 dffa96d6 Michael Hanselmann
    logging.debug("Can't get local cluster name: %s", err)
485 dffa96d6 Michael Hanselmann
  else:
486 dffa96d6 Michael Hanselmann
    if name != local_name:
487 dffa96d6 Michael Hanselmann
      raise errors.GenericError("Current cluster name is '%s'" % local_name)
488 29a32ce5 Michael Hanselmann
489 29a32ce5 Michael Hanselmann
490 29a32ce5 Michael Hanselmann
def VerifyKeys(keys):
491 29a32ce5 Michael Hanselmann
  """Raises an exception if unknown ssconf keys are given.
492 29a32ce5 Michael Hanselmann

493 29a32ce5 Michael Hanselmann
  @type keys: sequence
494 29a32ce5 Michael Hanselmann
  @param keys: Key names to verify
495 29a32ce5 Michael Hanselmann
  @raise errors.GenericError: When invalid keys were found
496 29a32ce5 Michael Hanselmann

497 29a32ce5 Michael Hanselmann
  """
498 29a32ce5 Michael Hanselmann
  invalid = frozenset(keys) - _VALID_KEYS
499 29a32ce5 Michael Hanselmann
  if invalid:
500 29a32ce5 Michael Hanselmann
    raise errors.GenericError("Invalid ssconf keys: %s" %
501 29a32ce5 Michael Hanselmann
                              utils.CommaJoin(sorted(invalid)))