update

Frigate: Script to Check For Update


Manually checking for software updates is cumbersome, so here is another little script to check for a software update for Frigate, which runs inside a Docker Container.

Create Script

Create a file using your text editor, I use nano editor. Be sure to edit to match your setup in the Edit Area.

touch frigate_check nano -w frigate_check

#!/bin/bash

######################
##    EDIT AREA     ##
######################

# Optional: include [ test ] with call to script, i.e. ./frigate_check test
# Else Force Test API and return values only (0=TRUE;1=FALSE)
API_TEST=1
# Frigate IP and PORT
IPADDR="192.168.1.5"
IPPORT="5000"
# Frigate docker file location (docker-compose.yml)
FRIGATE_DOCKER="/srv/docker/Frigate"
# CURL timeout [seconds]
CURL_TIMEOUT=10
# Frigate REPO to Check
FRIGATE_REPO="blakeblackshear/frigate/releases/latest"
# Github API REPO
FRIGATE_GITHUB_API="https://api.github.com/repos/$FRIGATE_REPO"
# Frigate LOCAL to Check
FRIGATE_LOCAL_API="http://$IPADDR:$IPPORT/api/version"

######################
##     EDIT END     ##
######################

# Check cURL command if available (required), abort if does not exists
type curl >/dev/null 2>&1 || { echo -e >&2 "Requires 'curl' but it's not installed. Install with:\nsudo apt install curl\nAborting..."; exit 1; }
# Check jq command if available (required), abort if not installed
type jq >/dev/null 2>&1 || { echo -e >&2 "Requires 'jq' but it's not installed. Install with:\nsudo apt install jq\nAborting..."; exit 1; }

if [ -n "$1" ] && [ "$1" != "test" ]; then
     { echo -e >&2 "Unknown command '$1'\nAborting..."; exit 1; }
elif [ "$1" == "test" ]; then
    echo
    echo "-*- Testing API and Return Value -*-"
else
    echo -n "Checking if there is an newer version of Frigate"
fi
while (( i++ < 4 )); do
    echo -n " ."
    sleep 1
done
echo

CURRENT_CONNECT=$(curl --connect-timeout $CURL_TIMEOUT --head --write-out "%{http_code}" --output /dev/null --silent "$FRIGATE_GITHUB_API" )
INSTALL_CONNECT=$(curl --connect-timeout $CURL_TIMEOUT --head --write-out "%{http_code}" --output /dev/null --silent "$FRIGATE_LOCAL_API" )

if [[ "$CURRENT_CONNECT" -ne 200 ]]; then
    CURRENT="API unreachable, current version unavailable"
else
    #CURRENT=$(curl --connect-timeout $CURL_TIMEOUT --silent --request GET "$FRIGATE_GITHUB_API" -H "Accept: application/json" | jq ".name" -r)
    CURRENT=$(curl --silent --request GET "$FRIGATE_GITHUB_API" -H "Accept: application/json" | jq ".name" -r)
    CURRENT_VERSION=$(echo "$CURRENT" | sed -n "s/[^0-9]\+\([0-9.]\+\).*/\1/p" | sed -e "s/^[[:space:]]*$//")
fi
if [[ "$INSTALL_CONNECT" -ne 200 ]]; then
    INSTALL="API unreachable, installed version unavailable"
else
    #INSTALL=$(curl --connect-timeout $CURL_TIMEOUT --silent --request GET "$FRIGATE_LOCAL_API")
    INSTALL=$(curl --silent --request GET "$FRIGATE_LOCAL_API")
    INSTALL_VERSION=$(echo "$INSTALL" | cut -d"-" -f 1 | sed -n "s/[^0-9]\+\([0-9.]\+\).*/\0/p" | sed -e "s/^[[:space:]]*$//")
fi

## TEST cURL API RETURN VALUE ##
if  [[ $API_TEST -eq 0 ]] || [ "$1" == "test" ]; then
    echo "CURRENT $CURRENT"
    echo "INSTALL $INSTALL"
    echo "*/*  ##  *//*  ##  */*"
    echo "Current: $CURRENT_VERSION"
    echo "Installed: $INSTALL_VERSION"
    echo "*/*  ##  *//*  ##  */*"
	echo "-*- Change API_TEST to quit test -*-"
	echo
