Fast versions checker

Achterstraat

Verified User
Joined
Mar 21, 2015
Messages
76
Location
Netherlands
Code:
#!/bin/sh

if [ -f /etc/os-release ]; then
    . /etc/os-release
    if [[ "${NAME}" == "AlmaLinux" ]]; then
        updates="$(dnf check-update | awk 'p;/^$/{p=1}' | grep -c "\.")"
    else
        updates="$(yum check-updates | awk 'p;/^$/{p=1}' | grep -c "\.")"
    fi

    if [[ ${updates} != 0 ]]; then
        echo "${NAME} has ${updates} updates available!${bell}"
    fi
fi

custombuilds=()
versions=$(da build versions | grep -v '^$' | tr '/' '\n')
readarray -t array <<<"${versions}"

for (( i=0; i<((${#array[@]}-1)); i++ ));
do
    n=$((i + 1))
        if [ $((i%2)) -eq 0 ]; then
        a=${array[$i]##* of }
        b=${array[$n]##* of }
        software="${a%%:*}"
        currently="${a#*: }"
        available="${b#*: }"
        if [[ ${currently} != ${available} ]]; then
            if ! [[ "${custombuilds[@]}" =~ "${software}" ]]; then
                custombuilds+=("${software}")
            fi
        fi
    fi
done

if [ ${#custombuilds[@]} -gt 0 ]; then
    availabled=${custombuilds[@]}
    echo "Custombuild has ${#custombuilds[@]} updates available!${bell}"
fi

dav1="$(curl -s "https://www.directadmin.com/version.php" | cut -c 17-21 | sed 's/.\{4\}/&./')"
dav1a="${dav1}" | sed 's/[^0-9]//g'
dav2="$(/usr/local/directadmin/directadmin v | cut -c 13-17 | sed 's/.\{4\}/&./')"
dav2a="${dav2}" | sed 's/[^0-9]//g'
if [[ ${dav1} != ${dav2} ]]; then
    if [[ ${dav1a} -lt ${dav2a} ]]; then
        echo "Directadmin version ${dav1} available, currently ${dav2} installed!${bell}"
    else
        echo "Directadmin version ${dav2} betá installed, version ${dav1} stable!${bell}"
    fi
fi
 
Nice.
Tip: it gives a bit the impression that it does not really do anything when nothing is found.
Maybe you can add a notice like "no updates found today" for example or something similar. Looks just a little bit nicer.;)
 
@Achterstraat

Version detection can be simplified with:

Code:
current=$(da v | cut -d ' ' -f 2)
latest=$(dig +short current-version.directadmin.com txt | sed 's/.*v=\([^&]*\).*/\1/')

Version check over DNS should be much faster.

The DA itself is also always aware if update is available. So alternative approach is just ask DA for version info over API:

Code:
$ blob=$(curl -s $(da api-url)/api/version)
$ jq -r '.version + " " + .commit' <<< "${blob}"
1.677 e511a319e4ddb067e7033ccd14ff9c0c688e5744
$ jq -r '.update.version + " " + .update.commit' <<< "${blob}"
1.676 844f1d4cbe1b964d90d7a5a71e2c108c61e5944f

Consulting with DA over API will make sure it returns update from the currently configured update channel. In the example above it offers a downgrade to `current` release channel.

Note: There is also new API for checking system package updates as well. DA can report updates available via apt or yum with:

Code:
$ echo "You have $(curl -s $(da api-url)/api/system-packages/updates | jq '.packages | length') system package updates available"
You have 43 system package updates available
 
Back
Top