-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
87 lines (76 loc) · 2.09 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
#!/usr/bin/env sh
config_in_vcs() {
[ -n "$GIT_USER" ] && [ -n "$GIT_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
log_parameter=
if [ -n "$IMAPFILTER_LOGFILE" ]; then
log_parameter="-l $IMAPFILTER_LOGFILE"
fi
updated_config=no
pull_config() {
updated_config=no
if [ ! -d "$config_target_prefix" ]; then
printf ">>> Config has not been cloned yet, cloning\n"
mkdir -p "$config_target_prefix"
updated_config=yes
git clone "https://$GIT_USER:$(cat $GIT_TOKEN)@$GIT_TARGET" "$config_target_prefix"
else
cd "$config_target_prefix"
printf ">>> Pulling config\n"
git remote update
if [ "$(git rev-parse HEAD)" != "$(git rev-parse FETCH_HEAD)" ]; then
updated_config=yes
git pull
fi
cd -
fi
}
if config_in_vcs; then
pull_config
cd "$config_target_prefix"
elif [ -n "$IMAPFILTER_CONFIG_BASE" ]; then
cd "$IMAPFILTER_CONFIG_BASE"
else
cd "${config_target%/*}"
fi
if [ ! -f "$config_target" ]; then
printf "Config file '%s' does not exist\n" "$config_target"
exit 1
fi
imapfilter_pid=
imapfilter_update() {
if [ -n "$imapfilter_pid" ]; then
kill -TERM "$imapfilter_pid"
wait "$imapfilter_pid"
fi
imapfilter -c "$config_target" $log_parameter &
imapfilter_pid="$(jobs -p)"
}
if [ "$IMAPFILTER_DAEMON" = "yes" ]; then
imapfilter_update
fi
while true; do
if config_in_vcs; then
printf ">>> Updating config\n"
if ! pull_config; then
printf ">>> Pulling config failed\n"
fi
fi
if [ "$IMAPFILTER_DAEMON" = "yes" ]; then
if [ "$updated_config" = "yes" ]; then
printf ">>> Restarting imapfilter daemon\n"
imapfilter_update
fi
else
printf ">>> Running imapfilter\n"
imapfilter -c "$config_target" $log_parameter
fi
printf ">>> Sleeping\n"
sleep "${IMAPFILTER_SLEEP:-30}"
done