Ratan1870/code-execution
0
1#!/bin/bash2# ╔══════════════════════════════════════════════════════════════════╗3# ║ Code Execution Engine — Setup Script ║4# ║ Run: chmod +x scripts/setup.sh && bash scripts/setup.sh ║5# ║ Target: Oracle Cloud 1GB AMD Instance (Ubuntu/Oracle Linux) ║6# ╚══════════════════════════════════════════════════════════════════╝7 8set -e9 10BOLD='\033[1m'11GREEN='\033[0;32m'12RED='\033[0;31m'13YELLOW='\033[0;33m'14CYAN='\033[0;36m'15NC='\033[0m'16 17echo -e "${BOLD}${CYAN}"18echo "══════════════════════════════════════════════════"19echo " Code Execution Engine — Setup"20echo " Optimized for 1GB Oracle AMD Instance"21echo "══════════════════════════════════════════════════"22echo -e "${NC}"23 24# ─── Step 1: Check Docker ─────────────────────25echo -e "${BOLD}[1/5] Checking Docker...${NC}"26if ! command -v docker &> /dev/null; then27 echo -e "${RED}❌ Docker is not installed!${NC}"28 echo ""29 echo "Install Docker with:"30 echo " curl -fsSL https://get.docker.com | sh"31 echo " sudo usermod -aG docker \$USER"32 echo " newgrp docker"33 exit 134fi35 36if ! docker info &> /dev/null 2>&1; then37 echo -e "${RED}❌ Docker daemon is not running!${NC}"38 echo ""39 echo "Start Docker with:"40 echo " sudo systemctl start docker"41 echo " sudo systemctl enable docker"42 exit 143fi44 45DOCKER_VERSION=$(docker version --format '{{.Server.Version}}' 2>/dev/null)46echo -e "${GREEN}✅ Docker is available (v${DOCKER_VERSION})${NC}"47 48# ─── Step 2: Check Node.js ────────────────────49echo -e "\n${BOLD}[2/5] Checking Node.js...${NC}"50if ! command -v node &> /dev/null; then51 echo -e "${RED}❌ Node.js is not installed!${NC}"52 echo ""53 echo "Install Node.js with:"54 echo " curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -"55 echo " sudo apt-get install -y nodejs"56 exit 157fi58 59NODE_VERSION=$(node --version)60echo -e "${GREEN}✅ Node.js is available (${NODE_VERSION})${NC}"61 62# ─── Step 3: Install npm dependencies ─────────63echo -e "\n${BOLD}[3/5] Installing npm dependencies...${NC}"64npm install --production65echo -e "${GREEN}✅ Dependencies installed${NC}"66 67# ─── Step 4: Build sandbox Docker image ───────68echo -e "\n${BOLD}[4/5] Building sandbox Docker image...${NC}"69echo " This will take 1-2 minutes on first run..."70 71SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"72PROJECT_DIR="$(dirname "$SCRIPT_DIR")"73SANDBOX_DIR="$PROJECT_DIR/sandbox"74 75docker build -t judge-sandbox "$SANDBOX_DIR"76echo -e "${GREEN}✅ Sandbox image built (judge-sandbox)${NC}"77 78# ─── Step 5: Create .env if not exists ────────79echo -e "\n${BOLD}[5/5] Checking configuration...${NC}"80ENV_FILE="$PROJECT_DIR/.env"81 82if [ ! -f "$ENV_FILE" ]; then83 cat > "$ENV_FILE" << 'EOF'84# ─── Server ───────────────────────────────────85PORT=300086NODE_ENV=production87 88# ─── Execution Limits ─────────────────────────89MAX_CONCURRENT=290DEFAULT_TIME_LIMIT=591DEFAULT_MEMORY_LIMIT=25692MAX_TIME_LIMIT=1093MAX_MEMORY_LIMIT=51294MAX_CODE_SIZE=6553695 96# ─── Docker ───────────────────────────────────97SANDBOX_IMAGE=judge-sandbox98TEMP_DIR=/tmp/judge99 100# ─── Rate Limiting ────────────────────────────101RATE_LIMIT_WINDOW=60000102RATE_LIMIT_MAX=30103EOF104 echo -e "${GREEN}✅ Created .env file with defaults${NC}"105else106 echo -e "${GREEN}✅ .env file already exists${NC}"107fi108 109# ─── Create temp directory ────────────────────110mkdir -p /tmp/judge111 112# ─── Done! ────────────────────────────────────113echo -e "\n${BOLD}${CYAN}"114echo "══════════════════════════════════════════════════"115echo " ✅ Setup Complete!"116echo ""117echo " Start the server:"118echo " npm start (production)"119echo " npm run dev (development with auto-reload)"120echo ""121echo " API Endpoints:"122echo " GET /api/health Health check + metrics"123echo " GET /api/languages Supported languages"124echo " POST /api/execute Execute code"125echo " POST /api/judge Judge with test cases"126echo " POST /api/batch-judge Batch judge"127echo ""128echo " Quick test:"129echo " curl http://localhost:3000/api/health"130echo "══════════════════════════════════════════════════"131echo -e "${NC}"132 