From c50a462842724ebe874d0d18bf1d67165061de12 Mon Sep 17 00:00:00 2001
From: Konrad Botor <kbotor@syncad.com>
Date: Tue, 11 Feb 2025 14:44:12 +0100
Subject: [PATCH] Fix for building instance image from submodule

---
 Dockerfile                  |  4 ++--
 docker/docker_entrypoint.sh |  4 ++--
 scripts/setup_postgres.sh   | 45 ++++++++++++++++++++++++++++---------
 3 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index d58734949..612e0455b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -105,7 +105,7 @@ RUN \
   find . -name *.a  -type f -delete
 
 # Here we could use a smaller image without packages specific to build requirements
-FROM ${CI_REGISTRY_IMAGE}ci-base-image:$CI_IMAGE_TAG as base_instance
+FROM ${CI_REGISTRY_IMAGE}ci-base-image:$CI_IMAGE_TAG AS base_instance
 
 ENV BUILD_IMAGE_TAG=${BUILD_IMAGE_TAG:-:ubuntu22.04-8}
 
@@ -197,7 +197,7 @@ LABEL io.hive.image.commit.log_message="$GIT_LAST_LOG_MESSAGE"
 LABEL io.hive.image.commit.author="$GIT_LAST_COMMITTER"
 LABEL io.hive.image.commit.date="$GIT_LAST_COMMIT_DATE"
 
-FROM ${CI_REGISTRY_IMAGE}${IMAGE_TAG_PREFIX}base_instance:${BUILD_IMAGE_TAG} as instance
+FROM ${CI_REGISTRY_IMAGE}${IMAGE_TAG_PREFIX}base_instance:${BUILD_IMAGE_TAG} AS instance
 
 # Embedded postgres service
 EXPOSE 5432
diff --git a/docker/docker_entrypoint.sh b/docker/docker_entrypoint.sh
index d390d744b..0b90ac7da 100755
--- a/docker/docker_entrypoint.sh
+++ b/docker/docker_entrypoint.sh
@@ -254,7 +254,7 @@ if sudo --user=postgres -n [ ! -d "$PGDATA" -o ! -f "$PGDATA/PG_VERSION" ]; then
 
   echo "Attempting to setup postgres instance: running setup_postgres.sh..."
 
-  sudo -n "/home/haf_admin/source/${HIVE_SUBDIR}/scripts/setup_postgres.sh" --haf-admin-account=haf_admin --haf-binaries-dir="/home/haf_admin/build" --haf-database-store="/home/hived/datadir/haf_db_store/tablespace" --install-extension="${HAF_INSTALL_EXTENSION:-"yes"}"
+  sudo -n "/home/haf_admin/source/${HIVE_SUBDIR}/scripts/setup_postgres.sh" --haf-admin-account=haf_admin --haf-binaries-dir="/home/haf_admin/build" --haf-database-store="/home/hived/datadir/haf_db_store/tablespace" --install-extension="${HAF_INSTALL_EXTENSION:-"yes"},/home/haf_admin/build,/usr/share/postgresql/${POSTGRES_VERSION},/usr/lib/postgresql/${POSTGRES_VERSION}"
 
   echo "Postgres instance setup completed."
 
@@ -265,7 +265,7 @@ else
   echo "Attempting to setup postgres instance already containing HAF database..."
 
   # in case when container is restarted over already existing (and potentially filled) data directory, we need to be sure that docker-internal postgres has deployed HFM extension
-  sudo -n "/home/haf_admin/source/${HIVE_SUBDIR}/scripts/setup_postgres.sh" --haf-admin-account=haf_admin --haf-binaries-dir="/home/haf_admin/build" --haf-database-store="/home/hived/datadir/haf_db_store/tablespace" --install-extension="${HAF_INSTALL_EXTENSION:-"yes"}"
+  sudo -n "/home/haf_admin/source/${HIVE_SUBDIR}/scripts/setup_postgres.sh" --haf-admin-account=haf_admin --haf-binaries-dir="/home/haf_admin/build" --haf-database-store="/home/hived/datadir/haf_db_store/tablespace" --install-extension="${HAF_INSTALL_EXTENSION:-"yes"},/home/haf_admin/build,/usr/share/postgresql/${POSTGRES_VERSION},/usr/lib/postgresql/${POSTGRES_VERSION}"
   sudo -n "/usr/share/postgresql/${POSTGRES_VERSION}/extension/hive_fork_manager_update_script_generator.sh" --haf-admin-account=haf_admin --haf-db-name=haf_block_log
 
   echo "Postgres instance setup completed."
