gce-self-destruct.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env bash
  2. __gce_sd_here="$(dirname "${BASH_SOURCE[0]}")"
  3. # shellcheck disable=SC1091
  4. __gce_sd_conf="${__gce_sd_here}/gce-self-destruct.conf"
  5. gce_metadata_req() {
  6. declare endpoint url
  7. endpoint="$1"
  8. url="http://metadata.google.internal/computeMetadata/v1/$endpoint"
  9. curl -sf -H Metadata-Flavor:Google "$url"
  10. }
  11. unix_to_at_time() {
  12. declare unix="$1"
  13. date --date="@$unix" "+%Y%m%d%H%M.%S"
  14. }
  15. timeout_to_destruct() {
  16. declare timeout_sec now_unix
  17. declare timeout_hrs=$1
  18. timeout_sec=$((timeout_hrs * 60 * 60))
  19. now_unix=$(date +%s)
  20. echo $((now_unix + timeout_sec))
  21. }
  22. relative_timespan()
  23. {
  24. declare timeSpan="$1"
  25. declare -a units divs
  26. units+=( s ); divs+=( 60 )
  27. units+=( m ); divs+=( 60 )
  28. units+=( h ); divs+=( 24 )
  29. units+=( d ); divs+=( 7 )
  30. units+=( w ); divs+=( 52 )
  31. numUnits="${#units[@]}"
  32. units+=( y ); divs+=( 100 )
  33. declare -a outs
  34. declare i div remain
  35. for (( i=0; i < "$numUnits"; i++ )); do
  36. div="${divs[$i]}"
  37. [[ "$timeSpan" -lt "$div" ]] && break
  38. remain="$((timeSpan % div))"
  39. timeSpan="$((timeSpan / div))"
  40. outs+=( "$remain" )
  41. done
  42. outs+=( "$timeSpan" )
  43. numOut="${#outs[@]}"
  44. out1="$((numOut-1))"
  45. out2="$((numOut-2))"
  46. if [[ "$numOut" -eq 1 ]] || \
  47. [[ "$numOut" -ge "$numUnits" && \
  48. "${outs[out1]}" -ge "${divs[out1]}" ]]; then
  49. printf "%d%s" "${outs[out1]}" "${units[out1]}"
  50. else
  51. printf "%d%s%02d%s" "${outs[out1]}" \
  52. "${units[out1]}" "${outs[out2]}" "${units[out2]}"
  53. fi
  54. }
  55. gce_self_destruct_ttl() {
  56. declare colorize="${1:-true}"
  57. declare prefix="${2}"
  58. declare suffix="${3}"
  59. declare output=0
  60. if [[ -f "${__gce_sd_conf}" ]]; then
  61. # shellcheck disable=SC1090
  62. source "${__gce_sd_conf}"
  63. declare ttl pttl color
  64. ttl="$((destruct - $(date +%s)))"
  65. if [[ "$ttl" -lt 0 ]]; then
  66. ttl=0
  67. fi
  68. pttl="$(relative_timespan "$ttl")"
  69. color=
  70. if [[ ttl -lt 3600 ]]; then
  71. color="\033[01;31m"
  72. fi
  73. output="${prefix}${pttl}${suffix}"
  74. if $colorize; then
  75. output="${color}${output}\033[01;00m"
  76. fi
  77. fi
  78. echo -e "$output"
  79. }
  80. gce_self_destruct_setup() {
  81. declare destruct at_time zone
  82. destruct="$(timeout_to_destruct "$1")"
  83. at_time="$(unix_to_at_time "$destruct")"
  84. zone=$(gce_metadata_req "instance/zone")
  85. zone=$(basename "$zone")
  86. cat >"${__gce_sd_conf}" <<EOF
  87. export destruct=$destruct
  88. export zone=$zone
  89. EOF
  90. at -t "$at_time" <<EOF
  91. bash -i <<'EOF2'
  92. source /solana-scratch/gce-self-destruct.sh
  93. gce_self_destruct_check
  94. EOF2
  95. EOF
  96. }
  97. gce_self_destruct_check() {
  98. if [[ -f "${__gce_sd_conf}" ]]; then
  99. # shellcheck disable=SC1090
  100. source "${__gce_sd_conf}"
  101. declare now gcloudBin
  102. now=$(date +%s)
  103. if [[ "$now" -ge "$destruct" ]]; then
  104. # gcloud is installed in /snap/bin, but /snap/bin isn't in root's PATH...
  105. gcloudBin="$(command -v gcloud)"
  106. gcloudBin="${gcloudBin:-/snap/bin/gcloud}"
  107. "$gcloudBin" compute instances delete --quiet "$(hostname)" --zone "$zone"
  108. else
  109. at -t "$(unix_to_at_time "$destruct")" <<EOF
  110. bash -i <<'OEF2'
  111. source /solana-scratch/gce-self-destruct.sh
  112. gce_self_destruct_check
  113. EOF2
  114. EOF
  115. fi
  116. fi
  117. }
  118. gce_self_destruct_motd() {
  119. declare ttl
  120. ttl="$(gce_self_destruct_ttl)"
  121. echo -e '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
  122. if [[ -n "${ttl}" ]]; then
  123. echo -e "\tThis instance will self-destruct in ${ttl}!"
  124. else
  125. echo -e "\tThis instance will NOT self-destruct. YOU are responsible for deleting it!"
  126. fi
  127. echo -e '\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
  128. }
  129. gce_self_destruct_ps1() {
  130. declare ttl
  131. ttl="$(gce_self_destruct_ttl true "[T-" "]")"
  132. ttl="${ttl:-"[T-~~~~~]"}"
  133. echo "!${ttl}"
  134. }