New version of salt-formula from Saltstack
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

63 行
2.1KB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Salt engine for intercepting state jobs and forwarding to the Architect
  4. service.
  5. """
  6. # Import python libs
  7. from __future__ import absolute_import
  8. import json
  9. import logging
  10. # Import salt libs
  11. import salt.utils.event
  12. import salt.utils.http
  13. log = logging.getLogger(__name__)
  14. def start(project='default',
  15. host='127.0.0.1',
  16. port=8181,
  17. username=None,
  18. password=None):
  19. '''
  20. Listen to state jobs events and forward Salt states
  21. '''
  22. url = "{}://{}:{}/salt/{}/event/{}".format('http',
  23. host,
  24. port,
  25. 'v1',
  26. project)
  27. target_functions = ['state.sls', 'state.apply', 'state.highstate']
  28. if __opts__['__role'] == 'master':
  29. event_bus = salt.utils.event.get_master_event(__opts__,
  30. __opts__['sock_dir'],
  31. listen=True)
  32. else:
  33. event_bus = salt.utils.event.get_event(
  34. 'minion',
  35. transport=__opts__['transport'],
  36. opts=__opts__,
  37. sock_dir=__opts__['sock_dir'],
  38. listen=True)
  39. log.info('Salt Architect engine initialised')
  40. while True:
  41. event = event_bus.get_event()
  42. if event and event.get('fun', None) in target_functions:
  43. is_test_run = 'test=true' in [arg.lower() for arg in event.get('fun_args', [])]
  44. if not is_test_run:
  45. data = salt.utils.http.query(url=url,
  46. method='POST',
  47. decode=False,
  48. data=json.dumps(event))
  49. if 'OK' in data.get('body', ''):
  50. log.info("Architect Engine request to '{}'"
  51. " was successful".format(url))
  52. else:
  53. log.warning("Problem with Architect Engine"
  54. " request to '{}' ({})".format(url, data))