forked from p15670423/monkey
Extracted island setup parameter extraction into a separate workflow/DTO
This commit is contained in:
parent
8bb6e2f21f
commit
d768d60f9f
|
@ -1,26 +1,24 @@
|
|||
from gevent import monkey as gevent_monkey
|
||||
from setup_param_factory import SetupParamFactory
|
||||
|
||||
from monkey_island.cc.arg_parser import parse_cli_args
|
||||
|
||||
gevent_monkey.patch_all()
|
||||
|
||||
import json # noqa: E402
|
||||
import os # noqa: E402
|
||||
|
||||
from monkey_island import config_loader # 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.
|
||||
setup_params = SetupParamFactory.build(island_args)
|
||||
|
||||
try:
|
||||
server_config_path = os.path.expanduser(island_args.server_config)
|
||||
|
||||
config = config_loader.load_server_config_from_file(server_config_path)
|
||||
|
||||
setup_logging(config["data_dir"], config["log_level"])
|
||||
# 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(setup_params.data_dir, setup_params.log_level)
|
||||
|
||||
except OSError as ex:
|
||||
print(f"Error opening server config file: {ex}")
|
||||
|
@ -32,4 +30,4 @@ if "__main__" == __name__:
|
|||
|
||||
from monkey_island.cc.main import main # noqa: E402
|
||||
|
||||
main(config["data_dir"], island_args.setup_only, island_args.server_config)
|
||||
main(setup_params.data_dir, setup_params.setup_only, setup_params.server_config_path)
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH
|
||||
|
||||
|
||||
@dataclass
|
||||
class IslandArgs:
|
||||
setup_only: bool
|
||||
server_config: str
|
||||
server_config_path: str
|
||||
|
||||
|
||||
def parse_cli_args() -> IslandArgs:
|
||||
|
@ -25,10 +23,7 @@ def parse_cli_args() -> IslandArgs:
|
|||
"compiling/packaging Islands.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--server-config",
|
||||
action="store",
|
||||
help="The path to the server configuration file.",
|
||||
default=DEFAULT_SERVER_CONFIG_PATH,
|
||||
"--server-config", action="store", help="The path to the server configuration file."
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ __author__ = "itay.mizeretz"
|
|||
MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island")
|
||||
DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5
|
||||
|
||||
# TODO move setup consts
|
||||
DEFAULT_SERVER_CONFIG_PATH = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json")
|
||||
|
||||
DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
|
||||
|
@ -12,3 +13,6 @@ DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
|
|||
)
|
||||
|
||||
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")
|
||||
DEFAULT_LOG_LEVEL = "INFO"
|
||||
DEFAULT_START_MONGO_DB = True
|
||||
DEFAULT_SHOULD_SETUP_ONLY = False
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
import json
|
||||
|
||||
|
||||
def load_server_config_from_file(server_config_path):
|
||||
with open(server_config_path, "r") as f:
|
||||
config_content = f.read()
|
||||
config = json.loads(config_content)
|
||||
return config
|
|
@ -1,25 +0,0 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
|
||||
|
||||
DEFAULT_LOG_LEVEL = "INFO"
|
||||
|
||||
|
||||
def load_server_config_from_file(server_config_path):
|
||||
with open(server_config_path, "r") as f:
|
||||
config_content = f.read()
|
||||
config = json.loads(config_content)
|
||||
add_default_values_to_config(config)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def add_default_values_to_config(config):
|
||||
config["data_dir"] = os.path.abspath(
|
||||
os.path.expanduser(os.path.expandvars(config.get("data_dir", DEFAULT_DATA_DIR)))
|
||||
)
|
||||
|
||||
config.setdefault("log_level", DEFAULT_LOG_LEVEL)
|
||||
|
||||
return config
|
|
@ -0,0 +1,43 @@
|
|||
import os
|
||||
|
||||
from setup_params import SetupParams
|
||||
|
||||
from monkey_island import config_file_parser
|
||||
from monkey_island.cc.arg_parser import IslandArgs
|
||||
|
||||
|
||||
class SetupParamFactory:
|
||||
@staticmethod
|
||||
def build(cmd_args: IslandArgs) -> SetupParams:
|
||||
|
||||
setup_params = SetupParams()
|
||||
|
||||
setup_params = SetupParamFactory._update_by_cmd_args(setup_params, cmd_args)
|
||||
setup_params = SetupParamFactory._update_by_config_file(setup_params)
|
||||
|
||||
return setup_params
|
||||
|
||||
@staticmethod
|
||||
def _update_by_cmd_args(setup_params: SetupParams, cmd_args: IslandArgs) -> SetupParams:
|
||||
if type(cmd_args.setup_only) == bool:
|
||||
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)
|
||||
|
||||
return setup_params
|
||||
|
||||
@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:
|
||||
setup_params.data_dir = config["data_dir"]
|
||||
|
||||
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
|
|
@ -0,0 +1,18 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
from monkey_island.cc.server_utils.consts import (
|
||||
DEFAULT_DATA_DIR,
|
||||
DEFAULT_LOG_LEVEL,
|
||||
DEFAULT_SERVER_CONFIG_PATH,
|
||||
DEFAULT_SHOULD_SETUP_ONLY,
|
||||
DEFAULT_START_MONGO_DB,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SetupParams:
|
||||
server_config_path = DEFAULT_SERVER_CONFIG_PATH
|
||||
log_level = DEFAULT_LOG_LEVEL
|
||||
data_dir = DEFAULT_DATA_DIR
|
||||
start_mongodb = DEFAULT_START_MONGO_DB
|
||||
setup_only = DEFAULT_SHOULD_SETUP_ONLY
|
Loading…
Reference in New Issue