Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # GCloud startup script to auto-restart any instances with 'revive' tag.
  4. # The calling machine must have Read/Write access to compute API!!
  5. # I use this to reboot preemptible instances.
  6. # Output is logged to /tmp/revive.log
  7.  
  8. indent() { sed 's/^/ /'; }
  9.  
  10. revive_instances() {
  11. # Go through lines in the provided string
  12. for line in "$1"; do
  13. echo "$line"
  14. # Instance name is the first word in the line.
  15. instance_name=`echo "$line" | head -n1 | awk '{print $1}'`
  16. instance_zone=`echo "$line" | head -n1 | awk '{print $2}'`
  17. # Attempt to reboot the instance
  18. echo "Rebooting '$instance_name' in zone '$instance_zone'..."
  19. gcloud compute instances start "--zone=$instance_zone" "$instance_name"
  20. done
  21. }
  22.  
  23. auto_reviver () {
  24. REVIVE_TAG="$1"
  25. CHECK_INTERVAL="$2"
  26. LOG_FILE="$3"
  27. IFS=$'\n'
  28. date +"%F %T: monitoring instances with revive tag '$REVIVE_TAG', interval $CHECK_INTERVAL" >> "$LOG_FILE"
  29. while :; do
  30. # Look for instances with "revive" in their name/tags and TERMINATED status
  31. offline=`gcloud compute instances list --format='table(name,zone,status,tags.list())' | grep "$REVIVE_TAG" | grep "TERMINATED"`
  32. if [[ ! -z "$offline" ]] ; then
  33. # If we found some, reboot them
  34. date +"%F %T: some instances are down." >> "$LOG_FILE"
  35. revive_instances "$offline" | indent >> "$LOG_FILE"
  36. fi
  37. # Sleep for the check interval
  38. sleep $CHECK_INTERVAL
  39. done
  40. }
  41.  
  42. # Make sure revive.log is readable by general users
  43. printf '' >> "/tmp/revive.log"
  44. chmod 644 "/tmp/revive.log"
  45.  
  46. # Run auto-reviver with tag "revive", check interval 2 minutes, logging
  47. auto_reviver "revive" 120 "/tmp/revive.log"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement