-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #838 from Leo963:add_hetzner_dns
Add Hetzner DNS scripts
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env bash | ||
|
||
fulldomain="${1}" | ||
token="${2}" | ||
api_url="https://dns.hetzner.com/api/v1" | ||
api_key=${HETZNER_KEY:-''} | ||
zone_id=${HETZNER_ZONE_ID:-''} | ||
zone_name=${HETZNER_ZONE_NAME:-''} | ||
|
||
# Verify that required parameters are set | ||
if [[ -z "$fulldomain" ]]; then | ||
echo "DNS script requires full domain name as first parameter" | ||
exit 1 | ||
fi | ||
if [[ -z "$token" ]]; then | ||
echo "DNS script requires challenge token as second parameter" | ||
exit 1 | ||
fi | ||
if [[ -z "$HETZNER_KEY" ]]; then | ||
echo "HETZNER_KEY variable not set" | ||
exit 1 | ||
fi | ||
if [[ -z "$HETZNER_ZONE_ID" && -z "$HETZNER_ZONE_NAME" ]] ; then | ||
echo "HETZNER_ZONE_ID and HETZNER_ZONE_NAME variables not set" | ||
exit 1 | ||
fi | ||
|
||
# Get Zone ID if not set | ||
if [[ -z "$HETZNER_ZONE_ID" ]] ; then | ||
zone_id=$(curl --silent -X GET "$api_url/zones?name=$zone_name" -H 'Auth-API-Token: '"$api_key"'' | jq -r '.zones[0].id') | ||
if [[ "$zone_id" == "null" ]] ; then | ||
echo "Zone ID not found" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
txtname="_acme-challenge.$fulldomain." | ||
|
||
# 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": "'"$txtname"'","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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env bash | ||
|
||
fulldomain="${1}" | ||
token="${2}" | ||
api_url="https://dns.hetzner.com/api/v1" | ||
api_key=${HETZNER_KEY:-''} | ||
zone_id=${HETZNER_ZONE_ID:-''} | ||
zone_name=${HETZNER_ZONE_NAME:-''} | ||
|
||
# Verify that required parameters are set | ||
if [[ -z "$fulldomain" ]]; then | ||
echo "DNS script requires full domain name as first parameter" | ||
exit 1 | ||
fi | ||
if [[ -z "$token" ]]; then | ||
echo "DNS script requires challenge token as second parameter" | ||
exit 1 | ||
fi | ||
if [[ -z "$HETZNER_KEY" ]]; then | ||
echo "HETZNER_KEY variable not set" | ||
exit 1 | ||
fi | ||
if [[ -z "$HETZNER_ZONE_ID" && -z "$HETZNER_ZONE_NAME" ]] ; then | ||
echo "HETZNER_ZONE_ID and HETZNER_ZONE_NAME variables not set" | ||
exit 1 | ||
fi | ||
|
||
# Get Zone ID if not set | ||
if [[ -z "$HETZNER_ZONE_ID" ]] ; then | ||
zone_id=$(curl --silent -X GET "$api_url/zones?name=$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}') | ||
# domain=${fulldomain%.$domain_root} | ||
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" -o /dev/null -w '%{http_code}') | ||
|
||
if [[ "$response" != "200" ]] ; then | ||
echo "Record not deleted" | ||
echo "Response code: $response" | ||
exit 1 | ||
fi | ||
|