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.
one-click-install.sh · 7.4 KB · 157 lines
| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # DBBASIC Object Server on a new public server, for a one-click installer (cloud-init.yaml runs this). |
| 4 | # |
| 5 | # sudo bash scripts/one-click-install.sh 2026.11v1 |
| 6 | # |
| 7 | # scripts/install.sh assumes an operator who is there and opens things up step by step. A one-click box |
| 8 | # is on the public internet from its first second with nobody signed up, so this runs install.sh and then |
| 9 | # closes it up before anything outside can reach it: |
| 10 | # |
| 11 | # 1. a setup code. The installer (GitStable) shows the code to the person who pressed the button and |
| 12 | # puts only its SHA-256 in /etc/dbbasic-setup-code.sha256 (cloud-init). It moves into the data |
| 13 | # directory, and until someone uses it at /setup the server answers nothing but /health and /setup |
| 14 | # (object_setup.py). No default login, and no password the box could hand out. |
| 15 | # 2. password login on (setup makes one) and permission enforcement on, set before the restart below. |
| 16 | # The box's policy is the rules its starting apps ship (each package writes its own); no separate |
| 17 | # starter policy is saved. Enforcement is effective from that first start: password login counts as |
| 18 | # the sign-in path before any user exists, and until setup the gate above answers only /health and |
| 19 | # /setup (the admin token still works). A policy with no allow grants is enforced as deny-all, so |
| 20 | # even a box with no policy file starts. A box with enforcement on and no usable sign-in path or |
| 21 | # admin token refuses to start rather than run open. |
| 22 | # 3. HTTPS on the server's IP address, from Let's Encrypt, through Caddy: the setup code and the new |
| 23 | # password never cross the network in the clear. The server itself stays on 127.0.0.1:8001. |
| 24 | # |
| 25 | # Refuses to start without the setup code: a public box with no way to claim it but a race is worse |
| 26 | # than no box. Safe to re-run (a later release, or after a failure); a box already set up stays set up. |
| 27 | # |
| 28 | set -euo pipefail |
| 29 | |
| 30 | VERSION="${1:?usage: one-click-install.sh <release, such as 2026.11v1>}" |
| 31 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 32 | CODE_DIR="${DBBASIC_CODE_DIR:-/opt/dbbasic-object-server}" |
| 33 | STATE_DIR="${DBBASIC_STATE_DIR:-/var/lib/dbbasic-object-server}" |
| 34 | ENV_FILE="${DBBASIC_ENV_FILE:-/etc/dbbasic-object-server.env}" |
| 35 | DATA_DIR="${STATE_DIR}/data" |
| 36 | SETUP_HASH_IN="/etc/dbbasic-setup-code.sha256" |
| 37 | SETUP_HASH="${DATA_DIR}/identity/setup_code.sha256" |
| 38 | SERVICE_USER="dbbasic" |
| 39 | export DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a |
| 40 | |
| 41 | say() { printf '\033[1;36m==>\033[0m %s\n' "$*"; } |
| 42 | |
| 43 | if [[ "${EUID}" -ne 0 ]]; then |
| 44 | echo "Run as root." >&2 |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | already_set_up() { # an admin exists: the same test the server uses (object_setup._admin_exists) |
| 49 | [[ -f "${DATA_DIR}/identity/users.tsv" && -x "${CODE_DIR}/.venv/bin/python" ]] || return 1 |
| 50 | ( cd "${CODE_DIR}" && sudo -u "${SERVICE_USER}" .venv/bin/python -c \ |
| 51 | 'import sys, object_setup as s; sys.exit(0 if s._admin_exists(sys.argv[1]) else 1)' "${DATA_DIR}" ) |
| 52 | } |
| 53 | |
| 54 | if [[ ! -s "${SETUP_HASH_IN}" && ! -s "${SETUP_HASH}" ]] && ! already_set_up; then |
| 55 | echo "No setup code (${SETUP_HASH_IN}). The installer puts its hash there; without it nobody could" >&2 |
| 56 | echo "become this server's admin except by racing for it. Stopping before anything is installed." >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | if ! grep -Eqx 'sha256:[0-9a-f]{64}' "${SETUP_HASH_IN}" 2>/dev/null && [[ -e "${SETUP_HASH_IN}" ]]; then |
| 60 | echo "${SETUP_HASH_IN} isn't a setup code hash (sha256:<64 hex>). Stopping." >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | bash "${HERE}/install.sh" --version "${VERSION}" |
| 65 | |
| 66 | say "Setup code: only its hash, readable by the server alone" |
| 67 | if [[ -s "${SETUP_HASH_IN}" ]]; then |
| 68 | if already_set_up; then |
| 69 | rm -f "${SETUP_HASH_IN}" # someone is admin already: there's nothing left to claim |
| 70 | else |
| 71 | install -d -o "${SERVICE_USER}" -g "${SERVICE_USER}" -m 0700 "${DATA_DIR}/identity" |
| 72 | install -o "${SERVICE_USER}" -g "${SERVICE_USER}" -m 0600 "${SETUP_HASH_IN}" "${SETUP_HASH}" |
| 73 | rm -f "${SETUP_HASH_IN}" |
| 74 | FRESH=1 |
| 75 | fi |
| 76 | fi |
| 77 | |
| 78 | if [[ "${FRESH:-0}" == 1 ]]; then |
| 79 | # A new box with no apps has no home page: the owner finished setup and landed on "Not found" (seen on the |
| 80 | # first test droplet, 2026-11-23). The apps to start with are the desk's set (nc-object-bootstrap) without |
| 81 | # the ones that need keys or more setup (shell, notify, sites). Order matters: install_package doesn't pull |
| 82 | # in dependencies, and notes without app-views was "Object not found: site_view_render" (2026.11v1). |
| 83 | # Only on a fresh box, so a re-run never replaces apps the owner may have changed. |
| 84 | say "Starting apps: home and menu, projects, notes, tasks, files" |
| 85 | ( cd "${CODE_DIR}" && sudo -u "${SERVICE_USER}" .venv/bin/python -c ' |
| 86 | import sys |
| 87 | from pathlib import Path |
| 88 | import object_packages |
| 89 | for app in ("app-theme", "app-views", "app-nav", "app-projects", "app-files", "app-tasks", "app-collab", |
| 90 | "app-notes", "app-settings"): |
| 91 | object_packages.install_package(app, root=Path("packages"), base_dir=Path(sys.argv[1]), |
| 92 | object_roots=[Path(sys.argv[2])], allow_replace=True) |
| 93 | print(" " + app) |
| 94 | ' "${DATA_DIR}" "${STATE_DIR}/objects" ) |
| 95 | fi |
| 96 | |
| 97 | say "Login and permissions for a public server" |
| 98 | set_env() { # KEY VALUE: replace the line, or add it |
| 99 | if grep -q "^$1=" "${ENV_FILE}"; then |
| 100 | sed -i "s|^$1=.*|$1=$2|" "${ENV_FILE}" |
| 101 | else |
| 102 | printf '%s=%s\n' "$1" "$2" >> "${ENV_FILE}" |
| 103 | fi |
| 104 | } |
| 105 | set_env DBBASIC_ENABLE_PASSWORD_LOGIN true |
| 106 | set_env DBBASIC_ENABLE_PERMISSION_ENFORCEMENT true |
| 107 | set_env DBBASIC_COOKIE_SECURE true |
| 108 | set_env DBBASIC_RATE_LIMIT_TRUST_PROXY_HEADERS true # Caddy writes X-Forwarded-For itself |
| 109 | systemctl restart dbbasic-object-server dbbasic-object-daemon |
| 110 | |
| 111 | say "Caddy, for HTTPS on this server's address" |
| 112 | ADDRESS="${DBBASIC_PUBLIC_ADDRESS:-}" |
| 113 | if [[ -z "${ADDRESS}" ]]; then |
| 114 | # DigitalOcean's metadata service; another cloud sets DBBASIC_PUBLIC_ADDRESS |
| 115 | ADDRESS="$(curl -fsS --max-time 5 http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address || true)" |
| 116 | fi |
| 117 | if ! [[ "${ADDRESS}" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]; then |
| 118 | echo "Couldn't tell this server's public IPv4 address; set DBBASIC_PUBLIC_ADDRESS and run again." >&2 |
| 119 | exit 1 |
| 120 | fi |
| 121 | if ! command -v caddy >/dev/null 2>&1; then |
| 122 | # Caddy's own packages: Ubuntu's caddy is too old to ask Let's Encrypt for an IP address certificate |
| 123 | apt-get install -y -qq debian-keyring debian-archive-keyring apt-transport-https curl gnupg |
| 124 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \ |
| 125 | | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg |
| 126 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list |
| 127 | apt-get update -qq |
| 128 | apt-get install -y -qq caddy |
| 129 | fi |
| 130 | cat > /etc/caddy/Caddyfile <<EOF |
| 131 | # Written by scripts/one-click-install.sh. A certificate for the bare IP address (Let's Encrypt gives |
| 132 | # those only as six-day "shortlived" certificates; Caddy renews them). With a domain, replace the |
| 133 | # address below with the domain name and drop the two global lines. |
| 134 | { |
| 135 | default_sni ${ADDRESS} |
| 136 | cert_issuer acme { |
| 137 | profile shortlived |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | ${ADDRESS} { |
| 142 | reverse_proxy 127.0.0.1:8001 |
| 143 | } |
| 144 | EOF |
| 145 | systemctl enable -q caddy |
| 146 | systemctl restart caddy |
| 147 | |
| 148 | say "Waiting for https://${ADDRESS}/health (the certificate takes a few seconds)" |
| 149 | for _ in $(seq 1 60); do |
| 150 | if curl -fsS --max-time 5 "https://${ADDRESS}/health" >/dev/null 2>&1; then |
| 151 | say "Up: https://${ADDRESS}/ (setup at https://${ADDRESS}/setup with the code the installer showed you)" |
| 152 | exit 0 |
| 153 | fi |
| 154 | sleep 3 |
| 155 | done |
| 156 | echo "HTTPS didn't answer yet; see: journalctl -u caddy -e" >&2 |
| 157 | exit 1 |