Skip to content

Commit

Permalink
Add error handling for Hetzner DNS scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo963 committed Feb 22, 2024
1 parent b0aae24 commit 5a96a01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
22 changes: 12 additions & 10 deletions dns_scripts/dns_add_hetzner
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ fi
# Get Zone ID if not set
if [ -z "$HETZNER_ZONE_ID" ] ; then
zone_id=$(curl --silent -X GET "$api_url/zones?name=$HETZNER_ZONE_NAME" -H 'Auth-API-Token: '"$api_key"'' | jq -r '.zones[0].id')
if [ -z "$zone_id" == "null" ] ; then
echo "Zone ID not found"
exit 1
fi
fi

# domain_root=$(echo "$fulldomain" | awk -F\. '{print $(NF-1) FS $NF FS}')
# domain=${fulldomain%.$domain_root}
txtname="_acme-challenge.$fulldomain."

echo $zone_id
# Create TXT record
response=$(curl --silent -X POST "$api_url/records" \
-H 'Content-Type: application/json' \
-H "Auth-API-Token: $api_key" \
-d '{"value": "'$token'","ttl": 60,"type": "TXT","name": "_acme-challenge.'$fulldomain'.","zone_id": "'$zone_id'"}')
echo "$response"
# errors=$(echo "$response" | egrep -o "\"ERRORARRAY\":\[.*\]")
# if [[ $errors != "\"ERRORARRAY\":[]" ]]; then
# echo "Something went wrong: $errors"
# exit 1
# fi
-d '{"value": "'$token'","ttl": 60,"type": "TXT","name": "_acme-challenge.'$fulldomain'.","zone_id": "'$zone_id'"}' \
-o /dev/null -w '%{http_code}')

if [ "$response" != "200" ] ; then
echo "Record not created"
echo "Response code: $response"
exit 1
fi
20 changes: 15 additions & 5 deletions dns_scripts/dns_del_hetzner
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ fi
# Get Zone ID if not set
if [ -z "$HETZNER_ZONE_ID" ] ; then
zone_id=$(curl --silent -X GET "$api_url/zones?name=$HETZNER_ZONE_NAME" -H 'Auth-API-Token: '"$api_key"'' | jq -r '.zones[0].id')
if [ "$zone_id" == "null" ] ; then
echo "Zone by name not found"
exit 1
fi
fi

# domain_root=$(echo "$fulldomain" | awk -F\. '{print $(NF-1) FS $NF FS}')
Expand All @@ -37,11 +41,17 @@ txtname="_acme-challenge.$fulldomain."

record_id=$(curl --silent -X GET "$api_url/records?zone_id=$zone_id" -H "Auth-API-Token: $api_key" | jq -r '.records[] | select(.name=="'$txtname'") | .id')

if [ "$record_id" == "null" ] ; then
echo "Record not found"
exit 1
fi

# Create TXT record
response=$(curl --silent -X DELETE "$api_url/records/$record_id" -H "Auth-API-Token: $api_key")
response=$(curl --silent -X DELETE "$api_url/records/$record_id" -H "Auth-API-Token: $api_key" -o /dev/null -w '%{http_code}')

# errors=$(echo "$response" | egrep -o "\"ERRORARRAY\":\[.*\]")
if [[ $response != "\"ERRORARRAY\":[]" ]]; then
echo "Something went wrong: $errors"
exit 1
if [ "$response" != "200" ] ; then
echo "Record not deleted"
echo "Response code: $response"
exit 1
fi

0 comments on commit 5a96a01

Please sign in to comment.