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