Merge branch 'geni-v3' of ssh://git.onelab.eu/git/sfa into geni-v3
[sfa.git] / sfa / importer / iotlabimporter.py
index 87cb0cd..3528db4 100644 (file)
@@ -1,25 +1,26 @@
 """ File defining the importer class and all the methods needed to import
 the nodes, users and slices from OAR and LDAP to the SFA database.
-Also creates the iotlab specific databse and table to keep track
+Also creates the iotlab specific  table to keep track
 of which slice hrn contains which job.
 """
 from sfa.util.config import Config
-from sfa.generic import Generic
 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn
 from sfa.iotlab.iotlabshell import IotlabShell
 # from sfa.iotlab.iotlabdriver import IotlabDriver
-from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB
+from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB
 from sfa.trust.certificate import Keypair, convert_public_key
 from sfa.trust.gid import create_uuid
 
 # using global alchemy.session() here is fine
 # as importer is on standalone one-shot process
-from sfa.storage.alchemy import global_dbsession
+
+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:
@@ -45,6 +46,7 @@ class IotlabImporter:
         self.auth_hierarchy = auth_hierarchy
         self.logger = loc_logger
         self.logger.setLevelDebug()
+
         #retrieve all existing SFA objects
         self.all_records = global_dbsession.query(RegRecord).all()
 
@@ -68,6 +70,27 @@ class IotlabImporter:
                   for record in self.all_records if record.pointer != -1])
 
 
+
+    def exists(self, tablename):
+        """
+        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):
         """
@@ -122,18 +145,17 @@ class IotlabImporter:
 
     def locate_by_type_pointer(self, record_type, pointer):
         """
-
-        Returns the record corresponding to the key pointer and record
-        type. Returns None if the record does not exist and is not in the
-        records_by_type_pointer dictionnary.
+        Returns the record corresponding to the key pointer and record type.
+            Returns None if the record does not exist and is not in the
+            records_by_type_pointer dictionnary.
 
         :param record_type: the record's type (slice, node, authority...)
         :type  record_type: string
-        :param pointer:Pointer to where the record is in the origin db,
+        :param pointer: Pointer to where the record is in the origin db,
             used in case the record comes from a trusted authority.
         :type pointer: integer
-        :rtype: RegUser if user, RegSlice if slice, RegNode if node...
-            or None if record does not exist.
+        :rtype: RegUser if user, RegSlice if slice, RegNode if node, or None if
+            record does not exist.
         """
         return self.records_by_type_pointer.get((record_type, pointer), None)
 
@@ -199,7 +221,7 @@ class IotlabImporter:
                 node_gid = \
                     self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
 
-                def iotlab_get_authority(hrn):
+                def testbed_get_authority(hrn):
                     """ Gets the authority part in the hrn.
                     :param hrn: hrn whose authority we are looking for.
                     :type hrn: string
@@ -212,7 +234,7 @@ class IotlabImporter:
 
                 node_record = RegNode(hrn=hrn, gid=node_gid,
                                       pointer='-1',
-                                      authority=iotlab_get_authority(hrn))
+                                      authority=testbed_get_authority(hrn))
                 try:
 
                     node_record.just_created()
@@ -287,15 +309,14 @@ class IotlabImporter:
 
     def init_person_key(self, person, iotlab_key):
         """
-
         Returns a tuple pubkey and pkey.
 
         :param person Person's data.
         :type person: dict
-        :param iotlab_key: SSH public key, from LDAP user's data.
-            RSA type supported.
+        :param iotlab_key: SSH public key, from LDAP user's data. RSA type
+            supported.
         :type iotlab_key: string
-        :rtype (string, Keypair)
+        :rtype: (string, Keypair)
 
         """
         pubkey = None
@@ -495,11 +516,9 @@ class IotlabImporter:
         #No slice update upon import in iotlab
         else:
             # xxx update the record ...
-            self.logger.warning("Slice update not yet implemented")
-            pass
-        # record current users affiliated with the slice
-
+            self.logger.warning("Iotlab Slice update not implemented")
 
+        # record current users affiliated with the slice
         slice_record.reg_researchers = [user_record]
         try:
             global_dbsession.commit()
@@ -510,7 +529,7 @@ class IotlabImporter:
 
     def run(self, options):
         """
-        Create the special iotlab table, lease_table, in the iotlab database.
+        Create the special iotlab table, lease_table, in the SFA 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.
@@ -521,13 +540,13 @@ class IotlabImporter:
         config = Config ()
         interface_hrn = config.SFA_INTERFACE_HRN
         root_auth = config.SFA_REGISTRY_ROOT_AUTH
-        api = Generic.the_flavour().make_api(interface='registry')
-        testbed_shell = IotlabShell(api)
-        leases_db = TestbedAdditionalSfaDB(config)
+
+        testbed_shell = IotlabShell(config)
+        leases_db = TestbedAdditionalSfaDB(config)
         #Create special slice table for iotlab
 
-        if not leases_db.exists('lease_table'):
-            leases_db.createtable()
+        if not self.exists('lease_table'):
+            init_tables(engine)
             self.logger.info("IotlabImporter.run:  lease_table table created ")
 
         # import site and node records in site into the SFA db.