Skip to content

Instantly share code, notes, and snippets.

@mehalter
Created October 5, 2021 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehalter/9d780e8a7275bfd069bfeec23cf53ad2 to your computer and use it in GitHub Desktop.
Save mehalter/9d780e8a7275bfd069bfeec23cf53ad2 to your computer and use it in GitHub Desktop.
trigger_update_sv.sh
#!/bin/bash
### !!! Script neccessitates the tool jq https://stedolan.github.io/jq/
### !!! Script neccessitates the tool GNU Parallel https://www.gnu.org/software/parallel/
## INPUTS
## Auth token taken from https://dashboard.balena-cloud.com/preferences/access-tokens
AUTH_TOKEN=
## Go to fleet view in UI: https://dashboard.balena-cloud.com/orgs/<fleetID>/fleets
FLEET_ID=<fleetID>
## Target Supervisor Version that your device should run on. Latest: 'v12.10.15'
## Downgrade is not possible, be aware when updating to latest supervisor version
# SUPERVISOR_VERSION='v12.10.15'
SUPERVISOR_VERSION=
# Set the maximum number of parallel jobs
MAX_JOBS=10
## Get devicetype of fleet
DEVICE_TYPE=$(curl -s -X GET \
"https://api.balena-cloud.com/v6/device?\$select=is_of__device_type&\$filter=(belongs_to__application%20eq%20${FLEET_ID})" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" | jq '.d[0].is_of__device_type.__id')
echo "DeviceType detected ${DEVICE_TYPE}"
## Get latest supvervisor release for given supervisor version string
SUPERVISOR_RELEASE=$(curl -s -X GET \
"https://api.balena-cloud.com/v6/supervisor_release?\$filter=(is_for__device_type%20eq%20${DEVICE_TYPE})%20and%20(supervisor_version%20eq%20'${SUPERVISOR_VERSION}')" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" | jq '.d[0].id')
echo "Supervisor ${SUPERVISOR_VERSION} has release ${SUPERVISOR_RELEASE}"
## Get all devices that are not running the desired supervisor version (includes already update triggered devices - for script this doesn't matter)
_DEVICE_IDS=$(curl -s -X GET \
"https://api.balena-cloud.com/v6/device?\$select=id,belongs_to__application,supervisor_version,should_be_managed_by__supervisor_release&\$filter=(belongs_to__application%20eq%20${FLEET_ID})%20and%20(supervisor_version%20ne%20'${SUPERVISOR_VERSION}')" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" | jq '.d[].id')
## ATTENTION: This loop RUNS over all devices. If any devices should not be selected don't run this script.
## Uncomment
# DEVICE_IDS=$_DEVICE_IDS
# Define updating the supervisor as a function that can be parallelized
UPDATE_SUPERVISOR() {
DEVICE_ID=$1
curl -X PATCH \
"https://api.balena-cloud.com/v6/device(${DEVICE_ID})" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
--data "{
\"should_be_managed_by__supervisor_release\": ${SUPERVISOR_RELEASE}
}"
}
export -f UPDATE_SUPERVISOR
# run the UPDATE_SUPERVISOR API call in parallel on the list of DEVICE_IDS
echo ${DEVICE_IDS[@]} | parallel -j$MAX_JOBS UPDATE_SUPERVISOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment