Harden curl retries for Porkbun API calls

This commit is contained in:
kacper 2026-04-14 21:42:38 -04:00
parent b16137f238
commit 3f626fe113

View file

@ -72,6 +72,8 @@ fi
PORKBUN_API_URL="https://api-ipv4.porkbun.com/api/json/v3"
NOTES_PREFIX="ddns:"
# Fail on transient HTTP error bodies so retries cannot append HTML to JSON output.
CURL_RETRY_ARGS=(--silent --show-error --fail --retry 3 --retry-delay 2)
# Optional dry-run mode. When DRY_RUN=1, destructive or mutating API calls
# (dns/create, dns/delete) are logged but not executed.
@ -99,7 +101,7 @@ function get_public_ip() {
fi
local ping_response
ping_response=$(curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/ping" \
ping_response=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/ping" \
-H "Content-Type: application/json" \
-d "$auth_json" 2>&1 || echo "")
@ -112,14 +114,14 @@ function get_public_ip() {
# Fallback methods if Porkbun ping fails
if [ -z "$ip" ]; then
ip=$(curl -s --max-time 5 --retry 2 https://api.ipify.org 2>/dev/null || echo "")
ip=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 5 https://api.ipify.org 2>/dev/null || echo "")
if [ -n "$ip" ]; then
printf '%s [INFO] IP source: api.ipify.org\n' "$(date)" >&2
fi
fi
if [ -z "$ip" ]; then
ip=$(curl -s --max-time 5 --retry 2 https://icanhazip.com 2>/dev/null || echo "")
ip=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 5 https://icanhazip.com 2>/dev/null || echo "")
if [ -n "$ip" ]; then
printf '%s [INFO] IP source: icanhazip.com\n' "$(date)" >&2
fi
@ -178,7 +180,7 @@ function register_host_dns() {
# Retrieve all DNS records for the domain
local records
if ! records=$(curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
if ! records=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
-H "Content-Type: application/json" \
-d "$auth_json" 2>&1); then
printf '%s [ERROR] Failed to retrieve DNS records: %s\n' "$(date)" "$records" >&2
@ -250,7 +252,7 @@ function register_host_dns() {
if [ "$DRY_RUN" -eq 1 ]; then
printf "%s [INFO] DRY RUN: Skipping actual delete for record ID %s\n" "$(date)" "$record_id"
else
if curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/dns/delete/$domain/$record_id" \
if curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/dns/delete/$domain/$record_id" \
-H "Content-Type: application/json" \
-d "$auth_json" > /dev/null 2>&1; then
((delete_count++)) || true
@ -267,7 +269,7 @@ function register_host_dns() {
# Verify no duplicates remain before creating new record
# Re-fetch records to ensure we have latest state
if ! records=$(curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
if ! records=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
-H "Content-Type: application/json" \
-d "$auth_json" 2>&1); then
printf '%s [WARN] Failed to re-verify DNS records after deletion: %s\n' "$(date)" "$records" >&2
@ -307,7 +309,7 @@ function register_host_dns() {
fi
local create_response
if ! create_response=$(curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/dns/create/$domain" \
if ! create_response=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/dns/create/$domain" \
-H "Content-Type: application/json" \
-d "$create_json" 2>&1); then
printf '%s [ERROR] Failed to create DNS record: %s\n' "$(date)" "$create_response" >&2
@ -320,7 +322,7 @@ function register_host_dns() {
printf "%s [INFO] Successfully created A record for %s (machine: %s)\n" "$(date)" "$fqdn" "$MACHINE_ID"
# Final verification: ensure exactly one record exists
if records=$(curl -s --max-time 10 --retry 2 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
if records=$(curl "${CURL_RETRY_ARGS[@]}" --max-time 10 -X POST "$PORKBUN_API_URL/dns/retrieve/$domain" \
-H "Content-Type: application/json" \
-d "$auth_json" 2>&1); then
local final_count