| #!/bin/bash |
|
|
| |
| |
|
|
| set -e |
|
|
| echo "=========================================" |
| echo "GCP Spot Instance - Test OPTION A" |
| echo "=========================================" |
|
|
| |
| INSTANCE_NAME="ensemble-test-$(date +%s)" |
| MACHINE_TYPES=( |
| "e2-micro" |
| "e2-small" |
| "e2-medium" |
| "n1-standard-1" |
| ) |
|
|
| ZONE="us-central1-a" |
| IMAGE_FAMILY="ubuntu-2204-lts" |
| IMAGE_PROJECT="ubuntu-os-cloud" |
|
|
| |
| GREEN='\033[0;32m' |
| YELLOW='\033[1;33m' |
| RED='\033[0;31m' |
| NC='\033[0m' |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| 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 "" |
|
|
| |
| 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 "" |
|
|
| |
| MACHINE_TYPE="e2-medium" |
| 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 |
|
|
| |
| cat > /tmp/startup-script.sh << 'STARTUP' |
| |
|
|
| |
| apt-get update |
| apt-get install -y python3-pip git |
|
|
| |
| pip3 install --upgrade pip |
|
|
| |
| cd /home/ensemble_test |
| git clone https://huggingface.co/marcosremar2/ensemble-tts-annotation |
| cd ensemble-tts-annotation |
|
|
| |
| pip3 install -q torch --index-url https://download.pytorch.org/whl/cpu |
| pip3 install -q transformers datasets librosa soundfile numpy pandas tqdm scikit-learn |
|
|
| |
| 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 |
|
|
| |
| 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}" |
|
|
| |
| echo "" |
| echo "Waiting for instance to be ready..." |
| sleep 10 |
|
|
| |
| 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 "=========================================" |
|
|
| |
| 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 "" |
|
|
| |
| 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 "" |
|
|