forked from Blendman974/faceclaw
Add stats collection and first-run on-glasses onboarding app
This commit is contained in:
1 parent
84baee9c76
commit
406cefe0c5
73 files changed
+3906
-98
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# Template: setup-server.sh fills in @USER@ and @DIR@ and installs it as
|
||||
# /etc/systemd/system/faceclaw-stats.service.
|
||||
[Unit]
|
||||
Description=Faceclaw stats server (stats.faceclaw.org)
|
||||
After=network.target postgresql.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=@USER@
|
||||
WorkingDirectory=@DIR@
|
||||
EnvironmentFile=@DIR@/deploy/stats.env
|
||||
ExecStart=@DIR@/deploy/run.sh
|
||||
# deploy.sh restarts the server by ending the process.
|
||||
Restart=always
|
||||
RestartSec=2
|
||||
NoNewPrivileges=yes
|
||||
PrivateTmp=yes
|
||||
ProtectSystem=full
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,33 @@
|
||||
# stats.faceclaw.org, installed by setup-server.sh as
|
||||
# /etc/nginx/sites-available/stats.faceclaw.org. certbot then adds the TLS
|
||||
# server block and the HTTP-to-HTTPS redirect to the installed copy, so
|
||||
# setup-server.sh never overwrites it; make later changes on the server too.
|
||||
|
||||
limit_req_zone $binary_remote_addr zone=faceclaw_stats:1m rate=10r/m;
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name stats.faceclaw.org;
|
||||
|
||||
# Reports are anonymous: don't keep the IP addresses they come from.
|
||||
access_log off;
|
||||
limit_req_log_level notice;
|
||||
client_max_body_size 64k;
|
||||
|
||||
location = /v1/report {
|
||||
limit_req zone=faceclaw_stats burst=20 nodelay;
|
||||
limit_req_status 429;
|
||||
proxy_pass http://127.0.0.1:8290;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /healthz {
|
||||
proxy_pass http://127.0.0.1:8290;
|
||||
}
|
||||
|
||||
location / {
|
||||
default_type text/plain;
|
||||
return 200 "Faceclaw's opt-in usage statistics endpoint. See https://faceclaw.org/privacy.html\n";
|
||||
}
|
||||
}
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
# Started by faceclaw-stats.service, with deploy/stats.env in the environment.
|
||||
# Node comes from nvm, as for the other services on this host.
|
||||
cd "$(dirname "$0")/.."
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
. "$NVM_DIR/nvm.sh"
|
||||
nvm use 22 >/dev/null
|
||||
# The absolute path lets deploy.sh find this process to restart it.
|
||||
exec node "$PWD/src/server.ts"
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
# One-time root setup for the stats server on the VPS; safe to rerun. Run it
|
||||
# via sudo, as the user that owns the deployed copy (deploy.sh puts it there):
|
||||
#
|
||||
# ssh -t cumulus 'sudo bash ~/faceclaw-stats/deploy/setup-server.sh'
|
||||
#
|
||||
# It creates a Postgres role (named after that user, so the service and
|
||||
# psql connect by peer auth) and the database on the cluster in stats.env,
|
||||
# installs and starts the systemd service, adds the nginx site, and gets the
|
||||
# site's Let's Encrypt certificate with certbot.
|
||||
set -euo pipefail
|
||||
[ "$(id -u)" = 0 ] || { echo "Run this with sudo." >&2; exit 1; }
|
||||
DEPLOY_USER="${SUDO_USER:?Run this via sudo from the user that owns the deployed copy.}"
|
||||
DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
DOMAIN=stats.faceclaw.org
|
||||
set -a; . "$DIR/deploy/stats.env"; set +a
|
||||
cd /
|
||||
|
||||
echo "== Postgres: role $DEPLOY_USER, database $PGDATABASE (port $PGPORT)"
|
||||
pg_lsclusters --no-header | awk -v port="$PGPORT" '$3 == port && $4 == "online" { found = 1 } END { exit !found }' \
|
||||
|| { echo "No online Postgres cluster on port $PGPORT." >&2; exit 1; }
|
||||
sudo -u postgres psql -p "$PGPORT" -v ON_ERROR_STOP=1 -q -c \
|
||||
"do \$\$ begin if not exists (select from pg_roles where rolname = '$DEPLOY_USER') then create role \"$DEPLOY_USER\" login; end if; end \$\$;"
|
||||
if ! sudo -u postgres psql -p "$PGPORT" -Atc "select 1 from pg_database where datname = '$PGDATABASE'" | grep -q 1; then
|
||||
sudo -u postgres createdb -p "$PGPORT" -O "$DEPLOY_USER" "$PGDATABASE"
|
||||
fi
|
||||
sudo -u "$DEPLOY_USER" psql -h "$PGHOST" -p "$PGPORT" -d "$PGDATABASE" -v ON_ERROR_STOP=1 -q -f "$DIR/schema.sql"
|
||||
|
||||
echo "== systemd: faceclaw-stats.service"
|
||||
[ -d "$DIR/node_modules" ] || { echo "No node_modules in $DIR; run deploy.sh first." >&2; exit 1; }
|
||||
sed -e "s|@USER@|$DEPLOY_USER|g" -e "s|@DIR@|$DIR|g" "$DIR/deploy/faceclaw-stats.service" \
|
||||
> /etc/systemd/system/faceclaw-stats.service
|
||||
systemctl daemon-reload
|
||||
systemctl enable faceclaw-stats.service
|
||||
systemctl restart faceclaw-stats.service
|
||||
|
||||
echo "== nginx: $DOMAIN"
|
||||
SITE="/etc/nginx/sites-available/$DOMAIN"
|
||||
# certbot edits the installed copy, so only install it the first time.
|
||||
[ -e "$SITE" ] || cp "$DIR/deploy/nginx-site.conf" "$SITE"
|
||||
ln -sf "$SITE" "/etc/nginx/sites-enabled/$DOMAIN"
|
||||
if ! nginx -t; then
|
||||
# Don't leave a broken site enabled: it would block reloads for every site.
|
||||
rm -f "/etc/nginx/sites-enabled/$DOMAIN"
|
||||
echo "nginx rejected the site config; it has been disabled again." >&2
|
||||
exit 1
|
||||
fi
|
||||
systemctl reload nginx
|
||||
|
||||
echo "== TLS certificate"
|
||||
if [ ! -d "/etc/letsencrypt/live/$DOMAIN" ]; then
|
||||
certbot --nginx -d "$DOMAIN" --non-interactive --redirect
|
||||
fi
|
||||
|
||||
echo "== Checking https://$DOMAIN/healthz"
|
||||
sleep 2
|
||||
curl -fsS "https://$DOMAIN/healthz" && echo
|
||||
@@ -0,0 +1,7 @@
|
||||
# Server settings, read by the systemd unit (EnvironmentFile) and sourced by
|
||||
# setup-server.sh and deploy.sh. The database is on the Postgres 16 cluster
|
||||
# (port 5433); the server's older 14 cluster on 5432 is left alone.
|
||||
PORT=8290
|
||||
PGHOST=/var/run/postgresql
|
||||
PGPORT=5433
|
||||
PGDATABASE=faceclaw_stats
|
||||
Reference in new issue
Block a user