From c2a35c0c0aac093d0cc35181c1fda0162e22ac4c Mon Sep 17 00:00:00 2001 From: Victor Zhestkov <35733135+vzhestkov@users.noreply.github.com> Date: Mon, 8 Nov 2021 18:09:53 +0300 Subject: [PATCH] dnfnotify pkgset plugin implementation - 3002.2 (#450) * dnfnotify pkgset plugin implementation * Fix failing check * Add error reporting if not possible to save cookie * Try to create dir if not exists * Show the exception message instead of file name * Fix isort --- scripts/suse/dnf/plugins/README.md | 21 +++++++++ scripts/suse/dnf/plugins/dnfnotify.conf | 2 + scripts/suse/dnf/plugins/dnfnotify.py | 60 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 scripts/suse/dnf/plugins/README.md create mode 100644 scripts/suse/dnf/plugins/dnfnotify.conf create mode 100644 scripts/suse/dnf/plugins/dnfnotify.py diff --git a/scripts/suse/dnf/plugins/README.md b/scripts/suse/dnf/plugins/README.md new file mode 100644 index 0000000000..b19428608e --- /dev/null +++ b/scripts/suse/dnf/plugins/README.md @@ -0,0 +1,21 @@ +## What it is + +Plugin which provides a notification mechanism to Salt, if DNF is +used outside of it. + +## Installation + +Configuration files are going to: + + `/etc/dnf/plugins/[name].conf` + +Plugin itself goes to: + + `%{python_sitelib}/dnf-plugins/[name].py` + The path to dnf-plugins directory is Python version dependant. + +## Permissions + +User: root +Group: root +Mode: 644 diff --git a/scripts/suse/dnf/plugins/dnfnotify.conf b/scripts/suse/dnf/plugins/dnfnotify.conf new file mode 100644 index 0000000000..e7002aa3e9 --- /dev/null +++ b/scripts/suse/dnf/plugins/dnfnotify.conf @@ -0,0 +1,2 @@ +[main] +enabled = 1 diff --git a/scripts/suse/dnf/plugins/dnfnotify.py b/scripts/suse/dnf/plugins/dnfnotify.py new file mode 100644 index 0000000000..6e9df85f71 --- /dev/null +++ b/scripts/suse/dnf/plugins/dnfnotify.py @@ -0,0 +1,60 @@ +import hashlib +import os + +import dnf +from dnfpluginscore import _, logger + + +class DnfNotifyPlugin(dnf.Plugin): + def __init__(self, base, cli): + super().__init__(base, cli) + self.base = base + self.cookie_file = "/var/cache/salt/minion/rpmdb.cookie" + if os.path.exists("/var/lib/rpm/rpmdb.sqlite"): + self.rpmdb_file = "/var/lib/rpm/rpmdb.sqlite" + else: + self.rpmdb_file = "/var/lib/rpm/Packages" + + def transaction(self): + if "SALT_RUNNING" not in os.environ: + try: + ck_dir = os.path.dirname(self.cookie_file) + if not os.path.exists(ck_dir): + os.makedirs(ck_dir) + with open(self.cookie_file, "w") as ck_fh: + ck_fh.write( + "{chksum} {mtime}\n".format( + chksum=self._get_checksum(), mtime=self._get_mtime() + ) + ) + except OSError as e: + logger.error(_("Unable to save cookie file: %s"), e) + + def _get_mtime(self): + """ + Get the modified time of the RPM Database. + + Returns: + Unix ticks + """ + return ( + os.path.exists(self.rpmdb_file) + and int(os.path.getmtime(self.rpmdb_file)) + or 0 + ) + + def _get_checksum(self): + """ + Get the checksum of the RPM Database. + + Returns: + hexdigest + """ + digest = hashlib.sha256() + with open(self.rpmdb_file, "rb") as rpm_db_fh: + while True: + buff = rpm_db_fh.read(0x1000) + if not buff: + break + digest.update(buff) + return digest.hexdigest() -- 2.39.2