-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailctl
More file actions
executable file
·327 lines (278 loc) · 7.95 KB
/
mailctl
File metadata and controls
executable file
·327 lines (278 loc) · 7.95 KB
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
#!/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.22.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
_container bash -c 'printf "%s" "$DMS_RELEASE"'
}
_getDebianVersion() {
_container bash -c 'cat /etc/debian_version'
}
case "${1:-}" in
# Show status
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
;;
# show configuration
config)
_container cat /etc/dms-settings
;;
# Start container
start)
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 container
stop)
$DOCKER_COMPOSE down -t "$TIMEOUT"
;;
# Restart container
resta*)
$DOCKER_COMPOSE down -t "$TIMEOUT"
$DOCKER_COMPOSE up -d
;;
# Invoke 'setup.sh'
setup)
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 "$@"
;;
# Show mail queue
queue)
_container postqueue -p
;;
# Flush mail queue
flush)
_container postqueue -f
echo "Queue flushed."
;;
# Release mail that was put "on hold"
unhold)
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing"
else
shift
for i in "$@"; do
ARG+=("-H" "$i")
done
_container postsuper "${ARG[@]}"
fi
;;
# Show mail by queue id
view)
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing."
else
_container postcat -q "$2"
fi >&2
;;
# Delete mail from queue
delete)
if [ -z "${2:-}" ]; then
echo "Error: Queue ID missing."
else
shift
for i in "$@"; do
ARG+=("-d" "$i")
done
_container postsuper "${ARG[@]}"
fi
;;
# Interact with fail2ban
fail*)
shift
_container fail2ban "$@"
;;
# Show published ports
ports)
echo "Published ports:"
echo
# docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f4,8 | sed 's/"/:/g'
_ports
;;
# Show postfix configuration
postc*)
shift
_container postconf "$@"
;;
# Show container processes
proce*)
_container -it ps auxf
;;
# Show logs
logs)
if [ "${2:-}" == "-f" ]; then
docker logs -f "$CONTAINER"
else
docker logs "$CONTAINER"
fi
;;
# Run container shell
login)
_container -it bash
;;
# Interact with supervisorctl
super*)
shift
_container -it supervisorctl "$@"
;;
# Check for container package updates
update-c*)
_container -it bash -c 'apt update && echo && apt list --upgradable'
;;
# Update container packages
update-p*)
_container -it bash -c 'apt update && echo && apt-get upgrade'
;;
# Show versions
version*)
printf "%-15s%s\n" "Mailserver:" "$(_getDMSVersion)"
printf "%-15s%s\n\n" "Debian:" "$(_getDebianVersion)"
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 processes Show container processes
$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