File prevent-pkg-plugins-errors-on-missing-cookie-path-bs.patch of Package salt.21870

From 54e3b21d52f14b0a75f35b78fb830dfdc287ec22 Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <35733135+vzhestkov@users.noreply.github.com>
Date: Mon, 8 Nov 2021 17:42:36 +0300
Subject: [PATCH] Prevent pkg plugins errors on missing cookie path
 (bsc#1186738) - 3002.2 (#415)

* Prevent pkg plugins errors on missing cookie path (bsc#1186738)

* Narrowing down exception handling

* Modify for Python 3 only

* Fix yumnotify
---
 scripts/suse/dpkg/dpkgnotify                  | 18 ++++++++++++++---
 scripts/suse/yum/plugins/README.md            |  2 +-
 scripts/suse/yum/plugins/yumnotify.py         | 17 ++++++++++++----
 scripts/suse/zypper/plugins/commit/zyppnotify | 20 ++++++++++++-------
 4 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/scripts/suse/dpkg/dpkgnotify b/scripts/suse/dpkg/dpkgnotify
index d3ad3d2ba9..3d6d038a98 100644
--- a/scripts/suse/dpkg/dpkgnotify
+++ b/scripts/suse/dpkg/dpkgnotify
@@ -2,10 +2,12 @@
 
 import os
 import hashlib
+import sys
 
 CK_PATH = "/var/cache/salt/minion/dpkg.cookie"
 DPKG_PATH = "/var/lib/dpkg/status"
 
+
 def _get_mtime():
     """
     Get the modified time of the Package Database.
@@ -35,9 +37,19 @@ def dpkg_post_invoke():
     """
     Hook after the package installation transaction.
     """
-    if 'SALT_RUNNING' not in os.environ:
-        with open(CK_PATH, 'w') as ck_fh:
-            ck_fh.write('{chksum} {mtime}\n'.format(chksum=_get_checksum(), mtime=_get_mtime()))
+    if "SALT_RUNNING" not in os.environ:
+        try:
+            ck_dir = os.path.dirname(CK_PATH)
+            if not os.path.exists(ck_dir):
+                os.makedirs(ck_dir)
+            with open(CK_PATH, "w") as ck_fh:
+                ck_fh.write(
+                    "{chksum} {mtime}\n".format(
+                        chksum=_get_checksum(), mtime=_get_mtime()
+                    )
+                )
+        except OSError as e:
+            print("Unable to save the cookie file: %s" % (e), file=sys.stderr)
 
 
 if __name__ == "__main__":
diff --git a/scripts/suse/yum/plugins/README.md b/scripts/suse/yum/plugins/README.md
index cb3abd2260..3515845b31 100644
--- a/scripts/suse/yum/plugins/README.md
+++ b/scripts/suse/yum/plugins/README.md
@@ -11,7 +11,7 @@ Configuration files are going to:
 
 Plugin itself goes to:
 
-	`/usr/share/yum-plugins/[name].conf`
+	`/usr/share/yum-plugins/[name].py`
 
 ## Permissions
 
diff --git a/scripts/suse/yum/plugins/yumnotify.py b/scripts/suse/yum/plugins/yumnotify.py
index f715bb6697..7f40ecdffd 100644
--- a/scripts/suse/yum/plugins/yumnotify.py
+++ b/scripts/suse/yum/plugins/yumnotify.py
@@ -5,6 +5,7 @@
 
 import hashlib
 import os
+import sys
 
 from yum import config
 from yum.plugins import TYPE_CORE
@@ -52,7 +53,15 @@ def posttrans_hook(conduit):
     """
     # Integrate Yum with Salt
     if "SALT_RUNNING" not in os.environ:
-        with open(CK_PATH, "w") as ck_fh:
-            ck_fh.write(
-                "{chksum} {mtime}\n".format(chksum=_get_checksum(), mtime=_get_mtime())
-            )
+        try:
+            ck_dir = os.path.dirname(CK_PATH)
+            if not os.path.exists(ck_dir):
+                os.makedirs(ck_dir)
+            with open(CK_PATH, "w") as ck_fh:
+                ck_fh.write(
+                    "{chksum} {mtime}\n".format(
+                        chksum=_get_checksum(), mtime=_get_mtime()
+                    )
+                )
+        except OSError as e:
+            print("Unable to save the cookie file: %s" % (e), file=sys.stderr)
diff --git a/scripts/suse/zypper/plugins/commit/zyppnotify b/scripts/suse/zypper/plugins/commit/zyppnotify
index d6a1bef42b..e3528e87a9 100755
--- a/scripts/suse/zypper/plugins/commit/zyppnotify
+++ b/scripts/suse/zypper/plugins/commit/zyppnotify
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 #
 # Copyright (c) 2016 SUSE Linux LLC
 # All Rights Reserved.
@@ -52,15 +52,21 @@ class DriftDetector(Plugin):
 
     def PLUGINEND(self, headers, body):
         """
-        Hook when plugin closes Zypper's transaction.        
+        Hook when plugin closes Zypper's transaction.
         """
         if "SALT_RUNNING" not in os.environ:
-            with open(self.ck_path, "w") as ck_fh:
-                ck_fh.write(
-                    "{chksum} {mtime}\n".format(
-                        chksum=self._get_checksum(), mtime=self._get_mtime()
+            try:
+                ck_dir = os.path.dirname(self.ck_path)
+                if not os.path.exists(ck_dir):
+                    os.makedirs(ck_dir)
+                with open(self.ck_path, "w") as ck_fh:
+                    ck_fh.write(
+                        "{chksum} {mtime}\n".format(
+                            chksum=self._get_checksum(), mtime=self._get_mtime()
+                        )
                     )
-                )
+            except OSError as e:
+                print("Unable to save the cookie file: %s" % (e), file=sys.stderr)
 
         self.ack()
 
-- 
2.33.1


openSUSE Build Service is sponsored by