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:
70 if error.errno in [-3, 60, 61, 110, 111]:
71 b.log_line("unable to connect... trying again")
80 src_url = config.control_url + "/srpms/" + r.id + "/" + urllib.quote(b.src_rpm)
81 b.log_line("fetching %s" % src_url)
86 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
87 req = urllib2.Request(url=src_url, headers=headers)
88 f = urllib2.urlopen(req)
90 except urllib2.HTTPError, error:
91 # fail in a way where cron job will retry
92 msg = "unable to fetch url %s, http code: %d" % (src_url, error.code)
94 queue_time = time.time() - r.time
96 if error.code != 404 or (queue_time >= 0 and queue_time < (6 * 60 * 60)):
99 msg = "in queue for more than 6 hours, download failing"
102 except urllib2.URLError, error:
104 if error.errno in [-3, 60, 61, 110, 111]:
105 b.log_line("unable to connect to %s... trying again" % (src_url))
110 o = chroot.popen("cat > %s" % b.src_rpm, mode = "w")
113 bytes = util.sendfile(f, o)
115 b.log_line("error: unable to write to `%s': %s" % (b.src_rpm, e))
120 t = time.time() - start
122 b.log_line("fetched %d bytes" % bytes)
124 b.log_line("fetched %d bytes, %.1f K/s" % (bytes, bytes / 1024.0 / t))
128 test ! -f /proc/uptime && mount /proc 2>/dev/null
129 test ! -c /dev/full && rm -f /dev/full && mknod -m 666 /dev/full c 1 7
130 test ! -c /dev/null && rm -f /dev/null && mknod -m 666 /dev/null c 1 3
131 test ! -c /dev/random && rm -f /dev/random && mknod -m 644 /dev/random c 1 8
132 test ! -c /dev/urandom && rm -f /dev/urandom && mknod -m 644 /dev/urandom c 1 9
133 test ! -c /dev/zero && rm -f /dev/zero && mknod -m 666 /dev/zero c 1 5
135 # make neccessary files readable for builder user
136 # TODO: see if they really aren't readable for builder
137 for db in Packages Name Basenames Providename Pubkeys; do
139 test -f $db && chmod a+r $db
142 # try to limit network access for builder account
143 /bin/setfacl -m u:builder:--- /etc/resolv.conf
147 status.push("building %s" % b.spec)
148 b.log_line("request from: %s" % r.requester)
150 if check_skip_build(r, b):
151 b.log_line("build skipped due to src builder request")
152 res = "SKIP_REQUESTED"
155 b.log_line("started at: %s" % time.asctime())
157 b.log_line("installing srpm: %s" % b.src_rpm)
159 install -d rpm/SPECS rpm/SOURCES
161 """ % b.src_rpm, logfile = b.logfile)
162 chroot.run("rm -f %s" % b.src_rpm, logfile = b.logfile)
164 tmpdir = "/tmp/B." + b.b_id[0:6]
166 b.log_line("error: installing src rpm failed")
167 res = "FAIL_SRPM_INSTALL"
170 chroot.run("install -m 700 -d %s" % tmpdir)
172 b.default_target(config.arch)
173 rpmbuild_opt = "%s %s %s" % (b.target_string(), b.kernel_string(), b.bconds_string())
174 # check for build arch before filling BR
175 cmd = "cd rpm/SPECS; TMPDIR=%s exec nice -n %s rpmbuild -bp --short-circuit --nodeps --define 'prep exit 0' %s %s" % \
176 (tmpdir, config.nice, rpmbuild_opt, b.spec)
177 res = chroot.run(cmd, logfile = b.logfile)
180 b.log_line("error: build arch check (%s) failed" % cmd)
183 if ("no-install-br" not in r.flags) and not install.uninstall_self_conflict(b):
184 res = "FAIL_DEPS_UNINSTALL"
185 if ("no-install-br" not in r.flags) and not install.install_br(r, b):
186 res = "FAIL_DEPS_INSTALL"
188 max_jobs = max(min(int(os.sysconf('SC_NPROCESSORS_ONLN') + 1), config.max_jobs), 1)
190 max_jobs = max(min(config.max_jobs, r.max_jobs), 1)
191 cmd = "echo build-id: %s; cd rpm/SPECS; TMPDIR=%s exec nice -n %s rpmbuild -bb --define '_smp_mflags -j%d' %s %s" % \
192 (r.id, tmpdir, config.nice, max_jobs, rpmbuild_opt, b.spec)
193 b.log_line("building RPM using: %s" % cmd)
194 begin_time = time.time()
195 res = chroot.run(cmd, logfile = b.logfile)
196 end_time = time.time()
197 b.log_line("ended at: %s, done in %s" % (time.asctime(), datetime.timedelta(0, end_time - begin_time)))
200 files = util.collect_files(b.logfile)
202 r.chroot_files.extend(files)
204 b.log_line("error: No files produced.")
205 last_section = util.find_last_section(b.logfile)
206 if last_section == None:
209 res = "FAIL_%s" % last_section.upper()
212 chroot.run("rm -rf %s; cd rpm/SPECS; rpmbuild --nodeps --nobuild " \
213 "--clean --rmspec --rmsource %s" % \
214 (tmpdir, b.spec), logfile = b.logfile)
215 chroot.run("chmod -R u+rwX rpm/BUILD/*; rm -rf rpm/BUILD/*", logfile = b.logfile)
218 util.append_to(b.logfile, l)
221 rpm_cache_dir = config.rpm_cache_dir
222 if "test-build" not in r.flags:
223 # NOTE: copying to cache dir doesn't mean that build failed, so ignore result
224 b.log_line("copy rpm files to cache_dir: %s" % rpm_cache_dir)
226 "cp -f %s %s && poldek --mo=nodiff --mkidxz -s %s/" % \
227 (string.join(b.files), rpm_cache_dir, rpm_cache_dir),
228 logfile = b.logfile, user = "root"
231 ll("test-build: not copying to " + rpm_cache_dir)
232 ll("Begin-PLD-Builder-Info")
233 if "upgrade" in r.flags:
234 b.upgraded = install.upgrade_from_batch(r, b)
237 ll("End-PLD-Builder-Info")
240 local = r.tmp_dir + os.path.basename(f)
241 chroot.cp(f, outfile = local, rm = True)
245 c="file:SRPMS:%s\n" % b.src_rpm
247 c=c + "file:ARCH:%s\n" % os.path.basename(f)
251 if config.gen_upinfo and b.files != [] and 'test-build' not in r.flags:
252 fname = r.tmp_dir + b.src_rpm + ".uploadinfo"
254 f.write(uploadinfo(b))
256 ftp.add(fname, "uploadinfo")
262 def handle_request(r):
265 build.build_all(r, build_rpm)
266 report.send_report(r, is_src = False)
273 f = open("/proc/loadavg")
274 if float(string.split(f.readline())[2]) > config.max_load:
281 def main_for(builder):
286 q = B_Queue(path.queue_file + "-" + config.builder)
292 req = pick_request(q)
295 # high priority tasks have priority < 0, normal tasks >= 0
296 if req.priority >= 0:
298 # allow only one build in given builder at once
299 if not lock.lock("building-rpm-for-%s" % config.builder, non_block = 1):
303 # not more then job_slots builds at once
305 for slot in range(config.job_slots):
306 if lock.lock("building-rpm-slot-%d" % slot, non_block = 1):
312 # record fact that we got lock for this builder, load balancer
313 # will use it for fair-queuing
314 l = lock.lock("got-lock")
315 f = open(path.got_lock_file, "a")
316 f.write(config.builder + "\n")
320 msg = "HIGH PRIORITY: "
322 msg += "handling request %s (%d) for %s from %s, priority %s" \
323 % (req.id, req.no, config.builder, req.requester, req.priority)
335 q = B_Queue(path.queue_file + "-" + config.builder)
338 previouslen=len(q.requests)
339 q.requests=filter(otherreqs, q.requests)
340 if len(q.requests)<previouslen:
345 if len(sys.argv) < 2:
346 raise Exception, "fatal: need to have builder name as first arg"
347 return main_for(sys.argv[1])
349 if __name__ == '__main__':