Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ ac156ecd

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

241 8113a52e Luca Bigliardi
    """
242 8113a52e Luca Bigliardi
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
243 8113a52e Luca Bigliardi
    nl = data.splitlines(False)
244 8113a52e Luca Bigliardi
    return nl
245 8113a52e Luca Bigliardi
246 93384844 Iustin Pop
  def GetMasterIP(self):
247 93384844 Iustin Pop
    """Get the IP of the master node for this cluster.
248 93384844 Iustin Pop

249 93384844 Iustin Pop
    """
250 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_IP)
251 93384844 Iustin Pop
252 93384844 Iustin Pop
  def GetMasterNetdev(self):
253 93384844 Iustin Pop
    """Get the netdev to which we'll add the master ip.
254 93384844 Iustin Pop

255 93384844 Iustin Pop
    """
256 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NETDEV)
257 93384844 Iustin Pop
258 5a8648eb Andrea Spadaccini
  def GetMasterNetmask(self):
259 186c03b3 Andrea Spadaccini
    """Get the master netmask.
260 5a8648eb Andrea Spadaccini

261 5a8648eb Andrea Spadaccini
    """
262 186c03b3 Andrea Spadaccini
    try:
263 186c03b3 Andrea Spadaccini
      return self._ReadFile(constants.SS_MASTER_NETMASK)
264 186c03b3 Andrea Spadaccini
    except errors.ConfigurationError:
265 186c03b3 Andrea Spadaccini
      family = self.GetPrimaryIPFamily()
266 186c03b3 Andrea Spadaccini
      ipcls = netutils.IPAddress.GetClassFromIpFamily(family)
267 186c03b3 Andrea Spadaccini
      return ipcls.iplen
268 5a8648eb Andrea Spadaccini
269 93384844 Iustin Pop
  def GetMasterNode(self):
270 93384844 Iustin Pop
    """Get the hostname of the master node for this cluster.
271 93384844 Iustin Pop

272 93384844 Iustin Pop
    """
273 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NODE)
274 93384844 Iustin Pop
275 93384844 Iustin Pop
  def GetNodeList(self):
276 93384844 Iustin Pop
    """Return the list of cluster nodes.
277 93384844 Iustin Pop

278 93384844 Iustin Pop
    """
279 93384844 Iustin Pop
    data = self._ReadFile(constants.SS_NODE_LIST)
280 93384844 Iustin Pop
    nl = data.splitlines(False)
281 93384844 Iustin Pop
    return nl
282 93384844 Iustin Pop
283 790fc19d Klaus Aehlig
  def GetOnlineNodeList(self):
284 790fc19d Klaus Aehlig
    """Return the list of online cluster nodes.
285 790fc19d Klaus Aehlig

286 790fc19d Klaus Aehlig
    """
287 790fc19d Klaus Aehlig
    data = self._ReadFile(constants.SS_ONLINE_NODES)
288 790fc19d Klaus Aehlig
    nl = data.splitlines(False)
289 790fc19d Klaus Aehlig
    return nl
290 790fc19d Klaus Aehlig
291 f9780ccd Luca Bigliardi
  def GetNodePrimaryIPList(self):
292 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' primary IP.
293 f9780ccd Luca Bigliardi

294 f9780ccd Luca Bigliardi
    """
295 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
296 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
297 f9780ccd Luca Bigliardi
    return nl
298 f9780ccd Luca Bigliardi
299 f9780ccd Luca Bigliardi
  def GetNodeSecondaryIPList(self):
300 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' secondary IP.
301 f9780ccd Luca Bigliardi

302 f9780ccd Luca Bigliardi
    """
303 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
304 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
305 f9780ccd Luca Bigliardi
    return nl
306 f9780ccd Luca Bigliardi
307 6f076453 Guido Trotter
  def GetNodegroupList(self):
308 6f076453 Guido Trotter
    """Return the list of nodegroups.
309 6f076453 Guido Trotter

310 6f076453 Guido Trotter
    """
311 6f076453 Guido Trotter
    data = self._ReadFile(constants.SS_NODEGROUPS)
312 6f076453 Guido Trotter
    nl = data.splitlines(False)
313 6f076453 Guido Trotter
    return nl
314 6f076453 Guido Trotter
315 f2bd89b3 Apollon Oikonomopoulos
  def GetNetworkList(self):
316 f2bd89b3 Apollon Oikonomopoulos
    """Return the list of networks.
317 f2bd89b3 Apollon Oikonomopoulos

318 f2bd89b3 Apollon Oikonomopoulos
    """
319 f2bd89b3 Apollon Oikonomopoulos
    data = self._ReadFile(constants.SS_NETWORKS)
320 f2bd89b3 Apollon Oikonomopoulos
    nl = data.splitlines(False)
321 f2bd89b3 Apollon Oikonomopoulos
    return nl
322 f2bd89b3 Apollon Oikonomopoulos
323 25e39bfa Iustin Pop
  def GetClusterTags(self):
324 25e39bfa Iustin Pop
    """Return the cluster tags.
325 25e39bfa Iustin Pop

