-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: add helper to interact with janus admin API
- Loading branch information
1 parent
1c5f60b
commit 7e4b29f
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/bin/sh | ||
|
||
# exit on failure | ||
set -e | ||
|
||
# exit on unassigned variable | ||
set -u | ||
|
||
# variables | ||
janus_request="none" | ||
janus_options="none" | ||
janus_addr="${JANUS_HOST:-localhost}" | ||
janus_port="${JANUS_ADMIN_PORT:-7088}" | ||
janus_pass="${JANUS_ADMIN_SECRET:-janusoverlord}" | ||
|
||
# define usage | ||
usage() { | ||
cat <<EOF | ||
usage: $0 [-h] [-a JANUS_ADDR] [-p JANUS_ADMIN_PORT] [-s JANUS_ADMIN_SECRET] [-o NAME=VALUE] -r REQUEST | ||
-h show this help message | ||
-r janus request (required) | ||
-o janus request optional parameter (default: ${janus_options}) | ||
-a janus server address (default: ${janus_addr}) | ||
-p janus HTTP admin port (default: ${janus_port}) | ||
-s janus admin secret (default: ${janus_pass}) | ||
EOF | ||
|
||
exit ${1:-1} | ||
} | ||
|
||
# define fatal | ||
fatal() { | ||
printf "fatal: $*\n\n" >&2 | ||
usage 1 | ||
} | ||
|
||
# get random string | ||
rand_str() { | ||
length=${1:-32} | ||
LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w ${length} | head -n 1 | ||
} | ||
|
||
# parse parameters | ||
while getopts "ha:p:s:o:r:" opt; do | ||
case $opt in | ||
h) usage 0 ;; | ||
r) janus_request="${OPTARG}" ;; | ||
o) janus_options="${janus_options},${OPTARG}" ;; | ||
a) janus_addr="${OPTARG}" ;; | ||
p) janus_port="${OPTARG}" ;; | ||
s) janus_pass="${OPTARG}" ;; | ||
esac | ||
done | ||
|
||
# check parameters | ||
if [ "${janus_request}" = "none" ]; then | ||
fatal "Janus request parameter is mandatory" | ||
fi | ||
|
||
# parse optional parameter | ||
http_session_id= | ||
http_handle_id= | ||
http_payload_opts="" | ||
for opt in $(echo ${janus_options} | sed 's/,/ /g'); do | ||
if [ "${opt}" = "none" ]; then | ||
continue | ||
fi | ||
|
||
opt_name="$(echo ${opt} | cut -d= -f1)" | ||
opt_value="$(echo ${opt} | cut -d= -f2-)" | ||
|
||
# append double-quotes to JSON strings | ||
if echo "${opt_value}" | grep -qE '^([0-9]+|true|false|null)$'; then | ||
http_payload_opts="${http_payload_opts}\"${opt_name}\": ${opt_value}," | ||
else | ||
http_payload_opts="${http_payload_opts}\"${opt_name}\": \"${opt_value}\"," | ||
fi | ||
|
||
if [ "${opt_name}" = "session_id" ]; then | ||
http_session_id="/${opt_value}" | ||
elif [ "${opt_name}" = "handle_id" ]; then | ||
http_handle_id="/${opt_value}" | ||
fi | ||
done | ||
|
||
# prepare payload | ||
http_payload=$(cat <<EOF | ||
{ | ||
"janus": "${janus_request}", | ||
${http_payload_opts} | ||
"transaction": "$(rand_str 12)", | ||
"admin_secret": "${janus_pass}" | ||
} | ||
EOF | ||
) | ||
|
||
# send request | ||
curl \ | ||
--silent \ | ||
--fail \ | ||
--show-error \ | ||
--write-out '\n' \ | ||
--data "${http_payload}" \ | ||
http://${janus_addr}:${janus_port}/admin${http_session_id}${http_handle_id} |