diff --git a/scripts/install-snyk.py b/scripts/install-snyk.py index c3368face6..19edc4c6ed 100755 --- a/scripts/install-snyk.py +++ b/scripts/install-snyk.py @@ -59,43 +59,51 @@ def download_snyk_cli(download_version, base_url, use_backup_url=1): url = f"{base_url}/cli/{download_version}/{filename}" print(f"Downloading '{filename}' from: {url}") - response = requests.get(url) - - if response.status_code == 200: - sha_response = requests.get(url + ".sha256") - if not sha_response: - print("SHA256 checksum not available. Aborting download.") - return - - sha256_checksum = sha_response.text.split()[0] - - downloaded_file_path = filename - - with open(downloaded_file_path, "wb") as f: - f.write(response.content) - - print("Verifying checksum") - if verify_checksum(downloaded_file_path, sha256_checksum): - print("Checksum verified") - os.rename(downloaded_file_path, filename) - print(f"Snyk CLI {download_version} downloaded successfully to {filename}") - - # Make the file executable - os.chmod(filename, 0o755) - os.rename(filename, output_filename) - - print("Running 'snyk -v' to check the version:") - - executable = os.path.join(os.getcwd(), output_filename) - os.system(f"{executable} -v") - + + try: + response = requests.get(url) + + if response.status_code == 200: + sha_response = requests.get(url + ".sha256") + if not sha_response: + print("SHA256 checksum not available. Aborting download.") + return + + sha256_checksum = sha_response.text.split()[0] + + downloaded_file_path = filename + + with open(downloaded_file_path, "wb") as f: + f.write(response.content) + + print("Verifying checksum") + if verify_checksum(downloaded_file_path, sha256_checksum): + print("Checksum verified") + os.rename(downloaded_file_path, filename) + print(f"Snyk CLI {download_version} downloaded successfully to {filename}") + + # Make the file executable + os.chmod(filename, 0o755) + os.rename(filename, output_filename) + + print("Running 'snyk -v' to check the version:") + + executable = os.path.join(os.getcwd(), output_filename) + os.system(f"{executable} -v") + + else: + os.remove(downloaded_file_path) + print("SHA256 checksum verification failed. Downloaded file deleted.") + return fail + return success else: - os.remove(downloaded_file_path) - print("SHA256 checksum verification failed. Downloaded file deleted.") + print(f"Failed to download Snyk CLI {download_version} via {base_url}") + if use_backup_url == 1: + print(f"Trying download from: {backup_url}") + return download_snyk_cli(download_version, backup_url, use_backup_url=0) return fail - return success - else: - print(f"Failed to download Snyk CLI {download_version} via {base_url}") + except requests.RequestException as e: + print(f"Error trying to download Snyk CLI {download_version} via {base_url}: {e}") if use_backup_url == 1: print(f"Trying download from: {backup_url}") return download_snyk_cli(download_version, backup_url, use_backup_url=0)