Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ 6b3f0d7e

History | View | Annotate | Download (11.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 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 fb486969 Michael Hanselmann
  ])
69 fb486969 Michael Hanselmann
70 fb486969 Michael Hanselmann
#: Maximum size for ssconf files
71 fb486969 Michael Hanselmann
_MAX_SIZE = 128 * 1024
72 fb486969 Michael Hanselmann
73 0c223ea9 Michael Hanselmann
74 911dfc49 Michael Hanselmann
def ReadSsconfFile(filename):
75 911dfc49 Michael Hanselmann
  """Reads an ssconf file and verifies its size.
76 911dfc49 Michael Hanselmann

77 911dfc49 Michael Hanselmann
  @type filename: string
78 911dfc49 Michael Hanselmann
  @param filename: Path to file
79 911dfc49 Michael Hanselmann
  @rtype: string
80 911dfc49 Michael Hanselmann
  @return: File contents without newlines at the end
81 911dfc49 Michael Hanselmann
  @raise RuntimeError: When the file size exceeds L{_MAX_SIZE}
82 911dfc49 Michael Hanselmann

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

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

103 93384844 Iustin Pop
  Other particularities of the datastore:
104 93384844 Iustin Pop
    - keys are restricted to predefined values
105 93384844 Iustin Pop

106 93384844 Iustin Pop
  """
107 cc7f5bfc Michael Hanselmann
  def __init__(self, cfg_location=None, _lockfile=pathutils.SSCONF_LOCK_FILE):
108 93384844 Iustin Pop
    if cfg_location is None:
109 f45c5c09 Michael Hanselmann
      self._cfg_dir = pathutils.DATA_DIR
110 93384844 Iustin Pop
    else:
111 93384844 Iustin Pop
      self._cfg_dir = cfg_location
112 93384844 Iustin Pop
113 cc7f5bfc Michael Hanselmann
    self._lockfile = _lockfile
114 cc7f5bfc Michael Hanselmann
115 93384844 Iustin Pop
  def KeyToFilename(self, key):
116 93384844 Iustin Pop
    """Convert a given key into filename.
117 93384844 Iustin Pop

118 93384844 Iustin Pop
    """
119 fb486969 Michael Hanselmann
    if key not in _VALID_KEYS:
120 93384844 Iustin Pop
      raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
121 93384844 Iustin Pop
                                   % str(key))
122 93384844 Iustin Pop
123 c09254c2 Iustin Pop
    filename = self._cfg_dir + "/" + constants.SSCONF_FILEPREFIX + key
124 93384844 Iustin Pop
    return filename
125 93384844 Iustin Pop
126 7dd999fc Manuel Franceschini
  def _ReadFile(self, key, default=None):
127 93384844 Iustin Pop
    """Generic routine to read keys.
128 93384844 Iustin Pop

129 93384844 Iustin Pop
    This will read the file which holds the value requested. Errors
130 93384844 Iustin Pop
    will be changed into ConfigurationErrors.
131 93384844 Iustin Pop

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

145 9b4329e9 Michael Hanselmann
    @rtype: dict
146 9b4329e9 Michael Hanselmann
    @return: Dictionary, ssconf key as key, value as value
147 9b4329e9 Michael Hanselmann

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

165 89b14f05 Iustin Pop
    @type values: dict
166 89b14f05 Iustin Pop
    @param values: Dictionary of (name, value)
167 cc7f5bfc Michael Hanselmann
    @type dry_run boolean
168 cc7f5bfc Michael Hanselmann
    @param dry_run: Whether to perform a dry run
169 89b14f05 Iustin Pop

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

194 93384844 Iustin Pop
    This is used for computing node replication data.
195 93384844 Iustin Pop

196 93384844 Iustin Pop
    """
197 fb486969 Michael Hanselmann
    return [self.KeyToFilename(key) for key in _VALID_KEYS]
198 93384844 Iustin Pop
199 93384844 Iustin Pop
  def GetClusterName(self):
200 93384844 Iustin Pop
    """Get the cluster name.
201 93384844 Iustin Pop

202 93384844 Iustin Pop
    """
203 93384844 Iustin Pop
    return self._ReadFile(constants.SS_CLUSTER_NAME)
204 93384844 Iustin Pop
205 93384844 Iustin Pop
  def GetFileStorageDir(self):
206 93384844 Iustin Pop
    """Get the file storage dir.
207 93384844 Iustin Pop

208 93384844 Iustin Pop
    """
209 93384844 Iustin Pop
    return self._ReadFile(constants.SS_FILE_STORAGE_DIR)
210 93384844 Iustin Pop
211 4b97f902 Apollon Oikonomopoulos
  def GetSharedFileStorageDir(self):
