Improved setup_param_factory.py further, by extracting loading server config file out of the factory

This commit is contained in:
VakarisZ 2021-05-20 12:04:44 +03:00
parent 9055381873
commit 8c210fc21f
3 changed files with 26 additions and 26 deletions

View File

@ -1,7 +1,8 @@
from gevent import monkey as gevent_monkey 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.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() gevent_monkey.patch_all()
@ -11,8 +12,9 @@ from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E
if "__main__" == __name__: if "__main__" == __name__:
island_args = parse_cli_args() 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: try:

View File

@ -1,7 +1,12 @@
import json 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: with open(server_config_path, "r") as f:
config_content = f.read() config_content = f.read()
config = json.loads(config_content) config = json.loads(config_content)

View File

@ -6,37 +6,30 @@ from monkey_island.setup.setup_params import SetupParams
class SetupParamFactory: class SetupParamFactory:
@staticmethod def __init__(self):
def build(cmd_args: IslandArgs) -> SetupParams: 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) self._update_by_cmd_args(cmd_args)
setup_params = SetupParamFactory._update_by_config_file(setup_params) self._update_by_config_file(config_contents)
return setup_params return self.setup_params
@staticmethod def _update_by_cmd_args(self, cmd_args: IslandArgs):
def _update_by_cmd_args(setup_params: SetupParams, cmd_args: IslandArgs) -> SetupParams:
if type(cmd_args.setup_only) == bool: 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: 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 if "data_dir" in config_contents:
def _update_by_config_file(setup_params: SetupParams): self.setup_params.data_dir = config_contents["data_dir"]
config = config_file_parser.load_server_config_from_file(setup_params.server_config_path)
if "data_dir" in config: if "log_level" in config_contents:
setup_params.data_dir = config["data_dir"] self.setup_params.log_level = config_contents["log_level"]
if "log_level" in config: if "mongodb" in config_contents and "start_mongodb" in config_contents["mongodb"]:
setup_params.log_level = config["log_level"] self.setup_params.start_mongodb = config_contents["mongodb"]["start_mongodb"]
if "mongodb" in config and "start_mongodb" in config["mongodb"]:
setup_params.start_mongodb = config["mongodb"]["start_mongodb"]
return setup_params