From: Sandrine Avakian Date: Thu, 1 Dec 2011 10:25:54 +0000 (+0100) Subject: Added slice table support in slabpostgres.sql. X-Git-Tag: sfa-2.1-24~3^2~264 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=bb5a42bdbed56f777dccf8c15709fb8cefd0d001;p=sfa.git Added slice table support in slabpostgres.sql. Added missing PI and researcher fields for slices by searching through slice table in fill_record_info. Corrected nodes hrn in slab-import. => To be changed with new version of OAR coming. Fixed show command (sfi.py show). --- diff --git a/sfa/senslab/OARrspec.py b/sfa/senslab/OARrspec.py index 00303423..2aa8c030 100644 --- a/sfa/senslab/OARrspec.py +++ b/sfa/senslab/OARrspec.py @@ -15,6 +15,7 @@ from sfa.util.xrn import hrn_to_urn, urn_to_hrn, urn_to_sliver_id from sfa.util.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename from sfa.rspecs.rspec import RSpec +from sfa.rspecs.elements.location import Location from sfa.rspecs.elements.hardware_type import HardwareType from sfa.rspecs.elements.node import Node #from sfa.rspecs.elements.link import Link @@ -155,6 +156,10 @@ class OARrspec: rspec_node['component_manager_id'] = hrn_to_urn(self.driver.root_auth, 'authority+sa') rspec_node['authority_id'] = hrn_to_urn(PlXrn.site_hrn(self.driver.root_auth, node['site_login_base']), 'authority+sa') rspec_node['boot_state'] = node['boot_state'] + if node['posx'] and node['posy']: + location = Location({'longitude':node['posx'], 'latitude': node['posy']}) + rspec_node['location'] = location + rspec_node['exclusive'] = 'True' rspec_node['hardware_types']= [HardwareType({'name': 'senslab sensor node'})] # only doing this because protogeni rspec needs diff --git a/sfa/senslab/slab-import.py b/sfa/senslab/slab-import.py index 660abc1d..ec99668e 100644 --- a/sfa/senslab/slab-import.py +++ b/sfa/senslab/slab-import.py @@ -1,10 +1,4 @@ -########################################################################### -# Copyright (C) 2011 by root -# -# -# Copyright: See COPYING file that comes with this distribution -# -########################################################################### + import sys import datetime import time @@ -13,6 +7,7 @@ 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 @@ -106,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) + print>>sys.stderr, " \r\n \t slab-import : hrn %s" %(hrn ) 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']) @@ -115,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] @@ -159,7 +155,6 @@ def import_slice(person): if not existing_records: print>>sys.stderr, " \r\n \t slab-import : slice record %s inserted" %(slice_record['hrn']) table.insert(slice_record) - #table.insert_slab_slice(person) db.insert_slab_slice(person) else: @@ -176,12 +171,9 @@ 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=auth,hostname=login_base+'_'+hostname).get_hrn() + def main(): @@ -235,7 +227,9 @@ def main(): # 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) diff --git a/sfa/senslab/slabdriver.py b/sfa/senslab/slabdriver.py index e0c2d2cc..5a92a590 100644 --- a/sfa/senslab/slabdriver.py +++ b/sfa/senslab/slabdriver.py @@ -17,6 +17,7 @@ from sfa.senslab.OARrestapi import OARapi from sfa.senslab.LDAPapi import LDAPapi from sfa.senslab.SenslabImportUsers import SenslabImportUsers from sfa.senslab.parsing import parse_filter +from sfa.senslab.slabpostgres import SlabDB def list_to_dict(recs, key): """ @@ -291,8 +292,7 @@ class SlabDriver (): def startswith(prefix, values): return [value for value in values if value.startswith(prefix)] - - SenslabUsers = SenslabImportUsers() + # get person ids person_ids = [] site_ids = [] @@ -405,13 +405,32 @@ class SlabDriver (): def fill_record_info(self, records): """ - Given a SFA record, fill in the PLC specific and SFA specific + Given a SFA record, fill in the senslab specific and SFA specific fields in the record. """ - if not isinstance(records, list): - records = [records] + if isinstance(records, list): + records = records[0] print >>sys.stderr, "\r\n \t\t BEFORE fill_record_pl_info %s" %(records) + + + if records['type'] == 'slice': + db = SlabDB() + sfatable = SfaTable() + recslice = db.find('slice',str(records['hrn'])) + if isinstance(recslice,list) and len(recslice) == 1: + recslice = recslice[0] + recuser = sfatable.find( recslice['record_id_user'], ['hrn']) + + print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info %s" %(recuser) + records['type'] + if isinstance(recuser,list) and len(recuser) == 1: + recuser = recuser[0] + records.update({'PI':[recuser['hrn']], + 'researcher': [recuser['hrn']], + 'name':records['hrn'], 'oar_job_id':recslice['oar_job_id'], + 'person_ids':[recslice['record_id_user']]}) + #self.fill_record_pl_info(records) ##print >>sys.stderr, "\r\n \t\t after fill_record_pl_info %s" %(records) #self.fill_record_sfa_info(records) @@ -458,6 +477,7 @@ class SlabDriver (): delFunc(self.plauth, personId, containerId) def update_membership(self, oldRecord, record): + print >>sys.stderr, " \r\n \r\n ***SLABDRIVER.PY update_membership record ", record if record.type == "slice": self.update_membership_list(oldRecord, record, 'researcher', self.users.AddPersonToSlice, diff --git a/sfa/senslab/slabpostgres.py b/sfa/senslab/slabpostgres.py index fbfeb22f..f24d0b8a 100644 --- a/sfa/senslab/slabpostgres.py +++ b/sfa/senslab/slabpostgres.py @@ -49,9 +49,6 @@ class SlabDB: host = "%s:%d" % (self.config.SFA_PLC_DB_HOST, self.config.SFA_PLC_DB_PORT), database = self.config.SFA_PLC_DB_NAME) - (self.rowcount, self.description, self.lastrowid) = \ - (None, None, None) - return self.connection.cursor() #Close connection to database @@ -60,19 +57,47 @@ class SlabDB: self.connection.close() self.connection = None + def selectall(self, query, hashref = True, key_field = None): + """ + Return each row as a dictionary keyed on field name (like DBI + selectrow_hashref()). If key_field is specified, return rows + as a dictionary keyed on the specified field (like DBI + selectall_hashref()). + + """ + cursor = self.cursor() + cursor.execute(query) + rows = cursor.fetchall() + cursor.close() + self.connection.commit() + + if hashref or key_field is not None: + # Return each row as a dictionary keyed on field name + # (like DBI selectrow_hashref()). + labels = [column[0] for column in cursor.description] + rows = [dict(zip(labels, row)) for row in rows] + + if key_field is not None and key_field in labels: + # Return rows as a dictionary keyed on the specified field + # (like DBI selectall_hashref()). + return dict([(row[key_field], row) for row in rows]) + else: + return rows + + def exists(self, tablename): """ Checks if the table specified as tablename exists. """ - mark = self.cursor() + #mark = self.cursor() sql = "SELECT * from pg_tables" - mark.execute(sql) - rows = mark.fetchall() - mark.close() - labels = [column[0] for column in mark.description] - rows = [dict(zip(labels, row)) for row in rows] - + #mark.execute(sql) + #rows = mark.fetchall() + #mark.close() + #labels = [column[0] for column in mark.description] + #rows = [dict(zip(labels, row)) for row in rows] + rows = self.selectall(sql) rows = filter(lambda row: row['tablename'].startswith(tablename), rows) if rows: return True @@ -207,4 +232,42 @@ class SlabDB: return + def find(self, tablename,record_filter = None, columns=None): + if not columns: + columns = "*" + else: + columns = ",".join(columns) + sql = "SELECT %s FROM %s WHERE True " % (columns, tablename) + + #if isinstance(record_filter, (list, tuple, set)): + #ints = filter(lambda x: isinstance(x, (int, long)), record_filter) + #strs = filter(lambda x: isinstance(x, StringTypes), record_filter) + #record_filter = Filter(SfaRecord.all_fields, {'record_id': ints, 'hrn': strs}) + #sql += "AND (%s) %s " % record_filter.sql("OR") + #elif isinstance(record_filter, dict): + #record_filter = Filter(SfaRecord.all_fields, record_filter) + #sql += " AND (%s) %s" % record_filter.sql("AND") + #elif isinstance(record_filter, StringTypes): + #record_filter = Filter(SfaRecord.all_fields, {'hrn':[record_filter]}) + #sql += " AND (%s) %s" % record_filter.sql("AND") + #elif isinstance(record_filter, int): + #record_filter = Filter(SfaRecord.all_fields, {'record_id':[record_filter]}) + #sql += " AND (%s) %s" % record_filter.sql("AND") + + if isinstance(record_filter, dict): + for k in record_filter.keys(): + sql += "AND "+' \''+ str(k) + '\''+ '='+' \''+ str(record_filter[k])+'\'' + + elif isinstance(record_filter, str): + sql += "AND slice_hrn ="+ ' \''+record_filter+'\'' + + #elif isinstance(record_filter, int): + #record_filter = Filter(SfaRecord.all_fields, {'record_id':[record_filter]}) + #sql += " AND (%s) %s" % record_filter.sql("AND") + sql += ";" + print>>sys.stderr, " \r\n \r\n \t SLABPOSTGRES.PY find : sql %s record_filter %s %s" %(sql, record_filter , type(record_filter)) + results = self.selectall(sql) + if isinstance(results, dict): + results = [results] + return results