Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ bc57fa8d

History | View | Annotate | Download (12.8 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 790fc19d Klaus Aehlig
  def GetOnlineNodeList(self):
277 790fc19d Klaus Aehlig
    """Return the list of online cluster nodes.
278 790fc19d Klaus Aehlig

279 790fc19d Klaus Aehlig
    """
280 790fc19d Klaus Aehlig
    data = self._ReadFile(constants.SS_ONLINE_NODES)
281 790fc19d Klaus Aehlig
    nl = data.splitlines(False)
282 790fc19d Klaus Aehlig
    return nl
283 790fc19d Klaus Aehlig
284 f9780ccd Luca Bigliardi
  def GetNodePrimaryIPList(self):
285 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' primary IP.
286 f9780ccd Luca Bigliardi

287 f9780ccd Luca Bigliardi
    """
288 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
289 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
290 f9780ccd Luca Bigliardi
    return nl
291 f9780ccd Luca Bigliardi
292 f9780ccd Luca Bigliardi
  def GetNodeSecondaryIPList(self):
293 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' secondary IP.
294 f9780ccd Luca Bigliardi

295 f9780ccd Luca Bigliardi
    """
296 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
297 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
298 f9780ccd Luca Bigliardi
    return nl
299 f9780ccd Luca Bigliardi
300 6f076453 Guido Trotter
  def GetNodegroupList(self):
301 6f076453 Guido Trotter
    """Return the list of nodegroups.
302 6f076453 Guido Trotter

303 6f076453 Guido Trotter
    """
304 6f076453 Guido Trotter
    data = self._ReadFile(constants.SS_NODEGROUPS)
305 6f076453 Guido Trotter
    nl = data.splitlines(False)
306 6f076453 Guido Trotter
    return nl
307 6f076453 Guido Trotter
308 f2bd89b3 Apollon Oikonomopoulos
  def GetNetworkList(self):
309 f2bd89b3 Apollon Oikonomopoulos
    """Return the list of networks.
310 f2bd89b3 Apollon Oikonomopoulos

311 f2bd89b3 Apollon Oikonomopoulos
    """
312 f2bd89b3 Apollon Oikonomopoulos
    data = self._ReadFile(constants.SS_NETWORKS)
313 f2bd89b3 Apollon Oikonomopoulos
    nl = data.splitlines(False)
314 f2bd89b3 Apollon Oikonomopoulos
    return nl
315 f2bd89b3 Apollon Oikonomopoulos
316 25e39bfa Iustin Pop
  def GetClusterTags(self):
317 25e39bfa Iustin Pop
    """Return the cluster tags.
318 25e39bfa Iustin Pop

319 25e39bfa Iustin Pop
    """
320 25e39bfa Iustin Pop
    data = self._ReadFile(constants.SS_CLUSTER_TAGS)
321 25e39bfa Iustin Pop
    nl = data.splitlines(False)
322 25e39bfa Iustin Pop
    return nl
323 25e39bfa Iustin Pop
324 4f7a6a10 Iustin Pop
  def GetHypervisorList(self):
325 4f7a6a10 Iustin Pop
    """Return the list of enabled hypervisors.
326 4f7a6a10 Iustin Pop

327 4f7a6a10 Iustin Pop
    """
328 4f7a6a10 Iustin Pop
    data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
329 4f7a6a10 Iustin Pop
    nl = data.splitlines(False)
330 4f7a6a10 Iustin Pop
    return nl
331 4f7a6a10 Iustin Pop
332 def6577f Helga Velroyen
  def GetHvparamsForHypervisor(self, hvname):
333 def6577f Helga Velroyen
    """Return the hypervisor parameters of the given hypervisor.
334 def6577f Helga Velroyen

335 def6577f Helga Velroyen
    @type hvname: string
336 def6577f Helga Velroyen
    @param hvname: name of the hypervisor, must be in C{constants.HYPER_TYPES}
337 def6577f Helga Velroyen
    @rtype: dict of strings
338 def6577f Helga Velroyen
    @returns: dictionary with hypervisor parameters
339 def6577f Helga Velroyen

340 def6577f Helga Velroyen
    """
341 def6577f Helga Velroyen
    data = self._ReadFile(constants.SS_HVPARAMS_PREF + hvname)
342 def6577f Helga Velroyen
    lines = data.splitlines(False)
343 def6577f Helga Velroyen
    hvparams = {}
344 def6577f Helga Velroyen
    for line in lines:
345 def6577f Helga Velroyen
      (key, value) = line.split("=")
346 def6577f Helga Velroyen
      hvparams[key] = value
347 def6577f Helga Velroyen
    return hvparams
348 def6577f Helga Velroyen
349 def6577f Helga Velroyen
  def GetHvparams(self):
350 def6577f Helga Velroyen
    """Return the hypervisor parameters of all hypervisors.
351 def6577f Helga Velroyen

352 def6577f Helga Velroyen
    @rtype: dict of dict of strings
353 def6577f Helga Velroyen
    @returns: dictionary mapping hypervisor names to hvparams
354 def6577f Helga Velroyen

355 def6577f Helga Velroyen
    """
356 def6577f Helga Velroyen
    all_hvparams = {}
357 def6577f Helga Velroyen
    for hv in constants.HYPER_TYPES:
358 def6577f Helga Velroyen
      all_hvparams[hv] = self.GetHvparamsForHypervisor(hv)
359 def6577f Helga Velroyen
    return all_hvparams
360 def6577f Helga Velroyen
361 5c465a95 Iustin Pop
  def GetMaintainNodeHealth(self):
362 5c465a95 Iustin Pop
    """Return the value of the maintain_node_health option.
363 5c465a95 Iustin Pop

364 5c465a95 Iustin Pop
    """
365 5c465a95 Iustin Pop
    data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
366 5c465a95 Iustin Pop
    # we rely on the bool serialization here
367 5c465a95 Iustin Pop
    return data == "True"
368 5c465a95 Iustin Pop
369 0fbae49a Balazs Lecz
  def GetUidPool(self):
370 0fbae49a Balazs Lecz
    """Return the user-id pool definition string.
371 0fbae49a Balazs Lecz

372 0fbae49a Balazs Lecz
    The separator character is a newline.
373 0fbae49a Balazs Lecz

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

376 0fbae49a Balazs Lecz
      ss = ssconf.SimpleStore()
377 d3b790bb Balazs Lecz
      uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
378 0fbae49a Balazs Lecz

379 0fbae49a Balazs Lecz
    """
380 0fbae49a Balazs Lecz
    data = self._ReadFile(constants.SS_UID_POOL)
381 0fbae49a Balazs Lecz
    return data
382 0fbae49a Balazs Lecz
383 868a98ca Manuel Franceschini
  def GetPrimaryIPFamily(self):
384 868a98ca Manuel Franceschini
    """Return the cluster-wide primary address family.
385 868a98ca Manuel Franceschini

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

398 ee501db1 Michael Hanselmann
  Wrapper around L{SimpleStore.WriteFiles}.
399 ee501db1 Michael Hanselmann

400 ee501db1 Michael Hanselmann
  """
401 cc7f5bfc Michael Hanselmann
  SimpleStore().WriteFiles(values, dry_run=dry_run)
402 ee501db1 Michael Hanselmann
403 ee501db1 Michael Hanselmann
404 b33e986b Iustin Pop
def GetMasterAndMyself(ss=None):
405 b33e986b Iustin Pop
  """Get the master node and my own hostname.
406 b33e986b Iustin Pop

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

410 b33e986b Iustin Pop
  The function does not handle any errors, these should be handled in
411 b33e986b Iustin Pop
  the caller (errors.ConfigurationError, errors.ResolverError).
412 b33e986b Iustin Pop

413 8135a2db Iustin Pop
  @param ss: either a sstore.SimpleConfigReader or a
414 8135a2db Iustin Pop
      sstore.SimpleStore instance
415 8135a2db Iustin Pop
  @rtype: tuple
416 8135a2db Iustin Pop
  @return: a tuple (master node name, my own name)
417 8135a2db Iustin Pop

418 b33e986b Iustin Pop
  """
419 b33e986b Iustin Pop
  if ss is None:
420 93384844 Iustin Pop
    ss = SimpleStore()
421 b705c7a6 Manuel Franceschini
  return ss.GetMasterNode(), netutils.Hostname.GetSysName()
422 b33e986b Iustin Pop
423 06dc5b44 Iustin Pop
424 b33e986b Iustin Pop
def CheckMaster(debug, ss=None):
425 5675cd1f Iustin Pop
  """Checks the node setup.
426 5675cd1f Iustin Pop

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

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

449 dffa96d6 Michael Hanselmann
  @type name: string
450 dffa96d6 Michael Hanselmann
  @param name: Cluster name
451 dffa96d6 Michael Hanselmann

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

467 29a32ce5 Michael Hanselmann
  @type keys: sequence
468 29a32ce5 Michael Hanselmann
  @param keys: Key names to verify
469 29a32ce5 Michael Hanselmann
  @raise errors.GenericError: When invalid keys were found
470 29a32ce5 Michael Hanselmann

471 29a32ce5 Michael Hanselmann
  """
472 29a32ce5 Michael Hanselmann
  invalid = frozenset(keys) - _VALID_KEYS
473 29a32ce5 Michael Hanselmann
  if invalid:
474 29a32ce5 Michael Hanselmann
    raise errors.GenericError("Invalid ssconf keys: %s" %
475 29a32ce5 Michael Hanselmann
                              utils.CommaJoin(sorted(invalid)))