root / lib / runtime.py @ 848cdc34
History | View | Annotate | Download (6.3 kB)
1 | f12e1736 | René Nussbaumer | #
|
---|---|---|---|
2 | f299ca21 | Michael Hanselmann | #
|
3 | f12e1736 | René Nussbaumer | |
4 | f12e1736 | René Nussbaumer | # Copyright (C) 2010 Google Inc.
|
5 | f12e1736 | René Nussbaumer | #
|
6 | f12e1736 | René Nussbaumer | # This program is free software; you can redistribute it and/or modify
|
7 | f12e1736 | René Nussbaumer | # it under the terms of the GNU General Public License as published by
|
8 | f12e1736 | René Nussbaumer | # the Free Software Foundation; either version 2 of the License, or
|
9 | f12e1736 | René Nussbaumer | # (at your option) any later version.
|
10 | f12e1736 | René Nussbaumer | #
|
11 | f12e1736 | René Nussbaumer | # This program is distributed in the hope that it will be useful, but
|
12 | f12e1736 | René Nussbaumer | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | f12e1736 | René Nussbaumer | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | f12e1736 | René Nussbaumer | # General Public License for more details.
|
15 | f12e1736 | René Nussbaumer | #
|
16 | f12e1736 | René Nussbaumer | # You should have received a copy of the GNU General Public License
|
17 | f12e1736 | René Nussbaumer | # along with this program; if not, write to the Free Software
|
18 | f12e1736 | René Nussbaumer | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | f12e1736 | René Nussbaumer | # 02110-1301, USA.
|
20 | f12e1736 | René Nussbaumer | |
21 | f12e1736 | René Nussbaumer | """Module implementing configuration details at runtime.
|
22 | f12e1736 | René Nussbaumer |
|
23 | f12e1736 | René Nussbaumer | """
|
24 | f12e1736 | René Nussbaumer | |
25 | f12e1736 | René Nussbaumer | |
26 | f12e1736 | René Nussbaumer | import grp |
27 | f12e1736 | René Nussbaumer | import pwd |
28 | f12e1736 | René Nussbaumer | import threading |
29 | a20e4768 | Michael Hanselmann | import platform |
30 | f12e1736 | René Nussbaumer | |
31 | f12e1736 | René Nussbaumer | from ganeti import constants |
32 | f12e1736 | René Nussbaumer | from ganeti import errors |
33 | 44fbd23b | René Nussbaumer | from ganeti import utils |
34 | f12e1736 | René Nussbaumer | |
35 | f12e1736 | René Nussbaumer | |
36 | f12e1736 | René Nussbaumer | _priv = None
|
37 | f12e1736 | René Nussbaumer | _priv_lock = threading.Lock() |
38 | f12e1736 | René Nussbaumer | |
39 | a20e4768 | Michael Hanselmann | #: Architecture information
|
40 | a20e4768 | Michael Hanselmann | _arch = None
|
41 | a20e4768 | Michael Hanselmann | |
42 | f12e1736 | René Nussbaumer | |
43 | f12e1736 | René Nussbaumer | def GetUid(user, _getpwnam): |
44 | f12e1736 | René Nussbaumer | """Retrieve the uid from the database.
|
45 | f12e1736 | René Nussbaumer |
|
46 | f12e1736 | René Nussbaumer | @type user: string
|
47 | f12e1736 | René Nussbaumer | @param user: The username to retrieve
|
48 | f12e1736 | René Nussbaumer | @return: The resolved uid
|
49 | f12e1736 | René Nussbaumer |
|
50 | f12e1736 | René Nussbaumer | """
|
51 | f12e1736 | René Nussbaumer | try:
|
52 | f12e1736 | René Nussbaumer | return _getpwnam(user).pw_uid
|
53 | f12e1736 | René Nussbaumer | except KeyError, err: |
54 | f12e1736 | René Nussbaumer | raise errors.ConfigurationError("User '%s' not found (%s)" % (user, err)) |
55 | f12e1736 | René Nussbaumer | |
56 | f12e1736 | René Nussbaumer | |
57 | f12e1736 | René Nussbaumer | def GetGid(group, _getgrnam): |
58 | f12e1736 | René Nussbaumer | """Retrieve the gid from the database.
|
59 | f12e1736 | René Nussbaumer |
|
60 | f12e1736 | René Nussbaumer | @type group: string
|
61 | f12e1736 | René Nussbaumer | @param group: The group name to retrieve
|
62 | f12e1736 | René Nussbaumer | @return: The resolved gid
|
63 | f12e1736 | René Nussbaumer |
|
64 | f12e1736 | René Nussbaumer | """
|
65 | f12e1736 | René Nussbaumer | try:
|
66 | f12e1736 | René Nussbaumer | return _getgrnam(group).gr_gid
|
67 | f12e1736 | René Nussbaumer | except KeyError, err: |
68 | f12e1736 | René Nussbaumer | raise errors.ConfigurationError("Group '%s' not found (%s)" % (group, err)) |
69 | f12e1736 | René Nussbaumer | |
70 | f12e1736 | René Nussbaumer | |
71 | f12e1736 | René Nussbaumer | class GetentResolver: |
72 | f12e1736 | René Nussbaumer | """Resolves Ganeti uids and gids by name.
|
73 | f12e1736 | René Nussbaumer |
|
74 | f12e1736 | René Nussbaumer | @ivar masterd_uid: The resolved uid of the masterd user
|
75 | f12e1736 | René Nussbaumer | @ivar masterd_gid: The resolved gid of the masterd group
|
76 | f12e1736 | René Nussbaumer | @ivar confd_uid: The resolved uid of the confd user
|
77 | f12e1736 | René Nussbaumer | @ivar confd_gid: The resolved gid of the confd group
|
78 | f12e1736 | René Nussbaumer | @ivar rapi_uid: The resolved uid of the rapi user
|
79 | f12e1736 | René Nussbaumer | @ivar rapi_gid: The resolved gid of the rapi group
|
80 | f12e1736 | René Nussbaumer | @ivar noded_uid: The resolved uid of the noded user
|
81 | f12e1736 | René Nussbaumer | @ivar daemons_gid: The resolved gid of the daemons group
|
82 | f12e1736 | René Nussbaumer | @ivar admin_gid: The resolved gid of the admin group
|
83 | f00170e2 | Michael Hanselmann |
|
84 | f12e1736 | René Nussbaumer | """
|
85 | f12e1736 | René Nussbaumer | def __init__(self, _getpwnam=pwd.getpwnam, _getgrnam=grp.getgrnam): |
86 | f12e1736 | René Nussbaumer | """Initialize the resolver.
|
87 | f12e1736 | René Nussbaumer |
|
88 | f12e1736 | René Nussbaumer | """
|
89 | f12e1736 | René Nussbaumer | # Daemon pairs
|
90 | f12e1736 | René Nussbaumer | self.masterd_uid = GetUid(constants.MASTERD_USER, _getpwnam)
|
91 | f12e1736 | René Nussbaumer | self.masterd_gid = GetGid(constants.MASTERD_GROUP, _getgrnam)
|
92 | f12e1736 | René Nussbaumer | |
93 | f12e1736 | René Nussbaumer | self.confd_uid = GetUid(constants.CONFD_USER, _getpwnam)
|
94 | f12e1736 | René Nussbaumer | self.confd_gid = GetGid(constants.CONFD_GROUP, _getgrnam)
|
95 | f12e1736 | René Nussbaumer | |
96 | f12e1736 | René Nussbaumer | self.rapi_uid = GetUid(constants.RAPI_USER, _getpwnam)
|
97 | f12e1736 | René Nussbaumer | self.rapi_gid = GetGid(constants.RAPI_GROUP, _getgrnam)
|
98 | f12e1736 | René Nussbaumer | |
99 | f12e1736 | René Nussbaumer | self.noded_uid = GetUid(constants.NODED_USER, _getpwnam)
|
100 | 44fbd23b | René Nussbaumer | self.noded_gid = GetGid(constants.NODED_GROUP, _getgrnam)
|
101 | f12e1736 | René Nussbaumer | |
102 | f12e1736 | René Nussbaumer | # Misc Ganeti groups
|
103 | f12e1736 | René Nussbaumer | self.daemons_gid = GetGid(constants.DAEMONS_GROUP, _getgrnam)
|
104 | f12e1736 | René Nussbaumer | self.admin_gid = GetGid(constants.ADMIN_GROUP, _getgrnam)
|
105 | f12e1736 | René Nussbaumer | |
106 | 44fbd23b | René Nussbaumer | self._uid2user = {
|
107 | 44fbd23b | René Nussbaumer | self.masterd_uid: constants.MASTERD_USER,
|
108 | 44fbd23b | René Nussbaumer | self.confd_uid: constants.CONFD_USER,
|
109 | 44fbd23b | René Nussbaumer | self.rapi_uid: constants.RAPI_USER,
|
110 | 44fbd23b | René Nussbaumer | self.noded_uid: constants.NODED_USER,
|
111 | 44fbd23b | René Nussbaumer | } |
112 | 44fbd23b | René Nussbaumer | |
113 | 44fbd23b | René Nussbaumer | self._gid2group = {
|
114 | 44fbd23b | René Nussbaumer | self.masterd_gid: constants.MASTERD_GROUP,
|
115 | 44fbd23b | René Nussbaumer | self.confd_gid: constants.CONFD_GROUP,
|
116 | 44fbd23b | René Nussbaumer | self.rapi_gid: constants.RAPI_GROUP,
|
117 | 44fbd23b | René Nussbaumer | self.noded_gid: constants.NODED_GROUP,
|
118 | 44fbd23b | René Nussbaumer | self.daemons_gid: constants.DAEMONS_GROUP,
|
119 | 44fbd23b | René Nussbaumer | self.admin_gid: constants.ADMIN_GROUP,
|
120 | 44fbd23b | René Nussbaumer | } |
121 | 44fbd23b | René Nussbaumer | |
122 | 44fbd23b | René Nussbaumer | self._user2uid = utils.InvertDict(self._uid2user) |
123 | 44fbd23b | René Nussbaumer | self._group2gid = utils.InvertDict(self._gid2group) |
124 | 44fbd23b | René Nussbaumer | |
125 | 44fbd23b | René Nussbaumer | def LookupUid(self, uid): |
126 | 44fbd23b | René Nussbaumer | """Looks which Ganeti user belongs to this uid.
|
127 | 44fbd23b | René Nussbaumer |
|
128 | 44fbd23b | René Nussbaumer | @param uid: The uid to lookup
|
129 | 44fbd23b | René Nussbaumer | @returns The user name associated with that uid
|
130 | 44fbd23b | René Nussbaumer |
|
131 | 44fbd23b | René Nussbaumer | """
|
132 | 44fbd23b | René Nussbaumer | try:
|
133 | 44fbd23b | René Nussbaumer | return self._uid2user[uid] |
134 | 44fbd23b | René Nussbaumer | except KeyError: |
135 | 44fbd23b | René Nussbaumer | raise errors.ConfigurationError("Unknown Ganeti uid '%d'" % uid) |
136 | 44fbd23b | René Nussbaumer | |
137 | 44fbd23b | René Nussbaumer | def LookupGid(self, gid): |
138 | 44fbd23b | René Nussbaumer | """Looks which Ganeti group belongs to this gid.
|
139 | 44fbd23b | René Nussbaumer |
|
140 | 44fbd23b | René Nussbaumer | @param gid: The gid to lookup
|
141 | 44fbd23b | René Nussbaumer | @returns The group name associated with that gid
|
142 | 44fbd23b | René Nussbaumer |
|
143 | 44fbd23b | René Nussbaumer | """
|
144 | 44fbd23b | René Nussbaumer | try:
|
145 | 44fbd23b | René Nussbaumer | return self._gid2group[gid] |
146 | 44fbd23b | René Nussbaumer | except KeyError: |
147 | 44fbd23b | René Nussbaumer | raise errors.ConfigurationError("Unknown Ganeti gid '%d'" % gid) |
148 | 44fbd23b | René Nussbaumer | |
149 | 44fbd23b | René Nussbaumer | def LookupUser(self, name): |
150 | 44fbd23b | René Nussbaumer | """Looks which uid belongs to this name.
|
151 | 44fbd23b | René Nussbaumer |
|
152 | 44fbd23b | René Nussbaumer | @param name: The name to lookup
|
153 | 44fbd23b | René Nussbaumer | @returns The uid associated with that user name
|
154 | 44fbd23b | René Nussbaumer |
|
155 | 44fbd23b | René Nussbaumer | """
|
156 | 44fbd23b | René Nussbaumer | try:
|
157 | 44fbd23b | René Nussbaumer | return self._user2uid[name] |
158 | 44fbd23b | René Nussbaumer | except KeyError: |
159 | 44fbd23b | René Nussbaumer | raise errors.ConfigurationError("Unknown Ganeti user '%s'" % name) |
160 | 44fbd23b | René Nussbaumer | |
161 | 44fbd23b | René Nussbaumer | def LookupGroup(self, name): |
162 | 44fbd23b | René Nussbaumer | """Looks which gid belongs to this name.
|
163 | 44fbd23b | René Nussbaumer |
|
164 | 44fbd23b | René Nussbaumer | @param name: The name to lookup
|
165 | 44fbd23b | René Nussbaumer | @returns The gid associated with that group name
|
166 | 44fbd23b | René Nussbaumer |
|
167 | 44fbd23b | René Nussbaumer | """
|
168 | 44fbd23b | René Nussbaumer | try:
|
169 | 44fbd23b | René Nussbaumer | return self._group2gid[name] |
170 | 44fbd23b | René Nussbaumer | except KeyError: |
171 | 44fbd23b | René Nussbaumer | raise errors.ConfigurationError("Unknown Ganeti group '%s'" % name) |
172 | 44fbd23b | René Nussbaumer | |
173 | f12e1736 | René Nussbaumer | |
174 | f12e1736 | René Nussbaumer | def GetEnts(resolver=GetentResolver): |
175 | f12e1736 | René Nussbaumer | """Singleton wrapper around resolver instance.
|
176 | f12e1736 | René Nussbaumer |
|
177 | f12e1736 | René Nussbaumer | As this method is accessed by multiple threads at the same time
|
178 | 1fdfa87a | Iustin Pop | we need to take thread-safety carefully.
|
179 | f12e1736 | René Nussbaumer |
|
180 | f12e1736 | René Nussbaumer | """
|
181 | f12e1736 | René Nussbaumer | # We need to use the global keyword here
|
182 | b459a848 | Andrea Spadaccini | global _priv # pylint: disable=W0603 |
183 | f12e1736 | René Nussbaumer | |
184 | f12e1736 | René Nussbaumer | if not _priv: |
185 | f12e1736 | René Nussbaumer | _priv_lock.acquire() |
186 | f12e1736 | René Nussbaumer | try:
|
187 | f12e1736 | René Nussbaumer | if not _priv: |
188 | f12e1736 | René Nussbaumer | # W0621: Redefine '_priv' from outer scope (used for singleton)
|
189 | b459a848 | Andrea Spadaccini | _priv = resolver() # pylint: disable=W0621
|
190 | f12e1736 | René Nussbaumer | finally:
|
191 | f12e1736 | René Nussbaumer | _priv_lock.release() |
192 | f12e1736 | René Nussbaumer | |
193 | f12e1736 | René Nussbaumer | return _priv
|
194 | a20e4768 | Michael Hanselmann | |
195 | a20e4768 | Michael Hanselmann | |
196 | a20e4768 | Michael Hanselmann | def InitArchInfo(): |
197 | a20e4768 | Michael Hanselmann | """Initialize architecture information.
|
198 | a20e4768 | Michael Hanselmann |
|
199 | a20e4768 | Michael Hanselmann | We can assume this information never changes during the lifetime of a
|
200 | a20e4768 | Michael Hanselmann | process, therefore the information can easily be cached.
|
201 | a20e4768 | Michael Hanselmann |
|
202 | a20e4768 | Michael Hanselmann | @note: This function uses C{platform.architecture} to retrieve the Python
|
203 | a20e4768 | Michael Hanselmann | binary architecture and does so by forking to run C{file} (see Python
|
204 | a20e4768 | Michael Hanselmann | documentation for more information). Therefore it must not be used in a
|
205 | a20e4768 | Michael Hanselmann | multi-threaded environment.
|
206 | a20e4768 | Michael Hanselmann |
|
207 | a20e4768 | Michael Hanselmann | """
|
208 | a20e4768 | Michael Hanselmann | global _arch # pylint: disable=W0603 |
209 | a20e4768 | Michael Hanselmann | |
210 | a20e4768 | Michael Hanselmann | if _arch is not None: |
211 | a20e4768 | Michael Hanselmann | raise errors.ProgrammerError("Architecture information can only be" |
212 | a20e4768 | Michael Hanselmann | " initialized once")
|
213 | a20e4768 | Michael Hanselmann | |
214 | a20e4768 | Michael Hanselmann | _arch = (platform.architecture()[0], platform.machine())
|
215 | a20e4768 | Michael Hanselmann | |
216 | a20e4768 | Michael Hanselmann | |
217 | a20e4768 | Michael Hanselmann | def GetArchInfo(): |
218 | a20e4768 | Michael Hanselmann | """Returns previsouly initialized architecture information.
|
219 | a20e4768 | Michael Hanselmann |
|
220 | a20e4768 | Michael Hanselmann | """
|
221 | a20e4768 | Michael Hanselmann | if _arch is None: |
222 | a20e4768 | Michael Hanselmann | raise errors.ProgrammerError("Architecture information hasn't been" |
223 | a20e4768 | Michael Hanselmann | " initialized")
|
224 | a20e4768 | Michael Hanselmann | |
225 | a20e4768 | Michael Hanselmann | return _arch |