#!/usr/bin/env bash # RasterizedDB: install the server as a systemd service that starts at boot, # runs as its own system user and keeps its data in /var/lib/rasterizeddb. # Run as root, from the directory that holds rasterizeddb.service. # # Every setting can be overridden from the environment, for example: # sudo RDB_BIN=./rasterizeddb RASTERIZEDDB_PG_PASSWORD='…' ./setup.sh set -euo pipefail RDB_BIN="${RDB_BIN:-rasterizeddb}" # the binary to install RDB_ARGS="${RDB_ARGS:-}" # extra server flags, written to the environment file on first install RDB_SERVICE_USER="${RDB_SERVICE_USER:-rasterizeddb}" RDB_START="${RDB_START:-1}" # 0 installs and enables without starting die() { echo "error: $*" >&2; exit 1; } [ "$(id -u)" -eq 0 ] || die "run this as root (sudo)" command -v systemctl >/dev/null 2>&1 || die "systemd is needed" here="$(cd "$(dirname "$0")" && pwd)" [ -f "$here/rasterizeddb.service" ] || die "rasterizeddb.service must be next to this script" src="$(command -v "$RDB_BIN" 2>/dev/null)" || die "'$RDB_BIN' not found; set RDB_BIN to the server binary" # The binary, where the unit expects it. if [ "$(readlink -f "$src")" != "/usr/local/bin/rasterizeddb" ]; then install -m 0755 "$src" /usr/local/bin/rasterizeddb echo "Installed /usr/local/bin/rasterizeddb" fi # The service user: no login, no home outside the data directory. if ! id "$RDB_SERVICE_USER" >/dev/null 2>&1; then nologin="$(command -v nologin || echo /usr/sbin/nologin)" useradd --system --user-group --home-dir /var/lib/rasterizeddb --no-create-home \ --shell "$nologin" --comment "RasterizedDB server" "$RDB_SERVICE_USER" echo "Created system user $RDB_SERVICE_USER" fi # The environment file: the password and any extra flags. Kept on later runs. install -d -m 0755 /etc/rasterizeddb env_file=/etc/rasterizeddb/rasterizeddb.env if [ ! -f "$env_file" ]; then : "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}" case "$RASTERIZEDDB_PG_PASSWORD" in *"'"* | *$'\n'*) die "the password may not contain a single quote or a newline" ;; esac umask 077 cat > "$env_file" < "/etc/systemd/system/rasterizeddb.service" chmod 0644 "/etc/systemd/system/rasterizeddb.service" systemctl daemon-reload if [ "$RDB_START" = 1 ]; then if systemctl is-active --quiet rasterizeddb; then systemctl restart rasterizeddb # pick up a new binary or unit fi systemctl enable --now rasterizeddb sleep 2 systemctl --no-pager --lines=5 status rasterizeddb || true else systemctl enable rasterizeddb fi echo echo "Service: systemctl status rasterizeddb / journalctl -u rasterizeddb" echo "PostgreSQL: postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require" echo "Native: 127.0.0.1:61170 (user rasterized)" echo "The password is the one in $env_file."