From 107c4f39df68e9d13fedcfeb5f22ceb02961d896 Mon Sep 17 00:00:00 2001
From: Andrew Kapuscinski <akapusci@nrao.edu>
Date: Thu, 15 Jul 2021 14:45:47 -0600
Subject: [PATCH] added script to initialize ngas db in the existing postgres
 container

---
 ci/psql/Dockerfile.db                | 11 ++++++++++-
 ci/psql/Dockerfile.db-ngas           |  9 ---------
 ci/psql/init_ngas_db.sh              | 18 ++++++++++++++++++
 docker-compose.yml                   | 12 ++----------
 local_ngas_root/cfg/ngamsServer.conf |  4 ++--
 5 files changed, 32 insertions(+), 22 deletions(-)
 delete mode 100644 ci/psql/Dockerfile.db-ngas
 create mode 100755 ci/psql/init_ngas_db.sh

diff --git a/ci/psql/Dockerfile.db b/ci/psql/Dockerfile.db
index b3030c5ba..32af839a3 100644
--- a/ci/psql/Dockerfile.db
+++ b/ci/psql/Dockerfile.db
@@ -1,5 +1,14 @@
 FROM postgres:12.6-alpine
 
-COPY ./ci/psql/test_data_archive.sql.gz /docker-entrypoint-initdb.d/test_data_archive.sql.gz
+ARG NGAS_VERSION=11.0.2
+
+WORKDIR /repos
+
+RUN apk add git && \
+    git clone --depth 1 --branch "v${NGAS_VERSION}" https://github.com/ICRAR/ngas.git
+
+WORKDIR /
+
+COPY ./ci/psql/init_ngas_db.sh ./ci/psql/test_data_archive.sql.gz /docker-entrypoint-initdb.d/
 
 CMD ["postgres"]
\ No newline at end of file
diff --git a/ci/psql/Dockerfile.db-ngas b/ci/psql/Dockerfile.db-ngas
deleted file mode 100644
index 5a5365593..000000000
--- a/ci/psql/Dockerfile.db-ngas
+++ /dev/null
@@ -1,9 +0,0 @@
-FROM postgres:12.6-alpine
-
-ARG NGAS_VERSION=11.0.2
-
-WORKDIR /repos
-
-RUN apk add git && \
-    git clone --depth 1 --branch "v${NGAS_VERSION}" https://github.com/ICRAR/ngas.git && \
-    cp ngas/src/ngamsCore/ngamsSql/ngamsCreateTables-PostgreSQL.sql /docker-entrypoint-initdb.d/ngamsCreateTables-PostgreSQL.sql
diff --git a/ci/psql/init_ngas_db.sh b/ci/psql/init_ngas_db.sh
new file mode 100755
index 000000000..a2fe59688
--- /dev/null
+++ b/ci/psql/init_ngas_db.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+set -e
+set -o pipefail
+
+DB=ngas
+PATH_TO_SQL_FILE=/repos/ngas/src/ngamsCore/ngamsSql/ngamsCreateTables-PostgreSQL.sql
+
+echo "Creating database '$DB' "
+
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
+        CREATE DATABASE $DB;
+        GRANT ALL PRIVILEGES ON DATABASE $DB TO archive;
+EOSQL
+
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname $DB < $PATH_TO_SQL_FILE
+
+echo "'$DB' was created successfully"
diff --git a/docker-compose.yml b/docker-compose.yml
index aae440b94..fff6bf6b4 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -22,16 +22,6 @@ services:
       POSTGRES_PASSWORD: docker
     command: postgres -c listen_addresses=*
 
-  db-ngas:
-    image: ssa-containers.aoc.nrao.edu/ops/ci/db-ngas:workspaces
-    restart: always
-    ports:
-      - "54323:5432"
-    environment:
-      POSTGRES_USER: ngas
-      POSTGRES_PASSWORD: docker
-    command: postgres -c listen_addresses=*
-
   amqp:
     image: rabbitmq:3.8-management
     restart: always
@@ -57,6 +47,8 @@ services:
     build:
       context: .
       dockerfile: ./Dockerfile.ngas
+    depends_on:
+      - db
     ports:
       - "7777:7777"
     volumes:
diff --git a/local_ngas_root/cfg/ngamsServer.conf b/local_ngas_root/cfg/ngamsServer.conf
index fd4707d0a..19be4c173 100644
--- a/local_ngas_root/cfg/ngamsServer.conf
+++ b/local_ngas_root/cfg/ngamsServer.conf
@@ -120,7 +120,7 @@ passwords will be expressed literally.
 	-->
 	<Log Id="Log" LocalLogFile="log/LogFile.nglog" LocalLogLevel="4"
 	     LogRotateCache="30" LogRotateInt="01T00:00:00" SysLog="0"
-	     SysLogPrefix="ngas" ArchiveRotatedLogfiles="1"/>
+	     SysLogPrefix="ngas" ArchiveRotatedLogfiles="0"/>
 
 	<!--
 	    Archiving configuration
@@ -182,7 +182,7 @@ passwords will be expressed literally.
         MaxPoolConnections="5" UseFileIgnore="true"
         database="/tmp/ngas.sqlite"/> -->
     <Db Id="Db" Snapshot="0" Interface="psycopg2" UseFileIgnore="true"
-        host="db-ngas" dbname="ngas" user="ngas"
+        host="db" dbname="ngas" user="archive"
         password="docker"/>
 
 	<!--
-- 
GitLab