#!/bin/bash # Deploy routine-server (server/) to a machine you can ssh into, and (re)start it in a screen session. # # scripts/deploy-server.sh HOST copy the code and restart the server # scripts/deploy-server.sh HOST --stop stop it # scripts/deploy-server.sh HOST --token print the bearer token for the apps' Settings → Sync # # HOST is an ssh destination (user@host, or an alias from ~/.ssh/config); DEPLOY_HOST works too. # The rest comes from the environment: # DEPLOY_DIR folder on HOST, relative to its home or absolute (default routine-server) # ROUTINE_SERVER_HOST address the server listens on (default 127.0.0.1: only HOST itself; # use an address your devices reach privately, e.g. a VPN one) # ROUTINE_SERVER_PORT port (default 8070) # It needs ssh, rsync, and on HOST python3, screen and curl. To start it at boot, have HOST run # DEPLOY_DIR/app/run.sh (it reads the settings this script leaves in DEPLOY_DIR/app/server.env). set -euo pipefail HOST=${DEPLOY_HOST:-} ACTION=deploy for a in "$@"; do case "$a" in --stop) ACTION=stop ;; --token) ACTION=token ;; -h|--help) sed -n '2,16p' "$0"; exit 0 ;; *) HOST=$a ;; esac done [ -n "$HOST" ] || { echo "usage: $0 HOST [--stop|--token] (or set DEPLOY_HOST)" >&2; exit 2; } DIR=${DEPLOY_DIR:-routine-server} BIND=${ROUTINE_SERVER_HOST:-127.0.0.1} PORT=${ROUTINE_SERVER_PORT:-8070} NAME=routine-server cd "$(dirname "$0")/.." case "$ACTION" in token) ssh "$HOST" "cat '$DIR/data/token'"; echo; exit 0 ;; stop) ssh "$HOST" "screen -S $NAME -X quit || true"; echo "stopped"; exit 0 ;; esac ssh "$HOST" "mkdir -p '$DIR/app' '$DIR/data' && chmod 700 '$DIR/data'" rsync -a server/routine_server.py server/run.sh routine/model.py "$HOST:$DIR/app/" printf 'ROUTINE_SERVER_HOST=%q\nROUTINE_SERVER_PORT=%q\n' "$BIND" "$PORT" | ssh "$HOST" "cat > '$DIR/app/server.env'" CHECK=$BIND [ "$CHECK" = "0.0.0.0" ] && CHECK=127.0.0.1 ssh "$HOST" bash -s </dev/null || true sleep 1 screen -dmS $NAME bash -c "bash '\$PWD/app/run.sh' 2>&1 | tee '\$PWD/server.log'" sleep 2 for pid in \$(pgrep -f routine_server.py); do echo 1000 > /proc/\$pid/oom_score_adj 2>/dev/null || true; done # first to go if memory runs out curl -s -m 5 http://$CHECK:$PORT/health; echo REMOTE