island: Add connect_to_mongo() function

This commit is contained in:
Mike Salvatore 2021-06-02 14:05:40 -04:00
parent dc40713683
commit d1a2501a5b
2 changed files with 14 additions and 5 deletions

View File

@ -25,7 +25,11 @@ from monkey_island.cc.services.initialize import initialize_services # noqa: E4
from monkey_island.cc.services.reporting.exporter_init import populate_exporter_list # noqa: E402
from monkey_island.cc.services.utils.network_utils import local_ip_addresses # noqa: E402
from monkey_island.cc.setup.mongo.database_initializer import init_collections # noqa: E402
from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL, start_mongodb # noqa: E402
from monkey_island.cc.setup.mongo.mongo_setup import ( # noqa: E402
MONGO_URL,
connect_to_mongodb,
start_mongodb,
)
from monkey_island.setup.config_setup import setup_data_dir # noqa: E402
from monkey_island.setup.island_config_options import IslandConfigOptions # noqa: E402
@ -40,6 +44,8 @@ def run_monkey_island():
_initialize_global_resources(config_options, server_config_path)
start_mongodb(config_options)
connect_to_mongodb()
bootloader_server_thread = _start_bootloader_server(MONGO_URL)
_start_island_server(island_args.setup_only, config_options)
bootloader_server_thread.join()

View File

@ -23,18 +23,21 @@ def start_mongodb(config_options: IslandConfigOptions):
MongoDbProcess(
db_dir_parent_path=config_options.data_dir, logging_dir_path=config_options.data_dir
).start()
wait_for_mongo_db_server(MONGO_URL)
assert_mongo_db_version(MONGO_URL)
def connect_to_mongodb():
_wait_for_mongo_db_server(MONGO_URL)
_assert_mongo_db_version(MONGO_URL)
mongo_connector.connect_dal_to_mongodb()
def wait_for_mongo_db_server(mongo_url):
def _wait_for_mongo_db_server(mongo_url):
while not is_db_server_up(mongo_url):
logger.info("Waiting for MongoDB server on {0}".format(mongo_url))
time.sleep(1)
def assert_mongo_db_version(mongo_url):
def _assert_mongo_db_version(mongo_url):
"""
Checks if the mongodb version is new enough for running the app.
If the DB is too old, quits.