Skip to content

Commit ba4533d

Browse files
committed
add new 'onhold' command
1 parent 0f22902 commit ba4533d

2 files changed

Lines changed: 69 additions & 28 deletions

File tree

mailctl

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TIMEOUT=3600 # a lot of time for a graceful container stop
88

99
# --------------------------------------------------------------------
1010

11-
VER=0.17.0
11+
VER=0.18.0
1212

1313
set -ueo pipefail
1414

@@ -35,8 +35,8 @@ _checkBin() {
3535
_checkBin "cat" "cut" "docker" "docker-compose" "fold" "jq" "printf" "sed" "tail" "tput" "tr"
3636

3737
# Check if container is running
38-
# Skip check, if first argument is empty, "status" or "start"
39-
if [ -n "${1:-}" ] && [ "${1:-}" != "status" ] && [ "${1:-}" != "start" ] && [ "${1:-}" != "restart" ]; then
38+
# Skip check, if first argument is empty, "status", "start", "stop" or "restart"
39+
if [ -n "${1:-}" ] && [ "${1:-}" != "status" ] && [ "${1:-}" != "start" ] && [ "${1:-}" != "stop" ] && [ "${1:-}" != "restart" ]; then
4040
if [ -z "$(docker ps -q --filter "name=^$CONTAINER$")" ]; then
4141
echo -e "Error: Container '$CONTAINER' is not up.\n" >&2
4242
exit 1
@@ -66,39 +66,59 @@ _status() {
6666
printf "%-${indent}s%s\n" "$1:" "$status"
6767
}
6868

69+
_ports() {
70+
docker port "$CONTAINER"
71+
}
72+
73+
_container() {
74+
if [ "$1" == "-it" ]; then
75+
shift
76+
docker exec -it "$CONTAINER" "$@"
77+
else
78+
docker exec "$CONTAINER" "$@"
79+
fi
80+
}
81+
6982
case "${1:-}" in
7083
status) # Show status
7184
if [ -n "$(docker ps -q --filter "name=^$CONTAINER$")" ]; then
7285
# Container uptime
7386
_status "Container" "$(docker ps --no-trunc --filter "name=^$CONTAINER$" --format "{{.Status}}")"
87+
echo
7488

7589
# Version
76-
_status "Version" "$(docker exec "$CONTAINER" cat /VERSION)"
90+
_status "Version" "$(_container cat /VERSION)"
91+
echo
7792

7893
# Fail2ban
79-
docker exec "$CONTAINER" ls /var/run/fail2ban/fail2ban.sock &>/dev/null &&
80-
_status "Fail2ban" "$(docker exec "$CONTAINER" fail2ban)"
94+
_container ls /var/run/fail2ban/fail2ban.sock &>/dev/null &&
95+
_status "Fail2ban" "$(_container fail2ban)"
96+
echo
8197

8298
# Package updates available?
83-
_status "Packages" "$(docker exec "$CONTAINER" bash -c 'apt -q update 2>/dev/null | grep "All packages are up to date" || echo "Updates available"')"
99+
_status "Packages" "$(_container bash -c 'apt -q update 2>/dev/null | grep "All packages are up to date" || echo "Updates available"')"
100+
echo
84101

85102
# Published ports
86-
_status "Ports" "$(docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f8 | tr "\n" " ")"
103+
# _status "Ports" "$(docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f8 | tr "\n" " ")"
104+
_status "Ports" "$(_ports)"
105+
echo
87106

88107
# Postfix mail queue
89-
POSTFIX=$(docker exec "$CONTAINER" postqueue -p | tail -1 | cut -d' ' -f5)
108+
POSTFIX=$(_container postqueue -p | tail -1 | cut -d' ' -f5)
90109
[ -z "$POSTFIX" ] && POSTFIX="Mail queue is empty" || POSTFIX+=" mail(s) queued"
91110
_status "Postfix" "$POSTFIX"
111+
echo
92112

93113
# Service status
94-
_status "Supervisor" "$(docker exec "$CONTAINER" supervisorctl status | sort -b -k2,2)"
114+
_status "Supervisor" "$(_container supervisorctl status | sort -b -k2,2)"
95115
else
96116
echo "Container: down"
97117
fi
98118
;;
99119

100120
config) # show configuration
101-
docker exec "$CONTAINER" cat /etc/dms-settings
121+
_container cat /etc/dms-settings
102122
;;
103123

104124
start) # Start container
@@ -134,48 +154,61 @@ case "${1:-}" in
134154
;;
135155

136156
queue) # Show mail queue
137-
docker exec "$CONTAINER" postqueue -p
157+
_container postqueue -p
138158
;;
139159

140160
flush) # Flush mail queue
141-
docker exec "$CONTAINER" postqueue -f
161+
_container postqueue -f
142162
echo "Queue flushed."
143163
;;
144164

165+
unhold) # Release mail that was put "on hold"
166+
if [ -z "${2:-}" ]; then
167+
echo "Error: Queue ID missing"
168+
else
169+
shift
170+
for i in "$@"; do
171+
ARG+=("-H" "$i")
172+
done
173+
_container postsuper "${ARG[@]}"
174+
fi
175+
;;
176+
145177
view) # Show mail by queue id
146178
if [ -z "${2:-}" ]; then
147179
echo "Error: Queue ID missing."
148180
else
149-
docker exec "$CONTAINER" postcat -q "$2"
181+
_container postcat -q "$2"
150182
fi >&2
151183
;;
152184

