-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathdns_add_hetzner_cloud
More file actions
executable file
·69 lines (61 loc) · 1.74 KB
/
dns_add_hetzner_cloud
File metadata and controls
executable file
·69 lines (61 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
fulldomain="${1}"
token="${2}"
api_url="https://api.hetzner.cloud/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 'Authorization: Bearer '"$api_key"'' | jq -r '.zones[0].id')
if [[ "$zone_id" == "null" ]] ; then
echo "Zone ID not found"
exit 1
fi
fi
# Get Zone name if not set
if [[ -z "$zone_name" ]] ; then
zone_name=$(curl --silent -X GET "$api_url/zones/$zone_id" -H 'Authorization: Bearer '"$api_key"'' | jq -r '.zone.name')
if [[ "$zone_name" == "null" ]] ; then
echo "Zone name not found"
exit 1
fi
fi
txtname="_acme-challenge.$fulldomain"
txtname="${txtname%."$zone_name"}"
payload=$(jq -n \
--arg token "$token" \
'{
ttl: 60,
records: [
{ value: ("\"" + $token + "\"") }
]
}')
# Create TXT record
response=$(curl -s -o /dev/null -w '%{http_code}' \
-X POST "$api_url/zones/$zone_id/rrsets/$txtname/TXT/actions/add_records" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "$payload")
if [[ "$response" != "201" ]] ; then
echo "Record not created"
echo "Response code: $response"
exit 1
fi