Skip to content

Commit bc1c534

Browse files
committed
utils: add helper to interact with janus admin API
1 parent 6ecabec commit bc1c534

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

utils/janus-admin-cli

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

0 commit comments

Comments
 (0)