From f0e3d76501d835487f5a2ab85a467041da30e1ad Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 3 Aug 2021 09:59:03 +0200 Subject: [PATCH] Island: Check if mongodb is running. --- monkey/monkey_island/cc/server_setup.py | 12 +++++++++++- .../cc/setup/mongo/mongo_db_process.py | 18 +++++++++++++++++- .../cc/setup/mongo/mongo_setup.py | 13 +++++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index a72cea033..fe0dec126 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -33,6 +33,7 @@ from monkey_island.cc.setup import island_config_options_validator # noqa: E402 from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402 from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402 from monkey_island.cc.setup.mongo.database_initializer import init_collections # noqa: E402 +from monkey_island.cc.setup.mongo.mongo_db_process import MongoDbProcessException # noqa: E402 from monkey_island.cc.setup.mongo.mongo_setup import ( # noqa: E402 MONGO_URL, connect_to_mongodb, @@ -52,11 +53,20 @@ def run_monkey_island(): _configure_logging(config_options) _initialize_globals(config_options, server_config_path) + mongo_db_process = None + if config_options.start_mongodb: mongo_db_process = start_mongodb(config_options.data_dir) register_mongo_shutdown_callback(mongo_db_process) - connect_to_mongodb() + try: + connect_to_mongodb(mongo_db_process) + except MongoDbProcessException: + logger.error( + f"MongoDB could not start. For details, check the MongoDB log at " + f"{mongo_db_process.get_log_file()}" + ) + sys.exit(-1) _configure_gevent_exception_handling(Path(config_options.data_dir)) _start_island_server(island_args.setup_only, config_options) diff --git a/monkey/monkey_island/cc/setup/mongo/mongo_db_process.py b/monkey/monkey_island/cc/setup/mongo/mongo_db_process.py index 0a3b55d19..ffa531647 100644 --- a/monkey/monkey_island/cc/setup/mongo/mongo_db_process.py +++ b/monkey/monkey_island/cc/setup/mongo/mongo_db_process.py @@ -29,7 +29,10 @@ class MongoDbProcess: self._mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log ) - logger.info("MongoDB launched successfully!") + if not self.is_running(): + raise MongoDbProcessException + + logger.info("MongoDB has been launched!") def stop(self): if not self._process: @@ -47,3 +50,16 @@ class MongoDbProcess: f"MongoDB did not terminate gracefully and will be forcefully killed: {te}" ) self._process.kill() + + def is_running(self) -> bool: + if self._process.poll() is None: + return True + + return False + + def get_log_file(self) -> str: + return self._log_file + + +class MongoDbProcessException(Exception): + pass diff --git a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py index 02e097c68..449beeb72 100644 --- a/monkey/monkey_island/cc/setup/mongo/mongo_setup.py +++ b/monkey/monkey_island/cc/setup/mongo/mongo_setup.py @@ -8,7 +8,7 @@ from monkey_island.cc.database import get_db_version, is_db_server_up from monkey_island.cc.server_utils.file_utils import create_secure_directory from monkey_island.cc.setup.mongo import mongo_connector from monkey_island.cc.setup.mongo.mongo_connector import MONGO_DB_HOST, MONGO_DB_NAME, MONGO_DB_PORT -from monkey_island.cc.setup.mongo.mongo_db_process import MongoDbProcess +from monkey_island.cc.setup.mongo.mongo_db_process import MongoDbProcess, MongoDbProcessException DB_DIR_NAME = "db" MONGO_LOG_FILENAME = "mongodb.log" @@ -26,6 +26,7 @@ def start_mongodb(data_dir: str) -> MongoDbProcess: log_file = os.path.join(data_dir, MONGO_LOG_FILENAME) mongo_db_process = MongoDbProcess(db_dir=db_dir, log_file=log_file) + mongo_db_process.start() return mongo_db_process @@ -43,15 +44,19 @@ def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess): atexit.register(mongo_db_process.stop) -def connect_to_mongodb(): - _wait_for_mongo_db_server(MONGO_URL) +def connect_to_mongodb(mongo_db_process: MongoDbProcess): + _wait_for_mongo_db_server(MONGO_URL, mongo_db_process) _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, mongo_db_process): while not is_db_server_up(mongo_url): logger.info("Waiting for MongoDB server on {0}".format(mongo_url)) + + if mongo_db_process is not None and not mongo_db_process.is_running(): + raise MongoDbProcessException + time.sleep(1)