Skip to content

Commit bede2b5

Browse files
Asaf Agamicat2608
Asaf Agami
andauthored
chore: pass explicit version to snyk images [HEAD-746] (#4870)
* chore: handle sha mismatch as failure * chore: pass version parameter to snyk-images * fix: handle version strings starting with "v" * refactor(install): simplify ternary when formatting version for download * chore: print CLI version to download --------- Co-authored-by: Catalina Oyaneder <[email protected]>
1 parent 2dab7ba commit bede2b5

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

release-scripts/upload-artifacts.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ upload_github() {
7878
--target "${CIRCLE_SHA1}" \
7979
--title "${VERSION_TAG}" \
8080
--notes-file binary-releases/RELEASE_NOTES.md
81-
81+
8282
echo "DRY RUN: deleting draft from GitHub..."
8383
gh release delete "${VERSION_TAG}" \
8484
--yes
@@ -115,7 +115,7 @@ trigger_build_snyk_images() {
115115
-H "Authorization: Bearer $HAMMERHEAD_GITHUB_PAT" \
116116
-H "X-GitHub-Api-Version: 2022-11-28" \
117117
https://api.github.com/repos/snyk/snyk-images/dispatches \
118-
-d '{"event_type":"build_and_push_images"}' \
118+
-d "{\"event_type\":\"build_and_push_images\", \"client_payload\": {\"version\": \"$VERSION_TAG\"}}" \
119119
-w "%{http_code}" \
120120
-o /dev/null)
121121
if [ "$RESPONSE" -eq 204 ]; then
@@ -207,9 +207,9 @@ for arg in "${@}"; do
207207
# Trigger building Snyk images in snyk-images repository
208208
elif [ "${arg}" == "trigger-snyk-images" ]; then
209209
trigger_build_snyk_images
210-
210+
211211
# Upload files to S3 bucket
212212
else
213213
upload_s3 "${target}"
214-
fi
214+
fi
215215
done

scripts/install-snyk.py

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ def get_os_arch():
1212
system = platform.system()
1313
machine = platform.machine()
1414

15-
if system == 'Linux':
16-
if machine == 'x86_64':
17-
return 'linux', 'amd64'
18-
elif machine == 'aarch64':
19-
return 'linux', 'arm64'
15+
if system == "Linux":
16+
if machine == "x86_64":
17+
return "linux", "amd64"
18+
elif machine == "aarch64":
19+
return "linux", "arm64"
2020
else:
2121
print("Unsupported architecture for Linux. Aborting download.")
2222
return None, None
23-
elif system == 'Windows':
24-
if machine == 'AMD64':
25-
return 'windows', 'amd64'
23+
elif system == "Windows":
24+
if machine == "AMD64":
25+
return "windows", "amd64"
2626
else:
2727
print("Unsupported architecture for Windows. Aborting download.")
2828
return None, None
29-
elif system == 'Darwin':
30-
if machine == 'x86_64':
31-
return 'macos', 'amd64'
32-
elif machine == 'arm64':
33-
return 'macos', 'arm64'
29+
elif system == "Darwin":
30+
if machine == "x86_64":
31+
return "macos", "amd64"
32+
elif machine == "arm64":
33+
return "macos", "arm64"
3434
else:
3535
print("Unsupported architecture for macOS. Aborting download.")
3636
return None, None
@@ -40,6 +40,9 @@ def get_os_arch():
4040

4141

4242
def download_snyk_cli(download_version, base_url):
43+
success = 0
44+
fail = 1
45+
4346
os_type, arch_type = get_os_arch()
4447

4548
if not os_type or not arch_type:
@@ -48,7 +51,8 @@ def download_snyk_cli(download_version, base_url):
4851
filename, output_filename = get_filename(arch_type, os_type)
4952

5053
if download_version != "latest":
51-
download_version = f"v{download_version}"
54+
if download_version[0] != "v": # Add a "v" prefix if it's missing
55+
download_version = f"v{download_version}"
5256

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

@@ -64,7 +68,7 @@ def download_snyk_cli(download_version, base_url):
6468

6569
downloaded_file_path = filename
6670

67-
with open(downloaded_file_path, 'wb') as f:
71+
with open(downloaded_file_path, "wb") as f:
6872
f.write(response.content)
6973

7074
if verify_checksum(downloaded_file_path, sha256_checksum):
@@ -83,30 +87,31 @@ def download_snyk_cli(download_version, base_url):
8387
else:
8488
os.remove(downloaded_file_path)
8589
print("SHA256 checksum verification failed. Downloaded file deleted.")
86-
return 0
90+
return fail
91+
return success
8792
else:
8893
print(f"Failed to download Snyk CLI {download_version}")
89-
return 1
94+
return fail
9095

9196

9297
def get_filename(arch_type, os_type):
9398
filename = ""
9499
output_filename = "snyk"
95100
suffix = ""
96101

97-
if os_type == 'linux' and arch_type == 'arm64':
102+
if os_type == "linux" and arch_type == "arm64":
98103
filename = "snyk-linux-arm64"
99-
if os_type == 'linux' and arch_type == 'amd64':
104+
if os_type == "linux" and arch_type == "amd64":
100105
filename = "snyk-linux"
101106
stat_result = os.path.exists("/lib/ld-musl-x86_64.so.1")
102107
if stat_result:
103108
filename = "snyk-alpine"
104-
if os_type == 'windows' and arch_type == 'amd64':
109+
if os_type == "windows" and arch_type == "amd64":
105110
filename = "snyk-win"
106111
suffix = ".exe"
107-
if os_type == 'macos' and arch_type == 'amd64':
112+
if os_type == "macos" and arch_type == "amd64":
108113
filename = "snyk-macos"
109-
if os_type == 'macos' and arch_type == 'arm64':
114+
if os_type == "macos" and arch_type == "arm64":
110115
filename = "snyk-macos-arm64"
111116

112117
filename = filename + suffix
@@ -117,7 +122,7 @@ def get_filename(arch_type, os_type):
117122

118123
def verify_checksum(file_path, expected_checksum):
119124
sha256 = hashlib.sha256()
120-
with open(file_path, 'rb') as f:
125+
with open(file_path, "rb") as f:
121126
while True:
122127
data = f.read(65536)
123128
if not data:
@@ -127,19 +132,36 @@ def verify_checksum(file_path, expected_checksum):
127132

128133

129134
if __name__ == "__main__":
130-
parser = argparse.ArgumentParser(description="Download and install a specific version of Snyk CLI.")
131-
parser.add_argument("version", help="Version of Snyk CLI to download (e.g., 1.123.456)")
132-
parser.add_argument("--base_url", help="Base URL to download from", default="https://static.snyk.io")
135+
parser = argparse.ArgumentParser(
136+
description="Download and install a specific version of Snyk CLI."
137+
)
138+
parser.add_argument(
139+
"version", help="Version of Snyk CLI to download (e.g., 1.123.456)"
140+
)
141+
parser.add_argument(
142+
"--base_url", help="Base URL to download from", default="https://static.snyk.io"
143+
)
133144
parser.add_argument("--retry", help="number of retries", default=3)
134145

135146
args = parser.parse_args()
136147

137148
for retry in range(1, args.retry + 1):
138-
print("Trying to download: #" + str(retry) + " of #" + str(args.retry))
149+
print(
150+
"Trying to download version "
151+
+ str(args.version)
152+
+ ": #"
153+
+ str(retry)
154+
+ " of #"
155+
+ str(args.retry)
156+
)
139157
ret_value = download_snyk_cli(args.version, args.base_url)
140158
if ret_value == 0:
141159
break
142160
else:
143161
sleep_time = retry * 10
144-
print("Failed to download Snyk CLI. Retrying in "+str(sleep_time) +" seconds...")
162+
print(
163+
"Failed to download Snyk CLI. Retrying in "
164+
+ str(sleep_time)
165+
+ " seconds..."
166+
)
145167
time.sleep(sleep_time)

0 commit comments

Comments
 (0)