Package ganeti :: Module runtime
[hide private]
[frames] | no frames]

Source Code for Module ganeti.runtime

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2010 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """Module implementing configuration details at runtime. 
 31   
 32  """ 
 33   
 34   
 35  import grp 
 36  import pwd 
 37  import threading 
 38  import platform 
 39   
 40  from ganeti import constants 
 41  from ganeti import errors 
 42  from ganeti import utils 
 43   
 44   
 45  _priv = None 
 46  _priv_lock = threading.Lock() 
 47   
 48  #: Architecture information 
 49  _arch = None 
 50   
 51   
52 -def GetUid(user, _getpwnam):
53 """Retrieve the uid from the database. 54 55 @type user: string 56 @param user: The username to retrieve 57 @return: The resolved uid 58 59 """ 60 try: 61 return _getpwnam(user).pw_uid 62 except KeyError, err: 63 raise errors.ConfigurationError("User '%s' not found (%s)" % (user, err))
64 65
66 -def GetGid(group, _getgrnam):
67 """Retrieve the gid from the database. 68 69 @type group: string 70 @param group: The group name to retrieve 71 @return: The resolved gid 72 73 """ 74 try: 75 return _getgrnam(group).gr_gid 76 except KeyError, err: 77 raise errors.ConfigurationError("Group '%s' not found (%s)" % (group, err))
78 79
80 -class GetentResolver(object):
81 """Resolves Ganeti uids and gids by name. 82 83 @ivar masterd_uid: The resolved uid of the masterd user 84 @ivar masterd_gid: The resolved gid of the masterd group 85 @ivar confd_uid: The resolved uid of the confd user 86 @ivar confd_gid: The resolved gid of the confd group 87 @ivar luxid_uid: The resolved uid of the luxid user 88 @ivar luxid_gid: The resolved gid of the luxid group 89 @ivar rapi_uid: The resolved uid of the rapi user 90 @ivar rapi_gid: The resolved gid of the rapi group 91 @ivar noded_uid: The resolved uid of the noded user 92 @ivar daemons_gid: The resolved gid of the daemons group 93 @ivar admin_gid: The resolved gid of the admin group 94 95 """
96 - def __init__(self, _getpwnam=pwd.getpwnam, _getgrnam=grp.getgrnam):
97 """Initialize the resolver. 98 99 """ 100 # Daemon pairs 101 self.masterd_uid = GetUid(constants.MASTERD_USER, _getpwnam) 102 self.masterd_gid = GetGid(constants.MASTERD_GROUP, _getgrnam) 103 104 self.confd_uid = GetUid(constants.CONFD_USER, _getpwnam) 105 self.confd_gid = GetGid(constants.CONFD_GROUP, _getgrnam) 106 107 self.luxid_uid = GetUid(constants.LUXID_USER, _getpwnam) 108 self.luxid_gid = GetGid(constants.LUXID_GROUP, _getgrnam) 109 110 self.rapi_uid = GetUid(constants.RAPI_USER, _getpwnam) 111 self.rapi_gid = GetGid(constants.RAPI_GROUP, _getgrnam) 112 113 self.noded_uid = GetUid(constants.NODED_USER, _getpwnam) 114 self.noded_gid = GetGid(constants.NODED_GROUP, _getgrnam) 115 116 self.mond_uid = GetUid(constants.MOND_USER, _getpwnam) 117 self.mond_gid = GetGid(constants.MOND_GROUP, _getgrnam) 118 119 # Misc Ganeti groups 120 self.daemons_gid = GetGid(constants.DAEMONS_GROUP, _getgrnam) 121 self.admin_gid = GetGid(constants.ADMIN_GROUP, _getgrnam) 122 123 self._uid2user = { 124 self.masterd_uid: constants.MASTERD_USER, 125 self.confd_uid: constants.CONFD_USER, 126 self.luxid_uid: constants.LUXID_USER, 127 self.rapi_uid: constants.RAPI_USER, 128 self.noded_uid: constants.NODED_USER, 129 self.mond_uid: constants.MOND_USER, 130 } 131 132 self._gid2group = { 133 self.masterd_gid: constants.MASTERD_GROUP, 134 self.confd_gid: constants.CONFD_GROUP, 135 self.luxid_gid: constants.LUXID_GROUP, 136 self.rapi_gid: constants.RAPI_GROUP, 137 self.noded_gid: constants.NODED_GROUP, 138 self.mond_gid: constants.MOND_GROUP, 139 self.daemons_gid: constants.DAEMONS_GROUP, 140 self.admin_gid: constants.ADMIN_GROUP, 141 } 142 143 self._user2uid = utils.InvertDict(self._uid2user) 144 self._group2gid = utils.InvertDict(self._gid2group)
145
146 - def LookupUid(self, uid):
147 """Looks which Ganeti user belongs to this uid. 148 149 @param uid: The uid to lookup 150 @returns The user name associated with that uid 151 152 """ 153 try: 154 return self._uid2user[uid] 155 except KeyError: 156 raise errors.ConfigurationError("Unknown Ganeti uid '%d'" % uid)
157
158 - def LookupGid(self, gid):
159 """Looks which Ganeti group belongs to this gid. 160 161 @param gid: The gid to lookup 162 @returns The group name associated with that gid 163 164 """ 165 try: 166 return self._gid2group[gid] 167 except KeyError: 168 raise errors.ConfigurationError("Unknown Ganeti gid '%d'" % gid)
169
170 - def LookupUser(self, name):
171 """Looks which uid belongs to this name. 172 173 @param name: The name to lookup 174 @returns The uid associated with that user name 175 176 """ 177 try: 178 return self._user2uid[name] 179 except KeyError: 180 raise errors.ConfigurationError("Unknown Ganeti user '%s'" % name)
181
182 - def LookupGroup(self, name):
183 """Looks which gid belongs to this name. 184 185 @param name: The name to lookup 186 @returns The gid associated with that group name 187 188 """ 189 try: 190 return self._group2gid[name] 191 except KeyError: 192 raise errors.ConfigurationError("Unknown Ganeti group '%s'" % name)
193 194
195 -def GetEnts(resolver=GetentResolver):
196 """Singleton wrapper around resolver instance. 197 198 As this method is accessed by multiple threads at the same time 199 we need to take thread-safety carefully. 200 201 """ 202 # We need to use the global keyword here 203 global _priv # pylint: disable=W0603 204 205 if not _priv: 206 _priv_lock.acquire() 207 try: 208 if not _priv: 209 # W0621: Redefine '_priv' from outer scope (used for singleton) 210 _priv = resolver() # pylint: disable=W0621 211 finally: 212 _priv_lock.release() 213 214 return _priv
215 216
217 -def InitArchInfo():
218 """Initialize architecture information. 219 220 We can assume this information never changes during the lifetime of a 221 process, therefore the information can easily be cached. 222 223 @note: This function uses C{platform.architecture} to retrieve the Python 224 binary architecture and does so by forking to run C{file} (see Python 225 documentation for more information). Therefore it must not be used in a 226 multi-threaded environment. 227 228 """ 229 global _arch # pylint: disable=W0603 230 231 if _arch is not None: 232 raise errors.ProgrammerError("Architecture information can only be" 233 " initialized once") 234 235 _arch = (platform.architecture()[0], platform.machine())
236 237
238 -def GetArchInfo():
239 """Returns previsouly initialized architecture information. 240 241 """ 242 if _arch is None: 243 raise errors.ProgrammerError("Architecture information hasn't been" 244 " initialized") 245 246 return _arch
247