rishithayanidhi/datacenter-cooling-optimization
0
1#!/usr/bin/env bash2# Pre-submission validation script for OpenEnv submission3# Tests: 1) HF Space connectivity (or local server), 2) Docker build, 3) openenv validate4 5# Color codes6GREEN='\033[0;32m'7RED='\033[0;31m'8YELLOW='\033[1;33m'9BOLD='\033[1m'10NC='\033[0m' # No Color11 12# Configuration13REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"14DOCKER_BUILD_TIMEOUT=300 # 5 minutes15HF_SPACE_URL="${HF_SPACE_URL:-http://localhost:8000}"16PING_URL="$HF_SPACE_URL"17 18# Helper functions19pass() {20 printf "${GREEN}✓${NC} %s\n" "$1"21}22 23fail() {24 printf "${RED}✗${NC} %s\n" "$1"25}26 27hint() {28 printf " ${YELLOW}→${NC} %s\n" "$1"29}30 31log() {32 printf "%s\n" "$1"33}34 35stop_at() {36 printf "\n"37 printf "${RED}Validation stopped at %s.${NC}\n" "$1"38 exit 139}40 41run_with_timeout() {42 local timeout=$143 shift44 timeout "$timeout" "$@"45}46 47# Main validation script48printf "\n"49printf "${BOLD}========================================${NC}\n"50printf "${BOLD} OpenEnv Pre-Submission Validation${NC}\n"51printf "${BOLD}========================================${NC}\n"52printf "\n"53 54# Step 1: Check HF Space connectivity55log "${BOLD}Step 1/3: Checking HF Space connectivity (or local server)${NC} ..."56log " Pinging: $PING_URL/reset"57 58HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$PING_URL/reset" 2>/dev/null || echo "000")59 60if [ "$HTTP_CODE" = "200" ]; then61 pass "HF Space is live and responds to /reset"62elif [ "$HTTP_CODE" = "000" ]; then63 fail "HF Space not reachable (connection failed or timed out)"64 hint "Check your network connection and that the Space is running."65 hint "Try: curl -s -o /dev/null -w '%{http_code}' -X POST $PING_URL/reset"66 stop_at "Step 1"67else68 fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"69 hint "Make sure your Space is running and the URL is correct."70 hint "Try opening $PING_URL in your browser first."71 stop_at "Step 1"72fi73 74printf "\n"75log "${BOLD}Step 2/3: Running docker build${NC} ..."76 77if ! command -v docker &>/dev/null; then78 fail "docker command not found"79 hint "Install Docker: https://docs.docker.com/get-docker/"80 stop_at "Step 2"81fi82 83# Find Dockerfile84DOCKER_CONTEXT=""85if [ -f "$REPO_DIR/Dockerfile" ]; then86 DOCKER_CONTEXT="$REPO_DIR"87elif [ -f "$REPO_DIR/server/Dockerfile" ]; then88 DOCKER_CONTEXT="$REPO_DIR/server"89else90 fail "No Dockerfile found in repo root or server/ directory"91 stop_at "Step 2"92fi93 94log " Found Dockerfile in $DOCKER_CONTEXT"95 96BUILD_OK=false97BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true98 99if [ "$BUILD_OK" = true ]; then100 pass "Docker build succeeded"101else102 fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"103 printf "%s\n" "$BUILD_OUTPUT" | tail -20104 stop_at "Step 2"105fi106 107printf "\n"108log "${BOLD}Step 3/3: Running openenv validate${NC} ..."109 110if ! command -v openenv &>/dev/null; then111 fail "openenv command not found"112 hint "Install it: pip install openenv-core"113 stop_at "Step 3"114fi115 116VALIDATE_OK=false117VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true118 119if [ "$VALIDATE_OK" = true ]; then120 pass "openenv validate passed"121 [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"122else123 fail "openenv validate failed"124 printf "%s\n" "$VALIDATE_OUTPUT"125 stop_at "Step 3"126fi127 128printf "\n"129printf "${BOLD}========================================${NC}\n"130printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"131printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"132printf "${BOLD}========================================${NC}\n"133printf "\n"134 135exit 0136 