Improve setup: verification and DB helpers

Refactor ente-install.sh to make first-time setup more robust: update ente-get-verification to use a new log pattern and broader fallback, add run_psql and run_psql_exec helpers for consistent sudo-psql usage, implement a retry loop when polling for the verification code, and improve prompts/error messages. Switch admin whitelisting to use the numeric user_id (and verify existence) instead of email in museum.yaml. Simplify and harden subscription upgrade logic to use the new DB helpers and ensure inserts/updates run under the postgres user. Miscellaneous messaging and minor formatting tweaks.
This commit is contained in:
CanbiZ (MickLesk)
2026-04-13 14:51:01 +02:00
parent 8dff6a908c
commit c5bbc956a1

View File

@@ -337,10 +337,10 @@ msg_info "Creating helper scripts"
cat <<'EOF' >/usr/local/bin/ente-get-verification cat <<'EOF' >/usr/local/bin/ente-get-verification
#!/usr/bin/env bash #!/usr/bin/env bash
echo "Searching for verification codes in museum logs..." echo "Searching for verification codes in museum logs..."
journalctl -u ente-museum --no-pager | grep -oP 'SendEmailOTT.*ott:\s*\K\d+' | tail -5 journalctl -u ente-museum --no-pager | grep -oP 'Verification code: \K\d+' | tail -5
if [[ $? -ne 0 ]] || [[ -z "$(journalctl -u ente-museum --no-pager | grep -oP 'ott:\s*\K\d+' | tail -1)" ]]; then if [[ -z "$(journalctl -u ente-museum --no-pager | grep -oP 'Verification code: \K\d+' | tail -1)" ]]; then
echo "No codes found via ott pattern. Showing recent relevant logs:" echo "No codes found. Showing recent relevant logs:"
journalctl -u ente-museum --no-pager -n 50 | grep -i "verification\|verify\|code\|ott" | tail -20 journalctl -u ente-museum --no-pager -n 50 | grep -iE "verification|ott|code|Skipping sending" | tail -20
fi fi
EOF EOF
chmod +x /usr/local/bin/ente-get-verification chmod +x /usr/local/bin/ente-get-verification
@@ -350,6 +350,14 @@ cat <<'SETUPEOF' >/usr/local/bin/ente-setup
LOCAL_IP=$(hostname -I | awk '{print $1}') LOCAL_IP=$(hostname -I | awk '{print $1}')
DB_NAME="ente_db" DB_NAME="ente_db"
run_psql() {
sudo -u postgres psql -t -d "$DB_NAME" -c "$1" 2>/dev/null | xargs
}
run_psql_exec() {
sudo -u postgres psql -d "$DB_NAME" -c "$1" 2>/dev/null
}
echo "=== Ente First-Time Setup ===" echo "=== Ente First-Time Setup ==="
echo "" echo ""
read -r -p "Enter your account email: " EMAIL read -r -p "Enter your account email: " EMAIL
@@ -364,55 +372,76 @@ echo "Step 1/4: Register your account"
echo " Open the web UI: http://${LOCAL_IP}:3000" echo " Open the web UI: http://${LOCAL_IP}:3000"
echo " Create an account with: ${EMAIL}" echo " Create an account with: ${EMAIL}"
echo "" echo ""
echo " Make sure you click 'Don't have an account?' and complete the signup form."
echo ""
read -r -p "Press ENTER after you submitted the signup form..." read -r -p "Press ENTER after you submitted the signup form..."
echo "" echo ""
echo "Step 2/4: Getting verification code from logs..." echo "Step 2/4: Getting verification code from logs..."
CODE=""
for i in 1 2 3; do
sleep 3 sleep 3
CODE=$(journalctl -u ente-museum --no-pager -n 100 | grep -oP 'ott:\s*\K\d+' | tail -1) CODE=$(journalctl -u ente-museum --no-pager -n 200 | grep -oP 'Verification code: \K\d+' | tail -1)
if [[ -n "$CODE" ]]; then if [[ -n "$CODE" ]]; then
break
fi
echo " Attempt ${i}/3: Code not found yet, waiting..."
done
if [[ -n "$CODE" ]]; then
echo ""
echo " Your verification code: ${CODE}" echo " Your verification code: ${CODE}"
echo " Enter this code in the web UI to complete registration." echo " Enter this code in the web UI to complete registration."
else else
echo " Could not find code automatically. Check manually:" echo ""
echo " journalctl -u ente-museum --no-pager | grep -i ott" echo " Could not find the verification code automatically."
echo " This usually means the signup form was not submitted yet."
echo ""
echo " Are you sure you entered '${EMAIL}' and clicked 'Create account'?"
echo " You can check manually with: ente-get-verification"
fi fi
echo "" echo ""
read -r -p "Press ENTER after you verified the code..." read -r -p "Press ENTER after you verified the code in the web UI..."
USER_ID=$(su -c "psql -t -d ${DB_NAME} -c \"SELECT user_id FROM users WHERE email = '$(echo "$EMAIL" | sed "s/'/''/g')';\"" postgres 2>/dev/null | xargs)
echo "Found user ID: ${USER_ID}"
echo "" echo ""
echo "Step 3/4: Whitelisting admin in museum.yaml..." echo "Step 3/4: Looking up user and whitelisting admin..."
USER_ID=$(run_psql "SELECT user_id FROM users WHERE email = '${EMAIL//\'/\'\'}';")
if [[ -z "$USER_ID" ]]; then
echo " Warning: User '${EMAIL}' not found in database."
echo " Make sure registration was completed successfully."
echo ""
echo "=== Setup incomplete ==="
echo "After completing registration, run ente-setup again."
exit 1
fi
echo " Found user ID: ${USER_ID}"
if grep -q "internal:" /opt/ente/server/museum.yaml; then if grep -q "internal:" /opt/ente/server/museum.yaml; then
if ! grep -qF "$EMAIL" /opt/ente/server/museum.yaml; then if ! grep -qF "${USER_ID}" /opt/ente/server/museum.yaml; then
sed -i "/admins:/a\\ - ${EMAIL}" /opt/ente/server/museum.yaml sed -i "/admins:/a\\ - ${USER_ID}" /opt/ente/server/museum.yaml
fi fi
else else
cat <<ADMEOF >>/opt/ente/server/museum.yaml cat <<ADMEOF >>/opt/ente/server/museum.yaml
internal: internal:
admins: admins:
- ${EMAIL} - ${USER_ID}
ADMEOF ADMEOF
fi fi
systemctl restart ente-museum systemctl restart ente-museum
sleep 2 sleep 2
echo "Done." echo " Admin whitelisted."
echo "" echo ""
echo "Step 4/4: Upgrading subscription..." echo "Step 4/4: Upgrading subscription..."
if [[ -n "$USER_ID" ]]; then ROWS=$(run_psql "SELECT count(*) FROM subscriptions WHERE user_id = ${USER_ID};")
su -c "psql -d ${DB_NAME} -c \"UPDATE subscriptions SET storage_in_mbs_per_plan = 10737418240, expiry_time = 2524608000000000 WHERE user_id = ${USER_ID};\"" postgres 2>/dev/null
ROWS=$(su -c "psql -t -d ${DB_NAME} -c \"SELECT count(*) FROM subscriptions WHERE user_id = ${USER_ID};\"" postgres 2>/dev/null | xargs)
if [[ "$ROWS" == "0" ]]; then if [[ "$ROWS" == "0" ]]; then
su -c "psql -d ${DB_NAME} -c \"INSERT INTO subscriptions (user_id, storage_in_mbs_per_plan, expiry_time, product_id, payment_provider, transaction_id, original_transaction_id) VALUES (${USER_ID}, 10737418240, 2524608000000000, 'self_hosted_unlimited', 'admin', 'admin_setup', 'admin_setup');\"" postgres 2>/dev/null run_psql_exec "INSERT INTO subscriptions (user_id, storage_in_mbs_per_plan, expiry_time, product_id, payment_provider, transaction_id, original_transaction_id) VALUES (${USER_ID}, 10737418240, 2524608000000000, 'self_hosted_unlimited', 'admin', 'admin_setup', 'admin_setup');"
else
run_psql_exec "UPDATE subscriptions SET storage_in_mbs_per_plan = 10737418240, expiry_time = 2524608000000000 WHERE user_id = ${USER_ID};"
fi fi
echo " Subscription upgraded to unlimited storage." echo " Subscription upgraded to unlimited storage."
else
echo "Warning: Could not find user ID. Try running: ente-upgrade-subscription ${EMAIL}"
fi
echo "" echo ""
echo "=== Setup complete ===" echo "=== Setup complete ==="
@@ -430,15 +459,17 @@ fi
EMAIL="$1" EMAIL="$1"
DB_NAME="ente_db" DB_NAME="ente_db"
echo "Upgrading subscription for: $EMAIL" echo "Upgrading subscription for: $EMAIL"
USER_ID=$(su -c "psql -t -d ${DB_NAME} -c \"SELECT user_id FROM users WHERE email = '$(echo "$EMAIL" | sed "s/'/''/g')';\"" postgres 2>/dev/null | xargs) USER_ID=$(sudo -u postgres psql -t -d "$DB_NAME" -c "SELECT user_id FROM users WHERE email = '${EMAIL//\'/\'\'}';")
USER_ID=$(echo "$USER_ID" | xargs)
if [[ -z "$USER_ID" ]]; then if [[ -z "$USER_ID" ]]; then
echo "Error: User not found in database." echo "Error: User not found in database."
exit 1 exit 1
fi fi
su -c "psql -d ${DB_NAME} -c \"UPDATE subscriptions SET storage_in_mbs_per_plan = 10737418240, expiry_time = 2524608000000000 WHERE user_id = ${USER_ID};\"" postgres 2>/dev/null ROWS=$(sudo -u postgres psql -t -d "$DB_NAME" -c "SELECT count(*) FROM subscriptions WHERE user_id = ${USER_ID};" | xargs)
ROWS=$(su -c "psql -t -d ${DB_NAME} -c \"SELECT count(*) FROM subscriptions WHERE user_id = ${USER_ID};\"" postgres 2>/dev/null | xargs)
if [[ "$ROWS" == "0" ]]; then if [[ "$ROWS" == "0" ]]; then
su -c "psql -d ${DB_NAME} -c \"INSERT INTO subscriptions (user_id, storage_in_mbs_per_plan, expiry_time, product_id, payment_provider, transaction_id, original_transaction_id) VALUES (${USER_ID}, 10737418240, 2524608000000000, 'self_hosted_unlimited', 'admin', 'admin_setup', 'admin_setup');\"" postgres 2>/dev/null sudo -u postgres psql -d "$DB_NAME" -c "INSERT INTO subscriptions (user_id, storage_in_mbs_per_plan, expiry_time, product_id, payment_provider, transaction_id, original_transaction_id) VALUES (${USER_ID}, 10737418240, 2524608000000000, 'self_hosted_unlimited', 'admin', 'admin_setup', 'admin_setup');"
else
sudo -u postgres psql -d "$DB_NAME" -c "UPDATE subscriptions SET storage_in_mbs_per_plan = 10737418240, expiry_time = 2524608000000000 WHERE user_id = ${USER_ID};"
fi fi
echo "Done. Subscription upgraded to unlimited storage for: $EMAIL" echo "Done. Subscription upgraded to unlimited storage for: $EMAIL"
EOF EOF