Improved monkey_island.py setup readability by extracting workflows of server_config setup by cmd_arguments and setup of server_config using defaults

This commit is contained in:
VakarisZ 2021-05-24 10:27:16 +03:00
parent 70b9a9f6b7
commit 8afe93747f
2 changed files with 26 additions and 12 deletions

View File

@ -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:

View File

@ -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