|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Exit on error |
| 4 | +set -e |
| 5 | + |
| 6 | +# Get the directory of the script |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +cd "$SCRIPT_DIR" |
| 9 | + |
| 10 | +# Check if Python is installed |
| 11 | +if ! command -v python3 &> /dev/null; then |
| 12 | + echo "Python 3 is required but not installed. Please install Python 3 and try again." |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Check if Modal is installed |
| 17 | +if ! python3 -c "import modal" &> /dev/null; then |
| 18 | + echo "Modal is not installed. Installing now..." |
| 19 | + pip install modal |
| 20 | +fi |
| 21 | + |
| 22 | +# Check if Modal token is set up |
| 23 | +if ! modal token list &> /dev/null; then |
| 24 | + echo "Modal token not set up. Please run 'modal token new' to set up your Modal token." |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +# Function to deploy a single example |
| 29 | +deploy_example() { |
| 30 | + local example_dir="$1" |
| 31 | + local example_name=$(basename "$example_dir") |
| 32 | + |
| 33 | + if [ -f "$example_dir/deploy.sh" ]; then |
| 34 | + echo "Deploying $example_name..." |
| 35 | + (cd "$example_dir" && bash deploy.sh) |
| 36 | + return $? |
| 37 | + else |
| 38 | + echo "No deploy.sh script found for $example_name. Skipping." |
| 39 | + return 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +# Find all examples with deploy.sh scripts |
| 44 | +examples=() |
| 45 | +for dir in "$SCRIPT_DIR"/*/; do |
| 46 | + if [ -f "${dir}deploy.sh" ]; then |
| 47 | + examples+=("$(basename "$dir")") |
| 48 | + fi |
| 49 | +done |
| 50 | + |
| 51 | +if [ ${#examples[@]} -eq 0 ]; then |
| 52 | + echo "No deployable examples found." |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +# Display menu |
| 57 | +echo "Available examples for deployment:" |
| 58 | +echo "" |
| 59 | + |
| 60 | +for i in "${!examples[@]}"; do |
| 61 | + echo "[$((i+1))] ${examples[$i]}" |
| 62 | +done |
| 63 | + |
| 64 | +echo "" |
| 65 | +echo "[a] Deploy all examples" |
| 66 | +echo "[q] Quit" |
| 67 | +echo "" |
| 68 | + |
| 69 | +# Get user selection |
| 70 | +selected_indices=() |
| 71 | +while true; do |
| 72 | + read -p "Select examples to deploy (e.g., '1 3 5' or 'a' for all, 'q' to quit, 'd' when done): " selection |
| 73 | + |
| 74 | + if [ "$selection" == "q" ]; then |
| 75 | + echo "Exiting without deployment." |
| 76 | + exit 0 |
| 77 | + elif [ "$selection" == "a" ]; then |
| 78 | + for i in "${!examples[@]}"; do |
| 79 | + selected_indices+=($i) |
| 80 | + done |
| 81 | + break |
| 82 | + elif [ "$selection" == "d" ]; then |
| 83 | + if [ ${#selected_indices[@]} -eq 0 ]; then |
| 84 | + echo "No examples selected. Please select at least one example." |
| 85 | + else |
| 86 | + break |
| 87 | + fi |
| 88 | + else |
| 89 | + # Parse space-separated numbers |
| 90 | + for num in $selection; do |
| 91 | + if [[ "$num" =~ ^[0-9]+$ ]] && [ "$num" -ge 1 ] && [ "$num" -le ${#examples[@]} ]; then |
| 92 | + idx=$((num-1)) |
| 93 | + # Check if already selected |
| 94 | + if [[ ! " ${selected_indices[@]} " =~ " ${idx} " ]]; then |
| 95 | + selected_indices+=($idx) |
| 96 | + echo "Added ${examples[$idx]} to deployment list." |
| 97 | + else |
| 98 | + echo "${examples[$idx]} is already selected." |
| 99 | + fi |
| 100 | + else |
| 101 | + echo "Invalid selection: $num. Please enter numbers between 1 and ${#examples[@]}." |
| 102 | + fi |
| 103 | + done |
| 104 | + fi |
| 105 | +done |
| 106 | + |
| 107 | +# Show selected examples |
| 108 | +echo "" |
| 109 | +echo "Selected examples for deployment:" |
| 110 | +for idx in "${selected_indices[@]}"; do |
| 111 | + echo "- ${examples[$idx]}" |
| 112 | +done |
| 113 | +echo "" |
| 114 | + |
| 115 | +# Confirm deployment |
| 116 | +read -p "Deploy these examples? (y/n): " confirm |
| 117 | +if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then |
| 118 | + echo "Deployment cancelled." |
| 119 | + exit 0 |
| 120 | +fi |
| 121 | + |
| 122 | +# Deploy selected examples concurrently |
| 123 | +echo "Starting deployment of selected examples..." |
| 124 | +pids=() |
| 125 | +results=() |
| 126 | + |
| 127 | +for idx in "${selected_indices[@]}"; do |
| 128 | + example="${examples[$idx]}" |
| 129 | + example_dir="$SCRIPT_DIR/$example" |
| 130 | + |
| 131 | + # Start deployment in background |
| 132 | + (deploy_example "$example_dir" && echo "SUCCESS: $example" || echo "FAILED: $example") & |
| 133 | + pids+=($!) |
| 134 | + results+=("") |
| 135 | +done |
| 136 | + |
| 137 | +# Wait for all deployments to complete |
| 138 | +for i in "${!pids[@]}"; do |
| 139 | + wait "${pids[$i]}" |
| 140 | + results[$i]=$? |
| 141 | +done |
| 142 | + |
| 143 | +# Print summary |
| 144 | +echo "" |
| 145 | +echo "Deployment Summary:" |
| 146 | +echo "==================" |
| 147 | +success_count=0 |
| 148 | +failure_count=0 |
| 149 | + |
| 150 | +for i in "${!selected_indices[@]}"; do |
| 151 | + idx="${selected_indices[$i]}" |
| 152 | + example="${examples[$idx]}" |
| 153 | + result="${results[$i]}" |
| 154 | + |
| 155 | + if [ "$result" -eq 0 ]; then |
| 156 | + echo "✅ ${example}: SUCCESS" |
| 157 | + ((success_count++)) |
| 158 | + else |
| 159 | + echo "❌ ${example}: FAILED" |
| 160 | + ((failure_count++)) |
| 161 | + fi |
| 162 | +done |
| 163 | + |
| 164 | +echo "" |
| 165 | +echo "Total: $((success_count + failure_count)), Successful: $success_count, Failed: $failure_count" |
| 166 | + |
| 167 | +if [ "$failure_count" -gt 0 ]; then |
| 168 | + echo "" |
| 169 | + echo "Some deployments failed. Check the logs above for details." |
| 170 | + exit 1 |
| 171 | +fi |
| 172 | + |
| 173 | +echo "" |
| 174 | +echo "All deployments completed successfully!" |
| 175 | + |
0 commit comments