Bugfix. NT.
[sfa.git] / sfa / senslab / slab-import.py
index 8867f99..4f20b00 100644 (file)
@@ -1,17 +1,13 @@
-###########################################################################
-#    Copyright (C) 2011 by root                                      
-#    <root@FlabFedora2>                                                             
-#
-# Copyright: See COPYING file that comes with this distribution
-#
-###########################################################################
+
 import sys
 import datetime
 import time
 from sfa.senslab.OARrestapi import OARapi
 from sfa.senslab.LDAPapi import LDAPapi
 from sfa.senslab.slabdriver import SlabDriver
+from sfa.senslab.slabpostgres import SlabDB
 from sfa.util.config import Config
+from sfa.util.plxrn import PlXrn
 from sfa.util.xrn import hrn_to_urn, get_authority,Xrn,get_leaf
 from sfa.util.table import SfaTable
 from sfa.util.record import SfaRecord
@@ -24,11 +20,12 @@ config = Config()
 TrustedR = TrustedRoots(Config.get_trustedroots_dir(config))
 AuthHierarchy = Hierarchy()
 table = SfaTable()
+db = SlabDB()
 if not table.exists():
     table.create()
     
     
-def create_sm_client_record(self):
+def create_sm_client_record():
     """
     Create a user record for the Slicemanager service.
     """
@@ -104,7 +101,8 @@ def import_node(hrn, node):
         hrn = hrn[:64]
 
     node_record = table.find({'type': 'node', 'hrn': hrn})
-    pkey = Keypair(create=True)
+    pkey = Keypair(create=True)        
+    
     urn = hrn_to_urn(hrn, 'node')
     node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
     node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
@@ -113,7 +111,7 @@ def import_node(hrn, node):
     node_record['date_created'] = int(time.mktime(extime.timetuple()))
     existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
     if not existing_records:
-        print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record['hrn'])
+        print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record )
         table.insert(node_record)
     else:
         existing_record = existing_records[0]
@@ -125,10 +123,7 @@ def import_person(authname,person):
     existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
     extime = datetime.datetime.utcnow()
     person['date_created'] = int(time.mktime(extime.timetuple()))
-    if 'savakian' in person['hrn']:
-        person['pkey']="ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwSUkJ+cr3xM47h8lFkIXJoJhg4wHakTaLJmgTXkzvUmQsQeFB2MjUZ6WAelMXj/EFz2+XkK+bcWNXwfbrLptJQ+XwGpPZlu9YV/kzO63ghVrAyEg0+p7Pn1TO9f1ZYg4R6JfP/3qwH1AsE+X3PNpIewsuEIKwd2wUCJDf5RXJTpl39GizcBFemrRqgs0bdqAN/vUT9YvtWn8fCYR5EfJHVXOK8P1KmnbuGZpk7ryz21pDMlgw13+8aYB+LPkxdv5zG54A5c6o9N3zOCblvRFWaNBqathS8y04cOYWPmyu+Q0Xccwi7vM3Ktm8RoJw+raQNwsmneJOm6KXKnjoOQeiQ== savakian@sfa2.grenoble.senslab.info"
 
-        print>>sys.stderr," \r\n \t slab-import : CMOI \r\n \t\t %s\r\n\r\n" %(person)
   
     if not existing_records:
         print>>sys.stderr, " \r\n \t slab-import : person record %s inserted" %(person['hrn'])
@@ -154,17 +149,20 @@ def import_slice(person):
    
     extime = datetime.datetime.utcnow()
     slice_record['date_created'] = int(time.mktime(extime.timetuple()))
-                               
+    #special slice table for Senslab, to store nodes info (OAR)                        
 
     existing_records = table.find({'hrn': slice_record['hrn'], 'type': 'slice'})
     if not existing_records:
         print>>sys.stderr, " \r\n \t slab-import : slice record %s inserted" %(slice_record['hrn'])
         table.insert(slice_record)
+        db.insert_slab_slice(person)
+
     else:
         print>>sys.stderr, " \r\n \t slab-import : slice record %s updated" %(slice_record['hrn'])
         existing_record = existing_records[0]
         slice_record['record_id'] = existing_record['record_id']
-        table.update(slice_record)        
+        table.update(slice_record)
+        db.update_senslab_slice(slice_record)   
         
 def delete_record( hrn, type):
     # delete the record
@@ -173,57 +171,52 @@ def delete_record( hrn, type):
         print>>sys.stderr, " \r\n \t slab-import : record %s deleted" %(record['hrn'])
         table.remove(record)
                 
-def hostname_to_hrn(root_auth,hostname):
-    # keep only the first part of the DNS name
-    #hrn='.'.join( [auth,hostname.split(".")[0] ] )
-    # escape the '.' in the hostname
-    hrn='.'.join( [root_auth,Xrn.escape(hostname)] )
-    return hrn_to_urn(hrn,'node')
+def hostname_to_hrn(root_auth,login_base,hostname):
+    return PlXrn(auth=root_auth,hostname=login_base+'_'+hostname).get_hrn()
+
     
 def main():
 
-    
+    if not db.exists('slice'):
+        db.createtable('slice')
+        
     if not config.SFA_REGISTRY_ENABLED:
         sys.exit(0)
     root_auth = config.SFA_REGISTRY_ROOT_AUTH
     interface_hrn = config.SFA_INTERFACE_HRN
-    print interface_hrn, root_auth
-    
-     # initialize registry db table
-    #table = SfaTable()
-    #if not table.exists():
-       #table.create()
-
-    # create root authority 
-    create_top_level_auth_records(root_auth)
-    if not root_auth == interface_hrn:
-       create_top_level_auth_records(interface_hrn)
-    
-    # create s user record for the slice manager Do we need this?
-    #create_sm_client_record()
-       
-    # create interface records ADDED 18 nov 11 Do we need this?
-
-    create_interface_records()
 
-    # add local root authority's cert  to trusted list ADDED 18 nov 11 Do we need this?
     
-    authority = AuthHierarchy.get_auth_info(interface_hrn)
-    TrustedR.add_gid(authority.get_gid_object())
-
-
-
-     # create dict of all existing sfa records
-     
+    #Get all records in the sfa table   
+    # create dict of all existing sfa records
     existing_records = {}
     existing_hrns = []
     key_ids = []
-    person_keys = {} 
     results = table.find()
+   
     for result in results:
         existing_records[(result['hrn'], result['type'])] = result
         existing_hrns.append(result['hrn'])   
         
+    # create root authority if it doesn't exist
+    if root_auth not in  existing_hrns or \
+    (root_auth, 'authority') not in existing_records:
+        create_top_level_auth_records(root_auth)
+        if not root_auth == interface_hrn:
+            create_top_level_auth_records(interface_hrn)
+    
+        # create s user record for the slice manager Do we need this?
+        create_sm_client_record()
+        
+        # create interface records ADDED 18 nov 11 Do we need this?
+    
+        create_interface_records()
+    
+        # add local root authority's cert  to trusted list ADDED 18 nov 11 Do we need this?
+        
+        authority = AuthHierarchy.get_auth_info(interface_hrn)
+        TrustedR.add_gid(authority.get_gid_object())
+
+
     #Get Senslab nodes 
    
     Driver = SlabDriver(config)
@@ -231,12 +224,12 @@ def main():
     #print "\r\n NODES8DICT ",nodes_dict
     
     ldap_person_list = Driver.GetPersons()
-    
 
-   
         # import node records
     for node in nodes_dict:
-        hrn =  hostname_to_hrn( root_auth, node['hostname'])
+        # Sandrine
+        # A changer pour l utilisation du nouveau OAR de prod, le site etant contenu dans le hostname
+        hrn =  hostname_to_hrn( root_auth,node['site_login_base'], node['hostname'])
         if hrn not in existing_hrns or \
         (hrn, 'node') not in existing_records:
             import_node(hrn, node)
@@ -247,10 +240,8 @@ def main():
             (person['hrn'], 'user') not in existing_records :
             import_person(root_auth,person)
             import_slice(person)
-
-
-
-                                       
+                               
+                                
     # remove stale records    
     system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
 
@@ -267,10 +258,6 @@ def main():
         found = False
         
         if type == 'authority':    
-            #for site in sites_dict:
-               #print "\t type : authority : ", site
-                #site_hrn = interface_hrn + "." + site['login_base']
-                #if site_hrn == record_hrn and site['site_id'] == record['pointer']:
             found = True
             print "\t \t Found :", found
             break