diff --git a/scripts/setup_postgres.sh b/scripts/setup_postgres.sh
index 143f716aa..ef08653aa 100755
--- a/scripts/setup_postgres.sh
+++ b/scripts/setup_postgres.sh
@@ -84,14 +84,30 @@ EOF
 }
 
 install_extension() {
-  echo "Script path is: $SCRIPTPATH"
-  local build_dir
-  build_dir=$(realpath -e --relative-base="$SCRIPTPATH" "$1")
-  build_dir=${build_dir%%[[:space:]]}
-  echo "Attempting to install hive_fork_manager extenstion into PostgreSQL directories..."
-  pushd "$build_dir" || return
-  ninja install
-  popd || return
+  local src_dir=${1}
+  local share_dir=${2:-}
+  local lib_dir=${3:-}
+
+  # If the path to the extension and the PostgreSQL installation are provided,
+  # simply copy the files.
+  # Otherwise, build and install the extension using ninja.
+  if [[ -n $share_dir && -n $lib_dir ]]; then
+    echo -e "Attempting to install hive_fork_manager extenstion from $src_dir into PostgreSQL directories at $share_dir and $lib_dir..."
+    cp --verbose "$src_dir/extensions/hive_fork_manager/"* "$share_dir/extension/"
+    cp --verbose "$src_dir/lib/libquery_supervisor.so" "$lib_dir/lib/"
+    cp --verbose "$src_dir/lib/libhfm-"* "$lib_dir/lib/"
+    ls -lah "$share_dir/extension/"
+    ls -lah "$lib_dir/lib/"
+  else
+    echo "Script path is: $SCRIPTPATH"
+    local build_dir
+    build_dir=$(realpath -e --relative-base="$SCRIPTPATH" "$src_dir")
+    build_dir=${build_dir%%[[:space:]]}
+    echo "Attempting to install hive_fork_manager extenstion into PostgreSQL directories..."
+    pushd "$build_dir" || return
+    ninja install
+    popd || return
+  fi
 }
 
 HAF_ADMIN_ACCOUNT="haf_admin"
@@ -103,6 +119,9 @@ HAF_BINARY_DIR="../build"
 POSTGRES_HOST="/var/run/postgresql"
 POSTGRES_PORT=5432
 INSTALL_EXTENSION=""
+EXTENSION_SRC=""
+SHARED_DST=""
+LIB_DST=""
 
 while [ $# -gt 0 ]; do
   case "$1" in
@@ -122,7 +141,13 @@ while [ $# -gt 0 ]; do
         HAF_TABLESPACE_LOCATION="${1#*=}"
         ;;
     --install-extension=*)
-        INSTALL_EXTENSION="${1#*=}"
+        # Read comma-separated options into an array
+        IFS="," read -ra INSTALL_EXTENSION_OPTIONS <<< "${1#*=}"
+        # The first option is mandatory, the other three are optional
+        INSTALL_EXTENSION="${INSTALL_EXTENSION_OPTIONS[0]}"
+        EXTENSION_SRC="${INSTALL_EXTENSION_OPTIONS[1]:-"${HAF_BINARY_DIR}"}"
+        SHARED_DST="${INSTALL_EXTENSION_OPTIONS[2]:-}"
+        LIB_DST="${INSTALL_EXTENSION_OPTIONS[3]:-}"
         ;;
     --help)
         print_help
@@ -152,7 +177,7 @@ if [ "$EUID" -ne 0 ]
 fi
 
 if [ "$INSTALL_EXTENSION" != "no" ]; then
-  install_extension "$HAF_BINARY_DIR"
+  install_extension "$EXTENSION_SRC" "$SHARED_DST" "$LIB_DST"
 fi
 
 # Be sure PostgreSQL is started.
-- 
GitLab