Skip to content

Commit

Permalink
Update deploy-ftp.sh
Browse files Browse the repository at this point in the history
Adding retry logic for creatin the tar ball to ensure what we upload is valid.
Looping back to creating the tar ball instead of looping on the download.
  • Loading branch information
oilcan-productions authored and larsbrinkhoff committed Aug 28, 2024
1 parent 63b10e0 commit fa5fd06
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions build/deploy-ftp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,69 @@ echo "login $USER" >> "$NETRC"
echo "password $FTP_SECRET" >> "$NETRC"
chmod 600 "$NETRC"

upload_file(){
(cd out; tar czf $EMULATOR.tgz $EMULATOR)
# create_archive will create a tar ball from the out folder
# try to extract it to see if it is valid
# retry 3 times then fail
create_archive() {
MAX_RETRIES=3
RETRY_COUNT=0

echo "Deploying as $USER at $HOST"
while [ "$RETRY_COUNT" -lt "$MAX_RETRIES" ]; do
(cd out; tar czf $EMULATOR.tgz $EMULATOR)

ftp "$HOST" <<EOF
# Verify the tarball
if tar tf $EMULATOR.tgz >/dev/null 2>&1; then
echo "Tarball is valid and can be expanded."
break
else
echo "Tarball is not valid or cannot be expanded. Retrying..."
RETRY_COUNT=`expr "$RETRY_COUNT" + 1`
rm -f $EMULATOR.tgz
continue
fi
done

if [ "$RETRY_COUNT" = "$MAX_RETRIES" ]; then
echo "Failed to create a valid tarball after $MAX_RETRIES attempts."
return 1
fi
}

# upload_file tries to upload the tar ball to the FTP server, will retry 5 times and then fail
upload_file() {
MAX_RETRIES=5
RETRY_COUNT=0

while [ "$RETRY_COUNT" -lt "$MAX_RETRIES" ]; do
echo "Deploying as $USER at $HOST"

# Attempt to upload the file
if ftp "$HOST" <<EOF
passive on
type image
cd $DIR
lcd out
put $EMULATOR.tgz
bye
EOF
then
echo "Upload successful."
break
else
echo "Upload failed. Retrying..."
RETRY_COUNT=`expr "$RETRY_COUNT" + 1`
fi
done

if [ "$RETRY_COUNT" = "$MAX_RETRIES" ]; then
echo "Failed to upload the file after $MAX_RETRIES attempts."
return 1
fi
}

test_archive_integrity(){
# test_archive_integrity will download the tarball after successful upload and verify its integrity
# by binary comparing the contents of the source folder and the expanded folder
test_archive_integrity() {
echo "Testing download of $EMULATOR.tgz"
mkdir -p "$TESTDIR"
cd "$TESTDIR"
Expand Down Expand Up @@ -78,15 +125,16 @@ test_archive_integrity(){
fi
}


# main loop
while [ $retry_count -lt $RETRY_LIMIT ]; do
create_archive
upload_file
if test_archive_integrity; then
echo "File integrity verified successfully."
exit 0
else
echo "File integrity verification failed, retrying... ($((retry_count+1))/$RETRY_LIMIT)"
retry_count=$((retry_count+1))
echo "File integrity verification failed, retrying... (`expr "$retry_count" + 1`/$RETRY_LIMIT)"
retry_count=`expr "$retry_count" + 1`
fi
done

Expand Down

0 comments on commit fa5fd06

Please sign in to comment.