3 # Copyright (C) 2010 Google Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 """Module implementing configuration details at runtime.
29 from ganeti import constants
30 from ganeti import errors
34 _priv_lock = threading.Lock()
37 def GetUid(user, _getpwnam):
38 """Retrieve the uid from the database.
41 @param user: The username to retrieve
42 @return: The resolved uid
46 return _getpwnam(user).pw_uid
48 raise errors.ConfigurationError("User '%s' not found (%s)" % (user, err))
51 def GetGid(group, _getgrnam):
52 """Retrieve the gid from the database.
55 @param group: The group name to retrieve
56 @return: The resolved gid
60 return _getgrnam(group).gr_gid
62 raise errors.ConfigurationError("Group '%s' not found (%s)" % (group, err))
66 """Resolves Ganeti uids and gids by name.
68 @ivar masterd_uid: The resolved uid of the masterd user
69 @ivar masterd_gid: The resolved gid of the masterd group
70 @ivar confd_uid: The resolved uid of the confd user
71 @ivar confd_gid: The resolved gid of the confd group
72 @ivar rapi_uid: The resolved uid of the rapi user
73 @ivar rapi_gid: The resolved gid of the rapi group
74 @ivar noded_uid: The resolved uid of the noded user
76 @ivar daemons_gid: The resolved gid of the daemons group
77 @ivar admin_gid: The resolved gid of the admin group
79 def __init__(self, _getpwnam=pwd.getpwnam, _getgrnam=grp.getgrnam):
80 """Initialize the resolver.
84 self.masterd_uid = GetUid(constants.MASTERD_USER, _getpwnam)
85 self.masterd_gid = GetGid(constants.MASTERD_GROUP, _getgrnam)
87 self.confd_uid = GetUid(constants.CONFD_USER, _getpwnam)
88 self.confd_gid = GetGid(constants.CONFD_GROUP, _getgrnam)
90 self.rapi_uid = GetUid(constants.RAPI_USER, _getpwnam)
91 self.rapi_gid = GetGid(constants.RAPI_GROUP, _getgrnam)
93 self.noded_uid = GetUid(constants.NODED_USER, _getpwnam)
96 self.daemons_gid = GetGid(constants.DAEMONS_GROUP, _getgrnam)
97 self.admin_gid = GetGid(constants.ADMIN_GROUP, _getgrnam)
100 def GetEnts(resolver=GetentResolver):
101 """Singleton wrapper around resolver instance.
103 As this method is accessed by multiple threads at the same time
104 we need to take thread-safty carefully
107 # We need to use the global keyword here
108 global _priv # pylint: disable-msg=W0603
114 # W0621: Redefine '_priv' from outer scope (used for singleton)
115 _priv = resolver() # pylint: disable-msg=W0621