Create `data_dir` if no `--server-config` is passed during Monkey Island initialisation

This commit is contained in:
Shreya 2021-05-12 19:41:49 +05:30
parent 6eb377858d
commit 76d82cecea
4 changed files with 20 additions and 12 deletions

View File

@ -16,7 +16,10 @@ if "__main__" == __name__:
# This is here in order to catch EVERYTHING, some functions are being called on # This is here in order to catch EVERYTHING, some functions are being called on
# imports, so the log init needs to be first. # imports, so the log init needs to be first.
try: try:
if island_args.server_config:
server_config_path = os.path.expanduser(island_args.server_config) server_config_path = os.path.expanduser(island_args.server_config)
else:
server_config_path = config_loader.create_default_server_config_path()
config = config_loader.load_server_config_from_file(server_config_path) config = config_loader.load_server_config_from_file(server_config_path)
@ -32,4 +35,4 @@ if "__main__" == __name__:
from monkey_island.cc.main import main # noqa: E402 from monkey_island.cc.main import main # noqa: E402
main(config["data_dir"], island_args.setup_only, island_args.server_config) main(config["data_dir"], island_args.setup_only, server_config_path)

View File

@ -1,7 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from monkey_island.cc.server_utils.consts import DEFAULT_SERVER_CONFIG_PATH
@dataclass @dataclass
class IslandArgs: class IslandArgs:
@ -25,10 +23,7 @@ def parse_cli_args() -> IslandArgs:
"compiling/packaging Islands.", "compiling/packaging Islands.",
) )
parser.add_argument( parser.add_argument(
"--server-config", "--server-config", action="store", help="The path to the server configuration file."
action="store",
help="The path to the server configuration file.",
default=DEFAULT_SERVER_CONFIG_PATH,
) )
args = parser.parse_args() args = parser.parse_args()

View File

@ -3,12 +3,13 @@ import os
__author__ = "itay.mizeretz" __author__ = "itay.mizeretz"
MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island") MONKEY_ISLAND_ABS_PATH = os.path.join(os.getcwd(), "monkey_island")
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")
DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5
DEFAULT_SERVER_CONFIG_PATH = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json") DEFAULT_SERVER_CONFIG_PATH = os.path.join(DEFAULT_DATA_DIR, "server_config.json")
DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join( DEFAULT_DEVELOP_SERVER_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop" MONKEY_ISLAND_ABS_PATH, "cc", "server_config.json.develop"
) )
DEFAULT_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc")

View File

@ -1,11 +1,20 @@
import json import json
import os import os
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR import monkey_island.cc.environment.server_config_generator as server_config_generator
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, DEFAULT_SERVER_CONFIG_PATH
DEFAULT_LOG_LEVEL = "INFO" DEFAULT_LOG_LEVEL = "INFO"
def create_default_server_config_path():
if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH):
if not os.path.isdir(DEFAULT_DATA_DIR):
os.mkdir(DEFAULT_DATA_DIR, mode=0o700)
server_config_generator.create_default_config_file(DEFAULT_SERVER_CONFIG_PATH)
return DEFAULT_SERVER_CONFIG_PATH
def load_server_config_from_file(server_config_path): def load_server_config_from_file(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()