-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathhassos-supervisor
More file actions
executable file
·126 lines (109 loc) · 5.54 KB
/
hassos-supervisor
File metadata and controls
executable file
·126 lines (109 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
# shellcheck disable=SC1091
# ==============================================================================
# Supervisor on HassOS
# ==============================================================================
set -e
# Load configs
. /etc/os-release
# Init supervisor
SUPERVISOR_DATA=/mnt/data/supervisor
SUPERVISOR_STARTUP_MARKER="/run/supervisor/startup-marker"
SUPERVISOR_CIDFILE="${SUPERVISOR_DATA}/hassio_supervisor.cid"
SUPERVISOR_STARTSCRIPT_VERSION="/mnt/data/.hassos-supervisor-version"
SUPERVISOR_IMAGE="ghcr.io/home-assistant/${SUPERVISOR_ARCH}-hassio-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}}" | sort | uniq || echo "")
# Intended splitting of SUPERVISOR_IMAGE_IDS
# Busybox sh doesn't support arrays
# shellcheck disable=SC2086
docker image rm --force ${SUPERVISOR_IMAGE_IDS} || true
SUPERVISOR_IMAGE_ID=""
fi
mkdir -p "$(dirname ${SUPERVISOR_STARTUP_MARKER})"
touch ${SUPERVISOR_STARTUP_MARKER}
# If Supervisor image is missing, pull it
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 back (e.g., bad release).
SUPERVISOR_VERSION=$(jq -r '.supervisor // "stable"' "${SUPERVISOR_DATA}/updater.json" || echo "stable")
# Get version from stable channel in case we have no local version
# information.
if [ "${SUPERVISOR_VERSION}" = "stable" ]; then
SUPERVISOR_VERSION="$(curl -s --location https://version.home-assistant.io/stable.json | jq -e -r '.supervisor')"
fi
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
docker tag "${SUPERVISOR_IMAGE}:${SUPERVISOR_VERSION}" "${SUPERVISOR_IMAGE}:latest"
else
# Pull failed, updater info might be corrupted or the release might have
# been removed from the container registry, delete the updater info
# to start from scratch on next try.
echo "[ERROR] Supervisor download failed."
rm -f "${SUPERVISOR_DATA}/updater.json"
exit 1
fi
SUPERVISOR_IMAGE_ID=$(docker inspect --format='{{.Id}}' "${SUPERVISOR_IMAGE}" || echo "")
fi
if [ -n "${SUPERVISOR_CONTAINER_ID}" ]; then
# Image changed, remove previous container
if [ "${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
# Start script changed, remove previous container
# shellcheck disable=SC3013
if [ ! -f "${SUPERVISOR_STARTSCRIPT_VERSION}" ] || [ "${SUPERVISOR_STARTSCRIPT_VERSION}" -nt "$0" ] || [ "${SUPERVISOR_STARTSCRIPT_VERSION}" -ot "$0" ]; then
echo "[INFO] Supervisor start script has changed, destroying previous container..."
docker container rm --force hassio_supervisor || true
SUPERVISOR_CONTAINER_ID=""
fi
fi
# If Supervisor container is missing, create it
if [ -z "${SUPERVISOR_CONTAINER_ID}" ]; then
# We need to remove the CID file here, Docker will refuse to start if the
# file is present. Single instance is ensured by other code paths.
[ -f "${SUPERVISOR_CIDFILE}" ] && rm -f "${SUPERVISOR_CIDFILE}"
echo "[INFO] Creating a new Supervisor container..."
# shellcheck disable=SC2086
docker container create \
--name hassio_supervisor \
--privileged --security-opt apparmor="hassio-supervisor" \
--oom-score-adj=-300 \
--cidfile "${SUPERVISOR_CIDFILE}" \
--mount type=bind,src="${SUPERVISOR_CIDFILE}",dst=/run/cid,readonly \
-v /run/docker.sock:/run/docker.sock:rw \
-v /run/containerd/containerd.sock:/run/containerd/containerd.sock:rw \
-v /run/systemd/journal/socket:/run/systemd/journal/socket:rw \
-v /run/systemd-journal-gatewayd.sock:/run/systemd-journal-gatewayd.sock:rw \
-v /run/dbus:/run/dbus:ro \
-v /run/supervisor:/run/os:rw \
-v /run/udev:/run/udev:ro \
-v /etc/machine-id:/etc/machine-id:ro \
-v ${SUPERVISOR_DATA}:/data:rw,slave \
-e SUPERVISOR_SHARE=${SUPERVISOR_DATA} \
-e SUPERVISOR_NAME=hassio_supervisor \
-e SUPERVISOR_MACHINE=${SUPERVISOR_MACHINE} \
"${SUPERVISOR_IMAGE}:latest"
# Store the timestamp of this script. If the script changed, let's
# recreate the container automatically.
touch --reference="$0" "${SUPERVISOR_STARTSCRIPT_VERSION}"
fi
# Run supervisor
mkdir -p ${SUPERVISOR_DATA}
echo "[INFO] Starting the Supervisor..."
docker container start hassio_supervisor
exec docker container wait hassio_supervisor