import sys
-from sqlalchemy import create_engine, and_
+from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sfa.util.config import Config
from sfa.util.sfalogging import logger
-from sqlalchemy import Column, Integer, String, DateTime
-from sqlalchemy import Table, Column, MetaData, join, ForeignKey
-import sfa.storage.model as model
-from sfa.storage.model import RegSlice
+from sqlalchemy import Column, Integer, String
+from sqlalchemy import Table, Column, MetaData
from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import relationship, backref
-
from sqlalchemy.dialects import postgresql
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy import String
-from sfa.storage.alchemy import dbsession
#Dict holding the columns names of the table as keys
#and their type, used for creation of the table
-slice_table = {'record_id_user':'integer PRIMARY KEY references X ON DELETE CASCADE ON UPDATE CASCADE','oar_job_id':'integer DEFAULT -1', 'record_id_slice':'integer', 'slice_hrn':'text NOT NULL'}
+slice_table = {'record_id_user':'integer PRIMARY KEY references X ON DELETE \
+CASCADE ON UPDATE CASCADE','oar_job_id':'integer DEFAULT -1', \
+'record_id_slice':'integer', 'slice_hrn':'text NOT NULL'}
#Dict with all the specific senslab tables
tablenames_dict = {'slice_senslab': slice_table}
SlabBase = declarative_base()
-
-
-
class SliceSenslab (SlabBase):
__tablename__ = 'slice_senslab'
#record_id_user = Column(Integer, primary_key=True)
- slice_hrn = Column(String,primary_key=True)
- peer_authority = Column( String,nullable = True)
+ slice_hrn = Column(String, primary_key=True)
+ peer_authority = Column(String, nullable = True)
record_id_slice = Column(Integer)
record_id_user = Column(Integer)
#oar_job_id = Column( Integer,default = -1)
#node_list = Column(postgresql.ARRAY(String), nullable =True)
- def __init__ (self, slice_hrn =None, record_id_slice=None, record_id_user= None,peer_authority=None):
+ def __init__ (self, slice_hrn =None, record_id_slice=None, \
+ record_id_user= None,peer_authority=None):
+ """
+ Defines a row of the slice_senslab table
+ """
if record_id_slice:
self.record_id_slice = record_id_slice
if slice_hrn:
def __repr__(self):
- result="<Record id user =%s, slice hrn=%s, Record id slice =%s ,peer_authority =%s"% \
- (self.record_id_user, self.slice_hrn, self.record_id_slice, self.peer_authority)
+ """Prints the SQLAlchemy record to the format defined
+ by the function.
+ """
+ result="<Record id user =%s, slice hrn=%s, Record id slice =%s , \
+ peer_authority =%s"% (self.record_id_user, self.slice_hrn, \
+ self.record_id_slice, self.peer_authority)
result += ">"
return result
def dump_sqlalchemyobj_to_dict(self):
+ """Transforms a SQLalchemy record object to a python dictionary.
+ Returns the dictionary.
+ """
+
dict = {'slice_hrn':self.slice_hrn,
'peer_authority':self.peer_authority,
'record_id':self.record_id_slice,
'record_id_slice':self.record_id_slice, }
return dict
-
-
-#class PeerSenslab(SlabBase):
- #__tablename__ = 'peer_senslab'
- #peername = Column(String, nullable = False)
- #peerid = Column( Integer,primary_key=True)
-
- #def __init__ (self,peername = None ):
- #if peername:
- #self.peername = peername
-
-
- #def __repr__(self):
- #result="<Peer id =%s, Peer name =%s" % (self.peerid, self.peername)
- #result += ">"
- #return result
class SlabDB:
def __init__(self,config, debug = False):
self.slab_session = None
# the former PostgreSQL.py used the psycopg2 directly and was doing
#self.connection.set_client_encoding("UNICODE")
- # it's unclear how to achieve this in sqlalchemy, nor if it's needed at all
+ # it's unclear how to achieve this in sqlalchemy, nor if it's needed
+ # at all
# http://www.sqlalchemy.org/docs/dialects/postgresql.html#unicode
# we indeed have /var/lib/pgsql/data/postgresql.conf where
# this setting is unset, it might be an angle to tweak that if need be
# try a unix socket first - omitting the hostname does the trick
unix_url = "postgresql+psycopg2://%s:%s@:%s/%s"%\
- (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_PORT,dbname)
- print >>sys.stderr, " \r\n \r\n SLAPOSTGRES INIT unix_url %s" %(unix_url)
+ (config.SFA_DB_USER, config.SFA_DB_PASSWORD, \
+ config.SFA_DB_PORT, dbname)
+
# the TCP fallback method
tcp_url = "postgresql+psycopg2://%s:%s@%s:%s/%s"%\
- (config.SFA_DB_USER,config.SFA_DB_PASSWORD,config.SFA_DB_HOST,config.SFA_DB_PORT,dbname)
+ (config.SFA_DB_USER, config.SFA_DB_PASSWORD, config.SFA_DB_HOST, \
+ config.SFA_DB_PORT, dbname)
for url in [ unix_url, tcp_url ] :
try:
- self.slab_engine = create_engine (url,echo_pool = l_echo_pool, echo = l_echo)
+ self.slab_engine = create_engine (url, echo_pool = \
+ l_echo_pool, echo = l_echo)
self.check()
- self.url=url
+ self.url = url
return
except:
pass
- self.slab_engine=None
- raise Exception,"Could not connect to database"
+ self.slab_engine = None
+ raise Exception, "Could not connect to database"
def session (self):
+ """
+ Creates a SQLalchemy session. Once the session object is created
+ it should be used throughout the code for all the operations on
+ tables for this given database.
+
+ """
if self.slab_session is None:
- Session=sessionmaker ()
- self.slab_session=Session(bind=self.slab_engine)
+ Session = sessionmaker()
+ self.slab_session = Session(bind = self.slab_engine)
return self.slab_session
-
-
-
- #Close connection to database
+
def close(self):
+ """
+ Closes connection to database.
+
+ """
if self.connection is not None:
self.connection.close()
self.connection = None
-
-
-
+
def exists(self, tablename):
"""
Checks if the table specified as tablename exists.
return True
except NoSuchTableError:
- print>>sys.stderr, " \r\n \r\n \t SLABPOSTGRES EXISTS NOPE! tablename %s " %(tablename)
+ logger.log_exc("SLABPOSTGRES tablename %s does not exists" \
+ %(tablename))
return False
def createtable(self, tablename ):
"""
- Creates the specifed table. Uses the global dictionnary holding the tablenames and
- the table schema.
+ Creates the specifed table. Uses the global dictionnary holding the
+ tablenames and the table schema.
"""
- print>>sys.stderr, " \r\n \r\n \t SLABPOSTGRES createtable SlabBase.metadata.sorted_tables %s \r\n engine %s" %(SlabBase.metadata.sorted_tables , slab_engine)
+ logger.debug("SLABPOSTGRES createtable SlabBase.metadata.sorted_tables \
+ %s \r\n engine %s" %(SlabBase.metadata.sorted_tables , slab_engine))
SlabBase.metadata.create_all(slab_engine)
return
-
-
-
-
from sfa.util.config import Config
class SlabSlices:
rspec_to_slice_tag = {'max_rate':'net_max_rate'}
-
- #def __init__(self, api, ttl = .5, origin_hrn=None):
- #self.api = api
- ##filepath = path + os.sep + filename
- #self.policy = Policy(self.api)
- #self.origin_hrn = origin_hrn
- #self.registry = api.registries[api.hrn]
- #self.credential = api.getCredential()
- #self.nodes = []
- #self.persons = []
-
-
+
+
def __init__(self, driver):
self.driver = driver
#First get the list of current leases from OAR
leases = self.driver.GetLeases({'name':sfa_slice['name']})
- #leases = self.driver.GetLeases({'name':sfa_slice['name']}, ['lease_id'])
+ #leases = self.driver.GetLeases({'name':sfa_slice['name']},\
+ #['lease_id'])
if leases :
current_leases = [lease['lease_id'] for lease in leases]
#Deleted leases are the ones with lease id not declared in the Rspec
try:
if peer:
#peer = RegAuyhority object is unsubscriptable
- #TODO :UnBindObjectFromPeer Quick and dirty auth='senslab2 SA 27/07/12
- self.driver.UnBindObjectFromPeer('senslab2', 'slice', \
- sfa_slice['record_id_slice'], peer.hrn)
+ #TODO :UnBindObjectFromPeer Quick and dirty
+ #auth='senslab2 SA 27/07/12
+
+ #Commented out UnBindObjectFromPeer SA 09/10/12
+ #self.driver.UnBindObjectFromPeer('senslab2', 'slice', \
+ #sfa_slice['record_id_slice'], peer.hrn)
self.driver.DeleteLeases(deleted_leases, \
sfa_slice['name'])
- #TODO : catch other exception?
+ #TODO verify_slice_leases: catch other exception?
except KeyError:
- logger.log_exc('Failed to add/remove slice leases')
+ logger.log_exc('Failed to remove slice leases')
#Add new leases
for start_time in requested_jobs_dict:
deleted_nodes = list(set(current_slivers).\
difference(requested_slivers))
# add nodes from rspec
- #added_nodes = list(set(requested_slivers).difference(current_slivers))
+ #added_nodes = list(set(requested_slivers).\
+ #difference(current_slivers))
#Update the table with the nodes that populate the slice
logger.debug("SLABSLICES \tverify_slice_nodes slice %s\
for sl in slices_list:
logger.debug("SLABSLICE \tverify_slice slicename %s sl %s \
- slice_record %s"%(slicename, sl, slice_record))
+ slice_record %s"%(slicename, sl, \
+ slice_record))
sfa_slice = sl
sfa_slice.update(slice_record)
#del slice['last_updated']
#slice['peer_slice_id'] = slice_record.get('slice_id', None)
## unbind from peer so we can modify if necessary.
## Will bind back later
- #self.driver.UnBindObjectFromPeer('slice', slice['slice_id'], \
- #peer['shortname'])
+ #self.driver.UnBindObjectFromPeer('slice', \
+ #slice['slice_id'], \
+ #peer['shortname'])
#Update existing record (e.g. expires field)
#it with the latest info.
##if slice_record and slice['expires'] != slice_record['expires']:
ldap_reslt = self.driver.ldap.LdapSearch(req)
if ldap_reslt:
logger.debug(" SLABSLICE.PY \tverify_person users \
- USER already in Senslab \t ldap_reslt %s "%( ldap_reslt))
+ USER already in Senslab \t ldap_reslt %s \
+ "%( ldap_reslt))
existing_users.append(ldap_reslt[1])
else:
#User not existing in LDAP
- #TODO SA 21/08/12 raise something to add user or add it auto ?
+ #TODO SA 21/08/12 raise smthg to add user or add it auto ?
logger.debug(" SLABSLICE.PY \tverify_person users \
- not in ldap ...NEW ACCOUNT NEEDED %s \r\n \t ldap_reslt %s " \
- %(users, ldap_reslt))
+ not in ldap ...NEW ACCOUNT NEEDED %s \r\n \t \
+ ldap_reslt %s " %(users, ldap_reslt))
requested_user_ids = users_by_id.keys()
requested_user_hrns = users_by_hrn.keys()