script.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. set -o nounset -o errexit -o pipefail -o xtrace
  3. # PowerDNS API configuration. Override default values by passing them as
  4. # environment variables.
  5. #
  6. # 'powerdns' is a Kubernetes service name.
  7. # 'pdns' is Kubernetes namespace.
  8. POWERDNS_API_URL="${POWERDNS_API_URL:-http://powerdns.pdns:8081}"
  9. #
  10. POWERDNS_API_KEY="${POWERDNS_API_KEY:-PowerDNSAPI}"
  11. POWERDNS_ZONE_NAME="${POWERDNS_ZONE_NAME:-home.wugi.info.}"
  12. POWERDNS_ZONE_NS1="${POWERDNS_ZONE_NS1:-ns1.home.wugi.info.}"
  13. POWERDNS_ZONE_NS2="${POWERDNS_ZONE_NS2:-ns2.home.wugi.info.}"
  14. # Log to a '/var/log/powerdns-majordomo-svc-ru.log' file.
  15. mkdir -p /var/log
  16. exec &> >(tee /var/log/powerdns-majordomo-svc-ru.log)
  17. # Working directory which will be deleted after exiting.
  18. workspace="$(mktemp -d -t "powerdns.XXXXXXXXXX")"
  19. trap 'chmod -Rf +w "$workspace"; rm -rf "$workspace"' EXIT
  20. cd "$workspace" || exit
  21. # Zone configuration.
  22. cat > zone.json <<EOF
  23. {
  24. "kind": "Native",
  25. "masters": [],
  26. "name": "$POWERDNS_ZONE_NAME",
  27. "nameservers": [
  28. "$POWERDNS_ZONE_NS1",
  29. "$POWERDNS_ZONE_NS2"
  30. ]
  31. }
  32. EOF
  33. if curl --silent \
  34. --fail \
  35. --header "X-API-Key: ${POWERDNS_API_KEY}" \
  36. "${POWERDNS_API_URL}/api/v1/servers/localhost/zones/${POWERDNS_ZONE_NAME}"
  37. then
  38. printf "Zone already exists, skipping create.\n"
  39. else
  40. # Create zone.
  41. curl --silent \
  42. --verbose \
  43. --header "X-API-Key: ${POWERDNS_API_KEY}" \
  44. --data @zone.json \
  45. "${POWERDNS_API_URL}/api/v1/servers/localhost/zones"
  46. fi
  47. soa()
  48. {
  49. curl --silent \
  50. --fail \
  51. --header "X-API-Key: ${POWERDNS_API_KEY}" \
  52. "${POWERDNS_API_URL}/api/v1/servers/localhost/zones/${POWERDNS_ZONE_NAME}" \
  53. | jq --raw-output '.rrsets[] | select(.type == "SOA") | .records[0].content'
  54. }
  55. cat > soa.json <<EOF
  56. {
  57. "rrsets": [
  58. {
  59. "name": "${POWERDNS_ZONE_NAME}",
  60. "type": "SOA",
  61. "ttl": 3600,
  62. "changetype": "REPLACE",
  63. "records": [
  64. {
  65. "content": "ns1.${POWERDNS_ZONE_NAME} support.${POWERDNS_ZONE_NAME} 2023011204 10800 3600 604800 3600"
  66. }
  67. ]
  68. }
  69. ]
  70. }
  71. EOF
  72. if [[ $(soa) == *"${POWERDNS_ZONE_NAME} support.${POWERDNS_ZONE_NAME}"* ]]
  73. then
  74. printf "Zone SOA correct, skipping patching.\n"
  75. else
  76. curl --request PATCH \
  77. --data @soa.json \
  78. --header "X-API-Key: ${POWERDNS_API_KEY}" \
  79. "${POWERDNS_API_URL}/api/v1/servers/localhost/zones/${POWERDNS_ZONE_NAME}"
  80. fi