Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ f22433c0

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 a8083063 Iustin Pop
from ganeti import errors
34 a8083063 Iustin Pop
from ganeti import constants
35 41a57aab Michael Hanselmann
from ganeti import utils
36 a744b676 Manuel Franceschini
from ganeti import netutils
37 f45c5c09 Michael Hanselmann
from ganeti import pathutils
38 a8083063 Iustin Pop
39 a8083063 Iustin Pop
40 0c223ea9 Michael Hanselmann
SSCONF_LOCK_TIMEOUT = 10
41 0c223ea9 Michael Hanselmann
42 fb486969 Michael Hanselmann
#: Valid ssconf keys
43 fb486969 Michael Hanselmann
_VALID_KEYS = frozenset([
44 fb486969 Michael Hanselmann
  constants.SS_CLUSTER_NAME,
45 fb486969 Michael Hanselmann
  constants.SS_CLUSTER_TAGS,
46 fb486969 Michael Hanselmann
  constants.SS_FILE_STORAGE_DIR,
47 fb486969 Michael Hanselmann
  constants.SS_SHARED_FILE_STORAGE_DIR,
48 fb486969 Michael Hanselmann
  constants.SS_MASTER_CANDIDATES,
49 fb486969 Michael Hanselmann
  constants.SS_MASTER_CANDIDATES_IPS,
50 fb486969 Michael Hanselmann
  constants.SS_MASTER_IP,
51 fb486969 Michael Hanselmann
  constants.SS_MASTER_NETDEV,
52 fb486969 Michael Hanselmann
  constants.SS_MASTER_NETMASK,
53 fb486969 Michael Hanselmann
  constants.SS_MASTER_NODE,
54 fb486969 Michael Hanselmann
  constants.SS_NODE_LIST,
55 fb486969 Michael Hanselmann
  constants.SS_NODE_PRIMARY_IPS,
56 fb486969 Michael Hanselmann
  constants.SS_NODE_SECONDARY_IPS,
57 fb486969 Michael Hanselmann
  constants.SS_OFFLINE_NODES,
58 fb486969 Michael Hanselmann
  constants.SS_ONLINE_NODES,
59 fb486969 Michael Hanselmann
  constants.SS_PRIMARY_IP_FAMILY,
60 fb486969 Michael Hanselmann
  constants.SS_INSTANCE_LIST,
61 fb486969 Michael Hanselmann
  constants.SS_RELEASE_VERSION,
62 fb486969 Michael Hanselmann
  constants.SS_HYPERVISOR_LIST,
63 fb486969 Michael Hanselmann
  constants.SS_MAINTAIN_NODE_HEALTH,
64 fb486969 Michael Hanselmann
  constants.SS_UID_POOL,
65 fb486969 Michael Hanselmann
  constants.SS_NODEGROUPS,
66 fb486969 Michael Hanselmann
  constants.SS_NETWORKS,
67 fb486969 Michael Hanselmann
  ])
68 fb486969 Michael Hanselmann
69 fb486969 Michael Hanselmann
#: Maximum size for ssconf files
70 fb486969 Michael Hanselmann
_MAX_SIZE = 128 * 1024
71 fb486969 Michael Hanselmann
72 0c223ea9 Michael Hanselmann
73 911dfc49 Michael Hanselmann
def ReadSsconfFile(filename):
74 911dfc49 Michael Hanselmann
  """Reads an ssconf file and verifies its size.
75 911dfc49 Michael Hanselmann

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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