signal-bot/scripts/install_service.sh
Krzysztof kuhy Rudnicki c35e382cd6 feat: make the bot actually reply on Signal, and gate every way it went silent
First live reply, as its own registered account. `!cat` and `!dog` from the
phone return images -- verified in the production shape (the bot *invited to
an already-existing group*) and under a systemd user unit.

Four failures, each of which left the bot looking perfectly healthy:

- Invited is not joined. Signal only adds an account outright if the adder
  holds its profile key; with no profile the add becomes a pending invite,
  and signal-cli cannot accept one addressed to a phone number ("Cannot find
  service ID for self"). Group listed, websocket up, /healthz OK, zero
  messages. register_bot.sh now sets the profile *before* the add, and
  startup refuses on `member: false`.
- MODE=json-rpc-native is a GraalVM build with no AWT: text sends work,
  every attachment 400s with "Can't load library: awt". Both mode guards now
  demand exactly json-rpc, and start_container replaces a wrong-mode
  container instead of reusing it.
- The API spells one group two ways -- /v1/groups returns `group.<base64>`,
  envelopes carry what that decodes to -- so the scoping gate rejected the
  bot's own group. e2e.sh now models both spellings; reverting the fix fails
  4 scenarios, which is why the offline suite missed it before.
- BIND_PORT in .env was dead: __main__ read os.getenv before anything loaded
  a .env. The bind address comes from Config now.

A taken port used to be uvicorn logging the collision and exiting 0, which
every supervisor reads as success. portcheck.py refuses to start, exits
non-zero, and names the holder by reading /proc directly -- no subprocess, no
PATH assumptions, testable against a fake proc tree. Default port is 8137:
not 8000, and below the ephemeral range so the kernel cannot take it.

The bot now runs as signal-bot.service (Restart=always, linger checked rather
than assumed, StartLimitBurst so a config error fails visibly instead of
looping). Verified by SIGKILL and by re-running a command under the unit.

100% branch coverage, 230 tests, all gates green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yaBT45KhHqxTKVCTA87iy
2026-08-29 14:37:27 +02:00

124 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# Install, start and verify the signal-bot user service.
#
# A *user* unit, not a system one: the bot runs as whoever registered the
# Signal account and reads that user's .env. Lingering is what makes it start
# at boot with nobody logged in -- without it, "survives reboots" is a lie the
# unit file cannot tell you about, so this script checks and fails loudly.
# ============================================================================
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
readonly REPO_ROOT
SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_NAME
readonly UNIT_NAME="signal-bot.service"
readonly UNIT_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user"
readonly TEMPLATE="${REPO_ROOT}/scripts/${UNIT_NAME}"
readonly DEFAULT_PORT=8137
VERIFY_ONLY="false"
# shellcheck source=scripts/lib_signal.sh
source "${REPO_ROOT}/scripts/lib_signal.sh"
usage() {
echo "Usage: $SCRIPT_NAME [--verify-only]"
echo " --verify-only Check the running service without reinstalling it."
exit 0
}
die() {
echo "Error: $1" >&2
exit 1
}
# The port the bot will actually bind: .env wins, code default otherwise.
bot_port() {
local port=""
if [[ -f "${REPO_ROOT}/.env" ]]; then
port="$(sed -n 's/^BIND_PORT=[[:space:]]*//p' "${REPO_ROOT}/.env" | tail -1)"
fi
printf '%s' "${port:-$DEFAULT_PORT}"
}
validate_requirements() {
command -v systemctl >/dev/null || die "systemctl not found; this needs systemd"
[[ -f "${REPO_ROOT}/.env" ]] ||
die "no ${REPO_ROOT}/.env -- run scripts/register_bot.sh first"
[[ -x "${REPO_ROOT}/run.sh" ]] || die "no executable ${REPO_ROOT}/run.sh"
[[ -f "$TEMPLATE" ]] || die "no unit template at $TEMPLATE"
}
# Without linger, a user unit dies at logout and never starts at boot.
require_linger() {
if [[ "$(loginctl show-user "$USER" --property=Linger --value)" == "yes" ]]; then
echo "== linger enabled: the bot starts at boot with nobody logged in"
return 0
fi
echo "== enabling linger for $USER"
if ! sudo -n loginctl enable-linger "$USER" 2>/dev/null; then
echo "Error: linger is off and sudo needs a password here." >&2
echo " Without it the bot will NOT come back after a reboot." >&2
echo " Run this, then re-run with --verify-only:" >&2
echo >&2
echo " sudo loginctl enable-linger $USER" >&2
return 1
fi
}
install_unit() {
mkdir -p "$UNIT_DIR"
sed "s|@REPO_ROOT@|${REPO_ROOT}|g" "$TEMPLATE" > "${UNIT_DIR}/${UNIT_NAME}"
systemctl --user daemon-reload
systemctl --user enable "$UNIT_NAME" >/dev/null
systemctl --user restart "$UNIT_NAME"
echo "== installed and started ${UNIT_DIR}/${UNIT_NAME}"
}
# Report what actually happened, not that the commands returned 0.
verify() {
local port="$1"
if ! systemctl --user is-active --quiet "$UNIT_NAME"; then
echo "Error: ${UNIT_NAME} is not active." >&2
systemctl --user --no-pager --lines=20 status "$UNIT_NAME" >&2 || true
return 1
fi
signal_wait_for_url "http://127.0.0.1:${port}/healthz" "signal-bot" 60
echo "== active, and answering on http://127.0.0.1:${port}/healthz"
echo "== enabled at boot: $(systemctl --user is-enabled "$UNIT_NAME")"
}
main() {
validate_requirements
local port
port="$(bot_port)"
if [[ "$VERIFY_ONLY" != "true" ]]; then
require_linger
install_unit
fi
verify "$port"
echo "== logs: journalctl --user -u ${UNIT_NAME} -f"
}
while [[ $# -gt 0 ]]; do
case $1 in
--verify-only)
VERIFY_ONLY="true"
shift
;;
-h | --help) usage ;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
main "$@"