-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskrunner
executable file
·111 lines (87 loc) · 2.67 KB
/
taskrunner
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
#!/usr/bin/env bash
# ----------------------------------------------------------------------
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# --
# Copyright 2018-2021 Andi Dittrich <https://aenon-dynamics.com>
# ----------------------------------------------------------------------
set -o pipefail
# taskrunner-sh https://github.com/AenonDynamics/taskrunner-sh
VERSION="1.0.0"
show_usage(){
cat << EOF
Usage: taskrunner <name> <executable> [args...]
--help Displays this help
--version Displays version
EOF
exit 0
}
show_version(){
cat << EOF
taskrunner-sh
- Version: ${VERSION}
- License: MPL-2.0 http://mozilla.org/MPL/2.0/
- Repository: https://github.com/AenonDynamics/taskrunner-sh
EOF
exit 0
}
# simple processing because of variadic arguments
[ "$1" = "--help" ] && show_usage
[ "$1" = "--version" ] && show_version
# task config available ?
[ ! -f "/etc/taskrunner.conf" ] && { echo >&2 "configuration file </etc/taskrunner.conf> not found"; exit 1; }
# load config
. /etc/taskrunner.conf
# extract task title
TASKNAME=${1}
# extract command
CMD=${2}
# taskname given ?
[ -z "${TASKNAME}" ] && { echo >&2 "taskname not set"; exit 1; }
# command given ?
[ -z "${CMD}" ] && { echo >&2 "executable not set"; exit 1; }
# command valid ?
command -v ${CMD} >/dev/null 2>&1 || { echo >&2 "executable <${CMD}> not found"; exit 1; }
# remove first+second argument from list and extract args
shift
shift
ARGS=$@
# create logfile
LOGFILE=$(mktemp)
trap "rm ${LOGFILE}" EXIT
# get command start time
T_START=$(date +%s)
# run command and capture output
${CMD} ${ARGS} 2>&1 | tee ${LOGFILE}
# store status code
TASK_STATUS=$?
# get command stop time
T_STOP=$(date +%s)
# send data - hide output: fail on http status >=400
# set api key within header
# avoid curl "Argument list too long" issue - use stdin
# use empty "Expect" header to avoid HTTP417 issues
curl \
--fail \
--silent \
--show-error \
--http1.1 \
--header "Expect: " \
--header "X-APIKEY: ${API_KEY}" \
--data-urlencode "title=${TASKNAME}" \
--data-urlencode "status=${TASK_STATUS}" \
--data-urlencode "t0=${T_START}" \
--data-urlencode "t1=${T_STOP}" \
--data-urlencode "content@-" \
--request POST \
"${API_URL}" \
> /dev/null < ${LOGFILE}
# store status code
LOG_STATUS=$?
# curl operation success?
[ ${LOG_STATUS} -ne 0 ] && { echo >&2 "tasklogging failed"; }
# command success ?
[ ${TASK_STATUS} -ne 0 ] && { echo >&2 "operation failed"; exit 1; }
# default exit code
exit 0