139 lines
3.8 KiB
Bash
139 lines
3.8 KiB
Bash
#!/bin/bash
|
|
set -o pipefail
|
|
|
|
USE_LOCAL=no
|
|
|
|
# Help function
|
|
function update-resin-supervisor-help {
|
|
cat << EOF
|
|
Wrapper to run supervisor agent updates on resin distributions.
|
|
$0 <OPTION>
|
|
|
|
Options:
|
|
-h, --help
|
|
Display this help and exit.
|
|
|
|
-i <SUPERVISOR IMAGE>, --supervisor-image <SUPERVISOR IMAGE>
|
|
Set supervisor image to update to.
|
|
|
|
-t <SUPERVISOR TAG>, --supervisor-tag <SUPERVISOR TAG>
|
|
Set supervisor tag to update to.
|
|
EOF
|
|
}
|
|
|
|
# Parse arguments
|
|
while [[ $# > 0 ]]; do
|
|
arg="$1"
|
|
|
|
case $arg in
|
|
-h|--help)
|
|
update-resin-supervisor-help
|
|
exit 0
|
|
;;
|
|
-i|--supervisor-image)
|
|
if [ -z "$2" ]; then
|
|
log ERROR "\"$1\" argument needs a value."
|
|
fi
|
|
UPDATER_SUPERVISOR_IMAGE=$2
|
|
shift
|
|
;;
|
|
-t|--supervisor-tag)
|
|
if [ -z "$2" ]; then
|
|
log ERROR "\"$1\" argument needs a value."
|
|
fi
|
|
UPDATER_SUPERVISOR_TAG=$2
|
|
shift
|
|
;;
|
|
-l|--local-update)
|
|
USE_LOCAL=yes
|
|
;;
|
|
*)
|
|
log ERROR "Unrecognized option $1."
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# Don't souce before parsing args - resin-vars parses args too
|
|
source /usr/sbin/resin-vars
|
|
source /etc/resin-supervisor/supervisor.conf
|
|
|
|
# A temporary file used until next reboot
|
|
UPDATECONF=/tmp/update-supervisor.conf
|
|
|
|
function error_handler {
|
|
# If docker pull fails, start the old supervisor again and exit
|
|
rm -rf $UPDATECONF
|
|
systemctl start resin-supervisor
|
|
exit 1
|
|
}
|
|
|
|
trap 'error_handler $LINENO' ERR
|
|
|
|
# use HassIO API for update
|
|
if [ "$USE_LOCAL" == "no" ] && [ -f $SUPERVISOR_CONFIG ]; then
|
|
echo "Update supervisor with HassIO API"
|
|
hassio_api=$($(jq --raw-output ".api_endpoint // empty" $SUPERVISOR_CONFIG))
|
|
|
|
if [ ! -z $UPDATER_SUPERVISOR_TAG ]; then
|
|
payload="{ \"version\": \"$UPDATER_SUPERVISOR_TAG\" }"
|
|
else
|
|
payload="{}"
|
|
fi
|
|
|
|
curl -s -X GET -d "$payload" "http://$hassio_api/supervisor/update"
|
|
fi
|
|
|
|
if [ -f $SUPERVISOR_CONFIG ]; then
|
|
tag=$(jq --raw-output ".hassio_last // empty" $SUPERVISOR_CONFIG)
|
|
image_name=$SUPERVISOR_IMAGE
|
|
fi
|
|
|
|
# override from params
|
|
if [ ! -z $UPDATER_SUPERVISOR_TAG ]; then
|
|
tag=$UPDATER_SUPERVISOR_TAG
|
|
fi
|
|
if [ ! -z $UPDATER_SUPERVISOR_IMAGE ]; then
|
|
image_name=$UPDATER_SUPERVISOR_IMAGE
|
|
fi
|
|
|
|
# Check that we didn't somehow get an empty tag version.
|
|
if [ -z $tag ] || [ -z $image_name ]; then
|
|
error_handler $LINENO "no tag received"
|
|
fi
|
|
|
|
# Get image id of tag. This will be non-empty only in case it's already downloaded.
|
|
echo "Getting image id..."
|
|
imageid=$(docker inspect -f '{{.Id}}' "$image_name:$tag") || imageid=""
|
|
|
|
if [ -n "$imageid" ]; then
|
|
echo "Supervisor $image_name:$tag already downloaded."
|
|
exit 0
|
|
fi
|
|
|
|
# Try to stop old supervisor to prevent it deleting the intermediate images while downloading the new one
|
|
echo "Stop supervisor..."
|
|
systemctl stop resin-supervisor
|
|
|
|
# Pull target version.
|
|
echo "Pulling supervisor $image_name:$tag..."
|
|
docker pull "$image_name:$tag"
|
|
|
|
docker rm --force resin_supervisor || true
|
|
|
|
# Store the tagged image string so resin-supervisor.service can pick it up
|
|
echo "SUPERVISOR_IMAGE=$image_name:$tag" > $UPDATECONF
|
|
|
|
# Run supervisor with the device-type-specific options.
|
|
# We give a specific name to the container to guarantee only one running.
|
|
echo "Start supervisor..."
|
|
systemctl start resin-supervisor
|
|
|
|
# Mark supervisor as working. This version will run when the device reboots.
|
|
echo "Mark pulled tag as latest..."
|
|
docker tag "$image_name:$tag" "$image_name:latest"
|
|
sed --in-place "s|SUPERVISOR_IMAGE=.*|SUPERVISOR_IMAGE=$image_name|" /etc/resin-supervisor/supervisor.conf
|
|
sed --in-place "s|SUPERVISOR_TAG=.*|SUPERVISOR_TAG=$tag|" /etc/resin-supervisor/supervisor.conf
|
|
|
|
# cleanup old image
|
|
echo "!!! Please cleanup old supervisor manual !!!"
|