-
Notifications
You must be signed in to change notification settings - Fork 8
/
gomngr.sh
executable file
·189 lines (161 loc) · 4.46 KB
/
gomngr.sh
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Env variables
# GOMNGRSCRIPTDIR: [mandatory] directory to search for .update files
# GOMNGRURL: [optional] my-app url to send script execution status
# SENTRY_DSN: [optional] sentry url for script error execution reports
# GOMNGRARCHIVEDIR : [optional] directory where .done and .log should be moved after 10 days
EXIT_REQUEST=0
function catch_sig()
{
echo "Received signal, exiting as soon as possible"
EXIT_REQUEST=1
}
trap catch_sig SIGINT SIGTERM
if [ -e /root/.env ]; then
. /root/.env
fi
MYDIR=""
if [ -z $GOMNGRSCRIPTDIR ]; then
MYDIR="/opt/my/scripts"
echo "No GOMNGRSCRIPTDIR given, using default $MYDIR"
else
MYDIR=$GOMNGRSCRIPTDIR
fi
if [ "a$MYDIR" == "a" ]; then
echo "Missing script directory parameter"
exit 1
fi
MYURL=""
if [ -z $GOMNGRURL ]; then
echo "Missing GOMNGRURL url (http://x.y.z) env variable"
else
MYURL=$GOMNGRURL
fi
echo "$(date) Execute cron tasks"
if [ -e /tmp/gomngr.lock ]; then
echo "gomngr locked"
exit 1
fi
TOMORROW=`date --date="1 day 05:00:00" +%s`
NEXTMONTH=`date --date="$(date +'%Y-%m-01') + 1 month 05:00:00" +%s`
while true; do
if [ -e $MYDIR/gomngr.exit ]; then
echo "Found $MYDIR/gomngr.exit, exiting on user request"
exit 1
fi
NBFILES=0
NOW=`date +%s`
echo "[date=$(date)] check pending tasks"
if [ -e /tmp/gomngr.lock ]; then
echo "gomngr locked"
sleep 30
continue
fi
touch /tmp/gomngr.lock
ERRCODE=0
ls $MYDIR/*.update | sort -n -t . -k 2 > /tmp/gomngr.list
while read p; do
((NBFILES++))
$p &> $p.log
EXITCODE=$?
echo "Exit code: $EXITCODE" >> $p.log
filename=$(basename $p)
if [ $EXITCODE -ne 0 ]; then
ERRCODE=$EXITCODE
echo "Got an error" >> $p.log
touch $p.err
if [ "a$SENTRY_DSN" != "a" ]; then
echo "Send sentry event" >> $p.log
/usr/local/bin/sentry-cli send-event -m "$p execution failure" --logfile $p.log
else
echo "no sentry, skip..." >> $p.log
fi
fi
if [ "a$MYURL" == "a" ]; then
echo "no my url available, skip status update call"
else
echo "send status code to $MYURL/log/status/$filename/$EXITCODE" >> $p.log
curl -m 10 --connect-timeout 2 "$MYURL/log/status/$filename/$EXITCODE"
fi
mv $p $p.done
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
done </tmp/gomngr.list
echo "${NBFILES} tasks handled"
if [ $NOW -gt $NEXTMONTH ]; then
echo "${NOW}: time for monthly tasks"
NEXTMONTH=`date --date="$(date +'%Y-%m-01') + 1 month 05:00:00" +%s`
#echo "Check for account upcoming expiration"
#/opt/crontask.sh test_expiration
#if [ $EXIT_REQUEST -eq 1 ]; then
# rm /tmp/gomngr.lock
# rm /tmp/gomngr.list
# echo "Exit requested"
# exit 0
#fi
echo "Check for account expiration"
/opt/crontask.sh expire
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
fi
if [ $NOW -gt $TOMORROW ]; then
echo "${NOW}: time for daily tasks"
TOMORROW=`date --date="1 day 05:00:00" +%s`
echo "Check for reservation removal"
/opt/crontask.sh reservation_remove
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
echo "Check for reservation creation"
/opt/crontask.sh reservation_create
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
echo "Check for account upcoming expiration"
/opt/crontask.sh test_expiration
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
echo "Check for project upcoming expiration"
/opt/crontask.sh test_projects
if [ $EXIT_REQUEST -eq 1 ]; then
rm /tmp/gomngr.lock
rm /tmp/gomngr.list
echo "Exit requested"
exit 0
fi
fi
ARCHIVE="${GOMNGRARCHIVEDIR:-/opt/my/archive}"
echo "Archive old script to $ARCHIVE"
if [ -e $ARCHIVE ]; then
find "${MYDIR}" -type f -mtime +10 -exec mv {} "${ARCHIVE}" \;
else
echo "Archive dir $ARCHIVE not found, skipping"
fi
rm /tmp/gomngr.lock
if [ "a$RUNONCE" != "a" ]; then
# For tests, run only once on demand
exit $ERRCODE
fi
if [ $NBFILES -eq 0 ]; then
# nothing to do, sleep
sleep 60
fi
done