From 504f8f20707ed46909a8e06089e76d4679d09dcc Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Thu, 7 Jan 2016 11:26:27 +0000 Subject: [PATCH] Task #8899: Updated OTDB services to accept and use dbcredentials; set exit code to 1 on invalid parameters --- SAS/OTDB_Services/TreeService.ini | 2 +- SAS/OTDB_Services/TreeService.py | 43 +++++++++++--------------- SAS/OTDB_Services/TreeStatusEvents.ini | 2 +- SAS/OTDB_Services/TreeStatusEvents.py | 23 +++++--------- 4 files changed, 27 insertions(+), 43 deletions(-) diff --git a/SAS/OTDB_Services/TreeService.ini b/SAS/OTDB_Services/TreeService.ini index a116e6494e5..accf056de1c 100644 --- a/SAS/OTDB_Services/TreeService.ini +++ b/SAS/OTDB_Services/TreeService.ini @@ -1,5 +1,5 @@ [program:TreeService] -command=/bin/bash -c 'source $LOFARROOT/lofarinit.sh;TreeService.py --hostname=sasdb --database=LOFAR_4' +command=/bin/bash -c 'source $LOFARROOT/lofarinit.sh;TreeService.py --dbcredentials=OTDB --busname=lofar.otdb.specification' user=lofarsys stopsignal=INT ; KeyboardInterrupt stdout_logfile=%{program_name)s.stdout diff --git a/SAS/OTDB_Services/TreeService.py b/SAS/OTDB_Services/TreeService.py index 39489ced5f2..644bc6f1cd2 100755 --- a/SAS/OTDB_Services/TreeService.py +++ b/SAS/OTDB_Services/TreeService.py @@ -34,6 +34,7 @@ import sys, time, pg import logging from optparse import OptionParser from lofar.messaging.Service import * +from lofar.common import dbcredentials QUERY_EXCEPTIONS = (TypeError, ValueError, MemoryError, pg.ProgrammingError, pg.InternalError) @@ -213,9 +214,7 @@ class PostgressMessageHandlerInterface(MessageHandlerInterface): """ def __init__(self, **kwargs): super(PostgressMessageHandlerInterface, self).__init__() - self.database = kwargs.pop("database") - self.db_user = kwargs.pop("db_user", "postgres") - self.db_host = kwargs.pop("db_host", "localhost") + self.dbcreds = kwargs.pop("dbcreds") if len(kwargs): raise AttributeError("Unknown keys in arguments of 'DatabaseTiedMessageHandler: %s" % kwargs) self.connection = None @@ -227,13 +226,13 @@ class PostgressMessageHandlerInterface(MessageHandlerInterface): self.connected = (self.connection and self.connection.status == 1) while not self.connected: try: - self.connection = pg.connect(user=self.db_user, host=self.db_host, dbname=self.database) + self.connection = pg.connect(user=self.dbcreds["user"], host=self.dbcreds["host"], dbname=self.dbcreds["database"]) self.connected = True - logger.info("Connected to database %s on host %s" % (self.database, self.db_host)) + logger.info("Connected to database %s on host %s" % (self.dbcreds["database"], self.dbcreds["host"])) except (TypeError, SyntaxError, pg.InternalError): self.connected = False logger.error("Not connected to database %s on host %s (anymore), retry in 5 seconds" - % (self.database, self.db_host)) + % (self.dbcreds["database"], self.dbcreds["host"])) time.sleep(5) class PostgressTaskSpecificationRequest(PostgressMessageHandlerInterface): @@ -275,33 +274,27 @@ class PostgressKeyUpdateCommand(PostgressMessageHandlerInterface): if __name__ == "__main__": # Check the invocation arguments parser = OptionParser("%prog [options]") - parser.add_option("-D", "--database", dest="dbName", type="string", default="", - help="Name of the database") - parser.add_option("-H", "--hostname", dest="dbHost", type="string", default="sasdb", - help="Hostname of database server") + parser.add_option("-B", "--busname", dest="busname", type="string", default="testbus", + help="Busname or queue-name on which RPC commands are received") + parser.add_group(dbcredentials.options_group()) (options, args) = parser.parse_args() - if not options.dbName: - print "Missing database name" - parser.print_help() - sys.exit(0) + dbcreds = dbcredentials.parse_options(options) - if not options.dbHost: - print "Missing database server name" + if not options.busname: + print "Missing busname" parser.print_help() - sys.exit(0) - - busname = sys.argv[1] if len(sys.argv) > 1 else "simpletest" + sys.exit(1) serv1 = Service("TaskSpecification", PostgressTaskSpecificationRequest, - busname=busname, numthreads=1, - handler_args = {"database" : options.dbName, "db_host" : options.dbHost}) + busname=options.busname, numthreads=1, + handler_args = {"dbcreds" : dbcreds}) serv2 = Service("StatusUpdateCmd", PostgressStatusUpdateCommand, - busname=busname, numthreads=1, - handler_args = {"database" : options.dbName, "db_host" : options.dbHost}) + busname=options.busname, numthreads=1, + handler_args = {"dbcreds" : dbcreds}) serv3 = Service("KeyUpdateCmd", PostgressKeyUpdateCommand, - busname=busname, numthreads=1, - handler_args = {"database" : options.dbName, "db_host" : options.dbHost}) + busname=options.busname, numthreads=1, + handler_args = {"dbcreds" : dbcreds}) with serv1, serv2, serv3: logger.info("Started the OTDB services") diff --git a/SAS/OTDB_Services/TreeStatusEvents.ini b/SAS/OTDB_Services/TreeStatusEvents.ini index 3eb140b7361..963ad910463 100644 --- a/SAS/OTDB_Services/TreeStatusEvents.ini +++ b/SAS/OTDB_Services/TreeStatusEvents.ini @@ -1,5 +1,5 @@ [program:TreeStatusEvents] -command=/bin/bash -c 'source $LOFARROOT/lofarinit.sh;TreeStatusEvents.py --hostname=sasdb --database=LOFAR_4 --busname lofar.otdb.status' +command=/bin/bash -c 'source $LOFARROOT/lofarinit.sh;TreeStatusEvents.py --dbcredentials=OTDB --busname lofar.otdb.status' user=lofarsys stopsignal=INT ; KeyboardInterrupt stdout_logfile=%{program_name)s.stdout diff --git a/SAS/OTDB_Services/TreeStatusEvents.py b/SAS/OTDB_Services/TreeStatusEvents.py index a9264f7b578..defab29117d 100755 --- a/SAS/OTDB_Services/TreeStatusEvents.py +++ b/SAS/OTDB_Services/TreeStatusEvents.py @@ -27,6 +27,7 @@ Daemon that watches the OTDB database for status changes of trees and publishes import os, sys, time, pg, signal from optparse import OptionParser from lofar.messaging import EventMessage, ToBus +from lofar.common import dbcredentials QUERY_EXCEPTIONS = (TypeError, ValueError, MemoryError, pg.ProgrammingError, pg.InternalError) alive = False @@ -76,28 +77,17 @@ def signal_handler(signum, frame): if __name__ == "__main__": # Check the invocation arguments parser = OptionParser("%prog [options]") - parser.add_option("-D", "--database", dest="dbName", type="string", default="", - help="Name of the database") - parser.add_option("-H", "--hostname", dest="dbHost", type="string", default="sasdb", - help="Hostname of database server") parser.add_option("-B", "--busname", dest="busname", type="string", default="", help="Busname or queue-name the status changes are published on") + parser.add_group(dbcredentials.options_group()) (options, args) = parser.parse_args() - if not options.dbName: - print "Missing database name" - parser.print_help() - sys.exit(0) - - if not options.dbHost: - print "Missing database server name" - parser.print_help() - sys.exit(0) + dbcreds = dbcredentials.parse_options(options) if not options.busname: print "Missing busname" parser.print_help() - sys.exit(0) + sys.exit(1) # Set signalhandler to stop the program in a neat way. signal.signal(signal.SIGINT, signal_handler) @@ -110,7 +100,7 @@ if __name__ == "__main__": while alive and not connected: # Connect to the database try: - otdb_connection = pg.connect(user="postgres", host=options.dbHost, dbname=options.dbName) + otdb_connection = pg.connect(user="postgres", host=dbcreds["host"], dbname=dbcreds["database"]) connected = True # Get list of allowed tree states allowed_states = {} @@ -118,7 +108,8 @@ if __name__ == "__main__": allowed_states[state_nr] = name except (TypeError, SyntaxError, pg.InternalError): connected = False - print "DatabaseError: Connection to database could not be made, reconnect attempt in 5 seconds" + logger.error("Not connected to database %s on host %s (anymore), retry in 5 seconds" + % (dbcreds["database"], dbcreds["host"])) time.sleep(5) # When we are connected we can poll the database -- GitLab