Advertisement
pattonme

create-links.sh

Sep 1st, 2012
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.16 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # The script assumes $CWD=='$DOCROOT/pub' as basis for links.
  4. # Prep the environment with:
  5. #   $DOCROOT/pub/kickstart -> /var/satellite/rhn/kickstart
  6. #   $DOCROOT/pub/channels -> /var/cache/rhn/repodata
  7. #
  8. # 'ks-' denotes kickstart (aka static) repository
  9. #
  10. # Example REPO file:
  11. #
  12. #[ks-rhel-x86_64-server-5.9]
  13. #name=RHEL5.9 - Server (x86_64)
  14. #baseurl=file:///var/satellite/rhn/kickstart/ks-rhel-x86_64-server-5-5.9/Server/
  15. #   http://<satellite>/ks/dist/ks-rhel-x86_64-server-5-5.9/Server/
  16. #   http://<satellite>/pub/kickstart/ks-rhel-x86_64-server-5-5.9/Server/
  17. #enabled=0
  18. #gpgkey=http://<satellite>/pub/RPM-GPG-KEY-redhat-release
  19. #
  20. #[rhel-x86_64-server-6]
  21. #name=RHEL6.x - Server (x86_64)
  22. #baseurl=file:///var/cache/rhn/repodata/rhel-x86_64-server-6/
  23. #   http://<satellite>/pub/channels/rhel-x86_64-server-6/
  24. #enabled=0
  25. #gpgkey=http://<satellite>/pub/RPM-GPG-KEY-redhat-release
  26.  
  27.  
  28.  
  29. # where Satellite keeps imported RPMs
  30. : ${SATDIR:=/var/satellite/redhat}
  31.  
  32. # must be fully-qualified path
  33. : ${REPOFILE:=/var/www/html/pub/satellite.repo}
  34.  
  35. # 'getPackage' path is hardcoded into repo metadata like so:
  36. # http://<satellite>/ks/dist/<channel>/.../getPackage/<rpm>
  37. PKGDIR=getPackage
  38.  
  39. pkg_cache=/tmp/create-links.dat
  40.  
  41. if [ -f $pkg_cache ]; then
  42.     test $pkg_cache -nt `ls -td /var/cache/rhn/reposync/* | head -1` || \
  43.     rm $pkg_cache
  44. else
  45.     rm -f $pkg_cache 2>/dev/null
  46. fi
  47.  
  48. # scan once to create list
  49. echo "creating package cache ..."
  50. test -s $pkg_cache || {
  51.     t=`mktemp`
  52.     trap "/bin/rm $t 2>/dev/null" INT KILL TERM EXIT
  53.     find $SATDIR -type f > $t
  54.     mv $t $pkg_cache
  55.   }
  56.  
  57. declare -A repos
  58. eval repos=(`awk '/^\[.*\]$/ { printf "%s=", $0; } /^baseurl=file:/ { sub(/.+:\/\//, "", $0); print $0; }' $REPOFILE`)
  59.  
  60. for ch in ${!repos[@]}; do
  61.   chp=${repos[$ch]}
  62.  
  63.   echo "processing '$ch' ..."
  64.   if [ "${chp:0:1}" != "/" ]; then
  65.     echo "  ERROR: couldn't parse file-based 'baseurl'"
  66.     continue
  67.   fi
  68.   if [ ! -d $chp -o ! -s $chp/repomd.xml ]; then
  69.     echo "  ERROR: baseurl path '$chp' is not invalid"
  70.     continue
  71.   fi
  72.  
  73.   ( cd $chp
  74.     [ -e .CREATE_SKIP -o -e .CREATE_DONE ] && continue
  75.  
  76.     if [ ! -d repodata ]; then
  77.     echo "  INFO: creating missing 'repodata' directory"
  78.     ln -s . repodata
  79.     fi
  80.     if [ ! -d $PKGDIR ]; then
  81.     echo "  INFO: creating package target directory '$PKGDIR'"
  82.     mkdir $PKGDIR
  83.     fi
  84.  
  85.     for rpm in `repoquery -c $REPOFILE --tempcache --repoid=$ch \
  86.             --qf='%{name}-%{version}-%{release}.%{arch}.rpm' \
  87.             --all --show-dupes 2>/dev/null`; do
  88.  
  89.     link=`grep -m 1 "${rpm}$" $pkg_cache`
  90.     if [ -z "$link" ]; then     # was 'reposync' run?
  91.         echo "  ERROR: package '$rpm' not found in repository"
  92.         continue
  93.     fi
  94.  
  95.     if [ -e "$PKGDIR/$rpm" ]; then
  96.         if [ ! -h "$PKGDIR/$rpm" ]; then
  97.         # remove non-symlink (eg. dedup populated repository)
  98.         echo "  WARN: removing non-symlink '$rpm'"
  99.         rm -f $PKGDIR/$rpm
  100.         elif ! stat -L $PKGDIR/$rpm &>/dev/null ; then
  101.         echo "  WARN: removing broken symlink '$rpm'"
  102.         rm -f $PKGDIR/$rpm
  103.         fi
  104.     fi
  105.  
  106.     if [ ! -e "$PKGDIR/$rpm" ]; then
  107.         ln -s "$link" "$PKGDIR/$rpm" || \
  108.         echo "  ERROR: create link to '$rpm' failed"
  109.     fi
  110.     done
  111.     [ "ks-" = "${ch:0:3}" ] && touch .CREATE_DONE
  112.   )
  113. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement