|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +fulldomain="${1}" |
| 4 | +api_url="https://api.linode.com/api/" |
| 5 | +api_key=${LINODE_KEY:-''} |
| 6 | + |
| 7 | +# Verify that required parameters are set |
| 8 | +if [[ -z "$fulldomain" ]]; then |
| 9 | + echo "DNS script requires full domain name as first parameter" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | +if [[ -z "$LINODE_KEY" ]]; then |
| 13 | + echo "LINODE_KEY variable not set" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +domain_root=$(echo "$fulldomain" | awk -F\. '{print $(NF-1) FS $NF}') |
| 18 | +domain=${fulldomain%.$domain_root} |
| 19 | +txtname="_acme-challenge.$domain" |
| 20 | + |
| 21 | +# Get Domain ID |
| 22 | +response=$(curl --silent -X POST "$api_url" \ |
| 23 | + -H "Accept: application/json" -H "User-Agent: getssl/0.1" -H "application/x-www-form-urlencoded" \ |
| 24 | + -d "api_key=${api_key}&api_action=domain.list" ) |
| 25 | +domain_id=$(echo "$response" | egrep -o "{\"DOMAIN\":\"$domain_root\".*\"DOMAINID\":([0-9]+)" | egrep -o "[0-9]+$") |
| 26 | +if [[ $domain_id == "" ]]; then |
| 27 | + echo "Failed to fetch DomainID" |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Get Resource ID |
| 32 | +response=$(curl --silent -X POST "$api_url" \ |
| 33 | + -H "Accept: application/json" -H "User-Agent: getssl/0.1" -H "application/x-www-form-urlencoded" \ |
| 34 | + -d "api_key=${api_key}&api_action=domain.resource.list&DomainID=$domain_id" ) |
| 35 | +resource_id=$(echo "$response" | egrep -o "\"RESOURCEID\":[0-9]+,\"TYPE\":\"TXT\",\"NAME\":\"$txtname\"" | egrep -o "\"RESOURCEID\":[0-9]+" | egrep -o "[0-9]+$") |
| 36 | +if [[ $resource_id == "" ]]; then |
| 37 | + echo "Failed to fetch ResourceID" |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +# Delete TXT record |
| 42 | +response=$(curl --silent -X POST "$api_url" \ |
| 43 | + -H "Accept: application/json" -H "User-Agent: getssl/0.1" -H "application/x-www-form-urlencoded" \ |
| 44 | + -d "api_key=$api_key&api_action=domain.resource.delete&DomainID=$domain_id&ResourceID=$resource_id" ) |
| 45 | +errors=$(echo "$response" | egrep -o "\"ERRORARRAY\":\[.*\]") |
| 46 | +if [[ $errors != "\"ERRORARRAY\":[]" ]]; then |
| 47 | + echo "Something went wrong: $errors" |
| 48 | + exit 1 |
| 49 | +fi |
0 commit comments