From 8c210fc21f45f612ddc57ccd6efb9c30c4ba4b60 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Thu, 20 May 2021 12:04:44 +0300 Subject: [PATCH] Improved setup_param_factory.py further, by extracting loading server config file out of the factory --- monkey/monkey_island.py | 6 ++- monkey/monkey_island/config_file_parser.py | 7 +++- .../setup/setup_param_factory.py | 39 ++++++++----------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 0ac7ebca3..9532e3654 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -1,7 +1,8 @@ from gevent import monkey as gevent_monkey -from setup_param_factory import SetupParamFactory from monkey_island.cc.arg_parser import parse_cli_args +from monkey_island.config_file_parser import load_server_config_from_file +from monkey_island.setup.setup_param_factory import SetupParamFactory gevent_monkey.patch_all() @@ -11,8 +12,9 @@ from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E if "__main__" == __name__: island_args = parse_cli_args() + config_contents = load_server_config_from_file(island_args.server_config_path) - setup_params = SetupParamFactory.build(island_args) + setup_params = SetupParamFactory().build(island_args, config_contents) try: diff --git a/monkey/monkey_island/config_file_parser.py b/monkey/monkey_island/config_file_parser.py index 6c9c5c707..78a621ab2 100644 --- a/monkey/monkey_island/config_file_parser.py +++ b/monkey/monkey_island/config_file_parser.py @@ -1,7 +1,12 @@ import json +from os.path import isfile + +from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH -def load_server_config_from_file(server_config_path): +def load_server_config_from_file(server_config_path: str) -> dict: + if not server_config_path or not isfile(server_config_path): + server_config_path = DEFAULT_SERVER_CONFIG_PATH with open(server_config_path, "r") as f: config_content = f.read() config = json.loads(config_content) diff --git a/monkey/monkey_island/setup/setup_param_factory.py b/monkey/monkey_island/setup/setup_param_factory.py index 72b236623..0c0fe8881 100644 --- a/monkey/monkey_island/setup/setup_param_factory.py +++ b/monkey/monkey_island/setup/setup_param_factory.py @@ -6,37 +6,30 @@ from monkey_island.setup.setup_params import SetupParams class SetupParamFactory: - @staticmethod - def build(cmd_args: IslandArgs) -> SetupParams: + def __init__(self): + self.setup_params = SetupParams() - setup_params = SetupParams() + def build(self, cmd_args: IslandArgs, config_contents: dict) -> SetupParams: - setup_params = SetupParamFactory._update_by_cmd_args(setup_params, cmd_args) - setup_params = SetupParamFactory._update_by_config_file(setup_params) + self._update_by_cmd_args(cmd_args) + self._update_by_config_file(config_contents) - return setup_params + return self.setup_params - @staticmethod - def _update_by_cmd_args(setup_params: SetupParams, cmd_args: IslandArgs) -> SetupParams: + def _update_by_cmd_args(self, cmd_args: IslandArgs): if type(cmd_args.setup_only) == bool: - setup_params.setup_only = cmd_args.setup_only + self.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) + self.setup_params.server_config_path = os.path.expanduser(cmd_args.server_config_path) - return setup_params + def _update_by_config_file(self, config_contents: dict): - @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_contents: + self.setup_params.data_dir = config_contents["data_dir"] - if "data_dir" in config: - setup_params.data_dir = config["data_dir"] + if "log_level" in config_contents: + self.setup_params.log_level = config_contents["log_level"] - 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 + if "mongodb" in config_contents and "start_mongodb" in config_contents["mongodb"]: + self.setup_params.start_mongodb = config_contents["mongodb"]["start_mongodb"]