X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fsenslab%2Fslab-import.py;h=38be563d809bb317fd7085d86dce25f73463bcf0;hb=1b8cb9daaab90f3e7cad7466741a44bf71ea869f;hp=9d177575a5e2bcd772a6486e6af356ea48574365;hpb=1896c4d204a0088202cba4300ffdeef640b69121;p=sfa.git diff --git a/sfa/senslab/slab-import.py b/sfa/senslab/slab-import.py index 9d177575..38be563d 100644 --- a/sfa/senslab/slab-import.py +++ b/sfa/senslab/slab-import.py @@ -6,16 +6,25 @@ # ########################################################################### 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.util.config import Config -from sfa.util.xrn import hrn_to_urn, get_authority +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 from sfa.trust.hierarchy import Hierarchy +from sfa.trust.certificate import Keypair,convert_public_key +from sfa.trust.gid import create_uuid + AuthHierarchy = Hierarchy() table = SfaTable() - +if not table.exists(): + table.create() + def create_top_level_auth_records(hrn): """ Create top level records (includes root and sub authorities (local/remote) @@ -42,6 +51,7 @@ def create_top_level_auth_records(hrn): if not auth_record: auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1) auth_record['authority'] = get_authority(auth_record['hrn']) + print sys.stderr, " \r\n \t slab-import : auth record %s inserted" %(auth_record['hrn']) table.insert(auth_record) print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority'] @@ -58,33 +68,72 @@ def import_node(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']) node_record['authority'] = get_authority(node_record['hrn']) + extime = datetime.datetime.utcnow() + 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']) table.insert(node_record) else: existing_record = existing_records[0] node_record['record_id'] = existing_record['record_id'] table.update(node_record) - -def import_person(person): +# person is already a sfa record +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 not existing_records: - table.insert(person_record) + print>>sys.stderr, " \r\n \t slab-import : person record %s inserted" %(person['hrn']) + uuid=create_uuid() + RSA_KEY_STRING=person['pkey'] + pkey=convert_public_key(RSA_KEY_STRING) + person['gid']=AuthHierarchy.create_gid("urn:publicid:IDN+"+authname+"+user+"+person['uid'], uuid, pkey, CA=False).save_to_string() + table.insert(person) else: existing_record = existing_records[0] person['record_id'] = existing_record['record_id'] - table.update(person_record) - + # handle key change ??? + table.update(person) +def import_slice(person): + + hrn = person['hrn']+'_slice' + pkey = Keypair(create=True) + urn = hrn_to_urn(hrn, 'slice') + gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey) + slice_record= SfaRecord(hrn=hrn, gid=gid, type="slice", pointer=-1) + slice_record['authority'] = get_authority(slice_record['hrn']) + + extime = datetime.datetime.utcnow() + slice_record['date_created'] = int(time.mktime(extime.timetuple())) + + print>>sys.stderr, " \r\n \t slab-import : slice record %s " %(slice_record['hrn']) + 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) + 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) def delete_record( hrn, type): # delete the record record_list = table.find({'type': type, 'hrn': hrn}) for record in record_list: + 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 main(): config = Config() @@ -95,12 +144,12 @@ def main(): print interface_hrn, root_auth # initialize registry db table - table = SfaTable() - if not table.exists(): - table.create() + #table = SfaTable() + #if not table.exists(): + #table.create() # create root authority - SenslabImporter.create_top_level_auth_records(root_auth) + create_top_level_auth_records(root_auth) # create s user record for the slice manager #Do we need this? @@ -121,16 +170,15 @@ def main(): existing_records[(result['hrn'], result['type'])] = result existing_hrns.append(result['hrn']) - #Get Senslab nodes - nodes_dict = OARImporter.GetNodes() - print "\r\n NODES8DICT ",nodes_dict + #Get Senslab nodes + + Driver = SlabDriver(config) + nodes_dict = Driver.GetNodes() + #print "\r\n NODES8DICT ",nodes_dict - persons_list = SenslabUsers.GetPersons() - print "\r\n PERSONS_LIST ",persons_list - - keys_list = SenslabUsers.GetKeys() - print "\r\n KEYSS_LIST ",keys_list + ldap_person_list = Driver.GetPersons() + #slices_list = SenslabUsers.GetSlices() #print "\r\n SLICES_LIST ",slices_list @@ -149,32 +197,21 @@ def main(): #(site_hrn, 'authority') not in existing_records: #print "SITE HRN UNKNOWN" , site, site_hrn #site_hrn = SenslabImporter.import_site(interface_hrn, site) - - print "\r\n \r\n ===========IMPORT NODE_RECORDS ==========\r\n site %s \r\n \t nodes_dict %s" %(site,nodes_dict) + # import node records - #for node_id in site['node_ids']: - #for[node['node_id'] for node in nodes_dict]: - #print '\r\n \t **NODE_ID %s node %s '%( node_id, node) - #continue for node in nodes_dict: - #if node_id is node['node_id']: - #node = nodes_dict[node_id] - print '\r\n \t NODE_ID %s node %s '%( node_id, node) - hrn = hostname_to_hrn(interface_hrn, root_auth, node['hostname']) - break - - if hrn not in existing_hrns or \ - (hrn, 'node') not in existing_records: - print "\t\t NODE HRN NOT in existing records!" ,hrn - import_node(hrn, node) + hrn = hostname_to_hrn( root_auth, node['hostname']) + if hrn not in existing_hrns or \ + (hrn, 'node') not in existing_records: + import_node(hrn, node) # import persons - for person in persons_list: - print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^PERSON hrn %s person %s site hrn %s" %(hrn,person) - SenslabImporter.import_person( site_hrn, person,keys_list) - if hrn not in existing_hrns or \ - (hrn, 'user') not in existing_records or update_record: - import_person(site_hrn, person) + for person in ldap_person_list: + if person['hrn'] not in existing_hrns or \ + (person['hrn'], 'user') not in existing_records : + import_person(root_auth,person) + import_slice(person) + # import slices #for slice_id in site['slice_ids']: #print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id %s " %(slice_id) @@ -190,6 +227,7 @@ def main(): # remove stale records system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager'] + for (record_hrn, type) in existing_records.keys(): if record_hrn in system_records: continue @@ -212,27 +250,31 @@ def main(): break elif type == 'user': - for person in persons: + for person in ldap_person_list: if person['hrn'] == record_hrn: found = True - break + break elif type == 'node': login_base = get_leaf(get_authority(record_hrn)) nodename = Xrn.unescape(get_leaf(record_hrn)) - print "type: node : nodename %s" %(nodename) - for node in nodes_dict.values(): + for node in nodes_dict: if node['hostname'] == nodename : found = True break - + + elif type == 'slice': + for person in ldap_person_list: + if person['hrn']+'_slice' == record_hrn: + found = True + break else: continue if not found: record_object = existing_records[(record_hrn, type)] - print "\t\t NOT FOUND ! " + print "\t\t NOT FOUND ! ", record_hrn delete_record(record_hrn, type) if __name__ == "__main__": - main() \ No newline at end of file + main()