| 1 |
#! /bin/bash
|
| 2 |
|
| 3 |
#*********************************************************************
|
| 4 |
#
|
| 5 |
# fai-do-scripts - call configuration scripts for every defined class
|
| 6 |
#
|
| 7 |
# This script is part of FAI (Fully Automatic Installation)
|
| 8 |
# (c) 2003-2006 by Thomas Lange, lange@informatik.uni-koeln.de
|
| 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 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 27 |
#*********************************************************************
|
| 28 |
|
| 29 |
version="version 1.5.1, 2-aug-2006"
|
| 30 |
|
| 31 |
# variables needed: $classes, $cfclasses, $LOGDIR
|
| 32 |
#
|
| 33 |
# And many other variables like:
|
| 34 |
|
| 35 |
# execute all scripts that match the name of a class.
|
| 36 |
# If class is a directory, execute all $class/[0-9][0-9]* scripts in
|
| 37 |
# it, but do not execute files ending in ~
|
| 38 |
|
| 39 |
# TODO: -n only shows which scripts should be executed, but do not execute them
|
| 40 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 41 |
fc_check_status() {
|
| 42 |
|
| 43 |
local cmd st res
|
| 44 |
cmd=$1
|
| 45 |
st=$2
|
| 46 |
|
| 47 |
if [ $st -eq 0 ]; then
|
| 48 |
res="OK."
|
| 49 |
else
|
| 50 |
res="FAILED with exit code $st."
|
| 51 |
fi
|
| 52 |
# put result in the log file and write to stdout
|
| 53 |
printf "%-20s $res\n" $cmd | tee -a $LOGDIR/status.log
|
| 54 |
}
|
| 55 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 56 |
call_conf() {
|
| 57 |
|
| 58 |
local class f
|
| 59 |
cd $1
|
| 60 |
for class in $classes ; do
|
| 61 |
[ -x $class -a -f $class ] && do_script $class
|
| 62 |
if [ -d $class ]; then
|
| 63 |
for f in $(echo $class/{S,}[0-9][0-9]* ) ; do
|
| 64 |
[ -x $f -a -f $f ] && do_script $f
|
| 65 |
done
|
| 66 |
fi
|
| 67 |
done
|
| 68 |
}
|
| 69 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 70 |
do_script() {
|
| 71 |
|
| 72 |
# execute scripts and save their output in log files
|
| 73 |
# cfengine, shell, perl and expect scripts are known types
|
| 74 |
local shelldebug file filetype name
|
| 75 |
|
| 76 |
file=$1
|
| 77 |
|
| 78 |
# may be remove some day
|
| 79 |
case $file in
|
| 80 |
*~) [ "$debug" ] && echo "Skipping backup file $file"
|
| 81 |
return ;;
|
| 82 |
esac
|
| 83 |
|
| 84 |
name=$(basename $file)
|
| 85 |
case $name in
|
| 86 |
S[0-9][0-9]*) echo -e "WARNING: The script $name does not match [0-9][0-9]*.\nIt's not executed. Please rename it."; return ;;
|
| 87 |
esac
|
| 88 |
|
| 89 |
filetype=$(file $file)
|
| 90 |
|
| 91 |
if [ "$fake" ]; then
|
| 92 |
echo "Executing $filetype"
|
| 93 |
return
|
| 94 |
fi
|
| 95 |
|
| 96 |
shelldebug=
|
| 97 |
case $filetype in
|
| 98 |
*"Bourne shell script"*)
|
| 99 |
[ "$debug" ] && shelldebug="sh -x" ;;
|
| 100 |
*"Bourne-Again shell script"*)
|
| 101 |
[ "$debug" ] && shelldebug="bash -x" ;;
|
| 102 |
esac
|
| 103 |
|
| 104 |
case $filetype in
|
| 105 |
|
| 106 |
*"executable shell script"*|*"/bash script"*|*"Bourne shell script"*|*"Bourne-Again shell script"*)
|
| 107 |
echo "Executing $shelldebug shell: $file"
|
| 108 |
echo "===== shell: $file =====" >> $LOGDIR/shell.log 2>&1
|
| 109 |
$shelldebug ./$file >> $LOGDIR/shell.log 2>&1
|
| 110 |
fc_check_status $file $? | tee -a $LOGDIR/shell.log
|
| 111 |
;;
|
| 112 |
|
| 113 |
*"cfagent"*)
|
| 114 |
echo "Executing cfagent: $file"
|
| 115 |
echo "===== cfagent: $file =====" >> $LOGDIR/cfagent.log 2>&1
|
| 116 |
./$file -qKkI -D${cfclasses} >> $LOGDIR/cfagent.log 2>&1
|
| 117 |
fc_check_status $file $? | tee -a $LOGDIR/cfagent.log
|
| 118 |
;;
|
| 119 |
|
| 120 |
*"cfengine script"*)
|
| 121 |
echo "Executing cfengine: $file"
|
| 122 |
echo "===== cfengine: $file =====" >> $LOGDIR/cfengine.log 2>&1
|
| 123 |
./$file -K -v -f $file -D${cfclasses} >> $LOGDIR/cfengine.log 2>&1
|
| 124 |
fc_check_status $file $? | tee -a $LOGDIR/cfengine.log
|
| 125 |
;;
|
| 126 |
|
| 127 |
*"perl script"*)
|
| 128 |
echo "Executing perl: $file"
|
| 129 |
echo "===== perl: $file =====" >> $LOGDIR/perl.log 2>&1
|
| 130 |
./$file >> $LOGDIR/perl.log 2>&1
|
| 131 |
fc_check_status $file $? | tee -a $LOGDIR/perl.log
|
| 132 |
;;
|
| 133 |
|
| 134 |
*"expect script"*)
|
| 135 |
echo "Executing expect: $file"
|
| 136 |
echo "===== expect: $file =====" >> $LOGDIR/expect.log 2>&1
|
| 137 |
./$file >> $LOGDIR/expect.log 2>&1
|
| 138 |
fc_check_status $file $? | tee -a $LOGDIR/expect.log
|
| 139 |
;;
|
| 140 |
|
| 141 |
*) echo "File $file has unsupported type $filetype." ;;
|
| 142 |
esac
|
| 143 |
}
|
| 144 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 145 |
usage() {
|
| 146 |
|
| 147 |
local ex
|
| 148 |
ex=$1
|
| 149 |
cat <<-EOF
|
| 150 |
fai-do-scripts $version. Copyright (C) 2003-2005 Thomas Lange
|
| 151 |
Read the manual page fai-do-scripts(1) for more information.
|
| 152 |
|
| 153 |
Usage: fai-do-scripts [OPTION] DIRECTORY
|
| 154 |
|
| 155 |
EOF
|
| 156 |
exit $ex
|
| 157 |
}
|
| 158 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 159 |
# main program
|
| 160 |
|
| 161 |
while getopts "nhL:" opt ; do
|
| 162 |
case "$opt" in
|
| 163 |
h) usage 0 ;;
|
| 164 |
L) LOGDIR=$OPTARG; export LOGDIR ;;
|
| 165 |
n) fake=1 ;;
|
| 166 |
esac
|
| 167 |
done
|
| 168 |
shift $(($OPTIND - 1))
|
| 169 |
[ -z "$1" ] || [ -n "$2" ] && usage 1
|
| 170 |
|
| 171 |
if [ "x$classes" = "x" ]; then
|
| 172 |
echo "No classes are defined."
|
| 173 |
exit 9
|
| 174 |
fi
|
| 175 |
|
| 176 |
call_conf $1
|