From 15a6c943cd68643f817ea13b30df91e2c1cd01fe Mon Sep 17 00:00:00 2001
From: Julien Leduc <julien.leduc@cern.ch>
Date: Mon, 11 Feb 2019 16:25:58 +0100
Subject: [PATCH] Adding everything needed to run a standalong postgres
 instance inside the kubernetes CI namespace and externally. More in
 README_postgres.md

---
 .../ctafrontend/cc7/opt/run/bin/init.sh       |  7 +-
 .../cc7/opt/run/bin/init_database.sh          |  5 ++
 .../orchestration/README_postgres.md          | 18 +++++
 .../orchestration/create_instance.sh          | 15 +++-
 .../database-postgres-sample.yaml             | 14 ++++
 .../orchestration/internal_postgres.yaml      | 71 +++++++++++++++++++
 .../orchestration/run_systemtest.sh           | 13 +++-
 7 files changed, 135 insertions(+), 8 deletions(-)
 create mode 100644 continuousintegration/orchestration/README_postgres.md
 create mode 100644 continuousintegration/orchestration/database-postgres-sample.yaml
 create mode 100644 continuousintegration/orchestration/internal_postgres.yaml

diff --git a/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init.sh b/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init.sh
index ae05209981..501b6feeb0 100755
--- a/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init.sh
+++ b/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init.sh
@@ -58,12 +58,15 @@ if [ "$KEEP_DATABASE" == "0" ]; then
     mkdir -p $(dirname $(echo ${DATABASEURL} | cut -d: -f2))
     cta-catalogue-schema-create /etc/cta/cta-catalogue.conf
     chmod -R 777 $(dirname $(echo ${DATABASEURL} | cut -d: -f2)) # needed?
-  else
-    # Oracle DB
+  elif [ "$DATABASETYPE" == "oracle" ]; then
     echo "Purging Oracle recycle bin"
     test -f ${ORACLE_SQLPLUS} || echo "ERROR: ORACLE SQLPLUS client is not present, cannot purge recycle bin: ${ORACLE_SQLPLUS}"
     LD_LIBRARY_PATH=$(readlink ${ORACLE_SQLPLUS} | sed -e 's;/bin/[^/]\+;/lib;') ${ORACLE_SQLPLUS} $(echo $DATABASEURL | sed -e 's/oracle://') @/opt/ci/init/purge_recyclebin.ext
     cta-catalogue-schema-create /etc/cta/cta-catalogue.conf
+  elif [ "$DATABASETYPE" == "postgres" ]; then
+    cta-catalogue-schema-create /etc/cta/cta-catalogue.conf
+  else
+    echo "${DATABASETYPE}: Unsupported database type."
   fi
 else
   echo "Reusing database (no check)"
diff --git a/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init_database.sh b/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init_database.sh
index 0434e4d507..4d42899a0a 100755
--- a/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init_database.sh
+++ b/continuousintegration/docker/ctafrontend/cc7/opt/run/bin/init_database.sh
@@ -28,6 +28,11 @@ case "$(get_conf database.type)" in
     DATABASETYPE=oracle
     DATABASEURL=oracle:$(get_conf database.oracle.username)/$(get_conf database.oracle.password)@$(get_conf database.oracle.database)
   ;;
+  "postgres")
+     echo "Configuring postgres database"
+     DATABASETYPE=postgres
+     DATABASEURL=postgresql:postgresql://$(get_conf database.postgres.username):$(get_conf database.postgres.password)@$(get_conf database.postgres.server)/$(get_conf database.postgres.database)
+  ;;
   *)
     echo "Error unknown database type: $(get_conf database.type)"
     exit 1
diff --git a/continuousintegration/orchestration/README_postgres.md b/continuousintegration/orchestration/README_postgres.md
new file mode 100644
index 0000000000..1403100bd2
--- /dev/null
+++ b/continuousintegration/orchestration/README_postgres.md
@@ -0,0 +1,18 @@
+# Running with `postgres`
+
+## External postgres instance:
+
+Modify `database-postgres-sample.yaml` file to suit your configuration and submit it to `./run_systemtest.sh` using the `-d` option for example:
+```
+[root@ctadevjulien orchestration]# (postgres_CI) ./run_systemtest.sh -S -O -d database_postgres_dbod.yaml -s tests/archive_retrieve.sh -n toto -k
+```
+
+## Internal postgres instance:
+
+In this case the postgres instance is launched in a `postgres` pod in the namespace.
+This configuration allows developers to launch a standalone offline instance that does not require any externally configured database.
+`internal_postgres.yaml` contains everything needed to launch an internal postgres instance, contact it with namespace DNS and configure CTA accordingly.
+Here is an instanciation example of a standalone postgres DB instance with a local objectstore:
+```
+[root@ctadevjulien orchestration]# (postgres_CI) ./run_systemtest.sh -S -d internal_postgres.yaml -s tests/archive_retrieve.sh -n toto -k
+```
diff --git a/continuousintegration/orchestration/create_instance.sh b/continuousintegration/orchestration/create_instance.sh
index dab70ca3b5..1e6281ec7b 100755
--- a/continuousintegration/orchestration/create_instance.sh
+++ b/continuousintegration/orchestration/create_instance.sh
@@ -25,7 +25,7 @@ keepobjectstore=1
 
 usage() { cat <<EOF 1>&2
 Usage: $0 -n <namespace> [-o <objectstore_configmap>] [-d <database_configmap>] \
-      [-e <eos_configmap>] \
+      [-e <eos_configmap>] [-a <additional_k8_resources>]\
       [-p <gitlab pipeline ID> | -b <build tree base> -B <build tree subdir> ]  \
       [-S] [-D] [-O] [-m [mhvtl|ibm]]
 
@@ -36,13 +36,14 @@ Options:
   -B    The subdirectory within the -b directory where the build tree is.
   -D	wipe database content during initialization phase (database content is kept by default)
   -O	wipe objectstore content during initialization phase (objectstore content is kept by default)
+  -a    additional kubernetes resources added to the kubernetes namespace
 EOF
 exit 1
 }
 
 die() { echo "$@" 1>&2 ; exit 1; }
 
-while getopts "n:o:d:e:p:b:B:SDOm:" o; do
+while getopts "n:o:d:e:a:p:b:B:SDOm:" o; do
     case "${o}" in
         o)
             config_objectstore=${OPTARG}
@@ -56,6 +57,10 @@ while getopts "n:o:d:e:p:b:B:SDOm:" o; do
             config_eos=${OPTARG}
             test -f ${config_eos} || error="${error}EOS configmap file ${config_eos} does not exist\n"
             ;;
+        a)
+            additional_resources=${OPTARG}
+            test -f ${additional_resources} || error="${error}File ${additional_resources} does not exist\n"
+            ;;
         m)
             model=${OPTARG}
             if [ "-${model}-" != "-ibm-" ] && [ "-${model}-" != "-mhvtl-" ] ; then error="${error}Library model ${model} does not exist\n"; fi 
@@ -181,13 +186,17 @@ kubectl --namespace ${instance} create configmap init --from-literal=keepdatabas
 
 kubectl --namespace ${instance} create configmap buildtree --from-literal=base=${buildtree} --from-literal=subdir=${buildtreesubdir}
 
+if [ ! -z "${additional_resources}" ]; then
+  kubectl --namespace ${instance} create -f ${additional_resources} || die "Could not create additional resources described in ${additional_resources}"
+  kubectl --namespace ${instance} get pods -a
+fi
+
 echo "creating configmaps in instance"
 
 kubectl create -f ${config_objectstore} --namespace=${instance}
 kubectl create -f ${config_database} --namespace=${instance}
 kubectl create -f ${config_eos} --namespace=${instance}
 
-
 echo -n "Requesting an unused ${model} library"
 kubectl create -f ./pvc_library_${model}.yaml --namespace=${instance}
 for ((i=0; i<120; i++)); do
