Island: improve and fix data directory exception handling/logging

This commit is contained in:
VakarisZ 2021-10-25 18:28:44 +03:00
parent cebd41b264
commit 9ef9ba0024
2 changed files with 24 additions and 12 deletions

View File

@ -70,7 +70,8 @@ def _setup_data_dir(island_args: IslandCmdArgs) -> Tuple[IslandConfigOptions, st
except json.JSONDecodeError as ex:
print(f"Error loading server config: {ex}")
exit(1)
except IncompatibleDataDirectory:
except IncompatibleDataDirectory as ex:
print(f"Incompatible data directory: {ex}")
exit(1)

View File

@ -29,33 +29,44 @@ def _is_data_dir_old(data_dir_path: Path) -> bool:
dir_exists = data_dir_path.exists()
if is_running_on_docker():
if _data_dir_version_mismatch_exists(data_dir_path):
error_message = "Found an old volume. "
"You must create an empty volume for each docker container "
"as specified in setup documentation: "
"https://www.guardicore.com/infectionmonkey/docs/setup/docker/"
raise IncompatibleDataDirectory(error_message)
else:
return False
return _is_docker_data_dir_old(data_dir_path)
if not dir_exists or not os.listdir(data_dir_path):
if not dir_exists or _is_directory_empty(data_dir_path):
return False
return _data_dir_version_mismatch_exists(data_dir_path)
def _is_docker_data_dir_old(data_dir_path: Path) -> bool:
if _data_dir_version_mismatch_exists(data_dir_path):
if _is_directory_empty(data_dir_path):
return False
else:
raise IncompatibleDataDirectory(
"Found an old volume. "
"You must create an empty volume for each docker container "
"as specified in setup documentation: "
"https://www.guardicore.com/infectionmonkey/docs/setup/docker/"
)
else:
return False
def _is_directory_empty(path: Path) -> bool:
return not os.listdir(path)
def _handle_old_data_directory(data_dir_path: Path) -> None:
should_delete_data_directory = _prompt_user_to_delete_data_directory(data_dir_path)
if should_delete_data_directory:
shutil.rmtree(data_dir_path)
logger.info(f"{data_dir_path} was deleted.")
else:
logger.error(
raise IncompatibleDataDirectory(
"Unable to set up data directory. Please backup and delete the existing data directory"
f" ({data_dir_path}). Then, try again. To learn how to restore and use a backup, please"
" refer to the documentation."
)
raise IncompatibleDataDirectory()
def _prompt_user_to_delete_data_directory(data_dir_path: Path) -> bool: