From b9ff20cf673d9ef4e42b34ffc4377f08c5656afc Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Wed, 19 Jun 2024 09:06:50 +0530 Subject: [PATCH 01/10] chore: Add script to upgrade Postgres 13 data to 14 --- Dockerfile | 8 ++ deploy/docker/base.dockerfile | 4 +- deploy/docker/fs/opt/appsmith/entrypoint.sh | 15 ++-- deploy/docker/fs/opt/appsmith/pg-upgrade.sh | 89 +++++++++++++++++++++ deploy/docker/tests/pg-upgrade/run.sh | 78 ++++++++++++++++++ 5 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 deploy/docker/fs/opt/appsmith/pg-upgrade.sh create mode 100755 deploy/docker/tests/pg-upgrade/run.sh diff --git a/Dockerfile b/Dockerfile index 8b72f7061814..36a6d8d750a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,6 +40,14 @@ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd && chmod ugo+w /etc /appsmith-stacks \ && chmod -R ugo+w /var/run /.mongodb /etc/ssl /usr/local/share +# Temporary, until we can change Postgres version in `base.dockerfile`. +RUN <&2 + exit +fi + +if [[ -f "$pg_data_dir/postmaster.pid" ]]; then + tlog "Previous Postgres was not shutdown cleanly. Please start and stop Postgres $old_version properly with 'supervisorctl' only." >&2 + exit 1 +fi + +declare -a to_uninstall +to_uninstall=() + +# 13 to 14 +if [[ "$old_version" == 13 ]]; then + if [[ ! -e "$postgres_path/$old_version" ]]; then + apt-get update + apt-get install --yes "postgresql-$old_version" + to_uninstall+=("postgresql-$old_version") + fi + + new_version="$((old_version + 1))" + new_data_dir="$pg_data_dir-$new_version" + + # `pg_upgrade` writes log to current folder. So change to a temp folder first. + rm -rf "$TMP/pg_upgrade" "$new_data_dir" + mkdir -p "$TMP/pg_upgrade" "$new_data_dir" + chown -R postgres "$TMP/pg_upgrade" "$new_data_dir" + cd "$TMP/pg_upgrade" + + # Required by the temporary Postgres server started by `pg_upgrade`. + chown postgres /etc/ssl/private/ssl-cert-snakeoil.key + chmod 0600 /etc/ssl/private/ssl-cert-snakeoil.key + + su postgres --command " + set -o errexit + set -o xtrace + '$postgres_path/$new_version/bin/initdb' --pgdata='$new_data_dir' + '$postgres_path/$new_version/bin/pg_upgrade' \ + --old-datadir='$pg_data_dir' \ + --new-datadir='$new_data_dir' \ + --old-bindir='$postgres_path/$old_version/bin' \ + --new-bindir='$postgres_path/$new_version/bin' + " + + date -u '+%FT%T.%3NZ' > "$pg_data_dir/deprecated-on.txt" + mv -v "$pg_data_dir" "$pg_data_dir-$old_version" + mv -v "$new_data_dir" "$pg_data_dir" + + # Dangerous generated script that deletes the now updated data folder. + rm -fv "$TMP/pg_upgrade/delete_old_cluster.sh" +fi + +if [[ -n "${#to_uninstall[@]}" ]]; then + apt-get purge "${to_uninstall[@]}" + apt-get clean +fi + +echo "== Fin ==" diff --git a/deploy/docker/tests/pg-upgrade/run.sh b/deploy/docker/tests/pg-upgrade/run.sh new file mode 100755 index 000000000000..33fda39ce7ca --- /dev/null +++ b/deploy/docker/tests/pg-upgrade/run.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +# A script to test Postgres upgrades. WIP. + +set -o errexit +set -o nounset +set -o xtrace + +from_tag=release +to_tag=local + +container_name=appsmith-pg-upgrade-test +port=20080 + +docker rm -f "$container_name" +docker volume rm --force "$container_name" + +# TODO: Add `--pull always` for images that have a manifest? + +docker volume create "$container_name" +docker run \ + --name "$container_name" \ + --detach \ + --publish "$port":80 \ + --volume "$container_name":/appsmith-stacks \ + appsmith/appsmith-ce:"$from_tag" + +wait-for-supervisor() { + while ! docker exec "$container_name" test -e /tmp/appsmith/supervisor.sock; do + sleep 1 + done + sleep 2 +} + +wait-for-supervisor + +docker exec "$container_name" bash -exc ' +supervisorctl status \ + | awk '\''$1 != "postgres" && $1 != "stdout" {print $1}'\'' \ + | xargs supervisorctl stop + +# Insert some sample data +su postgres -c "psql -h 127.0.0.1 -c \" +create table t (id serial, name text); +insert into t values (1, '\''one'\''); +insert into t values (2, '\''two'\''); +insert into t values (3, '\''three'\''); +\"" + +supervisorctl stop postgres + +cat /appsmith-stacks/data/postgres/main/PG_VERSION +' + +docker rm -f "$container_name" + +docker run \ + --name "$container_name" \ + --detach \ + --publish "$port":80 \ + --volume "$container_name":/appsmith-stacks \ + appsmith/appsmith-ce:"$to_tag" + +wait-for-supervisor + +status=0 + +if [[ 14 != "$(docker exec "$container_name" cat /appsmith-stacks/data/postgres/main/PG_VERSION)" ]]; then + echo "Version isn't 14" + status=1 +fi + +docker exec -it "$container_name" bash + +docker rm --force "$container_name" +docker volume rm --force "$container_name" + +exit "$status" From d843647a1b66f97c72060b44b380344414547a19 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Wed, 19 Jun 2024 09:16:26 +0530 Subject: [PATCH 02/10] Don't do anything if Postgres is v13 --- deploy/docker/fs/opt/appsmith/pg-upgrade.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deploy/docker/fs/opt/appsmith/pg-upgrade.sh b/deploy/docker/fs/opt/appsmith/pg-upgrade.sh index 9684ad9c3a2f..cf220f4564b6 100644 --- a/deploy/docker/fs/opt/appsmith/pg-upgrade.sh +++ b/deploy/docker/fs/opt/appsmith/pg-upgrade.sh @@ -38,11 +38,13 @@ if [[ -f "$pg_data_dir/postmaster.pid" ]]; then exit 1 fi +top_available_version="$(postgres --version | grep -o '[[:digit:]]\+' | head -1)" + declare -a to_uninstall to_uninstall=() # 13 to 14 -if [[ "$old_version" == 13 ]]; then +if [[ "$old_version" == 13 && "$top_available_version" > "$old_version" ]]; then if [[ ! -e "$postgres_path/$old_version" ]]; then apt-get update apt-get install --yes "postgresql-$old_version" From 0cfc63c3e5fbf9ab2d309be02e98e3ab456a8c0a Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Wed, 19 Jun 2024 19:57:12 +0530 Subject: [PATCH 03/10] Assert sample table contents in the tester script --- deploy/docker/tests/pg-upgrade/run.sh | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/deploy/docker/tests/pg-upgrade/run.sh b/deploy/docker/tests/pg-upgrade/run.sh index 33fda39ce7ca..4bbcf97219bd 100755 --- a/deploy/docker/tests/pg-upgrade/run.sh +++ b/deploy/docker/tests/pg-upgrade/run.sh @@ -6,8 +6,8 @@ set -o errexit set -o nounset set -o xtrace -from_tag=release -to_tag=local +from_tag=appsmith/appsmith-ce:v1.28 +to_tag=appsmith/appsmith-ce:latest container_name=appsmith-pg-upgrade-test port=20080 @@ -23,7 +23,7 @@ docker run \ --detach \ --publish "$port":80 \ --volume "$container_name":/appsmith-stacks \ - appsmith/appsmith-ce:"$from_tag" + "$from_tag" wait-for-supervisor() { while ! docker exec "$container_name" test -e /tmp/appsmith/supervisor.sock; do @@ -59,7 +59,7 @@ docker run \ --detach \ --publish "$port":80 \ --volume "$container_name":/appsmith-stacks \ - appsmith/appsmith-ce:"$to_tag" + "$to_tag" wait-for-supervisor @@ -68,6 +68,24 @@ status=0 if [[ 14 != "$(docker exec "$container_name" cat /appsmith-stacks/data/postgres/main/PG_VERSION)" ]]; then echo "Version isn't 14" status=1 +else + sample_table_contents="$(su postgres -c 'psql -h 127.0.0.1 -c "select * from t"')" + expected_contents=' id | name +----+------- + 1 | one + 2 | two + 3 | three +(3 rows)' + if ! diff <(echo "$expected_contents") <(su postgres -c 'psql -h 127.0.0.1 -c "select * from t"'); then + echo "Table contents mismatch. Found this:" + su postgres -c 'psql -h 127.0.0.1 -c "select * from t"' + echo "Instead of this:" + echo "$expected_contents" + fi +fi + +if [[ $status == 0 && su postgres -c 'psql -h 127.0.0.1 -c "select * from t"' ]]; then + fi docker exec -it "$container_name" bash From b38f794136d242abd4d1e21c198128eeb55d9ffa Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Wed, 19 Jun 2024 19:58:30 +0530 Subject: [PATCH 04/10] revert --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 6d4ede215b22..c0338a67e47c 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -418,18 +418,18 @@ init_postgres() { # Initialize embedded postgres by default; set APPSMITH_ENABLE_EMBEDDED_DB to 0, to use existing cloud postgres mockdb instance if [[ ${APPSMITH_ENABLE_EMBEDDED_DB: -1} != 0 ]]; then tlog "Checking initialized local postgres" - local postgres_db_path="$stacks_path/data/postgres/main" + POSTGRES_DB_PATH="$stacks_path/data/postgres/main" - mkdir -p "$postgres_db_path" "$TMP/pg-runtime" + mkdir -p "$POSTGRES_DB_PATH" "$TMP/pg-runtime" # Postgres does not allow it's server to be run with super user access, we use user postgres and the file system owner also needs to be the same user postgres - chown -R postgres:postgres "$postgres_db_path" "$TMP/pg-runtime" + chown -R postgres:postgres "$POSTGRES_DB_PATH" "$TMP/pg-runtime" - if [[ -e "$postgres_db_path/PG_VERSION" ]]; then + if [[ -e "$POSTGRES_DB_PATH/PG_VERSION" ]]; then /opt/appsmith/pg-upgrade.sh else tlog "Initializing local Postgres data folder" - su postgres -c "env PATH='$PATH' initdb -D $postgres_db_path" + su postgres -c "env PATH='$PATH' initdb -D $POSTGRES_DB_PATH" fi else runEmbeddedPostgres=0 From 65d90fea6d7bda6a1be97a1ee3427328cc8b06ba Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Wed, 19 Jun 2024 20:05:22 +0530 Subject: [PATCH 05/10] Discard changes to Dockerfile --- Dockerfile | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 36a6d8d750a9..8b72f7061814 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,14 +40,6 @@ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd && chmod ugo+w /etc /appsmith-stacks \ && chmod -R ugo+w /var/run /.mongodb /etc/ssl /usr/local/share -# Temporary, until we can change Postgres version in `base.dockerfile`. -RUN < Date: Wed, 19 Jun 2024 20:07:13 +0530 Subject: [PATCH 06/10] Revert "Discard changes to Dockerfile" This reverts commit 65d90fea6d7bda6a1be97a1ee3427328cc8b06ba. --- Dockerfile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Dockerfile b/Dockerfile index 8b72f7061814..36a6d8d750a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,6 +40,14 @@ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd && chmod ugo+w /etc /appsmith-stacks \ && chmod -R ugo+w /var/run /.mongodb /etc/ssl /usr/local/share +# Temporary, until we can change Postgres version in `base.dockerfile`. +RUN < Date: Wed, 19 Jun 2024 20:10:01 +0530 Subject: [PATCH 07/10] as on ee --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 36a6d8d750a9..5bd674890c79 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,6 +40,9 @@ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd && chmod ugo+w /etc /appsmith-stacks \ && chmod -R ugo+w /var/run /.mongodb /etc/ssl /usr/local/share +LABEL com.centurylinklabs.watchtower.lifecycle.pre-check=/watchtower-hooks/pre-check.sh +LABEL com.centurylinklabs.watchtower.lifecycle.pre-update=/watchtower-hooks/pre-update.sh + # Temporary, until we can change Postgres version in `base.dockerfile`. RUN < Date: Thu, 20 Jun 2024 07:42:44 +0530 Subject: [PATCH 08/10] Discard changes to Dockerfile --- Dockerfile | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5bd674890c79..8b72f7061814 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,14 +43,6 @@ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd LABEL com.centurylinklabs.watchtower.lifecycle.pre-check=/watchtower-hooks/pre-check.sh LABEL com.centurylinklabs.watchtower.lifecycle.pre-update=/watchtower-hooks/pre-update.sh -# Temporary, until we can change Postgres version in `base.dockerfile`. -RUN < Date: Thu, 20 Jun 2024 07:53:43 +0530 Subject: [PATCH 09/10] Small fixes in tester script --- deploy/docker/tests/pg-upgrade/run.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/deploy/docker/tests/pg-upgrade/run.sh b/deploy/docker/tests/pg-upgrade/run.sh index 4bbcf97219bd..10de912c0809 100755 --- a/deploy/docker/tests/pg-upgrade/run.sh +++ b/deploy/docker/tests/pg-upgrade/run.sh @@ -77,6 +77,7 @@ else 3 | three (3 rows)' if ! diff <(echo "$expected_contents") <(su postgres -c 'psql -h 127.0.0.1 -c "select * from t"'); then + status=1 echo "Table contents mismatch. Found this:" su postgres -c 'psql -h 127.0.0.1 -c "select * from t"' echo "Instead of this:" @@ -84,10 +85,6 @@ else fi fi -if [[ $status == 0 && su postgres -c 'psql -h 127.0.0.1 -c "select * from t"' ]]; then - -fi - docker exec -it "$container_name" bash docker rm --force "$container_name" From 7c5ab1e7a4556c7874d4d76baffcb7d4c710ebe4 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Thu, 20 Jun 2024 07:55:12 +0530 Subject: [PATCH 10/10] Check for running Postgres in the upgrade script --- deploy/docker/fs/opt/appsmith/pg-upgrade.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/deploy/docker/fs/opt/appsmith/pg-upgrade.sh b/deploy/docker/fs/opt/appsmith/pg-upgrade.sh index cf220f4564b6..e646276c0cf3 100644 --- a/deploy/docker/fs/opt/appsmith/pg-upgrade.sh +++ b/deploy/docker/fs/opt/appsmith/pg-upgrade.sh @@ -17,7 +17,11 @@ set -o nounset # 4. When we can't proceed due to any exceptional scenarios, communicate clearly. # 5. Mark old/stale/deprecated data with a date, so it can be deleted with confidence later. -# TODO: Verify that no Postgres server is running. +# Check if any Postgres server is running +if pgrep -x "postgres" > /dev/null; then + echo "Error: A Postgres server is currently running. Please stop it before proceeding with the upgrade." + exit 1 +fi postgres_path=/usr/lib/postgresql