#!/bin/bash # CUDA Quantum MCP Server - Build and Deployment Script # Automates the complete setup and deployment process set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}ℹ️ $1${NC}" } log_success() { echo -e "${GREEN}✅ $1${NC}" } log_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } log_error() { echo -e "${RED}❌ $1${NC}" } # Check if command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # System requirements check check_requirements() { log_info "Checking system requirements..." # Check Node.js if command_exists node; then NODE_VERSION=$(node --version) log_success "Node.js found: $NODE_VERSION" else log_error "Node.js not found. Please install Node.js 18 or later." exit 1 fi # Check Python if command_exists python3; then PYTHON_VERSION=$(python3 --version) log_success "Python found: $PYTHON_VERSION" else log_error "Python 3 not found. Please install Python 3.8 or later." exit 1 fi # Check npm if command_exists npm; then NPM_VERSION=$(npm --version) log_success "npm found: $NPM_VERSION" else log_error "npm not found. Please install npm." exit 1 fi # Check for GPU (optional) if command_exists nvidia-smi; then log_success "NVIDIA GPU detected" nvidia-smi --query-gpu=name --format=csv,noheader,nounits | head -1 else log_warning "No NVIDIA GPU detected. GPU acceleration will not be available." fi log_success "System requirements check completed" } # Install CUDA Quantum install_cudaq() { log_info "Installing CUDA Quantum..." # Check if CUDA Quantum is already installed if python3 -c "import cudaq" 2>/dev/null; then log_success "CUDA Quantum already installed" return 0 fi # Install CUDA Quantum log_info "Installing CUDA Quantum via pip..." python3 -m pip install cudaq --user # Verify installation if python3 -c "import cudaq" 2>/dev/null; then log_success "CUDA Quantum installed successfully" else log_error "Failed to install CUDA Quantum" exit 1 fi } # Install Node.js dependencies install_node_deps() { log_info "Installing Node.js dependencies..." if [ ! -f "package.json" ]; then log_error "package.json not found. Are you in the right directory?" exit 1 fi npm install log_success "Node.js dependencies installed" } # Install Python dependencies install_python_deps() { log_info "Installing additional Python dependencies..." # Install common scientific computing packages python3 -m pip install --user numpy scipy matplotlib jupyter notebook log_success "Python dependencies installed" } # Build TypeScript build_typescript() { log_info "Building TypeScript..." npm run build if [ -d "dist" ]; then log_success "TypeScript build completed" else log_error "TypeScript build failed" exit 1 fi } # Set up environment setup_environment() { log_info "Setting up environment..." # Copy environment template if .env doesn't exist if [ ! -f ".env" ]; then if [ -f ".env.example" ]; then cp .env.example .env log_info "Created .env from .env.example" log_warning "Please edit .env file with your configuration" else log_warning ".env.example not found, creating minimal .env" cat > .env << EOF # CUDA Quantum MCP Server Configuration NODE_ENV=development CUDAQ_DEFAULT_TARGET=qpp-cpu LOG_LEVEL=info CUDAQ_PYTHON_PATH=$(which python3) EOF fi else log_success "Environment file .env already exists" fi # Create logs directory mkdir -p logs log_success "Environment setup completed" } # Run tests run_tests() { log_info "Running tests..." if [ "$1" = "--skip-tests" ]; then log_warning "Skipping tests as requested" return 0 fi # Run unit tests if npm test 2>/dev/null; then log_success "All tests passed" else log_warning "Some tests failed or no tests configured" fi } # Validate installation validate_installation() { log_info "Validating installation..." # Check if main files exist if [ -f "dist/index.js" ]; then log_success "Main server file found" else log_error "Main server file not found" exit 1 fi # Test CUDA Quantum import if python3 -c "import cudaq; print('CUDA Quantum version:', cudaq.__version__ if hasattr(cudaq, '__version__') else 'unknown')" 2>/dev/null; then log_success "CUDA Quantum validation passed" else log_warning "CUDA Quantum validation failed" fi # Test server startup (quick test) log_info "Testing server startup..." timeout 5s node dist/index.js --test 2>/dev/null || log_warning "Server startup test inconclusive" log_success "Installation validation completed" } # Start server start_server() { log_info "Starting CUDA Quantum MCP Server..." if [ "$1" = "--daemon" ]; then nohup npm start > logs/server.log 2>&1 & SERVER_PID=$! echo $SERVER_PID > logs/server.pid log_success "Server started as daemon (PID: $SERVER_PID)" log_info "Logs: tail -f logs/server.log" log_info "Stop: kill $SERVER_PID" else log_success "Starting server in foreground..." npm start fi } # Stop server daemon stop_server() { if [ -f "logs/server.pid" ]; then SERVER_PID=$(cat logs/server.pid) if kill -0 $SERVER_PID 2>/dev/null; then kill $SERVER_PID rm logs/server.pid log_success "Server stopped (PID: $SERVER_PID)" else log_warning "Server process not running" rm -f logs/server.pid fi else log_warning "No server PID file found" fi } # Run integration examples run_examples() { log_info "Running integration examples..." if [ -f "examples/integration_example.py" ]; then python3 examples/integration_example.py else log_warning "Integration examples not found" fi } # Create Docker image build_docker() { log_info "Building Docker image..." if command_exists docker; then docker build -t mcp-quantum:latest . log_success "Docker image built: mcp-quantum:latest" else log_error "Docker not found. Please install Docker to build image." exit 1 fi } # Deploy to production deploy_production() { log_info "Deploying to production..." # Set production environment export NODE_ENV=production # Build with production settings npm run build # Install only production dependencies npm ci --only=production # Set up production environment if [ ! -f ".env.production" ]; then cp .env.example .env.production log_warning "Created .env.production - please configure for production" fi log_success "Production deployment prepared" log_info "Configure .env.production and start with: NODE_ENV=production npm start" } # Help function show_help() { echo "CUDA Quantum MCP Server - Build and Deployment Script" echo echo "Usage: $0 [COMMAND] [OPTIONS]" echo echo "Commands:" echo " setup Complete setup (install deps, build, configure)" echo " install Install all dependencies" echo " build Build TypeScript code" echo " test Run tests" echo " start Start the server" echo " stop Stop server daemon" echo " restart Restart server daemon" echo " status Check server status" echo " examples Run integration examples" echo " docker Build Docker image" echo " deploy Deploy to production" echo " clean Clean build artifacts" echo " help Show this help" echo echo "Options:" echo " --skip-tests Skip running tests" echo " --daemon Start server as daemon" echo " --gpu Enable GPU support check" echo echo "Examples:" echo " $0 setup # Complete setup" echo " $0 start --daemon # Start as daemon" echo " $0 test --skip-tests # Build without tests" echo " $0 deploy # Production deployment" } # Main execution main() { local command="${1:-help}" shift || true case "$command" in setup) log_info "🚀 Starting complete setup..." check_requirements install_cudaq install_node_deps install_python_deps setup_environment build_typescript run_tests "$@" validate_installation log_success "🎉 Setup completed successfully!" log_info "Start the server with: $0 start" ;; install) check_requirements install_cudaq install_node_deps install_python_deps ;; build) build_typescript ;; test) run_tests "$@" ;; start) start_server "$@" ;; stop) stop_server ;; restart) stop_server sleep 2 start_server "$@" ;; status) if [ -f "logs/server.pid" ]; then SERVER_PID=$(cat logs/server.pid) if kill -0 $SERVER_PID 2>/dev/null; then log_success "Server running (PID: $SERVER_PID)" else log_warning "Server PID file exists but process not running" fi else log_info "Server not running" fi ;; examples) run_examples ;; docker) build_docker ;; deploy) deploy_production ;; clean) log_info "Cleaning build artifacts..." rm -rf dist/ node_modules/ logs/ coverage/ log_success "Clean completed" ;; help|--help|-h) show_help ;; *) log_error "Unknown command: $command" show_help exit 1 ;; esac } # Script entry point if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi