Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env sh
- # Ensure your environment variable contains your API key:
- # VULTR_API_KEY="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- VULTR_API_BASEURL="https://api.vultr.com/v1"
- ######## Public functions #####################
- # add txt record to dns server
- dns_vultr_add() {
- fulldomain=$1
- txtvalue=$2
- _info "Using VULTR"
- _debug "Checking VULTR_API_KEY environment variable is set"
- if [ -z "$VULTR_API_KEY" ]; then
- _err "Missing VULTR API key"
- return 1
- fi
- _debug "VULTR API key found"
- _debug "First detect the root zone"
- if ! _get_root "$fulldomain"; then
- _err "Invalid domain"
- return 1
- fi
- # add our record to dns server
- _info "Attempting to add TXT record to DNS server"
- if ! _vultr_call_api POST "/dns/create_record" "domain=$_domain&name=$_sub_domain&type=TXT&ttl=300&data=\"$txtvalue\""; then
- _err "API call failed: $response"
- return 1
- fi
- # make sure we don't hit api rate limits
- sleep 1
- # check list of records for domain to see if we've added our txt record successfully
- _debug "Verifying we've added our TXT record"
- if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
- _err "API call failed: $response"
- return 1
- fi
- if [ $(echo $response | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -iPc "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"") -ne 0 ]; then
- _info "Successfully added TXT record: $fulldomain"
- return 0
- fi
- _err "Failed to create TXT record: $fulldomain"
- return 1
- }
- # remove txt record from dns server
- dns_vultr_rm() {
- fulldomain=$1
- txtvalue=$2
- _info "Using VULTR"
- _debug "Checking VULTR_API_KEY environment variable is set"
- if [ -z "$VULTR_API_KEY" ]; then
- _err "Missing VULTR API key"
- return 1
- fi
- _debug "VULTR API key found"
- _debug "First detect the root zone"
- if ! _get_root "$fulldomain"; then
- _err "Invalid domain"
- return 1
- fi
- _debug "domain: $_domain"
- _debug "subdomain: $_sub_domain"
- # get record id of TXT record matching $_sub_domain
- _debug "Searching for existing TXT record"
- if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
- _err "API call failed: $response"
- return 1
- fi
- _record_id=$(echo "$response" | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -iP "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"" | grep -oP "\"RECORDID\"\s*:\s*\K\d+")
- if [ -z "$_record_id" ]; then
- _err "Unable to find existing TXT record"
- return 1
- fi
- # use record id to delete the existing TXT record
- _info "Attempting to delete TXT record from DNS server"
- if ! _vultr_call_api POST "/dns/delete_record" "domain=$_domain&RECORDID=$_record_id"; then
- _err "API call failed: $response"
- return 1
- fi
- # make sure we don't hit api rate limits
- sleep 1
- # check list of records for domain to see if we've successfully removed our temporary txt record
- _debug "Verifying deletion of our TXT record"
- if ! _vultr_call_api GET "/dns/records?domain=$_domain"; then
- _err "API call failed: $response"
- return 1
- fi
- if [ $(echo $response | sed "s/},{/\n/g" | grep -iP "\"type\"\s*:\s*\"TXT\"" | grep -iP "\"name\"\s*:\s*\"$_sub_domain\"" | grep -icP "\"data\"\s*:\s*\"\\\\\"$txtvalue\\\\\"\"") -ne 0 ]; then
- _err "Failed to remove TXT record: $fulldomain"
- return 1
- fi
- _info "Successfully removed TXT record: $fulldomain"
- return 0
- }
- #################### Private functions below ##################################
- # get domain and subdomain parts from full domain
- #
- # param
- # domain=_acme-challenge.www.domain.com
- # returns
- # _sub_domain=_acme-challenge.www
- # _domain=domain.com
- _get_root() {
- domain=$1
- # get list of domains hosted by the dns server
- _debug "Getting list of existing domains"
- if ! _vultr_call_api GET "/dns/list"; then
- _err "API failure: Could not get list of domains"
- return 1
- fi;
- if [ -z "$response" ]; then
- _err "No domains hosted on DNS server"
- return 1
- fi;
- # parse response to get an array of domain names
- response=$(echo "$response" | grep -oP '"domain"\s*:\s*"\K[^"]+')
- read -r -a hosted_domains <<< $response
- # compare different parts of fulldomain with the list of domains hosted by dns server
- i=2
- p=1
- while true; do
- h=$(printf "%s" "$domain" | cut -d . -f $i-100)
- # no more parts left
- if [ -z "$h" ]; then
- return 1
- fi
- for dom in "${hosted_domains[@]}"; do
- if [ $dom == $h ]; then
- _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
- _domain=$h
- return 0
- fi
- done
- p=$i
- i=$(_math "$i" + 1)
- done
- return 1
- }
- # main vultr api method
- #
- # param
- # method=POST
- # call=/dns/create_record
- # data=var1="val1"&var2="va12"
- # method api_call data
- # returns
- # response
- _vultr_call_api() {
- method=$1
- api_call="$2"
- data="$3"
- _debug "API call: $api_call"
- _vultr_url="$VULTR_API_BASEURL$api_call"
- _debug2 _vultr_url "$_vultr_url"
- _secure_debug VULTR_API_KEY "$VULTR_API_KEY"
- # auth
- export _H1="API-Key: $VULTR_API_KEY"
- unset _H2 _H3 _H4 _H5
- if [ "$data" ] || [ "$method" = "POST" ]; then
- _debug data "$data"
- response="$(_post "$data" "$_vultr_url" "" "$method")"
- else
- response="$(_get "$_vultr_url")"
- fi
- if [ "$?" != "0" ] || _contains "$response" "Invalid API key"; then
- _err "Error: $response"
- return 1
- fi
- _debug2 response "$response"
- return 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement