#!/bin/sh # Run the manager and the Vite development server on this workstation. # Usage: ./dev.sh [PostgreSQL connection string] # The router's address comes from the outputs of the platform's root module, module_root of that # config.json, relative to its directory, read with its iac_tool; the sessions are kept in the database of the connection # string, or without one in PGDATABASE (else the login name) with libpq's PG* variables for the # rest. The manager reads the generated dev.json, whose bind is 127.0.0.1, and listens on 127.0.0.1:7080; its Python packages # are installed in .venv. The application is served at http://localhost:5180 and passes /api to the # manager. set -e if [ $# -lt 1 ] || [ $# -gt 2 ]; then printf 'Usage: %s [PostgreSQL connection string]\n' "$0" >&2 exit 2 fi DATABASE="${2:-dbname=${PGDATABASE:-$(id -un)}}" if [ ! -f "$1" ]; then printf 'ERROR: %s does not exist.\n' "$1" >&2 exit 2 fi ROOT="$(cd "$(dirname "$0")" && pwd)" SETTINGS="$ROOT/dev.json" PLATFORM_CONFIG="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" MODULE_ROOT="$(python3 - "$PLATFORM_CONFIG" <<'EOF' import json, os, sys path = sys.argv[1] root = json.load(open(path, encoding='utf-8')).get('module_root') if not isinstance(root, str) or not root.strip() or os.path.isabs(root): sys.exit(f'module_root in {path} must be a directory relative to {os.path.dirname(path)}') print(os.path.normpath(os.path.join(os.path.dirname(path), root))) EOF )" # The tool that reads the outputs is the platform's, iac_tool of its config.json: tofu or terraform. IAC_TOOL="$(python3 -c 'import json, sys; print(json.load(open(sys.argv[1], encoding="utf-8")).get("iac_tool") or "terraform")' "$PLATFORM_CONFIG")" if [ ! -d "$MODULE_ROOT" ]; then printf 'ERROR: module_root %s is not a directory.\n' "$MODULE_ROOT" >&2 exit 2 fi python3 - "$SETTINGS" "$MODULE_ROOT" "$DATABASE" "$IAC_TOOL" <<'EOF' import json, sys with open(sys.argv[1], 'w', encoding='utf-8') as handle: json.dump({'terraform_dir': sys.argv[2], 'database': sys.argv[3], 'iac_tool': sys.argv[4], 'bind': '127.0.0.1'}, handle) handle.write('\n') EOF if [ ! -x "$ROOT/.venv/bin/python" ]; then python3 -m venv "$ROOT/.venv" fi if ! "$ROOT/.venv/bin/python" -c 'import psycopg, requests' 2>/dev/null; then "$ROOT/.venv/bin/pip" install --quiet 'psycopg[binary]' requests fi cd "$ROOT/web" if [ ! -d node_modules ] || [ package-lock.json -nt node_modules ]; then npm install fi "$ROOT/.venv/bin/python" "$ROOT/debian-package/src/usr/lib/vpn-router/manager" --config "$SETTINGS" & MANAGER=$! trap 'kill $MANAGER 2>/dev/null' EXIT INT TERM echo "Platform module: $MODULE_ROOT" VITE_MANAGER=http://127.0.0.1:7080 npm run dev