153-
delete) # Delete mail by queue id
185+
delete) # Delete mail from queue
154186
if [ -z "${2:-}" ]; then
155187
echo "Error: Queue ID missing."
156188
else
157189
shift
158190
for i in "$@"; do
159191
ARG+=("-d" "$i")
160192
done
161-
docker exec "$CONTAINER" postsuper "${ARG[@]}"
193+
_container postsuper "${ARG[@]}"
162194
fi
163195
;;
164196

165197
fail*) # Interact with fail2ban
166198
shift
167-
docker exec "$CONTAINER" fail2ban "$@"
199+
_container fail2ban "$@"
168200
;;
169201

170202
ports) # Show published ports
171203
echo "Published ports:"
172204
echo
173-
docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f4,8 | sed 's/"/:/g'
205+
# docker inspect "$CONTAINER" | jq -r '.[].NetworkSettings.Ports | .[] | select(. != null) | tostring' | cut -d'"' -f4,8 | sed 's/"/:/g'
206+
_ports
174207
;;
175208

176209
postc*) # Show postfix configuration
177210
shift
178-
docker exec "$CONTAINER" postconf "$@"
211+
_container postconf "$@"
179212
;;
180213

181214
logs) # Show logs
@@ -187,29 +220,29 @@ case "${1:-}" in
187220
;;
188221

189222
login) # Run container shell
190-
docker exec -it "$CONTAINER" bash
223+
_container -it bash
191224
;;
192225

193226
super*) # Interact with supervisorctl
194227
shift
195-
docker exec -it "$CONTAINER" supervisorctl "$@"
228+
_container -it supervisorctl "$@"
196229
;;
197230

198231
update-c*) # Check for container package updates
199-
docker exec -it "$CONTAINER" bash -c 'apt update && echo && apt list --upgradable'
232+
_container -it bash -c 'apt update && echo && apt list --upgradable'
200233
;;
201234

202235
update-p*) # Update container packages
203-
docker exec -it "$CONTAINER" bash -c 'apt update && echo && apt-get upgrade'
236+
_container -it bash -c 'apt update && echo && apt-get upgrade'
204237
;;
205238

206239
version*) # Show versions
207-
DMS_VER="$(docker exec "$CONTAINER" cat /VERSION)"
240+
DMS_VER="$(_container cat /VERSION)"
208241
printf "%-15s%s\n\n" "Mailserver:" "$DMS_VER"
209242
PACKAGES=("amavisd-new" "clamav" "dovecot-core" "fail2ban" "opendkim" "opendmarc" "postfix" "spamassassin" "supervisor")
210243
for i in "${PACKAGES[@]}"; do
211244
printf "%-15s" "$i:"
212-
docker exec "$CONTAINER" bash -c "dpkg -s $i | grep ^Version | cut -d' ' -f2"
245+
_container bash -c "dpkg -s $i | grep ^Version | cut -d' ' -f2"
213246
done
214247
;;
215248

@@ -229,8 +262,10 @@ case "${1:-}" in
229262
$APP queue Show mail queue
230263
$APP flush Flush mail queue
231264
$APP view <queue id> Show mail by queue id
232-
$APP delete <queue id> [<queue id>] Delete mail by queue id
233-
$APP delete ALL Delete all queued mails
265+
$APP unhold <queue id> [<queue id>] Release mail that was put "on hold" (marked with '!')
266+
$APP unhold ALL Release all mails that were put "on hold" (marked with '!')
267+
$APP delete <queue id> [<queue id>] Delete mail from queue
268+
$APP delete ALL Delete all mails from queue
234269
$APP fail2ban [<ban|unban> <IP>] Interact with fail2ban
235270
$APP ports Show published ports
236271
$APP postconf Show postfix configuration

mailctl-completion.bash

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ _mailctl() {
22
local cur prev options
33
cur=${COMP_WORDS[COMP_CWORD]}
44
prev=${COMP_WORDS[COMP_CWORD-1]}
5-
options="config delete fail2ban flush login logs ports postconf queue restart setup start status stop supervisor update-check update-packages versions view"
5+
options="config delete fail2ban flush login logs ports postconf queue restart setup start status stop supervisor unhold update-check update-packages versions view"
66
if [ $COMP_CWORD -eq 1 ]; then
77
COMPREPLY=( $(compgen -W "${options[@]}" -- $cur) )
88
elif [ $COMP_CWORD -eq 2 ]; then
99
case "$prev" in
10+
delete)
11+
COMPREPLY=( $(compgen -W "ALL" -- $cur) )
12+
;;
1013
fail2ban)
1114
COMPREPLY=( $(compgen -W "ban unban" -- $cur) )
1215
;;
1316
logs)
1417
COMPREPLY=( $(compgen -W "-f" -- $cur) )
1518
;;
19+
unhold)
20+
COMPREPLY=( $(compgen -W "ALL" -- $cur) )
21+
;;
1622
esac
1723
fi
1824
return 0

0 commit comments

Comments
 (0)