Skip to content

Commit

Permalink
Robustness update
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner committed Feb 22, 2024
1 parent 8d54670 commit 709d7c9
Showing 1 changed file with 97 additions and 85 deletions.
182 changes: 97 additions & 85 deletions or-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,62 @@
source /docker-entrypoint.sh
docker_setup_env


# Check for presence of old/new directories, indicating a failed previous autoupgrade
echo "----------------------------------------------------------------------"
echo "Checking for left over artifacts from a failed previous autoupgrade..."
echo "----------------------------------------------------------------------"
OLD="${PGDATA}/old"
NEW="${PGDATA}/new"
if [ -d "${OLD}" ]; then
echo "*****************************************"
echo "Left over OLD directory found. Aborting."
echo "*****************************************"
exit 10
fi
if [ -d "${NEW}" ]; then
echo "*****************************************"
echo "Left over NEW directory found. Aborting."
echo "*****************************************"
exit 11
fi
echo "-------------------------------------------------------------------------------"
echo "No artifacts found from a failed previous autoupgrade. Continuing the process."
echo "-------------------------------------------------------------------------------"

echo "DB STATUS: "
pg_ctl -D "$PGDATA" status

if [ -n "$DATABASE_ALREADY_EXISTS" ]; then

echo "-----------------------------------------"
echo "Performing checks on existing database..."
echo "-----------------------------------------"
echo "Performing checks on existing database..."
echo "-----------------------------------------"

# Just make sure postgres isn't still running for some reason
set +e
pg_ctl -D "$PGDATA" -m immedate stop
set -e

# Remove any stale PID file
rm -f "${PGDATA}/postmaster.pid"

# Make sure timescaledb library is set to preload (won't work otherwise)
echo "---------------------------------------------------------------------------------------"
echo "---------------------------------------------------------------------------------------"
echo "Existing postgresql.conf found checking for shared_preload_libraries = 'timescaledb'..."
echo "---------------------------------------------------------------------------------------"
echo "---------------------------------------------------------------------------------------"
RESULT=$(cat "$PGDATA/postgresql.conf" | grep "^shared_preload_libraries = 'timescaledb'" || true)

if [ -n "$RESULT" ]; then
echo "-------------------------------------------"
echo "Timescale DB library already set to preload"
echo "-------------------------------------------"
echo "-------------------------------------------"
echo "Timescale DB library already set to preload"
echo "-------------------------------------------"
else
echo "------------------------------------------------------------------"
echo "Adding shared_preload_libraries = 'timescaledb' to postgresql.conf"
echo "------------------------------------------------------------------"
echo "shared_preload_libraries = 'timescaledb'" >> "$PGDATA/postgresql.conf"
echo "timescaledb.telemetry_level=off" >> "$PGDATA/postgresql.conf"
echo "------------------------------------------------------------------"
echo "Adding shared_preload_libraries = 'timescaledb' to postgresql.conf"
echo "------------------------------------------------------------------"
echo "shared_preload_libraries = 'timescaledb'" >> "$PGDATA/postgresql.conf"
echo "timescaledb.telemetry_level=off" >> "$PGDATA/postgresql.conf"
fi

########################################################################################
Expand Down Expand Up @@ -59,28 +93,6 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
exit 1
fi

# Check for presence of old/new directories, indicating a failed previous autoupgrade
echo "----------------------------------------------------------------------"
echo "Checking for left over artifacts from a failed previous autoupgrade..."
echo "----------------------------------------------------------------------"
OLD="${PGDATA}/old"
NEW="${PGDATA}/new"
if [ -d "${OLD}" ]; then
echo "*****************************************"
echo "Left over OLD directory found. Aborting."
echo "*****************************************"
exit 10
fi
if [ -d "${NEW}" ]; then
echo "*****************************************"
echo "Left over NEW directory found. Aborting."
echo "*****************************************"
exit 11
fi
echo "-------------------------------------------------------------------------------"
echo "No artifacts found from a failed previous autoupgrade. Continuing the process."
echo "-------------------------------------------------------------------------------"

# Don't automatically abort on non-0 exit status, as that messes with these upcoming mv commands
set +e

Expand Down Expand Up @@ -205,51 +217,51 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
fi

