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.

449 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 - 2004 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 [ "$daemon_mode" = 1 ] ; then
  68. . config/$config/config
  69. echo "Running $0 in the background (messages go to logfile only).."
  70. echo "Logfile: build/$ROCKCFG_ID/ROCK/logs/build_target.log"
  71. nohup $0 $options -nodaemon > /dev/null 2> /dev/null < /dev/null &
  72. exit 0
  73. fi
  74. . scripts/parse-config
  75. . scripts/functions
  76. # so we can disable stuff not available in Build-Target
  77. export ROCK_BUILD_TARGET=1
  78. build_root="$base/build/$ROCKCFG_ID"
  79. build_rock="$base/build/$ROCKCFG_ID/ROCK"
  80. build_logs="$build_rock/logs" ; mkdir -p "${build_logs}"
  81. build_pkgs="$build_rock/pkgs" ; mkdir -p "${build_root}"
  82. if [ "$ROCKCFG_PARANOIA_CHECK" = 1 ] ; then
  83. ./scripts/Check-System -paranoia || exit 1
  84. else
  85. ./scripts/Check-System || exit 1
  86. fi
  87. # Package Build loop - executed by build-target
  88. #
  89. pkgloop() {
  90. if [ "$ROCKCFG_NOBROKENDEPS" = 1 ]; then
  91. nobrokendeps="-nobrokendeps"
  92. else
  93. nobrokendeps=""
  94. fi
  95. if [ "$ROCKCFG_RETRY_BROKEN" -eq 1 -a \
  96. -z "$build_only_this_job" -a \
  97. "`ls ${build_root}/var/adm/logs/*.err 2> /dev/null`" ] ; then
  98. echo_header "Removing old error logs ..."
  99. for x in ${build_root}/var/adm/logs/*.err ; do
  100. echo_status "Removing ${x#$build_root/} ..."
  101. rm -f $x
  102. done
  103. fi
  104. if [ -z "$build_only_this_job" -a \
  105. "`ls ${build_root}/var/adm/logs/*.out 2> /dev/null`" ] ; then
  106. echo_header "Removing old output logs ..."
  107. for x in ${build_root}/var/adm/logs/*.out ; do
  108. echo_status "Removing ${x#$build_root/} ..."
  109. rm -f $x
  110. done
  111. fi
  112. if [ "$ROCKCFG_PARALLEL" = 1 -a -z "$build_only_this_job" ]
  113. then
  114. qdir="$base/build/$ROCKCFG_ID/ROCK/queue"
  115. mkdir -p $qdir; rm -f $qdir/*
  116. newqueue=1 ; printstatus=1 ; printstatus_counter=30
  117. finished=0 ; trap 'rm -f $qdir/queue.txt ; exit 130' SIGINT
  118. touch $qdir/queue.txt
  119. if [ -z "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  120. touch $qdir/use_build_job_daemon
  121. if [ $tasks = 0 ]; then
  122. echo "
  123. Job Queue for parallel (cluster) build created.
  124. You have not configuread a command for adding jobs. So you need to use the
  125. ROCK Linux built-in job queue. To do so, you need to login to your build
  126. nodes and execute the command:
  127. ./scripts/Build-Job -cfg $config -daemon
  128. "
  129. else
  130. echo "
  131. Job Queue for parallel (cluster) build created. Now creating worker tasks..
  132. "
  133. for ((n=0; n<tasks; n++)); do
  134. echo -n "[$n] "
  135. ./scripts/Build-Job -cfg $config -daemon
  136. done
  137. echo "
  138. Parallel (cluster) build running...
  139. "
  140. fi
  141. else
  142. echo "
  143. Job Queue for parallel (cluster) build created.
  144. You have configuread a command for adding jobs. So if everything is configured
  145. in the right way, the nodes will automatically start building the jobs.
  146. "
  147. rm -f $qdir/use_build_job_daemon
  148. fi
  149. while
  150. if [ -f $qdir/print_status ] ; then
  151. rm -f $qdir/print_status
  152. printstatus=1 ; printstatus_counter=0
  153. fi
  154. if [ "$printstatus" = 1 ] ; then
  155. if [ $printstatus_counter -le 0 ] ; then
  156. ./scripts/Create-ParaStatus -cfg "$config"
  157. printstatus=0 ; printstatus_counter=300
  158. else
  159. printstatus_counter=$(($printstatus_counter - 1))
  160. fi
  161. fi
  162. for x in $qdir/*.msg ; do
  163. if [ -f "$x" ] ; then
  164. expand -t20 < $x ; rm -f $x
  165. printstatus=1 ; newqueue=1
  166. fi
  167. done
  168. if [ "$newqueue" = 1 ] ; then
  169. ./scripts/Create-PkgQueue \
  170. -cfg $config -stages $stages \
  171. $nobrokendeps | sort -r -n -k2 | \
  172. if [ $ROCKCFG_PARALLEL_MAX -gt 0 ]
  173. then head -n $ROCKCFG_PARALLEL_MAX
  174. else cat ; fi > $qdir/queue.new
  175. mv $qdir/queue.new $qdir/queue.txt
  176. newqueue=0
  177. while read next ; do
  178. set $next ; qid="$1-$6"
  179. if [ ! -f $qdir/$qid.job ] ; then
  180. date "+%H:%M %Y-%m-%d: creating new job '$qid'" | expand -t20
  181. echo "Job $qid waiting in the job queue (priority $2)" > $qdir/$qid.todo
  182. printstatus=1
  183. # we need to remove some variables that should be uniqe per node
  184. dump_env | grep -v HOSTNAME= > $qdir/$qid.new
  185. mv $qdir/$qid.new $qdir/$qid.job
  186. if [ "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  187. x="${ROCKCFG_PARALLEL_ADDJOB//\{\}/.\/scripts\/Build-Job -cfg $config $qid}" ; sh -c "$x"
  188. fi
  189. fi
  190. done < $qdir/queue.txt
  191. fi
  192. if [ -s $qdir/queue.txt ] || \
  193. [ "`ls $qdir/*.job 2>/dev/null`" ]
  194. then
  195. finished=0
  196. else
  197. finished=$(( $finished + 1 ))
  198. newqueue=1
  199. fi
  200. [ $finished -le 3 ]
  201. do
  202. sleep 1
  203. done
  204. echo "All packages build."
  205. sleep 2 ; rm -rf $qdir ; trap - SIGINT
  206. else
  207. if [ "$build_only_this_job" ] ; then
  208. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.log"
  209. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.err"
  210. next="$( awk 'BEGIN { FS=" "; }
  211. $0 ~ /[ =]'${build_only_this_job#*-}' / && \
  212. $2 ~ /'${build_only_this_job%%-*}'/ \
  213. { $1="'${build_only_this_job%%-*}' 0";
  214. print; exit; }' < config/$config/packages )"
  215. [ "$next" ] && pkgloop_package $next
  216. exit 0
  217. else
  218. while
  219. next="`./scripts/Create-PkgQueue \
  220. -cfg "$config" -stages $stages -single $nobrokendeps`"
  221. [ "$next" ]
  222. do
  223. pkgloop_package $next
  224. done
  225. fi
  226. fi
  227. local pkglst=`mktemp` errors=0; rm -f src/invalid-files.lst
  228. echo_header "Searching for old liggering files ..."
  229. grep "^X" config/$config/packages | cut -d' ' -f5 | sed 's,.*=,,' |
  230. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then
  231. while read p; do
  232. v=$( grep -h '^Package Name and Version:' build/$ROCKCFG_ID/var/adm/packages/$p:* \
  233. build/$ROCKCFG_ID/var/adm/packages/$p 2> /dev/null | cut -f6 -d' ' | head -n1 )
  234. echo "$p-$v"
  235. done
  236. else
  237. cat
  238. fi > $pkglst
  239. for file in $( ls build/$ROCKCFG_ID/ROCK/pkgs/ ) ; do
  240. x="$file"; [ "$x" = packages.db ] && continue
  241. [ $ROCKCFG_CREATE_GEM = 0 ] || x=${x%.gem}
  242. [ $ROCKCFG_CREATE_TARBZ2 = 0 ] || x=${x%.tar.bz2}
  243. y=$( echo $x | sed 's,:[^-]*,,' )
  244. if ! grep -qx "$y" $pkglst; then
  245. file="build/$ROCKCFG_ID/ROCK/pkgs/$file"
  246. echo_error "$file [$x $y] should not be present" \
  247. "(now in src/invalid-files.lst)!"
  248. mkdir -p src; echo "$file" >> src/invalid-files.lst
  249. errors=1
  250. fi
  251. done
  252. for dir in build/$ROCKCFG_ID/var/adm/{cache,cksums,dependencies,descs,flists,md5sums,packages} ; do
  253. for file in $( ls $dir | cut -f1 -d: ) ; do
  254. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="$file-.*"; else x="$file"; fi
  255. if ! grep -xq "$x" $pkglst ; then
  256. echo_error "$dir/$file should not be present (now in src/invalid-files.lst)!"
  257. mkdir -p src; echo "$dir/$file" >> src/invalid-files.lst
  258. errors=1
  259. fi
  260. done
  261. done
  262. for file in $( ls build/$ROCKCFG_ID/var/adm/logs/ ) ; do
  263. x="`echo $file | sed -e 's/^.-//' -e 's/\.log//' -e 's/\.err//' -e s'/\.out//'`"
  264. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="$x-.*"; else x="$x"; fi
  265. if ! grep -xq "$x" $pkglst ; then
  266. file="build/$ROCKCFG_ID/var/adm/logs/$file"
  267. echo_error "$file should not be present (now in src/invalid-files.lst)!"
  268. mkdir -p src; echo "$file" >> src/invalid-files.lst
  269. errors=1
  270. fi
  271. done
  272. [ $errors = 0 ] && echo_status "No errors found."
  273. rm $pkglst
  274. }
  275. # Process one line of output generated by Create-PkgQueue
  276. #
  277. pkgloop_package() {
  278. for x in stagelevel pkg_depnr pkg_stages pkg_pri pkg_tree \
  279. pkg_name pkg_ver pkg_prefix pkg_extra
  280. do eval "$x=\$1" ; shift ; done
  281. # Maybe this is a forked package
  282. pkg_basename="${pkg_name%=*}"
  283. pkg_name="${pkg_name#*=}"
  284. [ "$build_only_this_job" -a \
  285. "$stagelevel-$pkg_name" != "$build_only_this_job" ] && return
  286. [ $(expr "$pkg_stages" : ".*$stagelevel.*") -eq 0 ] && return
  287. pkg_laststage=$(echo "$pkg_stages" | sed "s,-,,g; s,.*\(.\),\1,")
  288. cmd_root="-root auto"
  289. [ $stagelevel -gt 1 ] && cmd_root="$cmd_root -chroot"
  290. if [ "$pkg_prefix" != "/" ] ; then
  291. cmd_prefix="-prefix $pkg_prefix"
  292. else cmd_prefix="" ; fi
  293. cmd_buildpkg="./scripts/Build-Pkg -$stagelevel -cfg $config"
  294. cmd_buildpkg="$cmd_buildpkg $cmd_root $cmd_prefix $pkg_basename=$pkg_name"
  295. # Execute action handler
  296. pkgloop_action || [ "$ROCKCFG_ABORT_ON_ERROR" != 1 ] || exit 1
  297. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err -a \
  298. "$ROCKCFG_SENDMAIL" = 1 ]
  299. then
  300. {
  301. cat << EOT
  302. Subject: [ROCK Build-Target] $stagelevel-$pkg_name in $config failed
  303. Building package $pkg_name failed in stage $stagelevel:
  304. ----
  305. EOT
  306. echo; tail -n 200 ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err; echo
  307. } | $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO
  308. fi
  309. if [ ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.log -a \
  310. ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  311. then
  312. echo_header "Package build ended abnormally!"
  313. echo_error "Usually a package build creates eighter a *.log"
  314. echo_error "or a *.err file. Neither the 1st nor the 2nd is"
  315. echo_error "there. So I'm going to create a *.err file now"
  316. echo_error "and abort the build process."
  317. touch ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err
  318. exit 1
  319. fi
  320. if [ $pkg_laststage -eq $stagelevel ] && \
  321. [ "$ROCKCFG_CREATE_TARBZ2" = 1 -o "$ROCKCFG_CREATE_GEM" = 1 ]
  322. then
  323. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  324. then echo_error "Creation of binary package isn't possible,"
  325. echo_error "because the package was not successfully"
  326. echo_error "built in (at least) the current stage."
  327. else
  328. for spkg in $( cd ${build_root}/var/adm/packages/; ls ${pkg_name} ${pkg_name}:* 2>/dev/null )
  329. do
  330. echo_header "Creating binary package file for ${spkg}."
  331. mkdir -p "${build_pkgs}"
  332. if [ "$ROCKCFG_PKGFILE_VER" = 1 ]
  333. then
  334. v="-$( grep '^Package Name and Version:' \
  335. ${build_root}/var/adm/packages/$spkg | cut -f6,7 -d' ' | tr ' ' - )"
  336. else
  337. v=""
  338. fi
  339. echo_status "Building build/.../pkgs/`
  340. `${spkg}${v}.tar.bz2"
  341. ( cd "$build_root/"
  342. cut -f2- -d' ' var/adm/flists/$spkg | \
  343. tar -cf- --no-recursion --files-from=- | bzip2
  344. ) > "${build_pkgs}/${spkg}${v}.tar.bz2.tmp"
  345. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  346. echo_status "Building build/.../pkgs/`
  347. `${spkg}${v}.gem"
  348. mine -C "$build_root/var/adm" \
  349. "${build_pkgs}/${spkg}${v}.tar.bz2.tmp" \
  350. $spkg "$build_pkgs/${spkg}${v}.gem.tmp"
  351. fi
  352. if [ "$ROCKCFG_CREATE_TARBZ2" = 1 ] ; then
  353. mv "$build_pkgs/${spkg}${v}.tar.bz2.tmp" \
  354. "$build_pkgs/${spkg}${v}.tar.bz2"
  355. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  356. mv "$build_pkgs/${spkg}${v}.gem.tmp" \
  357. "$build_pkgs/${spkg}${v}.gem"
  358. fi
  359. else
  360. echo_status "Removing temporary tar.bz2."
  361. rm -f "$build_pkgs/${spkg}${v}.tar.bz2.tmp"
  362. mv "$build_pkgs/${spkg}${v}.gem.tmp" \
  363. "$build_pkgs/${spkg}${v}.gem"
  364. fi
  365. done
  366. fi
  367. fi
  368. }
  369. # Action executed by pkgloop(). This function may be redefined
  370. # before calling pkgloop().
  371. #
  372. pkgloop_action() {
  373. $cmd_buildpkg
  374. }
  375. # Try to umount any directories mounted by Build-Pkg -chroot
  376. # if we are the last process using them.
  377. #
  378. umount_chroot() {
  379. exec 201> /dev/null
  380. if [ -z "$( fuser ${build_logs}/*.log )" ]; then
  381. umount -d -f $build_rock/{loop,config,download} 2> /dev/null
  382. umount -d -f -l $build_rock/{loop,config,download} 2> /dev/null
  383. umount -d -f $build_root/proc 2> /dev/null
  384. umount -d -f -l $build_root/proc 2> /dev/null
  385. fi
  386. }
  387. # must trap outside the group command
  388. trap 'umount_chroot' EXIT
  389. {
  390. ln -sf build_target_$$.log ${build_logs}/build_target.log
  391. ./scripts/Build-Tools -1 -cfg $config
  392. . ./target/$ROCKCFG_TARGET/build.sh
  393. echo_header "Finished building this target."
  394. } 2>&1 201>> "${build_logs}/build_target_$$.log" | \
  395. tee -a "${build_logs}/build_target_$$.log"
  396. if [ "$ROCKCFG_SENDMAIL" = 1 ]; then
  397. $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO << EOT
  398. Subject: [ROCK Build-Target] $config finished.
  399. Finished building $config.
  400. EOT
  401. fi