Skip to content

Commit 64c5e38

Browse files
authored
Merge pull request #322 from JedMeister/18.x-service-wrapper
updates, linting and styling
2 parents ce818ec + 7614190 commit 64c5e38

3 files changed

Lines changed: 73 additions & 29 deletions

File tree

conf/turnkey.d/postfix-local

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
#!/bin/sh -e
1+
#!/bin/bash -e
22

3-
if [ ! $HOSTNAME ]; then
4-
echo "'$(basename $0)' error: hostname not defined"
3+
if [[ ! "$HOSTNAME" ]]; then
4+
echo "'$(basename "$0")' error: hostname not defined"
55
return 1
66
fi
77

88
postconf -e inet_interfaces=localhost
9-
postconf -e myhostname=$HOSTNAME
9+
postconf -e myhostname="$HOSTNAME"
1010

1111
# harden postfix
12+
# - we don't want '$myhostname' to be expanded
13+
# shellcheck disable=SC2016
1214
postconf -e smtpd_banner='$myhostname ESMTP'
1315

1416
# force TLS connections only - might break sending of mail so leaving disabled

overlays/turnkey.d/systemd-chroot/usr/local/bin/service

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ case "$1" in
5252
/usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf
5353
;;
5454

55+
postfix)
56+
# /etc/init.d/postfix still exists but start|stop|reload leverage
57+
# systemd. The postfix.service is a oneshot that calls /bin/true, the
58+
# "real" service is a '-' instance of the 'postfix@.service' template
59+
# The template is "PartOf=postfix.service"
60+
case "$2" in
61+
start|stop)
62+
if [[ "$2" == "start" ]]; then
63+
# check config - as per postfix@.service 'ExecStartPre='
64+
/usr/lib/postfix/configure-instance.sh -
65+
fi
66+
# 'ExecStart'|'ExecStop' commands from postfix@.service
67+
/usr/sbin/postmulti -i - -p "$2"
68+
;;
69+
*)
70+
echo "unexpected postfix command '$2'" >&2
71+
exit 1
72+
;;
73+
esac
74+
;;
75+
5576
apache2)
5677
if [[ "$2" == "reload" ]]; then
5778
APACHE_STARTED_BY_SYSTEMD=y /usr/sbin/apache2ctl restart
@@ -77,7 +98,10 @@ case "$1" in
7798

7899
odoo)
79100
stop_start "$2" "$1" \
80-
runuser odoo -s /bin/bash -c "/usr/bin/odoo --config /etc/odoo/odoo.conf --logfile /var/log/odoo/odoo-server.log"
101+
runuser odoo -s /bin/bash -c \
102+
"/usr/bin/odoo" \
103+
" --config /etc/odoo/odoo.conf" \
104+
" --logfile /var/log/odoo/odoo-server.log"
81105
;;
82106

83107
gitlab-runsvdir*)
@@ -87,7 +111,8 @@ case "$1" in
87111

88112
ghost)
89113
stop_start "$2" "$1" \
90-
cd /opt/ghost; sudo -u ghost /usr/local/bin/node /usr/local/bin/ghost start
114+
cd /opt/ghost; \
115+
sudo -u ghost /usr/local/bin/node /usr/local/bin/ghost start
91116
;;
92117

93118
mattermost)
@@ -97,11 +122,15 @@ case "$1" in
97122

98123
php-fpm*)
99124
stop_start "$2" "$1" \
100-
runuser www-data -s /bin/bash -c "/usr/sbin/php-fpm?.? --daemonize --fpm-config /etc/php/?.?/fpm/php-fpm.conf"
125+
runuser www-data -s /bin/bash -c \
126+
"/usr/sbin/php-fpm?.?" \
127+
" --daemonize" \
128+
" --fpm-config /etc/php/?.?/fpm/php-fpm.conf"
101129
;;
102130

103131
*)
104-
echo "$(basename "$0") tkldev service wrapper falling back to /usr/sbin/service"
132+
echo "[$*] - unrecognized service = tkldev '$(basename "$0")'" \
133+
" wrapper falling back to /usr/sbin/service"
105134
/usr/sbin/service "$@"
106135
;;
107136
esac

overlays/turnkey.d/systemd-chroot/usr/local/bin/systemctl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,33 @@
44
#
55
# Only passes enable|disable as other actions will fail.
66

7-
fatal() { echo "FATAL [$0]: $@" 1>&2; exit 1; }
8-
warning() { echo "WARNING [$0]: $@"; }
7+
fatal() { echo "FATAL [$0]: $*" 1>&2; exit 1; }
8+
warning() { echo "WARNING [$0]: $*" 1>&2; }
9+
10+
exit_code=0
911

