Put logic inside of Frigate's run

This commit is contained in:
Felipe Santos 2023-01-24 17:51:55 -03:00
parent a61740d0ce
commit 29123dacb5
10 changed files with 37 additions and 95 deletions

View File

@ -198,7 +198,7 @@ EXPOSE 8555/tcp 8555/udp
ENV S6_LOGGING_SCRIPT="T 1 n0 s10000000 T"
ENTRYPOINT ["/init"]
CMD ["/etc/s6-overlay/validate_services.sh"]
CMD []
# Frigate deps with Node.js and NPM for devcontainer
FROM deps AS devcontainer

View File

@ -1,36 +0,0 @@
#!/command/with-contenv bash
# shellcheck shell=bash
set -o errexit -o nounset -o pipefail
config_file="${CONFIG_FILE:-"/config/config.yml"}"
if [[ ! -f "${config_file}" ]]; then
config_file="${config_file//.yml/.yaml}"
if [[ ! -f "${config_file}" ]]; then
echo "[ERROR] Frigate config file not found" >&2
exit 1
fi
fi
readonly config_file
# Use yq to check if database.path is set
user_db_path=$(yq eval '.database.path' "${config_file}")
readonly user_db_path
if [[ "${user_db_path}" == "null" ]]; then
readonly previous_db_path="/media/frigate/frigate.db"
readonly new_db_dir="/config"
if [[ -f "${previous_db_path}" ]]; then
if mountpoint --quiet "${new_db_dir}"; then
# /config is a mount point, move the db
echo "[INFO] Moving db from '${previous_db_path}' to the '${new_db_dir}' dir..." >&2
# Move all files that starts with frigate.db to the new directory
mv -vf "${previous_db_path}"* "${new_db_dir}"
else
echo "[ERROR] Trying to migrate the db path from '${previous_db_path}' to the '${new_db_dir}' dir, but '${new_db_dir}' is not a mountpoint, please mount the '${new_db_dir}' dir" >&2
exit 1
fi
fi
fi

View File

@ -1 +0,0 @@
/etc/s6-overlay/s6-rc.d/frigate-prepare/run

View File

@ -7,6 +7,42 @@ set -o errexit -o nounset -o pipefail
# Tell S6-Overlay not to restart this service
s6-svc -O .
function migrate_db_path() {
# Find config file in yaml or yml, but prefer yaml
local config_file="${CONFIG_FILE:-"/config/config.yml"}"
local config_file_yaml="${config_file//.yaml/.yml}"
if [[ -f "${config_file_yaml}" ]]; then
config_file="${config_file_yaml}"
elif [[ ! -f "${config_file}" ]]; then
echo "[ERROR] Frigate config file not found" >&2
return 1
fi
unset config_file_yaml
# Use yq to check if database.path is set
local user_db_path
user_db_path=$(yq eval '.database.path' "${config_file}")
if [[ "${user_db_path}" == "null" ]]; then
local previous_db_path="/media/frigate/frigate.db"
local new_db_dir="/config"
if [[ -f "${previous_db_path}" ]]; then
if mountpoint --quiet "${new_db_dir}"; then
# /config is a mount point, move the db
echo "[INFO] Moving db from '${previous_db_path}' to the '${new_db_dir}' dir..." >&2
# Move all files that starts with frigate.db to the new directory
mv -vf "${previous_db_path}"* "${new_db_dir}"
else
echo "[ERROR] Trying to migrate the db path from '${previous_db_path}' to the '${new_db_dir}' dir, but '${new_db_dir}' is not a mountpoint, please mount the '${new_db_dir}' dir" >&2
return 1
fi
fi
fi
}
echo "[INFO] Preparing Frigate..." >&2
migrate_db_path
echo "[INFO] Starting Frigate..." >&2
cd /opt/frigate || echo "[ERROR] Failed to change working directory to /opt/frigate" >&2

View File

@ -1,26 +0,0 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Take down the S6 supervision tree when the service exits
set -o errexit -o nounset -o pipefail
declare exit_code_container
exit_code_container=$(cat /run/s6-linux-init-container-results/exitcode)
readonly exit_code_container
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
readonly service="log-prepare"
echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2
if [[ "${exit_code_service}" -eq 256 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
elif [[ "${exit_code_service}" -ne 0 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode
fi
fi
exec /run/s6/basedir/bin/halt

View File

@ -1,25 +0,0 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Checks if all oneshot services executed succesfully
# https://github.com/just-containers/s6-overlay/issues/513#issuecomment-1401399995
set -o errexit -o nounset -o pipefail
readonly wanted_services=(
log-prepare
frigate-prepare
# go2rtc-prepare
)
services=$(s6-rc -a list) # lists active/executed services, one per line
readonly services
for wanted_service in "${wanted_services[@]}"; do
if ! echo "${services}" | grep -qFx "${wanted_service}" ; then
echo "[ERROR] Service '${wanted_service}' failed to execute" >&2
echo 1 > /run/s6-linux-init-container-results/exitcode # to say the container failed
exec /run/s6/basedir/bin/halt
fi
done
exec s6-pause # or exec into your real CMD if you have one

View File

@ -1,5 +0,0 @@
#!/command/with-contenv bash
# shellcheck shell=bash
exec 2>&1
exec python3 -u -m frigate "${@}"