Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 192 lines (141 sloc) 5.561 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
#
# Copyright (c) 2010-2012 Liraz Siri <liraz@turnkeylinux.org>
#
# This file is part of CloudTask.
#
# CloudTask is open source software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#

import os
from os.path import *
import re

import shlex
from email.Message import Message
from command import Command

import traceback
from StringIO import StringIO

import logalyzer

class Error(Exception):
    pass

class PythonHandler:
    def __init__(self, expr):
        if isfile(expr):
            expr = file(expr).read()

        self.code = compile(expr, '--report', 'exec')

    def __call__(self, session):
        taskconf = session.taskconf

        vars = {'session': session,
                'jobs': session.jobs,
                'taskconf': session.taskconf,
                'command': taskconf.command,
                'split': taskconf.split}

        eval(self.code, {}, vars)

class MailHandler:
    class Sendmail:
        SENDMAIL_PATH = "/usr/sbin/sendmail"
        def __init__(self):
            if not exists(self.SENDMAIL_PATH):
                raise Error("can't use mail handler: missing " + self.SENDMAIL_PATH)

        def __call__(self, sender, recipient, subject, body):
            
            msg = Message()
            msg.add_header("Subject", subject)
            msg.add_header("To", str(recipient))
            msg.add_header("From", str(sender))
            msg.set_payload(body)

            command = [ self.SENDMAIL_PATH, '-i', '-f', sender.address, recipient.address ]
            command = Command(command)
            command.tochild.write(msg.as_string())
            command.tochild.close()
            command.wait()

            if command.exitcode != 0:
                raise Error("sendmail failed (%d): %s" % (command.exitcode,
                                                          command.output))

    class Email:
        def __init__(self, email):
            self.email = email
            m = re.search(r'<(.*)>', email)
            if m:
                address = m.group(1)
            else:
                address = email

            if not re.match(r'^[\w\.]+\@[\w\.]+$', address):
                raise Error("illegal email address '%s'" % email)

            self.address = address

        def __repr__(self):
            return '<Email(%s)>' % `str(self)`

        def __str__(self):
            return self.email

    def __init__(self, expr):
        args = [ self.Email(arg) for arg in shlex.split(expr) ]
        if len(args) < 2:
            raise Error("mail handler needs at least 1 recipient (in addition to the sender's address)")

        self.sender = args[0]
        self.recipients = args[1:]
        self.sendmail = self.Sendmail()

    def __call__(self, session):
        taskconf = session.taskconf

        jobs_total = len(session.jobs.pending) + len(session.jobs.finished)
        jobs_completed = len([ job for job, result in session.jobs.finished if result=="EXIT=0" ])
        jobs_incomplete = jobs_total - jobs_completed

        command = re.sub(r'^\S*/', '', taskconf['command'])

        title = "session %d: %d/%d !OK (%s)" % (session.id, jobs_incomplete, jobs_total, command)

        try:
            body = logalyzer.logalyzer(session.paths.path)
        except Exception:
            sio = StringIO()
            def header(title, c):
                return title + "\n" + len(title) * c + "\n"

            print >> sio, header("Logalyzer failure", "=")
            traceback.print_exc(file=sio)
            print >> sio
            print >> sio, header("Fallback session log", "=")
            print >> sio, taskconf.fmt()

            manager_log = file(session.paths.log).read()
            print >> sio, manager_log

            body = sio.getvalue()

        for recipient in self.recipients:

            subject = "[Cloudtask] " + title

            self.sendmail(self.sender, recipient,
                          subject, body)


class ShellHandler:
    ENV_WHITELIST = ('HOME', 'PATH', 'USER', 'SHELL')
    def __init__(self, expr):
        self.command = expr

    def __call__(self, session):
        os.chdir(session.paths.path)

        for var in os.environ.keys():
            if var not in self.ENV_WHITELIST:
                del os.environ[var]

        taskconf = session.taskconf
        for attr in taskconf.__all__:
            if attr == 'workers':
                continue

            if taskconf[attr]:
                os.environ['CLOUDTASK_' + attr.upper()] = str(taskconf[attr])

        if taskconf.workers:
            os.environ['CLOUDTASK_WORKERS'] = " ".join(taskconf.workers)

        os.system(self.command)

class Reporter:
    Error = Error

    handlers = {
        'py': PythonHandler,
        'sh': ShellHandler,
        'mail': MailHandler
    }

    def __init__(self, hook):

        handlers = self.handlers

        m = re.match(r'(.*?):\s*(.*)', hook)
        if not m:
            raise self.Error("can't parser reporting hook '%s'" % hook)

        handler, expr = m.groups()

        if handler not in handlers:
            raise self.Error("no '%s' in supported reporting handlers (%s)" % (handler, ", ".join(handlers)))


        handler = handlers[handler]
        try:
            self.handler = handler(expr)
        except Exception, e:
            raise self.Error(e)

    def report(self, session):
        self.handler(session)
Something went wrong with that request. Please try again.