1
0

yumnotify.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) 2016 SUSE Linux LLC
  2. # All Rights Reserved.
  3. #
  4. # Author: Bo Maryniuk <bo@suse.de>
  5. import hashlib
  6. import os
  7. from yum import config
  8. from yum.plugins import TYPE_CORE
  9. CK_PATH = "/var/cache/salt/minion/rpmdb.cookie"
  10. RPM_PATH = "/var/lib/rpm/Packages"
  11. requires_api_version = "2.5"
  12. plugin_type = TYPE_CORE
  13. def _get_mtime():
  14. """
  15. Get the modified time of the RPM Database.
  16. Returns:
  17. Unix ticks
  18. """
  19. return os.path.exists(RPM_PATH) and int(os.path.getmtime(RPM_PATH)) or 0
  20. def _get_checksum():
  21. """
  22. Get the checksum of the RPM Database.
  23. Returns:
  24. hexdigest
  25. """
  26. digest = hashlib.sha256()
  27. with open(RPM_PATH, "rb") as rpm_db_fh:
  28. while True:
  29. buff = rpm_db_fh.read(0x1000)
  30. if not buff:
  31. break
  32. digest.update(buff)
  33. return digest.hexdigest()
  34. def posttrans_hook(conduit):
  35. """
  36. Hook after the package installation transaction.
  37. :param conduit:
  38. :return:
  39. """
  40. # Integrate Yum with Salt
  41. if "SALT_RUNNING" not in os.environ:
  42. with open(CK_PATH, "w") as ck_fh:
  43. ck_fh.write(
  44. "{chksum} {mtime}\n".format(chksum=_get_checksum(), mtime=_get_mtime())
  45. )