SidhaGarg/Cloud-DevOps-RLEnv
0
1#!/usr/bin/env bash2#3# validate-submission.sh — OpenEnv Submission Validator4#5# Checks that your HF Space is live, Docker image builds, and openenv validate passes.6#7# Prerequisites:8# - Docker: https://docs.docker.com/get-docker/9# - openenv-core: pip install openenv-core10# - curl (usually pre-installed)11#12# Run:13# curl -fsSL https://raw.githubusercontent.com/<owner>/<repo>/main/scripts/validate-submission.sh | bash -s -- <ping_url> [repo_dir]14#15# Or download and run locally:16# chmod +x validate-submission.sh17# ./validate-submission.sh <ping_url> [repo_dir]18#19# Arguments:20# ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)21# repo_dir Path to your repo (default: current directory)22#23# Examples:24# ./validate-submission.sh https://my-team.hf.space25# ./validate-submission.sh https://my-team.hf.space ./my-repo26#27 28set -uo pipefail29 30DOCKER_BUILD_TIMEOUT=60031if [ -t 1 ]; then32 RED='\033[0;31m'33 GREEN='\033[0;32m'34 YELLOW='\033[1;33m'35 BOLD='\033[1m'36 NC='\033[0m'37else38 RED='' GREEN='' YELLOW='' BOLD='' NC=''39fi40 41run_with_timeout() {42 local secs="$1"; shift43 if command -v timeout &>/dev/null; then44 timeout "$secs" "$@"45 elif command -v gtimeout &>/dev/null; then46 gtimeout "$secs" "$@"47 else48 "$@" &49 local pid=$!50 ( sleep "$secs" && kill "$pid" 2>/dev/null ) &51 local watcher=$!52 wait "$pid" 2>/dev/null53 local rc=$?54 kill "$watcher" 2>/dev/null55 wait "$watcher" 2>/dev/null56 return $rc57 fi58}59 60portable_mktemp() {61 local prefix="${1:-validate}"62 mktemp "${TMPDIR:-/tmp}/${prefix}-XXXXXX" 2>/dev/null || mktemp63}64 65CLEANUP_FILES=()66cleanup() { rm -f "${CLEANUP_FILES[@]+"${CLEANUP_FILES[@]}"}"; }67trap cleanup EXIT68 69PING_URL="${1:-}"70REPO_DIR="${2:-.}"71 72if [ -z "$PING_URL" ]; then73 printf "Usage: %s <ping_url> [repo_dir]\n" "$0"74 printf "\n"75 printf " ping_url Your HuggingFace Space URL (e.g. https://your-space.hf.space)\n"76 printf " repo_dir Path to your repo (default: current directory)\n"77 exit 178fi79 80if ! REPO_DIR="$(cd "$REPO_DIR" 2>/dev/null && pwd)"; then81 printf "Error: directory '%s' not found\n" "${2:-.}"82 exit 183fi84PING_URL="${PING_URL%/}"85export PING_URL86PASS=087 88log() { printf "[%s] %b\n" "$(date -u +%H:%M:%S)" "$*"; }89pass() { log "${GREEN}PASSED${NC} -- $1"; PASS=$((PASS + 1)); }90fail() { log "${RED}FAILED${NC} -- $1"; }91hint() { printf " ${YELLOW}Hint:${NC} %b\n" "$1"; }92stop_at() {93 printf "\n"94 printf "${RED}${BOLD}Validation stopped at %s.${NC} Fix the above before continuing.\n" "$1"95 exit 196}97 98printf "\n"99printf "${BOLD}========================================${NC}\n"100printf "${BOLD} OpenEnv Submission Validator${NC}\n"101printf "${BOLD}========================================${NC}\n"102log "Repo: $REPO_DIR"103log "Ping URL: $PING_URL"104printf "\n"105 106log "${BOLD}Step 1/3: Pinging HF Space${NC} ($PING_URL/reset) ..."107 108CURL_OUTPUT=$(portable_mktemp "validate-curl")109CLEANUP_FILES+=("$CURL_OUTPUT")110HTTP_CODE=$(curl -s -o "$CURL_OUTPUT" -w "%{http_code}" -X POST \111 -H "Content-Type: application/json" -d '{}' \112 "$PING_URL/reset" --max-time 30 2>"$CURL_OUTPUT" || printf "000")113 114if [ "$HTTP_CODE" = "200" ]; then115 pass "HF Space is live and responds to /reset"116elif [ "$HTTP_CODE" = "000" ]; then117 fail "HF Space not reachable (connection failed or timed out)"118 hint "Check your network connection and that the Space is running."119 hint "Try: curl -s -o /dev/null -w '%%{http_code}' -X POST $PING_URL/reset"120 stop_at "Step 1"121else122 fail "HF Space /reset returned HTTP $HTTP_CODE (expected 200)"123 hint "Make sure your Space is running and the URL is correct."124 hint "Try opening $PING_URL in your browser first."125 stop_at "Step 1"126fi127 128log "${BOLD}Step 2/3: Running docker build${NC} ..."129 130if ! command -v docker &>/dev/null; then131 fail "docker command not found"132 hint "Install Docker: https://docs.docker.com/get-docker/"133 stop_at "Step 2"134fi135 136if [ -f "$REPO_DIR/Dockerfile" ]; then137 DOCKER_CONTEXT="$REPO_DIR"138elif [ -f "$REPO_DIR/server/Dockerfile" ]; then139 DOCKER_CONTEXT="$REPO_DIR/server"140else141 fail "No Dockerfile found in repo root or server/ directory"142 stop_at "Step 2"143fi144 145log " Found Dockerfile in $DOCKER_CONTEXT"146 147BUILD_OK=false148BUILD_OUTPUT=$(run_with_timeout "$DOCKER_BUILD_TIMEOUT" docker build "$DOCKER_CONTEXT" 2>&1) && BUILD_OK=true149 150if [ "$BUILD_OK" = true ]; then151 pass "Docker build succeeded"152else153 fail "Docker build failed (timeout=${DOCKER_BUILD_TIMEOUT}s)"154 printf "%s\n" "$BUILD_OUTPUT" | tail -20155 stop_at "Step 2"156fi157 158log "${BOLD}Step 3/3: Running openenv validate${NC} ..."159 160if ! command -v openenv &>/dev/null; then161 fail "openenv command not found"162 hint "Install it: pip install openenv-core"163 stop_at "Step 3"164fi165 166VALIDATE_OK=false167VALIDATE_OUTPUT=$(cd "$REPO_DIR" && openenv validate 2>&1) && VALIDATE_OK=true168 169if [ "$VALIDATE_OK" = true ]; then170 pass "openenv validate passed"171 [ -n "$VALIDATE_OUTPUT" ] && log " $VALIDATE_OUTPUT"172else173 fail "openenv validate failed"174 printf "%s\n" "$VALIDATE_OUTPUT"175 stop_at "Step 3"176fi177 178printf "\n"179printf "${BOLD}========================================${NC}\n"180printf "${GREEN}${BOLD} All 3/3 checks passed!${NC}\n"181printf "${GREEN}${BOLD} Your submission is ready to submit.${NC}\n"182printf "${BOLD}========================================${NC}\n"183printf "\n"184 185exit 0186 