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

Change FTLcheckUpdate logic #5190

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions automated install/basic-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2442,15 +2442,23 @@ FTLcheckUpdate() {
if [[ ${ftlLoc} ]]; then
local FTLversion
FTLversion=$(/usr/bin/pihole-FTL tag)

local FTLlatesttag
FTLlatesttag=$(curl -s https://api.github.com/repos/pi-hole/FTL/releases/latest | jq -sRr 'fromjson? | .tag_name | values')

if ! FTLlatesttag=$(curl -sI https://github.com/pi-hole/FTL/releases/latest | grep --color=never -i Location: | awk -F / '{print $NF}' | tr -d '[:cntrl:]'); then
if [ -z "${FTLlatesttag}" ]; then
Copy link
Member

Choose a reason for hiding this comment

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

This will fail if there is a bad response from the curl command.

Viking:~$ tag=$(curl -s https://api.github.com/repos/pi-hole/FTL/releases/lates | jq -r .tag_name)
Viking:~$ echo $tag
null

Copy link
Member Author

Choose a reason for hiding this comment

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

I tested and never got null inside the script, but I will replace the command with:

curl -s https://api.github.com/repos/pi-hole/FTL/releases/lates | jq -r '.tag_name | values'

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you want to use jq for the test, like this?

curl -s https://api.github.com/repos/pi-hole/FTL/releases/lat | jq '.tag_name == "${FTLversion}"'

Copy link
Member Author

Choose a reason for hiding this comment

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

The current code will work for any curl response:

  • if curl returns a valid JSON, jq will try to find and return tag_name.
  • if no tag_name is found in JSON response, an empty string will be returned.
  • if curl responds with anything that is not a valid JSON, jq command will return an empty string.

Command details:

jq -sRr 'fromjson? | .tag_name | values'
  • -s will read the entire input file into a large array before apply the filters;
  • -R will not parse the input as JSON. When combined with -s, it reads the whole response into a single long string;
  • The ? is the error suppression operator and works like a simple try/catch.
  • fromjson? will try to parse the text into JSON format. If not possible will return null.
  • .tag_name will return the value of "tag_name" (or null if not found);
  • values will filter the result and return only "non null values" (returning an empty string in case of null).
  • -r will output the result as plain text (without the quotes).

# There was an issue while retrieving the latest version
printf " %b Failed to retrieve latest FTL release metadata" "${CROSS}"
return 3
fi

if [[ "${FTLversion}" != "${FTLlatesttag}" ]]; then
# Convert version strings into numbers for comparison
local convertedFTLversion convertedFTLlatesttag
convertedFTLversion="$(VersionConverter "${FTLversion}")"
convertedFTLlatesttag="$(VersionConverter "${FTLlatesttag}")"

if [[ "${convertedFTLversion}" -lt "${convertedFTLlatesttag}" ]]; then
# FTL is out of date
return 0
else
printf " %b Latest FTL Binary already installed (%s). Confirming Checksum...\\n" "${INFO}" "${FTLlatesttag}"
Expand Down Expand Up @@ -2502,6 +2510,12 @@ copy_to_install_log() {
chmod 644 "${installLogLoc}"
}

# converts a given version string e.g. v3.7.1 to 3007001000 to allow for easier comparison of multi digit version numbers
# credits https://apple.stackexchange.com/a/123408
VersionConverter() {
echo "$@" | tr -d '[:alpha:]' | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}

main() {
######## FIRST CHECK ########
# Must be root to install
Expand Down