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
24 def check_double_id(id):
27 ids = open(path.processed_ids_file)
30 # FIXME: security email here?
31 log.alert("request %s already processed" % id)
35 ids = open(path.processed_ids_file, "a")
41 def handle_group(r, user):
44 if len(r.batches) >= 1:
45 spec = r.batches[0].spec
48 log.error("%s: %s" % (spec, msg))
50 m.set_headers(to = r.requester_email, cc = config.builder_list)
51 m.set_headers(subject = "building %s failed" % spec)
55 lockf = lock("request")
56 if check_double_id(r.id):
61 if (user.change_requester and r.requester):
62 user = acl.user_by_login(r.requester)
64 r.requester += '/' + user.get_login()
66 r.requester = user.get_login()
67 r.requester_email = user.mail_to()
69 for batch in r.batches:
71 if not user.can_do("src", config.builder, batch.branch):
72 fail_mail("user %s is not allowed to src:%s:%s" \
73 % (user.get_login(), config.builder, batch.branch))
77 if 'test-build' in r.flags and 'upgrade' in r.flags:
78 fail_mail("it's forbidden to upgrade from a test build")
82 if "upgrade" in r.flags and not user.can_do("upgrade", config.builder, batch.branch):
83 fail_mail("user %s is not allowed to upgrade:%s:%s" \
84 % (user.get_login(), config.builder, batch.branch))
88 # src builder handles only special commands
89 if batch.is_command() and (batch.command in ["git pull"] or batch.command[:5] == "skip:" or config.builder in batch.builders):
90 batch.expand_builders(config.binary_builders + [config.src_builder])
92 batch.expand_builders(config.binary_builders)
94 if not batch.is_command() and config.builder in batch.builders:
95 batch.builders.remove(config.builder)
97 for bld in batch.builders:
98 batch.builders_status[bld] = '?'
99 batch.builders_status_time[bld] = time.time()
100 if bld not in config.binary_builders and bld != config.builder:
101 fail_mail("I (src rpm builder '%s') do not handle binary builder '%s', only '%s'" % \
102 (config.builder, bld, ' '.join(config.binary_builders)))
105 if batch.is_command():
106 if "no-chroot" in batch.command_flags:
107 if not user.can_do("command-no-chroot", bld):
108 fail_mail("user %s is not allowed to command-no-chroot:%s" \
109 % (user.get_login(), bld))
112 if not user.can_do("command", bld):
113 fail_mail("user %s is not allowed to command:%s" \
114 % (user.get_login(), bld))
117 elif not user.can_do("binary", bld, batch.branch):
119 if pkg.endswith(".spec"):
121 if not user.can_do("binary-" + pkg, bld, batch.branch):
122 fail_mail("user %s is not allowed to binary-%s:%s:%s" \
123 % (user.get_login(), pkg, bld, batch.branch))
126 if not "test-build" in r.flags and not user.can_do("ready", bld, batch.branch):
127 fail_mail("user %s is not allowed to send ready builds (ready:%s:%s)" \
128 % (user.get_login(), bld, batch.branch))
133 if pkg.endswith(".spec"):
135 if not "test-build" in r.flags and blacklist.package(pkg):
136 fail_mail("package '%s' is blacklisted, only test-builds allowed" % pkg)
140 r.priority = user.check_priority(r.priority,config.builder)
142 log.notice("queued %s from %s" % (r.id, user.get_login()))
143 q = B_Queue(path.queue_file)
151 def handle_notification(r, user):
152 if not user.can_do("notify", r.builder):
153 log.alert("user %s is not allowed to notify:%s" % (user.login, r.builder))
154 q = B_Queue(path.req_queue_file)
157 not_fin = list(filter(lambda r: not r.is_done(), q.requests))
161 util.clean_tmp(path.srpms_dir + '/' + r.id)
164 # for ,,done'' set timeout to 4d
165 if r.is_done() and r.time + 4 * 24 * 60 * 60 < now:
167 # and for not ,,done'' set it to 20d
168 if r.time + 20 * 24 * 60 * 60 < now:
169 util.clean_tmp(path.srpms_dir + '/' + r.id)
172 q.requests = list(filter(leave_it, q.requests))
174 q.dump(path.queue_stats_file)
175 q.dump_html(path.queue_html_stats_file)
176 q.write_signed(path.req_queue_signed_file)
179 def handle_request(req, filename = None):
181 log.alert('Empty body received. Filename: %s' % filename)
184 keys = gpg.get_keys(req)
185 (em, body) = gpg.verify_sig(req)
187 log.alert("Invalid signature, missing/untrusted key. Keys in gpg batch: '%s'" % keys)
189 user = acl.user_by_email(em)
191 # FIXME: security email here
192 log.alert("'%s' not in acl. Keys in gpg batch: '%s'" % (em, keys))
195 acl.set_current_user(user)
196 status.push("request from %s" % user.login)
197 r = request.parse_request(body)
198 if r.kind == 'group':
199 # messagebus.notify(topic="request.group", user=user.login, **r.dump_json())
200 handle_group(r, user)
201 elif r.kind == 'notification':
202 # messagebus.notify(topic="request.notify", user=user.login, **r.dump_json())
203 handle_notification(r, user)
205 msg = "%s: don't know how to handle requests of this kind '%s'" \
206 % (user.get_login(), r.kind)
208 m = user.message_to()
209 m.set_headers(subject = "unknown request")
215 def handle_request_main(req, filename = None):
217 blacklist.try_reload()
219 status.push("handling email request")
220 ret = handle_request(req, filename = filename)
225 sys.exit(not handle_request_main(sys.stdin.read()))
227 if __name__ == '__main__':