def test(verbose): """Test database connectivity.""" utils.configureLogging(verbose) config = utils.loadConfig() logging.debug(f"Testing DB connection...") db_conn = db.connect(config["DB_HOST"], config["DB_NAME"], config["DB_USER"], config["DB_PASS"]) logging.info(db.show_version(db_conn)) db.close(db_conn)
def main(config): configureLogging(config) log = logging.getLogger('main') log.info('Starting conf_tracker version %s.', pkg_resources.require('conf_tracker')[0].version) if config.getboolean('daemon', 'daemon'): pidfile = config.get('daemon', 'pidfile') daemonize(pidfile) pw = config.get('host', 'password') h = config.get('host', 'host') p = config.get('host', 'port') #Setting up the DB connection log.info('Connecting to DB on: %s ' % (config.get('database', 'url'))) if ("sqlite" in config.get('database', 'url')): engine = create_engine( config.get('database', 'url'), poolclass=NullPool ) log.warning('You are using SQLite and we will use NullPool as pooling engine.') else: engine = create_engine( config.get('database', 'url'), pool_recycle=3600 ) #meta.Session = sessionmaker( autoflush=False, autocommit=False, bind=engine) meta.Session.configure(bind=engine) con = connect(h, p, pw) conferences = {} while True: while not con.connected(): log.warning("On main loop retrying...") con = connect(h, p, pw) e = con.recvEventTimed(5000) if e is None: continue conf_uuid = getUnquotedHeader(e, 'Conference-Unique-ID') action = getUnquotedHeader(e, 'Action') conf_size = getUnquotedHeader(e, 'Conference-Size') if action == 'stop-recording': try: TrackerConference.stop_recording(e, config) continue except Exception, e: log.exception('An error has occurred while executing stop-recording!') continue if conf_uuid not in conferences.keys(): try: conf = TrackerConference(e, config) conferences[conf_uuid] = conf except Exception, e: log.exception('An error occured creating a new conference object! Continuing...') continue
def main(config): configureLogging(config) log = logging.getLogger('main') if config.getboolean('daemon', 'daemon'): pidfile = config.get('daemon', 'pidfile') daemonize(pidfile) pw = config.get('host', 'password') h = config.get('host', 'host') p = config.get('host', 'port') #Setting up the DB connection log.info('Connecting to DB on: %s ' % (config.get('database', 'url'))) if ("sqlite" in config.get('database', 'url')): engine = create_engine( config.get('database', 'url'), poolclass=NullPool ) log.warning('You are using SQLite and we will use NullPool as pooling engine.') else: engine = create_engine( config.get('database', 'url'), pool_recycle=3600 ) Session = sessionmaker( autoflush=False, autocommit=False, bind=engine) log.info('Connecting to: %s:%s ' % (h, p)) con = ESLconnection(h, p, pw) con.events('plain', 'CUSTOM conference::maintenance') conferences = {} while con.connected(): e = con.recvEvent() if e is None: continue conf_uuid = getUnquotedHeader(e, 'Conference-Unique-ID') action = getUnquotedHeader(e, 'Action') conf_size = getUnquotedHeader(e, 'Conference-Size') if action == 'stop-recording': try: TrackerConference.stop_recording(e, config, Session()) continue except Exception, e: log.critical('An error has occurred while executing stop-recording!\n%s' % traceback.format_exc()) continue if conf_uuid not in conferences.keys(): try: conf = TrackerConference(e, config, Session()) conferences[conf_uuid] = conf except Exception, e: log.warning('An error occured creating a new conference object! Continuing... \n%s' % e) continue
parameters = {'pv': old_name, 'newname': new_name} renamedPV = requests.get(url, params=parameters) time.sleep(0.1) url = bplURL + '/resumeArchivingPV' parameters = {'pv': new_name} resumedPV = requests.get(url, params=parameters) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-v', "--verbose", action="store_true", help="Turn on verbose logging") parser.add_argument( "url", help= "This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl" ) parser.add_argument("rename_list", help="CSV file with list of PVs to rename") args = parser.parse_args() configureLogging(args.verbose) pvs = readInputData(args.rename_list) renamePVs(args.url, pvs)
def create(service_name, database_name, readonly_role_name, readwrite_role_name, user_name, verbose): """Create database, roles and user.""" # ------------------------------------------------------------------------- # 0. Init # ------------------------------------------------------------------------- utils.configureLogging(verbose) # Look for DB credentials in .env file logging.debug(f"Looking for DB credentials in .env file...") config = utils.loadConfig() # ------------------------------------------------------------------------- # 1. Connect to a default database to create the service database # ------------------------------------------------------------------------- # Connect to db server initial_db_conn = db.connect(config["DB_HOST"], config["DB_NAME"], config["DB_USER"], config["DB_PASS"]) # Create a db for the service service_db_name = database_name if database_name else f"{service_name}" logging.info( f"Create a database with name={service_db_name} for service={service_name}" ) db.create_database(initial_db_conn, service_db_name) # Close the db connection db.close(initial_db_conn) # ------------------------------------------------------------------------- # 2. Connect to new service db to create roles and a user for the service # ------------------------------------------------------------------------- # Connect to the new service database db_conn = db.connect(config["DB_HOST"], service_db_name, config["DB_USER"], config["DB_PASS"]) # Revoke default, public permissions from public schema logging.info(f"Revoke default, public permissions from public schema") db.revoke_public_permissions(db_conn, service_db_name) # Create a read-only role service_readonly_role = readonly_role_name if readonly_role_name else f"role_{service_name}_readonly" logging.info(f"Create a read-only role with name={service_readonly_role}") db.create_readonly_role(db_conn, service_db_name, service_readonly_role) # Create a read/write role service_readwrite_role = readwrite_role_name if readwrite_role_name else f"role_{service_name}_readwrite" logging.info( f"Create a read/write role with name={service_readwrite_role}") db.create_readowrite_role(db_conn, service_db_name, service_readwrite_role) # Create a user for the service with read/write role grants service_user = user_name if user_name else f"user_{service_name}_app" service_pass = utils.getRandomPassword(30) logging.info(f"Create a user with name={service_user}") db.create_user(db_conn, service_user, service_pass, service_readwrite_role) # Output user pass to a file utils.writeFile(".pass", service_pass) # Close the db connection db.close(db_conn)