diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 650cfe95d..0ac7ebca3 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -1,26 +1,24 @@ from gevent import monkey as gevent_monkey +from setup_param_factory import SetupParamFactory from monkey_island.cc.arg_parser import parse_cli_args gevent_monkey.patch_all() import json # noqa: E402 -import os # noqa: E402 -from monkey_island import config_loader # noqa: E402 from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E402 if "__main__" == __name__: island_args = parse_cli_args() - # This is here in order to catch EVERYTHING, some functions are being called on - # imports, so the log init needs to be first. + setup_params = SetupParamFactory.build(island_args) + try: - server_config_path = os.path.expanduser(island_args.server_config) - config = config_loader.load_server_config_from_file(server_config_path) - - setup_logging(config["data_dir"], config["log_level"]) + # This is here in order to catch EVERYTHING, some functions are being called on + # imports, so the log init needs to be first. + setup_logging(setup_params.data_dir, setup_params.log_level) except OSError as ex: print(f"Error opening server config file: {ex}") @@ -32,4 +30,4 @@ if "__main__" == __name__: from monkey_island.cc.main import main # noqa: E402 - main(config["data_dir"], island_args.setup_only, island_args.server_config) + main(setup_params.data_dir, setup_params.setup_only, setup_params.server_config_path) diff --git a/monkey/monkey_island/cc/arg_parser.py b/monkey/monkey_island/cc/arg_parser.py index 6e12ef38f..338db153d 100644 --- a/monkey/monkey_island/cc/arg_parser.py +++ b/monkey/monkey_island/cc/arg_parser.py @@ -1,12 +1,10 @@ from dataclasses import dataclass -from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH - @dataclass class IslandArgs: setup_only: bool - server_config: str + server_config_path: str def parse_cli_args() -> IslandArgs: @@ -25,10 +23,7 @@ def parse_cli_args() -> IslandArgs: "compiling/packaging Islands.", ) parser.add_argument( - "--server-config", - action="store", - help="The path to the server configuration file.", - default=DEFAULT_SERVER_CONFIG_PATH, + "--server-config", action="store", help="The path to the server configuration file." ) args = parser.parse_args() diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index f0dba26dc..c25822c6f 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -5,6 +5,7 @@ __author__ = "itay.mizeretz" MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island") DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 +# TODO move setup consts DEFAULT_SERVER_CONFIG_PATH = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json") DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( @@ -12,3 +13,6 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( ) DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc") +DEFAULT_LOG_LEVEL = "INFO" +DEFAULT_START_MONGO_DB = True +DEFAULT_SHOULD_SETUP_ONLY = False diff --git a/monkey/monkey_island/config_file_parser.py b/monkey/monkey_island/config_file_parser.py new file mode 100644 index 000000000..6c9c5c707 --- /dev/null +++ b/monkey/monkey_island/config_file_parser.py @@ -0,0 +1,8 @@ +import json + + +def load_server_config_from_file(server_config_path): + with open(server_config_path, "r") as f: + config_content = f.read() + config = json.loads(config_content) + return config diff --git a/monkey/monkey_island/config_loader.py b/monkey/monkey_island/config_loader.py deleted file mode 100644 index aaa9185d7..000000000 --- a/monkey/monkey_island/config_loader.py +++ /dev/null @@ -1,25 +0,0 @@ -import json -import os - -from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR - -DEFAULT_LOG_LEVEL = "INFO" - - -def load_server_config_from_file(server_config_path): - with open(server_config_path, "r") as f: - config_content = f.read() - config = json.loads(config_content) - add_default_values_to_config(config) - - return config - - -def add_default_values_to_config(config): - config["data_dir"] = os.path.abspath( - os.path.expanduser(os.path.expandvars(config.get("data_dir", DEFAULT_DATA_DIR))) - ) - - config.setdefault("log_level", DEFAULT_LOG_LEVEL) - - return config diff --git a/monkey/setup_param_factory.py b/monkey/setup_param_factory.py new file mode 100644 index 000000000..416a408a5 --- /dev/null +++ b/monkey/setup_param_factory.py @@ -0,0 +1,43 @@ +import os + +from setup_params import SetupParams + +from monkey_island import config_file_parser +from monkey_island.cc.arg_parser import IslandArgs + + +class SetupParamFactory: + @staticmethod + def build(cmd_args: IslandArgs) -> SetupParams: + + setup_params = SetupParams() + + setup_params = SetupParamFactory._update_by_cmd_args(setup_params, cmd_args) + setup_params = SetupParamFactory._update_by_config_file(setup_params) + + return setup_params + + @staticmethod + def _update_by_cmd_args(setup_params: SetupParams, cmd_args: IslandArgs) -> SetupParams: + if type(cmd_args.setup_only) == bool: + setup_params.setup_only = cmd_args.setup_only + + if cmd_args.server_config_path: + setup_params.server_config_path = os.path.expanduser(cmd_args.server_config_path) + + return setup_params + + @staticmethod + def _update_by_config_file(setup_params: SetupParams): + config = config_file_parser.load_server_config_from_file(setup_params.server_config_path) + + if "data_dir" in config: + setup_params.data_dir = config["data_dir"] + + if "log_level" in config: + setup_params.log_level = config["log_level"] + + if "mongodb" in config and "start_mongodb" in config["mongodb"]: + setup_params.start_mongodb = config["mongodb"]["start_mongodb"] + + return setup_params diff --git a/monkey/setup_params.py b/monkey/setup_params.py new file mode 100644 index 000000000..c302c7fc0 --- /dev/null +++ b/monkey/setup_params.py @@ -0,0 +1,18 @@ +from dataclasses import dataclass + +from monkey_island.cc.server_utils.consts import ( + DEFAULT_DATA_DIR, + DEFAULT_LOG_LEVEL, + DEFAULT_SERVER_CONFIG_PATH, + DEFAULT_SHOULD_SETUP_ONLY, + DEFAULT_START_MONGO_DB, +) + + +@dataclass +class SetupParams: + server_config_path = DEFAULT_SERVER_CONFIG_PATH + log_level = DEFAULT_LOG_LEVEL + data_dir = DEFAULT_DATA_DIR + start_mongodb = DEFAULT_START_MONGO_DB + setup_only = DEFAULT_SHOULD_SETUP_ONLY