326 25e39bfa Iustin Pop
    """
327 25e39bfa Iustin Pop
    data = self._ReadFile(constants.SS_CLUSTER_TAGS)
328 25e39bfa Iustin Pop
    nl = data.splitlines(False)
329 25e39bfa Iustin Pop
    return nl
330 25e39bfa Iustin Pop
331 4f7a6a10 Iustin Pop
  def GetHypervisorList(self):
332 4f7a6a10 Iustin Pop
    """Return the list of enabled hypervisors.
333 4f7a6a10 Iustin Pop

334 4f7a6a10 Iustin Pop
    """
335 4f7a6a10 Iustin Pop
    data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
336 4f7a6a10 Iustin Pop
    nl = data.splitlines(False)
337 4f7a6a10 Iustin Pop
    return nl
338 4f7a6a10 Iustin Pop
339 def6577f Helga Velroyen
  def GetHvparamsForHypervisor(self, hvname):
340 def6577f Helga Velroyen
    """Return the hypervisor parameters of the given hypervisor.
341 def6577f Helga Velroyen

342 def6577f Helga Velroyen
    @type hvname: string
343 def6577f Helga Velroyen
    @param hvname: name of the hypervisor, must be in C{constants.HYPER_TYPES}
344 def6577f Helga Velroyen
    @rtype: dict of strings
345 def6577f Helga Velroyen
    @returns: dictionary with hypervisor parameters
346 def6577f Helga Velroyen

347 def6577f Helga Velroyen
    """
348 def6577f Helga Velroyen
    data = self._ReadFile(constants.SS_HVPARAMS_PREF + hvname)
349 def6577f Helga Velroyen
    lines = data.splitlines(False)
350 def6577f Helga Velroyen
    hvparams = {}
351 def6577f Helga Velroyen
    for line in lines:
352 def6577f Helga Velroyen
      (key, value) = line.split("=")
353 def6577f Helga Velroyen
      hvparams[key] = value
354 def6577f Helga Velroyen
    return hvparams
355 def6577f Helga Velroyen
356 def6577f Helga Velroyen
  def GetHvparams(self):
357 def6577f Helga Velroyen
    """Return the hypervisor parameters of all hypervisors.
358 def6577f Helga Velroyen

359 def6577f Helga Velroyen
    @rtype: dict of dict of strings
360 def6577f Helga Velroyen
    @returns: dictionary mapping hypervisor names to hvparams
361 def6577f Helga Velroyen

362 def6577f Helga Velroyen
    """
363 def6577f Helga Velroyen
    all_hvparams = {}
364 def6577f Helga Velroyen
    for hv in constants.HYPER_TYPES:
365 def6577f Helga Velroyen
      all_hvparams[hv] = self.GetHvparamsForHypervisor(hv)
366 def6577f Helga Velroyen
    return all_hvparams
367 def6577f Helga Velroyen
368 5c465a95 Iustin Pop
  def GetMaintainNodeHealth(self):
369 5c465a95 Iustin Pop
    """Return the value of the maintain_node_health option.
370 5c465a95 Iustin Pop

371 5c465a95 Iustin Pop
    """
372 5c465a95 Iustin Pop
    data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
373 5c465a95 Iustin Pop
    # we rely on the bool serialization here
374 5c465a95 Iustin Pop
    return data == "True"
375 5c465a95 Iustin Pop
376 0fbae49a Balazs Lecz
  def GetUidPool(self):
377 0fbae49a Balazs Lecz
    """Return the user-id pool definition string.
378 0fbae49a Balazs Lecz

379 0fbae49a Balazs Lecz
    The separator character is a newline.
380 0fbae49a Balazs Lecz

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

383 0fbae49a Balazs Lecz
      ss = ssconf.SimpleStore()
384 d3b790bb Balazs Lecz
      uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
385 0fbae49a Balazs Lecz

386 0fbae49a Balazs Lecz
    """
387 0fbae49a Balazs Lecz
    data = self._ReadFile(constants.SS_UID_POOL)
388 0fbae49a Balazs Lecz
    return data
389 0fbae49a Balazs Lecz
390 868a98ca Manuel Franceschini
  def GetPrimaryIPFamily(self):
391 868a98ca Manuel Franceschini
    """Return the cluster-wide primary address family.
392 868a98ca Manuel Franceschini

393 868a98ca Manuel Franceschini
    """
394 868a98ca Manuel Franceschini
    try:
395 7dd999fc Manuel Franceschini
      return int(self._ReadFile(constants.SS_PRIMARY_IP_FAMILY,
396 7dd999fc Manuel Franceschini
                                default=netutils.IP4Address.family))
397 868a98ca Manuel Franceschini
    except (ValueError, TypeError), err:
398 c5ac51bb Michael Hanselmann
      raise errors.ConfigurationError("Error while trying to parse primary IP"
399 868a98ca Manuel Franceschini
                                      " family: %s" % err)
400 868a98ca Manuel Franceschini
401 93384844 Iustin Pop
402 cc7f5bfc Michael Hanselmann
def WriteSsconfFiles(values, dry_run=False):
403 ee501db1 Michael Hanselmann
  """Update all ssconf files.
