From 8afe93747f47d73141330d4fe00c6cc6bdf573ca Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 24 May 2021 10:27:16 +0300 Subject: [PATCH] Improved monkey_island.py setup readability by extracting workflows of server_config setup by cmd_arguments and setup of server_config using defaults --- monkey/monkey_island.py | 17 +++++------------ monkey/monkey_island/setup/config_setup.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 monkey/monkey_island/setup/config_setup.py diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index 76399b6ff..662ebe892 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -1,32 +1,25 @@ from gevent import monkey as gevent_monkey from monkey_island.cc.arg_parser import parse_cli_args +from monkey_island.setup.config_setup import setup_config_by_cmd_arg, setup_default_config gevent_monkey.patch_all() import json # noqa: E402 -import os # noqa: E402 -import monkey_island.cc.environment.server_config_generator as server_config_generator # noqa: E402 -from monkey_island import config_loader # noqa: E402 -from monkey_island.cc.environment.data_dir_generator import create_data_dir # 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. try: if island_args.server_config: - server_config_path = os.path.expanduser(island_args.server_config) + config, server_config_path = setup_config_by_cmd_arg(island_args.server_config) else: - server_config_path = server_config_generator.create_default_server_config_file() - - config = config_loader.load_server_config_from_file(server_config_path) - - create_data_dir(config["data_dir"], True) + config, server_config_path = setup_default_config() + # 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(config["data_dir"], config["log_level"]) except OSError as ex: diff --git a/monkey/monkey_island/setup/config_setup.py b/monkey/monkey_island/setup/config_setup.py new file mode 100644 index 000000000..9980c66cd --- /dev/null +++ b/monkey/monkey_island/setup/config_setup.py @@ -0,0 +1,21 @@ +import os +from typing import Tuple + +from monkey_island.cc.environment import server_config_handler +from monkey_island.cc.environment.data_dir_generator import create_data_dir # noqa: E402 +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVER_CONFIG_PATH + + +def setup_config_by_cmd_arg(server_config_path) -> Tuple[dict, str]: + server_config_path = os.path.expanduser(server_config_path) + config = server_config_handler.load_server_config_from_file(server_config_path) + create_data_dir(config["data_dir"], create_parent_dirs=True) + return config, server_config_path + + +def setup_default_config() -> Tuple[dict, str]: + server_config_path = DEFAULT_SERVER_CONFIG_PATH + create_data_dir(DEFAULT_DATA_DIR, create_parent_dirs=False) + server_config_handler.create_default_server_config_file() + config = server_config_handler.load_server_config_from_file(server_config_path) + return config, server_config_path