def setup_mongodb_put(): global setup_state setup_key = flask.request.json['setup_key'] mongodb_uri = flask.request.json['mongodb_uri'] if setup_state != 'setup': return flask.abort(404) utils.rand_sleep() if setup_key != settings.local.setup_key: return utils.jsonify( { 'error': SETUP_KEY_INVALID, 'error_msg': SETUP_KEY_INVALID_MSG, }, 400) if not mongodb_uri: return utils.jsonify( { 'error': MONGODB_URI_INVALID, 'error_msg': MONGODB_URI_INVALID_MSG, }, 400) try: client = pymongo.MongoClient(mongodb_uri, connectTimeoutMS=MONGO_CONNECT_TIMEOUT) client.get_default_database() except pymongo.errors.ConfigurationError as error: if 'auth failed' in str(error): return utils.jsonify( { 'error': MONGODB_AUTH_ERROR, 'error_msg': MONGODB_AUTH_ERROR_MSG, }, 400) raise except pymongo.errors.ConnectionFailure: return utils.jsonify( { 'error': MONGODB_CONNECT_ERROR, 'error_msg': MONGODB_CONNECT_ERROR_MSG, }, 400) settings.conf.mongodb_uri = mongodb_uri settings.conf.commit() db_ver = utils.get_db_ver() if check_db_ver(db_ver): setup_state = 'upgrade' upgrade_database() else: stop_server() return ''
def setup_mongodb_put(): global setup_state setup_key = flask.request.json['setup_key'] mongodb_uri = flask.request.json['mongodb_uri'] if setup_state != 'setup': return flask.abort(404) utils.rand_sleep() if setup_key != settings.local.setup_key: return utils.jsonify({ 'error': SETUP_KEY_INVALID, 'error_msg': SETUP_KEY_INVALID_MSG, }, 400) if not mongodb_uri: return utils.jsonify({ 'error': MONGODB_URI_INVALID, 'error_msg': MONGODB_URI_INVALID_MSG, }, 400) try: client = pymongo.MongoClient(mongodb_uri, connectTimeoutMS=MONGO_CONNECT_TIMEOUT) client.get_default_database() except pymongo.errors.ConfigurationError as error: if 'auth failed' in str(error): return utils.jsonify({ 'error': MONGODB_AUTH_ERROR, 'error_msg': MONGODB_AUTH_ERROR_MSG, }, 400) raise except pymongo.errors.ConnectionFailure: return utils.jsonify({ 'error': MONGODB_CONNECT_ERROR, 'error_msg': MONGODB_CONNECT_ERROR_MSG, }, 400) settings.conf.mongodb_uri = mongodb_uri settings.conf.commit() db_ver = utils.get_db_ver() if check_db_ver(db_ver): setup_state = 'upgrade' upgrade_database() else: stop_server() return ''
def check_db_ver(): db_ver = utils.get_db_ver() db_min_ver = utils.get_min_db_ver() db_min_ver_int = utils.get_int_ver(db_min_ver) if settings.local.version_int < db_min_ver_int: logger.error('Pritunl version not compatible with database version', 'setup', db_version=db_ver, db_min_version=db_min_ver, server_version=settings.local.version, ) exit(75) return utils.get_int_ver(MIN_DATABASE_VER) > db_min_ver_int
def setup_server(): global setup_state last_error = time.time() - 24 while True: try: db_ver = utils.get_db_ver() break except: time.sleep(0.5) if time.time() - last_error > 30: last_error = time.time() logger.exception('Error connecting to mongodb server') listener.add_listener('system', on_system_msg) if not settings.conf.mongodb_uri: setup_state = 'setup' elif check_db_ver(db_ver): setup_state = 'upgrade' if setup_state: logger.info('Starting setup server', 'setup') if setup_state == 'upgrade': upgrade_database() settings.local.server_start.clear() thread = threading.Thread(target=server_thread) thread.daemon = True thread.start() setup_ready.wait() time.sleep(1.5) upgrade.database_clean_up() last_error = time.time() - 24 while True: try: utils.set_db_ver(__version__) break except: time.sleep(0.5) if time.time() - last_error > 30: last_error = time.time() logger.exception('Error connecting to mongodb server')