blob: dfeac61851789bbf7f2c826913a926e004b84bf0 (
plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
|