askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
install.sh · 6.3 KB · 173 lines
| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # DBBASIC Object Server — single-VM installer for Debian/Ubuntu. |
| 4 | # |
| 5 | # What it does (idempotent — safe to re-run): |
| 6 | # - installs python3, venv, pip, git |
| 7 | # - creates the `dbbasic` service user and the standard directory layout |
| 8 | # - clones (or reuses) the server under /opt/dbbasic-object-server |
| 9 | # - builds a virtualenv and installs the server |
| 10 | # - writes /etc/dbbasic-object-server.env with a freshly generated admin |
| 11 | # token (only if the file does not already exist — never overwrites yours) |
| 12 | # - installs and starts a systemd unit bound to 127.0.0.1:8001 |
| 13 | # - runs the filesystem checker and a health check |
| 14 | # |
| 15 | # What it deliberately does NOT do (these are short, deliberate follow-ups |
| 16 | # printed at the end, and covered in docs/quickstart.md): |
| 17 | # - create your admin user / turn on password login |
| 18 | # - enable source writes, file writes, or package installs |
| 19 | # - configure a public domain / TLS proxy |
| 20 | # |
| 21 | # Read it before you run it. Then: sudo bash scripts/install.sh |
| 22 | # |
| 23 | set -euo pipefail |
| 24 | |
| 25 | REPO_URL="${DBBASIC_REPO_URL:-https://github.com/askrobots/dbbasic-object-server.git}" |
| 26 | CODE_DIR="${DBBASIC_CODE_DIR:-/opt/dbbasic-object-server}" |
| 27 | STATE_DIR="${DBBASIC_STATE_DIR:-/var/lib/dbbasic-object-server}" |
| 28 | ENV_FILE="${DBBASIC_ENV_FILE:-/etc/dbbasic-object-server.env}" |
| 29 | UNIT_FILE="/etc/systemd/system/dbbasic-object-server.service" |
| 30 | SERVICE_USER="dbbasic" |
| 31 | |
| 32 | say() { printf '\033[1;36m==>\033[0m %s\n' "$*"; } |
| 33 | warn() { printf '\033[1;33m warn:\033[0m %s\n' "$*"; } |
| 34 | |
| 35 | if [[ "${EUID}" -ne 0 ]]; then |
| 36 | echo "Run as root: sudo bash scripts/install.sh" >&2 |
| 37 | exit 1 |
| 38 | fi |
| 39 | |
| 40 | say "Installing system packages" |
| 41 | apt-get update -qq |
| 42 | apt-get install -y -qq python3 python3-venv python3-pip git |
| 43 | |
| 44 | say "Creating service user and directory layout" |
| 45 | if ! id "${SERVICE_USER}" >/dev/null 2>&1; then |
| 46 | useradd --system --home "${STATE_DIR}" --shell /usr/sbin/nologin "${SERVICE_USER}" |
| 47 | fi |
| 48 | mkdir -p "${CODE_DIR}" "${STATE_DIR}/objects" "${STATE_DIR}/data" |
| 49 | chown -R "${SERVICE_USER}:${SERVICE_USER}" "${CODE_DIR}" "${STATE_DIR}" |
| 50 | chmod 755 "${CODE_DIR}" |
| 51 | chmod 750 "${STATE_DIR}" "${STATE_DIR}/objects" "${STATE_DIR}/data" |
| 52 | |
| 53 | say "Fetching server code into ${CODE_DIR}" |
| 54 | if [[ -d "${CODE_DIR}/.git" ]]; then |
| 55 | sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" pull --ff-only |
| 56 | else |
| 57 | sudo -u "${SERVICE_USER}" git clone "${REPO_URL}" "${CODE_DIR}" |
| 58 | fi |
| 59 | |
| 60 | say "Building virtualenv and installing the server" |
| 61 | if [[ ! -x "${CODE_DIR}/.venv/bin/python" ]]; then |
| 62 | sudo -u "${SERVICE_USER}" python3 -m venv "${CODE_DIR}/.venv" |
| 63 | fi |
| 64 | sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q --upgrade pip |
| 65 | sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q -e "${CODE_DIR}[server]" |
| 66 | |
| 67 | if [[ -f "${ENV_FILE}" ]]; then |
| 68 | warn "${ENV_FILE} already exists — keeping it (admin token unchanged)" |
| 69 | else |
| 70 | say "Writing ${ENV_FILE} with a freshly generated admin token" |
| 71 | ADMIN_TOKEN="$("${CODE_DIR}/.venv/bin/python" -c 'import secrets; print(secrets.token_urlsafe(32))')" |
| 72 | cat > "${ENV_FILE}" <<EOF |
| 73 | # Generated by scripts/install.sh. Edit deliberately; restart the service after changes. |
| 74 | DBBASIC_OBJECTS_DIR=${STATE_DIR}/objects |
| 75 | DBBASIC_DATA_DIR=${STATE_DIR}/data |
| 76 | DBBASIC_BACKUPS_DIR=${STATE_DIR}/data/backups |
| 77 | DBBASIC_PACKAGES_DIR=${CODE_DIR}/packages |
| 78 | DBBASIC_ADMIN_TOKEN=${ADMIN_TOKEN} |
| 79 | |
| 80 | # First-boot posture: writes and public code execution stay closed until you |
| 81 | # have login, backups, and proxy access confirmed. Turn these on deliberately |
| 82 | # (see docs/quickstart.md and docs/single-vm-deployment.md). |
| 83 | DBBASIC_ENABLE_SOURCE_WRITES=false |
| 84 | DBBASIC_ENABLE_FILE_WRITES=false |
| 85 | DBBASIC_ENABLE_PACKAGE_INSTALLS=false |
| 86 | DBBASIC_ENABLE_PACKAGE_RESTORE=false |
| 87 | DBBASIC_ENABLE_PASSWORD_LOGIN=false |
| 88 | DBBASIC_COOKIE_SECURE=true |
| 89 | |
| 90 | # Clean page URLs (/notes, /tasks) resolved to objects and seeded view |
| 91 | # records. Without this every app page 404s before routes are even |
| 92 | # consulted -- the flag exists for API-only deployments, but the app |
| 93 | # suite is the product, so it ships ON (found via the e2e browser lane, |
| 94 | # whose fixture was the only place all the required flags appeared |
| 95 | # together). |
| 96 | DBBASIC_ENABLE_SITE_ROUTES=true |
| 97 | |
| 98 | # Permissions start in the open; move to audit, then enforcement, once a |
| 99 | # policy and login are in place (docs/permissions-model.md). |
| 100 | DBBASIC_ENABLE_PERMISSION_AUDIT=false |
| 101 | DBBASIC_ENABLE_PERMISSION_ENFORCEMENT=false |
| 102 | |
| 103 | # Conservative traffic and safety limits. |
| 104 | DBBASIC_MAX_REQUEST_BYTES=1048576 |
| 105 | DBBASIC_MAX_CONCURRENT_REQUESTS=64 |
| 106 | DBBASIC_MAX_CONCURRENT_EXECUTIONS=8 |
| 107 | DBBASIC_OBJECT_TIMEOUT_SECONDS=5 |
| 108 | DBBASIC_RATE_LIMIT_REQUESTS=1000 |
| 109 | DBBASIC_RATE_LIMIT_WINDOW_SECONDS=60 |
| 110 | DBBASIC_RATE_LIMIT_TRUST_PROXY_HEADERS=true |
| 111 | DBBASIC_LOGIN_LOCKOUT_ATTEMPTS=5 |
| 112 | DBBASIC_LOGIN_LOCKOUT_WINDOW_SECONDS=900 |
| 113 | DBBASIC_ENABLE_REALTIME=true |
| 114 | EOF |
| 115 | fi |
| 116 | chown root:"${SERVICE_USER}" "${ENV_FILE}" |
| 117 | chmod 640 "${ENV_FILE}" |
| 118 | |
| 119 | say "Installing systemd unit" |
| 120 | cat > "${UNIT_FILE}" <<EOF |
| 121 | [Unit] |
| 122 | Description=DBBASIC Object Server |
| 123 | After=network-online.target |
| 124 | Wants=network-online.target |
| 125 | |
| 126 | [Service] |
| 127 | User=${SERVICE_USER} |
| 128 | Group=${SERVICE_USER} |
| 129 | WorkingDirectory=${CODE_DIR} |
| 130 | EnvironmentFile=${ENV_FILE} |
| 131 | ExecStart=${CODE_DIR}/.venv/bin/uvicorn object_server:app --host 127.0.0.1 --port 8001 --no-access-log |
| 132 | Restart=on-failure |
| 133 | RestartSec=2 |
| 134 | NoNewPrivileges=true |
| 135 | PrivateTmp=true |
| 136 | ProtectSystem=full |
| 137 | ReadWritePaths=${STATE_DIR} |
| 138 | |
| 139 | [Install] |
| 140 | WantedBy=multi-user.target |
| 141 | EOF |
| 142 | |
| 143 | systemctl daemon-reload |
| 144 | systemctl enable --now dbbasic-object-server |
| 145 | |
| 146 | say "Running filesystem checks" |
| 147 | set -a; . "${ENV_FILE}"; set +a |
| 148 | ( cd "${CODE_DIR}" && .venv/bin/python -m deployment_checks ) || warn "deployment_checks reported issues (see above)" |
| 149 | |
| 150 | say "Health check" |
| 151 | sleep 2 |
| 152 | if curl -fsS http://127.0.0.1:8001/health >/dev/null; then |
| 153 | say "Server is up on 127.0.0.1:8001" |
| 154 | else |
| 155 | warn "Health check failed — see: journalctl -u dbbasic-object-server -e" |
| 156 | fi |
| 157 | |
| 158 | cat <<EOF |
| 159 | |
| 160 | $(printf '\033[1;32mInstalled.\033[0m') Next, deliberate steps (docs/quickstart.md): |
| 161 | |
| 162 | 1. Create your admin user and turn on login: |
| 163 | cd ${CODE_DIR} |
| 164 | sudo -u ${SERVICE_USER} DBBASIC_DATA_DIR=${STATE_DIR}/data \\ |
| 165 | .venv/bin/python object_identity_cli.py create-superuser --user-id you --email you@example.com |
| 166 | # then set DBBASIC_ENABLE_PASSWORD_LOGIN=true in ${ENV_FILE} and: |
| 167 | sudo systemctl restart dbbasic-object-server |
| 168 | |
| 169 | 2. Put a domain and TLS in front (Caddy), then enable package installs to |
| 170 | add apps. Your admin token is in ${ENV_FILE} (DBBASIC_ADMIN_TOKEN). |
| 171 | |
| 172 | Watch logs: journalctl -u dbbasic-object-server -f |
| 173 | EOF |