-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcallback.py
71 lines (59 loc) · 1.98 KB
/
callback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# coding: utf-8
# A restful HTTP API for ansible
# Base on ansible-runner and sanic
# Github <https://github.com/lfbear/ansible-api>
# Author: lfbear
import asyncio
from .realtimemsg import RealTimeMessage
from .report import Reporter
class CallBack(object):
"""
Callback functions for ansible-runner
"""
def __init__(self):
self._result = []
self._drawer = []
self._pepper = {}
def event_pepper(self, event, data):
self._pepper[event] = data
def event_handler(self, data):
# print(data, "---event---")
rpt = Reporter(data)
rpt.adorn(self._pepper)
fmt = rpt.tidy()
detail = rpt.detail()
if fmt:
asyncio.run(RealTimeMessage.send(fmt))
if detail:
self._result.append(detail)
def get_summary(self):
result = {}
for item in self._result:
k = item.get('host', 'unknown')
# print(k, item)
if result.get(k, None) is None:
result[k] = []
result[k].append(item)
return result
def get_event(self):
return self._result
def status_drawer(self, data):
self._drawer.append(data)
def status_handler(self, data, runner_config=None):
status = data.get('status', '') if isinstance(data, dict) else data
for item in self._drawer:
if item.get('status', None) == status:
raw = item.get('raw', lambda: {})()
after = item.get('after', lambda: {})()
# print('====>', raw, after)
rpt = Reporter(raw)
rpt.adorn({raw.get('event'): after})
fmt = rpt.tidy()
detail = rpt.detail()
if fmt:
asyncio.run(RealTimeMessage.send(fmt))
# await RealTimeMessage.send(fmt)
if detail:
self._result.append(detail)
# print(data, "---status---")