django-orchestra/orchestra/apps/orchestration/helpers.py

62 lines
2.5 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.contrib import messages
from django.core.mail import mail_admins
2014-10-03 14:02:11 +00:00
from django.core.urlresolvers import reverse
2014-05-08 16:59:35 +00:00
from django.utils.html import escape
2014-10-03 14:02:11 +00:00
from django.utils.safestring import mark_safe
from django.utils.translation import ungettext, ugettext_lazy as _
2014-05-08 16:59:35 +00:00
def send_report(method, args, log):
backend = method.im_class().get_name()
server = args[0]
subject = '[Orchestra] %s execution %s on %s'
subject = subject % (backend, log.state, server)
separator = "\n%s\n\n" % ('~ '*40,)
message = separator.join([
"[EXIT CODE] %s" % log.exit_code,
"[STDERR]\n%s" % log.stderr,
"[STDOUT]\n%s" % log.stdout,
"[SCRIPT]\n%s" % log.script,
"[TRACEBACK]\n%s" % log.traceback,
])
html_message = '\n\n'.join([
'<h4 style="color:#505050;">Exit code %s</h4>' % log.exit_code,
'<h4 style="color:#505050;">Stderr</h4>'
'<pre style="margin-left:20px;font-size:11px">%s</pre>' % escape(log.stderr),
'<h4 style="color:#505050;">Stdout</h4>'
'<pre style="margin-left:20px;font-size:11px">%s</pre>' % escape(log.stdout),
'<h4 style="color:#505050;">Script</h4>'
'<pre style="margin-left:20px;font-size:11px">%s</pre>' % escape(log.script),
'<h4 style="color:#505050;">Traceback</h4>'
'<pre style="margin-left:20px;font-size:11px">%s</pre>' % escape(log.traceback),
])
mail_admins(subject, message, html_message=html_message)
def message_user(request, logs):
2014-10-03 14:02:11 +00:00
total, successes = 0, 0
ids = []
for log in logs:
total += 1
ids.append(log.pk)
if log.state == log.SUCCESS:
successes += 1
2014-05-08 16:59:35 +00:00
errors = total-successes
2014-10-03 14:02:11 +00:00
if total > 1:
url = reverse('admin:orchestration_backendlog_changelist')
2014-10-17 13:14:31 +00:00
url += '?id__in=%s' % ','.join(map(str, ids))
2014-10-03 14:02:11 +00:00
else:
url = reverse('admin:orchestration_backendlog_change', args=ids)
2014-05-08 16:59:35 +00:00
if errors:
2014-10-03 14:02:11 +00:00
msg = ungettext(
2015-03-02 10:37:25 +00:00
_('{errors} out of {total} <a href="{url}">backends</a> has fail to execute.'),
_('{errors} out of {total} <a href="{url}">backends</a> have fail to execute.'),
2014-10-03 14:02:11 +00:00
errors)
messages.error(request, mark_safe(msg.format(errors=errors, total=total, url=url)))
2014-05-08 16:59:35 +00:00
else:
2014-10-03 14:02:11 +00:00
msg = ungettext(
2015-03-02 10:37:25 +00:00
_('{total} <a href="{url}">backend</a> has been executed.'),
_('{total} <a href="{url}">backends</a> have been executed.'),
2014-10-03 14:02:11 +00:00
total)
messages.success(request, mark_safe(msg.format(total=total, url=url)))