From 76f96d914a0594ef81f0966d11d2769220e5e561 Mon Sep 17 00:00:00 2001
From: Francisco Barros <francisco.borges.aurindo.barros@cern.ch>
Date: Wed, 20 Apr 2022 11:59:56 +0200
Subject: [PATCH] Refactored script to be readable

---
 scripts/restore-stuck-updates.sh | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/scripts/restore-stuck-updates.sh b/scripts/restore-stuck-updates.sh
index f387d41..3c6cd4c 100755
--- a/scripts/restore-stuck-updates.sh
+++ b/scripts/restore-stuck-updates.sh
@@ -1,3 +1,27 @@
 #!/bin/sh
 
-oc get -o json drupalsite -A | jq -r '.items[] | select(.status.releaseID.current != .status.releaseID.failsafe)|"\(.metadata.name) \(.metadata.namespace) \(.status.releaseID.failsafe)"'| xargs -l bash -c 'export name=$(echo $2 | cut -d"-" -f -2); export spec=$(echo $2 |cut -d"-" -f 3"-"45); echo "[{\"op\": \"replace\",\"path\":\"/spec/version/name\",\"value\":\"${name}\"}, {\"op\":\"replace\",\"path\":\"/spec/version/releaseSpec\",\"value\":\"${spec}\"}]">patchs; oc patch drupalsite/$0 -n $1 --type json --patch-file patchs; sleep 1s'
+### FILTERs
+STUCK_WEBSITE_FILTER='.status.releaseID.current != .status.releaseID.failsafe'
+WEBSITE_LIST_OUTPUT='(.metadata.name + " " + .metadata.namespace + " " + .status.releaseID.failsafe)'
+
+# Time between patches not to overwhelm the Operator
+# This can be skipped/reduced once we have this issue closed: https://gitlab.cern.ch/webservices/webframeworks-planning/-/issues/869
+export GRACE_PERIOD="1s"
+
+## NB: This will match the spec of all websites based on failStatus version
+##
+oc get -o json drupalsite -A |
+    jq -r ".items[] | select(${STUCK_WEBSITE_FILTER}) | ${WEBSITE_LIST_OUTPUT}"  |
+        # Retrieve Spec Name from failSafe status
+        xargs -l bash -c 'export name=$(echo $2 | cut -d"-" -f -2);
+        # Retrieve spec ReleaseID from failsafe status
+        export spec=$(echo $2 |cut -d"-" -f 3"-"45);
+        # Generate Patch to be applied
+        cat > patch.yaml <<EOF
+spec:
+  version:
+    name: ${name}
+    releaseSpec: ${spec}
+EOF
+        oc patch drupalsite/$0 -n $1 --type=merge --patch-file patch.yaml;
+        sleep ${GRACE_PERIOD}'
-- 
GitLab