more registry operations
authorScott Baker <bakers@cs.arizona.edu>
Mon, 11 Aug 2008 22:14:34 +0000 (22:14 +0000)
committerScott Baker <bakers@cs.arizona.edu>
Mon, 11 Aug 2008 22:14:34 +0000 (22:14 +0000)
util/excep.py
util/geniserver.py
util/hierarchy.py
util/record.py

index 4f2a4fa..67d8911 100644 (file)
@@ -34,9 +34,41 @@ class NonexistingFile(Exception):
         self.value = value
     def __str__(self):
         return repr(self.value)
-        
+
 class InvalidRPCParams(Exception):
     def __init__(self, value):
         self.value = value
     def __str__(self):
         return repr(self.value)
+
+# SMBAKER exceptions follow
+
+class ConnectionKeyGIDMismatch(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
+class MissingCallerGID(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
+class RecordNotFound(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
+class UnknownGeniType(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
+class MissingAuthority(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
index 6a8eb87..c4cd60a 100644 (file)
@@ -163,6 +163,7 @@ class GeniServer():
     def decode_authentication(self, cred_string):
         self.client_cred = Credential(string = cred_string)
         self.client_gid = self.client_cred.get_gid_caller()
+        self.object_gid = self.client_cred.get_gid_object()
 
         # make sure the client_gid is not blank
         if not self.client_gid:
index 5dd2b96..738171f 100644 (file)
@@ -43,49 +43,65 @@ class Hierarchy():
     def __init__(self, basedir="."):
         self.basedir = basedir
 
-    # type = sa|ma
-    def get_auth_info(self, hrn, type, can_create=True):
+    def get_auth_filenames(self, hrn):
         leaf = get_leaf(hrn)
         parent_hrn = get_authority(hrn)
-        directory = os.path.join(self.basedir,
-                        os.path.join(type, hrn.replace(".", "/")))
+        directory = os.path.join(self.basedir, hrn.replace(".", "/"))
 
         gid_filename = os.path.join(directory, leaf+".gid")
         privkey_filename = os.path.join(directory, leaf+".pkey")
         dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
 
-        if (not os.path.exists(gid_filename)) or \
-           (not os.path.exists(privkey_filename)) or \
-           (not os.path.exists(dbinfo_filename)):
-            if not can_create:
-                return MissingAuthorityFiles(hrn)
+        return (directory, gid_filename, privkey_filename, dbinfo_filename)
+
+    def auth_exists(self, hrn):
+        (directory, gid_filename, privkey_filename, dbinfo_filename) = \
+            self.get_auth_filenames(hrn)
+
+        return os.path.exists(gid_filename) and \
+               os.path.exists(privkey_filename) and \
+               os.path.exists(dbinfo_filename)
+
+    def create_auth(self, hrn):
+        (directory, gid_filename, privkey_filename, dbinfo_filename) = \
+            self.get_auth_filenames(hrn)
 
-            # create the directory to hold the files
-            try:
-                os.makedirs(directory)\r
-            # if the path already exists then pass\r
-            except OSError, (errno, strerr):\r
-                if errno == 17:\r
-                    pass
+        # create the directory to hold the files
+        try:
+            os.makedirs(directory)\r
+        # if the path already exists then pass\r
+        except OSError, (errno, strerr):\r
+            if errno == 17:\r
+                pass
 
-            pkey = Keypair(create = True)
-            pkey.save_to_file(privkey_filename)
+        pkey = Keypair(create = True)
+        pkey.save_to_file(privkey_filename)
 
-            gid = self.create_gid(type, hrn, create_uuid(), pkey)
-            gid.save_to_file(gid_filename)
+        gid = self.create_gid(hrn, create_uuid(), pkey)
+        gid.save_to_file(gid_filename)
+
+        # XXX TODO: think up a better way for the dbinfo to work
+
+        dbinfo = get_default_dbinfo()
+        dbinfo_file = file(dbinfo_filename, "w")
+        dbinfo_file.write(str(dbinfo))\r
+        dbinfo_file.close()
+
+    def get_auth_info(self, hrn, can_create=True):
+        if not self.auth_exists(hrn):
+            if not can_create:
+                return MissingAuthority(hrn)
 
-            # XXX TODO: think up a better way for the dbinfo to work
+            self.create_auth(hrn)
 
-            dbinfo = get_default_dbinfo()
-            dbinfo_file = file(dbinfo_filename, "w")
-            dbinfo_file.write(str(dbinfo))\r
-            dbinfo_file.close()\r
+        (directory, gid_filename, privkey_filename, dbinfo_filename) = \
+            self.get_auth_filenames(hrn)
 
         auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
 
         return auth_info
 
-    def create_gid(self, type, hrn, uuid, pkey):
+    def create_gid(self, hrn, uuid, pkey):
         parent_hrn = get_authority(hrn)
 
         gid = GID(subject=hrn, uuid=uuid)
@@ -96,7 +112,7 @@ class Hierarchy():
             gid.set_issuer(pkey, hrn)
         else:
             # we need the parent's private key in order to sign this GID
-            parent_auth_info = self.get_auth_info(parent_hrn, type)
+            parent_auth_info = self.get_auth_info(parent_hrn)
             gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
             gid.set_parent(parent_auth_info.get_gid_object())
 
@@ -105,7 +121,7 @@ class Hierarchy():
 
         return gid
 
-    def refresh_gid(self, type, gid, hrn=None, uuid=None, pubkey=None):
+    def refresh_gid(self, gid, hrn=None, uuid=None, pubkey=None):
         # TODO: compute expiration time of GID, refresh it if necessary
         gid_is_expired = False
 
@@ -118,7 +134,7 @@ class Hierarchy():
             if not pubkey:
                 pubkey = gid.get_pubkey()
 
-            gid = self.create_gid(type, hrn, uuid, pubkey)
+            gid = self.create_gid(hrn, uuid, pubkey)
 
         return gid
 
index 85a1bd9..9149deb 100644 (file)
@@ -9,11 +9,16 @@ from pg import DB
 GENI_TABLE_PREFIX = "geni$"
 
 # Record is a tuple (Name, GID, Type, Info)
-#    info is implemented as a pointer to a PLC record
+#    info is comprised of the following sub-fields
+#        pointer = a pointer to the record in the PL database
+#        pl_info = planetlab-specific info (when talking to client)
+#        geni_info = geni-specific info (when talking to client)
 
 class GeniRecord():
     def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None):
-        self.dirty=True
+        self.dirty = True
+        self.pl_info = None
+        self.geni_info = None
         if name:
             self.set_name(name)
         if gid:
@@ -27,6 +32,10 @@ class GeniRecord():
             self.set_gid(dict['gid'])
             self.set_type(dict['type'])
             self.set_pointer(dict['pointer'])
+            if "pl_info" in dict:
+               self.set_pl_info(dict["pl_info"])
+            if "geni_info" in dict:
+               self.set_geni_info(dict["geni_info"])
 
     def set_name(self, name):
         self.name = name
@@ -44,6 +53,29 @@ class GeniRecord():
         self.pointer = pointer
         self.dirty = True
 
+    def set_pl_info(self, pl_info):
+        self.pl_info = pl_info
+        self.dirty = True
+
+    def set_geni_info(self, geni_info):
+        self.geni_info = geni_info
+        self.dirty = True
+
+    def get_pl_info(self):
+        if self.pl_info:
+            return self.pl_info
+        else:
+            return {}
+
+    def get_geni_info(self):
+        if self.geni_info:
+            return self.geni_info
+        else:
+            return {}
+
+    def get_pointer(self):
+        return pointer
+
     def get_key(self):
         return self.name + "#" + self.type
 
@@ -71,6 +103,13 @@ class GeniRecord():
         names = self.get_field_names()
         for name in names:
             dict[name] = self.getattr(name)
+
+        if self.pl_info:
+            dict['pl_info'] = self.pl_info
+
+        if self.geni_info:
+            dict['geni_info'] = self.geni_info
+
         return dict
 
 # GeniTable
@@ -93,6 +132,10 @@ class GeniTable():
         if create:
             self.create()
 
+    def exists(self):
+        tableList = self.cnx.get_tables()
+        return (self.tablename in tableList)
+
     def create(self):
         querystr = "CREATE TABLE " + self.tablename + " ( \
                 key text, \
@@ -104,6 +147,10 @@ class GeniTable():
         self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
         self.cnx.query(querystr)
 
+    def remove(self, record):
+        query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'"
+        self.cnx.quert(query_str)
+
     def insert(self, record):
         fieldnames = ["key"] + record.get_field_names()
         fieldvals = record.get_field_value_strings(fieldnames)
@@ -125,7 +172,7 @@ class GeniTable():
         #print query_str
         self.cnx.query(query_str)
 
-    def resolve_raw(self, type, hrn):
+    def resolve_dict(self, type, hrn):
         query_str = "SELECT * FROM " + self.tablename + " WHERE name = '" + hrn + "'"
         dict_list = self.cnx.query(query_str).dictresult()
         result_dict_list = []
@@ -135,7 +182,19 @@ class GeniTable():
         return result_dict_list
 
     def resolve(self, type, hrn):
-        result_dict_list = self.resolve_raw(type, hrn)
+        result_dict_list = self.resolve_dict(type, hrn)
+        result_rec_list = []
+        for dict in result_dict_list:
+            result_rec_list.append(GeniRecord(dict=dict))
+        return result_rec_list
+
+    def list_dict(self):
+        query_str = "SELECT * FROM " + self.tablename
+        result_dict_list = self.cnx.query(query_str).dictresult()
+        return result_dict_list
+
+    def list(self):
+        result_dict_list = self.list_dict()
         result_rec_list = []
         for dict in result_dict_list:
             result_rec_list.append(GeniRecord(dict=dict))
@@ -147,7 +206,7 @@ def set_geni_table_prefix(x):
     GENI_TABLE_PREFIX = x
 
 def geni_records_purge(cninfo):
-    global GENI_TABLE_PREFIX 
+    global GENI_TABLE_PREFIX
 
     cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
     tableList = cnx.get_tables()