Statistics
| Branch: | Tag: | Revision:

root / lib / runtime.py @ 18397489

History | View | Annotate | Download (6.8 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 3695a4e0 Thomas Thrainer
  @ivar luxid_uid: The resolved uid of the luxid user
79 3695a4e0 Thomas Thrainer
  @ivar luxid_gid: The resolved gid of the luxid group
80 f12e1736 René Nussbaumer
  @ivar rapi_uid: The resolved uid of the rapi user
81 f12e1736 René Nussbaumer
  @ivar rapi_gid: The resolved gid of the rapi group
82 f12e1736 René Nussbaumer
  @ivar noded_uid: The resolved uid of the noded user
83 f12e1736 René Nussbaumer
  @ivar daemons_gid: The resolved gid of the daemons group
84 f12e1736 René Nussbaumer
  @ivar admin_gid: The resolved gid of the admin group
85 f00170e2 Michael Hanselmann

86 f12e1736 René Nussbaumer
  """
87 f12e1736 René Nussbaumer
  def __init__(self, _getpwnam=pwd.getpwnam, _getgrnam=grp.getgrnam):
88 f12e1736 René Nussbaumer
    """Initialize the resolver.
89 f12e1736 René Nussbaumer

90 f12e1736 René Nussbaumer
    """
91 f12e1736 René Nussbaumer
    # Daemon pairs
92 f12e1736 René Nussbaumer
    self.masterd_uid = GetUid(constants.MASTERD_USER, _getpwnam)
93 f12e1736 René Nussbaumer
    self.masterd_gid = GetGid(constants.MASTERD_GROUP, _getgrnam)
94 f12e1736 René Nussbaumer
95 f12e1736 René Nussbaumer
    self.confd_uid = GetUid(constants.CONFD_USER, _getpwnam)
96 f12e1736 René Nussbaumer
    self.confd_gid = GetGid(constants.CONFD_GROUP, _getgrnam)
97 f12e1736 René Nussbaumer
98 3695a4e0 Thomas Thrainer
    self.luxid_uid = GetUid(constants.LUXID_USER, _getpwnam)
99 3695a4e0 Thomas Thrainer
    self.luxid_gid = GetGid(constants.LUXID_GROUP, _getgrnam)
100 670e954a Thomas Thrainer
101 f12e1736 René Nussbaumer
    self.rapi_uid = GetUid(constants.RAPI_USER, _getpwnam)
102 f12e1736 René Nussbaumer
    self.rapi_gid = GetGid(constants.RAPI_GROUP, _getgrnam)
103 f12e1736 René Nussbaumer
104 f12e1736 René Nussbaumer
    self.noded_uid = GetUid(constants.NODED_USER, _getpwnam)
105 44fbd23b René Nussbaumer
    self.noded_gid = GetGid(constants.NODED_GROUP, _getgrnam)
106 f12e1736 René Nussbaumer
107 d08a8359 Thomas Thrainer
    self.mond_uid = GetUid(constants.MOND_USER, _getpwnam)
108 d08a8359 Thomas Thrainer
    self.mond_gid = GetGid(constants.MOND_GROUP, _getgrnam)
109 d08a8359 Thomas Thrainer
110 f12e1736 René Nussbaumer
    # Misc Ganeti groups
111 f12e1736 René Nussbaumer
    self.daemons_gid = GetGid(constants.DAEMONS_GROUP, _getgrnam)
112 f12e1736 René Nussbaumer
    self.admin_gid = GetGid(constants.ADMIN_GROUP, _getgrnam)
113 f12e1736 René Nussbaumer
114 44fbd23b René Nussbaumer
    self._uid2user = {
115 44fbd23b René Nussbaumer
      self.masterd_uid: constants.MASTERD_USER,
116 44fbd23b René Nussbaumer
      self.confd_uid: constants.CONFD_USER,
117 3695a4e0 Thomas Thrainer
      self.luxid_uid: constants.LUXID_USER,
118 44fbd23b René Nussbaumer
      self.rapi_uid: constants.RAPI_USER,
119 44fbd23b René Nussbaumer
      self.noded_uid: constants.NODED_USER,
120 d08a8359 Thomas Thrainer
      self.mond_uid: constants.MOND_USER,
121 44fbd23b René Nussbaumer
      }
122 44fbd23b René Nussbaumer
123 44fbd23b René Nussbaumer
    self._gid2group = {
124 44fbd23b René Nussbaumer
      self.masterd_gid: constants.MASTERD_GROUP,
125 44fbd23b René Nussbaumer
      self.confd_gid: constants.CONFD_GROUP,
126 3695a4e0 Thomas Thrainer
      self.luxid_gid: constants.LUXID_GROUP,
127 44fbd23b René Nussbaumer
      self.rapi_gid: constants.RAPI_GROUP,
128 44fbd23b René Nussbaumer
      self.noded_gid: constants.NODED_GROUP,
129 d08a8359 Thomas Thrainer
      self.mond_gid: constants.MOND_GROUP,
130 44fbd23b René Nussbaumer
      self.daemons_gid: constants.DAEMONS_GROUP,
131 44fbd23b René Nussbaumer
      self.admin_gid: constants.ADMIN_GROUP,
132 44fbd23b René Nussbaumer
      }
133 44fbd23b René Nussbaumer
134 44fbd23b René Nussbaumer
    self._user2uid = utils.InvertDict(self._uid2user)
135 44fbd23b René Nussbaumer
    self._group2gid = utils.InvertDict(self._gid2group)
136 44fbd23b René Nussbaumer
137 44fbd23b René Nussbaumer
  def LookupUid(self, uid):
138 44fbd23b René Nussbaumer
    """Looks which Ganeti user belongs to this uid.
139 44fbd23b René Nussbaumer

140 44fbd23b René Nussbaumer
    @param uid: The uid to lookup
141 44fbd23b René Nussbaumer
    @returns The user name associated with that uid
142 44fbd23b René Nussbaumer

143 44fbd23b René Nussbaumer
    """
144 44fbd23b René Nussbaumer
    try:
145 44fbd23b René Nussbaumer
      return self._uid2user[uid]
146 44fbd23b René Nussbaumer
    except KeyError:
147 44fbd23b René Nussbaumer
      raise errors.ConfigurationError("Unknown Ganeti uid '%d'" % uid)
148 44fbd23b René Nussbaumer
149 44fbd23b René Nussbaumer
  def LookupGid(self, gid):
150 44fbd23b René Nussbaumer
    """Looks which Ganeti group belongs to this gid.
151 44fbd23b René Nussbaumer

152 44fbd23b René Nussbaumer
    @param gid: The gid to lookup
153 44fbd23b René Nussbaumer
    @returns The group name associated with that gid
154 44fbd23b René Nussbaumer

155 44fbd23b René Nussbaumer
    """
156 44fbd23b René Nussbaumer
    try:
157 44fbd23b René Nussbaumer
      return self._gid2group[gid]
158 44fbd23b René Nussbaumer
    except KeyError:
159 44fbd23b René Nussbaumer
      raise errors.ConfigurationError("Unknown Ganeti gid '%d'" % gid)
160 44fbd23b René Nussbaumer
161 44fbd23b René Nussbaumer
  def LookupUser(self, name):
162 44fbd23b René Nussbaumer
    """Looks which uid belongs to this name.
163 44fbd23b René Nussbaumer

164 44fbd23b René Nussbaumer
    @param name: The name to lookup
165 44fbd23b René Nussbaumer
    @returns The uid associated with that user name
166 44fbd23b René Nussbaumer

167 44fbd23b René Nussbaumer
    """
168 44fbd23b René Nussbaumer
    try:
169 44fbd23b René Nussbaumer
      return self._user2uid[name]
170 44fbd23b René Nussbaumer
    except KeyError:
171 44fbd23b René Nussbaumer
      raise errors.ConfigurationError("Unknown Ganeti user '%s'" % name)
172 44fbd23b René Nussbaumer
173 44fbd23b René Nussbaumer
  def LookupGroup(self, name):
174 44fbd23b René Nussbaumer
    """Looks which gid belongs to this name.
175 44fbd23b René Nussbaumer

176 44fbd23b René Nussbaumer
    @param name: The name to lookup
177 44fbd23b René Nussbaumer
    @returns The gid associated with that group name
178 44fbd23b René Nussbaumer

179 44fbd23b René Nussbaumer
    """
180 44fbd23b René Nussbaumer
    try:
181 44fbd23b René Nussbaumer
      return self._group2gid[name]
182 44fbd23b René Nussbaumer
    except KeyError:
183 44fbd23b René Nussbaumer
      raise errors.ConfigurationError("Unknown Ganeti group '%s'" % name)
184 44fbd23b René Nussbaumer
185 f12e1736 René Nussbaumer
186 f12e1736 René Nussbaumer
def GetEnts(resolver=GetentResolver):
187 f12e1736 René Nussbaumer
  """Singleton wrapper around resolver instance.
188 f12e1736 René Nussbaumer

189 f12e1736 René Nussbaumer
  As this method is accessed by multiple threads at the same time
190 1fdfa87a Iustin Pop
  we need to take thread-safety carefully.
191 f12e1736 René Nussbaumer

192 f12e1736 René Nussbaumer
  """
193 f12e1736 René Nussbaumer
  # We need to use the global keyword here
194 b459a848 Andrea Spadaccini
  global _priv # pylint: disable=W0603
195 f12e1736 René Nussbaumer
196 f12e1736 René Nussbaumer
  if not _priv:
197 f12e1736 René Nussbaumer
    _priv_lock.acquire()
198 f12e1736 René Nussbaumer
    try:
199 f12e1736 René Nussbaumer
      if not _priv:
200 f12e1736 René Nussbaumer
        # W0621: Redefine '_priv' from outer scope (used for singleton)
201 b459a848 Andrea Spadaccini
        _priv = resolver() # pylint: disable=W0621
202 f12e1736 René Nussbaumer
    finally:
203 f12e1736 René Nussbaumer
      _priv_lock.release()
204 f12e1736 René Nussbaumer
205 f12e1736 René Nussbaumer
  return _priv
206 a20e4768 Michael Hanselmann
207 a20e4768 Michael Hanselmann
208 a20e4768 Michael Hanselmann
def InitArchInfo():
209 a20e4768 Michael Hanselmann
  """Initialize architecture information.
210 a20e4768 Michael Hanselmann

211 a20e4768 Michael Hanselmann
  We can assume this information never changes during the lifetime of a
212 a20e4768 Michael Hanselmann
  process, therefore the information can easily be cached.
213 a20e4768 Michael Hanselmann

214 a20e4768 Michael Hanselmann
  @note: This function uses C{platform.architecture} to retrieve the Python
215 a20e4768 Michael Hanselmann
    binary architecture and does so by forking to run C{file} (see Python
216 a20e4768 Michael Hanselmann
    documentation for more information). Therefore it must not be used in a
217 a20e4768 Michael Hanselmann
    multi-threaded environment.
218 a20e4768 Michael Hanselmann

219 a20e4768 Michael Hanselmann
  """
220 a20e4768 Michael Hanselmann
  global _arch # pylint: disable=W0603
221 a20e4768 Michael Hanselmann
222 a20e4768 Michael Hanselmann
  if _arch is not None:
223 a20e4768 Michael Hanselmann
    raise errors.ProgrammerError("Architecture information can only be"
224 a20e4768 Michael Hanselmann
                                 " initialized once")
225 a20e4768 Michael Hanselmann
226 a20e4768 Michael Hanselmann
  _arch = (platform.architecture()[0], platform.machine())
227 a20e4768 Michael Hanselmann
228 a20e4768 Michael Hanselmann
229 a20e4768 Michael Hanselmann
def GetArchInfo():
230 a20e4768 Michael Hanselmann
  """Returns previsouly initialized architecture information.
231 a20e4768 Michael Hanselmann

232 a20e4768 Michael Hanselmann
  """
233 a20e4768 Michael Hanselmann
  if _arch is None:
234 a20e4768 Michael Hanselmann
    raise errors.ProgrammerError("Architecture information hasn't been"
235 a20e4768 Michael Hanselmann
                                 " initialized")
236 a20e4768 Michael Hanselmann
237 a20e4768 Michael Hanselmann
  return _arch