212 4b97f902 Apollon Oikonomopoulos
    """Get the shared file storage dir.
213 4b97f902 Apollon Oikonomopoulos

214 4b97f902 Apollon Oikonomopoulos
    """
215 4b97f902 Apollon Oikonomopoulos
    return self._ReadFile(constants.SS_SHARED_FILE_STORAGE_DIR)
216 4b97f902 Apollon Oikonomopoulos
217 f56618e0 Iustin Pop
  def GetMasterCandidates(self):
218 f56618e0 Iustin Pop
    """Return the list of master candidates.
219 f56618e0 Iustin Pop

220 f56618e0 Iustin Pop
    """
221 f56618e0 Iustin Pop
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES)
222 f56618e0 Iustin Pop
    nl = data.splitlines(False)
223 f56618e0 Iustin Pop
    return nl
224 f56618e0 Iustin Pop
225 8113a52e Luca Bigliardi
  def GetMasterCandidatesIPList(self):
226 8113a52e Luca Bigliardi
    """Return the list of master candidates' primary IP.
227 8113a52e Luca Bigliardi

228 8113a52e Luca Bigliardi
    """
229 8113a52e Luca Bigliardi
    data = self._ReadFile(constants.SS_MASTER_CANDIDATES_IPS)
230 8113a52e Luca Bigliardi
    nl = data.splitlines(False)
231 8113a52e Luca Bigliardi
    return nl
232 8113a52e Luca Bigliardi
233 93384844 Iustin Pop
  def GetMasterIP(self):
234 93384844 Iustin Pop
    """Get the IP of the master node for this cluster.
235 93384844 Iustin Pop

236 93384844 Iustin Pop
    """
237 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_IP)
238 93384844 Iustin Pop
239 93384844 Iustin Pop
  def GetMasterNetdev(self):
240 93384844 Iustin Pop
    """Get the netdev to which we'll add the master ip.
241 93384844 Iustin Pop

242 93384844 Iustin Pop
    """
243 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NETDEV)
244 93384844 Iustin Pop
245 5a8648eb Andrea Spadaccini
  def GetMasterNetmask(self):
246 186c03b3 Andrea Spadaccini
    """Get the master netmask.
247 5a8648eb Andrea Spadaccini

248 5a8648eb Andrea Spadaccini
    """
249 186c03b3 Andrea Spadaccini
    try:
250 186c03b3 Andrea Spadaccini
      return self._ReadFile(constants.SS_MASTER_NETMASK)
251 186c03b3 Andrea Spadaccini
    except errors.ConfigurationError:
252 186c03b3 Andrea Spadaccini
      family = self.GetPrimaryIPFamily()
253 186c03b3 Andrea Spadaccini
      ipcls = netutils.IPAddress.GetClassFromIpFamily(family)
254 186c03b3 Andrea Spadaccini
      return ipcls.iplen
255 5a8648eb Andrea Spadaccini
256 93384844 Iustin Pop
  def GetMasterNode(self):
257 93384844 Iustin Pop
    """Get the hostname of the master node for this cluster.
258 93384844 Iustin Pop

259 93384844 Iustin Pop
    """
260 93384844 Iustin Pop
    return self._ReadFile(constants.SS_MASTER_NODE)
261 93384844 Iustin Pop
262 93384844 Iustin Pop
  def GetNodeList(self):
263 93384844 Iustin Pop
    """Return the list of cluster nodes.
264 93384844 Iustin Pop

265 93384844 Iustin Pop
    """
266 93384844 Iustin Pop
    data = self._ReadFile(constants.SS_NODE_LIST)
267 93384844 Iustin Pop
    nl = data.splitlines(False)
268 93384844 Iustin Pop
    return nl
269 93384844 Iustin Pop
270 f9780ccd Luca Bigliardi
  def GetNodePrimaryIPList(self):
271 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' primary IP.
272 f9780ccd Luca Bigliardi

273 f9780ccd Luca Bigliardi
    """
274 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_PRIMARY_IPS)
275 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
276 f9780ccd Luca Bigliardi
    return nl
277 f9780ccd Luca Bigliardi
278 f9780ccd Luca Bigliardi
  def GetNodeSecondaryIPList(self):
279 f9780ccd Luca Bigliardi
    """Return the list of cluster nodes' secondary IP.
280 f9780ccd Luca Bigliardi

281 f9780ccd Luca Bigliardi
    """
282 f9780ccd Luca Bigliardi
    data = self._ReadFile(constants.SS_NODE_SECONDARY_IPS)
283 f9780ccd Luca Bigliardi
    nl = data.splitlines(False)
284 f9780ccd Luca Bigliardi
    return nl
285 f9780ccd Luca Bigliardi
286 6f076453 Guido Trotter
  def GetNodegroupList(self):
287 6f076453 Guido Trotter
    """Return the list of nodegroups.
288 6f076453 Guido Trotter

