Skip to content

Commit

Permalink
Merge pull request #5377 from snyk/chore/CLI-413_updateDownloadURL
Browse files Browse the repository at this point in the history
chore(scripts): update install-snyk.py to use downloads.snyk.io by default
  • Loading branch information
PeterSchafer authored Jul 25, 2024
2 parents 235d4b8 + 6e409d0 commit 45fc941
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions scripts/install-snyk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import requests

backup_url = "https://static.snyk.io"

def get_os_arch():
system = platform.system()
Expand Down Expand Up @@ -39,7 +40,7 @@ def get_os_arch():
return None, None


def download_snyk_cli(download_version, base_url):
def download_snyk_cli(download_version, base_url, use_backup_url=1):
success = 0
fail = 1

Expand All @@ -50,12 +51,14 @@ def download_snyk_cli(download_version, base_url):

filename, output_filename = get_filename(arch_type, os_type)

if download_version != "latest":
is_non_prefixed_version = download_version in ["latest", "stable", "preview", "rc"]
if not is_non_prefixed_version:
if download_version[0] != "v": # Add a "v" prefix if it's missing
download_version = f"v{download_version}"

url = f"{base_url}/cli/{download_version}/{filename}"

print(f"Downloading '{filename}' from: {url}")
response = requests.get(url)

if response.status_code == 200:
Expand All @@ -71,7 +74,9 @@ def download_snyk_cli(download_version, base_url):
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}")

Expand All @@ -90,7 +95,10 @@ def download_snyk_cli(download_version, base_url):
return fail
return success
else:
print(f"Failed to download Snyk CLI {download_version}")
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


Expand Down Expand Up @@ -139,12 +147,13 @@ def verify_checksum(file_path, expected_checksum):
"version", help="Version of Snyk CLI to download (e.g., 1.123.456)"
)
parser.add_argument(
"--base_url", help="Base URL to download from", default="https://static.snyk.io"
"--base_url", help="Base URL to download from", default="https://downloads.snyk.io"
)
parser.add_argument("--retry", help="number of retries", default=3)

args = parser.parse_args()

# will try to download via the base_url and backup_url, if base_url fails, for each retry iteration
for retry in range(1, args.retry + 1):
print(
"Trying to download version "
Expand Down

0 comments on commit 45fc941

Please sign in to comment.