From 706e17b665190b9d8b67057d3f110ba28dffd026 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 14 Dec 2021 21:44:47 +0100 Subject: semi-working --- jatwisd | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- test.sh | 3 ++ 2 files changed, 108 insertions(+), 9 deletions(-) create mode 100755 test.sh diff --git a/jatwisd b/jatwisd index 71c2f40..b136c8d 100755 --- a/jatwisd +++ b/jatwisd @@ -1,5 +1,20 @@ #!/bin/sh +JATWIS_MODE="" +JATWIS_LOCKFILE="" +JATWIS_TIMEOUT=10000 +JATWIS_FREQUENCY=600 +JATWIS_FIRST_RUN=1 + +JATWIS_SCRIPT_LOCATION="" +JATWIS_URL="" + +function poll_default() { + echo "error: no poll handler for \"$JATWIS_MODE\", exiting..." + exit 1 +} +JATWIS_POLL_FN=poll_default + function usage() { cat << EOF Usage: jatwisd [options] @@ -13,32 +28,104 @@ Usage: jatwisd [options] -s, --sftp poll url using sftp - -e, --exec + -e, --exec script to run if the url is reachable - -t, --timeout + -t, --timeout timeout period (in milliseconds) + default: 10 seconds (10000) - -f, --freq, --frequency - polling frequency + -f, --freq, --frequency + polling frequency (in seconds) + default: every 10 minutes (600) - -l, --lockfile + -l, --lockfile set lockfile location and enable lockfile checking + + -p, --no-protection + disable protection agains endpoint success on first run See jatwisd(1) for more info EOF } +function check_ready() { + if [ -z "$JATWIS_URL" ]; then + echo "error: no endpoint to poll, exiting..." + exit 1 + elif [ -z "$JATWIS_SCRIPT_LOCATION" ]; then + echo "error: no script or executable specified, exiting..." + exit 1 + fi + + if [ ! -f "$JATWIS_SCRIPT_LOCATION" ]; then + if [ ! -x "$JATWIS_SCRIPT_LOCATION" ]; then + echo "error: script or executable not executable, exiting..." + exit 1 + fi + echo "error: script or executable not found, exiting..." + exit 1 + fi + + if [ "$JATWIS_MODE" == "HTTPS" ] && [[ "$JATWIS_URL" != https://* ]]; then + echo "error: https url without https:// protocol, exiting..." + exit 1 + fi +} + +function trigger() { + if [ $JATWIS_FIRST_RUN -eq 1 ]; then + echo "error: poll succeeded on first run, exiting..." + exit 1 + fi + + sh "$JATWIS_SCRIPT_LOCATION" +} + +function poll_https() { + curl -s -o /dev/null -w "%{http_code}" "$JATWIS_URL" + trigger +} + +# parse args while [ "$1" != "" ]; do - PARAM=`echo $1 | awk -F= '{print $1}'` - VALUE=`echo $1 | awk -F= '{print $2}'` - case $PARAM in + case $1 in --help) usage exit ;; + -h|--https) + JATWIS_URL="$2" + JATWIS_MODE="HTTPS" + JATWIS_POLL_FN=poll_https + shift + ;; + -s|--sftp) + JATWIS_URL="$2" + JATWIS_MODE="SFTP" + shift + ;; + -e|--exec) + JATWIS_SCRIPT_LOCATION="$2" + shift + ;; + -t|--timeout) + JATWIS_TIMEOUT="$2" + shift + ;; + -f|--freq|--frequency) + JATWIS_FREQUENCY="$2" + shift + ;; + -l|--lockfile) + JATWIS_LOCKFILE="$2" + shift + ;; + -p|--no-protection) + JATWIS_FIRST_RUN=0 + ;; *) - echo "ERROR: unknown parameter \"$PARAM\"" + echo "unknown parameter \"$PARAM\"" usage exit 1 ;; @@ -46,3 +133,12 @@ while [ "$1" != "" ]; do shift done +check_ready + +while :; do + $JATWIS_POLL_FN + + JATWIS_FIRST_RUN=0 + sleep "$JATWIS_FREQUENCY" +done + diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..30307c1 --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "this is an example script to test jatwisd" -- cgit v1.2.3