blob: 4096a737b5bf973996acbc3a33dfbef95db7fb7b [file] [log] [blame]
Ales Komarek459407b2018-01-18 17:16:31 +01001# -*- coding: utf-8 -*-
2"""
3Salt engine for intercepting state jobs and forwarding to the Architect
4service.
5"""
6
7# Import python libs
8from __future__ import absolute_import
9import json
10import logging
11
12# Import salt libs
13import salt.utils.event
14import salt.utils.http
15
16log = logging.getLogger(__name__)
17
18
19def 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))