forked from p15670423/monkey
island: Remove directory creation logic from MongoDbProcess
This commit is contained in:
parent
7d85ce0611
commit
a3bd432538
|
@ -3,30 +3,27 @@ import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from monkey_island.cc.environment.utils import create_secure_directory
|
|
||||||
from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH
|
from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
DB_DIR_NAME = "db"
|
|
||||||
DB_DIR_PARAM = "--dbpath"
|
DB_DIR_PARAM = "--dbpath"
|
||||||
MONGO_LOG_FILENAME = "mongodb.log"
|
MONGO_LOG_FILENAME = "mongodb.log"
|
||||||
TERMINATE_TIMEOUT = 10
|
TERMINATE_TIMEOUT = 10
|
||||||
|
|
||||||
|
|
||||||
class MongoDbProcess:
|
class MongoDbProcess:
|
||||||
def __init__(self, db_dir_parent_path: str, logging_dir_path: str):
|
def __init__(self, db_dir: str, logging_dir_path: str):
|
||||||
"""
|
"""
|
||||||
@param db_dir_parent_path: Path where a folder for database contents will be created
|
@param db_dir: Path where a folder for database contents will be created
|
||||||
@param logging_dir_path: Path to a folder where mongodb logs will be created
|
@param logging_dir_path: Path to a folder where mongodb logs will be created
|
||||||
"""
|
"""
|
||||||
self.db_dir_parent_path = db_dir_parent_path
|
self._db_dir = db_dir
|
||||||
self.logging_dir_path = logging_dir_path
|
self.logging_dir_path = logging_dir_path
|
||||||
self._process = None
|
self._process = None
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
db_path = self._create_db_dir()
|
self._start_mongodb_process()
|
||||||
self._start_mongodb_process(db_path)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
if self._process:
|
if self._process:
|
||||||
|
@ -41,16 +38,10 @@ class MongoDbProcess:
|
||||||
)
|
)
|
||||||
self._process.kill()
|
self._process.kill()
|
||||||
|
|
||||||
def _create_db_dir(self) -> str:
|
def _start_mongodb_process(self):
|
||||||
db_path = os.path.join(self.db_dir_parent_path, DB_DIR_NAME)
|
|
||||||
logger.info(f"Database content directory: {db_path}.")
|
|
||||||
create_secure_directory(db_path, create_parent_dirs=False)
|
|
||||||
return db_path
|
|
||||||
|
|
||||||
def _start_mongodb_process(self, db_dir_path: str):
|
|
||||||
logger.info("Starting MongoDb process.")
|
logger.info("Starting MongoDb process.")
|
||||||
|
|
||||||
mongo_run_cmd = MongoDbProcess._build_mongo_launch_cmd(MONGO_EXECUTABLE_PATH, db_dir_path)
|
mongo_run_cmd = MongoDbProcess._build_mongo_launch_cmd(MONGO_EXECUTABLE_PATH, self._db_dir)
|
||||||
logger.info(f"Mongodb will be launched with command: {' '.join(mongo_run_cmd)}.")
|
logger.info(f"Mongodb will be launched with command: {' '.join(mongo_run_cmd)}.")
|
||||||
|
|
||||||
mongo_log_path = os.path.join(self.logging_dir_path, MONGO_LOG_FILENAME)
|
mongo_log_path = os.path.join(self.logging_dir_path, MONGO_LOG_FILENAME)
|
||||||
|
@ -61,5 +52,5 @@ class MongoDbProcess:
|
||||||
logger.info("MongoDb launched successfully!")
|
logger.info("MongoDb launched successfully!")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _build_mongo_launch_cmd(exec_path: str, db_path: str) -> List[str]:
|
def _build_mongo_launch_cmd(exec_path: str, db_dir: str) -> List[str]:
|
||||||
return [exec_path, DB_DIR_PARAM, db_path]
|
return [exec_path, DB_DIR_PARAM, db_dir]
|
||||||
|
|
|
@ -5,11 +5,13 @@ import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from monkey_island.cc.database import get_db_version, is_db_server_up
|
from monkey_island.cc.database import get_db_version, is_db_server_up
|
||||||
|
from monkey_island.cc.environment.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
|
||||||
from monkey_island.setup.island_config_options import IslandConfigOptions
|
from monkey_island.setup.island_config_options import IslandConfigOptions
|
||||||
|
|
||||||
|
DB_DIR_NAME = "db"
|
||||||
MONGO_URL = os.environ.get(
|
MONGO_URL = os.environ.get(
|
||||||
"MONKEY_MONGO_URL",
|
"MONKEY_MONGO_URL",
|
||||||
"mongodb://{0}:{1}/{2}".format(MONGO_DB_HOST, MONGO_DB_PORT, MONGO_DB_NAME),
|
"mongodb://{0}:{1}/{2}".format(MONGO_DB_HOST, MONGO_DB_PORT, MONGO_DB_NAME),
|
||||||
|
@ -20,15 +22,22 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def start_mongodb(config_options: IslandConfigOptions) -> MongoDbProcess:
|
def start_mongodb(config_options: IslandConfigOptions) -> MongoDbProcess:
|
||||||
mongo_db_process = MongoDbProcess(
|
db_dir = _create_db_dir(config_options.data_dir)
|
||||||
db_dir_parent_path=config_options.data_dir, logging_dir_path=config_options.data_dir
|
|
||||||
)
|
|
||||||
|
|
||||||
|
mongo_db_process = MongoDbProcess(db_dir=db_dir, logging_dir_path=config_options.data_dir)
|
||||||
mongo_db_process.start()
|
mongo_db_process.start()
|
||||||
|
|
||||||
return mongo_db_process
|
return mongo_db_process
|
||||||
|
|
||||||
|
|
||||||
|
def _create_db_dir(db_dir_parent_path) -> str:
|
||||||
|
db_dir = os.path.join(db_dir_parent_path, DB_DIR_NAME)
|
||||||
|
logger.info(f"Database content directory: {db_dir}.")
|
||||||
|
|
||||||
|
create_secure_directory(db_dir, create_parent_dirs=False)
|
||||||
|
return db_dir
|
||||||
|
|
||||||
|
|
||||||
def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess):
|
def register_mongo_shutdown_callback(mongo_db_process: MongoDbProcess):
|
||||||
atexit.register(mongo_db_process.stop)
|
atexit.register(mongo_db_process.stop)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue