-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentrypoint.sh
136 lines (116 loc) · 3 KB
/
entrypoint.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
#!/usr/bin/env sh
vcs_token() {
if [ -n "$GIT_TOKEN_RAW" ]; then
echo "$GIT_TOKEN_RAW"
return
fi
cat "${GIT_TOKEN}"
}
vcs_uri() {
s="https://"
if [ -n "$GIT_USER" ]; then
# https://user:
s="${s}${GIT_USER}:"
fi
# https://user:token@"
token="$(vcs_token)"
if [ -n "$token" ]; then
s="${s}${token}@"
fi
# https://user:token@target
echo "${s}${GIT_TARGET}"
}
config_in_vcs() {
[ -n "$(vcs_token)" ] && [ -n "$GIT_TARGET" ]
}
config_target_prefix="/opt/imapfilter/config"
if config_in_vcs; then
config_target="$config_target_prefix/$IMAPFILTER_CONFIG"
else
config_target="$IMAPFILTER_CONFIG"
fi
pull_config() {
config_in_vcs || return
printf ">>> Updating config\n"
if [ ! -d "$config_target_prefix" ]; then
printf ">>> Config has not been cloned yet, cloning\n"
mkdir -p "$config_target_prefix"
git clone "$(vcs_uri)" "$config_target_prefix"
return
else
cd "$config_target_prefix"
printf ">>> Pulling config\n"
git remote update
if [ "$(git rev-parse HEAD)" != "$(git rev-parse FETCH_HEAD)" ]; then
git pull
return
fi
cd -
fi
return 1
}
start_imapfilter() {
# enter a subshell to not affect the pwd of the running process
(
# Enter the basedir of the config. Required to allow relative
# includes in the lua scripts.
if config_in_vcs; then
cd "$config_target_prefix"
elif [ -n "$IMAPFILTER_CONFIG_BASE" ]; then
cd "$IMAPFILTER_CONFIG_BASE"
else
cd "${config_target%/*}"
fi
log_parameter=
if [ -n "$IMAPFILTER_LOGFILE" ]; then
log_parameter="-l $IMAPFILTER_LOGFILE"
fi
imapfilter -c "$config_target" $log_parameter
)
}
imapfilter_pid=
imapfilter_restart_daemon() {
if [ -n "$imapfilter_pid" ]; then
kill -TERM "$imapfilter_pid"
wait "$imapfilter_pid"
fi
start_imapfilter &
imapfilter_pid="$(jobs -p)"
}
loop_no_daemon() {
while true; do
pull_config
printf ">>> Running imapfilter\n"
if ! start_imapfilter; then
printf ">>> imapfilter failed\n"
exit 1
fi
printf ">>> Sleeping\n"
sleep "${IMAPFILTER_SLEEP:-30}"
done
}
loop_daemon() {
imapfilter_restart_daemon
while true; do
if pull_config; then
printf ">>> Update in VCS, restarting imapfilter daemon\n"
imapfilter_restart_daemon
fi
printf ">>> Sleeping\n"
sleep "${IMAPFILTER_SLEEP:-30}"
if ! kill -0 "$imapfilter_pid" 2>/dev/null; then
printf ">>> imapfilter daemon died, exiting\n"
exit 1
fi
done
}
pull_config
if [ ! -f "$config_target" ]; then
printf "Config file '%s' does not exist\n" "$config_target"
exit 1
fi
if [ "$IMAPFILTER_DAEMON" = "yes" ]; then
loop_daemon
else
loop_no_daemon
fi