zyppnotify 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2016 SUSE Linux LLC
  4. # All Rights Reserved.
  5. #
  6. # Author: Bo Maryniuk <bo@suse.de>
  7. import hashlib
  8. import os
  9. import sys
  10. from zypp_plugin import Plugin
  11. class DriftDetector(Plugin):
  12. """
  13. Return diff of the installed packages outside the Salt.
  14. """
  15. def __init__(self):
  16. Plugin.__init__(self)
  17. self.ck_path = "/var/cache/salt/minion/rpmdb.cookie"
  18. self.rpm_path = "/var/lib/rpm/Packages"
  19. def _get_mtime(self):
  20. """
  21. Get the modified time of the RPM Database.
  22. Returns:
  23. Unix ticks
  24. """
  25. return (
  26. os.path.exists(self.rpm_path) and int(os.path.getmtime(self.rpm_path)) or 0
  27. )
  28. def _get_checksum(self):
  29. """
  30. Get the checksum of the RPM Database.
  31. Returns:
  32. hexdigest
  33. """
  34. digest = hashlib.sha256()
  35. with open(self.rpm_path, "rb") as rpm_db_fh:
  36. while True:
  37. buff = rpm_db_fh.read(0x1000)
  38. if not buff:
  39. break
  40. digest.update(buff)
  41. return digest.hexdigest()
  42. def PLUGINEND(self, headers, body):
  43. """
  44. Hook when plugin closes Zypper's transaction.
  45. """
  46. if "SALT_RUNNING" not in os.environ:
  47. with open(self.ck_path, "w") as ck_fh:
  48. ck_fh.write(
  49. "{chksum} {mtime}\n".format(
  50. chksum=self._get_checksum(), mtime=self._get_mtime()
  51. )
  52. )
  53. self.ack()
  54. DriftDetector().main()