1
|
import os
|
2
|
from datetime import datetime
|
3
|
from collections import defaultdict
|
4
|
import json
|
5
|
import uuid
|
6
|
import requests
|
7
|
import threading
|
8
|
import time
|
9
|
|
10
|
try:
|
11
|
from ansible.plugins.callback import CallbackBase
|
12
|
parent_class = CallbackBase
|
13
|
except ImportError:
|
14
|
parent_class = object
|
15
|
|
16
|
FOREMAN_URL = os.getenv('FOREMAN_URL', "http://cm.research-infrastructures.eu:80")
|
17
|
# Substitute by a real SSL certificate and key if your Foreman uses HTTPS
|
18
|
FOREMAN_SSL_CERT = (os.getenv('FOREMAN_SSL_CERT', ""),
|
19
|
os.getenv('FOREMAN_SSL_KEY', ""))
|
20
|
FOREMAN_SSL_VERIFY = os.getenv('FOREMAN_SSL_VERIFY', "0")
|
21
|
FOREMAN_HEADERS = {
|
22
|
"Content-Type": "application/json",
|
23
|
"Accept": "application/json"
|
24
|
}
|
25
|
TIME_FORMAT="%Y-%m-%d %H:%M:%S %f"
|
26
|
FACTS_FORMAT="""
|
27
|
{
|
28
|
"name":"%(host)s",
|
29
|
"facts": %(data)s
|
30
|
}
|
31
|
"""
|
32
|
REPORT_FORMAT="""
|
33
|
{
|
34
|
"report":
|
35
|
{
|
36
|
"host":"%(host)s",
|
37
|
"reported_at":"%(now)s",
|
38
|
"metrics": %(metrics)s,
|
39
|
"status": %(status)s,
|
40
|
"logs" : %(log)s
|
41
|
}
|
42
|
}
|
43
|
"""
|
44
|
|
45
|
class WorkerRequest(threading.Thread):
|
46
|
def __init__(self, facts_json, ssl_verify):
|
47
|
threading.Thread.__init__(self)
|
48
|
self.facts_json = facts_json
|
49
|
self.ssl_verify = ssl_verify
|
50
|
|
51
|
def run(self):
|
52
|
requests.post(url=FOREMAN_URL + '/api/v2/hosts/facts',
|
53
|
data=self.facts_json,
|
54
|
headers=FOREMAN_HEADERS,
|
55
|
cert=FOREMAN_SSL_CERT,
|
56
|
verify=self.ssl_verify)
|
57
|
|
58
|
class CallbackModule(parent_class):
|
59
|
|
60
|
"""
|
61
|
Sends Ansible facts (if ansible -m setup ran) and reports
|
62
|
"""
|
63
|
def __init__(self):
|
64
|
super(CallbackModule, self).__init__()
|
65
|
self.items = defaultdict(list)
|
66
|
self.start_time = int(time.time())
|
67
|
self.ssl_verify = self._ssl_verify()
|
68
|
|
69
|
def log(self, host, category, data):
|
70
|
if type(data) != dict:
|
71
|
data = dict(msg=data)
|
72
|
data['category'] = category
|
73
|
if 'ansible_facts' in data:
|
74
|
self.send_facts(host, data)
|
75
|
self.send_report(host, data)
|
76
|
|
77
|
def _ssl_verify(self):
|
78
|
if FOREMAN_SSL_VERIFY.lower() in [ "1", "true", "on" ]:
|
79
|
verify = True
|
80
|
elif FOREMAN_SSL_VERIFY.lower() in [ "0", "false", "off" ]:
|
81
|
requests.packages.urllib3.disable_warnings()
|
82
|
#print ("plugin %s: SSL verification of %s disabled" % (os.path.basename(__file__), FOREMAN_URL))
|
83
|
verify = False
|
84
|
else: # Set ta a CA bundle:
|
85
|
verify = FOREMAN_SSL_VERIFY
|
86
|
return verify
|
87
|
|
88
|
def send_facts(self, host, data):
|
89
|
"""
|
90
|
Sends facts to Foreman, to be parsed by foreman_ansible fact
|
91
|
parser. The default fact importer should import these facts
|
92
|
properly.
|
93
|
"""
|
94
|
data["_type"] = "ansible"
|
95
|
data["_timestamp"] = datetime.now().strftime(TIME_FORMAT)
|
96
|
data = json.dumps(data)
|
97
|
facts_json = FACTS_FORMAT % dict(host=host, data=data)
|
98
|
|
99
|
r = WorkerRequest(facts_json, self.ssl_verify)
|
100
|
r.start()
|
101
|
|
102
|
def _build_log(self, data):
|
103
|
logs = []
|
104
|
for entry in data:
|
105
|
if isinstance(entry, tuple):
|
106
|
# v2 plugins have the task name
|
107
|
source, msg = entry
|
108
|
else:
|
109
|
if 'invocation' in entry:
|
110
|
source = json.dumps(entry['invocation'])
|
111
|
else:
|
112
|
source = json.dumps({"encrypted": "true"})
|
113
|
msg = entry
|
114
|
if 'failed' in msg:
|
115
|
level = 'err'
|
116
|
else:
|
117
|
level = 'notice' if 'changed' in msg and msg['changed'] else 'info'
|
118
|
logs.append({ "log": {
|
119
|
'sources' : { 'source' : source },
|
120
|
'messages' : { 'message': json.dumps(msg) },
|
121
|
'level': level
|
122
|
}})
|
123
|
return logs
|
124
|
|
125
|
|
126
|
def send_reports(self, stats):
|
127
|
"""
|
128
|
Send reports to Foreman, to be parsed by Foreman config report
|
129
|
importer. I massage the data get a report json that Foreman
|
130
|
can handle without writing another report importer.
|
131
|
|
132
|
Currently it just sets the status. It's missing:
|
133
|
- metrics, which we can get from data, except for runtime
|
134
|
"""
|
135
|
status = defaultdict(lambda:0)
|
136
|
metrics = {}
|
137
|
|
138
|
for host in stats.processed.keys():
|
139
|
sum = stats.summarize(host)
|
140
|
status["applied"] = sum['changed']
|
141
|
status["failed"] = sum['failures'] + sum['unreachable']
|
142
|
status["skipped"] = sum['skipped']
|
143
|
log = self._build_log(self.items[host])
|
144
|
metrics["time"] = { "total": int(time.time()) - self.start_time }
|
145
|
self.items[host] = []
|
146
|
|
147
|
report_json = REPORT_FORMAT % dict(host=host,
|
148
|
now=datetime.now().strftime(TIME_FORMAT),
|
149
|
metrics=json.dumps(metrics),
|
150
|
status=json.dumps(status),
|
151
|
log=json.dumps(log))
|
152
|
# To be changed to /api/v2/config_reports in 1.11.
|
153
|
# Maybe we could make a GET request to get the Foreman version & do this
|
154
|
# automatically.
|
155
|
requests.post(url=FOREMAN_URL + '/api/v2/reports',
|
156
|
data=report_json,
|
157
|
headers=FOREMAN_HEADERS,
|
158
|
cert=FOREMAN_SSL_CERT,
|
159
|
verify=self.ssl_verify)
|
160
|
|
161
|
def on_any(self, *args, **kwargs):
|
162
|
pass
|
163
|
|
164
|
def runner_on_failed(self, host, res, ignore_errors=False):
|
165
|
self.items[host].append(res)
|
166
|
|
167
|
def runner_on_ok(self, host, res):
|
168
|
if 'invocation' in res and res['invocation']['module_name'] == 'setup':
|
169
|
self.send_facts(host, res)
|
170
|
else:
|
171
|
self.items[host].append(res)
|
172
|
|
173
|
def runner_on_skipped(self, host, item=None):
|
174
|
pass
|
175
|
|
176
|
def runner_on_unreachable(self, host, res):
|
177
|
self.items[host].append(res)
|
178
|
|
179
|
def runner_on_no_hosts(self):
|
180
|
pass
|
181
|
|
182
|
def runner_on_async_poll(self, host, res, jid, clock):
|
183
|
pass
|
184
|
|
185
|
def runner_on_async_ok(self, host, res, jid):
|
186
|
self.items[host].append(res)
|
187
|
|
188
|
def runner_on_async_failed(self, host, res, jid):
|
189
|
self.items[host].append(res)
|
190
|
|
191
|
def playbook_on_start(self):
|
192
|
pass
|
193
|
|
194
|
def playbook_on_notify(self, host, handler):
|
195
|
pass
|
196
|
|
197
|
def playbook_on_no_hosts_matched(self):
|
198
|
pass
|
199
|
|
200
|
def playbook_on_no_hosts_remaining(self):
|
201
|
pass
|
202
|
|
203
|
def playbook_on_task_start(self, name, is_conditional):
|
204
|
pass
|
205
|
|
206
|
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
|
207
|
pass
|
208
|
|
209
|
def playbook_on_setup(self):
|
210
|
pass
|
211
|
|
212
|
def playbook_on_import_for_host(self, host, imported_file):
|
213
|
pass
|
214
|
|
215
|
def playbook_on_not_import_for_host(self, host, missing_file):
|
216
|
pass
|
217
|
|
218
|
def playbook_on_play_start(self, name):
|
219
|
pass
|
220
|
|
221
|
def playbook_on_stats(self, stats):
|
222
|
self.send_reports(stats)
|
223
|
|
224
|
# v2 callback API
|
225
|
def v2_runner_on_ok(self, result):
|
226
|
res = result._result
|
227
|
host = result._host.get_name()
|
228
|
try:
|
229
|
module = res['invocation']['module_name']
|
230
|
except KeyError:
|
231
|
module = None
|
232
|
if module == 'setup':
|
233
|
self.send_facts(host, res)
|
234
|
else:
|
235
|
name = result._task.get_name()
|
236
|
self.items[host].append((name, res))
|
237
|
|
238
|
def v2_runner_on_failed(self, result, ignore_errors=False):
|
239
|
name = result._task.get_name()
|
240
|
host = result._host.get_name()
|
241
|
self.items[host].append((name, result._result))
|