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
This commit is contained in:
Xika committed 2026-06-08 13:14:25 +00:00
1 parent 1c67b0744f
commit e570468610
2 files changed
+350

No files matched your search

+247
View File
@@ -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 <vmid> 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 <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '${DB_ROOT_PASS}';
CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'localhost';
FLUSH PRIVILEGES;
SQL
echo " MariaDB: database ${DB_NAME}, user ${DB_USER} created."
# --- PHP 8.1 + php-fpm ---
echo "[3/8] Installing PHP 8.1 + php-fpm..."
add-apt-repository -y ppa:ondrej/php >/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" <<EOF
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=${DB_NAME}
DB_USER=${DB_USER}
DB_PASS=${DB_PASS}
DB_CHARSET=utf8mb4
REDIRECTS_API_KEY=${REDIRECTS_API_KEY}
APP_ENV=production
APP_DEBUG=false
APP_URL=http://127.0.0.1:${CRM_PORT}
APP_TIMEZONE=Europe/Lisbon
EOF
chmod 640 "$APP_DIR/.env"
# Apply DB schema if not already applied
if ! mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e "SHOW TABLES LIKE 'redirects';" | grep -q redirects; then
if [[ -f "$APP_DIR/tools/lab/schema.sql" ]]; then
mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" < "$APP_DIR/tools/lab/schema.sql"
echo " DB schema applied."
else
echo " WARNING: schema.sql not found — apply manually."
fi
fi
mkdir -p "$APP_DIR/storage/logs" "$APP_DIR/storage/cache"
chown -R www-data:www-data "$APP_DIR/storage"
chmod -R 775 "$APP_DIR/storage"
# --- Redirector: npm install + env ---
echo " Setting up Node.js redirector..."
cd "$APP_DIR/nodejs-redirector"
npm ci --omit=dev --silent
cat > "$APP_DIR/nodejs-redirector/.env" <<EOF
NODE_ENV=production
PORT=${REDIRECTOR_PORT}
CRM_API_URL=http://127.0.0.1:${CRM_PORT}/api/redirects
CRM_API_KEY=${REDIRECTS_API_KEY}
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASS}
DB_DATABASE=${DB_NAME}
QUEUE_MAX_SIZE=1000
QUEUE_FLUSH_INTERVAL=5000
TZ=Europe/Lisbon
EOF
chmod 640 "$APP_DIR/nodejs-redirector/.env"
# --- nginx config ---
echo "[8/8] Configuring nginx + systemd units..."
# Redirector vhost (:80) — main redirect traffic
cat > /etc/nginx/sites-available/moneyflix-redirector <<NGINX
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:${REDIRECTOR_PORT};
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_read_timeout 10s;
}
}
NGINX
# CRM vhost (:8080) — admin panel (internal, accessed via NPM)
cat > /etc/nginx/sites-available/moneyflix-crm <<NGINX
server {
listen 8080;
server_name _;
root ${APP_DIR}/crm;
index index.php;
location / {
try_files \$uri \$uri/ /index.php\$is_args\$args;
}
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|ico|gif|svg|woff2?|ttf)\$ {
expires 1d;
access_log off;
}
location ~ /\. { deny all; }
location ~ \.env$ { deny all; }
}
NGINX
ln -sf /etc/nginx/sites-available/moneyflix-redirector /etc/nginx/sites-enabled/
ln -sf /etc/nginx/sites-available/moneyflix-crm /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl enable --now nginx
systemctl reload nginx
# --- systemd unit for Node.js redirector ---
cat > /etc/systemd/system/moneyflix-redirector.service <<UNIT
[Unit]
Description=MoneyFlix Node.js Redirector
After=network.target mariadb.service
Requires=mariadb.service
[Service]
Type=simple
User=www-data
WorkingDirectory=${APP_DIR}/nodejs-redirector
EnvironmentFile=${APP_DIR}/nodejs-redirector/.env
ExecStart=/usr/bin/node ${APP_DIR}/nodejs-redirector/index.mjs
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=moneyflix-redirector
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now moneyflix-redirector
echo ""
echo "=== Install complete ==="
echo " CRM (admin): http://$(hostname -I | awk '{print $1}'):${CRM_PORT}/"
echo " Redirector: http://$(hostname -I | awk '{print $1}'):80/"
echo " Logs: journalctl -u moneyflix-redirector -f"
echo " DB: mariadb -u ${DB_USER} -p${DB_PASS} ${DB_NAME}"
echo ""
echo "Next: bash /opt/moneyflix/tools/proxmox/smoke_moneyflix_prod.sh"
echo " Configure NPM CT 102 proxy hosts (see runbook)"
+103
View File
@@ -0,0 +1,103 @@
#!/usr/bin/env bash
# Post-deploy smoke test for MoneyFlix production LXC (BIT-24694).
# Run from the Paperclip host (10.10.50.17) or any host on VLAN 1050.
# Usage: CT_IP=10.10.50.37 bash tools/proxmox/smoke_moneyflix_prod.sh
set -euo pipefail
CT_IP="${CT_IP:-10.10.50.37}"
CRM_PORT="${CRM_PORT:-8080}"
REDIRECTOR_PORT="${REDIRECTOR_PORT:-80}"
TIMEOUT="${TIMEOUT:-10}"
PASS=0
FAIL=0
RESULTS=()
check() {
local name="$1"
local cmd="$2"
local expect="${3:-200}"
local out
out="$(eval "$cmd" 2>/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"