OpenSDE Framework (without history before r20070)
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.

252 lines
5.6 KiB

  1. #!/bin/bash
  2. # --- T2-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # T2 SDE: misc/vserver/install
  6. # Copyright (C) 2006 Juergen "George" Sawinski
  7. # Copyright (C) 2006 The T2 SDE Project
  8. #
  9. # More information can be found in the files COPYING and README.
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License. A copy of the
  14. # GNU General Public License can be found in the file COPYING.
  15. # --- T2-COPYRIGHT-NOTE-END ---
  16. # TODO:
  17. # - implement "update" mode
  18. # - add "check" mode
  19. # - implement $admdir outside $root
  20. # - post-installation methods
  21. # - add rootfs alike copy_and_parse functionality
  22. # root directory defaults
  23. root=./test #FIXME
  24. vdirbase=/etc/vservers/.defaults/vdirbase
  25. guest=
  26. # files (selections) to install
  27. files=
  28. selection=
  29. # installer defaults
  30. method=install
  31. installer=mine
  32. # misc defaults
  33. verbose=0
  34. pkgdir=
  35. pkgs=
  36. admdir=
  37. templates=
  38. templatedir=./${0%/*}/templates #FIXME /etc/vserver/.distributions/t2/templates
  39. patterndir=./${0%/*}/patterns #FIXME /etc/vserver/.distributions/t2/patterns
  40. #############################################################################
  41. usage() {
  42. cat << EOF
  43. vserver-guest-install [options] [packages]
  44. Options:
  45. Methods
  46. -i,-install Install packages (default)
  47. -u,-update Update packages
  48. -r,-remove Remove packages
  49. -c,-check Check packages
  50. VServer setup
  51. -g,-guest <name of guest> Name of the vserver guest
  52. -R,-root <root> Root installation directory (./test)
  53. Package selection
  54. -t,-template <template> Template name or file
  55. Miscellaneous:
  56. -v Increase verbosity level
  57. -pkgdir Package directory
  58. -admdir Set alternate /var/adm directory
  59. (a link into the install root is created)
  60. EOF
  61. }
  62. # parse options
  63. while [ "$1" ]; do
  64. case "$1" in
  65. -i|-install) method=install ;;
  66. -u|-update) method=update ;;
  67. -r|-remove) method=remove ;;
  68. -g|-guest) guest=$2 ; root=$vdirbase/$2 ; shift ;;
  69. -R|-root) root=$2 ; shift ;;
  70. -t|-template) templates="$templates $2" ; shift ;;
  71. -v) verbose=$(( $verbose + 1 )) ;;
  72. -vv) verbose=$(( $verbose + 2 )) ;;
  73. -vvv) verbose=$(( $verbose + 3 )) ;;
  74. -pkgdir) pkgdir=$2 ; shift ;;
  75. -admdir) admdir=$2 ; shift ;;
  76. -*) usage ; exit ;;
  77. *) pkgs="$pkgs $1" ;;
  78. esac
  79. shift
  80. done
  81. #############################################################################
  82. # screen logging
  83. dbg() {
  84. [ $1 -le $verbose ] || return
  85. msg info "$@"
  86. }
  87. msg() {
  88. local lvl;
  89. local t=$1 ; shift
  90. case "$t" in
  91. head) echo "=== $*" 1>&2 ;;
  92. status) echo "-> $*" 1>&2 ;;
  93. warn) echo "!!! $*" 1>&2 ;;
  94. error) echo "*** $*" 1>&2 ;;
  95. info) lvl=$1; shift ; echo "[$lvl] $*" 1>&2 ;;
  96. esac
  97. }
  98. #############################################################################
  99. # parse templates
  100. parse_template() {
  101. local line
  102. dbg 2 " Parsing template \`$1'"
  103. while read line; do
  104. if [[ $line = .include* ]]; then
  105. parse_template ${line/.include/}
  106. else
  107. echo $line
  108. fi
  109. done < <(egrep -v '(#.*|^)$' $templatedir/$1)
  110. }
  111. dbg 1 "Assembling templates...."
  112. pfile=`mktemp`
  113. tmpfile=`mktemp`
  114. for tmpl in $templates; do
  115. parse_template $tmpl >> $tmpfile
  116. done
  117. echo $pkgs >> $tmpfile
  118. sort -u < $tmpfile > $pfile
  119. pkgs=" `cat $pfile` "
  120. rm -f $tmpfile $pfile
  121. dbg 1 "Done."
  122. #############################################################################
  123. # parse package.db
  124. pkgdb=$pkgdir/packages.db
  125. block_end=$'\004'
  126. entry_end=$'\027'
  127. # _pkgdb_node pkg
  128. _pkgdb_node() {
  129. zcat $pkgdb | egrep "^$1$" -A 10000 | grep "$block_end" -m 1 -B 10000
  130. }
  131. # extract_package_node <package> <dir>
  132. extract_pkg_node() {
  133. local state=0
  134. local line
  135. _pkgdb_node $1 | while read line; do
  136. if [ "$line" = "$entry_end" -o "$line" = "$block_end" ]; then
  137. state=$(( $state + 1 ))
  138. else
  139. case "$state" in
  140. 1) echo $line >> $2/$1.desc ;;
  141. 2) echo $line >> $2/$1.deps ;;
  142. 3) echo $line >> $2/$1.md5 ;;
  143. esac
  144. fi
  145. done
  146. }
  147. #############################################################################
  148. # exclude pattern
  149. # pattern <pkg>
  150. pattern() {
  151. local f pat
  152. (
  153. for f in default $*; do
  154. [ -f $patterndir/$f ] && egrep -v "(^|#.*)$" $patterndir/$f
  155. done
  156. ) | while read pat; do
  157. case "$installer" in
  158. mine) echo -n "-x '$pat' " ;;
  159. esac
  160. done
  161. }
  162. #############################################################################
  163. # find the tar file
  164. # archive <package>
  165. archive() {
  166. local ext
  167. local ver
  168. for ext in tar.bz2; do # FIXME list of extensions!!!
  169. if [ -f $pkgdir/$1.$ext ]; then
  170. echo $pkgdir/$1.$ext
  171. return
  172. else
  173. ver="`_pkgdb_node $1 | fgrep '[V]'`" ; ver=${ver##* }
  174. if [ -f $pkgdir/$1-$ver.$ext ]; then
  175. echo $pkgdir/$1-$ver.$ext
  176. return
  177. fi
  178. fi
  179. done
  180. msg error "Package archive of \`$1' not found."
  181. }
  182. #############################################################################
  183. # install, update, remove packages
  184. msg head "Installing..."
  185. case "$method" in
  186. install)
  187. case "$installer" in
  188. mine)
  189. [ $verbose -gt 2 ] && v="-v"
  190. for p in $pkgs; do
  191. pkg=${p/\%*/}
  192. msg status " $pkg"
  193. dbg 2 mine -i -R $root $( pattern $p ) $( archive $pkg )
  194. eval mine -i $v -R $root $( pattern $p ) $( archive $pkg )
  195. done
  196. ;;
  197. esac
  198. ;;
  199. remove)
  200. case "$installer" in
  201. mine)
  202. for p in $pkgs; do
  203. pkg=${p/\%*/}
  204. echo mine -r -R $vdirbase/$guest $pkg
  205. done
  206. ;;
  207. esac
  208. ;;
  209. *)
  210. echo "Not implemented: $method"
  211. esac