from time import gmtime, strftime
from sfa.util.faults import MissingSfaInfo , SliverDoesNotExist
-#from sfa.util.sfatime import datetime_to_string
from sfa.util.sfalogging import logger
-from sfa.storage.table import SfaTable
from sfa.util.defaultdict import defaultdict
+from sfa.storage.record import Record
+from sfa.storage.alchemy import dbsession
+from sfa.storage.model import RegRecord
+
+
from sfa.trust.certificate import *
from sfa.trust.credential import *
from sfa.trust.gid import GID
from sfa.rspecs.rspec import RSpec
from sfa.util.xrn import hrn_to_urn, urn_to_sliver_id
-from sfa.util.plxrn import slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename, hrn_to_pl_login_base
+from sfa.util.plxrn import slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename
## thierry: everything that is API-related (i.e. handling incoming requests)
# is taken care of
## thierry : please avoid wildcard imports :)
from sfa.senslab.OARrestapi import OARrestapi
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
+from sfa.senslab.slabpostgres import SlabDB, slab_dbsession,SliceSenslab
from sfa.senslab.slabaggregate import SlabAggregate
from sfa.senslab.slabslices import SlabSlices
#self.oar = OARapi()
self.oar = OARrestapi()
self.ldap = LDAPapi()
- self.users = SenslabImportUsers()
+ #self.users = SenslabImportUsers()
self.time_format = "%Y-%m-%d %H:%M:%S"
- self.db = SlabDB()
+ self.db = SlabDB(config)
#self.logger=sfa_logger()
self.cache=None
# shall return a structure as described in
# http://groups.geni.net/geni/wiki/GAPI_AM_API_V2#SliverStatus
# NT : not sure if we should implement this or not, but used by sface.
- slices = self.GetSlices([slice_hrn])
+
+
+ slices = self.GetSlices(slice_filter= slice_hrn, filter_type = 'slice_hrn')
if len(slices) is 0:
raise SliverDoesNotExist("%s slice_hrn" % (slice_hrn))
sl = slices[0]
if nodes:
top_level_status = 'ready'
result['geni_urn'] = slice_urn
- result['pl_login'] = sl['job_user']
+ result['slab_login'] = sl['job_user']
timestamp = float(sl['startTime']) + float(sl['walltime'])
- result['pl_expires'] = strftime(self.time_format, gmtime(float(timestamp)))
+ result['slab_expires'] = strftime(self.time_format, gmtime(float(timestamp)))
resources = []
for node in nodes:
res = {}
- res['pl_hostname'] = node['hostname']
- res['pl_boot_state'] = node['boot_state']
+ res['slab_hostname'] = node['hostname']
+ res['slab_boot_state'] = node['boot_state']
sliver_id = urn_to_sliver_id(slice_urn, sl['record_id_slice'], node['node_id'])
res['geni_urn'] = sliver_id
def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
aggregate = SlabAggregate(self)
- #aggregate = SlabAggregate(self)
+
slices = SlabSlices(self)
peer = slices.get_peer(slice_hrn)
sfa_peer = slices.get_sfa_peer(slice_hrn)
slice_record=None
- #print>>sys.stderr, " \r\n \r\n create_sliver creds %s \r\n \r\n users %s " %(creds,users)
+
if not isinstance(creds, list):
creds = [creds]
- #for cred in creds:
- #cred_obj=Credential(string=cred)
- #print >>sys.stderr," \r\n \r\n create_sliver cred %s " %(cred)
- #GIDcall = cred_obj.get_gid_caller()
- #GIDobj = cred_obj.get_gid_object()
- #print >>sys.stderr," \r\n \r\n create_sliver GIDobj pubkey %s hrn %s " %(GIDobj.get_pubkey().get_pubkey_string(), GIDobj.get_hrn())
- #print >>sys.stderr," \r\n \r\n create_sliver GIDcall pubkey %s hrn %s" %(GIDcall.get_pubkey().get_pubkey_string(),GIDobj.get_hrn())
-
-
- #tmpcert = GID(string = users[0]['gid'])
- #print >>sys.stderr," \r\n \r\n create_sliver tmpcer pubkey %s hrn %s " %(tmpcert.get_pubkey().get_pubkey_string(), tmpcert.get_hrn())
if users:
slice_record = users[0].get('slice_record', {})
def delete_sliver (self, slice_urn, slice_hrn, creds, options):
- slices = self.GetSlices({'slice_hrn': slice_hrn})
+ slices = self.GetSlices(slice_filter= slice_hrn, filter_type = 'slice_hrn')
if not slices:
return 1
slice = slices[0]
#No site or node register supported
def register (self, sfa_record, hrn, pub_key):
type = sfa_record['type']
- pl_record = self.sfa_fields_to_pl_fields(type, hrn, sfa_record)
+ slab_record = self.sfa_fields_to_slab_fields(type, hrn, sfa_record)
#if type == 'authority':
- #sites = self.shell.GetSites([pl_record['login_base']])
+ #sites = self.shell.GetSites([slab_record['login_base']])
#if not sites:
- #pointer = self.shell.AddSite(pl_record)
+ #pointer = self.shell.AddSite(slab_record)
#else:
#pointer = sites[0]['site_id']
if type == 'slice':
acceptable_fields=['url', 'instantiation', 'name', 'description']
- for key in pl_record.keys():
+ for key in slab_record.keys():
if key not in acceptable_fields:
- pl_record.pop(key)
+ slab_record.pop(key)
print>>sys.stderr, " \r\n \t\t SLABDRIVER.PY register"
- slices = self.GetSlices([pl_record['hrn']])
+ slices = self.GetSlices(slice_filter =slab_record['hrn'], filter_type = 'slice_hrn')
if not slices:
- pointer = self.AddSlice(pl_record)
+ pointer = self.AddSlice(slab_record)
else:
pointer = slices[0]['slice_id']
#No node adding outside OAR
#elif type == 'node':
- #login_base = hrn_to_pl_login_base(sfa_record['authority'])
- #nodes = self.GetNodes([pl_record['hostname']])
+ #login_base = hrn_to_slab_login_base(sfa_record['authority'])
+ #nodes = self.GetNodes([slab_record['hostname']])
#if not nodes:
- #pointer = self.AddNode(login_base, pl_record)
+ #pointer = self.AddNode(login_base, slab_record)
#else:
#pointer = nodes[0]['node_id']
#self.shell.UpdateSite(pointer, new_sfa_record)
if type == "slice":
- pl_record=self.sfa_fields_to_pl_fields(type, hrn, new_sfa_record)
- if 'name' in pl_record:
- pl_record.pop('name')
- self.UpdateSlice(pointer, pl_record)
+ slab_record=self.sfa_fields_to_slab_fields(type, hrn, new_sfa_record)
+ if 'name' in slab_record:
+ slab_record.pop('name')
+ self.UpdateSlice(pointer, slab_record)
elif type == "user":
update_fields = {}
if persons and persons[0]['site_ids']:
self.DeletePerson(username)
elif type == 'slice':
- if self.GetSlices(hrn):
+ if self.GetSlices(slice_filter = hrn, filter_type = 'slice_hrn'):
self.DeleteSlice(hrn)
#elif type == 'authority':
return True
def GetPeers (self,auth = None, peer_filter=None, return_fields=None):
- table = SfaTable()
- return_records = []
- records_list = table.findObjects({'type':'authority+sa'})
+
+ existing_records = {}
+ existing_hrns_by_types= {}
+ print >>sys.stderr, "\r\n \r\n SLABDRIVER GetPeers auth = %s, peer_filter %s, return_field %s " %(auth , peer_filter, return_fields)
+ all_records = dbsession.query(RegRecord).filter(RegRecord.type.like('%authority%')).all()
+ for record in all_records:
+ existing_records[record.hrn] = record
+ if record.type not in existing_hrns_by_types:
+ existing_hrns_by_types[record.type] = [record.hrn]
+ print >>sys.stderr, "\r\n \r\n SLABDRIVER GetPeers \t NOT IN existing_hrns_by_types %s " %( existing_hrns_by_types)
+ else:
+
+ print >>sys.stderr, "\r\n \r\n SLABDRIVER GetPeers \t INNN type %s hrn %s " %( record.type,record.hrn )
+ existing_hrns_by_types.update({record.type:(existing_hrns_by_types[record.type].append(record.hrn))})
+
+ print >>sys.stderr, "\r\n \r\n SLABDRIVER GetPeers existing_hrns_by_types %s " %( existing_hrns_by_types)
+ records_list= []
+
+ try:
+ for hrn in existing_hrns_by_types['authority+sa']:
+ records_list.append(existing_records[hrn])
+ print >>sys.stderr, "\r\n \r\n SLABDRIVER GetPeers records_list %s " %(records_list)
+
+ except:
+ pass
+
if not peer_filter and not return_fields:
return records_list
return_records = parse_filter(records_list,peer_filter, 'peers', return_fields)
return_site_list = parse_filter(site_dict.values(), site_filter,'site', return_fields)
return return_site_list
-
- def GetSlices(self,slice_filter = None, return_fields=None):
- sliceslist = self.db.find('slice_senslab',columns = ['oar_job_id', 'slice_hrn', 'record_id_slice','record_id_user'], record_filter=slice_filter)
-
- print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices slices %s slice_filter %s " %(sliceslist,slice_filter)
-
- return_slice_list = parse_filter(sliceslist, slice_filter,'slice', return_fields)
-
- if return_slice_list:
- for sl in return_slice_list:
- login = sl['slice_hrn'].split(".")[1].split("_")[0]
- print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices sl %s " %(sl)
- if sl['oar_job_id'] is not -1:
- rslt = self.GetJobs( sl['oar_job_id'],resources=False, username = login )
+ def GetSlices(self,slice_filter = None, filter_type = None, return_fields=None):
+ return_slice_list = []
+ slicerec = {}
+ ftypes = ['slice_hrn', 'record_id_user']
+ if filter_type and filter_type in ftypes:
+ if filter_type == 'slice_hrn':
+ slicerec = slab_dbsession.query(SliceSenslab).filter_by(slice_hrn = slice_filter).first()
+ if filter_type == 'record_id_user':
+ slicerec = slab_dbsession.query(SliceSenslab).filter_by(record_id_user = slice_filter).first()
+ if slicerec:
+ rec = slicerec.dumpquerytodict()
+ login = slicerec.slice_hrn.split(".")[1].split("_")[0]
+ print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY slicerec GetSlices %s " %(slicerec)
+ if slicerec.oar_job_id is not -1:
+ rslt = self.GetJobs( slicerec.oar_job_id, resources=False, username = login )
print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices GetJobs %s " %(rslt)
if rslt :
- sl.update(rslt)
- sl.update({'hrn':str(sl['slice_hrn'])})
- #If GetJobs is empty, this means the job is now in the 'Terminated' state
- #Update the slice record
+ rec.update(rslt)
+ rec.update({'hrn':str(rec['slice_hrn'])})
+ #If GetJobs is empty, this means the job is now in the 'Terminated' state
+ #Update the slice record
else :
- sl['oar_job_id'] = '-1'
- sl.update({'hrn':str(sl['slice_hrn'])})
- self.db.update_senslab_slice(sl)
+ self.db.update_job(slice_filter, job_id = '-1')
+ rec['oar_job_id'] = '-1'
+ rec.update({'hrn':str(rec['slice_hrn'])})
- print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices return_slice_list %s" %(return_slice_list)
- return return_slice_list
+ print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices rec %s" %(rec)
+ return rec
+
+
+ else:
+ return_slice_list = slab_dbsession.query(SliceSenslab).all()
+
+ print >>sys.stderr, " \r\n \r\n \tSLABDRIVER.PY GetSlices slices %s slice_filter %s " %(return_slice_list,slice_filter)
+
+ #if return_fields:
+ #return_slice_list = parse_filter(sliceslist, slice_filter,'slice', return_fields)
+
+
+
+ return return_slice_list
+
# @param type type of record (user, slice, ...)
# @param hrn human readable name
# @param sfa_fields dictionary of SFA fields
- # @param pl_fields dictionary of PLC fields (output)
+ # @param slab_fields dictionary of PLC fields (output)
- def sfa_fields_to_pl_fields(self, type, hrn, record):
+ def sfa_fields_to_slab_fields(self, type, hrn, record):
def convert_ints(tmpdict, int_fields):
for field in int_fields:
if field in tmpdict:
tmpdict[field] = int(tmpdict[field])
- pl_record = {}
+ slab_record = {}
#for field in record:
- # pl_record[field] = record[field]
+ # slab_record[field] = record[field]
if type == "slice":
#instantion used in get_slivers ?
- if not "instantiation" in pl_record:
- pl_record["instantiation"] = "senslab-instantiated"
- pl_record["hrn"] = hrn_to_pl_slicename(hrn)
+ if not "instantiation" in slab_record:
+ slab_record["instantiation"] = "senslab-instantiated"
+ slab_record["hrn"] = hrn_to_pl_slicename(hrn)
+ print >>sys.stderr, "\r\n \r\n \t SLABDRIVER.PY sfa_fields_to_slab_fields slab_record %s hrn_to_pl_slicename(hrn) hrn %s " %(slab_record['hrn'], hrn)
if "url" in record:
- pl_record["url"] = record["url"]
+ slab_record["url"] = record["url"]
if "description" in record:
- pl_record["description"] = record["description"]
+ slab_record["description"] = record["description"]
if "expires" in record:
- pl_record["expires"] = int(record["expires"])
+ slab_record["expires"] = int(record["expires"])
#nodes added by OAR only and then imported to SFA
#elif type == "node":
- #if not "hostname" in pl_record:
+ #if not "hostname" in slab_record:
#if not "hostname" in record:
#raise MissingSfaInfo("hostname")
- #pl_record["hostname"] = record["hostname"]
- #if not "model" in pl_record:
- #pl_record["model"] = "geni"
+ #slab_record["hostname"] = record["hostname"]
+ #if not "model" in slab_record:
+ #slab_record["model"] = "geni"
#One authority only
#elif type == "authority":
- #pl_record["login_base"] = hrn_to_pl_login_base(hrn)
+ #slab_record["login_base"] = hrn_to_slab_login_base(hrn)
- #if not "name" in pl_record:
- #pl_record["name"] = hrn
+ #if not "name" in slab_record:
+ #slab_record["name"] = hrn
- #if not "abbreviated_name" in pl_record:
- #pl_record["abbreviated_name"] = hrn
+ #if not "abbreviated_name" in slab_record:
+ #slab_record["abbreviated_name"] = hrn
- #if not "enabled" in pl_record:
- #pl_record["enabled"] = True
+ #if not "enabled" in slab_record:
+ #slab_record["enabled"] = True
- #if not "is_public" in pl_record:
- #pl_record["is_public"] = True
+ #if not "is_public" in slab_record:
+ #slab_record["is_public"] = True
- return pl_record
+ return slab_record
#OAR = OARrestapi()
answer = self.oar.POSTRequestToOARRestAPI('POST_job',reqdict,slice_user)
print>>sys.stderr, "\r\n \r\n AddSliceToNodes jobid %s " %(answer)
- self.db.update('slice',['oar_job_id'], [answer['id']], 'slice_hrn', slice_name)
-
+ #self.db.update('slice',['oar_job_id'], [answer['id']], 'slice_hrn', slice_name)
+
+
+ self.db.update_job( slice_name, job_id = answer['id'] )
jobid=answer['id']
print>>sys.stderr, "\r\n \r\n AddSliceToNodes jobid %s added_nodes %s slice_user %s" %(jobid,added_nodes,slice_user)
# second step : configure the experiment
# we obtain
# get the sfa records
- table = SfaTable()
+ #table = SfaTable()
+ existing_records = {}
+ all_records = dbsession.query(RegRecord).all()
+ for record in all_records:
+ existing_records[(record.type,record.pointer)] = record
+
+ print >>sys.stderr, " \r\r\n SLABDRIVER fill_record_sfa_info existing_records %s " %(existing_records)
person_list, persons = [], {}
- person_list = table.find({'type': 'user', 'pointer': person_ids})
+ #person_list = table.find({'type': 'user', 'pointer': person_ids})
+ try:
+ for p_id in person_ids:
+ person_list.append( existing_records.get(('user',p_id)))
+ except KeyError:
+ print >>sys.stderr, " \r\r\n SLABDRIVER fill_record_sfa_info ERRRRRRRRRROR"
+
# create a hrns keyed on the sfa record's pointer.
# Its possible for multiple records to have the same pointer so
# the dict's value will be a list of hrns.
persons[person['pointer']].append(person)
# get the pl records
- pl_person_list, pl_persons = [], {}
- pl_person_list = self.GetPersons(person_ids, ['person_id', 'roles'])
- pl_persons = list_to_dict(pl_person_list, 'person_id')
- #print>>sys.stderr, "\r\n \r\n _fill_record_sfa_info ___ _list %s \r\n \t\t SenslabUsers.GetPersons ['person_id', 'roles'] pl_persons %s \r\n records %s" %(pl_person_list, pl_persons,records)
+ slab_person_list, slab_persons = [], {}
+ slab_person_list = self.GetPersons(person_ids, ['person_id', 'roles'])
+ slab_persons = list_to_dict(slab_person_list, 'person_id')
+ #print>>sys.stderr, "\r\n \r\n _fill_record_sfa_info ___ _list %s \r\n \t\t SenslabUsers.GetPersons ['person_id', 'roles'] slab_persons %s \r\n records %s" %(slab_person_list, slab_persons,records)
# fill sfa info
for record in records:
record['researcher'].extend(hrns)
# pis at the slice's site
- pl_pis = site_pis[record['site_id']]
- pi_ids = [pi['person_id'] for pi in pl_pis]
+ slab_pis = site_pis[record['site_id']]
+ pi_ids = [pi['person_id'] for pi in slab_pis]
for person_id in pi_ids:
hrns = [person['hrn'] for person in persons[person_id]]
record['PI'].extend(hrns)
record['operator'] = []
record['owner'] = []
for pointer in record['person_ids']:
- if pointer not in persons or pointer not in pl_persons:
+ if pointer not in persons or pointer not in slab_persons:
# this means there is not sfa or pl record for this user
continue
hrns = [person['hrn'] for person in persons[pointer]]
- roles = pl_persons[pointer]['roles']
+ roles = slab_persons[pointer]['roles']
if 'pi' in roles:
record['PI'].extend(hrns)
if 'tech' in roles:
Given a SFA record, fill in the senslab specific and SFA specific
fields in the record.
"""
- print >>sys.stderr, "\r\n \t\t BEFORE fill_record_info %s" %(records)
+
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info 000000000 fill_record_info %s " %(records)
if not isinstance(records, list):
records = [records]
- #print >>sys.stderr, "\r\n \t\t BEFORE fill_record_pl_info %s" %(records)
+
parkour = records
try:
for record in parkour:
if str(record['type']) == 'slice':
- print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info record %s" %(record)
- sfatable = SfaTable()
- recslice = self.db.find('slice',str(record['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 \t \t record %s" %(record)
+ #sfatable = SfaTable()
- print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info %s" %(recuser)
+ #existing_records_by_id = {}
+ #all_records = dbsession.query(RegRecord).all()
+ #for rec in all_records:
+ #existing_records_by_id[rec.record_id] = rec
+ #print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info \t\t existing_records_by_id %s" %(existing_records_by_id[record['record_id']])
+
+ #recslice = self.db.find('slice',{'slice_hrn':str(record['hrn'])})
+ #recslice = slab_dbsession.query(SliceSenslab).filter_by(slice_hrn = str(record['hrn'])).first()
+ recslice = self.GetSlices(slice_filter = str(record['hrn']), filter_type = 'slice_hrn')
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info \t\t HOY HOY reclise %s" %(recslice)
+ #if isinstance(recslice,list) and len(recslice) == 1:
+ #recslice = recslice[0]
+
+ recuser = dbsession.query(RegRecord).filter_by(record_id = recslice['record_id_user']).first()
+ #existing_records_by_id[recslice['record_id_user']]
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info \t\t recuser %s" %(recuser)
- if isinstance(recuser,list) and len(recuser) == 1:
- recuser = recuser[0]
- record.update({'PI':[recuser['hrn']],
- 'researcher': [recuser['hrn']],
+
+ record.update({'PI':[recuser.hrn],
+ 'researcher': [recuser.hrn],
'name':record['hrn'],
'oar_job_id':recslice['oar_job_id'],
'node_ids': [],
'person_ids':[recslice['record_id_user']]})
- elif str(record['type']) == 'user':
- recslice = self.db.find('slice', record_filter={'record_id_user':record['record_id']})
- for rec in recslice:
- rec.update({'type':'slice'})
- rec.update({'hrn':rec['slice_hrn'], 'record_id':rec['record_id_slice']})
- records.append(rec)
- print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info ADDING SLIC EINFO recslice %s" %(recslice)
+ elif str(record['type']) == 'user':
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info USEEEEEEEEEERDESU!"
+
+ rec = self.GetSlices(slice_filter = record['record_id'], filter_type = 'record_id_user')
+ #Append record in records list, therfore fetches user and slice info again(one more loop)
+ #Will update PIs and researcher for the slice
+
+ rec.update({'type':'slice','hrn':rec['slice_hrn']})
+ records.append(rec)
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info ADDING SLIC EINFO rec %s" %(rec)
-
+ print >>sys.stderr, "\r\n \t\t SLABDRIVER.PY fill_record_info OKrecords %s" %(records)
except TypeError:
print >>sys.stderr, "\r\n \t\t SLABDRIVER fill_record_info EXCEPTION RECORDS : %s" %(records)
return
- #self.fill_record_pl_info(records)
- ##print >>sys.stderr, "\r\n \t\t after fill_record_pl_info %s" %(records)
+ #self.fill_record_slab_info(records)
+ ##print >>sys.stderr, "\r\n \t\t after fill_record_slab_info %s" %(records)
#self.fill_record_sfa_info(records)
#print >>sys.stderr, "\r\n \t\t after fill_record_sfa_info"
+
+
+
+
#def update_membership_list(self, oldRecord, record, listName, addFunc, delFunc):
## get a list of the HRNs tht are members of the old and new records
#if oldRecord: