Update with pre-condition

As indicated in the configure documentation it is possible to use qbee to keep Debian packages updated. The way this works is that the package management coniguration function can be configured to update all packages from defined repositories that have updates available or specific packages that have updates. This is a fully automated process and does not need any user involvement. However, the update operation can interfere with the daily operation of the device. It would be unacceptable if a prodcution machine would update in the middle of a production run.

Tip

Here qbee offers a function which is called "pre-condition". Only if the script being called by that function exits with a zero the update process is initiated. Therefore both time and date or any other pre-condition can be used (input pin HIGH)

This offers a few interesting use cases to check if an update should occur. Some ideas are:

  • check for time and/or date

  • check for physical ports/buttons

  • check if certain applications run

  • check if a file or environment variable exists "update-me"

  • check with an external system if update should be performed (for example with curl)

Below the UI is displayed. The pre-condition script needs to be inserted into the pre-condition form with full path. Make sure it is exectuable. An empty form will always execute the package update.

qbee-package-management

The first thing that needs to be done is to play out the script. This is done with the file distribution function.

  1. Upload any files with the file manager. Please note the pathes.

  2. In the qbee app go to "Configure" and select "System->File Distribution".

    On the device tab select the group or device that is in scope. For more inofrmation please see the configuration documentation.

  3. Start the actual file distribution definition:

    File Source:

    update-window-check.sh
    

    File Destination:

    /usr/local/bin/update-window-check.sh
    

    When does this file gets played out?

    qbee checks if the file exists in the defined destination. If not it will be transfered. Both the file in the file manager and the file on the machine have a hash. If the file manager file is replaced a new file distribution happens. Likewise, if the file on the target machine is changed this will also trigger a new file download and the changes will be overwritten with the original file from the file manager.

  4. In order to do anything with the script the optional "Command to run" function can be used. Here commands can be chained using "&&" and this opens up for a lot of complex options. In this case we only do one simple operation:

    Run as root in the background and suppress output:

    bash /usr/local/bin/update-window-check.sh > /dev/null 2>&1 &
    

    These commands will be triggered any time the file is distributed again

    If the file in the file manager or the file on the target is replaced or changed these commands get run again.

qbee-pre-condition-update-script-check

Above different ideas for pre-condition scripts were given. Here we want to give you two script ideas as a starting point to derive your own pre-condition.

The following script checks if an environmental variale "update" is set to "1". If true the update starts, otherwise it will not run. This can be used to set this variable from a webserver or C++ application on the device. So users can flag when they see a good window for an update period.

#!/usr/bin/env bash

CheckNamekEnv="update"
CheckVarEnv="1"

VarEnv=`printenv $CheckNamekEnv`

if [ -n "$VarEnv" ]; then
        echo $CheckNamekEnv exist
        exit 0

        if [ "$VarEnv" == "$CheckVarEnv" ]; then
            echo $CheckNamekEnv = OK
        else
            echo $CheckNamekEnv = FAIL
            exit 2
        fi
else
        echo $CheckNamekEnv not exist
        exit 1
fi

The next script checks for a time period and only kicks off potential updates between that time period:

#!/usr/bin/env bash

currenttime=$(date +%H:%M)
   if [[ "$currenttime" > "23:00" ]] || [[ "$currenttime" < "04:30" ]]; then
     exit 0
   else
     exit 1
   fi

Why it makes sense to use pre-condition

The idea is that the pre-condition gives an infinite amount of flexibility. It is possible to check for time, timezone, specific date periods, sunset, sunrise or whatever makes sense for the application. Please do not hesitate to send us some of the pre-conditions you use and we can publish them here.

A poor man's update control

This is so obvious that it probably should not be here, but you can disable auto update and only enable it when you want to update. Let's assume your devices have an update rate of 15 minutes. If you enable the "update all" functionality the devices will eventually all pick up this new configuration within the next 15 minutes and start to update. Turning it off again after an hour or two should enable all devices to update. The big drawback with this is obviously that devices that are offline during this operation will not receive the update. So please use with care and rather deploy a pre-condition.