171 lines
5.1 KiB
Bash
Executable File
171 lines
5.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Deploy Frontend Container on Proxmox VE
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
CONFIG_FILE="$SCRIPT_DIR/config/dapp.conf"
|
|
|
|
# Load configuration
|
|
if [[ -f "$CONFIG_FILE" ]]; then
|
|
source "$CONFIG_FILE"
|
|
fi
|
|
|
|
# Default values
|
|
VMID="${VMID_FRONTEND:-3000}"
|
|
HOSTNAME="${HOSTNAME:-solace-frontend}"
|
|
IP_ADDRESS="${FRONTEND_IP:-192.168.11.60}"
|
|
MEMORY="${FRONTEND_MEMORY:-2048}"
|
|
CORES="${FRONTEND_CORES:-2}"
|
|
DISK="${FRONTEND_DISK:-20}"
|
|
|
|
# Application configuration
|
|
FRONTEND_PORT="${FRONTEND_PORT:-3000}"
|
|
PROJECT_ROOT="${PROJECT_ROOT:-/home/intlc/projects/solace-bg-dubai}"
|
|
|
|
echo "=========================================="
|
|
echo "Deploying Frontend Container"
|
|
echo "=========================================="
|
|
echo "VMID: $VMID"
|
|
echo "Hostname: $HOSTNAME"
|
|
echo "IP: $IP_ADDRESS"
|
|
echo "Memory: ${MEMORY}MB"
|
|
echo "Cores: $CORES"
|
|
echo "Disk: ${DISK}GB"
|
|
echo ""
|
|
|
|
# Check if running on Proxmox host
|
|
if ! command -v pct &> /dev/null; then
|
|
echo "ERROR: This script must be run on Proxmox host (pct command not found)"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if container already exists
|
|
if pct list | grep -q "^\s*$VMID\s"; then
|
|
echo "Container $VMID already exists. Skipping creation."
|
|
echo "To recreate, delete the container first: pct destroy $VMID"
|
|
else
|
|
echo "Creating container $VMID..."
|
|
pct create "$VMID" \
|
|
"${CONTAINER_OS_TEMPLATE:-local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst}" \
|
|
--storage "${PROXMOX_STORAGE:-local-lvm}" \
|
|
--hostname "$HOSTNAME" \
|
|
--memory "$MEMORY" \
|
|
--cores "$CORES" \
|
|
--rootfs "${PROXMOX_STORAGE:-local-lvm}:${DISK}" \
|
|
--net0 "bridge=${PROXMOX_BRIDGE:-vmbr0},name=eth0,ip=${IP_ADDRESS}/24,gw=${GATEWAY:-192.168.11.1},type=veth" \
|
|
--unprivileged "${CONTAINER_UNPRIVILEGED:-1}" \
|
|
--swap "${CONTAINER_SWAP:-512}" \
|
|
--onboot "${CONTAINER_ONBOOT:-1}" \
|
|
--timezone "${CONTAINER_TIMEZONE:-UTC}" \
|
|
--features nesting=1,keyctl=1
|
|
|
|
echo "Container $VMID created successfully"
|
|
fi
|
|
|
|
# Start container
|
|
echo "Starting container $VMID..."
|
|
pct start "$VMID" || true
|
|
|
|
# Wait for container to be ready
|
|
echo "Waiting for container to be ready..."
|
|
sleep 5
|
|
for i in {1..30}; do
|
|
if pct exec "$VMID" -- test -f /etc/os-release 2>/dev/null; then
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
# Install Node.js and pnpm
|
|
echo "Installing Node.js and pnpm..."
|
|
pct exec "$VMID" -- bash -c "
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get update
|
|
apt-get install -y curl
|
|
|
|
# Install Node.js 18
|
|
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
|
|
apt-get install -y nodejs
|
|
|
|
# Install pnpm
|
|
npm install -g pnpm
|
|
"
|
|
|
|
# Create application directory
|
|
echo "Setting up application directory..."
|
|
pct exec "$VMID" -- bash -c "
|
|
mkdir -p /opt/solace-frontend
|
|
chown -R 1000:1000 /opt/solace-frontend
|
|
"
|
|
|
|
# Copy frontend code to container
|
|
echo "Copying frontend code to container..."
|
|
if [[ -d "$PROJECT_ROOT/frontend" ]]; then
|
|
# Remove existing directory if it exists
|
|
pct exec "$VMID" -- bash -c "rm -rf /opt/solace-frontend/* /opt/solace-frontend/.* 2>/dev/null || true"
|
|
# Copy files using tar for better directory handling
|
|
cd "$PROJECT_ROOT"
|
|
tar czf - frontend | pct exec "$VMID" -- bash -c "cd /opt && tar xzf - && mv frontend/* solace-frontend/ && mv frontend/.* solace-frontend/ 2>/dev/null || true && rmdir frontend 2>/dev/null || true"
|
|
else
|
|
echo "WARNING: Frontend directory not found at $PROJECT_ROOT/frontend"
|
|
echo "You will need to copy the code manually or clone the repository"
|
|
fi
|
|
|
|
# Install dependencies and build
|
|
echo "Installing dependencies and building..."
|
|
pct exec "$VMID" -- bash -c "
|
|
cd /opt/solace-frontend
|
|
export NODE_ENV=production
|
|
pnpm install --frozen-lockfile
|
|
pnpm run build
|
|
"
|
|
|
|
# Create systemd service
|
|
echo "Creating systemd service..."
|
|
pct exec "$VMID" -- bash -c "
|
|
cat > /etc/systemd/system/solace-frontend.service <<'EOF'
|
|
[Unit]
|
|
Description=Solace Treasury Frontend
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=root
|
|
WorkingDirectory=/opt/solace-frontend
|
|
Environment=NODE_ENV=production
|
|
EnvironmentFile=/opt/solace-frontend/.env.production
|
|
ExecStart=/usr/bin/node /opt/solace-frontend/node_modules/.bin/next start
|
|
Restart=always
|
|
RestartSec=10
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
systemctl daemon-reload
|
|
systemctl enable solace-frontend
|
|
"
|
|
|
|
# Create log directory
|
|
pct exec "$VMID" -- bash -c "
|
|
mkdir -p /var/log/nextjs
|
|
chmod 755 /var/log/nextjs
|
|
"
|
|
|
|
echo ""
|
|
echo "=========================================="
|
|
echo "Frontend Container Deployment Complete"
|
|
echo "=========================================="
|
|
echo "Container: $VMID ($HOSTNAME)"
|
|
echo "IP Address: $IP_ADDRESS"
|
|
echo "Port: $FRONTEND_PORT"
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo "1. Copy frontend/.env.production to container: pct push $VMID frontend/.env.production /opt/solace-frontend/.env.production"
|
|
echo "2. Update .env.production with Chain 138 RPC URL and contract addresses"
|
|
echo "3. Start the service: pct exec $VMID -- systemctl start solace-frontend"
|
|
echo "4. Check status: pct exec $VMID -- systemctl status solace-frontend"
|
|
|