-
Notifications
You must be signed in to change notification settings - Fork 0
/
croni.sh
executable file
·481 lines (393 loc) · 11 KB
/
croni.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/sh
# doc: https://git.boddenberg.it/croni
# author: André Boddenberg
# license: GPL 3.0
init_config="
# croni instance configuration file
# https://git.boddenberg.it/croni
croni_run=true
croni_update=false
croni_sendmail=false
"
init () {
# creating necessary directories & symlinks
mkdir -p "$runtime"
if [ ! -d "$base/logs" ]; then
ln -s $webroot/logs/ $base/logs
fi
log "initialising croni..."
if [ ! -f "croni.sh" ]; then
ln -s "$croni" "$base/croni.sh"
fi
# avoid vanishing last update information if already initialised
if [ ! -f "$runtime/last_update" ]; then
echo "n/a" > $runtime/last_update
fi
if [ ! -f "$runtime/croni_last_update" ]; then
echo "n/a" > $runtime/croni_last_update
fi
# creating config file in home directory
if [ ! -f "$HOME/.croni" ]; then
echo "$init_config" > "$HOME/.croni"
. "$HOME/.croni"
fi
update_croni_table
create_index_html
if [ ! -f $base/index.html ]; then
ln -s $webroot/index.html $base/index.html
fi
deploy
start_server
}
deploy () {
update_navbar
old_crontab="$base/.cronitab"
new_crontab="$base/.cronitab_new"
# create job-unrelated cronjobs to update and ensure HTTP server is listening
echo "@reboot $croni start_server" > $new_crontab
if [ $croni_update ]; then
echo "$croni_update_expression $croni update" >> $new_crontab
fi
if [ "$croni_server_check_expression" != "" ]; then
echo "$croni_server_check_expression $croni start_server" >> $new_crontab
fi
# create project and job pages
projects="$(ls "$base/jobs")"
for project in $projects; do
create_project_page "$project"
jobs="$(ls "$base/jobs/$project")"
for job_file in $jobs; do
job="$(echo $job_file | cut -d '.' -f1)"
create_job_page "$project" "$job"
deploy_job "$project" "$job" "$job_file" "$new_crontab"
done
update_project_table "$project"
done
# deploy new crontab if changes have been introduced
diff "$old_crontab" "$new_crontab"
if [ $? -gt 0 ]; then
cp "$new_crontab" "$old_crontab"
crontab "$old_crontab"
/etc/init.d/cron reload
log "deploy call: changes applied"
else
log "deploy call: nothing changed."
fi
rm "$new_crontab"
}
deploy_job () {
project="$1"
job="$2"
job_file="$3"
new_crontab="$4"
# ensure logs folder exists
mkdir -p "$base/logs/$project/$job"
if [ ! -f "$base/logs/$project/${job}.last_build" ]; then
echo "NOT RUN YET" > "$base/logs/$project/${job}.last_build"
fi
croni_expression="$(job_value "$project" "$job_file" "croni")"
if [ "$croni_expression" = "" ]; then
log "[ERROR] Cannot deploy, no cron_expression declared in $base/jobs/$project/$job_file"
else
echo "$croni_expression $croni run $project $job_file" >> $new_crontab
fi
}
# updating jobs repository
update () {
cd $base || exit
old_head="$(revision)"
branch="$(git branch | cut -d ' ' -f2)"
git fetch origin
git rebase "origin/$branch"
if [ $? -gt 0 ]; then
set -e
log "update call: rebase failed, stashing possible local changes before retrying"
git stash
git rebase "origin/$branch"
set +e
fi
new_head="$(revision)"
date="$(date +%H:%m:%S\ %d.%m.%y)"
echo "$date" > "$runtime/last_update"
if [ "$new_head" != "$old_head" ]; then
log "update call: changes found -> deploy()"
deploy
fi
update_croni_table
}
# updating croni submodule
upgrade () {
cd $base || exit
echo "$(date +%H:%m:%S\ %d.%m.%y)" > "$runtime/croni_last_update"
old_head="$(revision croni)"
git submodule update --remote
if [ $? -gt 0 ]; then
cd $submodule_base || exit
git stash
git reset HEAD --hard
cd $base || exit
git submodule update --remote
fi
new_head="$(revision croni)"
if [ "$new_head" != "$old_head" ]; then
log "Upgrade: updated changes, old: $old_head, new: $new_head"
deploy
else
log "Upgrade: no changes, currrent: $new_head"
fi
update_croni_table
}
# allow user to test jobs on disabled instance
test () {
croni_run=true
run $@
}
run () {
if [ "$croni_run" = "false" ]; then
echo "[ERROR] croni is currently disabled... try 'test' instead."
exit 0;
fi
project="$1"
job_file="$2"
job="$(echo $job_file | cut -d '.' -f1)"
job_dir="$base/logs/$project/$job"
if [ ! -f "$base/jobs/$project/$job_file" ]; then
echo "[ERROR] Job is not existing"
exit 1
fi
# obtain build number
if [ ! -f "$job_dir/latest_build_number" ]; then
echo "-1" > "$job_dir/latest_build_number"
fi
# increase build number
current_bn="$(cat $job_dir/latest_build_number)"
next_bn="$((current_bn+1))"
echo "$next_bn" > "$job_dir/latest_build_number"
# obtain log file
job_log="$job_dir/${job}_${next_bn}.log"
# create workspace
mkdir -p "$job_dir/workspaces/${next_bn}"
cd "$job_dir/workspaces/${next_bn}" || exit 1
# obtain timeout from job script
timeout="$(job_value "$project" "$job_file" "croni_timeout")"
script="$base/jobs/$project/$job_file"
# triggering job
date="$(date +%y-%m-%d_%H:%m:%S)"
echo "<pre>[INFO] Build $project/$job_file #${next_bn} triggered at $date" > "$job_log"
start=$(date +%s)
# exit code is 124 in case of a timeout
timeout "$timeout" "$script" >> "$job_log" 2>&1
exit_code=$?
stop=$(date +%s)
duration=$((stop-start))
echo "" >> "$job_log"
echo "[INFO] Build took: $duration s" >> "$job_log"
# exit code evaluation
result=""
if [ "$exit_code" -gt 0 ]; then
if [ "$exit_code" -eq 124 ]; then
result="TIMEOUT"
else
# failure let's try to parse the error from script
reason="unknown"
parsed_reason="$(job_value "$project" "$job_file" "croni_reason_${exit_code}")"
if [ "$parsed_reason" != "" ]; then
reason="$parsed_reason"
echo "[INFO] Noted failure reason: $reason" >> "$job_log"
result="KNOWN FAIL"
else
result="FAIL"
fi
fi
if [ $croni_send_mail ]; then
rcpt="$(job_value "$project" "$job_file" "$croni_mail_recipients")"
sendmail -t "$rcpt" -u "[croni] build $project/$job $result $parsed_reason" -a "$job_log"
fi
else
result="OK"
fi
echo "[INFO] Build result: $result </pre>" >> "$job_log"
result_txt="Build $project/$job #$next_bn took ${duration}s, result: $result $reason"
log "$result_txt"
# echo results to output log in case of invokation from command line.
echo "$result_txt"
# rotate logs and workspaces
job_cleanup "$project" "$job" "$next_bn"
# updating/cleanup front end
echo "$result" > "$base/logs/$project/${job}.last_build"
add_job_to_timelines "$project" "$job" "$result" "$next_bn" "$duration"
update_timelines "$project" "$job"
update_project_table "$project"
}
job_cleanup () {
project="$1"
job="$2"
# clean up old log(s)
number="$3"
number="$((number - croni_build_rotation))"
while [ -f "$base/logs/$project/$job/${job}_${number}.log" ]; do
rm "$base/logs/$project/$job/${job}_${number}.log"
number=$((number-1))
done
# clean up old workspace(s)
number="$3"
number="$((number - croni_workspace_rotation))"
while [ -d "$base/logs/$project/$job/workspaces/$number" ]; do
rm -rf "$base/logs/$project/$job/workspaces/$number"
number=$((number-1))
done
}
start_server () {
is_online="$(ps a | grep "python -m SimpleHTTPServer $croni_port" | wc -l)"
if [ "$is_online" = "1" ]; then
cd "$webroot"
python -m SimpleHTTPServer "$croni_port" >> "$base/logs/server.log" 2>&1 &
fi
}
### page creation ###
create_page () {
project="$1"
content="$2"
job="$3"
# obtain page name
page=""
if [ "$#" -gt 2 ]; then
page="${project}-${job}"
else
page="$project"
fi
dest="$webroot/${page}.html"
. "$templates"
# create page
echo "$page_start" > "$dest"
echo "$content" >> "$dest"
echo "$page_end" >> "$dest"
}
create_index_html () {
create_page "croni" "$landing_page"
mv "$webroot/croni.html" "$webroot/index.html"
}
create_project_page () {
project="$1"
. "$templates"
create_page "$project" "$project_page"
}
create_job_page () {
project="$1"
job="$2"
. "$templates"
create_page "$project" "$job_page" "$job"
}
update_navbar () {
rm "$base/logs/.runtime/navbar" > /dev/null 2>&1 || true
for p in $(ls "$base/jobs"); do
echo "<li><a class="croni_navbar" href=\"$p.html\">$p</a></li>" >> "$runtime/navbar"
done
}
update_croni_table () {
revision="$(revision)"
croni_revision="$(revision croni)"
cd "$base" || exit
remote_url="$(git config --get remote.origin.url)"
repo="<a href=\"$remote_url\"croni_>$remote_url</a>"
branch="$(git branch | head -1)"
last_update="$(cat $runtime/last_update)"
cd "$submodule_base" || exit
croni_remote_url="$(git config --get remote.origin.url)"
croni_repo="<a href=\"$croni_remote_url\">$croni_remote_url</a>"
croni_branch="$(git branch | head -1)"
croni_last_update="$(cat ${runtime}/croni_last_update)"
. "$templates"
echo "$croni_table_template" > "${runtime}/croni_table"
}
# called everytime a job finishes within this project
update_project_table () {
project="$1"
jobs="$(ls "$base/jobs/$project")"
rm "$runtime/${project}_project" > /dev/null 2>&1 || true
for job in $jobs; do
job="$(echo $job | cut -d '.' -f1)"
state="$(cat "$base/logs/$project/${job}.last_build")"
page="${project}-${job}"
name="$job"
. "$templates"
echo "$script_item_template" >> "$base/logs/.runtime/${project}_project"
done
. "$templates"
write_to_file "$runtime/${project}_project" "$project_table_header"
}
update_timeline () {
timeline_name="$1"
tl="$runtime/${timeline_name}_timeline"
cat "$tl" | head -"$croni_build_rotation" > "${tl}.tmp"
mv "${tl}.tmp" "$tl"
}
update_timelines () {
update_timeline "$1-$2"
update_timeline "$1"
update_timeline "croni"
}
add_job_to_timelines () {
project="$1"
job="$2"
result="$3"
build_number="$4"
duration="$5"
date="$(date +%H:%m:%S\ -\ %d.%m.%y)"
item="$project - $job"
item_path="${project}-${job}.html"
# following paths are relative to webroot
log_path="logs/$project/$job/${job}_${build_number}.log"
workspace_path="logs/$project/$job/workspaces/${build_number}/"
. "$templates"
write_to_file "$runtime/croni_timeline" "$timeline_item_template"
# only job name for project and job page
item="$job"
. "$templates"
write_to_file "$runtime/${project}-${job}_timeline" "$timeline_item_template"
write_to_file "$runtime/${project}_timeline" "$timeline_item_template"
}
# HELPERS
revision () {
cd "$base/$1" || exit
echo "$(git rev-parse --short HEAD)"
}
# parsing job value from job file or using default value of ~/.croni
job_value () {
job_var="$(cat "$base/jobs/$1/$2" | grep "$3\=" | cut -d "\"" -f2)"
if [ ! -n "$job_var" ]; then
default="default_$3"
default="echo \$$default"
default=$(eval $default)
if [ "$default" != "" ]; then
echo "$default"
fi
else
echo "$job_var"
fi
}
write_to_file () {
file="$1"
msg="$2"
if [ ! -f "$file" ]; then
echo "" > "$file"
fi
# append at the beginning
sed -i "1i${msg}" "$file"
}
log () {
write_to_file "$base/logs/croni.log" "\<pre\>[$(date +%H:%m:%S\ %d.%m.%y)] $1\</pre\>"
}
# actual entry point
submodule_base="$(dirname "$(readlink -f $0)")"
base="$(dirname $submodule_base)"
croni="$submodule_base/croni.sh"
webroot="$submodule_base/webroot"
templates="$webroot/templates.html"
runtime="$webroot/logs/.runtime/"
export base submodule_base webroot templates runtime
. "$base/croni.cfg"
if [ "$1" != "init" ]; then
. "$HOME/.croni" > /dev/null 2>&1 || true
fi
$@