ensemble-tts-annotation / scripts /test /launch_gcp_spot.sh
marcosremar
Add comprehensive testing infrastructure
98938e3
#!/bin/bash
# Launch cheap GCP spot (preemptible) instance for testing
# GCP spot instances são ~70-90% mais baratos
set -e
echo "========================================="
echo "GCP Spot Instance - Test OPTION A"
echo "========================================="
# Configuration
INSTANCE_NAME="ensemble-test-$(date +%s)"
MACHINE_TYPES=(
"e2-micro" # 0.25-2 vCPU, 1GB RAM - ~$0.0025/hr spot (~$0.01/hr normal)
"e2-small" # 0.5-2 vCPU, 2GB RAM - ~$0.005/hr spot (~$0.02/hr normal)
"e2-medium" # 1-2 vCPU, 4GB RAM - ~$0.01/hr spot (~$0.04/hr normal)
"n1-standard-1" # 1 vCPU, 3.75GB RAM - ~$0.01/hr spot
)
ZONE="us-central1-a" # Cheapest zone
IMAGE_FAMILY="ubuntu-2204-lts"
IMAGE_PROJECT="ubuntu-os-cloud"
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
echo -e "${RED}Error: gcloud CLI not installed${NC}"
echo "Install: https://cloud.google.com/sdk/docs/install"
exit 1
fi
# Check if authenticated
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then
echo -e "${YELLOW}Not authenticated. Running gcloud auth login...${NC}"
gcloud auth login
fi
# Get current project
PROJECT=$(gcloud config get-value project)
if [ -z "$PROJECT" ]; then
echo -e "${RED}No project set. Please run: gcloud config set project YOUR_PROJECT${NC}"
exit 1
fi
echo -e "\n${YELLOW}Project: $PROJECT${NC}"
echo -e "${YELLOW}Zone: $ZONE${NC}"
echo ""
# Show pricing for spot instances
echo "Spot (Preemptible) Pricing:"
echo " e2-micro: ~\$0.0025/hr (1GB RAM) ⭐ CHEAPEST"
echo " e2-small: ~\$0.005/hr (2GB RAM)"
echo " e2-medium: ~\$0.01/hr (4GB RAM)"
echo " n1-standard-1: ~\$0.01/hr (3.75GB RAM)"
echo ""
# Default to cheapest
MACHINE_TYPE="e2-medium" # 4GB needed for models
ESTIMATED_COST="0.01"
echo -e "${GREEN}Selected: $MACHINE_TYPE (~\$$ESTIMATED_COST/hr)${NC}"
echo ""
read -p "Launch spot instance? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Cancelled."
exit 0
fi
# Create startup script
cat > /tmp/startup-script.sh << 'STARTUP'
#!/bin/bash
# Update system
apt-get update
apt-get install -y python3-pip git
# Install dependencies
pip3 install --upgrade pip
# Clone repository
cd /home/ensemble_test
git clone https://huggingface.co/marcosremar2/ensemble-tts-annotation
cd ensemble-tts-annotation
# Install requirements (basic only for quick test)
pip3 install -q torch --index-url https://download.pytorch.org/whl/cpu
pip3 install -q transformers datasets librosa soundfile numpy pandas tqdm scikit-learn
# Create test user
useradd -m -s /bin/bash ensemble_test || true
chown -R ensemble_test:ensemble_test /home/ensemble_test
echo "✅ Setup complete" > /tmp/setup-complete
date >> /tmp/setup-complete
STARTUP
# Launch instance
echo ""
echo "Launching spot instance..."
gcloud compute instances create "$INSTANCE_NAME" \
--zone="$ZONE" \
--machine-type="$MACHINE_TYPE" \
--preemptible \
--maintenance-policy=TERMINATE \
--image-family="$IMAGE_FAMILY" \
--image-project="$IMAGE_PROJECT" \
--boot-disk-size=20GB \
--boot-disk-type=pd-standard \
--metadata-from-file startup-script=/tmp/startup-script.sh \
--scopes=cloud-platform \
--tags=ensemble-test
echo -e "${GREEN}✓ Instance created: $INSTANCE_NAME${NC}"
# Wait for instance to be running
echo ""
echo "Waiting for instance to be ready..."
sleep 10
# Get external IP
EXTERNAL_IP=$(gcloud compute instances describe "$INSTANCE_NAME" \
--zone="$ZONE" \
--format="get(networkInterfaces[0].accessConfigs[0].natIP)")
echo ""
echo "========================================="
echo -e "${GREEN}✓ Instance launched successfully!${NC}"
echo "========================================="
echo ""
echo "Instance Name: $INSTANCE_NAME"
echo "Machine Type: $MACHINE_TYPE"
echo "Cost: ~\$$ESTIMATED_COST/hr (spot/preemptible)"
echo "External IP: $EXTERNAL_IP"
echo "Zone: $ZONE"
echo ""
echo "SSH Command:"
echo " gcloud compute ssh $INSTANCE_NAME --zone=$ZONE"
echo ""
echo "Run test (wait ~2min for setup):"
echo " gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py'"
echo ""
echo "Check setup status:"
echo " gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cat /tmp/setup-complete'"
echo ""
echo "Delete instance:"
echo " gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet"
echo ""
echo "========================================="
# Save info
cat > /tmp/gcp-spot-info.txt << EOF
Instance Name: $INSTANCE_NAME
Machine Type: $MACHINE_TYPE
Cost: ~\$$ESTIMATED_COST/hr
External IP: $EXTERNAL_IP
Zone: $ZONE
Project: $PROJECT
SSH: gcloud compute ssh $INSTANCE_NAME --zone=$ZONE
Test: gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py'
Delete: gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet
EOF
echo "Instance info saved to: /tmp/gcp-spot-info.txt"
echo ""
# Optionally run test automatically
read -p "Run test automatically? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "Waiting for setup to complete (60s)..."
sleep 60
echo ""
echo "Running test..."
gcloud compute ssh "$INSTANCE_NAME" --zone="$ZONE" --command="cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py" || true
fi
echo ""
echo -e "${YELLOW}Remember to delete the instance when done to avoid charges!${NC}"
echo " gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet"
echo ""