Island: Check if mongodb is running.

This commit is contained in:
Ilija Lazoroski 2021-08-03 09:59:03 +02:00
parent 62b2217821
commit f0e3d76501
3 changed files with 37 additions and 6 deletions

View File

@ -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.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.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.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 from monkey_island.cc.setup.mongo.mongo_setup import ( # noqa: E402
MONGO_URL, MONGO_URL,
connect_to_mongodb, connect_to_mongodb,
@ -52,11 +53,20 @@ def run_monkey_island():
_configure_logging(config_options) _configure_logging(config_options)
_initialize_globals(config_options, server_config_path) _initialize_globals(config_options, server_config_path)
mongo_db_process = None
if config_options.start_mongodb: if config_options.start_mongodb:
mongo_db_process = start_mongodb(config_options.data_dir) mongo_db_process = start_mongodb(config_options.data_dir)
register_mongo_shutdown_callback(mongo_db_process) 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)) _configure_gevent_exception_handling(Path(config_options.data_dir))
_start_island_server(island_args.setup_only, config_options) _start_island_server(island_args.setup_only, config_options)

View File

@ -29,7 +29,10 @@ class MongoDbProcess:
self._mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log 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): def stop(self):
if not self._process: if not self._process:
@ -47,3 +50,16 @@ class MongoDbProcess:
f"MongoDB did not terminate gracefully and will be forcefully killed: {te}" f"MongoDB did not terminate gracefully and will be forcefully killed: {te}"
) )
self._process.kill() 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

View File

@ -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.server_utils.file_utils import create_secure_directory
from monkey_island.cc.setup.mongo import mongo_connector 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_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" DB_DIR_NAME = "db"
MONGO_LOG_FILENAME = "mongodb.log" 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) 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 = MongoDbProcess(db_dir=db_dir, log_file=log_file)
mongo_db_process.start() mongo_db_process.start()
return mongo_db_process return mongo_db_process
@ -43,15 +44,19 @@ def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess):
atexit.register(mongo_db_process.stop) atexit.register(mongo_db_process.stop)
def connect_to_mongodb(): def connect_to_mongodb(mongo_db_process: MongoDbProcess):
_wait_for_mongo_db_server(MONGO_URL) _wait_for_mongo_db_server(MONGO_URL, mongo_db_process)
_assert_mongo_db_version(MONGO_URL) _assert_mongo_db_version(MONGO_URL)
mongo_connector.connect_dal_to_mongodb() 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): while not is_db_server_up(mongo_url):
logger.info("Waiting for MongoDB server on {0}".format(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) time.sleep(1)