]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | import cherrypy | |
4 | from flup.server.fcgi import WSGIServer | |
5 | import logging | |
6 | import json | |
7 | import os | |
8 | import subprocess | |
9 | import sys | |
10 | import traceback | |
11 | import dateutil.parser | |
12 | ||
13 | HERE = os.path.abspath(os.path.dirname(__file__)) | |
14 | ZWRITE = os.path.join(HERE, 'bin', 'zsend') | |
15 | LOG_FILENAME = 'logs/zcommit.log' | |
16 | ||
17 | # Set up a specific logger with our desired output level | |
18 | logger = logging.getLogger(__name__) | |
19 | logger.setLevel(logging.DEBUG) | |
20 | ||
21 | # Add the log message handler to the logger | |
22 | handler = logging.FileHandler(LOG_FILENAME) | |
23 | logger.addHandler(handler) | |
24 | ||
25 | def zephyr(sender, klass, instance, zsig, msg): | |
26 | # TODO: spoof the sender | |
27 | logger.info("""About to send zephyr: | |
28 | sender: %(sender)s | |
29 | class: %(klass)s | |
30 | instance: %(instance)s | |
31 | zsig: %(zsig)s | |
32 | msg: %(msg)s""" % {'sender' : sender, | |
33 | 'klass' : klass, | |
34 | 'instance' : instance, | |
35 | 'zsig' : zsig, | |
36 | 'msg' : msg}) | |
37 | cmd = [ZWRITE, '-S', sender, '-c', klass, '-i', instance, | |
38 | '-s', zsig, '-d', '-m', msg] | |
39 | subprocess.check_call([p.encode('utf-8') for p in cmd]) | |
40 | ||
41 | class Application(object): | |
42 | @cherrypy.expose | |
43 | def index(self): | |
44 | logger.debug('Hello world app reached') | |
45 | return """ | |
46 | <p> <i>Welcome to zcommit.</i> </p> | |
47 | ||
48 | <p> zcommit allows you to send zephyr notifications by sending an HTTP | |
49 | POST request to a URL. Currently zcommit supports POST-backs from | |
50 | github. If you would like it to support another form of POST-back, | |
51 | please let us know (zcommit@mit.edu). </p> | |
52 | ||
53 | <h1> URL structure </h1> | |
54 | ||
55 | The URL you post to is structured as follows: | |
56 | <tt>http://zcommit.mit.edu/$type/$key1/$value1/$key2/$value2/...</tt>. | |
57 | So for example, the URL | |
58 | <tt>http://zcommit.mit.edu/github/class/zcommit/instance/commit</tt> | |
59 | is parsed as having type <tt>github</tt>, class <tt>zcommit</tt>, and | |
60 | instance <tt>commit</tt>. Using this information, zcommit figures out | |
61 | how to form a useful message which is then sends as a zephyr. | |
62 | ||
63 | <h1> Types </h1> | |
64 | ||
65 | <h2> Github </h2> | |
66 | ||
67 | Set your POST-back URL to | |
68 | <tt>http://zcommit.mit.edu/github/class/$classname</tt>, followed by | |
69 | any of the following optional key/value parameters: | |
70 | ||
71 | <ul> | |
72 | <li> <tt>/instance/$instance</tt> </li> | |
73 | <li> <tt>/zsig/$zsig</tt> (sets the prefix of the zsig; the postfix is always the branch name) </li> | |
74 | <li> <tt>/sender/$sender</tt> </li> | |
75 | </ul> | |
76 | """ | |
77 | ||
78 | class Github(object): | |
79 | @cherrypy.expose | |
80 | def default(self, *args, **query): | |
81 | try: | |
82 | return self._default(*args, **query) | |
83 | except Exception, e: | |
84 | logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc())) | |
85 | raise | |
86 | ||
87 | def _default(self, *args, **query): | |
88 | logger.info('A %s request with args: %r and query: %r' % | |
89 | (cherrypy.request.method, args, query)) | |
90 | opts = {} | |
91 | if len(args) % 2: | |
92 | raise cherrypy.HTTPError(400, 'Invalid submission URL') | |
93 | logger.debug('Passed validation') | |
94 | for i in xrange(0, len(args), 2): | |
95 | opts[args[i]] = unicode(args[i + 1], 'utf-8', 'replace') | |
96 | logger.debug('Set opts') | |
97 | if 'class' not in opts: | |
98 | raise cherrypy.HTTPError(400, 'Must specify a zephyr class name') | |
99 | logger.debug('Specified a class') | |
100 | if cherrypy.request.method == 'POST': | |
101 | logger.debug('About to load data') | |
102 | payload = json.loads(query['payload']) | |
103 | logger.debug('Loaded payload data') | |
104 | zsig = payload['ref'] | |
105 | if 'zsig' in opts: | |
106 | zsig = '%s: %s' % (opts['zsig'], zsig) | |
107 | sender = opts.get('sender', 'daemon.zcommit') | |
108 | logger.debug('Set zsig') | |
109 | for c in payload['commits']: | |
110 | inst = opts.get('instance', c['id'][:8]) | |
111 | actions = [] | |
112 | if c.get('added'): | |
113 | actions.extend(' A %s\n' % f for f in c['added']) | |
114 | if c.get('removed'): | |
115 | actions.extend(' D %s\n' % f for f in c['removed']) | |
116 | if c.get('modified'): | |
117 | actions.extend(' M %s\n' % f for f in c['modified']) | |
118 | if not actions: | |
119 | actions.append('Did not add/remove/modify any nonempty files.') | |
120 | info = {'name' : c['author']['name'], | |
121 | 'email' : c['author']['email'], | |
122 | 'message' : c['message'], | |
123 | 'timestamp' : dateutil.parser.parse(c['timestamp']).strftime('%F %T %z'), | |
124 | 'actions' : ''.join(actions), | |
125 | 'url' : c['url']} | |
126 | ||
127 | msg = """%(url)s | |
128 | Author: %(name)s <%(email)s> | |
129 | Date: %(timestamp)s | |
130 | ||
131 | %(message)s | |
132 | --- | |
133 | %(actions)s""" % info | |
134 | zephyr(sender, opts['class'], inst, zsig, msg) | |
135 | msg = 'Thanks for posting!' | |
136 | else: | |
137 | msg = ('If you had sent a POST request to this URL, would have sent' | |
138 | ' a zepyhr to -c %s' % opts['class']) | |
139 | return msg | |
140 | ||
141 | github = Github() | |
142 | ||
143 | def main(): | |
144 | app = cherrypy.tree.mount(Application(), '/zcommit') | |
145 | cherrypy.server.unsubscribe() | |
146 | cherrypy.engine.start() | |
147 | try: | |
148 | WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run() | |
149 | finally: | |
150 | cherrypy.engine.stop() | |
151 | ||
152 | if __name__ == '__main__': | |
153 | sys.exit(main()) |