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.

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