aboutsummaryrefslogtreecommitdiff
path: root/jatwisd
diff options
context:
space:
mode:
Diffstat (limited to 'jatwisd')
-rwxr-xr-xjatwisd114
1 files changed, 105 insertions, 9 deletions
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 <sftp url>
poll url using sftp
- -e, --exec
+ -e, --exec <path>
script to run if the url is reachable
- -t, --timeout
+ -t, --timeout <milliseconds>
timeout period (in milliseconds)
+ default: 10 seconds (10000)
- -f, --freq, --frequency
- polling frequency
+ -f, --freq, --frequency <seconds>
+ polling frequency (in seconds)
+ default: every 10 minutes (600)
- -l, --lockfile
+ -l, --lockfile <path>
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
+