Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ 31d3b918

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 02b31f32 Iustin Pop
        if value and not value.endswith("\n"):
186 89b14f05 Iustin Pop
          value += "\n"
187 cc7f5bfc Michael Hanselmann
188 fb486969 Michael Hanselmann
        if len(value) > _MAX_SIZE:
189 cc7f5bfc Michael Hanselmann
          msg = ("Value '%s' has a length of %s bytes, but only up to %s are"
190 cc7f5bfc Michael Hanselmann
                 " allowed" % (name, len(value), _MAX_SIZE))
191 cc7f5bfc Michael Hanselmann
          raise errors.ConfigurationError(msg)
192 cc7f5bfc Michael Hanselmann
193 cd57bab6 Michael Hanselmann
        utils.WriteFile(self.KeyToFilename(name), data=value,
194 cc7f5bfc Michael Hanselmann
                        mode=constants.SS_FILE_PERMS,
195 cc7f5bfc Michael Hanselmann
                        dry_run=dry_run)
196 89b14f05 Iustin Pop
    finally:
197 89b14f05 Iustin Pop
      ssconf_lock.Unlock()
198 89b14f05 Iustin Pop
199 93384844 Iustin Pop
  def GetFileList(self):
200 93384844 Iustin Pop
    """Return the list of all config files.
201 93384844 Iustin Pop

202 93384844 Iustin Pop
    This is used for computing node replication data.
203 93384844 Iustin Pop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

396 0fbae49a Balazs Lecz
    The separator character is a newline.
397 0fbae49a Balazs Lecz

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

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

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

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

422 ee501db1 Michael Hanselmann
  Wrapper around L{SimpleStore.WriteFiles}.
423 ee501db1 Michael Hanselmann

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

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

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

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

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

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

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

473 dffa96d6 Michael Hanselmann
  @type name: string
474 dffa96d6 Michael Hanselmann
  @param name: Cluster name
475 dffa96d6 Michael Hanselmann

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

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

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