-
Notifications
You must be signed in to change notification settings - Fork 2
/
dnsimple
executable file
·227 lines (184 loc) · 6.68 KB
/
dnsimple
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# License: See LICENSE
set -e
declare _api="https://api.dnsimple.com/v2"
declare _config=$(mktemp -t dnsimple)
declare _json_data=$(mktemp -t dnsimple_json)
declare _debug=$(mktemp -t dnsimple_debug)
declare _api_out=$(mktemp -t dnsimple_curl)
declare _jq_single='.'
declare _jq_list='.'
declare -a _output
#-----------------------------------------------------------------------------
cleanup() {
local _exit_code="$1"
[[ -n "$_exit_code" ]] || _exit_code=0
for f in "$_config" "$_json_data" "$_api_out" "$_debug" ; do
if [[ -f "$f" ]] ; then
[[ "$_exit_code" -gt 0 ]] && cat "$f"
rm "$f"
fi
done
exit "$_exit_code"
}
#-----------------------------------------------------------------------------
usage() {
cat <<USAGE
Usage: ${0##*/} <command> [options] [command specific options]
OPTIONS:
-h, --help This.
--dat, Specify a \$DNSIMPLE_ACCOUNT_TOKEN, in lieu of or to
--account-token override the setting from your environment.
You can create 'User access token' on your dnsimple.com
User Settings page.
--api Override the default v2 API URL. The default is:
$_api
--compact Don't show the full json output.
COMMANDS:
whoami Prints info for your \$DNSIMPLE_ACCOUNT_TOKEN.
zones Prints available zones.
zone_info Prints info for a specific zone.
-z, --zone <zone name>
zone_records Prints records for a specific zone.
-z, --zone <zone name>
zone_record Prints info for a specific zone record.
-z, --zone <zone name>
-r, --record <record name>
zone_record_id Prints the id for a specific zone record.
-z, --zone <zone name>
-r, --record <record name>
update_a_record Creates/Updates the zone record with an IP address.
Specifying an IP address of "auto" will detect and use
the current public internet IP.
-z, --zone <zone name>
-r, --record <record name>
-i, --ip <IP or "auto">
You need to have 'dig', 'curl', and 'jq' installed and in your \$PATH.
https://curl.haxx.se/
https://stedolan.github.io/jq/
USAGE
cleanup -1
}
#-----------------------------------------------------------------------------
# API
typeset -t dnsimple_api
dnsimple_api() {
local _resource="$1"
local _method="$2"
local -i _per_page=100
local _url="${_api}/${_resource}?per_page=${_per_page}"
local -a _args=(
[1]="silent"
[2]="header = \"Authorization: Bearer ${DNSIMPLE_ACCOUNT_TOKEN}\""
[3]="header = \"Accept: application/json\""
)
[[ -n "$_method" ]] && _args+=([10]="header = \"Content-Type: application/json\"" [11]="request = \"${_method}\"")
[[ -s "$_json_data" ]] && _args+=([20]="data = \"@${_json_data}\"")
_args+=([30]="url = \"${_url}\"")
local -i _total_pages=1
for (( _current_page = 1 ; _current_page <= _total_pages ; _current_page++ )) ; do
_args[30]="url = \"${_url}&page=${_current_page}\""
$(IFS=$'\n' ; echo "${_args[*]}" > "$_config")
curl --config "$_config" --output "$_api_out" >& "$_debug"
_output+=$(cat "$_api_out")
_total_pages=$(jq -cMj ".pagination.total_pages" < "$_api_out")
done
}
#-----------------------------------------------------------------------------
jq_single() {
for _out in "${_output[@]}" ; do
echo "$_out" | jq "$_jq_single"
done
}
#-----------------------------------------------------------------------------
jq_list() {
for _out in "${_output[@]}" ; do
echo "$_out" | jq "$_jq_list"
done
}
#-----------------------------------------------------------------------------
_whoami() {
dnsimple_api "whoami" && jq_single
}
#-----------------------------------------------------------------------------
_zones() {
dnsimple_api "${DAI}/domains" && jq_list
}
#-----------------------------------------------------------------------------
_zone_info() {
[[ -n "$_zone" ]] || usage
dnsimple_api "${DAI}/zones/${_zone}" && jq_single
}
#-----------------------------------------------------------------------------
_zone_records() {
[[ -n "$_zone" ]] || usage
dnsimple_api "${DAI}/zones/${_zone}/records" && jq_list
}
#-----------------------------------------------------------------------------
_zone_record() {
[[ -n "$_zone" && -n "$_record" ]] || usage
dnsimple_api "${DAI}/zones/${_zone}/records" && jq ".data[] | select(.name == \"${_record}\")" < "$_api_out"
}
#-----------------------------------------------------------------------------
_zone_record_id() {
[[ -n "$_zone" && -n "$_record" ]] || usage
_zone_record "$DAI" "$_zone" "$_record" | jq -cMj ".id" ; echo
}
#-----------------------------------------------------------------------------
_update_a_record() {
[[ -n "$_zone" && -n "$_record" && -n "$_ip" ]] || usage
local _id=$(_zone_record_id "$DAI" "$_zone" "$_record")
local _method="POST"
[[ -n "$_id" ]] && _method="PATCH"
echo "{\"name\":\"${_record}\",\"type\":\"A\",\"content\":\"${_ip}\"}" > "$_json_data"
dnsimple_api "${DAI}/zones/${_zone}/records/${_id}" "${_method}" && jq_single
}
#-----------------------------------------------------------------------------
# Main program
[[ $# -eq 0 ]] && usage
for _cli in curl jq dig ; do
command -v "$_cli" >/dev/null 2>/dev/null || usage
done
declare _command
_command="$1" ; shift
declare _zone
declare _record
declare _ip
while [[ $# -gt 0 ]] ; do
declare _arg="$1" && shift
case "$_arg" in
-c|--compact)
_jq_single='.data'
_jq_list='.data[]'
;;
*)
declare _next="$1" && shift
case "$_arg" in
--dat|--account-token) DNSIMPLE_ACCOUNT_TOKEN="$_next" ;;
--api) _api="$_next" ;;
-z|--zone) _zone="$_next" ;;
-r|--record) _record="$_next" ;;
-i|--ip) _ip="$_next" ;;
*) usage ;;
esac
;;
esac
done
[[ -n "$DNSIMPLE_ACCOUNT_TOKEN" ]] || usage
if [[ -n "$_ip" && "$_ip" == "auto" ]] ; then
_ip=$(dig +short myip.opendns.com @resolver1.opendns.com || curl -f4L http://ifconfig.me/ || curl -f4L http://icanhazip.com/)
fi
for _signal in 0 1 ; do
trap "cleanup $_signal" $_signal
done
declare DAI=$(dnsimple_api "whoami" && jq '.data.account.id' < "$_api_out")
case "$_command" in
whoami) _whoami ;;
zones) _zones ;;
zone_info) _zone_info ;;
zone_records) _zone_records ;;
zone_record) _zone_record ;;
zone_record_id) _zone_record_id ;;
update_a_record) _update_a_record ;;
*) usage ;;
esac