File prevent-tracebacks-if-directory-for-cookie-is-missin.patch of Package salt

From 9f4b779b79896f9e8c2df396caf807bd60114868 Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <35733135+vzhestkov@users.noreply.github.com>
Date: Mon, 8 Nov 2021 18:12:14 +0300
Subject: [PATCH] Prevent tracebacks if directory for cookie is missing
 (#451)

---
 scripts/suse/dpkg/dpkgnotify                  | 20 ++++++++---
 scripts/suse/yum/plugins/README.md            |  2 +-
 scripts/suse/yum/plugins/yumnotify.py         | 26 ++++++++++----
 scripts/suse/zypper/plugins/commit/zyppnotify | 35 +++++++++++++------
 4 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/scripts/suse/dpkg/dpkgnotify b/scripts/suse/dpkg/dpkgnotify
index d3ad3d2ba9..b4ee6df554 100644
--- a/scripts/suse/dpkg/dpkgnotify
+++ b/scripts/suse/dpkg/dpkgnotify
@@ -1,11 +1,13 @@
-#!/usr/bin/python3
+#!/usr/bin/python
 
 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 (IOError, OSError) as e:
+            sys.stderr.write("Unable to save the cookie file: %s\n" % (e))
 
 
 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 dd2485c886..eba37461da 100644
--- a/scripts/suse/yum/plugins/yumnotify.py
+++ b/scripts/suse/yum/plugins/yumnotify.py
@@ -3,15 +3,17 @@
 #
 # Author: Bo Maryniuk <bo@suse.de>
 
-from yum.plugins import TYPE_CORE
-from yum import config
-import os
 import hashlib
+import os
+import sys
+
+from yum import config
+from yum.plugins import TYPE_CORE
 
 CK_PATH = "/var/cache/salt/minion/rpmdb.cookie"
 RPM_PATH = "/var/lib/rpm/Packages"
 
-requires_api_version = '2.5'
+requires_api_version = "2.5"
 plugin_type = TYPE_CORE
 
 
@@ -50,6 +52,16 @@ def posttrans_hook(conduit):
     :return:
     """
     # 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()))
+    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 (IOError, OSError) as e:
+            sys.stderr.write("Unable to save the cookie file: %s\n" % (e))
diff --git a/scripts/suse/zypper/plugins/commit/zyppnotify b/scripts/suse/zypper/plugins/commit/zyppnotify
index 17869aa733..973dc5859d 100755
--- a/scripts/suse/zypper/plugins/commit/zyppnotify
+++ b/scripts/suse/zypper/plugins/commit/zyppnotify
@@ -5,9 +5,9 @@
 #
 # Author: Bo Maryniuk <bo@suse.de>
 
-import sys
-import os
 import hashlib
+import os
+import sys
 
 from zypp_plugin import Plugin
 
@@ -16,6 +16,7 @@ class DriftDetector(Plugin):
     """
     Return diff of the installed packages outside the Salt.
     """
+
     def __init__(self):
         Plugin.__init__(self)
         self.ck_path = "/var/cache/salt/minion/rpmdb.cookie"
@@ -24,19 +25,21 @@ class DriftDetector(Plugin):
             self.rpm_path = "/var/lib/rpm/Packages"
 
     def _get_mtime(self):
-        '''
+        """
         Get the modified time of the RPM Database.
         Returns:
             Unix ticks
-        '''
-        return os.path.exists(self.rpm_path) and int(os.path.getmtime(self.rpm_path)) or 0
+        """
+        return (
+            os.path.exists(self.rpm_path) and int(os.path.getmtime(self.rpm_path)) or 0
+        )
 
     def _get_checksum(self):
-        '''
+        """
         Get the checksum of the RPM Database.
         Returns:
             hexdigest
-        '''
+        """
         digest = hashlib.sha256()
         with open(self.rpm_path, "rb") as rpm_db_fh:
             while True:
@@ -49,11 +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()))
+        if "SALT_RUNNING" not in os.environ:
+            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 (IOError, OSError) as e:
+                sys.stderr.write("Unable to save the cookie file: %s\n" % (e))
 
         self.ack()
 
-- 
2.33.1


openSUSE Build Service is sponsored by