Debian Codegraph @

bin/fai-class

raw symbols SCIP
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-class - determine all classes a host belongs to
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2002-2022 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 # import variables: $LOGDIR $verbose $debug
30 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31 verbosemsg() {
32
33 # a very nice subroutine
34 # write message if the verbose flag is set
35 [ X$verbose = X1 ] && echo "$*"
36 }
37 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38 debugmsg() {
39
40 # a very nice subroutine
41 # write message if the debug flag is set
42 [ "$debug" ] && echo "$0: $*"
43 }
44 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45 addclass() {
46
47 # append classes to a file
48 while read line ; do
49 case $line in
50 \#*) ;; # strip comments
51 *) debugmsg "Adding class $line"
52 for class in $line ; do
53 echo $class >> $filename
54 done
55 esac
56 done
57 }
58 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59 fc_check_status() {
60
61 cmd=$1
62 st=$2
63
64 if [ $st -eq 0 ]; then
65 res="OK."
66 else
67 res="FAILED with exit code $st."
68 err=1
69 fi
70 # put result in the log file and write to stdout
71 printf "%-20s $res\n" $cmd | tee -a $LOGDIR/status.log
72 }
73 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74 usage() {
75
76 ex=$1
77 cat <<-EOF
78 fai-class Copyright (C) 2002-2022 Thomas Lange
79
80 Usage: fai-class [OPTION] DIRECTORY CLASSFILE
81 Define classes using files and scripts in DIRECTORY
82
83 Executes scripts in DIRECTORY starting with two digits.
84 The standard output of these scripts are names of classes which
85 are written to CLASSFILE.
86 EOF
87 exit $ex
88 }
89 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90 testclass() {
91
92 # test if some classes are define multiple times
93 duplicate=$(sort $filename | uniq -dc)
94 if [ -n "$duplicate" ]; then
95 echo "$0: WARNING. Following classes are defined multiple times: $duplicate" >&2
96 exit 2
97 fi
98 }
99 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100 setup() {
101
102 # parse options, test and set some basic variables
103
104 while getopts "dhvt:T" opt ; do
105 case "$opt" in
106 d) debug=1 ;;
107 v) verbose=1 ;;
108 h) usage 0 ;;
109 T) ctest=1 ;;
110 t) LOGDIR=$OPTARG; export LOGDIR ;;
111 *) usage 1 ;;
112 esac
113 done
114 shift $((OPTIND - 1))
115 [ -z "$2" ] || [ -n "$3" ] && usage 1
116
117 classdir=$1
118 filename=$2
119 cd $classdir || {
120 echo "$0: Can't change dir to $classdir."
121 exit 99
122 }
123 if [ -s $filename ]; then
124 verbosemsg "$filename exists. Renaming to $filename.bak"
125 mv $filename $filename.bak
126 fi
127 if [ -z "$LOGDIR" ]; then
128 verbosemsg "Setting LOGDIR to default value /tmp/fai"
129 LOGDIR=/tmp/fai; export LOGDIR
130 fi
131 }
132 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133 # main program
134
135 PATH=.:$PATH # so scripts in /fai/class are found
136 export PATH
137
138 err=0
139 setup "$@"
140 verbosemsg "fai-class: Defining classes."
141
142 echo DEFAULT | addclass
143
144 # define classes by executing scripts
145 # alphabetical sort is important
146 if [ "$debug" ]; then
147 scripts=$(echo [0-9][0-9]*)
148 else
149 scripts=$(echo [0-9][0-9]* 2>/dev/null)
150 fi
151 debugmsg "Scripts found: $scripts"
152
153 for f in $scripts ; do
154 [ -f $f ] || continue # skip sockets, pipes, symlinks
155 debugmsg "File $f found."
156 if [ ! -x $f ]; then
157 echo "Warning: File $f is not executable, so it's not used for defining classes." >&2
158 continue
159 fi
160 classes=$(< $filename)
161 export classes
162 case $f in
163 *~|*.bak) debugmsg "Skipping backup file $f" ;;
164 *.sh)
165 verbosemsg "Executing $classdir/$f."
166 # this script can define $newclasses
167 newclasses=
168 . $f
169 fc_check_status $f $?
170 echo $newclasses | addclass ;;
171 *.source)
172 echo "ERROR: The .source suffix is deprecated. Use .sh instead." ;;
173 *)
174 verbosemsg "Executing $classdir/$f."
175 classes=`./$f`
176 fc_check_status $f $?
177 if [ -f $LOGDIR/task_local_error ]; then
178 lerr=$(< $LOGDIR/task_local_error)
179 task_error $lerr
180 fi
181 echo $classes | addclass
182 ;;
183 esac
184 done
185
186 # $LOGDIR should not be writable by everybody
187 # scripts can also write additional classes to a file, if they
188 # can't print them to stdout. Define these classes.
189
190 if [ -f $LOGDIR/additional-classes ]; then
191 cat $LOGDIR/additional-classes | addclass
192 if [ "$debug" ]; then
193 mv $LOGDIR/additional-classes $LOGDIR/additional-classes.used
194 else
195 # remove the file after it was used. Do not use the same file more than once.
196 rm -f $LOGDIR/additional-classes
197 fi
198 fi
199 if [ -z "$HOSTNAME" ]; then
200 read HOSTNAME < /proc/sys/kernel/hostname
201 fi
202
203 # add all classes which are listed in a file with the hostname
204 if [ -f "$HOSTNAME" ]; then
205 verbosemsg "Using classes from file $classdir/$HOSTNAME"
206 grep -v "^#" $HOSTNAME | addclass
207 fi
208
209 echo ${ADDCLASSES//,/ } | addclass
210
211 # now add the hostname (the only class in lowercase) and LAST to
212 # the list of classes
213 echo $HOSTNAME LAST | addclass
214
215 # show all classes if verbose
216 debugmsg List of all classes: $(< $filename)
217 [ "$ctest" ] && testclass
218 exit $err