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.
install.sh · 12.1 KB · 306 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 | # - creates the `dbbasic-sandbox` user that user objects run as, and a small |
| 9 | # root launcher unit that starts them (dbbasic-sandbox-launcher.service) |
| 10 | # - clones (or reuses) the server under /opt/dbbasic-object-server |
| 11 | # - builds a virtualenv and installs the server |
| 12 | # - writes /etc/dbbasic-object-server.env with a freshly generated admin |
| 13 | # token (only if the file does not already exist — never overwrites yours) |
| 14 | # - installs and starts a systemd unit bound to 127.0.0.1:8001 |
| 15 | # - runs the filesystem checker and a health check |
| 16 | # |
| 17 | # What it deliberately does NOT do (these are short, deliberate follow-ups |
| 18 | # printed at the end, and covered in docs/quickstart.md): |
| 19 | # - create your admin user / turn on password login |
| 20 | # - enable source writes, file writes, or package installs |
| 21 | # - configure a public domain / TLS proxy |
| 22 | # |
| 23 | # Read it before you run it. Then: sudo bash scripts/install.sh |
| 24 | # |
| 25 | # --version 2026.11v1 install that release (a tag) instead of the newest main, and say so in |
| 26 | # /health (data/installed.json). Running again with another release moves to it. |
| 27 | # |
| 28 | # For a box on the public internet with nobody signed up yet, scripts/one-click-install.sh runs this |
| 29 | # and then closes it up (login, permissions, a setup code, HTTPS). |
| 30 | # |
| 31 | set -euo pipefail |
| 32 | |
| 33 | VERSION="" |
| 34 | while [[ $# -gt 0 ]]; do |
| 35 | case "$1" in |
| 36 | --version) VERSION="${2:?--version needs a release, such as 2026.11v1}"; shift 2 ;; |
| 37 | *) echo "Unknown option: $1" >&2; exit 2 ;; |
| 38 | esac |
| 39 | done |
| 40 | |
| 41 | REPO_URL="${DBBASIC_REPO_URL:-https://github.com/askrobots/dbbasic-object-server.git}" |
| 42 | CODE_DIR="${DBBASIC_CODE_DIR:-/opt/dbbasic-object-server}" |
| 43 | STATE_DIR="${DBBASIC_STATE_DIR:-/var/lib/dbbasic-object-server}" |
| 44 | ENV_FILE="${DBBASIC_ENV_FILE:-/etc/dbbasic-object-server.env}" |
| 45 | UNIT_FILE="/etc/systemd/system/dbbasic-object-server.service" |
| 46 | DAEMON_UNIT_FILE="/etc/systemd/system/dbbasic-object-daemon.service" |
| 47 | LAUNCHER_UNIT_FILE="/etc/systemd/system/dbbasic-sandbox-launcher.service" |
| 48 | LAUNCHER_COPY="/usr/local/lib/dbbasic/object_sandbox_launcher.py" |
| 49 | LAUNCHER_SOCKET="/run/dbbasic-sandbox/launcher.sock" |
| 50 | SERVICE_USER="dbbasic" |
| 51 | SANDBOX_USER="dbbasic-sandbox" |
| 52 | |
| 53 | say() { printf '\033[1;36m==>\033[0m %s\n' "$*"; } |
| 54 | warn() { printf '\033[1;33m warn:\033[0m %s\n' "$*"; } |
| 55 | |
| 56 | if [[ "${EUID}" -ne 0 ]]; then |
| 57 | echo "Run as root: sudo bash scripts/install.sh" >&2 |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
| 61 | say "Installing system packages" |
| 62 | apt-get update -qq |
| 63 | apt-get install -y -qq python3 python3-venv python3-pip git |
| 64 | |
| 65 | say "Creating service user and directory layout" |
| 66 | if ! id "${SERVICE_USER}" >/dev/null 2>&1; then |
| 67 | useradd --system --home "${STATE_DIR}" --shell /usr/sbin/nologin "${SERVICE_USER}" |
| 68 | fi |
| 69 | # User objects run as this second user: it owns nothing and is in no group, |
| 70 | # so it cannot chmod, utime or even stat the server's files. |
| 71 | if ! id "${SANDBOX_USER}" >/dev/null 2>&1; then |
| 72 | useradd --system --user-group --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin "${SANDBOX_USER}" |
| 73 | fi |
| 74 | mkdir -p "${CODE_DIR}" "${STATE_DIR}/objects" "${STATE_DIR}/data" |
| 75 | chown -R "${SERVICE_USER}:${SERVICE_USER}" "${CODE_DIR}" "${STATE_DIR}" |
| 76 | chmod 755 "${CODE_DIR}" |
| 77 | chmod 750 "${STATE_DIR}" |
| 78 | # 0700 (0750 before the sandbox user existed): re-running the installer |
| 79 | # closes an existing install's directories too. |
| 80 | chmod 700 "${STATE_DIR}/objects" "${STATE_DIR}/data" |
| 81 | |
| 82 | say "Fetching server code into ${CODE_DIR}" |
| 83 | if [[ -d "${CODE_DIR}/.git" ]]; then |
| 84 | if [[ -n "${VERSION}" ]]; then |
| 85 | sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" fetch -q --tags origin |
| 86 | else |
| 87 | sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" pull --ff-only |
| 88 | fi |
| 89 | else |
| 90 | sudo -u "${SERVICE_USER}" git clone "${REPO_URL}" "${CODE_DIR}" |
| 91 | fi |
| 92 | if [[ -n "${VERSION}" ]]; then |
| 93 | # a release is a tag: a branch of the same name must not stand in for it |
| 94 | if ! sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" rev-parse -q --verify "refs/tags/${VERSION}^{commit}" >/dev/null; then |
| 95 | echo "No release named ${VERSION} in ${REPO_URL}" >&2 |
| 96 | exit 1 |
| 97 | fi |
| 98 | sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" -c advice.detachedHead=false checkout -q --detach "refs/tags/${VERSION}" |
| 99 | fi |
| 100 | |
| 101 | say "Building virtualenv and installing the server" |
| 102 | # `scheduler` (croniter) is for the daemon unit installed below: every schedule the shipped packages |
| 103 | # declare is cron, and without croniter the daemon can never compute a next run. |
| 104 | if [[ ! -x "${CODE_DIR}/.venv/bin/python" ]]; then |
| 105 | sudo -u "${SERVICE_USER}" python3 -m venv "${CODE_DIR}/.venv" |
| 106 | fi |
| 107 | sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q --upgrade pip |
| 108 | sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q -e "${CODE_DIR}[server,scheduler]" |
| 109 | |
| 110 | say "Recording what was installed (${STATE_DIR}/data/installed.json, shown by /health)" |
| 111 | COMMIT="$(sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" rev-parse HEAD)" |
| 112 | printf '{"release": "%s", "commit": "%s", "installed_at": "%s"}\n' \ |
| 113 | "${VERSION}" "${COMMIT}" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "${STATE_DIR}/data/installed.json" |
| 114 | chown "${SERVICE_USER}:${SERVICE_USER}" "${STATE_DIR}/data/installed.json" |
| 115 | |
| 116 | if [[ -f "${ENV_FILE}" ]]; then |
| 117 | warn "${ENV_FILE} already exists — keeping it (admin token unchanged)" |
| 118 | else |
| 119 | say "Writing ${ENV_FILE} with a freshly generated admin token" |
| 120 | ADMIN_TOKEN="$("${CODE_DIR}/.venv/bin/python" -c 'import secrets; print(secrets.token_urlsafe(32))')" |
| 121 | cat > "${ENV_FILE}" <<EOF |
| 122 | # Generated by scripts/install.sh. Edit deliberately; restart the service after changes. |
| 123 | DBBASIC_OBJECTS_DIR=${STATE_DIR}/objects |
| 124 | DBBASIC_DATA_DIR=${STATE_DIR}/data |
| 125 | DBBASIC_BACKUPS_DIR=${STATE_DIR}/data/backups |
| 126 | DBBASIC_PACKAGES_DIR=${CODE_DIR}/packages |
| 127 | DBBASIC_ADMIN_TOKEN=${ADMIN_TOKEN} |
| 128 | |
| 129 | # First-boot posture: writes and public code execution stay closed until you |
| 130 | # have login, backups, and proxy access confirmed. Turn these on deliberately |
| 131 | # (see docs/quickstart.md and docs/single-vm-deployment.md). |
| 132 | DBBASIC_ENABLE_SOURCE_WRITES=false |
| 133 | DBBASIC_ENABLE_FILE_WRITES=false |
| 134 | DBBASIC_ENABLE_PACKAGE_INSTALLS=false |
| 135 | DBBASIC_ENABLE_PACKAGE_RESTORE=false |
| 136 | DBBASIC_ENABLE_PASSWORD_LOGIN=false |
| 137 | DBBASIC_COOKIE_SECURE=true |
| 138 | |
| 139 | # Clean page URLs (/notes, /tasks) resolved to objects and seeded view |
| 140 | # records. Without this every app page 404s before routes are even |
| 141 | # consulted -- the flag exists for API-only deployments, but the app |
| 142 | # suite is the product, so it ships ON (found via the e2e browser lane, |
| 143 | # whose fixture was the only place all the required flags appeared |
| 144 | # together). |
| 145 | DBBASIC_ENABLE_SITE_ROUTES=true |
| 146 | |
| 147 | # Permissions start in the open; move to audit, then enforcement, once a |
| 148 | # policy and login are in place (docs/permissions-model.md). |
| 149 | DBBASIC_ENABLE_PERMISSION_AUDIT=false |
| 150 | DBBASIC_ENABLE_PERMISSION_ENFORCEMENT=false |
| 151 | |
| 152 | # Conservative traffic and safety limits. |
| 153 | DBBASIC_MAX_REQUEST_BYTES=1048576 |
| 154 | DBBASIC_MAX_CONCURRENT_REQUESTS=64 |
| 155 | DBBASIC_MAX_CONCURRENT_EXECUTIONS=8 |
| 156 | DBBASIC_OBJECT_TIMEOUT_SECONDS=5 |
| 157 | DBBASIC_RATE_LIMIT_REQUESTS=1000 |
| 158 | DBBASIC_RATE_LIMIT_WINDOW_SECONDS=60 |
| 159 | DBBASIC_RATE_LIMIT_TRUST_PROXY_HEADERS=true |
| 160 | DBBASIC_LOGIN_LOCKOUT_ATTEMPTS=5 |
| 161 | DBBASIC_LOGIN_LOCKOUT_WINDOW_SECONDS=900 |
| 162 | DBBASIC_ENABLE_REALTIME=true |
| 163 | EOF |
| 164 | fi |
| 165 | chown root:"${SERVICE_USER}" "${ENV_FILE}" |
| 166 | chmod 640 "${ENV_FILE}" |
| 167 | |
| 168 | say "Installing the sandbox launcher (${LAUNCHER_COPY}, root-owned)" |
| 169 | # Root runs a root-owned copy with the system interpreter (-I -S: no |
| 170 | # environment, no site-packages), never the file in the checkout, which the |
| 171 | # service user can edit. The worker it starts may come from the checkout: |
| 172 | # it runs as ${SANDBOX_USER}, which has less than ${SERVICE_USER} already has. |
| 173 | install -D -o root -g root -m 0644 "${CODE_DIR}/object_sandbox_launcher.py" "${LAUNCHER_COPY}" |
| 174 | cat > "${LAUNCHER_UNIT_FILE}" <<EOF |
| 175 | [Unit] |
| 176 | Description=DBBASIC sandbox launcher (starts user-object workers as ${SANDBOX_USER}) |
| 177 | Before=dbbasic-object-server.service dbbasic-object-daemon.service |
| 178 | |
| 179 | [Service] |
| 180 | User=root |
| 181 | ExecStart=/usr/bin/python3 -I -S ${LAUNCHER_COPY} --socket ${LAUNCHER_SOCKET} --server-user ${SERVICE_USER} --sandbox-user ${SANDBOX_USER} --python ${CODE_DIR}/.venv/bin/python --worker ${CODE_DIR}/object_sandbox.py |
| 182 | RuntimeDirectory=dbbasic-sandbox |
| 183 | RuntimeDirectoryMode=0750 |
| 184 | Restart=on-failure |
| 185 | RestartSec=2 |
| 186 | # Workers inherit everything below. What the launcher itself needs: switch |
| 187 | # uid/gid, kill its workers, give its socket directory to the server's |
| 188 | # group, and create a network namespace per worker. |
| 189 | CapabilityBoundingSet=CAP_SETUID CAP_SETGID CAP_KILL CAP_CHOWN CAP_SYS_ADMIN |
| 190 | NoNewPrivileges=true |
| 191 | UMask=0077 |
| 192 | ProtectSystem=strict |
| 193 | ProtectHome=true |
| 194 | ProtectProc=invisible |
| 195 | PrivateTmp=true |
| 196 | PrivateDevices=true |
| 197 | PrivateNetwork=true |
| 198 | IPAddressDeny=any |
| 199 | RestrictAddressFamilies=AF_UNIX |
| 200 | RestrictNamespaces=net |
| 201 | ProtectKernelTunables=true |
| 202 | ProtectKernelModules=true |
| 203 | ProtectKernelLogs=true |
| 204 | ProtectControlGroups=true |
| 205 | ProtectClock=true |
| 206 | ProtectHostname=true |
| 207 | RestrictRealtime=true |
| 208 | RestrictSUIDSGID=true |
| 209 | LockPersonality=true |
| 210 | SystemCallArchitectures=native |
| 211 | |
| 212 | [Install] |
| 213 | WantedBy=multi-user.target |
| 214 | EOF |
| 215 | |
| 216 | say "Installing systemd unit" |
| 217 | cat > "${UNIT_FILE}" <<EOF |
| 218 | [Unit] |
| 219 | Description=DBBASIC Object Server |
| 220 | After=network-online.target dbbasic-sandbox-launcher.service |
| 221 | Wants=network-online.target dbbasic-sandbox-launcher.service |
| 222 | |
| 223 | [Service] |
| 224 | User=${SERVICE_USER} |
| 225 | Group=${SERVICE_USER} |
| 226 | WorkingDirectory=${CODE_DIR} |
| 227 | EnvironmentFile=${ENV_FILE} |
| 228 | ExecStart=${CODE_DIR}/.venv/bin/uvicorn object_server:app --host 127.0.0.1 --port 8001 --no-access-log |
| 229 | Restart=on-failure |
| 230 | RestartSec=2 |
| 231 | NoNewPrivileges=true |
| 232 | PrivateTmp=true |
| 233 | ProtectSystem=full |
| 234 | ReadWritePaths=${STATE_DIR} |
| 235 | |
| 236 | [Install] |
| 237 | WantedBy=multi-user.target |
| 238 | EOF |
| 239 | |
| 240 | # The background daemon (object_daemon.py): scheduled tasks -- package |
| 241 | # schedules and the methods objects schedule for themselves with |
| 242 | # _schedule -- the queue, change dispatch, retention and cleanup passes. |
| 243 | # Without it a box looks healthy while nothing time-driven ever happens. |
| 244 | # Same user, code, env file and data dir as the server; no port. |
| 245 | cat > "${DAEMON_UNIT_FILE}" <<EOF |
| 246 | [Unit] |
| 247 | Description=DBBASIC Object Server daemon (scheduler, queue, background passes) |
| 248 | After=network-online.target dbbasic-object-server.service dbbasic-sandbox-launcher.service |
| 249 | Wants=network-online.target dbbasic-sandbox-launcher.service |
| 250 | |
| 251 | [Service] |
| 252 | User=${SERVICE_USER} |
| 253 | Group=${SERVICE_USER} |
| 254 | WorkingDirectory=${CODE_DIR} |
| 255 | EnvironmentFile=${ENV_FILE} |
| 256 | ExecStart=${CODE_DIR}/.venv/bin/python object_daemon.py |
| 257 | Restart=on-failure |
| 258 | RestartSec=5 |
| 259 | NoNewPrivileges=true |
| 260 | PrivateTmp=true |
| 261 | ProtectSystem=full |
| 262 | ReadWritePaths=${STATE_DIR} |
| 263 | |
| 264 | [Install] |
| 265 | WantedBy=multi-user.target |
| 266 | EOF |
| 267 | |
| 268 | systemctl daemon-reload |
| 269 | systemctl enable -q dbbasic-sandbox-launcher dbbasic-object-server dbbasic-object-daemon |
| 270 | # restart, not start: a second run has new code (another release, or a newer main) to load |
| 271 | systemctl restart dbbasic-sandbox-launcher |
| 272 | systemctl restart dbbasic-object-server dbbasic-object-daemon |
| 273 | |
| 274 | say "Running filesystem checks" |
| 275 | set -a; . "${ENV_FILE}"; set +a |
| 276 | ( cd "${CODE_DIR}" && .venv/bin/python -m deployment_checks ) || warn "deployment_checks reported issues (see above)" |
| 277 | |
| 278 | say "User-object isolation (object_sandbox.py --status)" |
| 279 | ( cd "${CODE_DIR}" && sudo -u "${SERVICE_USER}" .venv/bin/python object_sandbox.py --status ) \ |
| 280 | || warn "user objects (u_<id>_*) will be refused on this host; the status above says why" |
| 281 | |
| 282 | say "Health check" |
| 283 | sleep 2 |
| 284 | if curl -fsS http://127.0.0.1:8001/health >/dev/null; then |
| 285 | say "Server is up on 127.0.0.1:8001" |
| 286 | else |
| 287 | warn "Health check failed — see: journalctl -u dbbasic-object-server -e" |
| 288 | fi |
| 289 | |
| 290 | cat <<EOF |
| 291 | |
| 292 | $(printf '\033[1;32mInstalled.\033[0m') Next, deliberate steps (docs/quickstart.md): |
| 293 | |
| 294 | 1. Create your admin user and turn on login: |
| 295 | cd ${CODE_DIR} |
| 296 | sudo -u ${SERVICE_USER} DBBASIC_DATA_DIR=${STATE_DIR}/data \\ |
| 297 | .venv/bin/python object_identity_cli.py create-superuser --user-id you --email you@example.com |
| 298 | # then set DBBASIC_ENABLE_PASSWORD_LOGIN=true in ${ENV_FILE} and: |
| 299 | sudo systemctl restart dbbasic-object-server |
| 300 | |
| 301 | 2. Put a domain and TLS in front (Caddy), then enable package installs to |
| 302 | add apps. Your admin token is in ${ENV_FILE} (DBBASIC_ADMIN_TOKEN). |
| 303 | |
| 304 | Watch logs: journalctl -u dbbasic-object-server -f |
| 305 | Daemon logs: journalctl -u dbbasic-object-daemon -f |
| 306 | EOF |