From 6733edb1a88c0c2b6fe6f0396585503f12b1856f Mon Sep 17 00:00:00 2001
From: Maarten de Waard <maarten@greenhost.nl>
Date: Tue, 25 Oct 2022 15:16:38 +0200
Subject: [PATCH] process feedback on nextcloud postStart script

---
 README.md                                  |  2 +-
 templates/nextcloud-onlyoffice-config.yaml | 23 +++++++++++++---------
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index 783af9b2..d883b7b6 100644
--- a/README.md
+++ b/README.md
@@ -83,7 +83,7 @@ This chart adds a "postStart" command to the Nextcloud pod, that installs apps
 integration with ONLYOFFICE as well as the Stackspin OIDC provider.
 
 Kubernetes postStart commands do not log to the pod log. Instead, the script
-creates its own log in `/var/www/html/tmp/postStart<date>.log`. This means that
+creates its own log in `/var/www/tmp/postStart<date>.log`. This means that
 even if you can't `exec` into the pod (because something is failing), you can
 see the logs inside the `data` folder in the PVC. Often, if the `postStart`
 command fails, you can also see the problem by running `kubectl describe pod
diff --git a/templates/nextcloud-onlyoffice-config.yaml b/templates/nextcloud-onlyoffice-config.yaml
index addc5e9e..f1c5a808 100644
--- a/templates/nextcloud-onlyoffice-config.yaml
+++ b/templates/nextcloud-onlyoffice-config.yaml
@@ -57,14 +57,14 @@ data:
         sleep $wait
     done
 
-    echo "$occ now exists!!!"
+    echo "$occ now exists!"
 
     count=0
 
     # As soon as the $occ command exist, we know that we can run occ, but
-    # Nextcloud might still be initializing, a "lock file" will exist, wait
-    # until that's gone before running our own setup:
-    until run_as "php occ status --output json" | grep '"installed":true' || [ "$count" -gt "$limit" ]
+    # Nextcloud might still be initializing, we use `occ` to find out whether
+    # the installation process has finished, and then we continue.
+    until [[ $(run_as "php occ status --output json") =~ '"installed":true' ]] || [ "$count" -gt "$limit" ]
     do
         count=$((count+1))
         wait=$((count*10))
@@ -72,8 +72,12 @@ data:
         sleep $wait
     done
 
-    echo "Nextcloud lock file $lock is gone, we can do our thing!"
+    echo "Nextcloud is now installed, we can do our thing!"
 
+    # The `php occ status` command can, and is allowed to, fail when the `occ`
+    # file already exists, but the rest of the Nextcloud files have not been
+    # synchronized yet. However from this point in the script onwards,
+    # everything should succeed.
     set -e
 
     # Enable app store so we can run `install` and `enable` commands
@@ -120,13 +124,14 @@ data:
     fi
     {{- else }}
     # Unpinned app
-    if ! echo "$app_versions" | grep -q '"{{ .name }}"'; then
-        echo "Installing app {{ .name }}"
-        run_as "php $occ app:install {{ .name }} --keep-disabled --no-interaction"
-    else
+    if [[ "$app_versions" =~ '"{{ .name }}"' ]]
+    then
         # Update the app to its latest version
         echo "Updating app {{ .name }}"
         run_as "php $occ app:update {{ .name }} --no-interaction"
+    else
+        echo "Installing app {{ .name }}"
+        run_as "php $occ app:install {{ .name }} --keep-disabled --no-interaction"
     fi
     {{ end }} # end if and .github_repository .version
     {{- if .enabled }}
-- 
GitLab