Hello,

Here is the patch for this issue that I uploaded to Ubuntu. It is slightly
adapted to take into account the possibility of being install on a non-systemd
installation.

I tried to make the changelog entry as explicit as possible but here are some
details.

As outlined previously, the systemd unit is changed to be an ExecStart instead
of an ExecStop. RequiresMountsFor= are added for /var/log, /var/run, /var/lib &
/boot. Disabling DefaultDependencies is removed so, as outlined previously,
local-fs.target might be superfluous.  RemainAfterExit is set to Yes so the unit
is seen as started. WantedBy is set to multi-user.target

This introduces a problem since Bug #797108[1] is causing the unit not to be
enabled upon upgrade. The following is done to work around this issue:

1) On systemd enabled system, postinst forces the unattended-upgrades service to
be disabled before deb-systemd-helper is executed so the previous
shutdown.target symlink does not remain.

2) At the end of the postinst script, after the deb-systemd-helper has been run,
manually enable and start the service. This will leave the service correctly
configured, as if the deb-systemd-helper had no bug.

3) systemctl enable requires the SysV init script to have a Default-Start
statement in the header otherwise it fails. Add the header in the script.

4) Remove the override_dh_installinit since it uses the 'stop' option which is
no longer available hence switching to 'default' which is the normal installinit
behavior. The postinst script also needs to cleanup the faulty stop symlink
created previously otherwise the systemclt enable fails.

5) Add DEP8 tests to verify that the unit is correctly started and that
InstallOnShutdown works as expected.

I have only tested the upgrade from 0.93.1 to 0.93.2 on Debian/Sid but I have
done extensive testing on Ubuntu which includes :

 * do-release-upgrade from Trusty(upstart) to Xenial(systemd)
 * upgrade on Xenial, Yakkety, Zesty & Artful

Please let me know if I can help with any more testing.

Kind regards,

...Louis

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=797108
--
Louis Bouchard
Software engineer,
Ubuntu Developer / Debian Maintainer
GPG : 429D 7A3B DD05 B6F8 AF63  B9C4 8B3D 867C 823E 7A61
@@ -1,3 +1,34 @@
+unattended-upgrades (0.93.2) unstable; urgency=medium
+
+  [ Louis Bouchard ]
+  * Fix the unattended-upgrades.service unit not correctly working:
+    - d/rules : Remove the override_dh_installinit. The stop option is no 
longer
+      available so the command falls back to default. This is the normal
+      behavior so the override is not required
+    - d/unattended-upgrades.init : Add Default-Start runlevels, otherwise the
+      unattended-upgrades.service unit cannot be enabled on boot
+    - d/postinst : Cleanup the stop symlinks created by the wrong
+      override_dh_installinit. Without that, the systemd unit cannot be
+      enabled correctly.
+      Force disable the service before deb-systemd-helper runs so the old
+      symlink is not left dangling (workaround for Debian Bug #797108).
+      Force enable and start of the systemd unit to work around Debian Bug 
#797108
+      which fails to enable systemd units correctly when WantedBy= statement
+      is changed which is the case here.
+    - d/unattended-upgrades.service : Fix the service so it runs correctly on
+      shutdown :
+        Remove DefaultDependencies=no : Breaks normal shutdown dependencies
+        Set After= to network.target and local-fs.target. Since our service is
+        now ExecStop, it will run before network and local-fs become 
unavailable.
+        Add RequiresMountsFor=/var/log /var/run /var/lib /boot : Necessary if
+        /var is a separate file system. Set WantedBy= to multi-user.target
+    - Add DEP8 tests to verify the following :
+      Verify that the unattended-upgrades.service unit is enabled and started.
+      Verify that InstallOnShutdown works when configured.
+    (Closes: #809669)
+
+ -- Louis Bouchard <lo...@ubuntu.com>  Mon, 24 Apr 2017 14:42:01 +0200
+
 unattended-upgrades (0.93.1) unstable; urgency=medium
 
   [ Brian Murray ]
diff -Nru unattended-upgrades-0.93.1/debian/postinst 
unattended-upgrades-0.93.2/debian/postinst
--- unattended-upgrades-0.93.1/debian/postinst  2016-12-11 11:31:26.000000000 
+0100
+++ unattended-upgrades-0.93.2/debian/postinst  2017-04-24 14:42:01.000000000 
+0200
@@ -61,6 +61,20 @@
             && [ -f /etc/rc6.d/S[0-9][0-9]unattended-upgrades ] ; then
             update-rc.d -f unattended-upgrades remove
         fi
+        # Recover from broken dh_installinit override in versions < 0.93.2
+        if dpkg --compare-versions "$2" lt "0.93.2"; then
+            if [ -f /etc/rc0.d/K[0-9][0-9]unattended-upgrades ] \
+            && [ -f /etc/rc6.d/K[0-9][0-9]unattended-upgrades ] ; then
+               update-rc.d -f unattended-upgrades remove
+           fi
+           # If running systemd, explicitely disable the service otherwise
+           # the shutdown.target symlink will remain (See Debian Bug #797108)
+           if [ -d /run/systemd/system ]; then
+               if deb-systemd-helper --quiet was-enabled 
unattended-upgrades.service; then
+                   deb-systemd-helper disable unattended-upgrades.service 
>/dev/null || true
+               fi
+           fi
+       fi
     ;;
 
     abort-upgrade|abort-remove|abort-deconfigure)
@@ -77,6 +91,21 @@
 
 #DEBHELPER#
 
+# Explicitely enable and start the service.i Debian Bug #797108 for 
+# deb-systemd-helper fails to correctly enable the unit. It checks for 
+# enablement using the content of the WantedBy= which has changed so it
+# sees the service as disable and will not enable it.
+case "$1" in
+    configure)
+        if dpkg --compare-versions "$2" lt "0.93.2" \
+       && [ -d /run/systemd/system ]; then
+                # workaround systemd bug with enable --now which
+                # fails to start the unit
+               systemctl enable unattended-upgrades || true
+               systemctl start unattended-upgrades || true
+       fi
+    ;;
+esac
 exit 0
 
 
