mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
14 KiB

  1. #!/bin/bash
  2. #
  3. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  4. #
  5. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  6. # Please add additional copyright information _after_ the line containing
  7. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  8. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  9. #
  10. # ROCK Linux: rock-src/scripts/Build-Target
  11. # ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version. A copy of the GNU General Public
  17. # License can be found at Documentation/COPYING.
  18. #
  19. # Many people helped and are helping developing ROCK Linux. Please
  20. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  21. # file for details.
  22. #
  23. # --- ROCK-COPYRIGHT-NOTE-END ---
  24. #
  25. # Run this command from the ROCK directory as ./scripts/Build-Target
  26. # after running the ./scripts/Config and ./scripts/Download commands.
  27. #
  28. # It compiles/builds all the packages and stores them as tar balls suitable
  29. # for distribution.
  30. #
  31. # This script is the ROCK work-horse. It builds in a chroot environment
  32. # (stage 2..9) and goes through a number of build stages:
  33. #
  34. config=default
  35. build_only_this_job=
  36. stages=0123456789
  37. daemon_mode=0
  38. options="$*"
  39. tasks=0
  40. while [ "$1" ] ; do
  41. case "$1" in
  42. -cfg) config=$2 ; shift ; shift ;;
  43. -stages) stages=$2 ; shift ; shift ;;
  44. -tasks) tasks=$2 ; shift ; shift ;;
  45. -job) build_only_this_job=$2 ; shift ; shift ;;
  46. -daemon) daemon_mode=1 ; shift ;;
  47. -nodaemon) daemon_mode=0 ; shift ;;
  48. *) echo "Usage: $0 [ -daemon ] [ -tasks <N> ] [ -cfg <config> ]" \
  49. "[ -stages <list> ] [ -job <stage>-<package> ]"
  50. echo
  51. echo " Compile/build all packages for a given configuration and store them as tar"
  52. echo " balls suitable for distribution."
  53. echo " This script is the ROCK work-horse. It builds in a chroot environment"
  54. echo " (stage 2..9) and goes through a number of build stages."
  55. echo " Run this command from the ROCK directory as ./scripts/Build-Target after"
  56. echo " running the ./scripts/Config and ./scripts/Download commands."
  57. echo
  58. echo " -cfg <config> the build configuration to use; defaults to 'default'"
  59. echo " -tasks <N> run <N> Build-Job daemons (for cluster builds)"
  60. echo " -daemon run as daemon in the background"
  61. echo " -stages <list> only build the stages listed in the parameter"
  62. echo " -job <stage>-<package> build only one job: the given package"
  63. echo " in the given stage"
  64. exit 1 ;;
  65. esac
  66. done
  67. if [ $tasks -gt 1 ] ; then
  68. . config/$config/config
  69. if [ "$ROCKCFG_PARALLEL" = 0 ] ; then
  70. echo_error "You have not configured this to be a cluster build!"
  71. echo_error "Please remove -tasks $tasks or configure this for cluster build"
  72. exit 1
  73. fi
  74. fi
  75. if [ "$daemon_mode" = 1 ] ; then
  76. . config/$config/config
  77. echo "Running $0 in the background (messages go to logfile only).."
  78. echo "Logfile: build/$ROCKCFG_ID/ROCK/logs/build_target.log"
  79. nohup $0 $options -nodaemon > /dev/null 2> /dev/null < /dev/null &
  80. exit 0
  81. fi
  82. . scripts/parse-config
  83. . scripts/functions
  84. # so we can disable stuff not available in Build-Target
  85. export ROCK_BUILD_TARGET=1
  86. build_root="$base/build/$ROCKCFG_ID"
  87. build_rock="$base/build/$ROCKCFG_ID/ROCK"
  88. build_logs="$build_rock/logs" ; mkdir -p "${build_logs}"
  89. build_pkgs="$build_rock/pkgs" ; mkdir -p "${build_root}"
  90. if [ "$ROCKCFG_PARANOIA_CHECK" = 1 ] ; then
  91. ./scripts/Check-System -paranoia || exit 1
  92. else
  93. ./scripts/Check-System || exit 1
  94. fi
  95. # Package Build loop - executed by build-target
  96. #
  97. pkgloop() {
  98. if [ "$ROCKCFG_NOBROKENDEPS" = 1 ]; then
  99. nobrokendeps="-nobrokendeps"
  100. else
  101. nobrokendeps=""
  102. fi
  103. if [ "$ROCKCFG_RETRY_BROKEN" -eq 1 -a \
  104. -z "$build_only_this_job" -a \
  105. "`ls ${build_root}/var/adm/logs/*.err 2> /dev/null`" ] ; then
  106. echo_header "Removing old error logs ..."
  107. for x in ${build_root}/var/adm/logs/*.err ; do
  108. echo_status "Removing ${x#$build_root/} ..."
  109. rm -f $x
  110. done
  111. fi
  112. if [ -z "$build_only_this_job" -a \
  113. "`ls ${build_root}/var/adm/logs/*.out 2> /dev/null`" ] ; then
  114. echo_header "Removing old output logs ..."
  115. for x in ${build_root}/var/adm/logs/*.out ; do
  116. echo_status "Removing ${x#$build_root/} ..."
  117. rm -f $x
  118. done
  119. fi
  120. if [ "$ROCKCFG_PARALLEL" = 1 -a -z "$build_only_this_job" ]
  121. then
  122. qdir="$base/build/$ROCKCFG_ID/ROCK/queue"
  123. mkdir -p $qdir; rm -f $qdir/*
  124. newqueue=1 ; printstatus=1 ; printstatus_counter=30
  125. finished=0 ; touch $qdir/queue.txt
  126. if [ -z "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  127. touch $qdir/use_build_job_daemon
  128. if [ $tasks = 0 ]; then
  129. echo "
  130. Job Queue for parallel (cluster) build created.
  131. You have not configured a command for adding jobs. So you need to use the
  132. ROCK Linux built-in job queue. To do so, you need to login to your build
  133. nodes and execute the command:
  134. ./scripts/Build-Job -cfg $config -daemon
  135. "
  136. else
  137. echo "
  138. Job Queue for parallel (cluster) build created. Now creating worker tasks..
  139. "
  140. for ((n=0; n<tasks; n++)); do
  141. echo -n "[$n] "
  142. ./scripts/Build-Job -cfg $config -daemon
  143. done
  144. echo "
  145. Parallel (cluster) build running...
  146. "
  147. fi
  148. else
  149. echo "
  150. Job Queue for parallel (cluster) build created.
  151. You have configured a command for adding jobs. So if everything is configured
  152. in the right way, the nodes will automatically start building the jobs.
  153. "
  154. rm -f $qdir/use_build_job_daemon
  155. fi
  156. while
  157. if [ -f $qdir/print_status ] ; then
  158. rm -f $qdir/print_status
  159. printstatus=1 ; printstatus_counter=0
  160. fi
  161. if [ "$printstatus" = 1 ] ; then
  162. if [ $printstatus_counter -le 0 ] ; then
  163. ./scripts/Create-ParaStatus -cfg "$config"
  164. printstatus=0 ; printstatus_counter=300
  165. else
  166. printstatus_counter=$(($printstatus_counter - 1))
  167. fi
  168. fi
  169. for x in $qdir/*.msg ; do
  170. if [ -f "$x" ] ; then
  171. expand -t20 < $x ; rm -f $x
  172. printstatus=1 ; newqueue=1
  173. fi
  174. done
  175. if [ "$newqueue" = 1 ] ; then
  176. ./scripts/Create-PkgQueue \
  177. -cfg $config -stages $stages \
  178. $nobrokendeps | sort -r -n -k2 | \
  179. if [ $ROCKCFG_PARALLEL_MAX -gt 0 ]
  180. then head -n $ROCKCFG_PARALLEL_MAX
  181. else cat ; fi > $qdir/queue.new
  182. mv $qdir/queue.new $qdir/queue.txt
  183. newqueue=0
  184. while read next ; do
  185. set $next ; qid="$1-$6"
  186. if [ ! -f $qdir/$qid.job ] ; then
  187. date "+%H:%M %Y-%m-%d: creating new job '$qid'" | expand -t20
  188. echo "Job $qid waiting in the job queue (priority $2)" > $qdir/$qid.todo
  189. printstatus=1
  190. # we need to remove some variables that should be uniqe per node
  191. dump_env | grep -v HOSTNAME= > $qdir/$qid.new
  192. mv $qdir/$qid.new $qdir/$qid.job
  193. if [ "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  194. x="${ROCKCFG_PARALLEL_ADDJOB//\{\}/.\/scripts\/Build-Job -cfg $config $qid}" ; sh -c "$x"
  195. fi
  196. fi
  197. done < $qdir/queue.txt
  198. fi
  199. if [ -s $qdir/queue.txt ] || \
  200. [ "`ls $qdir/*.job 2>/dev/null`" ]
  201. then
  202. finished=0
  203. else
  204. finished=$(( $finished + 1 ))
  205. newqueue=1
  206. fi
  207. [ $finished -le 3 ]
  208. do
  209. sleep 1
  210. done
  211. echo "All packages built."
  212. sleep 2; rm -rf $qdir
  213. else
  214. if [ "$build_only_this_job" ] ; then
  215. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.log"
  216. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.err"
  217. next="$( awk 'BEGIN { FS=" "; }
  218. $0 ~ /[ =]'${build_only_this_job#*-}' / && \
  219. $2 ~ /'${build_only_this_job%%-*}'/ \
  220. { $1="'${build_only_this_job%%-*}' 0";
  221. print; exit; }' < config/$config/packages )"
  222. [ "$next" ] && pkgloop_package $next
  223. exit 0
  224. else
  225. while
  226. next="`./scripts/Create-PkgQueue \
  227. -cfg "$config" -stages $stages -single $nobrokendeps`"
  228. [ "$next" ]
  229. do
  230. pkgloop_package $next
  231. done
  232. fi
  233. fi
  234. local pkglst=`mktemp` errors=0; rm -f src/invalid-files.lst
  235. echo_header "Searching for old lingering files ..."
  236. grep "^X" config/$config/packages | cut -d' ' -f5 | sed 's,.*=,,' |
  237. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then
  238. while read p; do
  239. v=$( grep -h '^Package Name and Version:' build/$ROCKCFG_ID/var/adm/packages/$p:* \
  240. build/$ROCKCFG_ID/var/adm/packages/$p 2> /dev/null | cut -f6,7 -d' ' | tr ' ' - | head -n1 )
  241. echo "$p-$v"
  242. done
  243. else
  244. cat
  245. fi > $pkglst
  246. for file in $( ls build/$ROCKCFG_ID/ROCK/pkgs/ ) ; do
  247. x="$file"
  248. [ "$x" = packages.db ] && continue
  249. [ "$x" = packages.db.md5 ] && continue
  250. [ "$x" = packages_stripped.db ] && continue
  251. [ "$x" = packages_stripped.db.md5 ] && continue
  252. [ $ROCKCFG_CREATE_GEM = 0 ] || x=${x%.gem}
  253. [ $ROCKCFG_CREATE_TARBZ2 = 0 ] || x=${x%.tar.bz2}
  254. y=$( echo $x | sed 's,:[^-]*,,' )
  255. if ! grep -qx "$y" $pkglst; then
  256. file="build/$ROCKCFG_ID/ROCK/pkgs/$file"
  257. echo_error "$file [$x $y] should not be present" \
  258. "(now in src/invalid-files.lst)!"
  259. mkdir -p src; echo "$file" >> src/invalid-files.lst
  260. errors=1
  261. fi
  262. done
  263. for dir in build/$ROCKCFG_ID/var/adm/{cache,cksums,dependencies,descs,flists,md5sums,packages} ; do
  264. for file in $( ls $dir ) ; do
  265. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="${file%:*}-.*"; else x="${file%:*}"; fi
  266. if ! grep -xq "$x" $pkglst ; then
  267. echo_error "$dir/$file should not be present (now in src/invalid-files.lst)!"
  268. mkdir -p src; echo "$dir/$file" >> src/invalid-files.lst
  269. errors=1
  270. fi
  271. done
  272. done
  273. for file in $( ls build/$ROCKCFG_ID/var/adm/logs/ ) ; do
  274. x="`echo $file | sed -e 's/^.-//' -e 's/\.log//' -e 's/\.err//' -e s'/\.out//'`"
  275. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="$x-.*"; else x="$x"; fi
  276. if ! grep -xq "$x" $pkglst ; then
  277. file="build/$ROCKCFG_ID/var/adm/logs/$file"
  278. echo_error "$file should not be present (now in src/invalid-files.lst)!"
  279. mkdir -p src; echo "$file" >> src/invalid-files.lst
  280. errors=1
  281. fi
  282. done
  283. [ $errors = 0 ] && echo_status "No errors found."
  284. rm $pkglst
  285. }
  286. # Process one line of output generated by Create-PkgQueue
  287. #
  288. pkgloop_package() {
  289. for x in stagelevel pkg_depnr pkg_stages pkg_pri pkg_tree \
  290. pkg_name pkg_ver pkg_prefix pkg_extra
  291. do eval "$x=\$1" ; shift ; done
  292. # Maybe this is a forked package
  293. pkg_basename="${pkg_name%=*}"
  294. pkg_name="${pkg_name#*=}"
  295. [ "$build_only_this_job" -a \
  296. "$stagelevel-$pkg_name" != "$build_only_this_job" ] && return
  297. [ $(expr "$pkg_stages" : ".*$stagelevel.*") -eq 0 ] && return
  298. pkg_laststage=$(echo "$pkg_stages" | sed "s,-,,g; s,.*\(.\),\1,")
  299. cmd_root="-root auto"
  300. [ $stagelevel -gt 1 ] && cmd_root="$cmd_root -chroot"
  301. if [ "$pkg_prefix" != "/" ] ; then
  302. cmd_prefix="-prefix $pkg_prefix"
  303. else cmd_prefix="" ; fi
  304. cmd_buildpkg="./scripts/Build-Pkg -$stagelevel -cfg $config -nopostinst"
  305. cmd_buildpkg="$cmd_buildpkg $cmd_root $cmd_prefix $pkg_basename=$pkg_name"
  306. # Execute action handler
  307. pkgloop_action || [ "$ROCKCFG_ABORT_ON_ERROR" != 1 ] || exit 1
  308. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err -a \
  309. "$ROCKCFG_SENDMAIL" = 1 ]
  310. then
  311. {
  312. cat << EOT
  313. Subject: [ROCK Build-Target] $stagelevel-$pkg_name in $config failed
  314. Building package $pkg_name failed in stage $stagelevel:
  315. ----
  316. EOT
  317. echo; tail -n 200 ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err; echo
  318. } | $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO
  319. fi
  320. if [ ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.log -a \
  321. ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  322. then
  323. echo_header "Package build ended abnormally!"
  324. echo_error "Usually a package build creates eighter a *.log"
  325. echo_error "or a *.err file. Neither the 1st nor the 2nd is"
  326. echo_error "there. So I'm going to create a *.err file now"
  327. echo_error "and abort the build process."
  328. touch ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err
  329. exit 1
  330. fi
  331. if [ $pkg_laststage -eq $stagelevel ] && \
  332. [ "$ROCKCFG_CREATE_TARBZ2" = 1 -o "$ROCKCFG_CREATE_GEM" = 1 ]
  333. then
  334. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  335. then echo_error "Creation of binary package isn't possible,"
  336. echo_error "because the package was not successfully"
  337. echo_error "built in (at least) the current stage."
  338. else
  339. for spkg in $( cd ${build_root}/var/adm/packages/; ls ${pkg_name} ${pkg_name}:* 2>/dev/null )
  340. do
  341. echo_header "Creating binary package file for ${spkg}."
  342. mkdir -p "${build_pkgs}"
  343. if [ "$ROCKCFG_PKGFILE_VER" = 1 ]
  344. then
  345. v="-$( grep '^Package Name and Version:' \
  346. ${build_root}/var/adm/packages/$spkg | cut -f6,7 -d' ' | tr ' ' - )"
  347. else
  348. v=""
  349. fi
  350. echo_status "Building build/.../pkgs/`
  351. `${spkg}${v}.tar.bz2"
  352. ( cd "$build_root/"
  353. cut -f2- -d' ' var/adm/flists/$spkg | \
  354. tar -cf- --no-recursion --files-from=- | bzip2
  355. ) > "${build_pkgs}/${spkg}${v}.tar.bz2.tmp"
  356. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  357. echo_status "Building build/.../pkgs/`
  358. `${spkg}${v}.gem"
  359. mine -C "$build_root/var/adm" \
  360. "${build_pkgs}/${spkg}${v}.tar.bz2.tmp" \
  361. $spkg "$build_pkgs/${spkg}${v}.gem"
  362. fi
  363. if [ "$ROCKCFG_CREATE_TARBZ2" = 1 ] ; then
  364. mv "$build_pkgs/${spkg}${v}.tar.bz2.tmp" \
  365. "$build_pkgs/${spkg}${v}.tar.bz2"
  366. else
  367. echo_status "Removing temporary tar.bz2."
  368. rm -f "$build_pkgs/${spkg}${v}.tar.bz2.tmp"
  369. fi
  370. done
  371. fi
  372. fi
  373. }
  374. # Action executed by pkgloop(). This function may be redefined
  375. # before calling pkgloop().
  376. #
  377. pkgloop_action() {
  378. $cmd_buildpkg
  379. }
  380. # Try to umount any directories mounted by Build-Pkg -chroot
  381. # if we are the last process using them.
  382. #
  383. build_target_exit() {
  384. exec 201> /dev/null
  385. exec 1>&0 2>&0
  386. echo_header "Running Build-Target cleanup procedure"
  387. echo_status "Killing lingering processes and removing parallel build queue."
  388. rm -rf $build_rock/queue
  389. fuser -k ${build_logs}/*.log &> /dev/null
  390. sleep 2
  391. echo_status "Umounting lingering mounts in build/$ROCKCFG_ID."
  392. umount -d -f $build_rock/{loop,config,download} 2> /dev/null
  393. umount -d -f -l $build_rock/{loop,config,download} 2> /dev/null
  394. umount -d -f $build_root/proc 2> /dev/null
  395. umount -d -f -l $build_root/proc 2> /dev/null
  396. echo_status "READY."
  397. echo
  398. }
  399. # must trap outside the group command
  400. trap 'build_target_exit' EXIT
  401. {
  402. ln -sf build_target_$$.log ${build_logs}/build_target.log
  403. ./scripts/Build-Tools -1 -cfg $config
  404. . ./target/$ROCKCFG_TARGET/build.sh
  405. echo_header "Finished building this target."
  406. echo_status "Going to run cleanup procedure now.."
  407. } 2>&1 201>> "${build_logs}/build_target_$$.log" | \
  408. tee -a "${build_logs}/build_target_$$.log"
  409. if [ "$ROCKCFG_SENDMAIL" = 1 ]; then
  410. $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO << EOT
  411. Subject: [ROCK Build-Target] $config finished.
  412. Finished building $config.
  413. EOT
  414. fi