404 ee501db1 Michael Hanselmann

405 ee501db1 Michael Hanselmann
  Wrapper around L{SimpleStore.WriteFiles}.
406 ee501db1 Michael Hanselmann

407 ee501db1 Michael Hanselmann
  """
408 cc7f5bfc Michael Hanselmann
  SimpleStore().WriteFiles(values, dry_run=dry_run)
409 ee501db1 Michael Hanselmann
410 ee501db1 Michael Hanselmann
411 b33e986b Iustin Pop
def GetMasterAndMyself(ss=None):
412 b33e986b Iustin Pop
  """Get the master node and my own hostname.
413 b33e986b Iustin Pop

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

417 b33e986b Iustin Pop
  The function does not handle any errors, these should be handled in
418 b33e986b Iustin Pop
  the caller (errors.ConfigurationError, errors.ResolverError).
419 b33e986b Iustin Pop

420 8135a2db Iustin Pop
  @param ss: either a sstore.SimpleConfigReader or a
421 8135a2db Iustin Pop
      sstore.SimpleStore instance
422 8135a2db Iustin Pop
  @rtype: tuple
423 8135a2db Iustin Pop
  @return: a tuple (master node name, my own name)
424 8135a2db Iustin Pop

425 b33e986b Iustin Pop
  """
426 b33e986b Iustin Pop
  if ss is None:
427 93384844 Iustin Pop
    ss = SimpleStore()
428 b705c7a6 Manuel Franceschini
  return ss.GetMasterNode(), netutils.Hostname.GetSysName()
429 b33e986b Iustin Pop
430 06dc5b44 Iustin Pop
431 b33e986b Iustin Pop
def CheckMaster(debug, ss=None):
432 5675cd1f Iustin Pop
  """Checks the node setup.
433 5675cd1f Iustin Pop

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

437 5675cd1f Iustin Pop
  """
438 5675cd1f Iustin Pop
  try:
439 b33e986b Iustin Pop
    master_name, myself = GetMasterAndMyself(ss)
440 5675cd1f Iustin Pop
  except errors.ConfigurationError, err:
441 5675cd1f Iustin Pop
    print "Cluster configuration incomplete: '%s'" % str(err)
442 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
443 5675cd1f Iustin Pop
  except errors.ResolverError, err:
444 5675cd1f Iustin Pop
    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
445 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
446 5675cd1f Iustin Pop
447 b33e986b Iustin Pop
  if myself != master_name:
448 5675cd1f Iustin Pop
    if debug:
449 5675cd1f Iustin Pop
      sys.stderr.write("Not master, exiting.\n")
450 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NOTMASTER)
451 dffa96d6 Michael Hanselmann
452 dffa96d6 Michael Hanselmann
453 dffa96d6 Michael Hanselmann
def VerifyClusterName(name, _cfg_location=None):
454 dffa96d6 Michael Hanselmann
  """Verifies cluster name against a local cluster name.
455 dffa96d6 Michael Hanselmann

456 dffa96d6 Michael Hanselmann
  @type name: string
457 dffa96d6 Michael Hanselmann
  @param name: Cluster name
458 dffa96d6 Michael Hanselmann

459 dffa96d6 Michael Hanselmann
  """
460 dffa96d6 Michael Hanselmann
  sstore = SimpleStore(cfg_location=_cfg_location)
461 dffa96d6 Michael Hanselmann
462 dffa96d6 Michael Hanselmann
  try:
463 dffa96d6 Michael Hanselmann
    local_name = sstore.GetClusterName()
464 dffa96d6 Michael Hanselmann
  except errors.ConfigurationError, err:
465 dffa96d6 Michael Hanselmann
    logging.debug("Can't get local cluster name: %s", err)
466 dffa96d6 Michael Hanselmann
  else:
467 dffa96d6 Michael Hanselmann
    if name != local_name:
468 dffa96d6 Michael Hanselmann
      raise errors.GenericError("Current cluster name is '%s'" % local_name)
469 29a32ce5 Michael Hanselmann
470 29a32ce5 Michael Hanselmann
471 29a32ce5 Michael Hanselmann
def VerifyKeys(keys):
472 29a32ce5 Michael Hanselmann
  """Raises an exception if unknown ssconf keys are given.
473 29a32ce5 Michael Hanselmann

474 29a32ce5 Michael Hanselmann
  @type keys: sequence
475 29a32ce5 Michael Hanselmann
  @param keys: Key names to verify
476 29a32ce5 Michael Hanselmann
  @raise errors.GenericError: When invalid keys were found
477 29a32ce5 Michael Hanselmann

478 29a32ce5 Michael Hanselmann
  """
479 29a32ce5 Michael Hanselmann
  invalid = frozenset(keys) - _VALID_KEYS
480 29a32ce5 Michael Hanselmann
  if invalid:
481 29a32ce5 Michael Hanselmann
    raise errors.GenericError("Invalid ssconf keys: %s" %
482 29a32ce5 Michael Hanselmann
                              utils.CommaJoin(sorted(invalid)))