289 6f076453 Guido Trotter
    """
290 6f076453 Guido Trotter
    data = self._ReadFile(constants.SS_NODEGROUPS)
291 6f076453 Guido Trotter
    nl = data.splitlines(False)
292 6f076453 Guido Trotter
    return nl
293 6f076453 Guido Trotter
294 f2bd89b3 Apollon Oikonomopoulos
  def GetNetworkList(self):
295 f2bd89b3 Apollon Oikonomopoulos
    """Return the list of networks.
296 f2bd89b3 Apollon Oikonomopoulos

297 f2bd89b3 Apollon Oikonomopoulos
    """
298 f2bd89b3 Apollon Oikonomopoulos
    data = self._ReadFile(constants.SS_NETWORKS)
299 f2bd89b3 Apollon Oikonomopoulos
    nl = data.splitlines(False)
300 f2bd89b3 Apollon Oikonomopoulos
    return nl
301 f2bd89b3 Apollon Oikonomopoulos
302 25e39bfa Iustin Pop
  def GetClusterTags(self):
303 25e39bfa Iustin Pop
    """Return the cluster tags.
304 25e39bfa Iustin Pop

305 25e39bfa Iustin Pop
    """
306 25e39bfa Iustin Pop
    data = self._ReadFile(constants.SS_CLUSTER_TAGS)
307 25e39bfa Iustin Pop
    nl = data.splitlines(False)
308 25e39bfa Iustin Pop
    return nl
309 25e39bfa Iustin Pop
310 4f7a6a10 Iustin Pop
  def GetHypervisorList(self):
311 4f7a6a10 Iustin Pop
    """Return the list of enabled hypervisors.
312 4f7a6a10 Iustin Pop

313 4f7a6a10 Iustin Pop
    """
314 4f7a6a10 Iustin Pop
    data = self._ReadFile(constants.SS_HYPERVISOR_LIST)
315 4f7a6a10 Iustin Pop
    nl = data.splitlines(False)
316 4f7a6a10 Iustin Pop
    return nl
317 4f7a6a10 Iustin Pop
318 5c465a95 Iustin Pop
  def GetMaintainNodeHealth(self):
319 5c465a95 Iustin Pop
    """Return the value of the maintain_node_health option.
320 5c465a95 Iustin Pop

321 5c465a95 Iustin Pop
    """
322 5c465a95 Iustin Pop
    data = self._ReadFile(constants.SS_MAINTAIN_NODE_HEALTH)
323 5c465a95 Iustin Pop
    # we rely on the bool serialization here
324 5c465a95 Iustin Pop
    return data == "True"
325 5c465a95 Iustin Pop
326 0fbae49a Balazs Lecz
  def GetUidPool(self):
327 0fbae49a Balazs Lecz
    """Return the user-id pool definition string.
328 0fbae49a Balazs Lecz

329 0fbae49a Balazs Lecz
    The separator character is a newline.
330 0fbae49a Balazs Lecz

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

333 0fbae49a Balazs Lecz
      ss = ssconf.SimpleStore()
334 d3b790bb Balazs Lecz
      uid_pool = uidpool.ParseUidPool(ss.GetUidPool(), separator="\\n")
335 0fbae49a Balazs Lecz

336 0fbae49a Balazs Lecz
    """
337 0fbae49a Balazs Lecz
    data = self._ReadFile(constants.SS_UID_POOL)
338 0fbae49a Balazs Lecz
    return data
339 0fbae49a Balazs Lecz
340 868a98ca Manuel Franceschini
  def GetPrimaryIPFamily(self):
341 868a98ca Manuel Franceschini
    """Return the cluster-wide primary address family.
342 868a98ca Manuel Franceschini

343 868a98ca Manuel Franceschini
    """
344 868a98ca Manuel Franceschini
    try:
345 7dd999fc Manuel Franceschini
      return int(self._ReadFile(constants.SS_PRIMARY_IP_FAMILY,
346 7dd999fc Manuel Franceschini
                                default=netutils.IP4Address.family))
347 868a98ca Manuel Franceschini
    except (ValueError, TypeError), err:
348 c5ac51bb Michael Hanselmann
      raise errors.ConfigurationError("Error while trying to parse primary IP"
349 868a98ca Manuel Franceschini
                                      " family: %s" % err)
350 868a98ca Manuel Franceschini
351 93384844 Iustin Pop
352 cc7f5bfc Michael Hanselmann
def WriteSsconfFiles(values, dry_run=False):
353 ee501db1 Michael Hanselmann
  """Update all ssconf files.
354 ee501db1 Michael Hanselmann

355 ee501db1 Michael Hanselmann
  Wrapper around L{SimpleStore.WriteFiles}.
356 ee501db1 Michael Hanselmann

