remove.j2.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. set -euo pipefail
  3. function teardown_cmd() {
  4. a=( $@ )
  5. local name=$1
  6. a=( "${a[@]:1}" )
  7. local flag=0
  8. local found=
  9. for i in ${a[@]}; do
  10. if [[ "$i" == "--"* ]]; then
  11. found=true
  12. break
  13. fi
  14. flag=$((flag+1))
  15. done
  16. if [[ -z "${found}" ]]; then
  17. flag=$((flag+1))
  18. fi
  19. if gcloud --project "{{ openshift_gcp_project }}" ${a[@]::$flag} describe "${name}" ${a[@]:$flag} &>/dev/null; then
  20. gcloud --project "{{ openshift_gcp_project }}" ${a[@]::$flag} delete -q "${name}" ${a[@]:$flag}
  21. fi
  22. }
  23. function teardown() {
  24. for i in `seq 1 20`; do
  25. if teardown_cmd $@; then
  26. break
  27. fi
  28. sleep 0.5
  29. done
  30. }
  31. # Preemptively spin down the instances
  32. {% for node_group in openshift_gcp_node_group_config %}
  33. # scale down {{ node_group.name }}
  34. (
  35. # performs a delete and scale down as one operation to ensure maximum parallelism
  36. if ! instances=$( gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed list-instances "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" --zone "{{ openshift_gcp_zone }}" --format='value[terminator=","](instance)' 2>/dev/null ); then
  37. exit 0
  38. fi
  39. instances="${instances%?}"
  40. if [[ -z "${instances}" ]]; then
  41. echo "warning: No instances in {{ node_group.name }}" 1>&2
  42. exit 0
  43. fi
  44. if ! gcloud --project "{{ openshift_gcp_project }}" compute instance-groups managed delete-instances "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" --zone "{{ openshift_gcp_zone }}" --instances "${instances}"; then
  45. echo "warning: Unable to scale down the node group {{ node_group.name }}" 1>&2
  46. exit 0
  47. fi
  48. ) &
  49. {% endfor %}
  50. # Bucket for registry
  51. (
  52. if gsutil ls -p "{{ openshift_gcp_project }}" "gs://{{ openshift_gcp_registry_bucket_name }}" &>/dev/null; then
  53. gsutil -m rm -r "gs://{{ openshift_gcp_registry_bucket_name }}"
  54. fi
  55. ) &
  56. # Project metadata prefixed with {{ openshift_gcp_prefix }}
  57. (
  58. for key in $( gcloud --project "{{ openshift_gcp_project }}" compute project-info describe --flatten=commonInstanceMetadata.items[] '--format=value(commonInstanceMetadata.items.key)' ); do
  59. if [[ "${key}" == "{{ openshift_gcp_prefix }}"* ]]; then
  60. gcloud --project "{{ openshift_gcp_project }}" compute project-info remove-metadata "--keys=${key}"
  61. fi
  62. done
  63. ) &
  64. # Instances and disks used for image building
  65. (
  66. teardown "{{ openshift_gcp_prefix }}build-image-instance" compute instances --zone "{{ openshift_gcp_zone }}"
  67. teardown "{{ openshift_gcp_prefix }}build-image-instance" compute disks --zone "{{ openshift_gcp_zone }}"
  68. ) &
  69. # DNS
  70. (
  71. dns_zone="{{ dns_managed_zone | default(openshift_gcp_prefix + 'managed-zone') }}"
  72. if gcloud --project "{{ openshift_gcp_project }}" dns managed-zones describe "${dns_zone}" &>/dev/null; then
  73. # Retry DNS changes until they succeed since this may be a shared resource
  74. while true; do
  75. dns="${TMPDIR:-/tmp}/dns.yaml"
  76. rm -f "${dns}"
  77. # export all dns records that match into a zone format, and turn each line into a set of args for
  78. # record-sets transaction.
  79. gcloud dns record-sets export --project "{{ openshift_gcp_project }}" -z "${dns_zone}" --zone-file-format "${dns}"
  80. if grep -F -e '{{ openshift_master_cluster_hostname }}' -e '{{ openshift_master_cluster_public_hostname }}' -e '{{ wildcard_zone }}' "${dns}" | \
  81. awk '{ print "--name", $1, "--ttl", $2, "--type", $4, $5; }' > "${dns}.input"
  82. then
  83. rm -f "${dns}"
  84. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  85. cat "${dns}.input" | xargs -L1 gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file="${dns}" remove -z "${dns_zone}"
  86. # Commit all DNS changes, retrying if preconditions are not met
  87. if ! out="$( gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns execute -z "${dns_zone}" 2>&1 )"; then
  88. rc=$?
  89. if [[ "${out}" == *"HTTPError 412: Precondition not met"* ]]; then
  90. continue
  91. fi
  92. exit $rc
  93. fi
  94. fi
  95. rm "${dns}.input"
  96. break
  97. done
  98. fi
  99. ) &
  100. (
  101. # Router network rules
  102. teardown "{{ openshift_gcp_prefix }}router-network-lb-rule" compute forwarding-rules --region "{{ openshift_gcp_region }}"
  103. teardown "{{ openshift_gcp_prefix }}router-network-lb-pool" compute target-pools --region "{{ openshift_gcp_region }}"
  104. teardown "{{ openshift_gcp_prefix }}router-network-lb-health-check" compute http-health-checks
  105. teardown "{{ openshift_gcp_prefix }}router-network-lb-ip" compute addresses --region "{{ openshift_gcp_region }}"
  106. # Internal master network rules
  107. teardown "{{ openshift_gcp_prefix }}master-network-lb-rule" compute forwarding-rules --region "{{ openshift_gcp_region }}"
  108. teardown "{{ openshift_gcp_prefix }}master-network-lb-pool" compute target-pools --region "{{ openshift_gcp_region }}"
  109. teardown "{{ openshift_gcp_prefix }}master-network-lb-health-check" compute http-health-checks
  110. teardown "{{ openshift_gcp_prefix }}master-network-lb-ip" compute addresses --region "{{ openshift_gcp_region }}"
  111. ) &
  112. (
  113. # Master SSL network rules
  114. teardown "{{ openshift_gcp_prefix }}master-ssl-lb-rule" compute forwarding-rules --global
  115. teardown "{{ openshift_gcp_prefix }}master-ssl-lb-target" compute target-tcp-proxies
  116. teardown "{{ openshift_gcp_prefix }}master-ssl-lb-ip" compute addresses --global
  117. teardown "{{ openshift_gcp_prefix }}master-ssl-lb-backend" compute backend-services --global
  118. teardown "{{ openshift_gcp_prefix }}master-ssl-lb-health-check" compute health-checks
  119. ) &
  120. #Firewall rules
  121. #['name']='parameters for "gcloud compute firewall-rules create"'
  122. #For all possible parameters see: gcloud compute firewall-rules create --help
  123. declare -A FW_RULES=(
  124. ['icmp']=""
  125. ['ssh-external']=""
  126. ['ssh-internal']=""
  127. ['master-internal']=""
  128. ['master-external']=""
  129. ['node-internal']=""
  130. ['infra-node-internal']=""
  131. ['infra-node-external']=""
  132. )
  133. for rule in "${!FW_RULES[@]}"; do
  134. ( if gcloud --project "{{ openshift_gcp_project }}" compute firewall-rules describe "{{ openshift_gcp_prefix }}$rule" &>/dev/null; then
  135. # retry a few times because this call can be flaky
  136. for i in `seq 1 3`; do
  137. if gcloud -q --project "{{ openshift_gcp_project }}" compute firewall-rules delete "{{ openshift_gcp_prefix }}$rule"; then
  138. break
  139. fi
  140. done
  141. fi ) &
  142. done
  143. for i in `jobs -p`; do wait $i; done
  144. {% for node_group in openshift_gcp_node_group_config %}
  145. # teardown {{ node_group.name }} - any load balancers referencing these groups must be removed
  146. (
  147. teardown "{{ openshift_gcp_prefix }}ig-{{ node_group.suffix }}" compute instance-groups managed --zone "{{ openshift_gcp_zone }}"
  148. teardown "{{ openshift_gcp_prefix }}instance-template-{{ node_group.name }}" compute instance-templates
  149. ) &
  150. {% endfor %}
  151. for i in `jobs -p`; do wait $i; done
  152. # Images specifically located under this cluster prefix family
  153. for name in $( gcloud --project "{{ openshift_gcp_project }}" compute images list "--filter=family={{ openshift_gcp_prefix }}images" '--format=value(name)' ); do
  154. ( gcloud --project "{{ openshift_gcp_project }}" compute images delete "${name}" ) &
  155. done
  156. # Network
  157. ( teardown "{{ openshift_gcp_network_name }}" compute networks ) &
  158. for i in `jobs -p`; do wait $i; done