From 80ddc39767cfb2e32ae95f6f26a9dd3bd8570be2 Mon Sep 17 00:00:00 2001
From: Boxiang Sun <boxiang.sun@nexedi.com>
Date: Fri, 14 Oct 2022 11:18:11 +0200
Subject: [PATCH] peertube: Configure database

---
 software/peertube/instance.cfg.in             | 24 ++++++-------
 .../peertube/template-peertube-service.sh.in  | 36 ++++++++++++++++++-
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/software/peertube/instance.cfg.in b/software/peertube/instance.cfg.in
index 8327fce5c..85be76117 100644
--- a/software/peertube/instance.cfg.in
+++ b/software/peertube/instance.cfg.in
@@ -6,7 +6,6 @@ parts =
   service-redis
   promise-redis
   postgresql
-  postgresql-extension
   nginx-service
   nginx-listen-promise
   peertube-service
@@ -68,15 +67,21 @@ init =
 
   with open(options['production_config_template'], "r", encoding='utf-8') as config_template_file:
     with open(options['production_config_prod'], "w", encoding='utf-8') as config_prod_file:
-      print('Fuck!!!')
-      print(options['production_config_template'])
-      print(options['production_config_prod'])
+      local_host_counter = 0
       for line in config_template_file:
           if "hostname: 'example.com'" in line:
             line = line.replace("example.com", "https://[$${nginx-configuration:ip}]/$${nginx-configuration:port}")
-          if "password: 'peertube'" in line:
+          elif "password: 'peertube'" in line:
             line = line.replace("peertube", "$${postgresql:password}")
-          if "/var/www/peertube" in line:
+          elif "hostname: 'localhost'" in line:
+            local_host_counter += 1
+            if local_host_counter == 2:
+              line = line.replace("localhost", "$${postgresql:ipv4}")
+            if local_host_counter == 3:
+              line = '' # set 'hostname' and 'port' to null, use redis.socket
+          elif "6379" in line:
+            line = line.replace("port: 6379", "unixsocket: $${service-redis:unixsocket}") # set 'hostname' and 'port' to null, use redis.socket
+          elif "/var/www/peertube" in line:
             line = line.replace("/var/www/peertube", "$${directory:peertube_directory}")
           config_prod_file.write(line)
 
@@ -102,13 +107,6 @@ ipv4 = $${instance-parameter:ipv4-random}
 ipv6 =
 port = 5432
 
-[postgresql-extension]
-recipe = slapos.cookbook:wrapper
-wrapper-path = $${directory:bin}/$${:_buildout_section_name_}
-command-line =
-    $${postgresql:bin}/psql -c "CREATE EXTENSION pg_trgm;" $${postgresql:dbname}
-    $${postgresql:bin}/psql -c "CREATE EXTENSION unaccent;" $${postgresql:dbname}
-
 [service-postgresql]
 recipe  = slapos.cookbook:postgres
 bin     = {{ postgresql_location }}/bin
diff --git a/software/peertube/template-peertube-service.sh.in b/software/peertube/template-peertube-service.sh.in
index adcfcf5f2..9f64f1df7 100644
--- a/software/peertube/template-peertube-service.sh.in
+++ b/software/peertube/template-peertube-service.sh.in
@@ -1,4 +1,38 @@
 cd ${peertube:location} &&
 
-exec env NODE_ENV=production NODE_CONFIG_DIR=$${peertube-setup:peertube_config} \
+# setup db
+
+die() {
+    echo "$*" 1>&2
+    exit 1
+}
+
+# run psql on gitlab db
+psql() {
+    $${postgresql:bin}/psql \
+        -h $${postgresql:pgdata-directory}  \
+        -U $${postgresql:superuser}         \
+        -d $${postgresql:dbname}            \
+        "$@"
+}
+
+# initial db setup
+# ( first quering PG several times waiting a bit till postgresql is started and ready )
+tpgwait=5
+while true; do
+    pgtables="$(psql -c '\d' 2>&1)" && break
+    tpgwait=$(( $tpgwait - 1 ))
+    test $tpgwait = 0 && die "pg query problem"
+    echo "I: PostgreSQL is not ready (yet ?); will retry $tpgwait times..." 1>&2
+    sleep 1
+done
+echo "I: PostgreSQL ready." 1>&2
+
+# make sure pg_trgm extension is enabled for peertube db
+# $${postgresql:bin}/psql -c 'CREATE EXTENSION IF NOT EXISTS pg_trgm;'  $${postgresql:dbname} || die "pg_trgm setup failed"
+
+# make sure unaccent extension is enabled for peertube db
+# $${postgresql:bin}/psql -c 'CREATE EXTENSION IF NOT EXISTS unaccent;'  $${postgresql:dbname} || die "unaccent setup failed"
+
+exec env NODE_ENV=production NODE_CONFIG_DIR=$${peertube-setup:peertube_config} PATH=${ffmpeg:location}/bin:$PATH\
   ${nodejs:location}/bin/node ${peertube:location}/dist/server
-- 
2.30.9