]> git.pld-linux.org Git - projects/pld-builder.new.git/blame - PLD_Builder/request_handler.py
Remove done jobs from queue.
[projects/pld-builder.new.git] / PLD_Builder / request_handler.py
CommitLineData
dfff8bd5
MM
1# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
57b6e61d
MM
3import email
4import string
5import time
6import os
7import StringIO
cc94eb63 8import sys
91f92271 9import fnmatch
57b6e61d
MM
10
11import gpg
12import request
13import log
14import path
c8782384 15import util
6b44a683 16import wrap
bdc3292c 17import status
57b6e61d
MM
18from acl import acl
19from lock import lock
20from bqueue import B_Queue
b5a39692 21from config import config, init_conf
57b6e61d
MM
22
23def check_double_id(id):
dfff8bd5
MM
24 id_nl = id + "\n"
25
26 ids = open(path.processed_ids_file)
27 for i in ids.xreadlines():
28 if i == id_nl:
29 # FIXME: security email here?
30 log.alert("request %s already processed" % id)
31 return 1
32 ids.close()
33
34 ids = open(path.processed_ids_file, "a")
35 ids.write(id_nl)
36 ids.close()
57b6e61d 37
dfff8bd5 38 return 0
57b6e61d 39
34738db3 40def handle_group(r, user):
e1389e9c 41 lockf = None
dfff8bd5
MM
42 def fail_mail(msg):
43 if len(r.batches) >= 1:
44 spec = r.batches[0].spec
45 else:
46 spec = "None.spec"
47 log.error("%s: %s" % (spec, msg))
48 m = user.message_to()
49 m.set_headers(subject = "building %s failed" % spec)
50 m.write_line(msg)
51 m.send()
57b6e61d 52
e1389e9c 53 lockf = lock("request")
dfff8bd5 54 if check_double_id(r.id):
e1389e9c 55 lockf.close()
dfff8bd5
MM
56 return
57
91f92271 58
dfff8bd5
MM
59 for batch in r.batches:
60 if not user.can_do("src", config.builder, batch.branch):
61 fail_mail("user %s is not allowed to src:%s:%s" \
62 % (user.get_login(), config.builder, batch.branch))
e1389e9c 63 lockf.close()
dfff8bd5 64 return
d2c1ec3f 65
e936beda
MM
66 if 'test-build' in r.flags and 'upgrade' in r.flags:
67 fail_mail("it's forbidden to upgrade from a test build")
e1389e9c 68 lockf.close()
e936beda
MM
69 return
70
dfff8bd5
MM
71 if "upgrade" in r.flags and not user.can_do("upgrade", config.builder, batch.branch):
72 fail_mail("user %s is not allowed to upgrade:%s:%s" \
73 % (user.get_login(), config.builder, batch.branch))
e1389e9c 74 lockf.close()
dfff8bd5 75 return
d2c1ec3f 76
07db6665 77 # src builder handles only special commands
d078b186 78 if batch.is_command() and (batch.command in ["cvs up"] or config.builder in batch.builders):
07db6665
AM
79 batch.expand_builders(config.binary_builders + [config.src_builder])
80 else:
81 batch.expand_builders(config.binary_builders)
82
dfff8bd5
MM
83 if not batch.is_command() and config.builder in batch.builders:
84 batch.builders.remove(config.builder)
07db6665 85
dfff8bd5
MM
86 for bld in batch.builders:
87 batch.builders_status[bld] = '?'
88 if bld not in config.binary_builders and bld != config.builder:
89 fail_mail("I (src rpm builder '%s') do not handle binary builder '%s', only '%s'" % \
9e8a7e70 90 (config.builder, bld, string.join(config.binary_builders)))
e1389e9c 91 lockf.close()
dfff8bd5
MM
92 return
93 if batch.is_command():
e242bc52
AM
94 if "no-chroot" in batch.command_flags:
95 if not user.can_do("command-no-chroot", bld):
96 fail_mail("user %s is not allowed to command-no-chroot:%s" \
97 % (user.get_login(), bld))
98 lockf.close()
99 return
dfff8bd5
MM
100 if not user.can_do("command", bld):
101 fail_mail("user %s is not allowed to command:%s" \
102 % (user.get_login(), bld))
e1389e9c 103 lockf.close()
dfff8bd5
MM
104 return
105 elif not user.can_do("binary", bld, batch.branch):
106 pkg = batch.spec
107 if pkg.endswith(".spec"):
108 pkg = pkg[:-5]
109 if not user.can_do("binary-" + pkg, bld, batch.branch):
110 fail_mail("user %s is not allowed to binary-%s:%s:%s" \
111 % (user.get_login(), pkg, bld, batch.branch))
e1389e9c 112 lockf.close()
dfff8bd5
MM
113 return
114
115 r.priority = user.check_priority(r.priority,config.builder)
116 r.requester = user.get_login()
117 r.requester_email = user.mail_to()
118 r.time = time.time()
119 log.notice("queued %s from %s" % (r.id, user.get_login()))
120 q = B_Queue(path.queue_file)
121 q.lock(0)
122 q.read()
123 q.add(r)
124 q.write()
125 q.unlock()
e1389e9c 126 lockf.close()
57b6e61d 127
59ce7cd6 128def handle_notification(r, user):
dfff8bd5
MM
129 if not user.can_do("notify", r.builder):
130 log.alert("user %s is not allowed to notify:%s" % (user.login, r.builder))
131 q = B_Queue(path.req_queue_file)
132 q.lock(0)
133 q.read()
134 not_fin = filter(lambda (r): not r.is_done(), q.requests)
135 r.apply_to(q)
136 for r in not_fin:
137 if r.is_done():
9be34149 138 util.clean_tmp(path.srpms_dir + '/' + r.id)
dfff8bd5
MM
139 now = time.time()
140 def leave_it(r):
141 # for ,,done'' set timeout to 4d
142 if r.is_done() and r.time + 4 * 24 * 60 * 60 < now:
143 return False
144 # and for not ,,done'' set it to 20d
145 if r.time + 20 * 24 * 60 * 60 < now:
9be34149 146 util.clean_tmp(path.srpms_dir + '/' + r.id)
dfff8bd5
MM
147 return False
148 return True
149 q.requests = filter(leave_it, q.requests)
150 q.write()
151 q.dump(open(path.queue_stats_file, "w"))
152 q.dump_html(open(path.queue_html_stats_file, "w"))
153 os.chmod(path.queue_html_stats_file, 0644)
154 os.chmod(path.queue_stats_file, 0644)
89ad1c79 155 q.write_signed(path.req_queue_signed_file)
156 os.chmod(path.req_queue_signed_file, 0644)
dfff8bd5 157 q.unlock()
59ce7cd6 158
f02c1e4d
AM
159def handle_request(req, filename = None):
160 if req == '':
c1ad9456
ER
161 log.alert('Empty body received. Filename: %s' % filename)
162 return False
163
8736fafd 164 keys = gpg.get_keys(req)
f02c1e4d 165 (em, body) = gpg.verify_sig(req)
8736fafd 166 if not em:
030fb437 167 log.alert("Invalid signature, missing/untrusted key. Keys in gpg batch: '%s'" % keys)
8736fafd 168 return False
dfff8bd5
MM
169 user = acl.user_by_email(em)
170 if user == None:
171 # FIXME: security email here
8736fafd 172 log.alert("'%s' not in acl. Keys in gpg batch: '%s'" % (em, keys))
f6eb7039 173 return False
b5e0afc4 174
dfff8bd5
MM
175 acl.set_current_user(user)
176 status.push("email from %s" % user.login)
177 r = request.parse_request(body)
178 if r.kind == 'group':
179 handle_group(r, user)
180 elif r.kind == 'notification':
181 handle_notification(r, user)
182 else:
183 msg = "%s: don't know how to handle requests of this kind '%s'" \
184 % (user.get_login(), r.kind)
185 log.alert(msg)
186 m = user.message_to()
187 m.set_headers(subject = "unknown request")
188 m.write_line(msg)
189 m.send()
190 status.pop()
f6eb7039 191 return True
cc94eb63 192
f02c1e4d 193def handle_request_main(req, filename = None):
dfff8bd5
MM
194 init_conf("src")
195 status.push("handling email request")
f02c1e4d 196 ret = handle_request(req, filename = filename)
dfff8bd5 197 status.pop()
63319e0a
AM
198 return ret
199
200def main():
f02c1e4d 201 sys.exit(not handle_request_main(sys.stdin.read()))
1f62bccc 202
e8ee9db8 203if __name__ == '__main__':
dfff8bd5 204 wrap.wrap(main)
This page took 0.104023 seconds and 4 git commands to generate.