zyppnotify 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 sys
  8. import os
  9. import hashlib
  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 os.path.exists(self.rpm_path) and int(os.path.getmtime(self.rpm_path)) or 0
  26. def _get_checksum(self):
  27. '''
  28. Get the checksum of the RPM Database.
  29. Returns:
  30. hexdigest
  31. '''
  32. digest = hashlib.sha256()
  33. with open(self.rpm_path, "rb") as rpm_db_fh:
  34. while True:
  35. buff = rpm_db_fh.read(0x1000)
  36. if not buff:
  37. break
  38. digest.update(buff)
  39. return digest.hexdigest()
  40. def PLUGINEND(self, headers, body):
  41. """
  42. Hook when plugin closes Zypper's transaction.
  43. """
  44. if 'SALT_RUNNING' not in os.environ:
  45. with open(self.ck_path, 'w') as ck_fh:
  46. ck_fh.write('{chksum} {mtime}\n'.format(chksum=self._get_checksum(), mtime=self._get_mtime()))
  47. self.ack()
  48. DriftDetector().main()