#!/bin/sh
SOLR_HOST=solr.elyograg.org
SOLR_PORT=443
SOLR_SCHEME=https
INDEX_NAME="dovecot"
#SOLR_COLLECTION="dovecot_2023_03"
##########################
# DO NOT MODIFY BELOW
# WITHOUT GOOD REASON
#
SOLR_URL_BASE="${SOLR_SCHEME}://${SOLR_HOST}:${SOLR_PORT}/solr"
DOVECOT_SOLR_UPDATE_URL="${SOLR_URL_BASE}/${INDEX_NAME}"
ZK_SOLR_URL="${SOLR_URL_BASE}/admin/zookeeper"
CORE_ADMIN_SOLR_URL="${SOLR_URL_BASE}/admin/cores"
COLLECTIONS_ADMIN_SOLR_URL="${SOLR_URL_BASE}/admin/collections"
DEL_ALL_QUERY_XML="<delete><query>*:*</query></delete>"
CLOUD=""
echo "=== Stopping dovecot"
sudo service dovecot stop
echo -n "=== TESTING FOR CLOUD..."
for i in 1 2 3 4
do
curl -m 2 -s -f "${ZK_SOLR_URL}" > /dev/null
RET="$?"
if [ "$RET" -eq 0 ]; then
CLOUD="true"
echo -n " Cloud detected"
RESPONSE="$(curl -s "${COLLECTIONS_ADMIN_SOLR_URL}?action=CLUSTERSTATUS&wt=json")"
SOLR_COLLECTION="$(echo "$RESPONSE" | jq -r ".cluster.aliases.\"${INDEX_NAME}\"")"
if [ -z "${SOLR_COLLECTION}" ]; then
SOLR_COLLECTION="${INDEX_NAME}"
fi
break
else
sleep 5
fi
done
echo
RET="1"
for i in 1 2 3 4
do
echo "=== Attempt ${i} of index wipe"
curl -m 2 -f -s \
"${DOVECOT_SOLR_UPDATE_URL}/update?commit=true&optimize=true&wt=json" \
-H "Content-Type: text/xml" \
--data-binary "${DEL_ALL_QUERY_XML}"
RET="$?"
if [ "${RET}" -eq 0 ]; then
break
fi
sleep 5
done
echo
ABORT_REBUILD=""
if [ "${RET}" -ne 0 ]; then
echo "===== Wipe failed! Aborting."
ABORT_REBUILD="true"
fi
if [ -z "${ABORT_REBUILD}" ]; then
RET="1"
for i in 1 2 3 4
do
echo "=== Attempt ${i} of index reload"
if [ -z "${CLOUD}" ]; then
curl -m 2 -f -s \
"${CORE_ADMIN_SOLR_URL}?action=RELOAD&core=${INDEX_NAME}&wt=json"
RET="$?"
else
curl -m 2 -f -s \
"${COLLECTIONS_ADMIN_SOLR_URL}?action=RELOAD&name=${SOLR_COLLECTION}&wt=json"
RET="$?"
fi
if [ "${RET}" -eq 0 ]; then
break
fi
sleep 1
done
echo
if [ "${RET}" -ne 0 ]; then
echo "----- Reload failed! Continuing anyway."
fi
fi
echo "=== Starting dovecot"
sudo service dovecot start
if [ -z "${ABORT_REBUILD}" ]; then
echo "--- force-resync"
sudo doveadm force-resync -A '*'
echo "--- fts rescan"
sudo doveadm fts rescan -A
echo "--- fts index"
sudo doveadm index -A -q '*'
echo "=== Now reindexing"
fi