# Do timescale upgrade if needed - First look for latest extension version number in extension files
echo "----------------------------------------------------------"
echo "Checking latest available TimescaleDB extension version..."
echo "----------------------------------------------------------"
TS_VERSION_REGEX="\-\-([0-9|\.]+)\."
TS_SCRIPT_NAME=$(find /usr/share/postgresql/$PG_MAJOR/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
TS_VERSION=""
TS_VERSION_FILE="${PGDATA}/OR_TS_VERSION"

if [ "$TS_SCRIPT_NAME" == "" ] || ! [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
echo "------------------------------------------------------"
echo "Cannot determine current TimescaleDB extension version"
echo "------------------------------------------------------"
exit 15
echo "----------------------------------------------------------"
echo "Checking latest available TimescaleDB extension version..."
echo "----------------------------------------------------------"
TS_VERSION_REGEX="\-\-([0-9|\.]+)\."
TS_SCRIPT_NAME=$(find /usr/share/postgresql/$PG_MAJOR/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
TS_VERSION=""
TS_VERSION_FILE="${PGDATA}/OR_TS_VERSION"

if [ "$TS_SCRIPT_NAME" == "" ] || ! [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
echo "------------------------------------------------------"
echo "Cannot determine current TimescaleDB extension version"
echo "------------------------------------------------------"
exit 15
else
TS_VERSION=${BASH_REMATCH[1]}
fi
TS_VERSION=${BASH_REMATCH[1]}
fi

if [ "$TS_VERSION" == "" ]; then
echo "------------------------------------------------------"
echo "Cannot determine current TimescaleDB extension version"
echo "------------------------------------------------------"
exit 15
fi
if [ "$TS_VERSION" == "" ]; then
echo "------------------------------------------------------"
echo "Cannot determine current TimescaleDB extension version"
echo "------------------------------------------------------"
exit 15
fi

DO_TS_UPGRADE=false
echo "Checking whether Timescale needs upgrading..."
if [ ! -f "${TS_VERSION_FILE}" ]; then
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo "No OR_TS_VERSION file so assuming upgrade is required"
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
DO_TS_UPGRADE=true
else
echo "-------------------------------------------------------"
echo "-------------------------------------------------------"
echo "Getting version number from existing OR_TS_VERSION file"
echo "-------------------------------------------------------"
echo "-------------------------------------------------------"
PREVIOUS_TS_VERSION=$(cat "$TS_VERSION_FILE")
if [ "${PREVIOUS_TS_VERSION}" != "${TS_VERSION}" ]; then
echo "------------------------------------------------------------------------------"
echo "------------------------------------------------------------------------------"
echo "TimescaleDB extension upgrade required ${PREVIOUS_TS_VERSION} -> ${TS_VERSION}"
echo "------------------------------------------------------------------------------"
echo "------------------------------------------------------------------------------"
DO_TS_UPGRADE=true
else
echo "----------------------------------------------------"
echo "TimescaleDB extension is up to date at: ${TS_VERSION}"
echo "----------------------------------------------------"
else
echo "----------------------------------------------------"
echo "TimescaleDB extension is up to date at: ${TS_VERSION}"
echo "----------------------------------------------------"
fi
fi

Expand All @@ -267,58 +279,58 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
# Do re-indexing check
DO_REINDEX=false
if [ "$OR_DISABLE_REINDEX" == 'true' ] || [ -z "$OR_REINDEX_COUNTER" ]; then
echo "----------------------------"
echo "----------------------------"
echo "OR_REINDEX check is disabled"
echo "----------------------------"
echo "----------------------------"
else
echo "---------------------------------------"
echo "---------------------------------------"
echo "Checking whether REINDEX is required..."
echo "---------------------------------------"
echo "---------------------------------------"
REINDEX_FILE="$PGDATA/OR_REINDEX_COUNTER.$OR_REINDEX_COUNTER"
if [ -f "$REINDEX_FILE" ]; then
echo "-------------------------------------------------------------------------"
echo "-------------------------------------------------------------------------"
echo "REINDEX file '$REINDEX_FILE' already exists so no re-indexing required"
echo "-------------------------------------------------------------------------"
echo "-------------------------------------------------------------------------"
else
echo "-------------------------------------------------------------------------"
echo "-------------------------------------------------------------------------"
echo "REINDEX file '$REINDEX_FILE' doesn't exist so re-indexing required"
echo "-------------------------------------------------------------------------"
echo "-------------------------------------------------------------------------"
DO_REINDEX=true
fi
fi

if [ "$DO_REINDEX" == "true" ] || [ "$DO_TS_UPGRADE" == "true" ]; then
echo "-------------------------"
echo "-------------------------"
echo "Starting temporary server"
echo "-------------------------"
echo "-------------------------"
docker_temp_server_start "$@"

# Cannot do this on a running DB as the extension is configured to preload
# Cannot do this on a running DB as the extension is configured to preload
if [ "$DO_TS_UPGRADE" == "true" ]; then
echo "------------------------"
echo "------------------------"
echo "Performing TS upgrade..."
echo "------------------------"
echo "------------------------"
docker_process_sql -X -c "ALTER EXTENSION timescaledb UPDATE;"
docker_process_sql -c "CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit; ALTER EXTENSION timescaledb_toolkit UPDATE;"

echo "-------------------"
echo "TS upgrade complete"
echo "-------------------"
echo "-------------------"
echo "$TS_VERSION" > "${PGDATA}/OR_TS_VERSION"
fi

if [ "$DO_REINDEX" == "true" ]; then
echo "----------------------------------"
echo "----------------------------------"
echo "Running timescaledb tune script..."
echo "----------------------------------"
echo "----------------------------------"
/docker-entrypoint-initdb.d/001_timescaledb_tune.sh
echo "---------------------"
echo "---------------------"
echo "Re-indexing the DB..."
echo "---------------------"
echo "---------------------"
docker_process_sql -c "REINDEX database $POSTGRES_DB;"
echo "------------------"
echo "------------------"
echo "REINDEX completed!"
echo "------------------"
echo "------------------"
touch "$REINDEX_FILE"
fi

Expand Down

0 comments on commit 709d7c9

Please sign in to comment.