Skip to content

Commit 7e4b29f

Browse files
committed
utils: add helper to interact with janus admin API
1 parent 1c5f60b commit 7e4b29f

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

utils/janus-admin-cli

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/bin/sh
2+
3+
# exit on failure
4+
set -e
5+
6+
# exit on unassigned variable
7+
set -u
8+
9+
# variables
10+
janus_request="none"
11+
janus_options="none"
12+
janus_addr="${JANUS_HOST:-localhost}"
13+
janus_port="${JANUS_ADMIN_PORT:-7088}"
14+
janus_pass="${JANUS_ADMIN_SECRET:-janusoverlord}"
15+
16+
# define usage
17+
usage() {
18+
cat <<EOF
19+
usage: $0 [-h] [-a JANUS_ADDR] [-p JANUS_ADMIN_PORT] [-s JANUS_ADMIN_SECRET] [-o NAME=VALUE] -r REQUEST
20+
21+
-h show this help message
22+
-r janus request (required)
23+
-o janus request optional parameter (default: ${janus_options})
24+
-a janus server address (default: ${janus_addr})
25+
-p janus HTTP admin port (default: ${janus_port})
26+
-s janus admin secret (default: ${janus_pass})
27+
EOF
28+
29+
exit ${1:-1}
30+
}
31+
32+
# define fatal
33+
fatal() {
34+
printf "fatal: $*\n\n" >&2
35+
usage 1
36+
}
37+
38+
# get random string
39+
rand_str() {
40+
length=${1:-32}
41+
LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w ${length} | head -n 1
42+
}
43+
44+
# parse parameters
45+
while getopts "ha:p:s:o:r:" opt; do
46+
case $opt in
47+
h) usage 0 ;;
48+
r) janus_request="${OPTARG}" ;;
49+
o) janus_options="${janus_options},${OPTARG}" ;;
50+
a) janus_addr="${OPTARG}" ;;
51+
p) janus_port="${OPTARG}" ;;
52+
s) janus_pass="${OPTARG}" ;;
53+
esac
54+
done
55+
56+
# check parameters
57+
if [ "${janus_request}" = "none" ]; then
58+
fatal "Janus request parameter is mandatory"
59+
fi
60+
61+
# parse optional parameter
62+
http_session_id=
63+
http_handle_id=
64+
http_payload_opts=""
65+
for opt in $(echo ${janus_options} | sed 's/,/ /g'); do
66+
if [ "${opt}" = "none" ]; then
67+
continue
68+
fi
69+
70+
opt_name="$(echo ${opt} | cut -d= -f1)"
71+
opt_value="$(echo ${opt} | cut -d= -f2-)"
72+
73+
# append double-quotes to JSON strings
74+
if echo "${opt_value}" | grep -qE '^([0-9]+|true|false|null)$'; then
75+
http_payload_opts="${http_payload_opts}\"${opt_name}\": ${opt_value},"
76+
else
77+
http_payload_opts="${http_payload_opts}\"${opt_name}\": \"${opt_value}\","
78+
fi
79+
80+
if [ "${opt_name}" = "session_id" ]; then
81+
http_session_id="/${opt_value}"
82+
elif [ "${opt_name}" = "handle_id" ]; then
83+
http_handle_id="/${opt_value}"
84+
fi
85+
done
86+
87+
# prepare payload
88+
http_payload=$(cat <<EOF
89+
{
90+
"janus": "${janus_request}",
91+
${http_payload_opts}
92+
"transaction": "$(rand_str 12)",
93+
"admin_secret": "${janus_pass}"
94+
}
95+
EOF
96+
)
97+
98+
# send request
99+
curl \
100+
--silent \
101+
--fail \
102+
--show-error \
103+
--write-out '\n' \
104+
--data "${http_payload}" \
105+
http://${janus_addr}:${janus_port}/admin${http_session_id}${http_handle_id}

0 commit comments

Comments
 (0)