1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
17 from blacklist import blacklist
19 from bqueue import B_Queue
20 from config import config, init_conf
21 from mailer import Message
23 def check_double_id(id):
26 ids = open(path.processed_ids_file)
29 # FIXME: security email here?
30 log.alert("request %s already processed" % id)
34 ids = open(path.processed_ids_file, "a")
40 def handle_group(r, user):
43 if len(r.batches) >= 1:
44 spec = r.batches[0].spec
47 log.error("%s: %s" % (spec, msg))
49 m.set_headers(to = r.requester_email, cc = config.builder_list)
50 m.set_headers(subject = "building %s failed" % spec)
54 lockf = lock("request")
55 if check_double_id(r.id):
60 if (user.change_requester and r.requester):
61 user = acl.user_by_login(r.requester)
63 r.requester += '/' + user.get_login()
65 r.requester = user.get_login()
66 r.requester_email = user.mail_to()
68 for batch in r.batches:
70 if not user.can_do("src", config.builder, batch.branch):
71 fail_mail("user %s is not allowed to src:%s:%s" \
72 % (user.get_login(), config.builder, batch.branch))
76 if 'test-build' in r.flags and 'upgrade' in r.flags:
77 fail_mail("it's forbidden to upgrade from a test build")
81 if "upgrade" in r.flags and not user.can_do("upgrade", config.builder, batch.branch):
82 fail_mail("user %s is not allowed to upgrade:%s:%s" \
83 % (user.get_login(), config.builder, batch.branch))
87 # src builder handles only special commands
88 if batch.is_command() and (batch.command in ["git pull"] or batch.command[:5] == "skip:" or config.builder in batch.builders):
89 batch.expand_builders(config.binary_builders + [config.src_builder])
91 batch.expand_builders(config.binary_builders)
93 if not batch.is_command() and config.builder in batch.builders:
94 batch.builders.remove(config.builder)
96 for bld in batch.builders:
97 batch.builders_status[bld] = '?'
98 batch.builders_status_time[bld] = time.time()
99 if bld not in config.binary_builders and bld != config.builder:
100 fail_mail("I (src rpm builder '%s') do not handle binary builder '%s', only '%s'" % \
101 (config.builder, bld, ' '.join(config.binary_builders)))
104 if batch.is_command():
105 if "no-chroot" in batch.command_flags:
106 if not user.can_do("command-no-chroot", bld):
107 fail_mail("user %s is not allowed to command-no-chroot:%s" \
108 % (user.get_login(), bld))
111 if not user.can_do("command", bld):
112 fail_mail("user %s is not allowed to command:%s" \
113 % (user.get_login(), bld))
116 elif not user.can_do("binary", bld, batch.branch):
118 if pkg.endswith(".spec"):
120 if not user.can_do("binary-" + pkg, bld, batch.branch):
121 fail_mail("user %s is not allowed to binary-%s:%s:%s" \
122 % (user.get_login(), pkg, bld, batch.branch))
125 if not "test-build" in r.flags and not user.can_do("ready", bld, batch.branch):
126 fail_mail("user %s is not allowed to send ready builds (ready:%s:%s)" \
127 % (user.get_login(), bld, batch.branch))
132 if pkg.endswith(".spec"):
134 if not "test-build" in r.flags and blacklist.package(pkg):
135 fail_mail("package '%s' is blacklisted, only test-builds allowed" % pkg)
139 r.priority = user.check_priority(r.priority,config.builder)
141 log.notice("queued %s from %s" % (r.id, user.get_login()))
142 q = B_Queue(path.queue_file)
150 def handle_notification(r, user):
151 if not user.can_do("notify", r.builder):
152 log.alert("user %s is not allowed to notify:%s" % (user.login, r.builder))
153 q = B_Queue(path.req_queue_file)
156 not_fin = list(filter(lambda r: not r.is_done(), q.requests))
160 util.clean_tmp(path.srpms_dir + '/' + r.id)
163 # for ,,done'' set timeout to 4d
164 if r.is_done() and r.time + 4 * 24 * 60 * 60 < now:
166 # and for not ,,done'' set it to 20d
167 if r.time + 20 * 24 * 60 * 60 < now:
168 util.clean_tmp(path.srpms_dir + '/' + r.id)
171 q.requests = list(filter(leave_it, q.requests))
173 q.dump(path.queue_stats_file)
174 q.dump_html(path.queue_html_stats_file)
175 q.write_signed(path.req_queue_signed_file)
178 def handle_request(req, filename = None):
180 log.alert('Empty body received. Filename: %s' % filename)
183 keys = gpg.get_keys(req)
184 (em, body) = gpg.verify_sig(req)
186 log.alert("Invalid signature, missing/untrusted key. Keys in gpg batch: '%s'" % keys)
188 user = acl.user_by_email(em)
190 # FIXME: security email here
191 log.alert("'%s' not in acl. Keys in gpg batch: '%s'" % (em, keys))
194 acl.set_current_user(user)
195 status.push("request from %s" % user.login)
196 r = request.parse_request(body)
197 if r.kind == 'group':
198 handle_group(r, user)
199 elif r.kind == 'notification':
200 handle_notification(r, user)
202 msg = "%s: don't know how to handle requests of this kind '%s'" \
203 % (user.get_login(), r.kind)
205 m = user.message_to()
206 m.set_headers(subject = "unknown request")
212 def handle_request_main(req, filename = None):
214 blacklist.try_reload()
216 status.push("handling email request")
217 ret = handle_request(req, filename = filename)
222 sys.exit(not handle_request_main(sys.stdin.read()))
224 if __name__ == '__main__':