diff options
Diffstat (limited to 'roles/domain-zone-management/templates/update_ovhcloud_domain_zone')
| -rwxr-xr-x | roles/domain-zone-management/templates/update_ovhcloud_domain_zone | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/roles/domain-zone-management/templates/update_ovhcloud_domain_zone b/roles/domain-zone-management/templates/update_ovhcloud_domain_zone new file mode 100755 index 0000000..dfeac61 --- /dev/null +++ b/roles/domain-zone-management/templates/update_ovhcloud_domain_zone @@ -0,0 +1,156 @@ +#!/bin/sh + + +SCRIPT="$0" +SCRIPT_DIR=$(cd "${SCRIPT%/*}" && pwd ) +SCRIPT_NAME=${SCRIPT##*/} +APPEND_RECORDS_DIR="{{ ovh_domain_zone_append_records_directory }}" +REPLACE_RECORDS_DIR="{{ ovh_domain_zone_replace_records_directory }}" +mkdir -p ${APPEND_RECORDS_DIR} +mkdir -p ${REPLACE_RECORDS_DIR} + +cut_nth_word() +{ + echo "$1" | cut -d ' ' -f "$2" +} + +get_record_subdomain() +{ + record="$1" + subdomain="$(cut_nth_word "$record" 1)" + if test "$(echo "$subdomain" | grep -q [A-Z])" + then + subdomain="$(cut_nth_word "$record" 2)" + fi + if test "$subdomain" == "@" + then + subdomain="" + fi + echo "$subdomain" +} + +get_record_type() +{ + record="$1" + record_type="$(cut_nth_word "$record" 2)" + if test "$(echo "$record_type" | grep -q [0-9])" + then + record_type="$(cut_nth_word "$record" 1)" + fi + echo "$record_type" +} + +get_record_target() +{ + record="$1" + target_and_ttl="$(cut_nth_word "$record" "3-")" + target="$(cut_nth_word "$(echo "$target_and_ttl" | rev)" "2-" | rev)" + echo "$target" +} + +get_record_ttl() +{ + record="$1" + ttl="$(cut_nth_word "$(echo "$record" | rev)" 1 | rev)" + echo "$ttl" +} + +find_existing_record_ids() +{ + subdomain="$1" + record_type="$2" + target="$3" + query=".subDomain == \"${subdomain}\" and .fieldType == \"${record_type}\"" + if [ -n "$target" ] + then + query="${query} and .target == ${target}" + fi + ovhcloud domain-zone record list joeac.net -o json \ + | jq "map(select($query))" \ + | jq "map(.id)" \ + | grep -Eo [0-9]\+ +} + +append_record() +{ + subdomain="$1" + record_type="$2" + target="$3" + ttl="$4" + echo "Appending record to joeac.net zone: ${record_type} ${subdomain:-@} ${target} ${ttl}" + ovhcloud domain-zone record create joeac.net \ + --field-type "$record_type" \ + --sub-domain "$subdomain" \ + --target "$target" \ + --ttl "$ttl" +} + +replace_records() +{ + subdomain="$1" + target="$2" + ttl="$3" + first_existing_record_id="$4" + echo "Replacing record ${first_existing_record_id} in joeac.net zone: ${record_type} ${subdomain:-@} ${target} ${ttl}" + ovhcloud domain-zone record update joeac.net "$first_existing_record_id" \ + --sub-domain "${subdomain}" \ + --target "${target}" \ + --ttl "${ttl}" + shift 4 + excess_record_ids="$@" + if [ -n "$excess_record_ids" ] + then + for id in "$excess_record_ids" + do + echo "Removing record ${id} in joeac.net zone: ${record_type} ${subdomain:-@}" + ovhcloud domain-zone record delete joeac.net "$id" + done + fi +} + +if test $(ls ${APPEND_RECORDS_DIR} | wc -l) -eq 0 +then + echo "Nothing to append: no zone files in ${APPEND_RECORDS_DIR}" +else + for zone_file in ${APPEND_RECORDS_DIR}/* + do + while read record + do + subdomain="$(get_record_subdomain "$record")" + record_type="$(get_record_type "$record")" + target="$(get_record_target "$record")" + ttl="$(get_record_ttl "$record")" + if test -z "$(find_existing_record_ids "$subdomain" "$record_type" "$target")" + then + append_record "$subdomain" "$record_type" "$target" "$ttl" + fi + done < "${zone_file}" + done +fi + +if test $(ls ${REPLACE_RECORDS_DIR} | wc -l) -eq 0 +then + echo "Nothing to replace: no zone files in ${REPLACE_RECORDS_DIR}" + exit 0 +else + for zone_file in ${REPLACE_RECORDS_DIR}/* + do + while read record + do + subdomain="$(get_record_subdomain "$record")" + record_type="$(get_record_type "$record")" + target="$(get_record_target "$record")" + ttl="$(get_record_ttl "$record")" + + existing_record_ids="$(find_existing_record_ids "$subdomain" "$record_type")" + if test -n "$existing_record_ids" + then + replace_records "$subdomain" "$target" "$ttl" "$existing_record_ids" + else + append_record "$subdomain" "$record_type" "$target" "$ttl" "$existing_record_ids" + fi + done < "${zone_file}" + done +fi + +ovhcloud domain-zone refresh joeac.net |
