Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 3 | Salt engine for intercepting state jobs and forwarding to the Architect. |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 4 | """ |
| 5 | |
| 6 | # Import python libs |
| 7 | from __future__ import absolute_import |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 8 | import logging |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 9 | from architect_client.libarchitect import ArchitectClient |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 10 | |
| 11 | # Import salt libs |
| 12 | import salt.utils.event |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 13 | |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 14 | logger = logging.getLogger(__name__) |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def start(project='default', |
| 18 | host='127.0.0.1', |
| 19 | port=8181, |
| 20 | username=None, |
| 21 | password=None): |
| 22 | ''' |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 23 | Listen to state jobs events and forward state functions and node info |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 24 | ''' |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 25 | state_functions = ['state.sls', 'state.apply', 'state.highstate'] |
| 26 | model_functions = ['architect.node_info'] |
| 27 | class_tag = 'architect/minion/classify' |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 28 | |
| 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 Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 41 | logger.info('Architect Engine initialised') |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 42 | |
| 43 | while True: |
| 44 | event = event_bus.get_event() |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 45 | if event and event.get('fun', None) in state_functions: |
Ales Komarek | 459407b | 2018-01-18 17:16:31 +0100 | [diff] [blame] | 46 | is_test_run = 'test=true' in [arg.lower() for arg in event.get('fun_args', [])] |
| 47 | if not is_test_run: |
Ales Komarek | c312a29 | 2018-02-15 11:01:04 +0100 | [diff] [blame] | 48 | 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)) |