diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index d32019b6a..ca5b2639b 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -5,7 +5,11 @@ from monkey_island.cc.arg_parser import parse_cli_args gevent_monkey.patch_all() import json # noqa: E402 +import os # noqa: E402 +from pathlib import Path # noqa: E402 +import monkey_island.cc.environment.server_config_generator as server_config_generator # noqa: E402 +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_LOG_LEVEL # noqa: E402 from monkey_island.cc.server_utils.island_logger import json_setup_logging # noqa: E402 if "__main__" == __name__: @@ -14,7 +18,22 @@ if "__main__" == __name__: # This is here in order to catch EVERYTHING, some functions are being called on # imports, so the log init needs to be first. try: - json_setup_logging(island_args.logger_config) + server_config_path = os.path.expanduser(island_args.server_config) + if not Path(server_config_path).is_file(): + server_config_generator.create_default_config_file(server_config_path) + + with open(server_config_path, "r") as f: + config_content = f.read() + data = json.loads(config_content) + data_dir = os.path.abspath( + os.path.expanduser( + os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR) + ) + ) + log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL + + # json_setup_logging(data_dir, log_level) + except json.JSONDecodeError as ex: print(f"Error loading logging config: {ex}") exit(1) diff --git a/monkey/monkey_island/cc/arg_parser.py b/monkey/monkey_island/cc/arg_parser.py index 91a2b7d25..6e12ef38f 100644 --- a/monkey/monkey_island/cc/arg_parser.py +++ b/monkey/monkey_island/cc/arg_parser.py @@ -1,16 +1,12 @@ from dataclasses import dataclass -from monkey_island.cc.server_utils.consts import ( - DEFAULT_LOGGER_CONFIG_PATH, - DEFAULT_SERVER_CONFIG_PATH, -) +from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH @dataclass class IslandArgs: setup_only: bool server_config: str - logger_config: str def parse_cli_args() -> IslandArgs: @@ -34,12 +30,6 @@ def parse_cli_args() -> IslandArgs: help="The path to the server configuration file.", default=DEFAULT_SERVER_CONFIG_PATH, ) - parser.add_argument( - "--logger-config", - action="store", - help="The path to the logging configuration file.", - default=DEFAULT_LOGGER_CONFIG_PATH, - ) args = parser.parse_args() - return IslandArgs(args.setup_only, args.server_config, args.logger_config) + return IslandArgs(args.setup_only, args.server_config) diff --git a/monkey/monkey_island/cc/server_utils/consts.py b/monkey/monkey_island/cc/server_utils/consts.py index 67c7209eb..41bd6e4a7 100644 --- a/monkey/monkey_island/cc/server_utils/consts.py +++ b/monkey/monkey_island/cc/server_utils/consts.py @@ -15,4 +15,6 @@ DEFAULT_LOGGER_CONFIG_PATH = os.path.join( MONKEY_ISLAND_ABS_PATH, "cc", "island_logger_default_config.json" ) +DEFAULT_LOG_LEVEL = "NOTSET" + DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")