Skip to content
Snippets Groups Projects
Commit 81db0fec authored by Jan David Mol's avatar Jan David Mol
Browse files

Task #8887: Use DBCredentials module to provide "qpidinfra" db credentials

parent 8df69e82
No related branches found
No related tags found
No related merge requests found
# $Id: CMakeLists.txt 30355 2014-11-04 13:46:05Z loose $
lofar_package(QPIDInfrastructure 0.1)
lofar_package(QPIDInfrastructure 0.1 DEPENDS PyCommon)
include(PythonInstall)
......
......@@ -3,7 +3,7 @@ QPIDinfra Database
bin:
addtoQPIDDB.py Tool to add information in the database
configQPIDfromDB.py Tool to create a buildscript from the database
cep4_config.sh Script to insert CEP4 config into database
populateDB.sh Script to insert LOFAR config into database
compareQPIDwithDB.py WIP: Tool to read database and compare with QPID brokers
gatherfrombrokers.sh WIP: Tool to retrieve info from brokers and insert in database
route_to_struct.py
......
......@@ -2,6 +2,7 @@
import sys
from lofar.qpidinfrastructure.QPIDDB import qpidinfra
from lofar.common import dbcredentials
from optparse import OptionParser
......@@ -13,13 +14,15 @@ if __name__ == '__main__':
parser.add_option('-q', '--queue', dest='queue', type='string', default=None, help='Name of the queue on the broker')
parser.add_option('-e', '--exchange', dest='exchange', type='string', default=None, help='Name of the exchange on the broker')
parser.add_option('-k', '--routingkey', dest='routingkey', type='string', default='#', help='Federation routing key')
parser.add_option_group(dbcredentials.options_group(parser))
(options, args) = parser.parse_args()
if (len(sys.argv)<2):
parser.print_help()
QPIDinfra = qpidinfra()
dbcreds = dbcredentials.parse_options(options)
QPIDinfra = qpidinfra(dbcreds)
if (options.broker==None):
parser.print_help()
......
#!/usr/bin/python
from lofar.qpidinfrastructure.QPIDDB import qpidinfra
from lofar.common import dbcredentials
S_INDB = 1
S_ONQPID = 2
......@@ -81,7 +82,8 @@ def qpidQroute_add(settings):
Host(settings['fromhost']).tagqueueroute(settings['tohost'],settings['queuename'],DEFINED)
QPIDinfra = qpidinfra()
dbcreds = dbcredentials.DBCredentials().get("qpidinfra")
QPIDinfra = qpidinfra(dbcreds)
QPIDinfra.perqueue(qpidconfig_add_queue)
......
#!/usr/bin/python
from lofar.qpidinfrastructure.QPIDDB import qpidinfra
from lofar.common import dbcredentials
def qpidconfig_add_queue(settings):
print ("qpid-config -b %s add queue %s --durable" %(settings['hostname'],settings['queuename']))
......@@ -14,7 +15,8 @@ def qpidroute_add(settings):
def qpidQroute_add(settings):
print ("qpid-route -d queue add %s %s %s '%s'" %(settings['tohost'],settings['fromhost'],settings['queuename'],settings['exchangename']))
QPIDinfra = qpidinfra()
dbcreds = dbcredentials.DBCredentials().get("qpidinfra")
QPIDinfra = qpidinfra(dbcreds)
QPIDinfra.perqueue(qpidconfig_add_queue)
QPIDinfra.perexchange(qpidconfig_add_topic)
QPIDinfra.perfederationexchange(qpidroute_add)
......
......@@ -2,9 +2,10 @@
import sys
from lofar.qpidinfrastructure.QPIDDB import qpidinfra
from lofar.common import dbcredentials
todb=qpidinfra()
dbcreds = dbcredentials.DBCredentials().get("qpidinfra")
todb=qpidinfra(dbcreds)
tosearch = sys.stdin.readlines()
......
......@@ -5,10 +5,10 @@ from psqlQPIDDB import psqlQPIDDB
class qpidinfra:
""" Class to access and edit the QPIDInfra database.
"""
def __init__(self):
def __init__(self, dbcreds):
""" Initialize the database connection.
"""
self.db=psqlQPIDDB('qpidinfra')
self.db=psqlQPIDDB(dbcreds)
def perqueue(self,callback):
""" Iterate over all queues defined in the database.
......
#!/usr/bin/python
import psycopg2 as pg
import psycopg2.extras as pgdefs
......@@ -8,26 +9,31 @@ class psqlQPIDDB:
postgres database that holds the QPID infra configuration.
"""
def __init__(self,dbname):
def __init__(self, dbcreds=None):
""" Init the class with the name of the database
example: db = psqlQPIDDB('qpidinfra')
example: db = psqlQPIDDB(dbcreds)
where `dbcreds' is an lofar.common.dbcredentials.Credentials object.
"""
self.dbname = dbname
self.conn = pg.connect("dbname='%s'" %(self.dbname))
self.dbcreds = dbcreds
self.conn = None
self.ensure_connect()
def ensure_connect(self):
""" ensure that the database is still connected.
raises an exception "ERROR: Failed to connect to database XXX"
if the reconnect failed.
"""
if (self.conn and self.conn.status==1):
if self.conn and self.conn.status==1:
return
self.conn=pg.connect("dbname='%s'" %(self.dbname))
if (self.conn and self.conn.status==1):
self.conn = pg.connect(**self.dbcreds.pg_connect_options())
if self.conn and self.conn.status==1:
return
else:
raise Exception( "ERROR: Failed to reconnect to database %s" %(self.dbname))
raise Exception("ERROR: Failed to reconnect to database %s" % (self.dbcreds,))
def doquery(self,query):
""" execute a query on the database and return reult as a list of dicts.
......@@ -35,6 +41,7 @@ class psqlQPIDDB:
useful for fetching infromation from the database.
usage: ret=doquery("select * from table;")
"""
self.ensure_connect()
cur = self.conn.cursor(cursor_factory = pgdefs.RealDictCursor)
cur.execute(query)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment