mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-01-11 02:11:01 +01:00
Refactor
This commit is contained in:
@@ -19,9 +19,7 @@ fix_permissions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chmod -R 755 "$CONFIG_HOME"
|
chmod -R 755 "$CONFIG_HOME"
|
||||||
|
|
||||||
RESTART_NEEDED=false
|
RESTART_NEEDED=false
|
||||||
|
|
||||||
cd /config || true
|
cd /config || true
|
||||||
|
|
||||||
get_pgdata_version() {
|
get_pgdata_version() {
|
||||||
@@ -34,9 +32,7 @@ get_pgdata_version() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extract_so_from_deb() {
|
extract_so_from_deb() {
|
||||||
local debfile="$1"
|
local debfile="$1" targetdir="$2" sofile="$3"
|
||||||
local targetdir="$2"
|
|
||||||
local sofile="$3"
|
|
||||||
local tmpdir
|
local tmpdir
|
||||||
tmpdir=$(mktemp -d)
|
tmpdir=$(mktemp -d)
|
||||||
dpkg-deb -x "$debfile" "$tmpdir"
|
dpkg-deb -x "$debfile" "$tmpdir"
|
||||||
@@ -49,34 +45,25 @@ install_vchord_and_vectors_for_old_pg() {
|
|||||||
local vectorchord_tag="${VECTORCHORD_TAG:-0.3.0}"
|
local vectorchord_tag="${VECTORCHORD_TAG:-0.3.0}"
|
||||||
local pgvectors_tag="${PGVECTORS_TAG:-0.3.0}"
|
local pgvectors_tag="${PGVECTORS_TAG:-0.3.0}"
|
||||||
case "$(uname -m)" in
|
case "$(uname -m)" in
|
||||||
x86_64 | amd64 | AMD64 | x86-64)
|
x86_64 | amd64 | AMD64 | x86-64) targetarch=amd64 ;;
|
||||||
targetarch=amd64
|
aarch64 | arm64 | ARM64) targetarch=arm64 ;;
|
||||||
;;
|
*) echo "Unsupported architecture: $(uname -m)" ; exit 1 ;;
|
||||||
aarch64 | arm64 | ARM64)
|
|
||||||
targetarch=arm64
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Unsupported architecture: $(uname -m)"
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
local vchord_url
|
|
||||||
local vectors_url
|
|
||||||
local vchord_deb
|
|
||||||
local vectors_deb
|
|
||||||
local old_pg_lib="/usr/lib/postgresql/$old_pgver/lib"
|
|
||||||
|
|
||||||
|
local old_pg_lib="/usr/lib/postgresql/$old_pgver/lib"
|
||||||
mkdir -p "$old_pg_lib"
|
mkdir -p "$old_pg_lib"
|
||||||
|
|
||||||
vchord_url="https://github.com/tensorchord/VectorChord/releases/download/${vectorchord_tag}/postgresql-${old_pgver}-vchord_${vectorchord_tag}-1_${targetarch}.deb"
|
# VectorChord
|
||||||
vchord_deb="/tmp/vchord-${old_pgver}.deb"
|
local vchord_deb="/tmp/vchord-${old_pgver}.deb"
|
||||||
|
local vchord_url="https://github.com/tensorchord/VectorChord/releases/download/${vectorchord_tag}/postgresql-${old_pgver}-vchord_${vectorchord_tag}-1_${targetarch}.deb"
|
||||||
bashio::log.info "Downloading $vchord_url"
|
bashio::log.info "Downloading $vchord_url"
|
||||||
wget -nv -O "$vchord_deb" "$vchord_url"
|
wget -nv -O "$vchord_deb" "$vchord_url"
|
||||||
extract_so_from_deb "$vchord_deb" "$old_pg_lib" "vchord.so"
|
extract_so_from_deb "$vchord_deb" "$old_pg_lib" "vchord.so"
|
||||||
rm -f "$vchord_deb"
|
rm -f "$vchord_deb"
|
||||||
|
|
||||||
vectors_url="https://github.com/tensorchord/pgvecto.rs/releases/download/v${pgvectors_tag}/vectors-pg${old_pgver}_${pgvectors_tag}_${targetarch}.deb"
|
# pgvecto.rs
|
||||||
vectors_deb="/tmp/pgvectors-${old_pgver}.deb"
|
local vectors_deb="/tmp/pgvectors-${old_pgver}.deb"
|
||||||
|
local vectors_url="https://github.com/tensorchord/pgvecto.rs/releases/download/v${pgvectors_tag}/vectors-pg${old_pgver}_${pgvectors_tag}_${targetarch}.deb"
|
||||||
bashio::log.info "Downloading $vectors_url"
|
bashio::log.info "Downloading $vectors_url"
|
||||||
wget -nv -O "$vectors_deb" "$vectors_url"
|
wget -nv -O "$vectors_deb" "$vectors_url"
|
||||||
extract_so_from_deb "$vectors_deb" "$old_pg_lib" "vectors.so"
|
extract_so_from_deb "$vectors_deb" "$old_pg_lib" "vectors.so"
|
||||||
@@ -86,23 +73,25 @@ install_vchord_and_vectors_for_old_pg() {
|
|||||||
drop_vectors_everywhere() {
|
drop_vectors_everywhere() {
|
||||||
local old_pgver="$1"
|
local old_pgver="$1"
|
||||||
fix_permissions
|
fix_permissions
|
||||||
su - "$DB_USERNAME" -c "$BINARIES_DIR/$old_pgver/bin/pg_ctl \
|
|
||||||
-w -D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf \
|
su - postgres -c "$BINARIES_DIR/$old_pgver/bin/pg_ctl -w -D '$PGDATA' \
|
||||||
-c listen_addresses='' -c port=65432\" start"
|
-o \"-c config_file=/etc/postgresql/postgresql.conf \
|
||||||
for db in $(su - "$DB_USERNAME" -c \
|
-c listen_addresses='' -c port=65432\" start"
|
||||||
"$BINARIES_DIR/$old_pgver/bin/psql -Atc \
|
|
||||||
\"SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn\""); do
|
for db in $(su - postgres -c \
|
||||||
if su - "$DB_USERNAME" -c \
|
"$BINARIES_DIR/$old_pgver/bin/psql -U \"$DB_USERNAME\" -Atc \
|
||||||
"$BINARIES_DIR/$old_pgver/bin/psql -d $db -Atc \
|
\"SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn\""); do
|
||||||
\"SELECT 1 FROM pg_extension WHERE extname='vectors'\"" |
|
if su - postgres -c \
|
||||||
grep -q 1; then
|
"$BINARIES_DIR/$old_pgver/bin/psql -U \"$DB_USERNAME\" -d $db -Atc \
|
||||||
|
\"SELECT 1 FROM pg_extension WHERE extname='vectors'\"" | grep -q 1; then
|
||||||
bashio::log.warning "Dropping extension vectors from DB $db"
|
bashio::log.warning "Dropping extension vectors from DB $db"
|
||||||
su - "$DB_USERNAME" -c \
|
su - postgres -c \
|
||||||
"$BINARIES_DIR/$old_pgver/bin/psql -d $db -c \
|
"$BINARIES_DIR/$old_pgver/bin/psql -U \"$DB_USERNAME\" -d $db -c \
|
||||||
'DROP EXTENSION vectors CASCADE;'"
|
'DROP EXTENSION vectors CASCADE;'"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
su - "$DB_USERNAME" -c "$BINARIES_DIR/$old_pgver/bin/pg_ctl -w -D '$PGDATA' stop"
|
|
||||||
|
su - postgres -c "$BINARIES_DIR/$old_pgver/bin/pg_ctl -w -D '$PGDATA' stop"
|
||||||
}
|
}
|
||||||
|
|
||||||
start_postgres() {
|
start_postgres() {
|
||||||
@@ -110,69 +99,69 @@ start_postgres() {
|
|||||||
if [ "$(bashio::info.arch)" = "armv7" ]; then
|
if [ "$(bashio::info.arch)" = "armv7" ]; then
|
||||||
bashio::log.warning "ARMv7 detected: Starting without vectors.so"
|
bashio::log.warning "ARMv7 detected: Starting without vectors.so"
|
||||||
/usr/local/bin/immich-docker-entrypoint.sh postgres &
|
/usr/local/bin/immich-docker-entrypoint.sh postgres &
|
||||||
true
|
|
||||||
exit 0
|
|
||||||
else
|
else
|
||||||
/usr/local/bin/immich-docker-entrypoint.sh postgres -c config_file=/etc/postgresql/postgresql.conf &
|
/usr/local/bin/immich-docker-entrypoint.sh postgres -c config_file=/etc/postgresql/postgresql.conf &
|
||||||
true
|
|
||||||
fi
|
fi
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
wait_for_postgres() {
|
wait_for_postgres() {
|
||||||
local tries=0
|
local tries=0
|
||||||
while ! pg_isready -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" >/dev/null 2>&1; do
|
while ! pg_isready -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" >/dev/null 2>&1; do
|
||||||
tries=$((tries + 1))
|
tries=$((tries + 1))
|
||||||
if [ "$tries" -ge 60 ]; then
|
[ "$tries" -ge 60 ] && { bashio::log.error "Postgres did not start after 2 minutes, aborting." ; exit 1 ; }
|
||||||
bashio::log.error "Postgres did not start after 2 minutes, aborting."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "PostgreSQL is starting up... ($tries/60)"
|
echo "PostgreSQL is starting up... ($tries/60)"
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
restart_immich_addons_if_flagged() {
|
restart_immich_addons_if_flagged() {
|
||||||
if [ -f "$RESTART_FLAG_FILE" ]; then
|
[ ! -f "$RESTART_FLAG_FILE" ] && return 0
|
||||||
bashio::log.warning "Detected pending Immich add-on restart flag. Restarting all running Immich add-ons..."
|
bashio::log.warning "Detected pending Immich add‑on restart flag. Restarting all running Immich add‑ons…"
|
||||||
|
|
||||||
local addons_json slug found=0
|
local addons_json found=0
|
||||||
|
addons_json=$(curl -fsSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/addons) || {
|
||||||
# Get the add-ons list, fail on HTTP errors, show errors if API call fails
|
bashio::log.error "Supervisor API call failed or unauthorized: $addons_json"
|
||||||
addons_json=$(curl -fsSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/addons) || {
|
|
||||||
bashio::log.error "Supervisor API call failed or unauthorized: $addons_json"
|
|
||||||
rm -f "$RESTART_FLAG_FILE"
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if command -v jq >/dev/null; then
|
|
||||||
# Use correct JSON path for modern Supervisor API
|
|
||||||
for slug in $(echo "$addons_json" | jq -r '.addons[] | select(.state=="started") | .slug'); do
|
|
||||||
if [[ "$slug" == *immich* ]]; then
|
|
||||||
bashio::log.info "Restarting addon $slug"
|
|
||||||
curl -fsSL -X POST -H "Authorization: Bearer $SUPERVISOR_TOKEN" \
|
|
||||||
"http://supervisor/addons/$slug/restart"
|
|
||||||
found=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
else
|
|
||||||
# Fallback: grep/cut for legacy environments, less robust
|
|
||||||
for slug in $(echo "$addons_json" | grep -o '"slug":"[^"]*"' | cut -d: -f2 | tr -d '"'); do
|
|
||||||
if [[ "$slug" == *immich* ]]; then
|
|
||||||
bashio::log.info "Restarting addon $slug"
|
|
||||||
curl -fsSL -X POST -H "Authorization: Bearer $SUPERVISOR_TOKEN" \
|
|
||||||
"http://supervisor/addons/$slug/restart"
|
|
||||||
found=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$found" -eq 0 ]; then
|
|
||||||
bashio::log.info "No Immich-related addon found running."
|
|
||||||
fi
|
|
||||||
rm -f "$RESTART_FLAG_FILE"
|
rm -f "$RESTART_FLAG_FILE"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if command -v jq >/dev/null; then
|
||||||
|
for slug in $(echo "$addons_json" | jq -r '.addons[] | select(.state=="started") | .slug'); do
|
||||||
|
if [[ "$slug" == *immich* ]]; then
|
||||||
|
bashio::log.info "Restarting addon $slug"
|
||||||
|
curl -fsSL -X POST -H "Authorization: Bearer $SUPERVISOR_TOKEN" "http://supervisor/addons/$slug/restart"
|
||||||
|
found=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
for slug in $(echo "$addons_json" | grep -o '"slug":"[^"]*"' | cut -d: -f2 | tr -d '"'); do
|
||||||
|
if [[ "$slug" == *immich* ]]; then
|
||||||
|
bashio::log.info "Restarting addon $slug"
|
||||||
|
curl -fsSL -X POST -H "Authorization: Bearer $SUPERVISOR_TOKEN" "http://supervisor/addons/$slug/restart"
|
||||||
|
found=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
[ "$found" -eq 0 ] && bashio::log.info "No Immich-related addon found running."
|
||||||
|
rm -f "$RESTART_FLAG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --------------------------- DB connection setup ---------------------------
|
||||||
|
DB_PORT=5432
|
||||||
|
DB_HOSTNAME=localhost
|
||||||
|
DB_PASSWORD="$(bashio::config 'POSTGRES_PASSWORD')"
|
||||||
|
DB_PASSWORD="$(jq -rn --arg x "$DB_PASSWORD" '$x|@uri')"
|
||||||
|
|
||||||
|
if bashio::config.has_value "POSTGRES_USER"; then
|
||||||
|
DB_USERNAME="$(bashio::config 'POSTGRES_USER')"
|
||||||
|
else
|
||||||
|
DB_USERNAME="postgres"
|
||||||
|
fi
|
||||||
|
export DB_PORT DB_HOSTNAME DB_USERNAME DB_PASSWORD
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
get_available_extension_version() {
|
get_available_extension_version() {
|
||||||
local extname="$1"
|
local extname="$1"
|
||||||
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/postgres" -v ON_ERROR_STOP=1 -tAc \
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/postgres" -v ON_ERROR_STOP=1 -tAc \
|
||||||
@@ -193,20 +182,15 @@ get_user_databases() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get_installed_extension_version() {
|
get_installed_extension_version() {
|
||||||
local extname="$1"
|
local extname="$1" dbname="$2"
|
||||||
local dbname="$2"
|
|
||||||
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/$dbname" -v ON_ERROR_STOP=1 -tAc \
|
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/$dbname" -v ON_ERROR_STOP=1 -tAc \
|
||||||
"SELECT extversion FROM pg_extension WHERE extname = '$extname';" 2>/dev/null | xargs
|
"SELECT extversion FROM pg_extension WHERE extname = '$extname';" 2>/dev/null | xargs
|
||||||
}
|
}
|
||||||
|
|
||||||
compare_versions() {
|
compare_versions() {
|
||||||
local v1="$1"
|
local v1="$1" v2="$2"
|
||||||
local v2="$2"
|
[ "$v1" = "$v2" ] && return 1
|
||||||
if [ "$v1" = "$v2" ]; then return 1; fi
|
[ "$(printf '%s\n' "$v1" "$v2" | sort -V | head -n1)" = "$v1" ]
|
||||||
if [ "$(printf '%s\n' "$v1" "$v2" | sort -V | head -n1)" = "$v1" ]; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
show_db_extensions() {
|
show_db_extensions() {
|
||||||
@@ -228,33 +212,29 @@ show_db_extensions() {
|
|||||||
|
|
||||||
upgrade_extension_if_needed() {
|
upgrade_extension_if_needed() {
|
||||||
local extname="$1"
|
local extname="$1"
|
||||||
if ! is_extension_available "$extname"; then
|
is_extension_available "$extname" || { bashio::log.info "$extname extension not available." ; return; }
|
||||||
bashio::log.info "$extname extension not available on this Postgres instance."
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
local available_version
|
local available_version
|
||||||
available_version=$(get_available_extension_version "$extname")
|
available_version=$(get_available_extension_version "$extname")
|
||||||
if [ -z "$available_version" ]; then
|
[ -z "$available_version" ] && { bashio::log.info "Could not determine available version for $extname." ; return; }
|
||||||
bashio::log.info "Could not determine available version for $extname."
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
for db in $(get_user_databases); do
|
for db in $(get_user_databases); do
|
||||||
local installed_version
|
local installed_version
|
||||||
installed_version=$(get_installed_extension_version "$extname" "$db")
|
installed_version=$(get_installed_extension_version "$extname" "$db")
|
||||||
if [ -n "$installed_version" ]; then
|
[ -z "$installed_version" ] && continue
|
||||||
if compare_versions "$installed_version" "$available_version"; then
|
|
||||||
bashio::log.info "Upgrading $extname in $db from $installed_version to $available_version"
|
if compare_versions "$installed_version" "$available_version"; then
|
||||||
if psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "ALTER EXTENSION $extname UPDATE;"; then
|
bashio::log.info "Upgrading $extname in $db from $installed_version to $available_version"
|
||||||
bashio::log.info "Reindexing database $db"
|
if psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "ALTER EXTENSION $extname UPDATE;"; then
|
||||||
psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "REINDEX DATABASE $db;"
|
bashio::log.info "Reindexing database $db"
|
||||||
RESTART_NEEDED=true
|
psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "REINDEX DATABASE $db;"
|
||||||
else
|
RESTART_NEEDED=true
|
||||||
bashio::log.error "Failed to upgrade $extname in $db. Aborting startup."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
bashio::log.info "$extname in $db already at latest version ($installed_version)"
|
bashio::log.error "Failed to upgrade $extname in $db. Aborting startup."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
bashio::log.info "$extname in $db already at latest version ($installed_version)"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
@@ -262,137 +242,93 @@ upgrade_extension_if_needed() {
|
|||||||
upgrade_postgres_if_needed() {
|
upgrade_postgres_if_needed() {
|
||||||
CLUSTER_VERSION=$(get_pgdata_version)
|
CLUSTER_VERSION=$(get_pgdata_version)
|
||||||
IMAGE_VERSION="$PG_MAJOR_VERSION"
|
IMAGE_VERSION="$PG_MAJOR_VERSION"
|
||||||
|
[ "$CLUSTER_VERSION" = "$IMAGE_VERSION" ] && { bashio::log.info "PostgreSQL data directory version ($CLUSTER_VERSION) matches image version ($IMAGE_VERSION)." ; return; }
|
||||||
|
|
||||||
if [ "$CLUSTER_VERSION" != "$IMAGE_VERSION" ]; then
|
bashio::log.warning "Postgres data directory version is $CLUSTER_VERSION but image wants $IMAGE_VERSION. Running upgrade..."
|
||||||
bashio::log.warning "Postgres data directory version is $CLUSTER_VERSION but image wants $IMAGE_VERSION. Running upgrade..."
|
export DATA_DIR="$PGDATA" BINARIES_DIR="/usr/lib/postgresql" BACKUP_DIR="/config/backups"
|
||||||
|
export PSQL_VERSION="$IMAGE_VERSION" SUPPORTED_POSTGRES_VERSIONS="$CLUSTER_VERSION $IMAGE_VERSION"
|
||||||
|
|
||||||
export DATA_DIR="$PGDATA"
|
apt-get update &>/dev/null
|
||||||
export BINARIES_DIR="/usr/lib/postgresql"
|
apt-get install -y procps rsync "postgresql-$IMAGE_VERSION" "postgresql-$CLUSTER_VERSION"
|
||||||
export BACKUP_DIR="/config/backups"
|
|
||||||
export PSQL_VERSION="$IMAGE_VERSION"
|
|
||||||
export SUPPORTED_POSTGRES_VERSIONS="$CLUSTER_VERSION $IMAGE_VERSION"
|
|
||||||
|
|
||||||
apt-get update &>/dev/null
|
[ -d "$BINARIES_DIR/$CLUSTER_VERSION/bin" ] || { bashio::log.error "Old postgres binaries missing." ; exit 1 ; }
|
||||||
apt-get install -y procps rsync "postgresql-$IMAGE_VERSION" "postgresql-$CLUSTER_VERSION"
|
[ -d "$BINARIES_DIR/$IMAGE_VERSION/bin" ] || { bashio::log.error "New postgres binaries missing." ; exit 1 ; }
|
||||||
|
|
||||||
if [ ! -d "$BINARIES_DIR/$CLUSTER_VERSION/bin" ]; then
|
install_vchord_and_vectors_for_old_pg "$CLUSTER_VERSION"
|
||||||
bashio::log.error "Old postgres binaries not found at $BINARIES_DIR/$CLUSTER_VERSION/bin"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ ! -d "$BINARIES_DIR/$IMAGE_VERSION/bin" ]; then
|
|
||||||
bashio::log.error "New postgres binaries not found at $BINARIES_DIR/$IMAGE_VERSION/bin"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
install_vchord_and_vectors_for_old_pg "$CLUSTER_VERSION"
|
mkdir -p "$BACKUP_DIR"
|
||||||
|
local backup_target="$BACKUP_DIR/postgresql-$CLUSTER_VERSION"
|
||||||
|
bashio::log.info "Backing up data directory to $backup_target..."
|
||||||
|
rsync -a --delete "$PGDATA/" "$backup_target/" || { bashio::log.error "Backup with rsync failed!" ; exit 1 ; }
|
||||||
|
|
||||||
mkdir -p "$BACKUP_DIR"
|
cp -n --preserve=mode "/var/postgresql-conf-tpl/postgresql.hdd.conf" /etc/postgresql/postgresql.conf
|
||||||
backup_target="$BACKUP_DIR/postgresql-$CLUSTER_VERSION"
|
sed -i "s@##PGDATA@$PGDATA@" /etc/postgresql/postgresql.conf
|
||||||
bashio::log.info "Backing up data directory to $backup_target..."
|
|
||||||
if ! rsync -a --delete "$PGDATA/" "$backup_target/"; then
|
|
||||||
bashio::log.error "Backup with rsync failed!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
cp -n --preserve=mode "/var/postgresql-conf-tpl/postgresql.hdd.conf" /etc/postgresql/postgresql.conf
|
drop_vectors_everywhere "$CLUSTER_VERSION"
|
||||||
sed -i "s@##PGDATA@$PGDATA@" /etc/postgresql/postgresql.conf
|
fix_permissions
|
||||||
|
|
||||||
drop_vectors_everywhere "$CLUSTER_VERSION"
|
bashio::log.info "Starting old Postgres ($CLUSTER_VERSION) to capture encoding/locale settings"
|
||||||
|
su - postgres -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/pg_ctl -w -D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf\" start"
|
||||||
|
|
||||||
fix_permissions
|
LC_COLLATE=$(su - postgres -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -U \"$DB_USERNAME\" -d postgres -Atc 'SHOW LC_COLLATE;'")
|
||||||
|
LC_CTYPE=$(su - postgres -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -U \"$DB_USERNAME\" -d postgres -Atc 'SHOW LC_CTYPE;'")
|
||||||
|
ENCODING=$(su - postgres -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -U \"$DB_USERNAME\" -d postgres -Atc 'SHOW server_encoding;'")
|
||||||
|
bashio::log.info "Detected cluster: LC_COLLATE=$LC_COLLATE, LC_CTYPE=$LC_CTYPE, ENCODING=$ENCODING"
|
||||||
|
|
||||||
bashio::log.info "Starting old Postgres ($CLUSTER_VERSION) to capture encoding/locale settings"
|
bashio::log.info "Stopping old Postgres ($CLUSTER_VERSION)"
|
||||||
su - "$DB_USERNAME" -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/pg_ctl -w -D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf\" start"
|
su - postgres -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/pg_ctl -w -D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf\" stop"
|
||||||
|
|
||||||
LC_COLLATE=$(su - "$DB_USERNAME" -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -d postgres -Atc 'SHOW LC_COLLATE;'")
|
rm -rf "$PGDATA"
|
||||||
LC_CTYPE=$(su - "$DB_USERNAME" -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -d postgres -Atc 'SHOW LC_CTYPE;'")
|
fix_permissions
|
||||||
ENCODING=$(su - "$DB_USERNAME" -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/psql -d postgres -Atc 'SHOW server_encoding;'")
|
|
||||||
|
|
||||||
bashio::log.info "Detected cluster: LC_COLLATE=$LC_COLLATE, LC_CTYPE=$LC_CTYPE, ENCODING=$ENCODING"
|
bashio::log.info "Initializing new data cluster for $IMAGE_VERSION"
|
||||||
|
su - postgres -c "$BINARIES_DIR/$IMAGE_VERSION/bin/initdb --encoding=$ENCODING --lc-collate=$LC_COLLATE --lc-ctype=$LC_CTYPE -D '$PGDATA'"
|
||||||
|
fix_permissions
|
||||||
|
|
||||||
bashio::log.info "Stopping old Postgres ($CLUSTER_VERSION)"
|
bashio::log.info "Running pg_upgrade from $CLUSTER_VERSION → $IMAGE_VERSION"
|
||||||
su - "$DB_USERNAME" -c "$BINARIES_DIR/$CLUSTER_VERSION/bin/pg_ctl -w -D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf\" stop"
|
chmod 700 "$PGDATA" "$backup_target"
|
||||||
|
su - postgres -c "$BINARIES_DIR/$IMAGE_VERSION/bin/pg_upgrade \
|
||||||
|
-b '$BINARIES_DIR/$CLUSTER_VERSION/bin' -B '$BINARIES_DIR/$IMAGE_VERSION/bin' \
|
||||||
|
-d '$backup_target' -D '$PGDATA' \
|
||||||
|
-o \"-c config_file=/etc/postgresql/postgresql.conf\" \
|
||||||
|
-O \"-c config_file=/etc/postgresql/postgresql.conf\""
|
||||||
|
|
||||||
rm -rf "$PGDATA"
|
[ -f "$backup_target/postgresql.conf" ] && cp "$backup_target/postgresql.conf" "$PGDATA"
|
||||||
|
[ -f "$backup_target/pg_hba.conf" ] && cp -f "$backup_target/pg_hba.conf" "$PGDATA"
|
||||||
|
|
||||||
fix_permissions
|
bashio::log.info "Upgrade completed successfully."
|
||||||
|
RESTART_NEEDED=true
|
||||||
bashio::log.info "Initializing new data cluster for $IMAGE_VERSION"
|
|
||||||
su - "$DB_USERNAME" -c "$BINARIES_DIR/$IMAGE_VERSION/bin/initdb --encoding=$ENCODING --lc-collate=$LC_COLLATE --lc-ctype=$LC_CTYPE -D '$PGDATA'"
|
|
||||||
|
|
||||||
fix_permissions
|
|
||||||
|
|
||||||
bashio::log.info "Running pg_upgrade from $CLUSTER_VERSION → $IMAGE_VERSION"
|
|
||||||
chmod 700 "$PGDATA"
|
|
||||||
chmod 700 "$backup_target"
|
|
||||||
if ! su - "$DB_USERNAME" -c "$BINARIES_DIR/$IMAGE_VERSION/bin/pg_upgrade \
|
|
||||||
-b '$BINARIES_DIR/$CLUSTER_VERSION/bin' \
|
|
||||||
-B '$BINARIES_DIR/$IMAGE_VERSION/bin' \
|
|
||||||
-d '$backup_target' \
|
|
||||||
-D '$PGDATA' -o \"-c config_file=/etc/postgresql/postgresql.conf\" -O \"-c config_file=/etc/postgresql/postgresql.conf\""; then
|
|
||||||
bashio::log.error "pg_upgrade failed!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$backup_target/postgresql.conf" ]; then
|
|
||||||
cp "$backup_target/postgresql.conf" "$PGDATA"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$backup_target/pg_hba.conf" ]; then
|
|
||||||
cp -f "$backup_target/pg_hba.conf" "$PGDATA"
|
|
||||||
fi
|
|
||||||
|
|
||||||
bashio::log.info "Upgrade completed successfully."
|
|
||||||
RESTART_NEEDED=true
|
|
||||||
|
|
||||||
else
|
|
||||||
bashio::log.info "PostgreSQL data directory version ($CLUSTER_VERSION) matches image version ($IMAGE_VERSION)."
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check_for_reindex() {
|
check_for_reindex() {
|
||||||
local log_tail
|
local log_tail
|
||||||
log_tail=$(timeout 15 cat /proc/1/fd/1 | tail -n 5)
|
log_tail=$(timeout 15 cat /proc/1/fd/1 | tail -n 5)
|
||||||
|
echo "$log_tail" | grep -q "please use REINDEX to rebuild the index" || return 0
|
||||||
|
|
||||||
if echo "$log_tail" | grep -q "please use REINDEX to rebuild the index"; then
|
bashio::log.warning "REINDEX needed, starting now"
|
||||||
bashio::log.warning "REINDEX needed, starting now"
|
for db in $(get_user_databases); do
|
||||||
for db in $(get_user_databases); do
|
psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "REINDEX DATABASE $db;"
|
||||||
psql -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" -d "$db" -v ON_ERROR_STOP=1 -c "REINDEX DATABASE $db;"
|
done
|
||||||
done
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
bashio::log.info "Checking for required PostgreSQL cluster upgrade before server start..."
|
bashio::log.info "Checking for required PostgreSQL cluster upgrade before server start…"
|
||||||
if [ -f /config/database/PG_VERSION ]; then
|
[ -f /config/database/PG_VERSION ] && upgrade_postgres_if_needed
|
||||||
upgrade_postgres_if_needed
|
|
||||||
fi
|
|
||||||
|
|
||||||
start_postgres
|
start_postgres
|
||||||
|
bashio::log.info "Waiting for PostgreSQL to start…"
|
||||||
bashio::log.info "Waiting for PostgreSQL to start..."
|
|
||||||
|
|
||||||
DB_PORT=5432
|
|
||||||
DB_HOSTNAME=localhost
|
|
||||||
DB_PASSWORD="$(bashio::config 'POSTGRES_PASSWORD')"
|
|
||||||
DB_PASSWORD="$(jq -rn --arg x "$DB_PASSWORD" '$x|@uri')"
|
|
||||||
DB_USERNAME=postgres
|
|
||||||
if bashio::config.has_value "POSTGRES_USER"; then
|
|
||||||
DB_USERNAME="$(bashio::config "POSTGRES_USER")"
|
|
||||||
fi
|
|
||||||
export DB_PORT DB_HOSTNAME DB_USERNAME DB_PASSWORD
|
|
||||||
|
|
||||||
wait_for_postgres
|
wait_for_postgres
|
||||||
restart_immich_addons_if_flagged
|
restart_immich_addons_if_flagged
|
||||||
|
|
||||||
su - "$DB_USERNAME" -c "psql -d postgres -c 'DROP EXTENSION IF EXISTS vectors CASCADE;'"
|
# Drop legacy vectors extension (connect as $DB_USERNAME)
|
||||||
|
su - postgres -c "psql -U \"$DB_USERNAME\" -d postgres -c 'DROP EXTENSION IF EXISTS vectors CASCADE;'"
|
||||||
|
|
||||||
upgrade_extension_if_needed "vectors"
|
upgrade_extension_if_needed "vectors"
|
||||||
upgrade_extension_if_needed "vchord"
|
upgrade_extension_if_needed "vchord"
|
||||||
show_db_extensions
|
show_db_extensions
|
||||||
|
|
||||||
if [ "$RESTART_NEEDED" = true ]; then
|
if [ "$RESTART_NEEDED" = true ]; then
|
||||||
bashio::log.warning "A critical update (Postgres or extension) occurred. Will trigger Immich add-on restart after DB comes back up."
|
bashio::log.warning "A critical update occurred. Restarting Immich add‑on(s) after DB restart."
|
||||||
touch "$RESTART_FLAG_FILE"
|
touch "$RESTART_FLAG_FILE"
|
||||||
bashio::addon.restart
|
bashio::addon.restart
|
||||||
exit 0
|
exit 0
|
||||||
@@ -404,8 +340,7 @@ main() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
check_for_reindex &
|
check_for_reindex &
|
||||||
bashio::log.info "All initialization/version check steps completed successfully!"
|
bashio::log.info "All initialization / version‑check steps completed successfully!"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main
|
main
|
||||||
|
|||||||
Reference in New Issue
Block a user