| 1 | #! /bin/bashscip-shell filesystem . . `/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 | verbosemsgscip-shell shell fai 6.6 verbosemsg().() { |
| 32 | |
| 33 | # a very nice subroutine |
| 34 | # write message if the verbose flag is set |
| 35 | [scip-shell system . . `[`. X$verbose = X1 ] && echoscip-shell system . . echo. "$*" |
| 36 | } |
| 37 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 38 | debugmsgscip-shell shell fai 6.6 debugmsg().() { |
| 39 | |
| 40 | # a very nice subroutine |
| 41 | # write message if the debug flag is set |
| 42 | [scip-shell system . . `[`. "$debug" ] && echoscip-shell system . . echo. "$0: $*" |
| 43 | } |
| 44 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 45 | addclassscip-shell shell fai 6.6 addclass().() { |
| 46 | |
| 47 | # append classes to a file |
| 48 | while read line ; do |
| 49 | case $line in |
| 50 | \#*) ;; # strip comments |
| 51 | *) debugmsgscip-shell shell fai 6.6 debugmsg(). "Adding class $line" |
| 52 | for class in $line ; do |
| 53 | echoscip-shell system . . echo. $class >> $filename |
| 54 | done |
| 55 | esac |
| 56 | done |
| 57 | } |
| 58 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 59 | fc_check_statusscip-shell shell fai 6.6 fc_check_status().() { |
| 60 | |
| 61 | cmd=$1 |
| 62 | st=$2 |
| 63 | |
| 64 | if [scip-shell system . . `[`. $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 | printfscip-shell system . . printf. "%-20s $res\n" $cmd | teescip-shell system . . tee. -a $LOGDIR/status.log |
| 72 | } |
| 73 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 74 | usagescip-shell shell fai 6.6 usage().() { |
| 75 | |
| 76 | ex=$1 |
| 77 | catscip-shell system . . 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 | testclassscip-shell shell fai 6.6 testclass().() { |
| 91 | |
| 92 | # test if some classes are define multiple times |
| 93 | duplicate=$(sortscip-shell system . . sort. $filename | uniqscip-shell system . . uniq. -dc) |
| 94 | if [scip-shell system . . `[`. -n "$duplicate" ]; then |
| 95 | echoscip-shell system . . echo. "$0: WARNING. Following classes are defined multiple times: $duplicate" >&2 |
| 96 | exit 2 |
| 97 | fi |
| 98 | } |
| 99 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 100 | setupscip-shell shell fai 6.6 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) usagescip-shell shell fai 6.6 usage(). 0 ;; |
| 109 | T) ctest=1 ;; |
| 110 | t) LOGDIR=$OPTARG; export LOGDIR ;; |
| 111 | *) usagescip-shell shell fai 6.6 usage(). 1 ;; |
| 112 | esac |
| 113 | done |
| 114 | shift $((OPTIND - 1)) |
| 115 | [scip-shell system . . `[`. -z "$2" ] || [scip-shell system . . `[`. -n "$3" ] && usagescip-shell shell fai 6.6 usage(). 1 |
| 116 | |
| 117 | classdir=$1 |
| 118 | filename=$2 |
| 119 | cd $classdir || { |
| 120 | echoscip-shell system . . echo. "$0: Can't change dir to $classdir." |
| 121 | exit 99 |
| 122 | } |
| 123 | if [scip-shell system . . `[`. -s $filename ]; then |
| 124 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "$filename exists. Renaming to $filename.bak" |
| 125 | mvscip-shell system . . mv. $filename $filename.bak |
| 126 | fi |
| 127 | if [scip-shell system . . `[`. -z "$LOGDIR" ]; then |
| 128 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "Setting LOGDIR to default value /tmp/fai" |
| 129 | LOGDIR=/tmp/faiscip-shell filesystem . . `/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 | setupscip-shell shell fai 6.6 setup(). "$@" |
| 140 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "fai-class: Defining classes." |
| 141 | |
| 142 | echoscip-shell system . . echo. DEFAULT | addclassscip-shell shell fai 6.6 addclass(). |
| 143 | |
| 144 | # define classes by executing scripts |
| 145 | # alphabetical sort is important |
| 146 | if [scip-shell system . . `[`. "$debug" ]; then |
| 147 | scripts=$(echoscip-shell system . . echo. [0-9][0-9]*) |
| 148 | else |
| 149 | scripts=$(echoscip-shell system . . echo. [0-9][0-9]* 2>/dev/nullscip-shell filesystem . . `/dev/null`.) |
| 150 | fi |
| 151 | debugmsgscip-shell shell fai 6.6 debugmsg(). "Scripts found: $scripts" |
| 152 | |
| 153 | for f in $scripts ; do |
| 154 | [scip-shell system . . `[`. -f $f ] || continue # skip sockets, pipes, symlinks |
| 155 | debugmsgscip-shell shell fai 6.6 debugmsg(). "File $f found." |
| 156 | if [scip-shell system . . `[`. ! -x $f ]; then |
| 157 | echoscip-shell system . . 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) debugmsgscip-shell shell fai 6.6 debugmsg(). "Skipping backup file $f" ;; |
| 164 | *.sh) |
| 165 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "Executing $classdir/$f." |
| 166 | # this script can define $newclasses |
| 167 | newclasses= |
| 168 | . $f |
| 169 | fc_check_statusscip-shell shell fai 6.6 fc_check_status(). $f $? |
| 170 | echoscip-shell system . . echo. $newclasses | addclassscip-shell shell fai 6.6 addclass(). ;; |
| 171 | *.source) |
| 172 | echoscip-shell system . . echo. "ERROR: The .source suffix is deprecated. Use .sh instead." ;; |
| 173 | *) |
| 174 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "Executing $classdir/$f." |
| 175 | classes=`./$f` |
| 176 | fc_check_statusscip-shell shell fai 6.6 fc_check_status(). $f $? |
| 177 | if [scip-shell system . . `[`. -f $LOGDIR/task_local_error ]; then |
| 178 | lerr=$(< $LOGDIR/task_local_error) |
| 179 | task_error $lerr |
| 180 | fi |
| 181 | echoscip-shell system . . echo. $classes | addclassscip-shell shell fai 6.6 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 [scip-shell system . . `[`. -f $LOGDIR/additional-classes ]; then |
| 191 | catscip-shell system . . cat. $LOGDIR/additional-classes | addclassscip-shell shell fai 6.6 addclass(). |
| 192 | if [scip-shell system . . `[`. "$debug" ]; then |
| 193 | mvscip-shell system . . 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 | rmscip-shell system . . rm. -f $LOGDIR/additional-classes |
| 197 | fi |
| 198 | fi |
| 199 | if [scip-shell system . . `[`. -z "$HOSTNAME" ]; then |
| 200 | read HOSTNAME < /proc/sys/kernel/hostnamescip-shell filesystem . . `/proc/sys/kernel/hostname`. |
| 201 | fi |
| 202 | |
| 203 | # add all classes which are listed in a file with the hostname |
| 204 | if [scip-shell system . . `[`. -f "$HOSTNAME" ]; then |
| 205 | verbosemsgscip-shell shell fai 6.6 verbosemsg(). "Using classes from file $classdir/$HOSTNAME" |
| 206 | grepscip-shell system . . grep. -v "^#" $HOSTNAME | addclassscip-shell shell fai 6.6 addclass(). |
| 207 | fi |
| 208 | |
| 209 | echoscip-shell system . . echo. ${ADDCLASSES//,/ } | addclassscip-shell shell fai 6.6 addclass(). |
| 210 | |
| 211 | # now add the hostname (the only class in lowercase) and LAST to |
| 212 | # the list of classes |
| 213 | echoscip-shell system . . echo. $HOSTNAME LAST | addclassscip-shell shell fai 6.6 addclass(). |
| 214 | |
| 215 | # show all classes if verbose |
| 216 | debugmsgscip-shell shell fai 6.6 debugmsg(). List of all classes: $(< $filename) |
| 217 | [scip-shell system . . `[`. "$ctest" ] && testclassscip-shell shell fai 6.6 testclass(). |
| 218 | exit $err |