/salt/beacons/proxy_example.py

https://gitlab.com/ricardo.hernandez/salt
Python | 67 lines | 19 code | 11 blank | 37 comment | 1 complexity | e121aab4f524d2027ae9c73e695204cf MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Example beacon to use with salt-proxy
  4. .. code-block:: yaml
  5. beacons:
  6. proxy_example:
  7. endpoint: beacon
  8. '''
  9. # Import Python libs
  10. from __future__ import absolute_import
  11. import logging
  12. # Import salt libs
  13. import salt.utils.http
  14. # Important: If used with salt-proxy
  15. # this is required for the beacon to load!!!
  16. __proxyenabled__ = ['*']
  17. __virtualname__ = 'proxy_example'
  18. log = logging.getLogger(__name__)
  19. def __virtual__():
  20. '''
  21. Trivially let the beacon load for the test example.
  22. For a production beacon we should probably have some expression here.
  23. '''
  24. return True
  25. def validate(config):
  26. '''
  27. Validate the beacon configuration
  28. '''
  29. if not isinstance(config, dict):
  30. return False, ('Configuration for rest_example beacon must be a dictionary.')
  31. return True, 'Valid beacon configuration'
  32. def beacon(config):
  33. '''
  34. Called several times each second
  35. https://docs.saltstack.com/en/latest/topics/beacons/#the-beacon-function
  36. .. code-block:: yaml
  37. beacons:
  38. proxy_example:
  39. endpoint: beacon
  40. '''
  41. # Important!!!
  42. # Although this toy example makes an HTTP call
  43. # to get beacon information
  44. # please be advised that doing CPU or IO intensive
  45. # operations in this method will cause the beacon loop
  46. # to block.
  47. beacon_url = '{0}{1}'.format(__opts__['proxy']['url'],
  48. config['endpoint'])
  49. ret = salt.utils.http.query(beacon_url,
  50. decode_type='json',
  51. decode=True)
  52. return [ret['dict']]