Skip to content

Commit

Permalink
Rewrite set_snapshot_url
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdotb committed Mar 14, 2023
1 parent 8ab93ff commit de59a0a
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions installer/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,14 +1343,9 @@ def download_snapshot(self) -> bool:
# This is a blocking operation that will take a while
try:
# Only proceed if a valid snapshot URL has been set
if self.set_snapshot_url():
logging.info(f"Valid snapshot URL found: {self.snapshot_url}")
fname = os.path.basename(self.snapshot_url)
file_path = os.path.join(self.cheqd_root_dir, fname)
else:
logging.error(f"No valid snapshot URL found in last {MAX_SNAPSHOT_DAYS} days!")
raise
return False
snapshot_url = self.set_snapshot_url()
fname = os.path.basename(snapshot_url)
file_path = os.path.join(self.cheqd_root_dir, fname)

# Install dependencies needed to show progress bar
if self.install_dependencies():
Expand All @@ -1361,7 +1356,7 @@ def download_snapshot(self) -> bool:
return False

# Fetch size of snapshot archive WITHOUT downloading it
req = request.Request(self.snapshot_url, method='HEAD')
req = request.Request(snapshot_url, method='HEAD')
response = request.urlopen(req)
content_length = response.getheader("Content-Length")
if content_length is not None:
Expand Down Expand Up @@ -1402,7 +1397,7 @@ def download_snapshot(self) -> bool:
# Use wget to download since it can show a progress bar while downloading natively
# This is a blocking operation that will take a while
# "wget -c" will resume a download if it gets interrupted
self.exec(f"wget -c {self.snapshot_url} -P {self.cheqd_home_dir}")
self.exec(f"wget -c {snapshot_url} -P {self.cheqd_home_dir}")

if self.compare_checksum(file_path):
logging.info(f"Snapshot download was successful AND checksums match.")
Expand All @@ -1417,7 +1412,7 @@ def download_snapshot(self) -> bool:
logging.exception(f"Failed to download snapshot. Reason: {e}")
return False

def set_snapshot_url(self) -> bool:
def set_snapshot_url(self) -> str:
# Get latest available snapshot URL from snapshots.cheqd.net for the given chain
# This checks whether there are any snapshots in past MAX_SNAPSHOT_DAYS (default: 7 days)
try:
Expand All @@ -1436,15 +1431,13 @@ def set_snapshot_url(self) -> bool:

# Set snapshot URL if found
if valid_url_found:
self.snapshot_url = _url
logging.debug(f"Snapshot URL: {self.snapshot_url}")
return True
logging.info(f"Snapshot URL found: {_url}")
return _url
else:
logging.debug("Could not find a valid snapshot in last {} days".format(MAX_SNAPSHOT_DAYS))
return False
logging.error("Could not find a valid snapshot in last {} days".format(MAX_SNAPSHOT_DAYS))
raise
except Exception as e:
logging.exception(f"Failed to get snapshot URL. Reason: {e}")
return False

def install_dependencies(self) -> bool:
# Install dependencies required for snapshot extraction
Expand Down

0 comments on commit de59a0a

Please sign in to comment.