Add stricter checks for OpInstanceSetParams.{nics,disks}
[ganeti-local] / lib / runtime.py
index 2d65ef0..5180486 100644 (file)
@@ -1,4 +1,5 @@
 #
+#
 
 # Copyright (C) 2010 Google Inc.
 #
@@ -28,6 +29,7 @@ import threading
 
 from ganeti import constants
 from ganeti import errors
+from ganeti import utils
 
 
 _priv = None
@@ -72,9 +74,9 @@ class GetentResolver:
   @ivar rapi_uid: The resolved uid of the rapi user
   @ivar rapi_gid: The resolved gid of the rapi group
   @ivar noded_uid: The resolved uid of the noded user
-
   @ivar daemons_gid: The resolved gid of the daemons group
   @ivar admin_gid: The resolved gid of the admin group
+
   """
   def __init__(self, _getpwnam=pwd.getpwnam, _getgrnam=grp.getgrnam):
     """Initialize the resolver.
@@ -91,11 +93,79 @@ class GetentResolver:
     self.rapi_gid = GetGid(constants.RAPI_GROUP, _getgrnam)
 
     self.noded_uid = GetUid(constants.NODED_USER, _getpwnam)
+    self.noded_gid = GetGid(constants.NODED_GROUP, _getgrnam)
 
     # Misc Ganeti groups
     self.daemons_gid = GetGid(constants.DAEMONS_GROUP, _getgrnam)
     self.admin_gid = GetGid(constants.ADMIN_GROUP, _getgrnam)
 
+    self._uid2user = {
+      self.masterd_uid: constants.MASTERD_USER,
+      self.confd_uid: constants.CONFD_USER,
+      self.rapi_uid: constants.RAPI_USER,
+      self.noded_uid: constants.NODED_USER,
+      }
+
+    self._gid2group = {
+      self.masterd_gid: constants.MASTERD_GROUP,
+      self.confd_gid: constants.CONFD_GROUP,
+      self.rapi_gid: constants.RAPI_GROUP,
+      self.noded_gid: constants.NODED_GROUP,
+      self.daemons_gid: constants.DAEMONS_GROUP,
+      self.admin_gid: constants.ADMIN_GROUP,
+      }
+
+    self._user2uid = utils.InvertDict(self._uid2user)
+    self._group2gid = utils.InvertDict(self._gid2group)
+
+  def LookupUid(self, uid):
+    """Looks which Ganeti user belongs to this uid.
+
+    @param uid: The uid to lookup
+    @returns The user name associated with that uid
+
+    """
+    try:
+      return self._uid2user[uid]
+    except KeyError:
+      raise errors.ConfigurationError("Unknown Ganeti uid '%d'" % uid)
+
+  def LookupGid(self, gid):
+    """Looks which Ganeti group belongs to this gid.
+
+    @param gid: The gid to lookup
+    @returns The group name associated with that gid
+
+    """
+    try:
+      return self._gid2group[gid]
+    except KeyError:
+      raise errors.ConfigurationError("Unknown Ganeti gid '%d'" % gid)
+
+  def LookupUser(self, name):
+    """Looks which uid belongs to this name.
+
+    @param name: The name to lookup
+    @returns The uid associated with that user name
+
+    """
+    try:
+      return self._user2uid[name]
+    except KeyError:
+      raise errors.ConfigurationError("Unknown Ganeti user '%s'" % name)
+
+  def LookupGroup(self, name):
+    """Looks which gid belongs to this name.
+
+    @param name: The name to lookup
+    @returns The gid associated with that group name
+
+    """
+    try:
+      return self._group2gid[name]
+    except KeyError:
+      raise errors.ConfigurationError("Unknown Ganeti group '%s'" % name)
+
 
 def GetEnts(resolver=GetentResolver):
   """Singleton wrapper around resolver instance.
@@ -105,16 +175,15 @@ def GetEnts(resolver=GetentResolver):
 
   """
   # We need to use the global keyword here
-  global _priv # pylint: disable-msg=W0603
+  global _priv # pylint: disable=W0603
 
   if not _priv:
     _priv_lock.acquire()
     try:
       if not _priv:
         # W0621: Redefine '_priv' from outer scope (used for singleton)
-        _priv = resolver() # pylint: disable-msg=W0621
+        _priv = resolver() # pylint: disable=W0621
     finally:
       _priv_lock.release()
 
   return _priv
-