mirror of
https://github.com/aljazceru/cyphernode.git
synced 2025-12-18 13:14:56 +01:00
First draft of the pub/sub notifier
This commit is contained in:
38
notifier_docker/script/requesthandler.sh
Normal file
38
notifier_docker/script/requesthandler.sh
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/bin/sh
|
||||
|
||||
. ./trace.sh
|
||||
. ./web.sh
|
||||
. ./response.sh
|
||||
|
||||
main() {
|
||||
trace "Entering main()..."
|
||||
|
||||
local msg
|
||||
local cmd
|
||||
local response
|
||||
local response_topic
|
||||
|
||||
while read msg; do
|
||||
trace "[main] msg=${msg}"
|
||||
|
||||
cmd=$(echo ${msg} | jq ".cmd" | tr -d '"')
|
||||
trace "[main] cmd=${cmd}"
|
||||
|
||||
response_topic=$(echo ${msg} | jq '."response-topic"' | tr -d '"')
|
||||
trace "[main] response_topic=${response_topic}"
|
||||
|
||||
case "${cmd}" in
|
||||
web)
|
||||
response=$(web "${msg}")
|
||||
publish_response "${response}" "${response_topic}" ${?}
|
||||
trace "[main] PR"
|
||||
;;
|
||||
esac
|
||||
trace "[main] case finished"
|
||||
done
|
||||
}
|
||||
|
||||
export TRACING=1
|
||||
|
||||
main
|
||||
exit $?
|
||||
22
notifier_docker/script/response.sh
Normal file
22
notifier_docker/script/response.sh
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
|
||||
. ./trace.sh
|
||||
|
||||
publish_response() {
|
||||
trace "Entering publish_response()..."
|
||||
|
||||
local response=${1}
|
||||
local response_topic=${2}
|
||||
local returncode=${3}
|
||||
|
||||
trace "[publish_response] response=${response}"
|
||||
trace "[publish_response] response_topic=${response_topic}"
|
||||
trace "[publish_response] returncode=${returncode}"
|
||||
|
||||
trace "[publish_response] mosquitto_pub -h broker -t \"${response_topic}\" -m \"${response}\""
|
||||
mosquitto_pub -h broker -t "${response_topic}" -m "${response}"
|
||||
returncode=$?
|
||||
trace_rc ${returncode}
|
||||
|
||||
return ${returncode}
|
||||
}
|
||||
5
notifier_docker/script/startnotifier.sh
Normal file
5
notifier_docker/script/startnotifier.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
. ./trace.sh
|
||||
|
||||
mosquitto_sub -h broker -t notifier | ./requesthandler.sh
|
||||
15
notifier_docker/script/trace.sh
Normal file
15
notifier_docker/script/trace.sh
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
|
||||
trace()
|
||||
{
|
||||
if [ -n "${TRACING}" ]; then
|
||||
echo "$(date -Is) $$ ${1}" 1>&2
|
||||
fi
|
||||
}
|
||||
|
||||
trace_rc()
|
||||
{
|
||||
if [ -n "${TRACING}" ]; then
|
||||
echo "$(date -Is) $$ Last return code: ${1}" 1>&2
|
||||
fi
|
||||
}
|
||||
76
notifier_docker/script/web.sh
Normal file
76
notifier_docker/script/web.sh
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/bin/sh
|
||||
|
||||
. ./trace.sh
|
||||
|
||||
web() {
|
||||
trace "Entering web()..."
|
||||
|
||||
local msg=${1}
|
||||
local url
|
||||
local body
|
||||
local returncode
|
||||
local http_code
|
||||
local result
|
||||
|
||||
trace "[web] msg=${msg}"
|
||||
url=$(echo ${msg} | jq ".url")
|
||||
trace "[web] url=${url}"
|
||||
|
||||
body=$(echo ${msg} | jq -e ".body")
|
||||
# jq -e will have a return code of 1 if the supplied tag is null.
|
||||
if [ "$?" -eq "0" ]; then
|
||||
# body tag not null, so it's a POST
|
||||
trace "[web] body=${body}"
|
||||
else
|
||||
body=
|
||||
trace "[web] no body, GET request"
|
||||
fi
|
||||
|
||||
http_code=$(curl_it "${url}" "${body}")
|
||||
returncode=$?
|
||||
trace_rc ${returncode}
|
||||
|
||||
if [ "${returncode}" -eq "0" ]; then
|
||||
# {"result":"success", "response":"<html></html>"}
|
||||
result="success"
|
||||
else
|
||||
# {"result":"error", "response":"<html></html>"}
|
||||
result="error"
|
||||
fi
|
||||
|
||||
echo "{\"result\":\"${result}\",\"http_code\":\"${http_code}\"}"
|
||||
|
||||
return ${returncode}
|
||||
}
|
||||
|
||||
curl_it() {
|
||||
trace "Entering curl_it()..."
|
||||
|
||||
local url=$(echo "${1}" | tr -d '"')
|
||||
local data=${2}
|
||||
local returncode
|
||||
|
||||
if [ -n "${data}" ]; then
|
||||
trace "[curl_it] curl -o /dev/null -w \"%{http_code}\" -H \"Content-Type: application/json\" -H \"X-Forwarded-Proto: https\" -d ${data} ${url}"
|
||||
rc=$(curl -o /dev/null -w "%{http_code}" -H "Content-Type: application/json" -H "X-Forwarded-Proto: https" -d ${data} ${url})
|
||||
returncode=$?
|
||||
else
|
||||
trace "[curl_it] curl -o /dev/null -w \"%{http_code}\" ${url}"
|
||||
rc=$(curl -o /dev/null -w "%{http_code}" ${url})
|
||||
returncode=$?
|
||||
fi
|
||||
trace "[curl_it] HTTP return code=${rc}"
|
||||
trace_rc ${returncode}
|
||||
|
||||
echo "${rc}"
|
||||
|
||||
if [ "${returncode}" -eq "0" ]; then
|
||||
if [ "${rc}" -lt "400" ]; then
|
||||
return 0
|
||||
else
|
||||
return ${rc}
|
||||
fi
|
||||
else
|
||||
return ${returncode}
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user