def check_postgresql(): with app.app_context(): try: if not db.session.query(Workspace).count(): logger.warn( 'No workspaces found. Remember to execute CouchDB importer' ) except sqlalchemy.exc.ArgumentError: logger.error( '\n\b{RED}Please check your PostgreSQL connection string in the file ~/.faraday/config/server.ini on your home directory.{WHITE} \n' .format(RED=Fore.RED, WHITE=Fore.WHITE)) sys.exit(1) except sqlalchemy.exc.OperationalError: logger.error( '\n\n{RED}Could not connect to PostgreSQL.\n{WHITE}Please check: \n{YELLOW} * if database is running \n * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} faraday-manage initdb\n\n' .format(GREEN=Fore.GREEN, YELLOW=Fore.YELLOW, WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1) except sqlalchemy.exc.ProgrammingError as e: logger.error( '\n\nn{WHITE}Missing migrations, please execute: \n\nfaraday-manage migrate' .format(WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1)
def process_reports(debug, workspace, polling): try: from requests import ConnectionError except ImportError: print( 'Python requests was not found. Please install it with: pip install requests' ) sys.exit(1) try: from sqlalchemy.exc import OperationalError except ImportError: print( 'SQLAlchemy was not found please install it with: pip install sqlalchemy' ) sys.exit(1) setUpLogger(debug) configuration = _conf() url = '{0}/_api/v2/info'.format( configuration.getServerURI() if FARADAY_UP else SERVER_URL) with app.app_context(): try: check_faraday_server(url) import_external_reports(workspace, polling) except OperationalError as ex: print('{0}'.format(ex)) print( 'Please verify database is running or configuration on server.ini!' ) except ConnectionError: print( 'Can\'t connect to {0}. Please check if the server is running.' .format(url))
def check_alembic_version(): config = Config() config.set_main_option("script_location", "migrations") script = ScriptDirectory.from_config(config) head_revision = script.get_current_head() with app.app_context(): try: conn = db.session.connection() except ImportError as ex: if not faraday.server.config.database.connection_string: print( "\n\nNo database configuration found. Did you execute \"faraday-manage initdb\"? \n\n" ) sys.exit(1) context = MigrationContext.configure(conn) current_revision = context.get_current_revision() if head_revision != current_revision: if glob.glob( os.path.join(FARADAY_BASE, 'migrations', 'versions', '{}_*.py'.format(current_revision))): print('--' * 20) print('Missing migrations, please execute: \n\n') print('faraday-manage migrate') sys.exit(1) else: logger.warning( "You are using an unknown schema version. If you are a " "developer, this probably happened because you used branch " "with a schema migration not merged yet. If you are a " "normal user, consider reporting this bug back to us")
def check_locks_postgresql(): with app.app_context(): psql_status = check_postgres() if psql_status: result = db.engine.execute("""SELECT blocked_locks.pid AS blocked_pid, blocked_activity.usename AS blocked_user, blocking_locks.pid AS blocking_pid, blocking_activity.usename AS blocking_user, blocked_activity.query AS blocked_statement, blocking_activity.query AS current_statement_in_blocking_process FROM pg_catalog.pg_locks blocked_locks JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid JOIN pg_catalog.pg_locks blocking_locks ON blocking_locks.locktype = blocked_locks.locktype AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid AND blocking_locks.pid != blocked_locks.pid JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid WHERE NOT blocked_locks.GRANTED;""") fetch = result.fetchall() if fetch: return True else: return False else: return None
def initdb(choose_password): with app.app_context(): InitDB().run(choose_password=choose_password) couchdb_config_present = faraday.server.config.couchdb if couchdb_config_present and couchdb_config_present.user and couchdb_config_present.password: print('Importing data from CouchDB, please wait...') ImportCouchDB().run() print('All users from CouchDB were imported. You can login with your old username/password to faraday now.')
def check_postgres(): with app.app_context(): try: result = (db.session.query("version()").one(),db.session.query("current_setting('server_version_num')").one()) return result except sqlalchemy.exc.OperationalError: return False except sqlalchemy.exc.ArgumentError: return None
def import_from_couchdb(): with app.app_context(): ImportCouchDB().run()