Skip to content

Commit

Permalink
download: handle case of s3 Forbidden
Browse files Browse the repository at this point in the history
on case of old artifacts which aren't configured to be publicly available
on s3 (Forbidden), we should fallback to download via the
http proxy (downloads.scylladb.com)
  • Loading branch information
fruch committed Jul 9, 2023
1 parent 33a6685 commit 974cea8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 1 addition & 2 deletions ccmlib/scylla_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,11 @@ def setup_scylla_manager(scylla_manager_package=None, verbose=False):
manager_install_dir = directory_name(os.path.join('manager', dir_hash))
if not os.path.exists(manager_install_dir):
os.makedirs(manager_install_dir)
destination_file = os.path.join(manager_install_dir, "manager.tar.gz")
_, destination_file = tempfile.mkstemp(suffix=".tar.gz", prefix="ccm-manager-")

if os.path.exists(scylla_manager_package) and scylla_manager_package.endswith('.tar.gz'):
destination_file = scylla_manager_package
elif is_valid(scylla_manager_package):
_, target = tempfile.mkstemp(suffix=".tar.gz", prefix="ccm-")
res = download_version_from_s3(url=scylla_manager_package, target_path=destination_file, verbose=verbose)
if not res:
download_file(url=scylla_manager_package, target_path=destination_file, verbose=verbose)
Expand Down
11 changes: 8 additions & 3 deletions ccmlib/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,15 @@ def download_version_from_s3(url: str, target_path: str, verbose=False):
try:
metadata = s3_client.head_object(Bucket=bucket_name, Key=download_path)
except botocore.client.ClientError as ex:
if 'Not Found' in str(ex):
error_message = ex.response['Error']['Message']
if error_message in ('NoSuchBucket', 'NoSuchKey', 'Not Found'):
logging.warning(f"url: '{url}' wasn't found on S3")
logging.warning(f"download might be very slow")
return None
elif error_message == "Forbidden":
logging.warning(f"url: '{url}' Forbidden (403) on S3")
logging.warning(f"download might be very slow")
return None
else:
raise

Expand All @@ -167,7 +172,7 @@ def get_url_hash(url: str) -> str:
"""

if os.path.exists(url): # if file/dir is local, hash based on the path
return hashlib.md5(url).hexdigest().encode()
return hashlib.md5(url).hexdigest()

# first try is on s3
parts = urllib.parse.urlparse(url)
Expand All @@ -177,6 +182,6 @@ def get_url_hash(url: str) -> str:
try:
metadata = s3_client.head_object(Bucket=bucket_name, Key=download_path)
return metadata.get('ETag')[1:-1]
except botocore.client.ClientError as ex:
except botocore.client.ClientError:
# fallback to http
return requests.head(url).headers.get('ETag')[1:-1]

0 comments on commit 974cea8

Please sign in to comment.