Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 7 minutes ago History
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#
31set -euo pipefail
32
33VERSION=""
34while [[ $# -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
39done
40
41REPO_URL="${DBBASIC_REPO_URL:-https://github.com/askrobots/dbbasic-object-server.git}"
42CODE_DIR="${DBBASIC_CODE_DIR:-/opt/dbbasic-object-server}"
43STATE_DIR="${DBBASIC_STATE_DIR:-/var/lib/dbbasic-object-server}"
44ENV_FILE="${DBBASIC_ENV_FILE:-/etc/dbbasic-object-server.env}"
45UNIT_FILE="/etc/systemd/system/dbbasic-object-server.service"
46DAEMON_UNIT_FILE="/etc/systemd/system/dbbasic-object-daemon.service"
47LAUNCHER_UNIT_FILE="/etc/systemd/system/dbbasic-sandbox-launcher.service"
48LAUNCHER_COPY="/usr/local/lib/dbbasic/object_sandbox_launcher.py"
49LAUNCHER_SOCKET="/run/dbbasic-sandbox/launcher.sock"
50SERVICE_USER="dbbasic"
51SANDBOX_USER="dbbasic-sandbox"
52
53say() { printf '\033[1;36m==>\033[0m %s\n' "$*"; }
54warn() { printf '\033[1;33m warn:\033[0m %s\n' "$*"; }
55
56if [[ "${EUID}" -ne 0 ]]; then
57 echo "Run as root: sudo bash scripts/install.sh" >&2
58 exit 1
59fi
60
61say "Installing system packages"
62apt-get update -qq
63apt-get install -y -qq python3 python3-venv python3-pip git
64
65say "Creating service user and directory layout"
66if ! id "${SERVICE_USER}" >/dev/null 2>&1; then
67 useradd --system --home "${STATE_DIR}" --shell /usr/sbin/nologin "${SERVICE_USER}"
68fi
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.
71if ! 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}"
73fi
74mkdir -p "${CODE_DIR}" "${STATE_DIR}/objects" "${STATE_DIR}/data"
75chown -R "${SERVICE_USER}:${SERVICE_USER}" "${CODE_DIR}" "${STATE_DIR}"
76chmod 755 "${CODE_DIR}"
77chmod 750 "${STATE_DIR}"
78# 0700 (0750 before the sandbox user existed): re-running the installer
79# closes an existing install's directories too.
80chmod 700 "${STATE_DIR}/objects" "${STATE_DIR}/data"
81
82say "Fetching server code into ${CODE_DIR}"
83if [[ -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
89else
90 sudo -u "${SERVICE_USER}" git clone "${REPO_URL}" "${CODE_DIR}"
91fi
92if [[ -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}"
99fi
100
101say "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.
104if [[ ! -x "${CODE_DIR}/.venv/bin/python" ]]; then
105 sudo -u "${SERVICE_USER}" python3 -m venv "${CODE_DIR}/.venv"
106fi
107sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q --upgrade pip
108sudo -u "${SERVICE_USER}" "${CODE_DIR}/.venv/bin/python" -m pip install -q -e "${CODE_DIR}[server,scheduler]"
109
110say "Recording what was installed (${STATE_DIR}/data/installed.json, shown by /health)"
111COMMIT="$(sudo -u "${SERVICE_USER}" git -C "${CODE_DIR}" rev-parse HEAD)"
112printf '{"release": "%s", "commit": "%s", "installed_at": "%s"}\n' \
113 "${VERSION}" "${COMMIT}" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "${STATE_DIR}/data/installed.json"
114chown "${SERVICE_USER}:${SERVICE_USER}" "${STATE_DIR}/data/installed.json"
115
116if [[ -f "${ENV_FILE}" ]]; then
117 warn "${ENV_FILE} already exists — keeping it (admin token unchanged)"
118else
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.
123DBBASIC_OBJECTS_DIR=${STATE_DIR}/objects
124DBBASIC_DATA_DIR=${STATE_DIR}/data
125DBBASIC_BACKUPS_DIR=${STATE_DIR}/data/backups
126DBBASIC_PACKAGES_DIR=${CODE_DIR}/packages
127DBBASIC_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).
132DBBASIC_ENABLE_SOURCE_WRITES=false
133DBBASIC_ENABLE_FILE_WRITES=false
134DBBASIC_ENABLE_PACKAGE_INSTALLS=false
135DBBASIC_ENABLE_PACKAGE_RESTORE=false
136DBBASIC_ENABLE_PASSWORD_LOGIN=false
137DBBASIC_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).
145DBBASIC_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).
149DBBASIC_ENABLE_PERMISSION_AUDIT=false
150DBBASIC_ENABLE_PERMISSION_ENFORCEMENT=false
151
152# Conservative traffic and safety limits.
153DBBASIC_MAX_REQUEST_BYTES=1048576
154DBBASIC_MAX_CONCURRENT_REQUESTS=64
155DBBASIC_MAX_CONCURRENT_EXECUTIONS=8
156DBBASIC_OBJECT_TIMEOUT_SECONDS=5
157DBBASIC_RATE_LIMIT_REQUESTS=1000
158DBBASIC_RATE_LIMIT_WINDOW_SECONDS=60
159DBBASIC_RATE_LIMIT_TRUST_PROXY_HEADERS=true
160DBBASIC_LOGIN_LOCKOUT_ATTEMPTS=5
161DBBASIC_LOGIN_LOCKOUT_WINDOW_SECONDS=900
162DBBASIC_ENABLE_REALTIME=true
163EOF
164fi
165chown root:"${SERVICE_USER}" "${ENV_FILE}"
166chmod 640 "${ENV_FILE}"
167
168say "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.
173install -D -o root -g root -m 0644 "${CODE_DIR}/object_sandbox_launcher.py" "${LAUNCHER_COPY}"
174cat > "${LAUNCHER_UNIT_FILE}" <<EOF
175[Unit]
176Description=DBBASIC sandbox launcher (starts user-object workers as ${SANDBOX_USER})
177Before=dbbasic-object-server.service dbbasic-object-daemon.service
178
179[Service]
180User=root
181ExecStart=/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
182RuntimeDirectory=dbbasic-sandbox
183RuntimeDirectoryMode=0750
184Restart=on-failure
185RestartSec=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.
189CapabilityBoundingSet=CAP_SETUID CAP_SETGID CAP_KILL CAP_CHOWN CAP_SYS_ADMIN
190NoNewPrivileges=true
191UMask=0077
192ProtectSystem=strict
193ProtectHome=true
194ProtectProc=invisible
195PrivateTmp=true
196PrivateDevices=true
197PrivateNetwork=true
198IPAddressDeny=any
199RestrictAddressFamilies=AF_UNIX
200RestrictNamespaces=net
201ProtectKernelTunables=true
202ProtectKernelModules=true
203ProtectKernelLogs=true
204ProtectControlGroups=true
205ProtectClock=true
206ProtectHostname=true
207RestrictRealtime=true
208RestrictSUIDSGID=true
209LockPersonality=true
210SystemCallArchitectures=native
211
212[Install]
213WantedBy=multi-user.target
214EOF
215
216say "Installing systemd unit"
217cat > "${UNIT_FILE}" <<EOF
218[Unit]
219Description=DBBASIC Object Server
220After=network-online.target dbbasic-sandbox-launcher.service
221Wants=network-online.target dbbasic-sandbox-launcher.service
222
223[Service]
224User=${SERVICE_USER}
225Group=${SERVICE_USER}
226WorkingDirectory=${CODE_DIR}
227EnvironmentFile=${ENV_FILE}
228ExecStart=${CODE_DIR}/.venv/bin/uvicorn object_server:app --host 127.0.0.1 --port 8001 --no-access-log
229Restart=on-failure
230RestartSec=2
231NoNewPrivileges=true
232PrivateTmp=true
233ProtectSystem=full
234ReadWritePaths=${STATE_DIR}
235
236[Install]
237WantedBy=multi-user.target
238EOF
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.
245cat > "${DAEMON_UNIT_FILE}" <<EOF
246[Unit]
247Description=DBBASIC Object Server daemon (scheduler, queue, background passes)
248After=network-online.target dbbasic-object-server.service dbbasic-sandbox-launcher.service
249Wants=network-online.target dbbasic-sandbox-launcher.service
250
251[Service]
252User=${SERVICE_USER}
253Group=${SERVICE_USER}
254WorkingDirectory=${CODE_DIR}
255EnvironmentFile=${ENV_FILE}
256ExecStart=${CODE_DIR}/.venv/bin/python object_daemon.py
257Restart=on-failure
258RestartSec=5
259NoNewPrivileges=true
260PrivateTmp=true
261ProtectSystem=full
262ReadWritePaths=${STATE_DIR}
263
264[Install]
265WantedBy=multi-user.target
266EOF
267
268systemctl daemon-reload
269systemctl 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
271systemctl restart dbbasic-sandbox-launcher
272systemctl restart dbbasic-object-server dbbasic-object-daemon
273
274say "Running filesystem checks"
275set -a; . "${ENV_FILE}"; set +a
276( cd "${CODE_DIR}" && .venv/bin/python -m deployment_checks ) || warn "deployment_checks reported issues (see above)"
277
278say "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
282say "Health check"
283sleep 2
284if curl -fsS http://127.0.0.1:8001/health >/dev/null; then
285 say "Server is up on 127.0.0.1:8001"
286else
287 warn "Health check failed — see: journalctl -u dbbasic-object-server -e"
288fi
289
290cat <<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
306EOF