island: Construct mongo run command in MongoDbProcess constructor

This commit is contained in:
Mike Salvatore 2021-06-02 15:39:49 -04:00
parent cc1865dc5b
commit 19e8042ee4
1 changed files with 5 additions and 11 deletions

View File

@ -1,7 +1,6 @@
import logging import logging
import os import os
import subprocess import subprocess
from typing import List
from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH from monkey_island.cc.server_utils.consts import MONGO_EXECUTABLE_PATH
@ -18,20 +17,19 @@ class MongoDbProcess:
@param db_dir: 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 to a folder where mongodb logs will be created @param logging_dir: Path to a folder where mongodb logs will be created
""" """
self._db_dir = db_dir self._mongo_run_cmd = [MONGO_EXECUTABLE_PATH, DB_DIR_PARAM, db_dir]
self._log_file = os.path.join(logging_dir, MONGO_LOG_FILENAME) self._log_file = os.path.join(logging_dir, MONGO_LOG_FILENAME)
self._process = None self._process = None
def start(self): def start(self):
logger.info("Starting MongoDB process.") logger.info("Starting MongoDB process.")
logger.debug(f"MongoDB will be launched with command: {' '.join(self._mongo_run_cmd)}.")
mongo_run_cmd = MongoDbProcess._build_mongo_run_cmd(MONGO_EXECUTABLE_PATH, self._db_dir)
logger.info(f"MongoDB will be launched with command: {' '.join(mongo_run_cmd)}.")
logger.info(f"MongoDB log will be available at {self._log_file}.") logger.info(f"MongoDB log will be available at {self._log_file}.")
with open(self._log_file, "w") as log: with open(self._log_file, "w") as log:
self._process = subprocess.Popen(mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log) self._process = subprocess.Popen(
self._mongo_run_cmd, stderr=subprocess.STDOUT, stdout=log
)
logger.info("MongoDB launched successfully!") logger.info("MongoDB launched successfully!")
@ -51,7 +49,3 @@ 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()
@staticmethod
def _build_mongo_run_cmd(exec_path: str, db_dir: str) -> List[str]:
return [exec_path, DB_DIR_PARAM, db_dir]