Files
wekan/snap-src/bin/database-ready
T
Lauri Ojansivu f927cb3c50 Snap: name an AppArmor-refused database connection at once, and stop the snapctl get errors.
The logs in #6746 show the real cause of "write: permission denied" from
database-restore and of WeKan waiting forever for FerretDB: 1300 kernel
lines of apparmor="DENIED" operation="file_perm" class="net" for
snap.wekan.wekan on 127.0.0.1:27019, with the same denial for snap-store
and standard-notes on that computer. FerretDB was running and accepting
connections; the kernel refused the first write. That is snapd 2.77.1 on a
kernel with an AppArmor socket mediation bug (Launchpad snapd bug 2169038),
fixed by a newer kernel and worked around by `snap revert snapd`.

WeKan cannot lift that denial, so it now says what it is. A new
bin/socket-denied recognises the refusal and prints the denial to look for,
the upstream bug and both ways out. wekan-control's FerretDB and MongoDB
wait loops keep each attempt's error and print it on the first refused
attempt instead of "not ready yet" for two minutes; database-backup and
database-restore stop at once instead of starting an already running
service and waiting 90 seconds.

The same log began with "error: snapctl: get which option?" four times:
SAML_IDP_PROFILE, SAML_WANT_RESPONSE_SIGNED, SAML_WANT_ASSERTIONS_SIGNED
and SAML_LOGIN_FLOW were listed in bin/config without a KEY_ name, so
wekan-read-settings ran a bare `snapctl get` for them and `snap set` could
never reach them. They now have their snap keys, the CORS_EXPOSE_HEADERS
description has its right name, and a key without a snap name falls back
to its default instead of calling snapctl.

Tests: snapSocketDenied, snapSettingsKeys, and extended snapDatabaseRestore
and ferretdbWaitTimeout.

Related to #6746: the reporter still has to update the kernel or revert
snapd, so the issue stays open until that is confirmed.

Thanks to fabiosalles and xet7 !
2026-10-07 15:56:19 +03:00

68 lines
3.0 KiB
Bash

#!/bin/bash
# database-ready — make sure the database WeKan's data is in answers, for the
# commands an admin runs by hand (database-backup, database-restore).
#
# #6746: those commands talked to whatever was on $MONGODB_PORT and nothing
# else. After `snap stop wekan` - which the restore steps people follow tell them
# to do - nothing is, and on a FerretDB snap the database is a separate service
# (wekan.ferretdb) that `snap start wekan.wekan` alone does not bring back. The
# tools then failed after a 30-second server-selection timeout with a driver
# topology dump that names neither the service nor where its log is.
#
# So: ask bin/database-role which database holds the data, start THAT service if
# nothing answers, wait for it, and when it still does not answer, stop with the
# driver's own error and the commands that show the reason.
#
# Source it, then: database_ready HOST PORT (returns 0 when it answers)
database_ready() {
local host="$1" port="$2" url role svc waited err timeout
case "$host" in *:*) host="[$host]" ;; esac
url="mongodb://${host}:${port}/?directConnection=true"
# Older layouts and the tests' stand-in snap have no db-eval: leave the
# connection to the tool itself, as before.
[ -e "$SNAP/bin/db-eval" ] || return 0
[ -f "$SNAP/bin/socket-denied" ] && source "$SNAP/bin/socket-denied"
err="$(bash "$SNAP/bin/db-eval" ping "$url" 2>&1 >/dev/null)" && return 0
# #6746: the kernel refused the connection, not the database. Starting the
# service changes nothing (it is already running - the connect succeeded)
# and waiting only delays the one message that helps, so say it now.
if declare -F socket_denied >/dev/null && socket_denied "$err"; then
echo "" >&2
echo "Cannot talk to the database on ${host}:${port}:" >&2
echo " ${err}" >&2
socket_denied_hint >&2
return 1
fi
role="$(bash "$SNAP/bin/database-role" 2>/dev/null)"
case "$role" in mongodb|ferretdb) ;; *) role=ferretdb ;; esac
svc="${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}.${role}"
echo "No database answers on ${host}:${port}; starting ${svc}, which holds the data ..."
if ! snapctl start --enable "$svc" >/dev/null 2>&1; then
echo "Could not start ${svc} from here (run this command with sudo, or: sudo snap start ${svc})." >&2
fi
timeout="${WEKAN_DB_READY_TIMEOUT:-90}"
waited=0
until err="$(bash "$SNAP/bin/db-eval" ping "$url" 2>&1 >/dev/null)"; do
if [ "$waited" -ge "$timeout" ]; then
echo "" >&2
echo "The database (${svc}) does not answer on ${host}:${port} after ${timeout} seconds." >&2
[ -n "$err" ] && echo " ${err}" >&2
if declare -F socket_denied >/dev/null && socket_denied "$err"; then
socket_denied_hint >&2
fi
echo "The reason is in its log:" >&2
echo " sudo snap logs -n 100 ${svc}" >&2
echo " sudo snap services ${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}" >&2
return 1
fi
sleep 2
waited=$((waited + 2))
done
echo "${svc} answers on ${host}:${port}."
return 0
}