-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart.sh
executable file
·82 lines (62 loc) · 1.59 KB
/
restart.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
#!/bin/bash
set -eu
CMD=$(basename "$0")
CMD=${CMD%.sh}
case "$CMD" in
restart|stop)
;;
*)
echo "Invalid command '$CMD', must be {restart|stop}" >&2
exit 1
;;
esac
HOST=$(hostname -s)
if [[ $HOST =~ ^d ]]; then
ENV=dev
else
ENV=prod
fi
# temp directory
RSTAR_TMPDIR="/content/$ENV/rstar/tmp"
# execuite command if this file exists
TRIGGER_FILE="$RSTAR_TMPDIR/tq-$CMD.txt"
# store date/time of command here
DATE_FILE="$RSTAR_TMPDIR/tq-$CMD-date-$HOST.txt"
# task queue config file
TQ_CONFIG_FILE="/content/$ENV/rstar/etc/task-queue.sysconfig"
# userid to email address mapping file
EMAIL_FILE="/content/$ENV/rstar/etc/email.yaml"
# name of queue on rabbitmq server
QUEUE_NAME="task_queue"
RUNUSER="runuser -u nobody --"
function get_admin_email
{
admin_email=$($RUNUSER python3 -c \
"import yaml;print(yaml.safe_load(open('$EMAIL_FILE'))['rstar'])")
}
function get_msg_count()
{
msg_count=$($RUNUSER curl -s -u guest:guest \
"http://${MQHOST}:15672/api/queues/%2f/${QUEUE_NAME}" | \
jq -r .messages)
}
unset MQHOST
. $TQ_CONFIG_FILE
get_admin_email
[ -n "$MQHOST" ] || exit
[ -n "$admin_email" ] || exit
declare -A cmd_verbs=(
["restart"]="Restarting"
["stop"]="Shutting down"
)
if [ -f $TRIGGER_FILE -a ! -f $DATE_FILE ]; then
get_msg_count
# make sure message queue is empty before shutting down
if [ "$msg_count" = "0" ]; then
date "+%Y-%m-%d %H:%M:%S" > $DATE_FILE
notice="${cmd_verbs[$CMD]} task queue on $(hostname)"
echo "$notice" | systemd-cat -t task-queue
echo "$notice" | mail -s "$notice" $admin_email
systemctl $CMD task-queue
fi
fi