In case a container image is corrupted `docker inspect` might fail:
# docker inspect --format='{{.Id}}' "${SUPERVISOR_IMAGE}"
Error response from daemon: readlink /mnt/data/docker/overlay2: invalid argument
In that same state the `docker images` command still shows the images.
Since `docker inspect` returns an error SUPERVISOR_IMAGE_ID will be empty
and a simple `docker pull` will be attempted. That does not suffice to
recover from a corrupted container image.
Use `docker images` to get the image ids and make sure to delete all
image ids found by that command.
Also don't use RuntimeDirectory since it deletes the runtime directory
between the service start attempts which defeats the purpose.
89 lines
3.8 KiB
Bash
Executable File
89 lines
3.8 KiB
Bash
Executable File
#!/bin/sh
|
|
# shellcheck disable=SC1091
|
|
# ==============================================================================
|
|
# Supervisor on HassOS
|
|
# ==============================================================================
|
|
set -e
|
|
|
|
# Load configs
|
|
. /etc/os-release
|
|
|
|
# Init supervisor
|
|
SUPERVISOR_STARTUP_MARKER="/run/supervisor/startup-marker"
|
|
SUPERVISOR_IMAGE="homeassistant/${SUPERVISOR_ARCH}-hassio-supervisor"
|
|
SUPERVISOR_DATA=/mnt/data/supervisor
|
|
SUPERVISOR_IMAGE_ID=$(docker images --no-trunc --filter "reference=${SUPERVISOR_IMAGE}:latest" --format "{{.ID}}" || echo "")
|
|
SUPERVISOR_CONTAINER_ID=$(docker inspect --format='{{.Image}}' hassio_supervisor || echo "")
|
|
|
|
# Check if previous run left the startup-marker in place. If so, we assume the
|
|
# Container image or container is somehow corrupted.
|
|
# Delete the container, delete the image, pull a fresh one
|
|
if [ -f "${SUPERVISOR_STARTUP_MARKER}" ]; then
|
|
echo "[WARNING] Supervisor container did not remove the startup marker file. Assuming container image or container corruption."
|
|
docker container rm --force hassio_supervisor || true
|
|
SUPERVISOR_CONTAINER_ID=""
|
|
# Make sure we delete all supervisor images
|
|
SUPERVISOR_IMAGE_IDS=$(docker images --no-trunc --filter "reference=${SUPERVISOR_IMAGE}" --format "{{.ID}}" | uniq || echo "")
|
|
docker image rm --force "${SUPERVISOR_IMAGE_IDS}" || true
|
|
SUPERVISOR_IMAGE_ID=""
|
|
fi
|
|
|
|
# If Supervisor image is missing, pull it
|
|
mkdir -p "$(dirname ${SUPERVISOR_STARTUP_MARKER})"
|
|
touch ${SUPERVISOR_STARTUP_MARKER}
|
|
if [ -z "${SUPERVISOR_IMAGE_ID}" ]; then
|
|
# Get the latest from update information
|
|
# Using updater information instead of config. If the config version is
|
|
# broken, this creates a way (e.g., bad release).
|
|
SUPERVISOR_VERSION_UPDATER=$(jq -r '.version // empty' "${SUPERVISOR_DATA}/config.json" || echo "")
|
|
SUPERVISOR_VERSION="${SUPERVISOR_VERSION_UPDATER:-latest}"
|
|
|
|
echo "[WARNING] Supervisor image missing, downloading a fresh one: ${SUPERVISOR_VERSION}"
|
|
|
|
# Pull in the Supervisor
|
|
if docker pull "${SUPERVISOR_IMAGE}:${SUPERVISOR_VERSION}"; then
|
|
# Tag as latest if versioned
|
|
if [ "${SUPERVISOR_VERSION}" != "latest" ]; then
|
|
docker tag "${SUPERVISOR_IMAGE}:${SUPERVISOR_VERSION}" "${SUPERVISOR_IMAGE}:latest"
|
|
fi
|
|
else
|
|
# Pull failed, updater info might be corrupted, re-trying with latest
|
|
echo "[WARNING] Supervisor downloading failed trying: latest"
|
|
docker pull "${SUPERVISOR_IMAGE}:latest"
|
|
fi
|
|
|
|
SUPERVISOR_IMAGE_ID=$(docker inspect --format='{{.Id}}' "${SUPERVISOR_IMAGE}" || echo "")
|
|
fi
|
|
|
|
# Image changed, remove previous container
|
|
if [ -n "${SUPERVISOR_CONTAINER_ID}" ] && [ "${SUPERVISOR_IMAGE_ID}" != "${SUPERVISOR_CONTAINER_ID}" ]; then
|
|
echo "[INFO] Supervisor image has been updated, destroying previous container..."
|
|
docker container rm --force hassio_supervisor || true
|
|
SUPERVISOR_CONTAINER_ID=""
|
|
fi
|
|
|
|
# If Supervisor container is missing, create it
|
|
if [ -z "${SUPERVISOR_CONTAINER_ID}" ]; then
|
|
echo "[INFO] Creating a new Supervisor container..."
|
|
# shellcheck disable=SC2086
|
|
docker container create \
|
|
--name hassio_supervisor \
|
|
--privileged --security-opt apparmor="hassio-supervisor" \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
-v /var/run/dbus:/var/run/dbus \
|
|
-v /run/supervisor:/run/os:rw \
|
|
-v /etc/machine-id:/etc/machine-id:ro \
|
|
-v ${SUPERVISOR_DATA}:/data:rw \
|
|
-v /mnt/overlay:/os/overlay:rw \
|
|
-v /mnt/boot:/os/boot:rw \
|
|
-e SUPERVISOR_SHARE=${SUPERVISOR_DATA} \
|
|
-e SUPERVISOR_NAME=hassio_supervisor \
|
|
-e SUPERVISOR_MACHINE=${SUPERVISOR_MACHINE} \
|
|
"${SUPERVISOR_IMAGE}:latest"
|
|
fi
|
|
|
|
# Run supervisor
|
|
mkdir -p ${SUPERVISOR_DATA}
|
|
echo "[INFO] Starting the Supervisor..."
|
|
exec docker container start --attach hassio_supervisor
|