-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailctl
executable file
·297 lines (250 loc) · 7.8 KB
/
mailctl
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
#!/bin/bash
# SETUP --------------------------------------------------------------
DIR=/mail # compose.yaml directory
CONTAINER=mail # DMS container name
TIMEOUT=3600 # a lot of time for a graceful container stop
# DOCKER_COMPOSE="docker-compose" # lagacy docker-compose
DOCKER_COMPOSE="docker compose" # compose plugin
# --------------------------------------------------------------------
VER=0.21.0
set -ueo pipefail
if [ -z "$DIR" ] || [ -z "$CONTAINER" ] || [ -z "$TIMEOUT" ]; then
echo "Error: Not all setup variables are set."
echo
echo "You can configure them in '$0'"
echo
exit 1
fi >&2
_checkBin() {
local cmd
for cmd in "$@"; do
hash "$cmd" 2>/dev/null || {
echo "Error: '$cmd' not found."
echo
exit 1
} >&2
done
# docker compose
$DOCKER_COMPOSE version &>/dev/null || {
echo "Error: '$DOCKER_COMPOSE' not available."
echo
exit 1
} >&2
}
# Dependencies
_checkBin "cat" "cut" "docker" "fold" "jq" "printf" "sed" "tail" "tput" "tr"
# Check if container is running
# Skip check, if first argument is empty, "status", "start", "stop" or "restart"
if [ -n "${1:-}" ] && [ "${1:-}" != "status" ] && [ "${1:-}" != "start" ] && [ "${1:-}" != "stop" ] && [ "${1:-}" != "restart" ]; then
if [ -z "$(docker ps -q --filter "name=^$CONTAINER$")" ]; then
echo -e "Error: Container '$CONTAINER' is not up.\n" >&2
exit 1
fi
fi
cd "$DIR" &>/dev/null || {
echo "Error: Could not change directory to '$DIR'."
echo
echo "Check if 'DIR' is correctly defined in the script setup section."
echo
exit 1
} >&2
# Print status
_status() {
# $1 name
# $2 status
local indent spaces status
indent=14
# Wrap long lines and prepend spaces to multi line status
spaces=$(printf "%${indent}s")
status=$(echo -n "$2" | fold -s -w $(($(tput cols) - 16)) | sed "s/^/$spaces/g")
status=${status:$indent}
printf "%-${indent}s%s\n" "$1:" "$status"
}
_ports() {
docker port "$CONTAINER"
}
_container() {
if [ "$1" == "-it" ]; then
shift
docker exec -it "$CONTAINER" "$@"
else
docker exec "$CONTAINER" "$@"
fi
}
_getDMSVersion() {
# shellcheck disable=SC2016
# todo 'cat /VERSION' is kept for compatibility with DMS versions < v13.0.1; remove in the future
_container bash -c 'cat /VERSION 2>/dev/null || printf "%s" "$DMS_RELEASE"'
}
case "${1:-}" in
status) # Show status
if [ -n "$(docker ps -q --filter "name=^$CONTAINER$")" ]; then
# Container uptime
_status "Container" "$(docker ps --no-trunc --filter "name=^$CONTAINER$" --format "{{.Status}}")"
echo
# Version
_status "Version" "$(_getDMSVersion)"
echo
# Fail2ban
_container ls /var/run/fail2ban/fail2ban.sock &>/dev/null &&
_status "Fail2ban" "$(_container fail2ban)"
echo
# Package updates available?
_status "Packages" "$(_container bash -c 'apt -q update 2>/dev/null | grep "All packages are up to date" || echo "Updates available"')"
echo
# Published ports
# _status "Ports" "$(docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f8 | tr "\n" " ")"
_status "Ports" "$(_ports)"
echo
# Postfix mail queue
POSTFIX=$(_container postqueue -p | tail -1 | cut -d' ' -f5)
[ -z "$POSTFIX" ] && POSTFIX="Mail queue is empty" || POSTFIX+=" mail(s) queued"
_status "Postfix" "$POSTFIX"
echo
# Service status
_status "Supervisor" "$(_container supervisorctl status | sort -b -k2,2)"
else
echo "Container: down"
fi
;;
config) # show configuration
_container cat /etc/dms-settings
;;
start) # Start container
if [ -n "$(docker ps -q --filter "name=^$CONTAINER$")" ]; then
echo "Container '$CONTAINER' is already up."
echo
exit
fi
# If container is stopped, remove container / network etc.
$DOCKER_COMPOSE down -t "$TIMEOUT" 2>/dev/null || true
$DOCKER_COMPOSE up -d
;;
stop) # Stop container
$DOCKER_COMPOSE down -t "$TIMEOUT"
;;
resta*) # Restart container
$DOCKER_COMPOSE down -t "$TIMEOUT"
$DOCKER_COMPOSE up -d
;;
setup) # Invoke 'setup.sh'
if [ ! -x "setup.sh" ]; then
echo "Error: '$DIR/setup.sh' does not exist or is not executable."
echo
echo "To fix, run 'curl -o setup.sh https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master/setup.sh; chmod a+x ./setup.sh' in '$DIR'."
echo
exit 1
fi >&2
shift
./setup.sh "$@"
;;
queue) # Show mail queue
_container postqueue -p
;;
flush) # Flush mail queue
_container postqueue -f
echo "Queue flushed."
;;
unhold) # Release mail that was put "on hold"
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing"
else
shift
for i in "$@"; do
ARG+=("-H" "$i")
done
_container postsuper "${ARG[@]}"
fi
;;
view) # Show mail by queue id
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing."
else
_container postcat -q "$2"
fi >&2
;;
delete) # Delete mail from queue
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing."
else
shift
for i in "$@"; do
ARG+=("-d" "$i")
done
_container postsuper "${ARG[@]}"
fi
;;
fail*) # Interact with fail2ban
shift
_container fail2ban "$@"
;;
ports) # Show published ports
echo "Published ports:"
echo
# docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f4,8 | sed 's/"/:/g'
_ports
;;
postc*) # Show postfix configuration
shift
_container postconf "$@"
;;
logs) # Show logs
if [ "${2:-}" == "-f" ]; then
docker logs -f "$CONTAINER"
else
docker logs "$CONTAINER"
fi
;;
login) # Run container shell
_container -it bash
;;
super*) # Interact with supervisorctl
shift
_container -it supervisorctl "$@"
;;
update-c*) # Check for container package updates
_container -it bash -c 'apt update && echo && apt list --upgradable'
;;
update-p*) # Update container packages
_container -it bash -c 'apt update && echo && apt-get upgrade'
;;
version*) # Show versions
printf "%-15s%s\n\n" "Mailserver:" "$(_getDMSVersion)"
PACKAGES=("amavisd-new" "clamav" "dovecot-core" "fail2ban" "fetchmail" "getmail6" "rspamd" "opendkim" "opendmarc" "postfix" "spamassassin" "supervisor")
for i in "${PACKAGES[@]}"; do
printf "%-15s" "$i:"
_container bash -c "set -o pipefail; dpkg -s $i 2>/dev/null | grep ^Version | cut -d' ' -f2 || echo 'Package not installed.'"
done
;;
*)
APP=${0##*/}
cat <<-EOF
$APP $VER
Usage:
$APP status Show status
$APP config Show configuration
$APP start Start container
$APP stop Stop container
$APP restart Restart container
$APP setup Invoke 'setup.sh'
$APP queue Show mail queue
$APP flush Flush mail queue
$APP view <queue id> Show mail by queue id
$APP unhold <queue id> [<queue id>] Release mail that was put "on hold" (marked with '!')
$APP unhold ALL Release all mails that were put "on hold" (marked with '!')
$APP delete <queue id> [<queue id>] Delete mail from queue
$APP delete ALL Delete all mails from queue
$APP fail2ban [<ban|unban> <IP>] Interact with fail2ban
$APP fail2ban log Show fail2ban log
$APP ports Show published ports
$APP postconf Show postfix configuration
$APP logs [-f] Show logs. Use -f to 'follow' the logs
$APP login Run container shell
$APP supervisor Interact with supervisorctl
$APP update-check Check for container package updates
$APP update-packages Update container packages
$APP versions Show versions
EOF
;;
esac
echo