Restructure code for creating default data directory and server config

This commit is contained in:
Shreya 2021-05-17 17:42:25 +05:30
parent 9eedac4eb2
commit 805e5e6c22
5 changed files with 31 additions and 20 deletions

View File

@ -7,9 +7,10 @@ gevent_monkey.patch_all()
import json # noqa: E402 import json # noqa: E402
import os # 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 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 from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E402
from monkey_island.cc.setup import create_data_dir # noqa: E402
if "__main__" == __name__: if "__main__" == __name__:
island_args = parse_cli_args() island_args = parse_cli_args()
@ -20,7 +21,7 @@ if "__main__" == __name__:
if island_args.server_config: 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: else:
server_config_path = config_loader.create_default_server_config_path() server_config_path = server_config_generator.create_default_server_config_file()
config = config_loader.load_server_config_from_file(server_config_path) config = config_loader.load_server_config_from_file(server_config_path)

View File

@ -0,0 +1,13 @@
import os
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
def create_data_dir(data_dir: str) -> None:
if not os.path.isdir(data_dir):
os.makedirs(data_dir, mode=0o700)
def create_default_data_dir() -> None:
if not os.path.isdir(DEFAULT_DATA_DIR):
os.mkdir(DEFAULT_DATA_DIR, mode=0o700)

View File

@ -1,8 +1,20 @@
import os
from pathlib import Path from pathlib import Path
from monkey_island.cc.server_utils.consts import DEFAULT_DEVELOP_SERVER_CONFIG_PATH from monkey_island.cc.environment.data_dir_generator import create_default_data_dir
from monkey_island.cc.server_utils.consts import (
DEFAULT_DEVELOP_SERVER_CONFIG_PATH,
DEFAULT_SERVER_CONFIG_PATH,
)
def create_default_config_file(path): def create_default_server_config_file() -> str:
if not os.path.isfile(DEFAULT_SERVER_CONFIG_PATH):
create_default_data_dir()
write_default_server_config_to_file(DEFAULT_SERVER_CONFIG_PATH)
return DEFAULT_SERVER_CONFIG_PATH
def write_default_server_config_to_file(path: str) -> None:
default_config = Path(DEFAULT_DEVELOP_SERVER_CONFIG_PATH).read_text() default_config = Path(DEFAULT_DEVELOP_SERVER_CONFIG_PATH).read_text()
Path(path).write_text(default_config) Path(path).write_text(default_config)

View File

@ -1,5 +1,4 @@
import logging import logging
import os
from pymongo import errors from pymongo import errors
@ -50,8 +49,3 @@ def store_mitigations_on_mongo():
) )
for key, mongo_object in mongo_mitigations.items(): for key, mongo_object in mongo_mitigations.items():
mongo_object.save() mongo_object.save()
def create_data_dir(data_dir):
if not os.path.isdir(data_dir):
os.makedirs(data_dir, mode=0o700)

View File

@ -1,20 +1,11 @@
import json import json
import os import os
import monkey_island.cc.environment.server_config_generator as server_config_generator from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
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()