Skip to content

Instantly share code, notes, and snippets.

@whiteinge
Last active August 30, 2017 18:37
Show Gist options
  • Save whiteinge/a646d4d937960d3ef306 to your computer and use it in GitHub Desktop.
Save whiteinge/a646d4d937960d3ef306 to your computer and use it in GitHub Desktop.
A POSIX sh implementation of Salt's eventlisten.py script. Uses salt-api's event HTTP stream.
#!/bin/sh
URL="http://localhost:8000"
COOKIES="cookies.txt"
fnmatch () { case "$2" in $1) return 0 ;; *) return 1 ;; esac ; }
listen() {
# Watch Salt's events HTTP stream.
events='event_stream'
mkfifo $events
curl -b "${COOKIES}" -N -sS "$URL/events" >$events &
curl_pid=$!
trap '
excode=$?; trap - EXIT;
exec 5>&-
kill '"${curl_pid}"'
rm '"${events}"'
rm '"${COOKIES}"'
exit
echo $excode
' INT TERM EXIT
tag=""
data=""
while read -r line; do
if fnmatch 'tag: *' "$line"; then
tag="${line#tag: *}"
elif fnmatch 'data: *' "$line"; then
data="${line#data: *}"
# Only listen for job return events.
if fnmatch 'salt/job/*/ret/*' "$tag"; then
printf '%s\n%s\n\n' "$tag" "$data"
fi
fi
done < $events
wait
}
login() {
curl -c "${COOKIES}" -sSi "${URL}/login" \
-d username='saltdev' -d password='saltdev' -d eauth='auto' \
| head -1 | grep -q '200 OK'
[ $? -eq 0 ] || exit $?
}
login
listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment