1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
12 from config import config, init_conf
13 from bqueue import B_Queue
28 # *HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*
31 socket.myorigsocket=socket.socket
33 def mysocket(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
34 s=socket.myorigsocket(family, type, proto)
35 s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
38 socket.socket=mysocket
39 # *HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*HACK*
41 # this code is duplicated in srpm_builder, but we
42 # might want to handle some cases differently here
45 if r1.kind != 'group' or r2.kind != 'group':
46 raise Exception, "non-group requests"
47 pri_diff = cmp(r1.priority, r2.priority)
49 return cmp(r1.time, r2.time)
52 q.requests.sort(mycmp)
56 def check_skip_build(r, b):
57 src_url = config.control_url + "/srpms/" + r.id + "/skipme"
59 b.log_line("checking if we should skip the build")
62 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
63 req = urllib2.Request(url=src_url, headers=headers)
64 f = urllib2.urlopen(req)
66 except urllib2.HTTPError, error:
68 except urllib2.URLError, error:
72 except AttributeError:
74 errno = error.reason[0]
76 if errno in [-3, 60, 61, 110, 111]:
77 b.log_line("unable to connect... trying again")
86 src_url = config.control_url + "/srpms/" + r.id + "/" + urllib.quote(b.src_rpm)
87 b.log_line("fetching %s" % src_url)
92 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
93 req = urllib2.Request(url=src_url, headers=headers)
94 f = urllib2.urlopen(req)
96 except urllib2.HTTPError, error:
97 # fail in a way where cron job will retry
98 msg = "unable to fetch url %s, http code: %d" % (src_url, error.code)
100 queue_time = time.time() - r.time
102 if error.code != 404 or (queue_time >= 0 and queue_time < (6 * 60 * 60)):
105 msg = "in queue for more than 6 hours, download failing"
108 except urllib2.URLError, error:
110 if isinstance(error.args[0], IOError):
111 errno = error.args[0].errno
113 if errno in [-3, 60, 61, 110, 111]:
114 b.log_line("unable to connect to %s... trying again" % (src_url))
118 print "error.errno: %s" % str(error.errno)
120 print "error.errno: exception %s" % e
122 print "error.reason %s" % str(error.reason)
124 print "error.reason exception %s" % e
127 o = chroot.popen("cat > %s" % b.src_rpm, mode = "w")
130 bytes = util.sendfile(f, o)
132 b.log_line("error: unable to write to `%s': %s" % (b.src_rpm, e))
137 t = time.time() - start
139 b.log_line("fetched %d bytes" % bytes)
141 b.log_line("fetched %d bytes, %.1f K/s" % (bytes, bytes / 1024.0 / t))
143 def prepare_env(logfile = None):
145 test ! -f /proc/uptime && mount /proc 2>/dev/null
146 test ! -c /dev/full && rm -f /dev/full && mknod -m 666 /dev/full c 1 7
147 test ! -c /dev/null && rm -f /dev/null && mknod -m 666 /dev/null c 1 3
148 test ! -c /dev/random && rm -f /dev/random && mknod -m 644 /dev/random c 1 8
149 test ! -c /dev/urandom && rm -f /dev/urandom && mknod -m 644 /dev/urandom c 1 9
150 test ! -c /dev/zero && rm -f /dev/zero && mknod -m 666 /dev/zero c 1 5
152 # need entry for "/" in mtab, for diskspace() to work in rpm
153 [ -z $(awk '$2 == "/" {print $1; exit}' /etc/mtab) ] && mount -f -t rootfs rootfs /
155 # make neccessary files readable for builder user
156 # TODO: see if they really aren't readable for builder
157 for db in Packages Name Basenames Providename Pubkeys; do
159 test -f $db && chmod a+r $db
162 # try to limit network access for builder account
163 /bin/setfacl -m u:builder:--- /etc/resolv.conf
164 """, 'root', logfile = logfile)
167 packagename = b.get_package_name()
169 # should not really get here
170 b.log_line("error: No .spec not given of malformed: '%s'" % b.spec)
171 res = "FAIL_INTERNAL"
174 status.push("building %s (%s)" % (b.spec, packagename))
175 b.log_line("request from: %s" % r.requester)
177 if check_skip_build(r, b):
178 b.log_line("build skipped due to src builder request")
179 res = "SKIP_REQUESTED"
182 b.log_line("started at: %s" % time.asctime())
184 b.log_line("killing old processes on a builder")
185 chroot.run("killall -91v"), logfile = b.logfile)
188 b.log_line("installing srpm: %s" % b.src_rpm)
191 install -d %(topdir)s/{BUILD,RPMS};
192 LC_ALL=en_US.UTF-8 rpm -qp --changelog %(src_rpm)s;
193 rpm -Uhv --nodeps %(rpmdefs)s %(src_rpm)s;
196 'topdir' : b.get_topdir(),
197 'rpmdefs' : b.rpmbuild_opts(),
198 'src_rpm' : b.src_rpm
199 }, logfile = b.logfile)
204 b.log_line("error: installing src rpm failed")
205 res = "FAIL_SRPM_INSTALL"
208 chroot.run("set -x; install -m 700 -d %s" % tmpdir, logfile=b.logfile)
209 b.default_target(config.arch)
210 # check for build arch before filling BR
211 cmd = "set -ex; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
212 "rpmbuild -bp --short-circuit --nodeps %(rpmdefs)s --define 'prep exit 0' %(topdir)s/%(spec)s" % {
214 'nice' : config.nice,
215 'topdir' : b.get_topdir(),
216 'rpmdefs' : b.rpmbuild_opts(),
219 res = chroot.run(cmd, logfile = b.logfile)
222 b.log_line("error: build arch check (%s) failed" % cmd)
225 if ("no-install-br" not in r.flags) and not install.uninstall_self_conflict(b):
226 res = "FAIL_DEPS_UNINSTALL"
227 if ("no-install-br" not in r.flags) and not install.install_br(r, b):
228 res = "FAIL_DEPS_INSTALL"
230 max_jobs = max(min(int(os.sysconf('SC_NPROCESSORS_ONLN') + 1), config.max_jobs), 1)
232 max_jobs = max(min(config.max_jobs, r.max_jobs), 1)
233 cmd = "set -ex; : build-id: %(r_id)s; TMPDIR=%(tmpdir)s exec nice -n %(nice)s " \
234 "rpmbuild -bb --define '_smp_mflags -j%(max_jobs)d' --define '_make_opts -Otarget' --define '_pld_builder 1' %(rpmdefs)s %(topdir)s/%(spec)s" % {
237 'nice' : config.nice,
238 'rpmdefs' : b.rpmbuild_opts(),
239 'topdir' : b.get_topdir(),
240 'max_jobs' : max_jobs,
243 b.log_line("building RPM using: %s" % cmd)
244 begin_time = time.time()
245 res = chroot.run(cmd, logfile = b.logfile)
246 end_time = time.time()
247 b.log_line("ended at: %s, done in %s" % (time.asctime(), datetime.timedelta(0, end_time - begin_time)))
250 files = util.collect_files(b.logfile, basedir = b.get_topdir())
252 r.chroot_files.extend(files)
254 b.log_line("error: No files produced.")
255 last_section = util.find_last_section(b.logfile)
256 if last_section == None:
259 res = "FAIL_%s" % last_section.upper()
262 # cleanup tmp and build files
265 chmod -R u+rwX %(topdir)s/BUILD;
266 rm -rf %(topdir)s/{tmp,BUILD}
268 'topdir' : b.get_topdir(),
269 }, logfile = b.logfile)
272 util.append_to(b.logfile, l)
275 rpm_cache_dir = config.rpm_cache_dir
276 if "test-build" not in r.flags:
277 # NOTE: copying to cache dir doesn't mean that build failed, so ignore result
278 b.log_line("copy rpm files to cache_dir: %s" % rpm_cache_dir)
280 "cp -f %s %s && poldek --mo=nodiff --mkidxz -s %s/" % \
281 (string.join(b.files), rpm_cache_dir, rpm_cache_dir),
282 logfile = b.logfile, user = "root"
285 ll("test-build: not copying to " + rpm_cache_dir)
286 ll("Begin-PLD-Builder-Info")
287 if "upgrade" in r.flags:
288 b.upgraded = install.upgrade_from_batch(r, b)
291 ll("End-PLD-Builder-Info")
294 local = r.tmp_dir + os.path.basename(f)
295 chroot.cp(f, outfile = local, rm = True)
298 # cleanup all remains from this build
303 'topdir' : b.get_topdir(),
304 }, logfile = b.logfile)
307 c="file:SRPMS:%s\n" % b.src_rpm
309 c=c + "file:ARCH:%s\n" % os.path.basename(f)
313 if config.gen_upinfo and b.files != [] and 'test-build' not in r.flags:
314 fname = r.tmp_dir + b.src_rpm + ".uploadinfo"
316 f.write(uploadinfo(b))
318 ftp.add(fname, "uploadinfo")
324 def handle_request(r):
327 build.build_all(r, build_rpm)
328 report.send_report(r, is_src = False)
335 f = open("/proc/loadavg")
336 if float(string.split(f.readline())[2]) > config.max_load:
343 def main_for(builder):
348 q = B_Queue(path.queue_file + "-" + config.builder)
354 req = pick_request(q)
357 # high priority tasks have priority < 0, normal tasks >= 0
358 if req.priority >= 0:
360 # allow only one build in given builder at once
361 if not lock.lock("building-rpm-for-%s" % config.builder, non_block = 1):
365 # not more then job_slots builds at once
367 for slot in range(config.job_slots):
368 if lock.lock("building-rpm-slot-%d" % slot, non_block = 1):
374 # record fact that we got lock for this builder, load balancer
375 # will use it for fair-queuing
376 l = lock.lock("got-lock")
377 f = open(path.got_lock_file, "a")
378 f.write(config.builder + "\n")
382 # be able to avoid locking with very low priority
383 if req.priority > -1000:
386 # allow only one build in given builder at once
387 if not lock.lock("building-high-priority-rpm-for-%s" % config.builder, non_block = 1):
390 msg = "HIGH PRIORITY: "
392 msg += "handling request %s (%d) for %s from %s, priority %s" \
393 % (req.id, req.no, config.builder, req.requester, req.priority)
405 q = B_Queue(path.queue_file + "-" + config.builder)
408 previouslen=len(q.requests)
409 q.requests=filter(otherreqs, q.requests)
410 if len(q.requests)<previouslen:
415 if len(sys.argv) < 2:
416 raise Exception, "fatal: need to have builder name as first arg"
417 return main_for(sys.argv[1])
419 if __name__ == '__main__':