-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathslack_webhook_return.py
More file actions
390 lines (297 loc) · 10.9 KB
/
Copy pathslack_webhook_return.py
File metadata and controls
390 lines (297 loc) · 10.9 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# -*- coding: utf-8 -*-
"""
Return salt data via Slack using Incoming Webhooks
:codeauthor: `Carlos D. Álvaro <github@cdalvaro.io>`
The following fields can be set in the minion conf file:
.. code-block:: yaml
slack_webhook.webhook (required, the webhook id. Just the part after: 'https://hooks.slack.com/services/')
slack_webhook.success_title (optional, short title for succeeded states. By default: '{id} | Succeeded')
slack_webhook.failure_title (optional, short title for failed states. By default: '{id} | Failed')
slack_webhook.author_icon (optional, a URL that with a small 16x16px image. Must be of type: GIF, JPEG, PNG, and BMP)
slack_webhook.show_tasks (optional, show identifiers for changed and failed tasks. By default: False)
Alternative configuration values can be used by prefacing the configuration.
Any values not found in the alternative configuration will be pulled from
the default location:
.. code-block:: yaml
slack_webhook.webhook
slack_webhook.success_title
slack_webhook.failure_title
slack_webhook.author_icon
slack_webhook.show_tasks
Slack settings may also be configured as:
.. code-block:: yaml
slack_webhook:
webhook: T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
success_title: '[{id}] | Success'
failure_title: '[{id}] | Failure'
author_icon: https://platform.slack-edge.com/img/default_application_icon.png
show_tasks: true
alternative.slack_webhook:
webhook: T00000000/C00000000/YYYYYYYYYYYYYYYYYYYYYYYY
show_tasks: false
To use the Slack returner,
append '--return slack_webhook' to the salt command.
.. code-block:: bash
salt '*' test.ping --return slack_webhook
To use the alternative configuration,
append '--return_config alternative' to the salt command.
.. code-block:: bash
salt '*' test.ping --return slack_webhook --return_config alternative
"""
from __future__ import absolute_import, print_function, unicode_literals
import json
# Import Python libs
import logging
# pylint: disable=import-error,no-name-in-module,redefined-builtin
import salt.ext.six.moves.http_client
# Import Salt Libs
import salt.returners
import salt.utils.http
import salt.utils.yaml
from salt.ext import six
from salt.ext.six.moves import map, range
from salt.ext.six.moves.urllib.parse import urlencode as _urlencode
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
# pylint: enable=import-error,no-name-in-module,redefined-builtin
log = logging.getLogger(__name__)
__virtualname__ = "slack_webhook"
UNCHANGED_KEY = "unchanged"
CHANGED_KEY = "changed"
FAILED_KEY = "failed"
TASKS_KEY = "tasks"
COUNTER_KEY = "counter"
DURATION_KEY = "duration"
TOTAL_KEY = "total"
def _get_options(ret=None):
"""
Get the slack_webhook options from salt.
:param ret: Salt return dictionary
:return: A dictionary with options
"""
defaults = {
"success_title": "{id} | Succeeded",
"failure_title": "{id} | Failed",
"author_icon": "",
"show_tasks": False,
}
attrs = {
"webhook": "webhook",
"success_title": "success_title",
"failure_title": "failure_title",
"author_icon": "author_icon",
"show_tasks": "show_tasks",
}
_options = salt.returners.get_returner_options(
__virtualname__,
ret,
attrs,
__salt__=__salt__,
__opts__=__opts__,
defaults=defaults,
)
return _options
def __virtual__():
"""
Return virtual name of the module.
:return: The virtual name of the module.
"""
return __virtualname__
def _sprinkle(config_str):
"""
Sprinkle with grains of salt, that is
convert "test {id} test {host} " types of strings
:param config_str: The string to be sprinkled
:return: The string sprinkled
"""
parts = [x for sub in config_str.split("{") for x in sub.split("}")]
for i in range(1, len(parts), 2):
parts[i] = six.text_type(__grains__.get(parts[i], ""))
return "".join(parts)
def _format_task(task):
"""
Return a dictionary with the task ready for slack fileds
:param task: The name of the task
:return: A dictionary ready to be inserted in Slack fields array
"""
return {"value": task, "short": False}
def _generate_payload(author_icon, title, report):
"""
Prepare the payload for Slack
:param author_icon: The url for the thumbnail to be displayed
:param title: The title of the message
:param report: A dictionary with the report of the Salt function
:return: The payload ready for Slack
"""
title = _sprinkle(title)
text = "Function: {}\n".format(report.get("function"))
if len(report.get("arguments", [])) > 0:
text += "Function Args: {}\n".format(str(list(map(str, report["arguments"]))))
text += "JID: {}\n".format(report.get("jid"))
if TOTAL_KEY in report:
text += "Total: {}\n".format(report[TOTAL_KEY])
if DURATION_KEY in report:
text += "Duration: {:.2f} secs".format(float(report[DURATION_KEY]))
attachments = [
{
"fallback": title,
"color": "#272727",
"author_name": _sprinkle("{id}"),
"author_link": _sprinkle("{localhost}"),
"author_icon": author_icon,
"title": "Success: {}".format(str(report["success"])),
"text": text,
}
]
if UNCHANGED_KEY in report:
# Unchanged
attachments.append(
{
"color": "good",
"title": "Unchanged: {}".format(
report[UNCHANGED_KEY].get(COUNTER_KEY, 0)
),
}
)
# Changed
changed = {
"color": "warning",
"title": "Changed: {}".format(report[CHANGED_KEY].get(COUNTER_KEY, 0)),
}
if len(report[CHANGED_KEY].get(TASKS_KEY, [])) > 0:
changed["fields"] = list(map(_format_task, report[CHANGED_KEY][TASKS_KEY]))
attachments.append(changed)
# Failed
failed = {
"color": "danger",
"title": "Failed: {}".format(report[FAILED_KEY].get(COUNTER_KEY, None)),
}
if len(report[FAILED_KEY].get(TASKS_KEY, [])) > 0:
failed["fields"] = list(map(_format_task, report[FAILED_KEY][TASKS_KEY]))
attachments.append(failed)
else:
attachments.append(
{
"color": "good" if report["success"] else "danger",
"title": "Return: {}".format(report.get("return", None)),
}
)
payload = {"attachments": attachments}
return payload
def _process_state(returns):
"""
Process the received output state
:param returns A dictionary with the returns of the recipe
:return A dictionary with Unchanges, Changed and Failed tasks
"""
sorted_data = sorted(returns.items(), key=lambda s: s[1].get("__run_num__", 0))
n_total = 0
n_failed = 0
n_changed = 0
duration = 0.0
changed_tasks = []
failed_tasks = []
# gather stats
for state, data in sorted_data:
# state: module, stateid, name, function
_, stateid, _, _ = state.split("_|-")
task = "{filename}.sls | {taskname}".format(
filename=str(data.get("__sls__")), taskname=stateid
)
if not data.get("result", True):
n_failed += 1
failed_tasks.append(task)
if data.get("changes", {}):
n_changed += 1
changed_tasks.append(task)
n_total += 1
try:
duration += float(data.get("duration", 0.0))
except ValueError:
pass
n_unchanged = n_total - n_failed - n_changed
return {
TOTAL_KEY: n_total,
UNCHANGED_KEY: {COUNTER_KEY: n_unchanged},
CHANGED_KEY: {COUNTER_KEY: n_changed, TASKS_KEY: changed_tasks},
FAILED_KEY: {COUNTER_KEY: n_failed, TASKS_KEY: failed_tasks},
DURATION_KEY: duration / 1000,
}
def _state_return(ret):
"""
Return True if ret is a Salt state return
:param ret: The Salt return
"""
ret_data = ret.get("return")
if not isinstance(ret_data, dict):
return False
return ret_data and "__id__" in next(iter(ret_data.values()))
def _generate_report(ret, show_tasks):
"""
Generate a report of the Salt function
:param ret: The Salt return
:param show_tasks: Flag to show the name of the changed and failed states
:return: The report
"""
report = {
"id": ret.get("id"),
"success": True if ret.get("retcode", 1) == 0 else False,
"function": ret.get("fun"),
"arguments": ret.get("fun_args", []),
"jid": ret.get("jid"),
}
ret_return = ret.get("return")
if _state_return(ret):
ret_return = _process_state(ret_return)
if not show_tasks:
del ret_return[CHANGED_KEY][TASKS_KEY]
del ret_return[FAILED_KEY][TASKS_KEY]
elif isinstance(ret_return, dict):
ret_return = {
"return": "\n{}".format(salt.utils.yaml.safe_dump(ret_return, indent=2))
}
else:
ret_return = {"return": ret_return}
report.update(ret_return)
return report
def _post_message(webhook, author_icon, title, report):
"""
Send a message to a Slack room through a webhook
:param webhook: The url of the incoming webhook
:param author_icon: The thumbnail image to be displayed on the right side of the message
:param title: The title of the message
:param report: The report of the function state
:return: Boolean if message was sent successfully
"""
payload = _generate_payload(author_icon, title, report)
data = _urlencode({"payload": json.dumps(payload, ensure_ascii=False)})
webhook_url = _urljoin("https://hooks.slack.com/services/", webhook)
query_result = salt.utils.http.query(webhook_url, "POST", data=data)
# Sometimes the status is not available, so status 200 is assumed when it is not present
if (
query_result.get("body", "failed") == "ok"
and query_result.get("status", 200) == 200
):
return True
else:
log.error("Slack incoming webhook message post result: %s", query_result)
return {"res": False, "message": query_result.get("body", query_result)}
def returner(ret):
"""
Send a slack message with the data through a webhook
:param ret: The Salt return
:return: The result of the post
"""
_options = _get_options(ret)
webhook = _options.get("webhook", None)
show_tasks = _options.get("show_tasks")
author_icon = _options.get("author_icon")
if not webhook or webhook == "":
log.error("%s.webhook not defined in salt config", __virtualname__)
return
report = _generate_report(ret, show_tasks)
if report.get("success"):
title = _options.get("success_title")
else:
title = _options.get("failure_title")
slack = _post_message(webhook, author_icon, title, report)
return slack