Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback to default url #433

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ def get_version_history(bazelisk_directory):
),
# This only handles versions with numeric components, but that is fine
# since prerelease versions have been excluded.
key=lambda version: tuple(int(component)
for component in version.split('.')),
key=lambda version: tuple(int(component) for component in version.split(".")),
reverse=True,
)

Expand Down Expand Up @@ -308,6 +307,8 @@ def download_bazel_into_directory(version, is_commit, directory):

sha256_path = destination_path + ".sha256"
expected_hash = ""
matcher = re.compile(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?")
matched = matcher.match(version)
if not os.path.exists(sha256_path):
try:
download(bazel_url + ".sha256", sha256_path)
Expand All @@ -316,7 +317,20 @@ def download_bazel_into_directory(version, is_commit, directory):
sys.stderr.write(
"The Bazel mirror does not have a checksum file; skipping checksum verification."
)
return destination_path
if "https://releases.bazel.build" in bazel_url or not matched:
return destination_path
if matched:
(version, rc) = matched.groups()
fallback_url = "https://releases.bazel.build/{}/{}/{}".format(
version, rc if rc else "release", bazel_filename
)
try:
download("{}.sha256".format(fallback_url), sha256_path)
os.remove(destination_path)
download(fallback_url, destination_path)
except HTTPError:
return destination_path
os.chmod(destination_path, 0o755)
raise e
with open(sha256_path, "r") as sha_file:
expected_hash = sha_file.read().split()[0]
Expand All @@ -326,16 +340,24 @@ def download_bazel_into_directory(version, is_commit, directory):
sha256_hash.update(byte_block)
actual_hash = sha256_hash.hexdigest()
if actual_hash != expected_hash:
os.remove(destination_path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why keep the destination_path folder? Doesn't this mean the binary binary downloaded is corrupted?
We should probably remove the folder and create it again to download the fallback URL

os.remove(sha256_path)
os.remove(destination_path)
print(
"The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Please try again.".format(
"The downloaded Bazel binary is corrupted. Expected SHA-256 {}, got {}. Fallback to default releases.bazel.build url.".format(
expected_hash, actual_hash
)
)
# Exiting with a special exit code not used by Bazel, so the calling process may retry based on that.
# https://docs.bazel.build/versions/0.21.0/guide.html#what-exit-code-will-i-get
sys.exit(22)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this exit code? It seems like we need it in case the caller retries on this exit code

if matched:
(version, rc) = matched.groups()
fallback_url = "https://releases.bazel.build/{}/{}/{}".format(
version, rc if rc else "release", bazel_filename
)
try:
download("{}.sha256".format(fallback_url), sha256_path)
download(fallback_url, destination_path)
except HTTPError:
exit(22)
os.chmod(destination_path, 0o755)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the download fails for the exit URL, we should probably exit with code 22.

return destination_path


Expand Down