sportrum-deploy/upgrade-funs.sh

107 lines
3.4 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# runcmd sends a command to the running app via the commands.txt /
# status.<cmd>.$pid handshake and blocks until the app reports completion.
#
# Was hardcoded to "storeall" only. Parameterized so the same function can
# drive any command that follows this same protocol - storeall today,
# finishcalls and dumpregistry once those are wired up server-side.
#
# Usage: runcmd <resultvar> <command> [command args...]
# runcmd result storeall
# runcmd result finishcalls 1
# runcmd result dumpregistry
#
# Requires $pid to already be set in the caller's scope (e.g. pid=$$),
# matching the existing convention in upgrade.sh/restart.sh.
function runcmd {
local __resultvar=$1
local cmd=$2
shift 2
local cmdargs="$@"
local storagedir="/home/sportrum/webapp/sportrum/WEB-INF/storage"
local sfile="${storagedir}/status.${cmd}.$pid"
rm -f ${sfile}
printf "%s $pid%s" "$cmd" "${cmdargs:+ $cmdargs}" > ${storagedir}/commands.txt || exit 1
while [ ! -f $sfile ]
do
sleep 1
done
while true
do
status=$(cut -d " " -f 1 $sfile)
if [ "x$(echo $status | cut -d ':' -f 1)" == "xcompleted" ]
then
local funresult="$(echo $status | cut -d ':' -f 2)"
eval $__resultvar="'$funresult'"
break;
else
echo "$(cat $sfile)"
sleep 1
fi
done
}
# maintenance_log drops one XML file per event into the maintenance-log
# folder for the sportrum webapp to pick up, deserialize into a
# MaintenanceLog object, and act on (trigger sms/email/etc through its
# own machinery - see MaintenanceLog.java). Fire-and-forget: this
# function doesn't wait for anything to process the file, and there's no
# execution-status handshake back to the caller - it just needs to land a
# well-formed file.
#
# Usage: maintenance_log <severity> <operation> <action> <reason> <message> [extra_xml]
# maintenance_log info nightly_storeall storeall quiet "storeall completed."
#
# severity/operation/action/reason are free-form lowercase tokens (e.g.
# severity: info|warning|error, action: nop|storeall, reason:
# busy|quiet|no_quiet_window) - adjust the exact vocabulary to match
# whatever the webapp's Severity/MaintenanceType lookups actually expect.
#
# <extra_xml>, if given, is inserted verbatim as additional children
# inside <details> (e.g. "<objectsStored>42</objectsStored>") - caller is
# responsible for passing well-formed XML there.
#
# Written via a temp file + atomic rename so whatever's watching the
# directory never sees a partially-written file.
function maintenance_log {
local severity=$1
local operation=$2
local action=$3
local reason=$4
local message=$5
local extra_xml="${6:-}"
local logdir="/home/sportrum/webapp/sportrum/WEB-INF/storage/maintenance-log"
mkdir -p "$logdir"
local esc_message
esc_message=$(printf '%s' "$message" | sed -e 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g')
local ts_utc
ts_utc="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
local fname
fname="$(date -u +%Y%m%dT%H%M%S).$$.${severity}"
local tmpfile="${logdir}/.${fname}.tmp"
local finalfile="${logdir}/${fname}.xml"
cat > "$tmpfile" <<EOF
<MaintenanceLog>
<creationTime>${ts_utc}</creationTime>
<severity>${severity}</severity>
<operation>${operation}</operation>
<action>${action}</action>
<reason>${reason}</reason>
<details>
<script>$(basename "$0")</script>
<host>$(hostname)</host>
<message>${esc_message}</message>
${extra_xml}
</details>
</MaintenanceLog>
EOF
mv "$tmpfile" "$finalfile"
}