diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/frigate/run b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/frigate/run index dddde74cd..a3a34e4f6 100755 --- a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/frigate/run +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/frigate/run @@ -9,39 +9,6 @@ set -o errexit -o nounset -o pipefail # Tell S6-Overlay not to restart this service s6-svc -O . -function migrate_db_from_media_to_config() { - # Find config file in yaml or yml, but prefer yaml - local config_file="${CONFIG_FILE:-"/config/config.yml"}" - local config_file_yaml="${config_file//.yml/.yaml}" - if [[ -f "${config_file_yaml}" ]]; then - config_file="${config_file_yaml}" - elif [[ ! -f "${config_file}" ]]; then - # Frigate will create the config file on startup - return 0 - 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 old_db_path="/media/frigate/frigate.db" - local new_db_dir="/config" - if [[ -f "${old_db_path}" ]]; then - if mountpoint --quiet "${new_db_dir}"; then - # /config is a mount point, move the db - echo "[INFO] Migrating database from '${old_db_path}' to '${new_db_dir}' dir..." - # Move all files that starts with frigate.db to the new directory - mv -vf "${old_db_path}"* "${new_db_dir}" - else - echo "[ERROR] Trying to migrate the database path from '${old_db_path}' to '${new_db_dir}' dir, but '${new_db_dir}' is not a mountpoint, please mount the '${new_db_dir}' dir" - return 1 - fi - fi - fi -} - function set_libva_version() { local ffmpeg_path ffmpeg_path=$(python3 /usr/local/ffmpeg/get_ffmpeg_path.py) @@ -50,8 +17,8 @@ function set_libva_version() { } echo "[INFO] Preparing Frigate..." -migrate_db_from_media_to_config set_libva_version + echo "[INFO] Starting Frigate..." cd /opt/frigate || echo "[ERROR] Failed to change working directory to /opt/frigate" diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/dependencies.d/base b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/dependencies.d/prepare similarity index 100% rename from docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/dependencies.d/base rename to docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/dependencies.d/prepare diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/run b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/run index 67148b73c..2c3a7ab6f 100755 --- a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/run +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/run @@ -50,71 +50,6 @@ function set_libva_version() { export LIBAVFORMAT_VERSION_MAJOR } -function migrate_addon_config_dir() { - if ! mountpoint --quiet /homeassistant; then - # Not running as a Home Assistant add-on - return 0 - fi - - local new_config_file="/config/config.yml" - local new_config_file_yaml="${new_config_file//.yml/.yaml}" - if [[ -f "${new_config_file_yaml}" || -f "${new_config_file}" ]]; then - # Already migrated - return 0 - fi - unset new_config_file new_config_file_yaml - - local old_config_file="/homeassistant/frigate.yml" - local old_config_file_yaml="${old_config_file//.yml/.yaml}" - local new_config_file="/config/config.yml" - if [[ -f "${old_config_file_yaml}" ]]; then - old_config_file="${old_config_file_yaml}" - new_config_file="/config/config.yaml" - elif [[ ! -f "${old_config_file}" ]]; then - # Nothing to migrate - return 0 - fi - unset old_config_file_yaml - - local db_path - db_path=$(yq eval '.database.path' "${old_config_file}") - if [[ "${db_path}" == "null" ]]; then - db_path="/config/frigate.db" - fi - if [[ "${db_path}" == /config/* ]]; then - # replace /config/ prefix with /homeassistant/ - local old_db_path="/homeassistant/${db_path:8}" - - if [[ -f "${old_db_path}" ]]; then - local new_db_dir - new_db_dir="$(dirname "${db_path}")" - echo "[INFO] Migrating database from '${old_db_path}' to '${new_db_dir}' dir..." - mkdir -p "${new_db_dir}" - mv -vf "${old_db_path}"* "${new_db_dir}" - fi - fi - - local model_path - model_path=$(yq eval '.model.path' "${old_config_file}") - if [[ "${model_path}" == /config/* ]]; then - # replace /config/ prefix with /homeassistant/ - local old_model_path="/homeassistant/${model_path:8}" - - if [[ -f "${old_model_path}" ]]; then - local new_model_dir - new_model_dir="$(dirname "${model_path}")" - echo "[INFO] Migrating model from '${old_model_path}' to '${model_path}'..." - mkdir -p "${new_model_dir}" - mv -vf "${old_model_path}" "${model_path}" - fi - fi - - echo "[INFO] Migrating config from '${old_config_file}' to '${new_config_file}'..." - mv -vf "${old_config_file}" "${new_config_file}" -} - -migrate_addon_config_dir - set_libva_version if [[ -f "/dev/shm/go2rtc.yaml" ]]; then diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/dependencies.d/base b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/dependencies.d/base new file mode 100644 index 000000000..e69de29bb diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/run b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/run new file mode 100755 index 000000000..3f6e7f248 --- /dev/null +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/run @@ -0,0 +1,106 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Do preparation tasks before starting the main services + +set -o errexit -o nounset -o pipefail + +function migrate_addon_config_dir() { + if ! mountpoint --quiet /homeassistant; then + # Not running as a Home Assistant add-on + return 0 + fi + + local new_config_file="/config/config.yml" + local new_config_file_yaml="${new_config_file//.yml/.yaml}" + if [[ -f "${new_config_file_yaml}" || -f "${new_config_file}" ]]; then + # Already migrated + return 0 + fi + unset new_config_file new_config_file_yaml + + local old_config_file="/homeassistant/frigate.yml" + local old_config_file_yaml="${old_config_file//.yml/.yaml}" + local new_config_file="/config/config.yml" + if [[ -f "${old_config_file_yaml}" ]]; then + old_config_file="${old_config_file_yaml}" + new_config_file="/config/config.yaml" + elif [[ ! -f "${old_config_file}" ]]; then + # Nothing to migrate + return 0 + fi + unset old_config_file_yaml + + local db_path + db_path=$(yq eval '.database.path' "${old_config_file}") + if [[ "${db_path}" == "null" ]]; then + db_path="/config/frigate.db" + fi + if [[ "${db_path}" == /config/* ]]; then + # replace /config/ prefix with /homeassistant/ + local old_db_path="/homeassistant/${db_path:8}" + + if [[ -f "${old_db_path}" ]]; then + local new_db_dir + new_db_dir="$(dirname "${db_path}")" + echo "[INFO] Migrating database from '${old_db_path}' to '${new_db_dir}' dir..." + mkdir -p "${new_db_dir}" + mv -vf "${old_db_path}"* "${new_db_dir}" + fi + fi + + local model_path + model_path=$(yq eval '.model.path' "${old_config_file}") + if [[ "${model_path}" == /config/* ]]; then + # replace /config/ prefix with /homeassistant/ + local old_model_path="/homeassistant/${model_path:8}" + + if [[ -f "${old_model_path}" ]]; then + local new_model_dir + new_model_dir="$(dirname "${model_path}")" + echo "[INFO] Migrating model from '${old_model_path}' to '${model_path}'..." + mkdir -p "${new_model_dir}" + mv -vf "${old_model_path}" "${model_path}" + fi + fi + + echo "[INFO] Migrating config from '${old_config_file}' to '${new_config_file}'..." + mv -vf "${old_config_file}" "${new_config_file}" +} + +function migrate_db_from_media_to_config() { + # Find config file in yaml or yml, but prefer yaml + local config_file="${CONFIG_FILE:-"/config/config.yml"}" + local config_file_yaml="${config_file//.yml/.yaml}" + if [[ -f "${config_file_yaml}" ]]; then + config_file="${config_file_yaml}" + elif [[ ! -f "${config_file}" ]]; then + # Frigate will create the config file on startup + return 0 + 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 old_db_path="/media/frigate/frigate.db" + local new_db_dir="/config" + if [[ -f "${old_db_path}" ]]; then + if mountpoint --quiet "${new_db_dir}"; then + # /config is a mount point, move the db + echo "[INFO] Migrating database from '${old_db_path}' to '${new_db_dir}' dir..." + # Move all files that starts with frigate.db to the new directory + mv -vf "${old_db_path}"* "${new_db_dir}" + else + echo "[ERROR] Trying to migrate the database path from '${old_db_path}' to '${new_db_dir}' dir, but '${new_db_dir}' is not a mountpoint, please mount the '${new_db_dir}' dir" + return 1 + fi + fi + fi +} + +echo "[INFO] Preparing Frigate..." + +migrate_addon_config_dir +migrate_db_from_media_to_config diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/type b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/type new file mode 100644 index 000000000..bdd22a185 --- /dev/null +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/type @@ -0,0 +1 @@ +oneshot diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/up b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/up new file mode 100644 index 000000000..ea17af548 --- /dev/null +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/prepare/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/prepare/run