diff --git a/continuousintegration/orchestration/database-postgres-sample.yaml b/continuousintegration/orchestration/database-postgres-sample.yaml
new file mode 100644
index 0000000000..e74b1e88b6
--- /dev/null
+++ b/continuousintegration/orchestration/database-postgres-sample.yaml
@@ -0,0 +1,14 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: database-config
+  labels:
+    config: database
+    type: postgres
+data:
+  database.type: postgres
+  database.postgres.username: cta
+  database.postgres.password: cta
+  database.postgres.database: cta
+  # postgres server points to the postgres external server
+  database.postgres.server: postgres1234.cern.ch
diff --git a/continuousintegration/orchestration/internal_postgres.yaml b/continuousintegration/orchestration/internal_postgres.yaml
new file mode 100644
index 0000000000..2b16819bf3
--- /dev/null
+++ b/continuousintegration/orchestration/internal_postgres.yaml
@@ -0,0 +1,71 @@
+###
+# Internal postgres server
+# implements:
+# - a postgres server relying on official docker images
+# - a postgres service
+# - a configmap for pod configuration and CTA configuration
+###
+apiVersion: v1
+kind: Service
+metadata:
+  name: postgres
+  labels:
+    k8s-app: postgres
+spec:
+  selector:
+    k8s-app: postgres
+  clusterIP: None
+  ports:
+  - name: postgres
+    port: 5432
+    protocol: TCP
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: database-config
+  labels:
+    config: database
+    type: postgres
+data:
+  database.type: postgres
+  database.postgres.username: cta
+  database.postgres.password: cta
+  database.postgres.database: cta
+  database.postgres.path: /var/lib/postgresql/data/pgdata
+  # postgres service points to the postgresql container in namespace
+  database.postgres.server: postgres
+---
+apiVersion: v1
+kind: Pod
+metadata:
+  name: postgres
+  labels:
+    k8s-app: postgres
+spec:
+  restartPolicy: Never
+  containers:
+  - name: postgres
+    image: postgres:9.6
+    stdin: true
+    env:
+    - name: POSTGRES_USER
+      valueFrom:
+        configMapKeyRef:
+          name: database-config
+          key: database.postgres.username
+    - name: POSTGRES_PASSWORD
+      valueFrom:
+        configMapKeyRef:
+          name: database-config
+          key: database.postgres.password
+    - name: PGDATA
+      valueFrom:
+        configMapKeyRef:
+          name: database-config
+          key: database.postgres.path
+    - name: POSTGRES_DB
+      valueFrom:
+        configMapKeyRef:
+          name: database-config
+          key: database.postgres.database
diff --git a/continuousintegration/orchestration/run_systemtest.sh b/continuousintegration/orchestration/run_systemtest.sh
index cefadc2c75..66a9af44fb 100755
--- a/continuousintegration/orchestration/run_systemtest.sh
+++ b/continuousintegration/orchestration/run_systemtest.sh
@@ -27,7 +27,7 @@ SYSTEMTEST_TIMEOUT=3600
 die() { echo "$@" 1>&2 ; exit 1; }
 
 usage() { cat <<EOF 1>&2
-Usage: $0 -n <namespace> -s <systemtest_script> [-p <gitlab pipeline ID> | -b <build tree base> -B <build tree subdir> ] [-t <systemtest timeout in seconds>] [-e <eos_configmap>] [-k] [-O] [-D] [-S]
+Usage: $0 -n <namespace> -s <systemtest_script> [-p <gitlab pipeline ID> | -b <build tree base> -B <build tree subdir> ] [-t <systemtest timeout in seconds>] [-e <eos_configmap>] [-a <additional_k8_resources>] [-k] [-O] [-D | -d <database_configmap>] [-S]
 
 Options:
   -b    The directory containing both the source and the build tree for CTA. It will be mounted RO in the
@@ -36,7 +36,8 @@ Options:
   -k    keep namespace after systemtest_script run if successful
   -O    use Ceph account associated to this node (wipe content before tests), by default use local VFS
   -D    use Oracle account associated to this node (wipe content before tests), by default use local sqlite DB
-  -S    Use systemd to manage services inside containers 
+  -S    Use systemd to manage services inside containers
+  -a    additional kubernetes resources added to the kubernetes namespace
 
 
 Create a kubernetes instance and launch the system test script specified.
@@ -50,7 +51,7 @@ exit 1
 # always delete DB and OBJECTSTORE for tests
 CREATE_OPTS="-D -O"
 
-while getopts "n:s:p:b:e:B:t:kDOS" o; do
+while getopts "n:d:s:p:b:e:a:B:t:kDOS" o; do
     case "${o}" in
         s)
             systemtest_script=${OPTARG}
@@ -59,6 +60,9 @@ while getopts "n:s:p:b:e:B:t:kDOS" o; do
         n)
             namespace=${OPTARG}
             ;;
+        d)
+            CREATE_OPTS="${CREATE_OPTS} -d ${OPTARG}"
+            ;;
         p)
             CREATE_OPTS="${CREATE_OPTS} -p ${OPTARG}"
             ;;
@@ -69,6 +73,9 @@ while getopts "n:s:p:b:e:B:t:kDOS" o; do
             config_eos=${OPTARG}
             test -f ${config_eos} || error="${error}EOS configmap file ${config_eos} does not exist\n"
             ;;
+        a)
+            CREATE_OPTS="${CREATE_OPTS} -a ${OPTARG}"
+            ;;
         B)
             CREATE_OPTS="${CREATE_OPTS} -B ${OPTARG}"
             ;;
-- 
GitLab