#!/usr/bin/env bash set -Eeuo pipefail APP_DIR="${APP_DIR:-/opt/cmpp-platform}" REPO_URL="${REPO_URL:-http://175.27.255.91:3000/hectorzhao/lislgosms.git}" BRANCH="${BRANCH:-main}" PUBLIC_HTTP_PORT="${PUBLIC_HTTP_PORT:-12026}" API_PORT="${API_PORT:-3000}" GATEWAY_CONTROL_ADDR="${GATEWAY_CONTROL_ADDR:-127.0.0.1:8090}" GATEWAY_CMPP_ADDR="${GATEWAY_CMPP_ADDR:-0.0.0.0:17890}" DB_NAME="${DB_NAME:-cmpp_platform}" DB_USER="${DB_USER:-cmpp}" DB_PASSWORD="${DB_PASSWORD:-$(openssl rand -base64 24 | tr -d '\n')}" MINIO_ROOT_USER="${MINIO_ROOT_USER:-cmpp_minio}" MINIO_ROOT_PASSWORD="${MINIO_ROOT_PASSWORD:-$(openssl rand -base64 32 | tr -d '\n')}" MINIO_BUCKET="${MINIO_BUCKET:-cmpp-platform}" PROD_ADMIN_EMAIL="${PROD_ADMIN_EMAIL:-admin@example.com}" PROD_ADMIN_USERNAME="${PROD_ADMIN_USERNAME:-prod_admin}" PROD_ADMIN_PASSWORD="${PROD_ADMIN_PASSWORD:-$(openssl rand -base64 18 | tr -d '\n')}" if [[ "$(id -u)" -ne 0 ]]; then echo "Run as root." >&2 exit 1 fi log() { printf '\n[%s] %s\n' "$(date '+%F %T')" "$*"; } install_packages() { log "Installing OS packages" if command -v apt-get >/dev/null 2>&1; then apt-get update DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates curl gnupg git nginx redis-server postgresql postgresql-contrib build-essential tar gzip openssl if ! command -v node >/dev/null 2>&1 || [[ "$(node -v | sed 's/^v//' | cut -d. -f1)" -lt 22 ]]; then curl -fsSL https://deb.nodesource.com/setup_22.x | bash - DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs fi elif command -v dnf >/dev/null 2>&1; then dnf install -y ca-certificates curl git nginx redis postgresql-server postgresql-contrib gcc gcc-c++ make tar gzip openssl if [[ ! -d /var/lib/pgsql/data/base ]]; then postgresql-setup --initdb fi if ! command -v node >/dev/null 2>&1 || [[ "$(node -v | sed 's/^v//' | cut -d. -f1)" -lt 22 ]]; then curl -fsSL https://rpm.nodesource.com/setup_22.x | bash - dnf install -y nodejs fi elif command -v yum >/dev/null 2>&1; then yum install -y ca-certificates curl git nginx redis postgresql-server postgresql-contrib gcc gcc-c++ make tar gzip openssl if [[ ! -d /var/lib/pgsql/data/base ]]; then postgresql-setup initdb fi if ! command -v node >/dev/null 2>&1 || [[ "$(node -v | sed 's/^v//' | cut -d. -f1)" -lt 22 ]]; then curl -fsSL https://rpm.nodesource.com/setup_22.x | bash - yum install -y nodejs fi else echo "Unsupported Linux distribution: apt-get/dnf/yum not found." >&2 exit 1 fi } install_go() { local version="${GO_VERSION:-1.26.0}" local arch case "$(uname -m)" in x86_64|amd64) arch="amd64" ;; aarch64|arm64) arch="arm64" ;; *) echo "Unsupported Go architecture: $(uname -m)" >&2; exit 1 ;; esac if command -v go >/dev/null 2>&1 && [[ "$(go version | awk '{print $3}' | sed 's/go//')" == "$version" ]]; then return fi log "Installing Go ${version} for linux-${arch}" curl -fL "https://go.dev/dl/go${version}.linux-${arch}.tar.gz" -o /tmp/go.tar.gz rm -rf /usr/local/go tar -C /usr/local -xzf /tmp/go.tar.gz ln -sf /usr/local/go/bin/go /usr/local/bin/go } install_minio() { local arch case "$(uname -m)" in x86_64|amd64) arch="amd64" ;; aarch64|arm64) arch="arm64" ;; *) echo "Unsupported MinIO architecture: $(uname -m)" >&2; exit 1 ;; esac log "Installing MinIO for linux-${arch}" curl -fL "https://dl.min.io/server/minio/release/linux-${arch}/minio" -o /usr/local/bin/minio chmod +x /usr/local/bin/minio useradd --system --home /var/lib/minio --shell /usr/sbin/nologin minio 2>/dev/null || true mkdir -p /var/lib/minio chown -R minio:minio /var/lib/minio } start_infra() { log "Starting PostgreSQL and Redis" systemctl enable --now postgresql || systemctl enable --now postgresql.service systemctl enable --now redis-server 2>/dev/null || systemctl enable --now redis runuser -u postgres -- psql </etc/cmpp-platform/cmpp-platform.env </etc/cmpp-platform/minio.env </etc/systemd/system/cmpp-minio.service <<'EOF' [Unit] Description=CMPP MinIO object storage After=network.target [Service] User=minio Group=minio EnvironmentFile=/etc/cmpp-platform/minio.env ExecStart=/usr/local/bin/minio server /var/lib/minio --address 127.0.0.1:9000 --console-address 127.0.0.1:9001 Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOF cat >/etc/systemd/system/cmpp-api.service </etc/systemd/system/cmpp-gateway.service </etc/nginx/conf.d/cmpp-platform.conf </root/cmpp-platform-credentials.txt <:${PUBLIC_HTTP_PORT}" echo "Gateway control health: http://127.0.0.1:8090/health" echo "CMPP inbound port variable reserved: ${GATEWAY_CMPP_ADDR}"