diff --git a/sidecarinit/commands.py b/sidecarinit/commands.py index 02636f1..54c1fd1 100644 --- a/sidecarinit/commands.py +++ b/sidecarinit/commands.py @@ -25,8 +25,7 @@ def run(env_yml): Path(suite.local_repository).mkdir(parents=True, exist_ok=True) if archive_name: - download.download_archive(suite.archive_api + "/" + archive_name, - suite.local_repository) + download.download_archive(suite.archive_api, suite.local_repository) else: logger.info('BUILD_CONFIG_ID does not exist, exit now.') diff --git a/sidecarinit/download.py b/sidecarinit/download.py index d244e1b..9f7a65d 100644 --- a/sidecarinit/download.py +++ b/sidecarinit/download.py @@ -1,17 +1,20 @@ import urllib.request import shutil +import hashlib import os -from zipfile import ZipFile -from zipfile import BadZipFile +import zlib +from zipfile import ZipFile, BadZipFile def download_archive(url, repository): build_id = os.environ.get('BUILD_CONFIG_ID') - archive_path = f"/{build_id}.zip" + archive_path = f"{build_id}.zip" + zip_path = os.path.join(repository, archive_path) try: + url = f"{url}/{build_id}" print(f"Trying to download archive from url {url}\n") with urllib.request.urlopen(url) as response: - with open(repository + archive_path, 'wb') as out_file: + with open(zip_path, 'wb') as out_file: shutil.copyfileobj(response, out_file) downloaded = True except urllib.error.HTTPError as e: @@ -22,10 +25,41 @@ def download_archive(url, repository): print(f"{e.code}: {e.reason}\n\n{e.headers}") if downloaded: + checksum = get_file_sha256(zip_path) + if checksum: + print(f"ZIP file SHA-256: {checksum}") + print(f"Archive is downloaded successfully, unpacking it in {repository}\n") try: - with ZipFile(repository + archive_path) as archive_zip: + with ZipFile(zip_path) as archive_zip: archive_zip.extractall(repository) print("Unpacked successfully.\n") - except BadZipFile: - print("Downloaded archive can not be extracted since it's a bad zip file.\n") + except (BadZipFile, zlib.error) as e: + print(f"Error extracting zip file: {str(e)}\n") + + print("Start cleaning up extracted files from local FS...") + try: + shutil.rmtree(repository) + print(f"Cleanup completed: {repository}\n") + except Exception as cleanup_e: + print(f"Error during cleanup: {str(cleanup_e)}\n") + + print("Start cleaning up zip file from Archive FS...") + clean_archive_url = f"{url}/{checksum}" + print(f"URL request: {clean_archive_url}") + req = urllib.request.Request(clean_archive_url, method='DELETE') + try: + urllib.request.urlopen(req) + except Exception as e: + print(f"DELETE Archive Error: {str(e)}") + +def get_file_sha256(file_path): + sha256_hash = hashlib.sha256() + try: + with open(file_path, "rb") as f: + for byte_block in iter(lambda: f.read(100 * 1024 * 1024), b""): + sha256_hash.update(byte_block) + return sha256_hash.hexdigest() + except Exception as e: + print(f"Error calculating SHA-256: {str(e)}") + return None \ No newline at end of file