blob: a6d6bede35f96d2e34f67e5adc32aef9ab5c53a8 [file] [log] [blame]
Ales Komarek459407b2018-01-18 17:16:31 +01001# -*- coding: utf-8 -*-
2"""
Ales Komarekc312a292018-02-15 11:01:04 +01003Salt engine for intercepting state jobs and forwarding to the Architect.
Ales Komarek459407b2018-01-18 17:16:31 +01004"""
5
6# Import python libs
7from __future__ import absolute_import
Ales Komarek459407b2018-01-18 17:16:31 +01008import logging
Ales Komarekc312a292018-02-15 11:01:04 +01009from architect_client.libarchitect import ArchitectClient
Ales Komarek459407b2018-01-18 17:16:31 +010010
11# Import salt libs
12import salt.utils.event
Ales Komarek459407b2018-01-18 17:16:31 +010013
Ales Komarekc312a292018-02-15 11:01:04 +010014logger = logging.getLogger(__name__)
Ales Komarek459407b2018-01-18 17:16:31 +010015
16
17def start(project='default',
18 host='127.0.0.1',
19 port=8181,
20 username=None,
21 password=None):
22 '''
Ales Komarekc312a292018-02-15 11:01:04 +010023 Listen to state jobs events and forward state functions and node info
Ales Komarek459407b2018-01-18 17:16:31 +010024 '''
Ales Komarekc312a292018-02-15 11:01:04 +010025 state_functions = ['state.sls', 'state.apply', 'state.highstate']
26 model_functions = ['architect.node_info']
27 class_tag = 'architect/minion/classify'
Ales Komarek459407b2018-01-18 17:16:31 +010028
29 if __opts__['__role'] == 'master':
30 event_bus = salt.utils.event.get_master_event(__opts__,
31 __opts__['sock_dir'],
32 listen=True)
33 else:
34 event_bus = salt.utils.event.get_event(
35 'minion',
36 transport=__opts__['transport'],
37 opts=__opts__,
38 sock_dir=__opts__['sock_dir'],
39 listen=True)
40
Ales Komarekc312a292018-02-15 11:01:04 +010041 logger.info('Architect Engine initialised')
Ales Komarek459407b2018-01-18 17:16:31 +010042
43 while True:
44 event = event_bus.get_event()
Ales Komarekc312a292018-02-15 11:01:04 +010045 if event and event.get('fun', None) in state_functions:
Ales Komarek459407b2018-01-18 17:16:31 +010046 is_test_run = 'test=true' in [arg.lower() for arg in event.get('fun_args', [])]
47 if not is_test_run:
Ales Komarekc312a292018-02-15 11:01:04 +010048 output = ArchitectClient().push_event(event)
49 logger.info("Sent Architect state function {}".format(output))
50 if event and event.get('fun', None) in model_functions:
51 output = ArchitectClient().push_node_info({event['id']: event['return']})
52 logger.info("Sent Architect node info function {}".format(output))
53 if event and event.get('tag', None) == class_tag:
54 output = ArchitectClient().classify_node({
55 'name': event['id'],
56 'data': event['data']
57 })
58 logger.info("Sent Architect node classification {}".format(output))