diff -Nru unattended-upgrades-0.93.1/debian/rules 
unattended-upgrades-0.93.2/debian/rules
--- unattended-upgrades-0.93.1/debian/rules     2016-12-11 11:31:26.000000000 
+0100
+++ unattended-upgrades-0.93.2/debian/rules     2017-04-24 14:42:01.000000000 
+0200
@@ -28,8 +28,3 @@
                rm -f $$f.py; \
        done
        $(PYTHON) setup.py clean -a
-
-override_dh_installinit:
-       # we do not want to run the init script in the postinst/prerm, its
-       #  really only useful on shutdown, see Debian bug #645919
-       dh_installinit $@ --no-start -- stop 10 0 6 .
diff -Nru unattended-upgrades-0.93.1/debian/tests/control 
unattended-upgrades-0.93.2/debian/tests/control
--- unattended-upgrades-0.93.1/debian/tests/control     2016-12-11 
11:31:26.000000000 +0100
+++ unattended-upgrades-0.93.2/debian/tests/control     2017-04-24 
14:42:01.000000000 +0200
@@ -1,2 +1,3 @@
-Tests: run-tests
+Tests: run-tests test-systemd.py
 Depends: @, @builddeps@
+Restrictions: needs-root, isolation-container
diff -Nru unattended-upgrades-0.93.1/debian/tests/test-systemd.py 
unattended-upgrades-0.93.2/debian/tests/test-systemd.py
--- unattended-upgrades-0.93.1/debian/tests/test-systemd.py     1970-01-01 
01:00:00.000000000 +0100
+++ unattended-upgrades-0.93.2/debian/tests/test-systemd.py     2017-04-24 
14:42:01.000000000 +0200
@@ -0,0 +1,80 @@
+#!/usr/bin/python3
+
+import os
+import sys
+import subprocess
+
+
+def test_systemd_service():
+    '''
+    Verify that the unattended-upgrades.service unit is started
+    correctly. The unit must be started in order for the ExecStop=
+    to work correctly
+    '''
+    Service = 'unattended-upgrades.service'
+    try:
+        subprocess.check_output(['systemctl', '--quiet', 'is-active', Service])
+    except subprocess.CalledProcessError:
+        out = subprocess.getoutput(
+              'systemctl status unattended-upgrades.service')
+        print('test_systemd_service() FAILED\n%s' % out)
+        return False
+    return True
+
+
+def enable_install_on_shutdown():
+    '''
+    Enable InstallOnShutdown to verify that the command runs correctly
+    upon reboot
+    '''
+    apt_conf_file = '/etc/apt/apt.conf.d/50unattended-upgrades'
+    param = 'Unattended-Upgrade::InstallOnShutdown'
+    sed_cmd = 's/\/\/%s/%s/' % (param, param)
+
+    try:
+        subprocess.check_output(['/bin/sed', '-i', sed_cmd, apt_conf_file])
+    except subprocess.CalledProcessError:
+        print("Unable to edit %s" % apt_conf_file)
+        return False
+    return True
+
+
+def check_log_files():
+    '''
+    Verify that the logfiles are correctly produced by the InstallOnShutdown
+    run upon reboot. This will confirm that it did run correctly when we
+    rebooted.
+    '''
+    logdir = '/var/log/unattended-upgrades/'
+    logfiles = ['unattended-upgrades.log', 'unattended-upgrades-shutdown.log']
+
+    for file in logfiles:
+        if not os.path.exists(logdir + file):
+            print("File missing : %s" % (logdir + file))
+            return False
+    return True
+
+
+if __name__ == '__main__':
+    autopkgtest_reboot_mark = os.getenv('AUTOPKGTEST_REBOOT_MARK')
+
+    if autopkgtest_reboot_mark is None:
+        if not test_systemd_service():
+            sys.exit(1)
+
+        if enable_install_on_shutdown():
+            print('Rebooting to test InstallOnShutdown...')
+            subprocess.check_call(['/tmp/autopkgtest-reboot',
+                                   'InstallOnShutdown'])
+        else:
+            sys.exit(1)
+
+    if autopkgtest_reboot_mark == 'InstallOnShutdown':
+        if not check_log_files():
+            print("InstallOnShutdown did not run")
+            sys.exit(1)
+    else:
+        print('Invalid autopkgtest_reboot_mark value')
+        sys.exit(1)
+
+    sys.exit(0)
diff -Nru unattended-upgrades-0.93.1/debian/unattended-upgrades.init 
unattended-upgrades-0.93.2/debian/unattended-upgrades.init
--- unattended-upgrades-0.93.1/debian/unattended-upgrades.init  2016-12-11 
11:31:26.000000000 +0100
+++ unattended-upgrades-0.93.2/debian/unattended-upgrades.init  2017-04-24 
14:42:01.000000000 +0200
@@ -4,7 +4,7 @@
 # Required-Start:    $local_fs $remote_fs
 # Required-Stop:     $local_fs $remote_fs
 # Provides:          unattended-upgrade-shutdown-check
-# Default-Start:
+# Default-Start:     2 3 4 5
 # Default-Stop:      0 6
 # Short-Description: Check if unattended upgrades are being applied
 # Description:       Check if unattended upgrades are being applied
diff -Nru unattended-upgrades-0.93.1/debian/unattended-upgrades.service 
unattended-upgrades-0.93.2/debian/unattended-upgrades.service
--- unattended-upgrades-0.93.1/debian/unattended-upgrades.service       
2016-12-11 11:31:26.000000000 +0100
+++ unattended-upgrades-0.93.2/debian/unattended-upgrades.service       
2017-04-24 14:42:01.000000000 +0200
@@ -1,7 +1,7 @@
 [Unit]
 Description=Unattended Upgrades Shutdown
-DefaultDependencies=no
-Before=shutdown.target reboot.target halt.target network.target local-fs.target
+After=network.target local-fs.target
+RequiresMountsFor=/var/log /var/run /var/lib /boot
 Documentation=man:unattended-upgrade(8)
 
 [Service]
@@ -10,4 +10,4 @@
 TimeoutStartSec=900
 
 [Install]
-WantedBy=shutdown.target
+WantedBy=multi-user.target

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to