aboutsummaryrefslogtreecommitdiff
path: root/jatwisd
blob: b136c8d7d05a86de4aedda053ab526ad8a463905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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]

	--help
		display (this) help message

	-h, --https <https url>
		poll url using https

	-s, --sftp <sftp url>
		poll url using sftp

	-e, --exec <path>
		script to run if the url is reachable

	-t, --timeout <milliseconds>
		timeout period (in milliseconds)
		default: 10 seconds (10000)

	-f, --freq, --frequency <seconds>
		polling frequency (in seconds)
		default: every 10 minutes (600)

	-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
	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 "unknown parameter \"$PARAM\""
			usage
			exit 1
			;;
	esac
	shift
done

check_ready

while :; do
	$JATWIS_POLL_FN

	JATWIS_FIRST_RUN=0
	sleep "$JATWIS_FREQUENCY"
done