else
    if [[ -z "$CURRENT_VERSION" ]] && [[ "$INSTALL_VERSION" ]]; then
        echo "$CURRENT"
        echo "Installed local version: $INSTALL_VERSION"
        echo "Unable to determine if update is available"
    elif [[ -z "$INSTALL_VERSION" ]] && [[ "$CURRENT_VERSION" ]]; then
        echo "Current available version: $CURRENT_VERSION"
        echo "$INSTALL"
        echo "Unable to determine if update available"
    elif [[ "$CURRENT_VERSION" ]] && [[ "$INSTALL_VERSION" ]]; then
	    if  [ "$CURRENT_VERSION" != "$INSTALL_VERSION" ]; then
            echo "There is a newer version of Frigate available"
            echo "Update to Version: $CURRENT_VERSION"
            echo "Installed Version: $INSTALL_VERSION"
            read -p "Would you like to update now?(Y/n)" -n 1 -r UPDATE
            update=${UPDATE,,}
            if [[ "$update" =~ ^(y| ) ]] || [[ -z "$update" ]]; then
			    cd "$FRIGATE_DOCKER" || { echo -e >&2 "Unable to find Frigate docker file\nAborting..."; exit 1; }
                docker compose pull
                docker compose up -d --remove-orphans
            else
                echo
                echo "Frigate not updated at this time"
                echo "Frigate Running $INSTALL_VERSION"
            fi
        else
		    echo "Current available version: $CURRENT_VERSION"
		    echo "Installed local version: $INSTALL_VERSION"
		    echo "The most recent version of Frigate is installed ($INSTALL_VERSION)"
	    fi
    else
        echo "$CURRENT"
        echo "$INSTALL"
        echo "Try again later"
    fi
fi

Save the file. Example in nano: ctrl+o and ctrl+x

Then switch to a root shell to move the script file, set permissions, and set owner. Be sure to use your own location; I save mine in the docker folder for convenience.

sudo -s
mv frigate_check /srv/docker/
chmod +x /srv/docker/frigate_check
chmod 0750 /srv/docker/frigate_check
chown root:root /srv/docker/frigate_check

You can run manually or use cron to set a schedule to check automatically.

Reference

https://github.com/blakeblackshear/frigate/issues/2236

Simple Script to Update Ubuntu on CLI


Updating Ubuntu using the CLI is done by typing the commands one-by-one or combining the commands using the && operator, i.e. sudo apt update && sudo apt upgrade. Running multiple servers and having to update them by typing out the apt commands over and over can be repetitious so writing a simple script can save some time and stop from misspelling commands. The following will simplify updating your Ubuntu servers.

Update Script

This script is currently being used on Ubuntu 18.04 20.04 22.04. You can choose to uncomment the option to refresh snap if you use it on your system. Create a file using your text editor, I use nano editor.

touch updateos
nano -w updateos

Copy the script into the editor.

#!/bin/bash

# Use: sudo updateos [Option]
# Option: upgrade, dist-upgrade,full-upgrade, default=[upgrade]
# Example: sudo updateos upgrade

# Check if root
if [[ $EUID -ne 0 ]]; then
     echo "You forgot sudo"
     exit 1
fi
# Check for option
if [ -z $1 ]; then
    upgoption="upgrade"
elif [ -n $1 ]; then
     case $1 in
       "dist-upgrade")
          upgoption="dist-upgrade"
          ;;
       "full-upgrade")
          upgoption="full-upgrade"
          ;;
       *)
          echo "Invalid Statement"
          exit
          ;;
     esac
else
     echo "Sum Ting Wong"
     exit
fi
# Going for it
echo "Starting Update"
sudo apt update
echo "Starting " $upgoption
sudo apt $upgoption
echo "Checking for orphaned dependencies..."
sudo apt autoremove
#sudo snap refresh
# Check if need reboot
RESTART_NEEDED=/var/run/reboot-required
if [ -f "$RESTART_NEEDED" ]; then
    echo "Update Complete"
    echo "Restart Required"
    read -p "Would you like to reboot now?(Y/n)" -n 1 -r REEBOOT
    reeboot=${REEBOOT,,}
    if [[ $reeboot =~ ^(y| ) ]] || [[ -z $reeboot ]]; then
         echo
         sudo reboot
    else
         echo
    fi
else
   echo "Update Complete"
   echo "No Restart Required"
fi

Save the file. Example in nano: ctrl+o and ctrl+x

Then switch to a root shell to move the script file, set permissions, and set owner.

sudo -s
mv updateos /usr/bin/
chmod +x /usr/bin/updateos
chmod 0750 /usr/bin/updateos
chown root:root /usr/bin/updateos

Once the above is done, type exit at the command prompt to exit the root shell and back into your user shell. Type the new command updateos and the server will execute the apt & optional snap commands. More experienced users can add commands to run additional updates in the script.

Reference

https://nationalpost.com/news/sum-ting-wong-intern-blamed-for-leaking-fake-names-of-pilots-aboard-plane-that-crashed-in-san-francisco

https://www.tutorialspoint.com/unix/shell_scripting.htm