cosmo-pay/install.sh

32 lines
2.2 KiB
Bash

true
# Cosmo Pay install on the bot server. Expects app.py and cosmo-pay.service in /tmp/cosmo-pay-src/.
set -e
export DEBIAN_FRONTEND=noninteractive
S=/tmp/cosmo-pay-src
[ -f $S/app.py ] && [ -f $S/cosmo-pay.service ] || { echo "sources missing"; exit 1; }
[ -r /etc/cosmo-pay.env ] || { echo "/etc/cosmo-pay.env missing"; exit 1; }
mkdir -p /opt/cosmo-pay /var/lib/cosmo-pay; chmod 700 /var/lib/cosmo-pay
[ -d /opt/cosmo-pay/venv ] || python3 -m venv /opt/cosmo-pay/venv
/opt/cosmo-pay/venv/bin/pip install -q --upgrade pip >/dev/null
/opt/cosmo-pay/venv/bin/pip install -q "fastapi>=0.110" "uvicorn>=0.29" "httpx>=0.27" "pywebpush>=2.0" >/dev/null
/opt/cosmo-pay/venv/bin/python -c "import fastapi, uvicorn, httpx, pywebpush; print('deps: fastapi', fastapi.__version__, '| uvicorn', uvicorn.__version__, '| httpx', httpx.__version__, '| pywebpush ok')"
# Web Push (PWA) VAPID key: generated once on the server, never printed.
if [ ! -s /etc/cosmo-pay-vapid.pem ]; then
(umask 077; /opt/cosmo-pay/venv/bin/python -c "
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
k = ec.generate_private_key(ec.SECP256R1())
open('/etc/cosmo-pay-vapid.pem', 'wb').write(k.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption()))")
echo "vapid key created"
fi
# PWA static files
if [ -d $S/web ]; then rm -rf /opt/cosmo-pay/web.new; cp -r $S/web /opt/cosmo-pay/web.new; rm -rf /opt/cosmo-pay/web; mv /opt/cosmo-pay/web.new /opt/cosmo-pay/web; echo "web: $(find /opt/cosmo-pay/web -type f | wc -l) files"; fi
[ -f /opt/cosmo-pay/app.py ] && cp -a /opt/cosmo-pay/app.py /opt/cosmo-pay/app.py.bak.$(date +%F-%H%M)
install -m 600 $S/app.py /opt/cosmo-pay/app.py
install -m 644 $S/cosmo-pay.service /etc/systemd/system/cosmo-pay.service
/opt/cosmo-pay/venv/bin/python -m py_compile /opt/cosmo-pay/app.py && echo "app compiles"
systemctl daemon-reload; systemctl enable cosmo-pay >/dev/null 2>&1; systemctl restart cosmo-pay; sleep 4
echo "service: $(systemctl is-active cosmo-pay)"; journalctl -u cosmo-pay --since -30s --no-pager | tail -5 | cut -c1-200
echo "local health: $(curl -s -m 5 http://127.0.0.1:3010/health)"
echo "listen: $(ss -tlnH '( sport = :3010 )' | awk '{print $4}')"
rm -rf $S