MeshAI-Base-Models / scripts /gcp_wait_start_ssh.sh
HayrettinIscan's picture
Add GCP wait-start-ssh helper
93a9d59 verified
Raw
History Blame Contribute Delete
2.89 kB
#!/usr/bin/env bash
# meshai-a100-trainer: start + SSH hazir olana kadar bekle / tekrar dene.
# Cloud Shell veya gcloud kurulu herhangi bir makinede:
# bash scripts/gcp_wait_start_ssh.sh
# bash scripts/gcp_wait_start_ssh.sh --no-ssh # sadece ac, SSH yapma
# bash scripts/gcp_wait_start_ssh.sh --cmd 'cd ~/MeshAI_Cloud && ...'
set -euo pipefail
INSTANCE="${INSTANCE:-meshai-a100-trainer}"
ZONE="${ZONE:-europe-west4-a}"
MAX_ATTEMPTS="${MAX_ATTEMPTS:-60}"
SLEEP_SEC="${SLEEP_SEC:-20}"
DO_SSH=1
REMOTE_CMD=""
while [[ $# -gt 0 ]]; do
case "$1" in
--no-ssh) DO_SSH=0; shift ;;
--cmd) REMOTE_CMD="$2"; shift 2 ;;
--zone) ZONE="$2"; shift 2 ;;
--instance) INSTANCE="$2"; shift 2 ;;
*) echo "Bilinmeyen arg: $1"; exit 1 ;;
esac
done
log() { echo "[$(date '+%H:%M:%S')] $*"; }
need_gcloud() {
if ! command -v gcloud >/dev/null 2>&1; then
echo "HATA: gcloud yok. Bu scripti Cloud Shell'de calistir."
exit 1
fi
}
instance_status() {
gcloud compute instances describe "$INSTANCE" --zone="$ZONE" --format='get(status)' 2>/dev/null || echo "UNKNOWN"
}
ensure_started() {
local st
st="$(instance_status)"
log "Durum: $st"
if [[ "$st" == "RUNNING" ]]; then
return 0
fi
if [[ "$st" == "STAGING" || "$st" == "PROVISIONING" || "$st" == "STOPPING" ]]; then
log "Gecis durumu ($st) β€” bekleniyor..."
return 1
fi
# TERMINATED / STOPPED / SUSPENDED / UNKNOWN
log "Start deneniyor: $INSTANCE ($ZONE)"
if gcloud compute instances start "$INSTANCE" --zone="$ZONE"; then
log "Start komutu OK"
return 0
fi
log "Start basarisiz β€” tekrar denenecek"
return 1
}
ssh_ready() {
# Exit 0 if SSH accepts a trivial command
gcloud compute ssh "$INSTANCE" --zone="$ZONE" --command="echo SSH_OK" --quiet 2>/dev/null | grep -q SSH_OK
}
need_gcloud
log "Hedef: $INSTANCE / $ZONE (max $MAX_ATTEMPTS x ${SLEEP_SEC}s)"
attempt=0
while (( attempt < MAX_ATTEMPTS )); do
attempt=$((attempt + 1))
log "Deneme $attempt/$MAX_ATTEMPTS"
ensure_started || true
sleep 5
st="$(instance_status)"
if [[ "$st" != "RUNNING" ]]; then
log "Henuz RUNNING degil ($st) β€” ${SLEEP_SEC}s"
sleep "$SLEEP_SEC"
continue
fi
log "RUNNING β€” SSH kontrol..."
if ssh_ready; then
log "SSH hazir."
if [[ "$DO_SSH" -eq 0 ]]; then
log " --no-ssh: cikis (VM acik)"
exit 0
fi
if [[ -n "$REMOTE_CMD" ]]; then
log "Uzak komut calistiriliyor..."
gcloud compute ssh "$INSTANCE" --zone="$ZONE" --command="$REMOTE_CMD"
exit $?
fi
log "Interaktif SSH aciliyor..."
exec gcloud compute ssh "$INSTANCE" --zone="$ZONE"
fi
log "SSH henuz yok β€” ${SLEEP_SEC}s sonra tekrar"
sleep "$SLEEP_SEC"
done
log "HATA: $MAX_ATTEMPTS denemede SSH hazir olmadi."
exit 1