Iotlab now using sfa database for the special table lease_table.
[sfa.git] / sfa / importer / iotlabimporter.py
index 8687437..c29457c 100644 (file)
@@ -5,18 +5,22 @@ of which slice hrn contains which job.
 """
 from sfa.util.config import Config
 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn
-
-from sfa.iotlab.iotlabdriver import IotlabDriver
-from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB
+from sfa.iotlab.iotlabshell import IotlabShell
+from sfa.iotlab.iotlabdriver import IotlabDriver
+from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB
 from sfa.trust.certificate import Keypair, convert_public_key
 from sfa.trust.gid import create_uuid
 
-from sfa.storage.alchemy import dbsession
+# using global alchemy.session() here is fine
+# as importer is on standalone one-shot process
+
+from sfa.storage.alchemy import global_dbsession, engine
 from sfa.storage.model import RegRecord, RegAuthority, RegSlice, RegNode, \
-    RegUser, RegKey
+    RegUser, RegKey, init_tables
 
+from sqlalchemy import Table, MetaData
+from sqlalchemy.exc import SQLAlchemyError, NoSuchTableError
 
-from sqlalchemy.exc import SQLAlchemyError
 
 
 class IotlabImporter:
@@ -42,8 +46,9 @@ class IotlabImporter:
         self.auth_hierarchy = auth_hierarchy
         self.logger = loc_logger
         self.logger.setLevelDebug()
+
         #retrieve all existing SFA objects
-        self.all_records = dbsession.query(RegRecord).all()
+        self.all_records = global_dbsession.query(RegRecord).all()
 
         # initialize record.stale to True by default,
         # then mark stale=False on the ones that are in use
@@ -65,6 +70,27 @@ class IotlabImporter:
                   for record in self.all_records if record.pointer != -1])
 
 
+
+    def exists(self, tablename, engine):
+        """
+        Checks if the table specified as tablename exists.
+        :param tablename: name of the table in the db that has to be checked.
+        :type tablename: string
+        :returns: True if the table exists, False otherwise.
+        :rtype: bool
+
+        """
+        metadata = MetaData(bind=engine)
+        try:
+            table = Table(tablename, metadata, autoload=True)
+            return True
+
+        except NoSuchTableError:
+            self.logger.log_exc("Iotlabimporter tablename %s does not exist"
+                           % (tablename))
+            return False
+
+
     @staticmethod
     def hostname_to_hrn_escaped(root_auth, hostname):
         """
@@ -152,7 +178,7 @@ class IotlabImporter:
         self.records_by_type_hrn[rec_tuple] = record
 
 
-    def import_nodes(self, site_node_ids, nodes_by_id, iotlabdriver):
+    def import_nodes(self, site_node_ids, nodes_by_id, testbed_shell):
         """
 
         Creates appropriate hostnames and RegNode records for each node in
@@ -164,9 +190,9 @@ class IotlabImporter:
         :param nodes_by_id: dictionary , key is the node id, value is the a dict
             with node information.
         :type nodes_by_id: dictionary
-        :param iotlabdriver: IotlabDriver object, used to have access to
-            iotlabdriver attributes.
-        :type iotlabdriver: IotlabDriver
+        :param testbed_shell: IotlabDriver object, used to have access to
+            testbed_shell attributes.
+        :type testbed_shell: IotlabDriver
 
         :returns: None
         :rtype: None
@@ -181,7 +207,7 @@ class IotlabImporter:
                         - ignored" % (node_id))
                 continue
             escaped_hrn =  \
-                self.hostname_to_hrn_escaped(iotlabdriver.iotlab_api.root_auth,
+                self.hostname_to_hrn_escaped(testbed_shell.root_auth,
                                              node['hostname'])
             self.logger.info("IOTLABIMPORTER node %s " % (node))
             hrn = node['hrn']
@@ -213,8 +239,8 @@ class IotlabImporter:
                 try:
 
                     node_record.just_created()
-                    dbsession.add(node_record)
-                    dbsession.commit()
+                    global_dbsession.add(node_record)
+                    global_dbsession.commit()
                     self.logger.info("IotlabImporter: imported node: %s"
                                      % node_record)
                     self.update_just_added_records_dict(node_record)
@@ -225,7 +251,7 @@ class IotlabImporter:
                 pass
             node_record.stale = False
 
-    def import_sites_and_nodes(self, iotlabdriver):
+    def import_sites_and_nodes(self, testbed_shell):
         """
 
         Gets all the sites and nodes from OAR, process the information,
@@ -233,13 +259,13 @@ class IotlabImporter:
         For each site, import the site's nodes to the DB by calling
         import_nodes.
 
