| 1 |
#! /bin/sh
|
| 2 |
|
| 3 |
# $Id$
|
| 4 |
#*********************************************************************
|
| 5 |
#
|
| 6 |
# fai-class - determine all classes a host belongs to
|
| 7 |
#
|
| 8 |
# This script is part of FAI (Fully Automatic Installation)
|
| 9 |
# (c) 2002 by Thomas Lange, lange@informatik.uni-koeln.de
|
| 10 |
# Universitaet zu Koeln
|
| 11 |
#
|
| 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.
|
| 17 |
#
|
| 18 |
# This program is distributed in the hope that it will be useful, but
|
| 19 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 20 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 21 |
# General Public License for more details.
|
| 22 |
#
|
| 23 |
# A copy of the GNU General Public License is available as
|
| 24 |
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
|
| 25 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
|
| 26 |
# can also obtain it by writing to the Free Software Foundation, Inc.,
|
| 27 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 28 |
#*********************************************************************
|
| 29 |
|
| 30 |
version="version 1.0.3, 07-dec-2002"
|
| 31 |
|
| 32 |
# import variables: $LOGDIR $verbose $debug
|
| 33 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 34 |
verbosemsg() {
|
| 35 |
|
| 36 |
# a very nice subroutine
|
| 37 |
# write message if the verbose flag is set
|
| 38 |
[ "$verbose" ] && echo "$0: $@"
|
| 39 |
}
|
| 40 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 41 |
debugmsg() {
|
| 42 |
|
| 43 |
# a very nice subroutine
|
| 44 |
# write message if the debug flag is set
|
| 45 |
[ "$debug" ] && echo "$0: $@"
|
| 46 |
}
|
| 47 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 48 |
addclass() {
|
| 49 |
|
| 50 |
# append classes to a file
|
| 51 |
while read line ; do
|
| 52 |
case $line in
|
| 53 |
\#*) ;; # strip comments
|
| 54 |
*) debugmsg "Adding class $line"
|
| 55 |
for class in $line ; do
|
| 56 |
echo $class >> $filename
|
| 57 |
done
|
| 58 |
esac
|
| 59 |
done
|
| 60 |
}
|
| 61 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 62 |
usage() {
|
| 63 |
|
| 64 |
ex=$1
|
| 65 |
cat <<-EOF
|
| 66 |
fai-class $version. Copyright (C) 2002 Thomas Lange
|
| 67 |
Report bugs to <fai@informatik.uni-koeln.de>.
|
| 68 |
|
| 69 |
Usage: fai-class [OPTION] DIRECTORY CLASSFILE
|
| 70 |
Define classes using files and scripts in DIRECTORY
|
| 71 |
|
| 72 |
Executes scripts in DIRECTORY starting with two digits.
|
| 73 |
The standard output of these scripts are names of classes which
|
| 74 |
are written to CLASSFILE.
|
| 75 |
EOF
|
| 76 |
exit $ex
|
| 77 |
}
|
| 78 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 79 |
testclass() {
|
| 80 |
|
| 81 |
# test if some classes are define multiple times
|
| 82 |
duplicate=`sort $classfile | uniq -dc`
|
| 83 |
if [ -n "$duplicate" ]; then
|
| 84 |
echo "$0 Following classes are defined multiple times: $duplicate"
|
| 85 |
exit 2
|
| 86 |
fi
|
| 87 |
}
|
| 88 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 89 |
fsetup() {
|
| 90 |
|
| 91 |
# parse options, test and set some basic variables
|
| 92 |
|
| 93 |
while getopts "dhvt:T" opt ; do
|
| 94 |
case "$opt" in
|
| 95 |
d) debug=1 ;;
|
| 96 |
v) verbose=1 ;;
|
| 97 |
h) usage 0 ;;
|
| 98 |
T) ctest=1 ;;
|
| 99 |
t) LOGDIR=$OPTARG; export LOGDIR ;;
|
| 100 |
*) usage 1 ;;
|
| 101 |
esac
|
| 102 |
done
|
| 103 |
shift `expr $OPTIND - 1`
|
| 104 |
[ -z "$2" ] || [ -n "$3" ] && usage 1
|
| 105 |
|
| 106 |
classdir=$1
|
| 107 |
filename=$2
|
| 108 |
if [ ! -d $classdir ]; then
|
| 109 |
echo "$0: $classdir is not a directory."
|
| 110 |
exit 99
|
| 111 |
fi
|
| 112 |
if [ -s $filename ]; then
|
| 113 |
verbosemsg "$filename exists. Renaming to $filename.bak"
|
| 114 |
mv $filename $filename.bak
|
| 115 |
fi
|
| 116 |
if [ -z "$LOGDIR" ]; then
|
| 117 |
verbosemsg "Setting LOGDIR to default value /tmp/fai"
|
| 118 |
LOGDIR=/tmp/fai; export LOGDIR
|
| 119 |
fi
|
| 120 |
HOSTNAME=`uname -n`; export HOSTNAME
|
| 121 |
}
|
| 122 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 123 |
# main program
|
| 124 |
fsetup "$@"
|
| 125 |
verbosemsg "Defining classes."
|
| 126 |
|
| 127 |
echo DEFAULT | addclass
|
| 128 |
|
| 129 |
# alphabetical sort is important
|
| 130 |
if [ "$debug" ]; then
|
| 131 |
scripts=`cd $classdir ; ls [0-9][0-9]*`
|
| 132 |
else
|
| 133 |
scripts=`cd $classdir ; ls [0-9][0-9]* 2>/dev/null`
|
| 134 |
fi
|
| 135 |
debugmsg "Scripts found: $scripts"
|
| 136 |
|
| 137 |
for f in $scripts ; do
|
| 138 |
[ -f $classdir/$f ] || continue # skip sockets, pipes, symlinks
|
| 139 |
debugmsg "File $f found."
|
| 140 |
if [ ! -x $classdir/$f ]; then
|
| 141 |
echo "File $f is not executable, so it's not used for defining classes."
|
| 142 |
continue
|
| 143 |
fi
|
| 144 |
verbosemsg "Executing $classdir/$f."
|
| 145 |
case $f in
|
| 146 |
# *.pl) perl $f </dev/null | addclass ;;
|
| 147 |
# *.sh) sh $f </dev/null | addclass ;;
|
| 148 |
*.source)
|
| 149 |
# this script can define $newclasses
|
| 150 |
newclasses=
|
| 151 |
. $classdir/$f
|
| 152 |
echo $newclasses | addclass ;;
|
| 153 |
*~|*.bak) echo "Skipping backup file $f" ;;
|
| 154 |
*) $classdir/$f </dev/null | addclass ;;
|
| 155 |
esac
|
| 156 |
done
|
| 157 |
|
| 158 |
# add all classes which are listed in a file with the hostname
|
| 159 |
if [ -f "$HOSTNAME" ]; then
|
| 160 |
verbosemsg "Using classes from file $HOSTNAME"
|
| 161 |
cat $HOSTNAME | addclass
|
| 162 |
fi
|
| 163 |
|
| 164 |
# $LOGDIR should not be writable by everybody
|
| 165 |
# scripts can also write additional classes to a file, if they
|
| 166 |
# can't print them to stdout. Define these classes.
|
| 167 |
|
| 168 |
if [ -f $LOGDIR/additional-classes ]; then
|
| 169 |
cat $LOGDIR/additional-classes | addclass
|
| 170 |
rm -f $LOGDIR/additional-classes
|
| 171 |
# remove the file after it was used. Do not use the same file more than once.
|
| 172 |
fi
|
| 173 |
|
| 174 |
# now add the hostname (the only class in lowercase) and LAST to
|
| 175 |
# the list of classes
|
| 176 |
echo $HOSTNAME LAST | addclass
|
| 177 |
|
| 178 |
# show all classes if verbose
|
| 179 |
verbosemsg List of all classes: `cat $filename`
|
| 180 |
[ "$ctest" ] && testclass
|
| 181 |
exit 0
|