def main(): """Entry point for this utility. Usage:: $ python createApiUser.py """ try: ## Setup requested log handlers globalSettings = utils.loadSettings( os.path.join(env.configPath, "globalSettings.json")) logFiles = utils.setupLogFile( "ApiApplication", env, globalSettings['fileContainingServiceLogSettings'], directoryName='service') logObserver = utils.setupObservers( logFiles, "ApiApplication", env, globalSettings['fileContainingServiceLogSettings']) logger = twisted.logger.Logger(observer=logObserver, namespace="ApiApplication") logger.info('Starting createApiUser') ## Connect to database dbClient = DatabaseClient(logger) if dbClient is None: raise SystemError( 'Failed to connect to database; unable to initialize tables.') ## Get list of valid users users = {} getUsers(dbClient, logger, users) ## Create and insert a new credential createUserEntry(dbClient, logger, users) ## Cleanup dbClient.session.remove() dbClient.close() logger.info('Exiting configureDatabase utility.') except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't ## able to use the logging mechanism before encountering the failure. print('Failure in createApiUser: {}'.format(stacktrace)) try: logger.debug('Failure in createApiUser: {}'.format(stacktrace)) except: pass ## end main return
def main(): """Entry point for the database configuration utility. Usage:: $ python rebuildDataSchema.py """ try: ## Setup requested log handlers logEntity = 'Database' logger = utils.setupLogger(logEntity, env, 'logSettingsCore.json') logger.info('Starting rebuildDataSchema utility.') ## Attempt connection dbClient = DatabaseClient(logger) if dbClient is None: raise SystemError( 'Failed to connect to database; unable to rebuild data schema.' ) print('\nDatabase connection successful.') logger.debug('Database connection successful') ## Start the work createTables(dbClient, logger) ## Close the connection dbClient.close() logger.info('Exiting rebuildDataSchema utility.') except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't ## able to use the logging mechanism before encountering the failure. print('Failure in rebuildDataSchema.main: {}'.format(stacktrace)) try: logger.debug( 'Failure in rebuildDataSchema.main: {}'.format(stacktrace)) except: pass ## end main return
def main(): """Entry point for this utility. Usage:: $ python createProtocolEntry.py """ try: ## Setup requested log handlers logEntity = 'Protocols' logger = utils.setupLogger(logEntity, env, 'logSettingsCore.json') logger.info('Starting manageProtocols.') ## Connect to database dbClient = DatabaseClient(logger) if dbClient is None: raise SystemError('Failed to connect to database; unable to initialize tables.') ## Get list of valid realms realms = [] getRealms(dbClient, logger, realms) ## Create and insert a new credential createProtocolEntry(dbClient, logger, realms) ## Cleanup dbClient.session.remove() dbClient.close() logger.info('Exiting configureDatabase utility.') except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't ## able to use the logging mechanism before encountering the failure. print('Failure in configureDatabase: {}'.format(stacktrace)) try: logger.debug('Failure in configureDatabase: {}'.format(stacktrace)) except: pass ## end main return
def getSharedDbPool(self): """Create a global context for sharing a DB connection pool. When using a scoped_session in sqlalchemy, the code checks if there was a previous thread-local session created. If one exists then sqlalchemy will simply reuse it. This enables a shared pool for API connections. """ dbClient = DatabaseClient(self.logger, globalSettings=self.globalSettings, env=env, poolSize=20, maxOverflow=7, poolRecycle=1800) if dbClient is None: self.logger.error('Failed to connect to shared database pool.') raise EnvironmentError( 'Failed to connect to shared database pool.') dbClient.session.close() dbClient.session.remove() dbClient.close() self.logger.info('Created the SqlAlchemy connection pool.') ## end getSharedDbPool return
## use a middleware class (SQLAlchemySessionMiddleware below). This allows us to ## share database resources across API connections. from database.connectionPool import DatabaseClient dbClient = DatabaseClient(logger, globalSettings=globalSettings, env=env, poolSize=20, maxOverflow=7, poolRecycle=1800) if dbClient is None: logger.error('Failed to connect to the shared database connection pool.') raise EnvironmentError( 'Failed to connect to the shared database connection pool.') dbClient.session.close() dbClient.session.remove() dbClient.close() logger.info('Acquired a handle to the shared database connection pool.') ############################################################################# ## Falcon middleware section ############################################################################# class SQLAlchemySessionMiddleware: """Aquire and release a DB connection for each API request. This extends our SqlAlchemy session onto the request.context, for functions to use: request.context['dbSession'] """ def __init__(self, dbClient): ### Get a handle on the scoped session self.dbSession = dbClient.session