-        :param iotlabdriver: IotlabDriver object, used to have access to
-            iotlabdriver methods and fetching info on sites and nodes.
-        :type iotlabdriver: IotlabDriver
+        :param testbed_shell: IotlabDriver object, used to have access to
+            testbed_shell methods and fetching info on sites and nodes.
+        :type testbed_shell: IotlabDriver
         """
 
-        sites_listdict = iotlabdriver.iotlab_api.GetSites()
-        nodes_listdict = iotlabdriver.iotlab_api.GetNodes()
+        sites_listdict = testbed_shell.GetSites()
+        nodes_listdict = testbed_shell.GetNodes()
         nodes_by_id = dict([(node['node_id'], node) for node in nodes_listdict])
         for site in sites_listdict:
             site_hrn = site['name']
@@ -259,8 +285,8 @@ class IotlabImporter:
                                      pointer='-1',
                                      authority=get_authority(site_hrn))
                     site_record.just_created()
-                    dbsession.add(site_record)
-                    dbsession.commit()
+                    global_dbsession.add(site_record)
+                    global_dbsession.commit()
                     self.logger.info("IotlabImporter: imported authority \
                                     (site) %s" % site_record)
                     self.update_just_added_records_dict(site_record)
@@ -276,7 +302,7 @@ class IotlabImporter:
                 pass
 
             site_record.stale = False
-            self.import_nodes(site['node_ids'], nodes_by_id, iotlabdriver)
+            self.import_nodes(site['node_ids'], nodes_by_id, testbed_shell)
 
         return
 
@@ -317,7 +343,7 @@ class IotlabImporter:
             pkey = Keypair(create=True)
         return (pubkey, pkey)
 
-    def import_persons_and_slices(self, iotlabdriver):
+    def import_persons_and_slices(self, testbed_shell):
         """
 
         Gets user data from LDAP, process the information.
@@ -328,11 +354,13 @@ class IotlabImporter:
         import the user's slice onto the database as well by calling
         import_slice.
 
-        :param iotlabdriver: IotlabDriver object, used to have access to
-            iotlabdriver attributes.
-        :type iotlabdriver: IotlabDriver
+        :param testbed_shell: IotlabDriver object, used to have access to
+            testbed_shell attributes.
+        :type testbed_shell: IotlabDriver
+
+        .. warning:: does not support multiple keys per user
         """
-        ldap_person_listdict = iotlabdriver.iotlab_api.GetPersons()
+        ldap_person_listdict = testbed_shell.GetPersons()
         self.logger.info("IOTLABIMPORT \t ldap_person_listdict %s \r\n"
                          % (ldap_person_listdict))
 
@@ -404,8 +432,8 @@ class IotlabImporter:
 
                         try:
                             user_record.just_created()
-                            dbsession.add (user_record)
-                            dbsession.commit()
+                            global_dbsession.add (user_record)
+                            global_dbsession.commit()
                             self.logger.info("IotlabImporter: imported person \
                                             %s" % (user_record))
                             self.update_just_added_records_dict(user_record)
@@ -440,7 +468,7 @@ class IotlabImporter:
                     user_record.email = person['email']
 
             try:
-                dbsession.commit()
+                global_dbsession.commit()
                 user_record.stale = False
             except SQLAlchemyError:
                 self.logger.log_exc("IotlabImporter: \
@@ -478,8 +506,8 @@ class IotlabImporter:
                                     authority=get_authority(slice_hrn))
             try:
                 slice_record.just_created()
-                dbsession.add(slice_record)
-                dbsession.commit()
+                global_dbsession.add(slice_record)
+                global_dbsession.commit()
 
 
                 self.update_just_added_records_dict(slice_record)
@@ -497,7 +525,7 @@ class IotlabImporter:
 
         slice_record.reg_researchers = [user_record]
         try:
-            dbsession.commit()
+            global_dbsession.commit()
             slice_record.stale = False
         except SQLAlchemyError:
             self.logger.log_exc("IotlabImporter: failed to update slice")
@@ -505,32 +533,35 @@ class IotlabImporter:
 
     def run(self, options):
         """
-        Create the special iotlab table, testbed_xp, in the iotlab database.
+        Create the special iotlab table, lease_table, in the iotlab database.
         Import everything (users, slices, nodes and sites from OAR
         and LDAP) into the SFA database.
         Delete stale records that are no longer in OAR or LDAP.
         :param options:
         :type options:
         """
-        config = Config()
 
-        iotlabdriver = IotlabDriver(config)
-        iotlab_db = TestbedAdditionalSfaDB(config)
+        config = Config ()
+        interface_hrn = config.SFA_INTERFACE_HRN
+        root_auth = config.SFA_REGISTRY_ROOT_AUTH
+
+        testbed_shell = IotlabShell(config)
+        # leases_db = TestbedAdditionalSfaDB(config)
         #Create special slice table for iotlab
 
-        if not iotlab_db.exists('testbed_xp'):
-            iotlab_db.createtable()
-            self.logger.info("IotlabImporter.run:  testbed_xp table created ")
+        if not self.exists('lease_table', engine):
+            init_tables(engine)
+            self.logger.info("IotlabImporter.run:  lease_table table created ")
 
         # import site and node records in site into the SFA db.
-        self.import_sites_and_nodes(iotlabdriver)
+        self.import_sites_and_nodes(testbed_shell)
         #import users and slice into the SFA DB.
-        self.import_persons_and_slices(iotlabdriver)
+        self.import_persons_and_slices(testbed_shell)
 
          ### remove stale records
         # special records must be preserved
-        system_hrns = [iotlabdriver.hrn, iotlabdriver.iotlab_api.root_auth,
-                       iotlabdriver.hrn + '.slicemanager']
+        system_hrns = [interface_hrn, root_auth,
+                        interface_hrn + '.slicemanager']
         for record in self.all_records:
             if record.hrn in system_hrns:
                 record.stale = False
@@ -551,8 +582,8 @@ class IotlabImporter:
                                  % (record))
 
                 try:
-                    dbsession.delete(record)
-                    dbsession.commit()
+                    global_dbsession.delete(record)
+                    global_dbsession.commit()
                 except SQLAlchemyError:
                     self.logger.log_exc("IotlabImporter: failed to delete \
                         stale record %s" % (record))