Files
JWeBot/deploy/scripts/upgrade-wechat-local-ocr.sh
T

70 lines
2.5 KiB
Bash

#!/bin/sh
set -eu
source_script=/tmp/wechat-local-ocr.py
source_bridge=/tmp/wechat-gui-bridge
runtime_user=${JWEBOT_RUNTIME_USER:-jwebot}
runtime_python=${JWEBOT_RUNTIME_PYTHON:-/opt/jwebot/venv/bin/python}
target_script=/usr/local/sbin/wechat-local-ocr
target_bridge=/usr/local/sbin/wechat-gui-bridge
recorder_service=wechat-local-ocr-recorder.service
responder_service=wechat-local-ocr-responder.service
backup_script=$(mktemp /root/wechat-local-ocr-script.XXXXXX)
backup_bridge=$(mktemp /root/wechat-gui-bridge.XXXXXX)
success=0
recorder_active=$(systemctl is-active "$recorder_service" 2>/dev/null || true)
responder_active=$(systemctl is-active "$responder_service" 2>/dev/null || true)
cp -a "$target_script" "$backup_script"
cp -a "$target_bridge" "$backup_bridge"
restore_service_state() {
systemctl stop "$responder_service" "$recorder_service" >/dev/null 2>&1 || true
case "$recorder_active" in
active|activating|reloading)
systemctl start "$recorder_service" >/dev/null 2>&1 || true
;;
esac
case "$responder_active" in
active|activating|reloading)
systemctl start "$responder_service" >/dev/null 2>&1 || true
;;
esac
}
rollback() {
status=$?
if [ "$success" -eq 0 ]; then
cp -a "$backup_script" "$target_script"
cp -a "$backup_bridge" "$target_bridge"
restore_service_state
fi
rm -f "$backup_script" "$backup_bridge"
exit "$status"
}
trap rollback EXIT HUP INT TERM
test -f "$source_script"
test -f "$source_bridge"
test "$(find /var/lib/wechat-local-ocr/queue -maxdepth 1 -type f | wc -l)" -eq 0
touch /var/lib/wechat-local-ocr/gui-interaction.lock
chown "$runtime_user:$runtime_user" /var/lib/wechat-local-ocr/gui-interaction.lock
chmod 0600 /var/lib/wechat-local-ocr/gui-interaction.lock
systemctl stop "$responder_service" "$recorder_service" >/dev/null 2>&1 || true
install -o root -g root -m 0755 "$source_script" "$target_script"
install -o root -g root -m 0755 "$source_bridge" "$target_bridge"
"$runtime_python" -m py_compile "$target_script"
bash -n "$target_bridge"
restore_service_state
test "$(find /var/lib/wechat-local-ocr/queue -maxdepth 1 -type f | wc -l)" -eq 0
success=1
trap - EXIT HUP INT TERM
rm -f "$backup_script" "$backup_bridge"
sha256sum "$target_script" "$target_bridge"
printf 'recorder_state=%s\n' "$(systemctl is-active "$recorder_service" 2>/dev/null || true)"
printf 'responder_state=%s\n' "$(systemctl is-active "$responder_service" 2>/dev/null || true)"