-
Notifications
You must be signed in to change notification settings - Fork 1
/
usg
executable file
·338 lines (285 loc) · 9.8 KB
/
usg
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env bash
# Utilities to make managing a Unifi Security Gateway a little easier.
#
# 1. $ usg config-refresh
# 2. $ usg dump > config.json
# 3. Make changes to USG
# 4. $ usg diff config.json
# 5. $ usg patch config.json default
# 6. Force provisioning in the Unifi UI
# 7. $ usg config-refresh
# 8. $ usg diff config.json
# 9. The previous usg diff fields show show up again, in addition to
# unifi.mgmt.cfgversion.
# ssh arguments to connect to the USG and the UniFi Controller. The arguments
# should be escaped, if necessary.
USG_CONNECTION="${USG_CONNECTION:-router}"
CONTROLLER_CONNECTION="${CONTROLLER_CONNECTION:-unifi}"
# https://github.com/evanphx/json-patch
# Currently relying on my forked version:
# https://github.com/evanphx/json-patch/pull/154
jsonpatch="${GOBIN:-"${GOPATH}/bin"}/json-patch"
# sudo apt install jq
jq="jq"
warn() {
echo "$@" 1>&2
}
die() {
warn "$@"
exit 1
}
# usg::dump dumps the current, complete USG config.gateway.json to stdout.
usg::dump() {
[[ $# -eq 0 ]] || die "dump does not take any arguments."
ssh -o loglevel=ERROR ${USG_CONNECTION} -- \
mca-ctrl -t dump-cfg
}
# usg::diff diffs the current, complete USG config.gateway.json with $1.
usg::diff() {
[[ $# -eq 1 ]] || die "diff takes exactly one argument: old config"
local old="$1"
"${jsonpatch}" create "${old}" <(usg::dump)
}
# usg::partial-copy copies the given JSON value from the USG's
# config.gateway.json to the Controller's config.gateway.json.
#
# The JSON values are identified by paths of the form "system.host-name".
#
# This command has two forms:
# 1) <site name> <JSON path>...
# This form copies the values from the USG's current, complete
# config.gateway.json.
# 2) <config file> <site name> <JSON path>...
# This form copies the values from the provided config file.
usg::partial-copy() {
[[ $# -ge 2 ]] || die "partial-copy needs at least two arguments: <site name> <JSON path>... or <config file> <site name> <JSON path>..."
local source_config
local site
local paths
if [[ -f "$1" ]]; then
source_config="$(cat "$1")"
if [[ $? != 0 ]]; then
die "Could not read $1"
fi
site="$2"
paths=( "${@:3}" )
else
source_config="$(usg::dump)"
if [[ $? != 0 ]]; then
die "Could not fetch the current, complete config.gateway.json from the USG"
fi
site="$1"
paths=( "${@:2}" )
fi
local dest_config
dest_config="$(usg::_fetch_controller_config "${site}")"
if [[ $? != 0 ]]; then
die "Could not fetch the current config.gateway.json from the controller."
fi
local original_dest_config="${dest_config}"
local path
for path in "${paths[@]}"; do
local source_value
source_value="$(usg::_extract_partial_json "${source_config}" "${path}")"
if [[ $? != 0 ]]; then
die "Did not find ${path} in config."
fi
local dest_value
dest_value="$(usg::_extract_partial_json "${dest_config}" "${path}")"
if [[ $? != 0 ]]; then
dest_value="{}"
fi
local patch
patch="$("${jsonpatch}" create <(echo "${dest_value}") <(echo "${source_value}"))"
if [[ $? != 0 ]]; then
die "Could not calculate patch for ${path}."
elif [[ "${patch}" == "{}" ]]; then
warn
warn "${path} has no diff, ignoring."
continue
fi
echo
echo "${path}"
echo
echo "${source_value}"
dest_config="$("${jsonpatch}" apply --indent " " -p <(echo "${patch}") <(echo "${dest_config}"))"
if [[ $? != 0 ]]; then
die "Could not update config.gateway.json."
fi
done
local diff
diff="$("${jsonpatch}" create <(echo "${original_dest_config}") <(echo "${dest_config}"))"
if [[ $? != 0 ]]; then
die "Could not calculate config.gateway.json diff."
elif [[ "${diff}" == "{}" ]]; then
die "config.gateway.json would not be updated."
fi
echo
echo
echo "Original controller config:"
echo "${original_dest_config}"
echo
echo "Updated controller config:"
echo "${dest_config}"
echo
echo "Diff:"
echo "${diff}"
echo
if dotfiles::user_permission "Do you want to commit these changes?"; then
usg::_set_controller_config "${site}" "${dest_config}"
if [[ $? != 0 ]]; then
die
fi
fi
}
# usg::patch determines what has changed in the USG's config by diffing its
# current, complete config.gateway.json with a locally stored copy (normally
# stored with usg::dump before modifying the USG's config), and then applies the
# diff to the Controller's config.gateway.json.
usg::patch(){
[[ $# -eq 2 ]] || die "patch takes exactly two arguments: old config, site name"
local old="$1"
local site="$2"
local patch
patch="$(usg::diff "${old}")"
if [[ $? != 0 ]]; then
die "diffing failed."
elif [[ "${patch}" == "{}" ]]; then
die "${old} appears to be the same as what's currently uploaded to the USG. There's nothing to do!"
fi
local controller_config
controller_config="$(usg::_fetch_controller_config "${site}")"
if [[ $? != 0 ]]; then
die "Could not fetch controller's config.gateway.json"
fi
echo
echo "Patch:"
echo
echo "${patch}"
echo
echo "Current config.gateway.json:"
echo
echo "${controller_config}"
echo
local patched
patched="$("${jsonpatch}" apply --indent " " -p <(echo "${patch}") <(echo "${controller_config}"))"
if [[ $? != 0 ]]; then
die "Could not apply patch to controller's config.gateway.json."
fi
echo "New config.gateway.json:"
echo
echo "${patched}"
echo
local effective_diff
effective_diff="$("${jsonpatch}" create <(echo "${controller_config}") <(echo "${patched}"))"
if [[ $? != 0 ]]; then
die "Could not calculate effective diff."
elif [[ "${effective_diff}" == "{}" ]]; then
die "It doesn't look like applying the patch to config.gateway.json would do anything. Does it already have the same values that the patch would be setting?"
fi
echo "Effective diff:"
echo
echo "${effective_diff}"
echo
if dotfiles::user_permission "Do you want to commit these changes?"; then
usg::_set_controller_config "${site}" "${patched}"
if [[ $? != 0 ]]; then
die
fi
fi
}
# usg::config-refresh refreshes the USG's config.gateway.json after a force
# provision to get rid of any transient differences that might exist (e.g.
# "123" -> 123).
usg::config-refresh() {
[[ $# == 0 ]] || die "config-refresh doesn't take any arguments."
local dir="$(dirname "${BASH_SOURCE}")"
ssh -o loglevel=ERROR ${USG_CONNECTION} -- \
"vbash -s" < "${dir}/usg_scripts/config_refresh.sh"
}
# usg::rollback rollsback the UniFi controller's config.gateway.json to its most
# recent config.gateway.json.old file.
usg::rollback() {
[[ $# == 1 ]] || die "rollback takes exactly one argument: site name"
local site="$1"
local controller_config_remote_path="$(usg::_controller_config_remote_path "${site}")"
ssh ${CONTROLLER_CONNECTION} -- \
cp "${controller_config_remote_path}" \
"${controller_config_remote_path}.rolled_back"
if [[ $? != 0 ]]; then
die "Something went wrong making a backup of the existing config.gateway.json"
fi
ssh ${CONTROLLER_CONNECTION} -- \
cp "${controller_config_remote_path}.old" \
"${controller_config_remote_path}"
if [[ $? != 0 ]]; then
die "Something went wrong rolling back config.gateway.json"
fi
}
usg::_controller_config_remote_path() {
[[ $# == 1 ]] || die "_controller_config_remote_path takes exactly one argument: site name"
local site="$1"
echo "/srv/unifi/data/sites/${site}/config.gateway.json"
}
usg::_fetch_controller_config() {
[[ $# == 1 ]] || die "_fetch_controller_config takes exactly one argument: site name"
ssh ${CONTROLLER_CONNECTION} -- \
cat "$(usg::_controller_config_remote_path "$1")"
}
usg::_set_controller_config() {
[[ $# == 2 ]] || die "_set_controller_config takes two arguments: site name, new config"
local site="$1"
local new_config="$2"
local controller_config_remote_path="$(usg::_controller_config_remote_path "${site}")"
ssh ${CONTROLLER_CONNECTION} -- \
cp "${controller_config_remote_path}" \
"${controller_config_remote_path}.old"
if [[ $? != 0 ]]; then
warn "Failed to backup existing config.gateway.json."
return 1
fi
ssh ${CONTROLLER_CONNECTION} -- \
"cat > \"${controller_config_remote_path}\"" <<< "${new_config}"
if [[ $? != 0 ]]; then
warn "Something went wrong uploading the patched config.gateway.json"
return 1
fi
# TODO: Automatically force provision the USG, then diff against the
# expected state. Rollback if something went wrong.
}
usg::_extract_partial_json() {
[[ $# == 2 ]] || die "_extract_partial_json takes two arguments: JSON and JSON path."
local json="$1"
local path="$2"
local IFS='.'
local parts=( ${path} )
unset IFS
local quoted_parts=()
local part
for part in "${parts[@]}"; do
quoted_parts+=( "\"${part}\"" )
done
local IFS='.'
path="$(echo "${quoted_parts[*]}")"
unset IFS
local value
local ret
value=$("${jq}" ".${path}" <<< "${json}")
ret=$?
if [[ ${ret} != 0 ]]; then
return $ret
fi
local i
for ((i=${#parts[@]} - 1; i >= 0; i--)); do
value="{\"${parts[$i]}\": ${value}}"
done
echo "${value}"
}
##########
# Main #
##########
if [[ "$1" = _* || "$(type -t "usg::$1")" != "function" ]]; then
echo "Unknown command $1"
exit 1
fi
"usg::$1" "${@:2}"