1012
_start_stop() {
1113
exit_code=0
1214

1315
# try service first; note this is also wrapped; see /usl/local/bin/service
14-
/usr/local/bin/service $2 $1 || exit_code=$?
16+
/usr/local/bin/service "$2" "$1" || exit_code=$?
1517

1618
if [[ "$exit_code" -eq 0 ]]; then
1719
return
1820
else
1921
# check if an init.d script exists
2022
initd=/etc/init.d/$2
2123
if [[ -f "$initd" ]]; then
22-
$initd $1 || fatal "$initd $1 returned a non-zero exit code."
24+
$initd "$1" || fatal "$initd $1 returned a non-zero exit code."
2325
else
24-
fatal "Unable to $1 service: $2 (tried 'service $2 $1', and $initd doesn't exist)."
26+
fatal "Unable to $1 service: $2 (tried 'service $2 $1', and" \
27+
" $initd doesn't exist)."
2528
fi
2629
fi
2730
}
2831

2932
_is_running() {
30-
pid=$(pgrep $1)
33+
pid=$(pgrep "$1")
3134
if [[ -n "$pid" ]]; then
3235
echo 'true'
3336
else
@@ -36,8 +39,10 @@ _is_running() {
3639
}
3740

3841
if [[ "$#" -lt 1 ]]; then
39-
# XXX hack for gitlab - respond to 'systemctl | grep "\-\.mount"'
40-
echo "-.mount loaded active mounted Root Mount"
42+
# XXX hack for gitlab
43+
# - respond _exactly_ as 'systemctl | grep "\-\.mount"' would
44+
echo "-.mount " \
45+
" loaded active mounted Root Mount"
4146
exit 0
4247
fi
4348

@@ -87,41 +92,49 @@ if [[ -n "$COMMAND" ]]; then
8792
warning "--quiet is only honored with commands is-active|is-failed."
8893
fi
8994

90-
if [[ "$COMMAND" != "daemon-reload" ]] && [[ "$COMMAND" != "list-units" ]] && [[ -z "$SERVICE_NAME" ]]; then
91-
fatal "Service name required with $COMMAND"
95+
if [[ "$COMMAND" != "daemon-reload" ]] \
96+
&& [[ "$COMMAND" != "list-units" ]] \
97+
&& [[ -z "$SERVICE_NAME" ]]; then
98+
fatal "Service name required with $COMMAND"
9299
fi
93100

94101
case $COMMAND in
95102
start|stop)
96-
_start_stop $COMMAND $SERVICE_NAME;;
103+
_start_stop "$COMMAND" "$SERVICE_NAME"
104+
;;
97105

98106
restart|reload)
99-
_start_stop stop $SERVICE_NAME
100-
_start_stop start $SERVICE_NAME;;
107+
_start_stop stop "$SERVICE_NAME"
108+
_start_stop start "$SERVICE_NAME"
109+
;;
101110

102111
enable|disable|mask|unmask)
103-
/usr/bin/systemctl $COMMAND $SERVICE_NAME || exit_code=$?
104-
exit $exit_code;;
112+
/usr/bin/systemctl "$COMMAND" "$SERVICE_NAME" || exit_code=$?
113+
exit "$exit_code"
114+
;;
105115

106116
daemon-reload)
107117
warning "Ignoring daemon-reload, init scripts not cached."
108-
exit 0;;
118+
exit 0
119+
;;
109120

110121
list-units)
111-
warning "list-units: Running in chroot, ignoring" >&2
112-
exit 0;;
122+
warning "list-units: Running in chroot, ignoring"
123+
exit 0
124+
;;
113125

114126
is-failed|is-active)
115-
running=$(_is_running $SERVICE_NAME)
116-
if [[ "$running" == 'true' ]]; then
127+
running=$(_is_running "$SERVICE_NAME")
128+
if [[ "$running" == "true" ]]; then
117129
[[ -n "$QUIET" ]] || echo "active/running"
118130
[[ "$COMMAND" == "is-active" ]] || exit 0
119131
exit 1
120132
else
121133
[[ -n "$QUIET" ]] || echo "inactive/failed"
122134
[[ "$COMMAND" == "is-failed" ]] || exit 1
123135
exit 0
124-
fi;;
136+
fi
137+
;;
125138
esac
126139
else
127140
fatal "Command ('$COMMAND') and/or Service name ('$SERVICE_NAME') not set or not found."

0 commit comments

Comments
 (0)