diff --git a/danix/danixfs.py b/danix/danixfs.py index 47df02c..dd1a5a5 100644 --- a/danix/danixfs.py +++ b/danix/danixfs.py @@ -1,6 +1,8 @@ import os, settings, app from sh import du from settings import MAIN_REPO +from utils import download_checksum, validate_checksum +from utils import get_message, check_equal_sentence as equals, generate_checksum, remove_checksum_files class Danix(): @staticmethod @@ -69,29 +71,43 @@ class Danix(): os.system(f"mkdir /tmp/{filesystem}") os.system(f"curl --silent -LO --output-dir /tmp/{filesystem} {settings.REPO_NAME}/{settings.ROOT_FS}") - os.system(f"tar -xf /tmp/{filesystem}/{settings.ROOT_FS} -C /tmp/{filesystem}") - os.system(f"rm /tmp/{filesystem}/{settings.ROOT_FS}") - os.system(f"mv /tmp/{filesystem} {MAIN_REPO}") - - print("\nPlease! Wait a moment!!") - print("Building container:") - print(f"Installing {len(packages)} packages\n") - - for command in config_comands: - os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs {command}") - - for package in packages: - os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add {package}") - - os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add fish") - os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add ruby") - os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs gem install lolcat") - os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/dev >/dev/null 2>&1") - os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/proc >/dev/null 2>&1") - os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/sys >/dev/null 2>&1") - - print(f"Environment builded succesfully!") - print("0 erros reported!") + resp_downloaded_checksum = download_checksum() + resp_generateed_checksum = generate_checksum(f"/tmp/{filesystem}/{settings.ROOT_FS}") - Danix.navigate(filesystem) + is_downloaded_and_generate = equals(resp_downloaded_checksum, 0) and equals(resp_generateed_checksum, 0) + + is_valid_checksum = validate_checksum(filesystem) + + remove_checksum_files() + + if is_downloaded_and_generate and is_valid_checksum: + + os.system(f"tar -xf /tmp/{filesystem}/{settings.ROOT_FS} -C /tmp/{filesystem}") + os.system(f"rm /tmp/{filesystem}/{settings.ROOT_FS}") + os.system(f"mv /tmp/{filesystem} {MAIN_REPO}") + + print("\nPlease! Wait a moment!!") + print("Building container:") + print(f"Installing {len(packages)} packages\n") + + for command in config_comands: + os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs {command}") + + for package in packages: + os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add {package}") + + os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add fish") + os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs apk add ruby") + os.system(f"chroot {MAIN_REPO}{filesystem}/danixfs gem install lolcat") + + os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/dev >/dev/null 2>&1") + os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/proc >/dev/null 2>&1") + os.system(f"rm -r {MAIN_REPO}{filesystem}/danixfs/sys >/dev/null 2>&1") + + print(f"Environment builded succesfully!") + print("0 erros reported!") + + Danix.navigate(filesystem) + else: + get_message("🔴 Failed integrity base image verification!", True, 1) diff --git a/danix/makefile b/danix/makefile index 242b9a1..282af67 100644 --- a/danix/makefile +++ b/danix/makefile @@ -50,7 +50,7 @@ else @$(PYTHON) $(DANIX_PATH)/manage.py migrate - @echo "Installation successfully!!! Please 'make aliases without sudo'" + @echo "Installation successfully!!! Please 'make aliases without' sudo" endif diff --git a/danix/utils.py b/danix/utils.py index 659b8d1..0220878 100644 --- a/danix/utils.py +++ b/danix/utils.py @@ -1,5 +1,33 @@ import os -from settings import MAIN_REPO, MAIN_DIR, BASE_DIR +import sh +from settings import MAIN_REPO, MAIN_DIR, BASE_DIR, REPO_NAME + + +def download_checksum(): + + return os.system(f"curl --silent -LO --output-dir /tmp/ {REPO_NAME}danixfs.checksum") + + +def generate_checksum(package_path): + os.system("touch /tmp/local_danixfs.checksum") + return os.system(f"sha256sum {package_path} >> /tmp/local_danixfs.checksum") + + +def remove_checksum_files(): + os.system("rm /tmp/danixfs.checksum >/dev/null 2>&1") + os.system("rm /tmp/local_danixfs.checksum >/dev/null 2>&1") + +def read_line(file_path): + file = open(file_path, 'r') + checksum = file.readlines() + return checksum + +def validate_checksum(filesystem): + + remote_checksum = ''.join(read_line(f"/tmp/danixfs.checksum")).replace(f"/tmp/{filesystem}/", "") + local_checksum = ''.join(read_line(f"/tmp/local_danixfs.checksum")).replace(f"/tmp/{filesystem}/", "") + + return True if local_checksum == remote_checksum else False def separate(path): list_path = path.split(" ") @@ -91,6 +119,7 @@ def print_environment_list_header(): @staticmethod def check_equal_sentence(left_expression, right_expression): + return left_expression == right_expression @staticmethod