remove.j2.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set -euxo pipefail
  3. # DNS
  4. (
  5. dns_zone="{{ dns_managed_zone | default(openshift_gcp_prefix + 'managed-zone') }}"
  6. if gcloud --project "{{ openshift_gcp_project }}" dns managed-zones describe "${dns_zone}" &>/dev/null; then
  7. # Retry DNS changes until they succeed since this may be a shared resource
  8. while true; do
  9. dns="${TMPDIR:-/tmp}/dns.yaml"
  10. rm -f "${dns}"
  11. # export all dns records that match into a zone format, and turn each line into a set of args for
  12. # record-sets transaction.
  13. gcloud dns record-sets export --project "{{ openshift_gcp_project }}" -z "${dns_zone}" --zone-file-format "${dns}"
  14. # Fetch API record to get a list of masters + bootstrap node
  15. bootstrap_and_masters=""
  16. public_ip_output=($(grep -F -e '{{ openshift_master_cluster_public_hostname }}.' "${dns}" | awk '{ print $5 }')) || public_ip_output=""
  17. for index in "${!public_ip_output[@]}"; do
  18. bootstrap_and_masters="${bootstrap_and_masters} ${public_ip_output[${index}]}"
  19. if [ ${index} -eq 0 ]; then
  20. # First record is bootstrap
  21. continue
  22. fi
  23. # etcd server name
  24. MASTER_DNS_NAME="{{ openshift_gcp_prefix }}etcd-$((index-1)).{{ public_hosted_zone }}."
  25. # Add a extra space here so that it won't match etcd discovery record
  26. grep -F -e "${MASTER_DNS_NAME} " "${dns}" | awk '{ print "--name", $1, "--ttl", $2, "--type", $4, $5; }' >> "${dns}.input" || true
  27. done
  28. # Remove API record
  29. if [ ! -z "${public_ip_output}" ]; then
  30. args=`grep -F -e '{{ openshift_master_cluster_public_hostname }}.' "${dns}" | awk '{ print "--name", $1, "--ttl", $2, "--type", $4; }' | head -n1`
  31. echo "${args}${bootstrap_and_masters}" >> "${dns}.input"
  32. fi
  33. # Remove etcd discovery record
  34. ETCD_DNS_NAME="_etcd-server-ssl._tcp.{{ lookup('env', 'INSTANCE_PREFIX') | mandatory }}.{{ public_hosted_zone }}."
  35. grep -F -e "${ETCD_DNS_NAME}" "${dns}" | awk '{ print "--name", $1, "--ttl", $2, "--type", $4, "\x27"$5" "$6" "$7" "$8"\x27"; }' >> "${dns}.input" || true
  36. if [ -s "${dns}.input" ]; then
  37. rm -f "${dns}"
  38. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  39. cat "${dns}.input" | xargs -L1 gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file="${dns}" remove -z "${dns_zone}"
  40. # Commit all DNS changes, retrying if preconditions are not met
  41. if ! out="$( gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns execute -z "${dns_zone}" 2>&1 )"; then
  42. rc=$?
  43. if [[ "${out}" == *"HTTPError 412: Precondition not met"* ]]; then
  44. continue
  45. fi
  46. exit $rc
  47. fi
  48. fi
  49. rm "${dns}.input"
  50. break
  51. done
  52. fi
  53. ) &
  54. for i in `jobs -p`; do wait $i; done