357 ee501db1 Michael Hanselmann
  """
358 cc7f5bfc Michael Hanselmann
  SimpleStore().WriteFiles(values, dry_run=dry_run)
359 ee501db1 Michael Hanselmann
360 ee501db1 Michael Hanselmann
361 b33e986b Iustin Pop
def GetMasterAndMyself(ss=None):
362 b33e986b Iustin Pop
  """Get the master node and my own hostname.
363 b33e986b Iustin Pop

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

367 b33e986b Iustin Pop
  The function does not handle any errors, these should be handled in
368 b33e986b Iustin Pop
  the caller (errors.ConfigurationError, errors.ResolverError).
369 b33e986b Iustin Pop

370 8135a2db Iustin Pop
  @param ss: either a sstore.SimpleConfigReader or a
371 8135a2db Iustin Pop
      sstore.SimpleStore instance
372 8135a2db Iustin Pop
  @rtype: tuple
373 8135a2db Iustin Pop
  @return: a tuple (master node name, my own name)
374 8135a2db Iustin Pop

375 b33e986b Iustin Pop
  """
376 b33e986b Iustin Pop
  if ss is None:
377 93384844 Iustin Pop
    ss = SimpleStore()
378 b705c7a6 Manuel Franceschini
  return ss.GetMasterNode(), netutils.Hostname.GetSysName()
379 b33e986b Iustin Pop
380 06dc5b44 Iustin Pop
381 b33e986b Iustin Pop
def CheckMaster(debug, ss=None):
382 5675cd1f Iustin Pop
  """Checks the node setup.
383 5675cd1f Iustin Pop

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

387 5675cd1f Iustin Pop
  """
388 5675cd1f Iustin Pop
  try:
389 b33e986b Iustin Pop
    master_name, myself = GetMasterAndMyself(ss)
390 5675cd1f Iustin Pop
  except errors.ConfigurationError, err:
391 5675cd1f Iustin Pop
    print "Cluster configuration incomplete: '%s'" % str(err)
392 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
393 5675cd1f Iustin Pop
  except errors.ResolverError, err:
394 5675cd1f Iustin Pop
    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
395 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
396 5675cd1f Iustin Pop
397 b33e986b Iustin Pop
  if myself != master_name:
398 5675cd1f Iustin Pop
    if debug:
399 5675cd1f Iustin Pop
      sys.stderr.write("Not master, exiting.\n")
400 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NOTMASTER)
401 dffa96d6 Michael Hanselmann
402 dffa96d6 Michael Hanselmann
403 dffa96d6 Michael Hanselmann
def VerifyClusterName(name, _cfg_location=None):
404 dffa96d6 Michael Hanselmann
  """Verifies cluster name against a local cluster name.
405 dffa96d6 Michael Hanselmann

406 dffa96d6 Michael Hanselmann
  @type name: string
407 dffa96d6 Michael Hanselmann
  @param name: Cluster name
408 dffa96d6 Michael Hanselmann

409 dffa96d6 Michael Hanselmann
  """
410 dffa96d6 Michael Hanselmann
  sstore = SimpleStore(cfg_location=_cfg_location)
411 dffa96d6 Michael Hanselmann
412 dffa96d6 Michael Hanselmann
  try:
413 dffa96d6 Michael Hanselmann
    local_name = sstore.GetClusterName()
414 dffa96d6 Michael Hanselmann
  except errors.ConfigurationError, err:
415 dffa96d6 Michael Hanselmann
    logging.debug("Can't get local cluster name: %s", err)
416 dffa96d6 Michael Hanselmann
  else:
417 dffa96d6 Michael Hanselmann
    if name != local_name:
418 dffa96d6 Michael Hanselmann
      raise errors.GenericError("Current cluster name is '%s'" % local_name)
419 29a32ce5 Michael Hanselmann
420 29a32ce5 Michael Hanselmann
421 29a32ce5 Michael Hanselmann
def VerifyKeys(keys):
422 29a32ce5 Michael Hanselmann
  """Raises an exception if unknown ssconf keys are given.
423 29a32ce5 Michael Hanselmann

424 29a32ce5 Michael Hanselmann
  @type keys: sequence
425 29a32ce5 Michael Hanselmann
  @param keys: Key names to verify
426 29a32ce5 Michael Hanselmann
  @raise errors.GenericError: When invalid keys were found
427 29a32ce5 Michael Hanselmann

428 29a32ce5 Michael Hanselmann
  """
429 29a32ce5 Michael Hanselmann
  invalid = frozenset(keys) - _VALID_KEYS
430 29a32ce5 Michael Hanselmann
  if invalid:
431 29a32ce5 Michael Hanselmann
    raise errors.GenericError("Invalid ssconf keys: %s" %
432 29a32ce5 Michael Hanselmann
                              utils.CommaJoin(sorted(invalid)))