allow-systemd-units-no-unit-files.patch 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. From 90bece1faa1862465e97f7caf262c65cd84583ff Mon Sep 17 00:00:00 2001
  2. From: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
  3. Date: Fri, 11 Apr 2014 14:43:02 +0200
  4. Subject: [PATCH] Allow systemd units no provided by unit files to be handled.
  5. This allows querying status, start, stop, restart and list units that
  6. are not actually provided by unit files. Such units cannot be
  7. enabled/disabled and that's why those actions still prefer the
  8. "list-unit-files" output over "list-units".
  9. Units that couldn't be handled otherwise include for example mount
  10. units and sysvinit compatibility units such as those present on
  11. debian systems.
  12. The output of a "service.running ssh" state on a debian wheezy target
  13. is:
  14. ID: ssh
  15. Function: service.running
  16. Result: False
  17. Comment: The named service ssh is not available
  18. Changes:
  19. after this patch:
  20. ID: ssh
  21. Function: service.running
  22. Result: True
  23. Comment: The service ssh is already running
  24. Changes:
  25. ---
  26. salt/modules/systemd.py | 24 +++++++++++++++++++++++-
  27. 1 file changed, 23 insertions(+), 1 deletion(-)
  28. diff --git a/salt/modules/systemd.py b/salt/modules/systemd.py
  29. index 57b55f5..e2cfb1d 100644
  30. --- a/salt/modules/systemd.py
  31. +++ b/salt/modules/systemd.py
  32. @@ -72,6 +72,28 @@ def _systemctl_cmd(action, name):
  33. return 'systemctl {0} {1}'.format(action, _canonical_unit_name(name))
  34. +def _get_all_units():
  35. + '''
  36. + Get all units and their state. Units ending in .service
  37. + are normalized so that they can be referenced without a type suffix.
  38. + '''
  39. + rexp = re.compile(r'(?m)^(?P<name>.+)\.(?P<type>' +
  40. + '|'.join(VALID_UNIT_TYPES) +
  41. + r')\s+loaded\s+(?P<active>[^\s]+)')
  42. +
  43. + out = __salt__['cmd.run_stdout'](
  44. + 'systemctl --full list-units | col -b'
  45. + )
  46. +
  47. + ret = {}
  48. + for match in rexp.finditer(out):
  49. + name = match.group('name')
  50. + if match.group('type') != 'service':
  51. + name += '.' + match.group('type')
  52. + ret[name] = match.group('active')
  53. + return ret
  54. +
  55. +
  56. def _get_all_unit_files():
  57. '''
  58. Get all unit files and their state. Unit files ending in .service
  59. @@ -173,7 +195,7 @@ def get_all():
  60. salt '*' service.get_all
  61. '''
  62. - return sorted(_get_all_unit_files().keys())
  63. + return sorted(_get_all_units().keys())
  64. def available(name):
  65. --
  66. 1.9.3