dnf-qubes-hooks.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # coding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. from __future__ import absolute_import
  22. from distutils.version import LooseVersion
  23. import logging
  24. import dnf
  25. import dnf.const
  26. import subprocess
  27. PLUGIN_CONF = 'qubes-hooks'
  28. def is_active(service):
  29. status = subprocess.call(["systemctl", "is-active", "--quiet", service])
  30. return status == 0
  31. class QubesHooks(dnf.Plugin):
  32. name = 'qubes-hooks'
  33. def __init__(self, base, cli):
  34. super(QubesHooks, self).__init__(base, cli)
  35. self.base = base
  36. self.log = logging.getLogger('dnf')
  37. def resolved(self):
  38. if not is_active("qubes-qrexec-agent"):
  39. return
  40. # in case of no action to do, transaction() hook won't be called;
  41. # report updates availability here - especially when everything is up
  42. # to date - to clear updates-available flag
  43. if not self.base.transaction:
  44. query = self.base.sack.query()
  45. query = query.upgrades()
  46. updates = set(query.run())
  47. subprocess.call([
  48. '/usr/lib/qubes/qrexec-client-vm',
  49. 'dom0',
  50. 'qubes.NotifyUpdates',
  51. '/bin/echo',
  52. str(len(updates))
  53. ])
  54. def transaction(self):
  55. if not is_active("qubes-qrexec-agent"):
  56. return
  57. if LooseVersion(dnf.const.VERSION) < '2.0.0':
  58. config = self.read_config(self.base.conf, PLUGIN_CONF)
  59. else:
  60. config = self.read_config(self.base.conf)
  61. if config.getboolean('main', 'notify-updates'):
  62. # Get all updates available _before_ this transaction
  63. query = self.base.sack.query()
  64. query = query.upgrades()
  65. updates = set(query.run())
  66. # Get packages installed in this transaction...
  67. just_installed = self.base.transaction
  68. # ...and filter them out of available updates
  69. for item in just_installed:
  70. updates.discard(item.pkg)
  71. subprocess.call([
  72. '/usr/lib/qubes/qrexec-client-vm',
  73. 'dom0',
  74. 'qubes.NotifyUpdates',
  75. '/bin/echo',
  76. str(len(updates))
  77. ])
  78. self.log.info("Notifying dom0 about installed applications")
  79. subprocess.call(['/etc/qubes-rpc/qubes.PostInstall'])