remove.j2.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. # Write the header
  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 }' | head -n1 | xargs echo -n > "${dns}.input"
  17. # Append all etcd records
  18. grep -F -e "${ETCD_DNS_NAME}" "${dns}" | awk '{ print " \x27"$5" "$6" "$7" "$8"\x27"; }' | tr -d '\n\r' >> "${dns}.input" || true
  19. echo >> "${dns}.input"
  20. if [ -s "${dns}.input" ]; then
  21. rm -f "${dns}"
  22. gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns start -z "${dns_zone}"
  23. cat "${dns}.input" | xargs -L1 gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file="${dns}" remove -z "${dns_zone}"
  24. # Commit all DNS changes, retrying if preconditions are not met
  25. if ! out="$( gcloud --project "{{ openshift_gcp_project }}" dns record-sets transaction --transaction-file=$dns execute -z "${dns_zone}" 2>&1 )"; then
  26. rc=$?
  27. if [[ "${out}" == *"HTTPError 412: Precondition not met"* ]]; then
  28. continue
  29. fi
  30. exit $rc
  31. fi
  32. fi
  33. rm "${dns}.input"
  34. break
  35. done
  36. fi
  37. ) &
  38. for i in `jobs -p`; do wait $i; done