#!/bin/bash # =========================================== # Domain Check API - Deployment Script # =========================================== # Usage: ./deploy.sh [install|update|status|logs] # =========================================== set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # Configuration REPO_URL="/ai_platform/modules/domain_check" INSTALL_DIR="/home/$(whoami)/domain-check" BRANCH="main" log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } check_requirements() { log_info "Checking requirements..." # Check Docker if ! command -v docker &> /dev/null; then log_error "Docker is not installed. Install it first:" echo " curl -fsSL https://get.docker.com | sh" echo " sudo usermod -aG docker \$USER" exit 1 fi # Check Docker Compose if ! docker compose version &> /dev/null; then log_error "Docker Compose is not installed." exit 1 fi # Check Git if ! command -v git &> /dev/null; then log_error "Git is not installed. Install it first:" echo " sudo apt install git -y" exit 1 fi log_info "All requirements met ✓" } install() { log_info "Installing Domain Check API..." check_requirements # Clone repository if [ -d "$INSTALL_DIR" ]; then log_warn "Directory $INSTALL_DIR already exists." read -p "Remove and reinstall? (y/N) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then rm -rf "$INSTALL_DIR" else log_info "Use './deploy.sh update' to update existing installation." exit 0 fi fi log_info "Cloning repository..." git clone "$REPO_URL" "$INSTALL_DIR" cd "$INSTALL_DIR" # Create .env file create_env # Build and start log_info "Building and starting containers..." docker compose up -d --build # Wait for health log_info "Waiting for services to be healthy..." sleep 10 # Check status status log_info "Installation complete!" log_info "API available at: http://$(hostname -I | awk '{print $1}'):51000/" } create_env() { log_info "Creating .env file..." if [ -f ".env" ]; then log_warn ".env file already exists. Skipping creation." return fi # Generate secure password and secret key DB_PASS=$(openssl rand -base64 24 | tr -dc 'a-zA-Z0-9' | head -c 24) SECRET=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | head -c 32) cat > .env << EOF # =========================================== # Domain Check API - Environment Configuration # Port Schema: Dev (5xxxx) per Port_Allocation_Schema_v2 # Generated: $(date) # =========================================== # Database Configuration (52000 = x2xxx = Databases/PostgreSQL) DB_PASSWORD=${DB_PASS} DB_USER=dns_admin DB_NAME=domain_check DB_HOST=domain_check_postgres DB_PORT=5432 DB_EXTERNAL_PORT=52000 # Redis Configuration (52300 = x23xx = Redis) REDIS_HOST=domain_check_redis REDIS_PORT=6379 REDIS_EXTERNAL_PORT=52300 REDIS_DB=0 REDIS_PASSWORD= # API Keys (set your own keys) WHOXY_API_KEY= VIRUSTOTAL_API_KEY= # Application Settings FLASK_ENV=production FLASK_APP=run.py SECRET_KEY=${SECRET} LOG_LEVEL=INFO DEBUG=False # Cache TTL (seconds) REDIS_TTL_HOT=21600 REDIS_TTL_WARM=86400 REDIS_TTL_COLD=604800 # Risk Score Thresholds RISK_THRESHOLD_LOW=30 RISK_THRESHOLD_MEDIUM=60 RISK_THRESHOLD_HIGH=85 # Domain Age Thresholds (days) DOMAIN_AGE_CRITICAL=90 DOMAIN_AGE_HIGH=180 DOMAIN_AGE_MEDIUM=365 # API Rate Limiting WHOXY_DAILY_LIMIT=8000 VIRUSTOTAL_DAILY_LIMIT=500 # Batch Processing BATCH_CHUNK_SIZE=50 BATCH_TIMEOUT_SECONDS=300 # API Configuration (51000 = x1xxx = API/Gateway) API_HOST=0.0.0.0 API_PORT=51000 API_VERSION=v1 # Celery Configuration CELERY_BROKER_URL=redis://domain_check_redis:6379/1 CELERY_RESULT_BACKEND=redis://domain_check_redis:6379/2 # Monitoring (optional) SENTRY_DSN= DATADOG_API_KEY= EOF log_info ".env file created with secure passwords." log_warn "Edit .env to add your API keys (WHOXY_API_KEY, etc.)" } update() { log_info "Updating Domain Check API..." cd "$INSTALL_DIR" || { log_error "Installation not found at $INSTALL_DIR"; exit 1; } # Pull latest changes log_info "Pulling latest changes..." git pull origin "$BRANCH" # Rebuild and restart log_info "Rebuilding containers..." docker compose down docker compose up -d --build # Wait and check sleep 10 status log_info "Update complete!" } status() { log_info "Checking status..." cd "$INSTALL_DIR" 2>/dev/null || { log_error "Installation not found"; exit 1; } echo "" docker compose ps echo "" # Health check API_PORT=$(grep API_PORT .env | cut -d= -f2) API_PORT=${API_PORT:-51000} if curl -s "http://localhost:${API_PORT}/health" | grep -q "healthy"; then log_info "API Health: ✓ Healthy" curl -s "http://localhost:${API_PORT}/health" | python3 -m json.tool 2>/dev/null || true else log_error "API Health: ✗ Not responding" fi } logs() { cd "$INSTALL_DIR" 2>/dev/null || { log_error "Installation not found"; exit 1; } if [ -n "$2" ]; then docker compose logs -f "$2" else docker compose logs -f fi } stop() { log_info "Stopping services..." cd "$INSTALL_DIR" 2>/dev/null || { log_error "Installation not found"; exit 1; } docker compose down log_info "Services stopped." } start() { log_info "Starting services..." cd "$INSTALL_DIR" 2>/dev/null || { log_error "Installation not found"; exit 1; } docker compose up -d sleep 5 status } restart() { stop start } # Main case "${1:-}" in install) install ;; update) update ;; status) status ;; logs) logs "$@" ;; stop) stop ;; start) start ;; restart) restart ;; *) echo "Domain Check API - Deployment Script" echo "" echo "Usage: $0 {install|update|status|logs|start|stop|restart}" echo "" echo "Commands:" echo " install - Fresh installation" echo " update - Pull latest changes and rebuild" echo " status - Check services status" echo " logs - View logs (optional: service name)" echo " start - Start services" echo " stop - Stop services" echo " restart - Restart services" echo "" echo "Example:" echo " $0 install" echo " $0 logs domain_check_api" ;; esac