Files
MoneyFlix/tools/proxmox/install_moneyflix_stack.sh
T
Xika da978374a1 infra(BIT-24694): add PBS DB extraction script and prod dump import support
- tools/proxmox/extract_db_from_pbs.sh: restores a PBS backup to an
  air-gapped temp CT (no net0), dumps MariaDB to a .sql.gz, then destroys
  the temp CT. Handles root no-pass, moneyflix.env creds, and --list mode.
- tools/proxmox/install_moneyflix_stack.sh: adds PROD_DB_DUMP env var;
  when set to a .sql.gz dump from PBS extraction, imports production data
  instead of applying the empty lab schema. Strips DEFINER clauses for
  clean import under a different user.

Paperclip-Issue: BIT-24694
Paperclip-Run: fda0935a-f525-46a5-81a0-6d63da4188cf
2026-06-08 13:30:41 +00:00

270 lines
8.4 KiB
Bash

#!/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}"
# Optional: path to a gzipped mysqldump from PBS extraction (see tools/proxmox/extract_db_from_pbs.sh).
# If set, the production dump is imported instead of applying the empty lab schema.
PROD_DB_DUMP="${PROD_DB_DUMP:-}"
# --- 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 or import production dump
if [[ -n "$PROD_DB_DUMP" ]]; then
if [[ ! -f "$PROD_DB_DUMP" ]]; then
echo "ERROR: PROD_DB_DUMP set but file not found: $PROD_DB_DUMP" >&2
exit 1
fi
echo " Importing production DB dump: $PROD_DB_DUMP"
# Strip DEFINER clauses so the dump imports cleanly under a different user
if [[ "$PROD_DB_DUMP" == *.gz ]]; then
zcat "$PROD_DB_DUMP" \
| sed 's|/\*![0-9]* DEFINER=[^ ]* \*/||g' \
| mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
else
sed 's|/\*![0-9]* DEFINER=[^ ]* \*/||g' "$PROD_DB_DUMP" \
| mariadb -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
fi
echo " Production DB imported."
else
# Apply empty schema when no production dump is available
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 (empty database)."
else
echo " WARNING: schema.sql not found — apply DB schema manually."
fi
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)"