remove.j2.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. # Remove etcd discovery record
  15. ETCD_DNS_NAME="_etcd-server-ssl._tcp.{{ lookup('env', 'INSTANCE_PREFIX') | mandatory }}.{{ public_hosted_zone }}."
  16. grep -F -e "${ETCD_DNS_NAME}" "${dns}" | awk '{ print "--name", $1, "--ttl", $2, "--type", $4, "\x27"$5" "$6" "$7" "$8"\x27"; }' >> "${dns}.input" || true
  17. if [ -s "${dns}.input" ]; then
  18. rm -f "${dns}"
  19. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  20. cat "${dns}.input" | xargs -L1 gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file="${dns}" remove -z "${dns_zone}"
  21. # Commit all DNS changes, retrying if preconditions are not met
  22. if ! out="$( gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns execute -z "${dns_zone}" 2>&1 )"; then
  23. rc=$?
  24. if [[ "${out}" == *"HTTPError 412: Precondition not met"* ]]; then
  25. continue
  26. fi
  27. exit $rc
  28. fi
  29. fi
  30. rm "${dns}.input"
  31. break
  32. done
  33. fi
  34. ) &
  35. for i in `jobs -p`; do wait $i; done