From e570468610f57b1b18319f632d814e0dacb6ef74 Mon Sep 17 00:00:00 2001 From: Xika Date: Mon, 8 Jun 2026 13:14:25 +0000 Subject: [PATCH] infra(BIT-24694): add MoneyFlix prod LXC install and smoke scripts - tools/proxmox/install_moneyflix_stack.sh: guest-side stack installer (MariaDB, PHP 8.1, nginx, Node.js 20, systemd unit moneyflix-redirector) Reads /etc/moneyflix/moneyflix.env; rejects placeholder credentials. - tools/proxmox/smoke_moneyflix_prod.sh: post-deploy smoke from Paperclip host Checks redirector /health, /status, /test-db and CRM /system/status. Paperclip-Issue: BIT-24694 Paperclip-Agent: devops --- tools/proxmox/install_moneyflix_stack.sh | 247 +++++++++++++++++++++++ tools/proxmox/smoke_moneyflix_prod.sh | 103 ++++++++++ 2 files changed, 350 insertions(+) create mode 100644 tools/proxmox/install_moneyflix_stack.sh create mode 100644 tools/proxmox/smoke_moneyflix_prod.sh diff --git a/tools/proxmox/install_moneyflix_stack.sh b/tools/proxmox/install_moneyflix_stack.sh new file mode 100644 index 0000000..3fb76a3 --- /dev/null +++ b/tools/proxmox/install_moneyflix_stack.sh @@ -0,0 +1,247 @@ +#!/usr/bin/env bash +# Install MoneyFlix stack on a fresh Ubuntu 22.04 LXC (BIT-24694). +# Run as root INSIDE the guest CT (pct enter or SSH). +# PREREQUISITE: /etc/moneyflix/moneyflix.env must exist with real credentials +# sourced from vault (Infisical CT 235 at 10.10.50.35) before running. +set -euo pipefail + +ENV_FILE="/etc/moneyflix/moneyflix.env" +APP_DIR="/opt/moneyflix" +GITEA_REPO="${GITEA_REPO:-https://git.bitreport.pt/BitReport.pt/MoneyFlix.git}" +GITEA_BRANCH="${GITEA_BRANCH:-master}" +CRM_PORT="${CRM_PORT:-8080}" +REDIRECTOR_PORT="${REDIRECTOR_PORT:-3000}" + +# --- Validate credentials file exists and is non-empty --- +if [[ ! -f "$ENV_FILE" ]]; then + echo "ERROR: $ENV_FILE not found." >&2 + echo "Populate it from Infisical (CT 235, 10.10.50.35) before running this script." >&2 + echo "See docs/runbooks/MONEYFLIX_LXC_DEPLOY_BIT24694.md § Secrets setup." >&2 + exit 1 +fi + +# Ensure no weak defaults remain in the env file +if grep -qE '^(DB_PASS|DB_PASSWORD|REDIRECTS_API_KEY|CRM_API_KEY)=($|change-me|placeholder|default|your_)' "$ENV_FILE"; then + echo "ERROR: $ENV_FILE contains placeholder/default credential values." >&2 + echo "Replace all placeholder values with real secrets from vault before running." >&2 + exit 1 +fi + +chmod 640 "$ENV_FILE" + +# shellcheck disable=SC1090 +source "$ENV_FILE" + +echo "=== MoneyFlix stack install (BIT-24694) ===" +echo "App dir: $APP_DIR" +echo "Repo: $GITEA_REPO ($GITEA_BRANCH)" +echo "" + +# --- System update + base deps --- +echo "[1/8] System update + base packages..." +export DEBIAN_FRONTEND=noninteractive +apt-get update -qq +apt-get install -y -qq \ + git curl wget ca-certificates gnupg lsb-release \ + software-properties-common unzip + +# --- MariaDB --- +echo "[2/8] Installing MariaDB..." +apt-get install -y -qq mariadb-server + +systemctl enable --now mariadb + +# Secure MariaDB: set root password and create app DB/user from env +mariadb -u root </dev/null 2>&1 +apt-get update -qq +apt-get install -y -qq \ + php8.1-fpm php8.1-cli php8.1-mysql php8.1-mbstring \ + php8.1-xml php8.1-curl php8.1-zip php8.1-intl php8.1-bcmath + +# Install Composer +if ! command -v composer &>/dev/null; then + curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +fi + +# --- nginx --- +echo "[4/8] Installing nginx..." +apt-get install -y -qq nginx + +# --- Node.js 20 LTS --- +echo "[5/8] Installing Node.js 20 LTS..." +curl -fsSL https://deb.nodesource.com/setup_20.x | bash - >/dev/null 2>&1 +apt-get install -y -qq nodejs + +# Install PM2 for process management +npm install -g pm2 --silent + +# --- Clone / update repo --- +echo "[6/8] Cloning MoneyFlix repo..." +if [[ -d "$APP_DIR/.git" ]]; then + git -C "$APP_DIR" fetch origin "$GITEA_BRANCH" + git -C "$APP_DIR" reset --hard "origin/$GITEA_BRANCH" + echo " Repo updated." +else + git clone --branch "$GITEA_BRANCH" --depth 1 "$GITEA_REPO" "$APP_DIR" + echo " Repo cloned." +fi + +# --- CRM: composer install + env --- +echo "[7/8] Setting up CRM..." +cd "$APP_DIR/crm" +composer install --no-interaction --prefer-dist --no-dev --quiet + +# Write CRM .env from vault env vars +cat > "$APP_DIR/.env" < "$APP_DIR/nodejs-redirector/.env" < /etc/nginx/sites-available/moneyflix-redirector < /etc/nginx/sites-available/moneyflix-crm < /etc/systemd/system/moneyflix-redirector.service </dev/null || true)" + local code="${out%%$'\n'*}" # first line = HTTP code when using -w '%{http_code}' + + if echo "$out" | grep -qE "$expect"; then + PASS=$((PASS + 1)) + RESULTS+=("PASS $name") + printf "PASS %s\n" "$name" + else + FAIL=$((FAIL + 1)) + RESULTS+=("FAIL $name") + printf "FAIL %s\n" "$name" + printf " expected: %s\n" "$expect" + printf " got: %s\n" "$(echo "$out" | head -c 300)" + fi +} + +echo "=== MoneyFlix prod smoke (BIT-24694) ===" +echo "Target CT: $CT_IP" +echo "CRM port: $CRM_PORT | Redirector port: $REDIRECTOR_PORT" +echo "" + +# 1. Redirector health +check "redirector /health" \ + "curl -sf --max-time $TIMEOUT http://${CT_IP}:${REDIRECTOR_PORT}/health" \ + '"status"[[:space:]]*:[[:space:]]*"healthy"' + +# 2. Redirector status +check "redirector /status" \ + "curl -sf --max-time $TIMEOUT http://${CT_IP}:${REDIRECTOR_PORT}/status" \ + '"status"[[:space:]]*:[[:space:]]*"running"' + +# 3. Redirector DB connection +check "redirector /test-db" \ + "curl -sf --max-time $TIMEOUT http://${CT_IP}:${REDIRECTOR_PORT}/test-db" \ + '"status"[[:space:]]*:[[:space:]]*"ok"' + +# 4. CRM system status +check "CRM /system/status" \ + "curl -sf --max-time $TIMEOUT http://${CT_IP}:${CRM_PORT}/system/status" \ + '"status"[[:space:]]*:[[:space:]]*"OK"' + +# 5. nginx responding on :80 (redirector proxy) +HTTP_CODE="$(curl -s -o /dev/null -w '%{http_code}' --max-time "$TIMEOUT" \ + "http://${CT_IP}:${REDIRECTOR_PORT}/" 2>/dev/null || echo 000)" +if [[ "$HTTP_CODE" =~ ^(200|301|302|404)$ ]]; then + PASS=$((PASS + 1)) + RESULTS+=("PASS nginx :${REDIRECTOR_PORT} responding (HTTP $HTTP_CODE)") + printf "PASS nginx :%s responding (HTTP %s)\n" "$REDIRECTOR_PORT" "$HTTP_CODE" +else + FAIL=$((FAIL + 1)) + RESULTS+=("FAIL nginx :${REDIRECTOR_PORT} (HTTP $HTTP_CODE)") + printf "FAIL nginx :%s (HTTP %s)\n" "$REDIRECTOR_PORT" "$HTTP_CODE" +fi + +# 6. MariaDB reachable (via redirector /test-db already covers this, extra check via nc) +if command -v nc &>/dev/null; then + if nc -z -w3 "$CT_IP" 3306 2>/dev/null; then + PASS=$((PASS + 1)) + RESULTS+=("PASS MariaDB :3306 reachable (LAN)") + echo "PASS MariaDB :3306 reachable (LAN)" + else + # Not necessarily an error — MariaDB may be firewalled externally + RESULTS+=("INFO MariaDB :3306 not reachable from this host (firewall expected)") + echo "INFO MariaDB :3306 not reachable from this host (firewall expected)" + fi +fi + +echo "" +echo "=== Summary: $PASS passed, $FAIL failed ===" +printf '%s\n' "${RESULTS[@]}" + +if [[ "$FAIL" -gt 0 ]]; then + echo "" + echo "Troubleshoot:" + echo " ssh root@$CT_IP 'systemctl status moneyflix-redirector nginx php8.1-fpm mariadb'" + echo " ssh root@$CT_IP 'journalctl -u moneyflix-redirector -n 50'" + exit 1 +fi + +echo "" +echo "All smoke checks passed." +echo "Next: configure NPM CT 102 proxy hosts — see docs/runbooks/MONEYFLIX_LXC_DEPLOY_BIT24694.md § NPM"