297660
From d3889c4645a1319c3d677006164b618ee53f4c8b Mon Sep 17 00:00:00 2001
297660
From: Eduardo Otubo <otubo@redhat.com>
297660
Date: Mon, 7 Dec 2020 14:23:22 +0100
297660
Subject: [PATCH 3/4] Fix unit failure of cloud-final.service if NetworkManager
297660
 was not present.
297660
297660
RH-Author: Eduardo Terrell Ferrari Otubo (eterrell)
297660
RH-MergeRequest: 27: Fix unit failure of cloud-final.service if NetworkManager was not present.
297660
RH-Commit: [1/1] 3c65a2cca140fff48df1ef32919e3cb035506a2b (eterrell/cloud-init)
297660
RH-Bugzilla: 1898943
297660
297660
cloud-final.service would fail if NetworkManager was not installed.
297660
297660
journal -u cloud-final.service would show:
297660
297660
   cloud-init[5328]: Cloud-init v. 19.4 finished at ...
297660
   echo[5346]: try restart NetworkManager.service
297660
   systemctl[5349]: Failed to reload-or-try-restart
297660
       NetworkManager.service: Unit not found.
297660
   systemd[1]: cloud-final.service: control process exited,
297660
       code=exited status=5
297660
   systemd[1]: Failed to start Execute cloud user/final scripts.
297660
   systemd[1]: Unit cloud-final.service entered failed state.
297660
   systemd[1]: cloud-final.service failed.
297660
297660
The change here is to only attempt to restart NetworkManager if it is
297660
present, and its SubState is 'running'.
297660
297660
The multi-line shell in a systemd unit is less than ideal, but I'm not
297660
aware of any other way of conditionally doing this.
297660
297660
Note that both of 'try-reload-or-restart' and 'reload-or-try-restart'
297660
will fail if the service is not present.  So this would also affect rhel
297660
8 systems that do not use NetworkManager.
297660
297660
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
297660
---
297660
 rhel/systemd/cloud-final.service | 7 +++++--
297660
 1 file changed, 5 insertions(+), 2 deletions(-)
297660
297660
diff --git a/rhel/systemd/cloud-final.service b/rhel/systemd/cloud-final.service
297660
index 05add077..e281c0cf 100644
297660
--- a/rhel/systemd/cloud-final.service
297660
+++ b/rhel/systemd/cloud-final.service
297660
@@ -11,8 +11,11 @@ ExecStart=/usr/bin/cloud-init modules --mode=final
297660
 RemainAfterExit=yes
297660
 TimeoutSec=0
297660
 KillMode=process
297660
-ExecStartPost=/bin/echo "trying to reload or restart NetworkManager.service"
297660
-ExecStartPost=/usr/bin/systemctl try-reload-or-restart NetworkManager.service
297660
+# Restart NetworkManager if it is present and running.
297660
+ExecStartPost=/bin/sh -c 'u=NetworkManager.service; \
297660
+ out=$(systemctl show --property=SubState $u) || exit; \
297660
+ [ "$out" = "SubState=running" ] || exit 0; \
297660
+ systemctl reload-or-try-restart $u'
297660
 
297660
 # Output needs to appear in instance console output
297660
 StandardOutput=journal+console
297660
-- 
297660
2.18.4
297660