#!/bin/bash set -e CONFIG="/etc/cave.conf" # Generate config if it doesn't exist if [ ! -f "$CONFIG" ]; then # Render the optional workflow image allowlist (space-separated prefixes) as a # Lisp list, or nil when unset. WF_ALLOWLIST="nil" if [ -n "${CAVE_WORKFLOWS_IMAGE_ALLOWLIST:-}" ]; then WF_ALLOWLIST="(" for prefix in ${CAVE_WORKFLOWS_IMAGE_ALLOWLIST}; do WF_ALLOWLIST="${WF_ALLOWLIST}\"${prefix}\" " done WF_ALLOWLIST="${WF_ALLOWLIST})" fi cat > "$CONFIG" </dev/tcp/${DB_HOST}/${DB_PORT}" 2>/dev/null; then echo "PostgreSQL is accepting connections." break fi if [ "$i" -eq 30 ]; then echo "PostgreSQL not ready after 30s, giving up." >&2 exit 1 fi sleep 1 done # Run migrations cave-server migrate --config "$CONFIG" # Generate initial authorized_keys cave-server update-keys \ --config "$CONFIG" \ --output /home/cave/.ssh/authorized_keys \ --cave-shell /usr/bin/cave-shell.sh || true chown cave:cave /home/cave/.ssh/authorized_keys 2>/dev/null || true # Ensure cave user owns data dirs and repos chown -R cave:cave /var/lib/cave # Trust all cave repos (ownership may differ between init and runtime) # Set for both root (Cave server) and cave user (SSH/git-shell) git config --global --add safe.directory '*' git config --global user.email "cave@localhost" git config --global user.name "Cave" su -c "git config --global --add safe.directory '*'" cave # Persist SSH host keys across restarts if [ ! -f /var/lib/cave/ssh_host_ed25519_key ]; then ssh-keygen -A cp /etc/ssh/ssh_host_*_key /etc/ssh/ssh_host_*_key.pub /var/lib/cave/ 2>/dev/null || true else cp /var/lib/cave/ssh_host_*_key /var/lib/cave/ssh_host_*_key.pub /etc/ssh/ 2>/dev/null || true chmod 600 /etc/ssh/ssh_host_*_key fi # Start sshd /usr/sbin/sshd # Start Cave (foreground) — run from /opt/cave so static/ is found cd /opt/cave exec cave-server serve --config "$CONFIG"