Skip to content

Commit

Permalink
Merge pull request #12918 from teacup-on-rockingchair/systemctl_remed…
Browse files Browse the repository at this point in the history
…iations_for_non_running_system

Add systemd check if it is running for systemctl start commands
  • Loading branch information
jan-cerny authored Jan 29, 2025
2 parents 9e4c6ee + 7a1927a commit 7d23e5b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ sed -i '/^\s*\(weekly\|monthly\|yearly\).*$/d' $LOGROTATE_CONF_FILE
{{% if 'sle' in product or product == 'slmicro5' %}}
# enable logrotate timer service
"$SYSTEMCTL_EXEC" unmask 'logrotate.timer'
"$SYSTEMCTL_EXEC" start 'logrotate.timer'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" start 'logrotate.timer'
fi
"$SYSTEMCTL_EXEC" enable 'logrotate.timer'
{{% else %}}
# configure cron.daily if not already
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ chmod 0644 /etc/systemd/system/aidecheck.*
# enable the aide related services
systemctl daemon-reload
systemctl enable aidecheck.service
systemctl --now enable aidecheck.timer
systemctl enable aidecheck.timer

if [[ $(systemctl is-system-running) != "offline" ]]; then
systemctl start aidecheck.timer
fi
4 changes: 4 additions & 0 deletions shared/macros/10-bash.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,15 @@ Macro to enable or disable a particular service.
#}}
{{%- if init_system == "systemd" -%}}
{{%- if service_state == "disable" -%}}
if [[ $(/usr/bin/systemctl is-system-running) != "offline" ]]; then
/usr/bin/systemctl stop "{{{ service }}}"
fi
/usr/bin/systemctl disable "{{{ service }}}"
{{%- else -%}}
/usr/bin/systemctl enable "{{{ service }}}"
if [[ $(/usr/bin/systemctl is-system-running) != "offline" ]]; then
/usr/bin/systemctl start "{{{ service }}}"
fi
{{%- endif %}}
# The service may not be running because it has been started and failed,
# so let's reset the state so OVAL checks pass.
Expand Down
8 changes: 6 additions & 2 deletions shared/templates/service_disabled/bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@


SYSTEMCTL_EXEC='/usr/bin/systemctl'
"$SYSTEMCTL_EXEC" stop '{{{ DAEMONNAME }}}.service'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" stop '{{{ DAEMONNAME }}}.service'
fi
"$SYSTEMCTL_EXEC" disable '{{{ DAEMONNAME }}}.service'
"$SYSTEMCTL_EXEC" mask '{{{ DAEMONNAME }}}.service'
# Disable socket activation if we have a unit file for it
if "$SYSTEMCTL_EXEC" -q list-unit-files {{{ DAEMONNAME }}}.socket; then
"$SYSTEMCTL_EXEC" stop '{{{ DAEMONNAME }}}.socket'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" stop '{{{ DAEMONNAME }}}.socket'
fi
"$SYSTEMCTL_EXEC" mask '{{{ DAEMONNAME }}}.socket'
fi
# The service may not be running because it has been started and failed,
Expand Down
4 changes: 3 additions & 1 deletion shared/templates/service_enabled/bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

SYSTEMCTL_EXEC='/usr/bin/systemctl'
"$SYSTEMCTL_EXEC" unmask '{{{ DAEMONNAME }}}.service'
"$SYSTEMCTL_EXEC" start '{{{ DAEMONNAME }}}.service'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" start '{{{ DAEMONNAME }}}.service'
fi
"$SYSTEMCTL_EXEC" enable '{{{ DAEMONNAME }}}.service'
{{% else %}}
JINJA TEMPLATE ERROR: Unknown init system '{{{ init_system }}}'
Expand Down
4 changes: 3 additions & 1 deletion shared/templates/socket_disabled/bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SOCKET_NAME="{{{ SOCKETNAME }}}.socket"
SYSTEMCTL_EXEC='/usr/bin/systemctl'

if "$SYSTEMCTL_EXEC" -q list-unit-files --type socket | grep -q "$SOCKET_NAME"; then
"$SYSTEMCTL_EXEC" stop "$SOCKET_NAME"
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" stop "$SOCKET_NAME"
fi
"$SYSTEMCTL_EXEC" mask "$SOCKET_NAME"
fi
4 changes: 3 additions & 1 deletion shared/templates/systemd_mount_enabled/bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

SYSTEMCTL_EXEC='/usr/bin/systemctl'
"$SYSTEMCTL_EXEC" unmask '{{{ MOUNTNAME }}}.mount'
"$SYSTEMCTL_EXEC" start '{{{ MOUNTNAME }}}.mount'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" start '{{{ MOUNTNAME }}}.mount'
fi
"$SYSTEMCTL_EXEC" enable '{{{ MOUNTNAME }}}.mount'
4 changes: 3 additions & 1 deletion shared/templates/timer_enabled/bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
# disruption = low

SYSTEMCTL_EXEC='/usr/bin/systemctl'
"$SYSTEMCTL_EXEC" start '{{{ TIMERNAME }}}.timer'
if [[ $("$SYSTEMCTL_EXEC" is-system-running) != "offline" ]]; then
"$SYSTEMCTL_EXEC" start '{{{ TIMERNAME }}}.timer'
fi
"$SYSTEMCTL_EXEC" enable '{{{ TIMERNAME }}}.timer'

0 comments on commit 7d23e5b

Please sign in to comment.