#!/bin/bash # Phase 2: Recover compose files, deploy services, start Gitea. # Pipeable — run after setup.sh and after rsync'ing the repo. # # Usage (from workstation): # ssh root@ 'bash -s' < bootstrap.sh # # Prerequisites: # - setup.sh already run (infra_service user, dirs, uv) # - Repo rsync'd to /opt/server-infra/ # # What it does: # 1. Validates prerequisites # 2. Recovers compose files from running containers (umami, reverse-proxy) # 3. Creates data directories # 4. Sets correct ownership # 5. Prints next steps (start Gitea, add proxy host, push repo to Gitea) set -euo pipefail SERVICE_USER="infra_service" REPO_DIR="/opt/server-infra" DATA_DIR="/data/server-infra" UV="/home/${SERVICE_USER}/.local/bin/uv" [ "$(id -u)" = "0" ] || { echo "ERROR: Run as root"; exit 1; } log() { echo "$(date '+%H:%M:%S') ==> $*"; } warn() { echo "$(date '+%H:%M:%S') ==> WARNING: $*" >&2; } # ── Preflight checks ─────────────────────────────────────────────────────────── if ! id "${SERVICE_USER}" >/dev/null 2>&1; then echo "ERROR: ${SERVICE_USER} user not found. Run setup.sh first." exit 1 fi if [ ! -d "${REPO_DIR}/.git" ]; then echo "ERROR: Repo not found at ${REPO_DIR}. Rsync the repo first:" echo " rsync -av --chown=root:root ~/Projects/server-infra/ root@:${REPO_DIR}/" exit 1 fi command -v docker >/dev/null 2>&1 || { echo "ERROR: docker not found"; exit 1; } [ -f "${UV}" ] || { echo "ERROR: uv not found at ${UV}. Run setup.sh first."; exit 1; } # ── Recover compose files from running containers ────────────────────────────── recover_project() { local outfile="$1" shift local containers=("$@") local running=0 for c in "${containers[@]}"; do if docker inspect "${c}" >/dev/null 2>&1; then running=1 break fi done if [ "${running}" = "0" ]; then warn "None of [${containers[*]}] are running — skipping recovery." return 0 fi log "Recovering: ${containers[*]}" sudo -u "${SERVICE_USER}" \ "/home/${SERVICE_USER}/.local/bin/uvx" --from docker-autocompose autocompose "${containers[@]}" > "${outfile}" log " Saved to ${outfile}" } log "Recovering compose files from running containers..." recover_project \ "${REPO_DIR}/umami/docker-compose.yml" \ umami-umami-1 umami-db-1 recover_project \ "${REPO_DIR}/reverse-proxy/docker-compose.yml" \ reverse_proxy-app-1 # ── Data directories ─────────────────────────────────────────────────────────── log "Creating data directories..." mkdir -p "${DATA_DIR}/gitea" # ── Ownership ────────────────────────────────────────────────────────────────── log "Setting ownership..." chown -R "${SERVICE_USER}:${SERVICE_USER}" "${REPO_DIR}" chown -R "${SERVICE_USER}:${SERVICE_USER}" "${DATA_DIR}" # ── Summary ──────────────────────────────────────────────────────────────────── SERVER_IP=$(hostname -I | awk '{print $1}') echo "" echo "==================================================================" echo "" echo " Bootstrap complete." echo "" echo " Services ready in ${REPO_DIR}/:" [ -f "${REPO_DIR}/umami/docker-compose.yml" ] && echo " umami (recovered)" [ -f "${REPO_DIR}/reverse-proxy/docker-compose.yml" ] && echo " reverse-proxy (recovered)" [ -f "${REPO_DIR}/gitea/docker-compose.yml" ] && echo " gitea (from repo)" echo "" echo " Service user: ${SERVICE_USER}" echo " Data dir: ${DATA_DIR}/" echo "" echo "==================================================================" echo "" echo " Next steps:" echo "" echo " 1. Start Gitea:" echo " sudo -u ${SERVICE_USER} docker compose -f ${REPO_DIR}/gitea/docker-compose.yml up -d" echo " # Web installer at http://${SERVER_IP}:3000" echo " # Set ROOT_URL to your public domain (e.g. https://git.yourdomain.com)" echo "" echo " 2. Add proxy host in nginx proxy manager:" echo " Forward hostname → ${SERVER_IP}:3000" echo "" echo " 3. Commit recovered compose files and push to Gitea:" echo " cd ${REPO_DIR}" echo " sudo -u ${SERVICE_USER} git add umami/docker-compose.yml reverse-proxy/docker-compose.yml" echo " sudo -u ${SERVICE_USER} git commit -m 'chore: recover compose files'" echo " sudo -u ${SERVICE_USER} git remote add origin https://git.yourdomain.com/youruser/server-infra.git" echo " sudo -u ${SERVICE_USER} git push -u origin master" echo "" echo "==================================================================" echo ""