* updated klickety (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / misc / vserver / install
blob2d7595ef642e1c80cc4e0c40f2d3512c90c23f8f
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.
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 ---
18 # TODO:
19 # - implement "update" mode
20 # - add "check" mode
21 # - implement $admdir outside $root
22 # - post-installation methods
23 # - add rootfs alike copy_and_parse functionality
25 # root directory defaults
26 root=./test #FIXME
27 vdirbase=/etc/vservers/.defaults/vdirbase
28 guest=
30 # files (selections) to install
31 files=
32 selection=
34 # installer defaults
35 method=install
36 installer=mine
38 # misc defaults
39 verbose=0
41 pkgdir=
42 pkgs=
44 admdir=
46 templates=
47 templatedir=./${0%/*}/templates #FIXME /etc/vserver/.distributions/t2/templates
48 patterndir=./${0%/*}/patterns #FIXME /etc/vserver/.distributions/t2/patterns
50 #############################################################################
51 usage() {
52 cat << EOF
53 vserver-guest-install [options] [packages]
55 Options:
56 Methods
57 -i,-install Install packages (default)
58 -u,-update Update packages
59 -r,-remove Remove packages
60 -c,-check Check packages
62 VServer setup
63 -g,-guest <name of guest> Name of the vserver guest
64 -R,-root <root> Root installation directory (./test)
66 Package selection
67 -t,-template <template> Template name or file
69 Miscellaneous:
70 -v Increase verbosity level
71 -pkgdir Package directory
72 -admdir Set alternate /var/adm directory
73 (a link into the install root is created)
74 EOF
77 # parse options
78 while [ "$1" ]; do
79 case "$1" in
80 -i|-install) method=install ;;
81 -u|-update) method=update ;;
82 -r|-remove) method=remove ;;
84 -g|-guest) guest=$2 ; root=$vdirbase/$2 ; shift ;;
85 -R|-root) root=$2 ; shift ;;
87 -t|-template) templates="$templates $2" ; shift ;;
89 -v) verbose=$(( $verbose + 1 )) ;;
90 -vv) verbose=$(( $verbose + 2 )) ;;
91 -vvv) verbose=$(( $verbose + 3 )) ;;
92 -pkgdir) pkgdir=$2 ; shift ;;
93 -admdir) admdir=$2 ; shift ;;
95 -*) usage ; exit ;;
97 *) pkgs="$pkgs $1" ;;
98 esac
99 shift
100 done
102 #############################################################################
103 # screen logging
104 dbg() {
105 [ $1 -le $verbose ] || return
106 msg info "$@"
109 msg() {
110 local lvl;
111 local t=$1 ; shift
112 case "$t" in
113 head) echo "=== $*" 1>&2 ;;
114 status) echo "-> $*" 1>&2 ;;
115 warn) echo "!!! $*" 1>&2 ;;
116 error) echo "*** $*" 1>&2 ;;
117 info) lvl=$1; shift ; echo "[$lvl] $*" 1>&2 ;;
118 esac
121 #############################################################################
122 # parse templates
123 parse_template() {
124 local line
126 dbg 2 " Parsing template \`$1'"
128 while read line; do
129 if [[ $line = .include* ]]; then
130 parse_template ${line/.include/}
131 else
132 echo $line
134 done < <(egrep -v '(#.*|^)$' $templatedir/$1)
137 dbg 1 "Assembling templates...."
138 pfile=`mktemp`
139 tmpfile=`mktemp`
140 for tmpl in $templates; do
141 parse_template $tmpl >> $tmpfile
142 done
143 echo $pkgs >> $tmpfile
144 sort -u < $tmpfile > $pfile
145 pkgs=" `cat $pfile` "
146 rm -f $tmpfile $pfile
148 dbg 1 "Done."
150 #############################################################################
151 # parse package.db
152 pkgdb=$pkgdir/packages.db
154 block_end=$'\004'
155 entry_end=$'\027'
157 # _pkgdb_node pkg
158 _pkgdb_node() {
159 zcat $pkgdb | egrep "^$1$" -A 10000 | grep "$block_end" -m 1 -B 10000
162 # extract_package_node <package> <dir>
163 extract_pkg_node() {
164 local state=0
165 local line
167 _pkgdb_node $1 | while read line; do
168 if [ "$line" = "$entry_end" -o "$line" = "$block_end" ]; then
169 state=$(( $state + 1 ))
170 else
171 case "$state" in
172 1) echo $line >> $2/$1.desc ;;
173 2) echo $line >> $2/$1.deps ;;
174 3) echo $line >> $2/$1.md5 ;;
175 esac
177 done
180 #############################################################################
181 # exclude pattern
183 # pattern <pkg>
184 pattern() {
185 local f pat
187 for f in default $*; do
188 [ -f $patterndir/$f ] && egrep -v "(^|#.*)$" $patterndir/$f
189 done
190 ) | while read pat; do
191 case "$installer" in
192 mine) echo -n "-x '$pat' " ;;
193 esac
194 done
197 #############################################################################
198 # find the tar file
200 # archive <package>
201 archive() {
202 local ext
203 local ver
205 for ext in tar.bz2; do # FIXME list of extensions!!!
206 if [ -f $pkgdir/$1.$ext ]; then
207 echo $pkgdir/$1.$ext
208 return
209 else
210 ver="`_pkgdb_node $1 | fgrep '[V]'`" ; ver=${ver##* }
211 if [ -f $pkgdir/$1-$ver.$ext ]; then
212 echo $pkgdir/$1-$ver.$ext
213 return
216 done
218 msg error "Package archive of \`$1' not found."
221 #############################################################################
222 # install, update, remove packages
224 msg head "Installing..."
225 case "$method" in
226 install)
227 case "$installer" in
228 mine)
229 [ $verbose -gt 2 ] && v="-v"
231 for p in $pkgs; do
232 pkg=${p/\%*/}
233 msg status " $pkg"
234 dbg 2 mine -i -R $root $( pattern $p ) $( archive $pkg )
235 eval mine -i $v -R $root $( pattern $p ) $( archive $pkg )
236 done
238 esac
240 remove)
241 case "$installer" in
242 mine)
243 for p in $pkgs; do
244 pkg=${p/\%*/}
245 echo mine -r -R $vdirbase/$guest $pkg
246 done
248 esac
251 echo "Not implemented: $method"
252 esac