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

Improve reliability of downloading PE tarball #215

Merged
merged 12 commits into from
Sep 28, 2021
Merged

Improve reliability of downloading PE tarball #215

merged 12 commits into from
Sep 28, 2021

Conversation

mcka1n
Copy link
Contributor

@mcka1n mcka1n commented Sep 27, 2021

This fix makes it so that when files are downloaded, they are first downloaded to a temp file and moved into place only if the download completes and is successful. This avoids a scenario where a file can be partially downloaded, after which PEAdm thinks that the file is present and will attempt to use the partial file as-is, rather than re-downloading.

There are still ways that an incorrect file can end up being placed—namely, the download server returning HTTP 200 but the wrong file. This fix though should eliminate the problem caused by partial interrupted downloads.

Changes

  • I'm validating whether curl successfully downloaded the file so we can throw an error

tasks/download.sh Outdated Show resolved Hide resolved
tasks/download.sh Outdated Show resolved Hide resolved
tasks/download.sh Outdated Show resolved Hide resolved
tasks/download.sh Outdated Show resolved Hide resolved
tasks/download.sh Outdated Show resolved Hide resolved
mcka1n and others added 5 commits September 27, 2021 17:57
Co-authored-by: Reid Vandewiele <reid@puppet.com>
Co-authored-by: Reid Vandewiele <reid@puppet.com>
Co-authored-by: Reid Vandewiele <reid@puppet.com>
Co-authored-by: Reid Vandewiele <reid@puppet.com>
tasks/download.sh Show resolved Hide resolved
tasks/download.sh Outdated Show resolved Hide resolved
@reidmv
Copy link
Contributor

reidmv commented Sep 27, 2021

I just realized something. This task is peadm::download, not peadm::download_pe. It was originally supposed to be a general purpose downloader. So, we should actually remove the tarball specific validation for now.

  1. Remove valid tarball check
  2. Ensure that if curl returns non-zero, the function returns non-zero

Maybe in the future we should consider creating a peadm::download_pe task, which would be a more appropriate place to get specific.

@mcka1n mcka1n changed the title uses temfile when downloading PE tarball Ensures that if curls returns non-zero, the func returns non-zero Sep 28, 2021
@mcka1n mcka1n marked this pull request as ready for review September 28, 2021 12:46
@mcka1n mcka1n requested a review from a team as a code owner September 28, 2021 12:46
@mcka1n mcka1n changed the title Ensures that if curls returns non-zero, the func returns non-zero Ensures that if curl returns non-zero, the func returns non-zero Sep 28, 2021
@mcka1n
Copy link
Contributor Author

mcka1n commented Sep 28, 2021

Note: After this PR gets approved, I would like to squash the commits, so we avoid moving to the main redundant commits I made for debugging purposes.

return 0
else
echo "Error: Curl has failed to download the file"
echo "|_ Removing temporary file: ${tmp_file}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a typo?

Suggested change
echo "|_ Removing temporary file: ${tmp_file}"
echo "Removing temporary file: ${tmp_file}"

curl -s -f -L -o "$2" "$1"
tmp_file=$(mktemp "peadm-download.XXX")
echo "Temporary file created at: ${tmp_file}"
download_file=$(curl -s -f -L -o ${tmp_file} "$1")
Copy link
Contributor

@reidmv reidmv Sep 28, 2021

Choose a reason for hiding this comment

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

The output of the curl command doesn't tell us if it succeeded or not. What we want to judge on is the exit code. There are three ways to do that, that I know of.

Option 1: && chaining

curl -s -f -L -o ${tmp_file} "$1" \
  && mv "${tmp_file}" "$2" \
  && return 0

# Failure to return means something failed
echo "Error: Curl has failed to download the file"
echo "Removing temporary file: ${tmp_file}"
rm "${tmp_file}"
return 1

Option 2: use an if statement

if curl -s -f -L -o ${tmp_file} "$1"; then
  mv "${tmp_file}" "$2"
  return 0
else
  echo "Error: Curl has failed to download the file"
  echo "Removing temporary file: ${tmp_file}"
  rm "${tmp_file}"
  return 1
fi

Option 3: check/capture $?

curl -s -f -L -o ${tmp_file} "$1"
curl_exit_code="$?"
if [[ "$curl_exit_code" = 0 ]]; then
  mv "${tmp_file}" "$2"
  return 0
else
  echo "Error: Curl has failed to download the file; exit code ${curl_exit_code}"
  echo "Removing temporary file: ${tmp_file}"
  rm "${tmp_file}"
  return 1
fi

I think I would probably go with option 1 because of my familiarity with it in lots of scripts from my past, but whichever of these is most readable would work great. The key bit is that we care about curl's exit code, not about its output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions @reidmv I went with option No.2, I find it more readable :)

I'm waiting for the Install Github Action to finish

tasks/download.sh Outdated Show resolved Hide resolved
Co-authored-by: Reid Vandewiele <reid@puppet.com>
@reidmv reidmv self-requested a review September 28, 2021 18:20
@reidmv
Copy link
Contributor

reidmv commented Sep 28, 2021

A transient agent install failure was the only problem with the smoke tests:

Finished: task peadm::agent_install with 1 failure in 6.68 sec

All other agent installs in that arch, and all agent installs in other architectures and versions, worked. So, transient.

LGTM! 👍

@reidmv reidmv added the bugfix label Sep 28, 2021
@reidmv reidmv changed the title Ensures that if curl returns non-zero, the func returns non-zero Improve reliability of downloading PE tarball Sep 28, 2021
@reidmv reidmv merged commit 3924918 into main Sep 28, 2021
@reidmv reidmv deleted the SOLARCH-622 branch September 28, 2021 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants