1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#! /bin/bash
#*********************************************************************
#
# fai-do-scripts - call configuration scripts for every defined class
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2006 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#*********************************************************************
version="version 1.5.1, 2-aug-2006"
# variables needed: $classes, $cfclasses, $LOGDIR
#
# And many other variables like:
# execute all scripts that match the name of a class.
# If class is a directory, execute all $class/[0-9][0-9]* scripts in
# it, but do not execute files ending in ~
# TODO: -n only shows which scripts should be executed, but do not execute them
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fc_check_status() {
local cmd st res
cmd=$1
st=$2
if [ $st -eq 0 ]; then
res="OK."
else
res="FAILED with exit code $st."
fi
# put result in the log file and write to stdout
printf "%-20s $res\n" $cmd | tee -a $LOGDIR/status.log
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call_conf() {
local class f
cd $1
for class in $classes ; do
[ -x $class -a -f $class ] && do_script $class
if [ -d $class ]; then
for f in $(echo $class/{S,}[0-9][0-9]* ) ; do
[ -x $f -a -f $f ] && do_script $f
done
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
do_script() {
# execute scripts and save their output in log files
# cfengine, shell, perl and expect scripts are known types
local shelldebug file filetype name
file=$1
# may be remove some day
case $file in
*~) [ "$debug" ] && echo "Skipping backup file $file"
return ;;
esac
name=$(basename $file)
case $name in
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 ;;
esac
filetype=$(file $file)
if [ "$fake" ]; then
echo "Executing $filetype"
return
fi
shelldebug=
case $filetype in
*"Bourne shell script"*)
[ "$debug" ] && shelldebug="sh -x" ;;
*"Bourne-Again shell script"*)
[ "$debug" ] && shelldebug="bash -x" ;;
esac
case $filetype in
*"executable shell script"*|*"/bash script"*|*"Bourne shell script"*|*"Bourne-Again shell script"*)
echo "Executing $shelldebug shell: $file"
echo "===== shell: $file =====" >> $LOGDIR/shell.log 2>&1
$shelldebug ./$file >> $LOGDIR/shell.log 2>&1
fc_check_status $file $? | tee -a $LOGDIR/shell.log
;;
*"cfagent"*)
echo "Executing cfagent: $file"
echo "===== cfagent: $file =====" >> $LOGDIR/cfagent.log 2>&1
./$file -qKkI -D${cfclasses} >> $LOGDIR/cfagent.log 2>&1
fc_check_status $file $? | tee -a $LOGDIR/cfagent.log
;;
*"cfengine script"*)
echo "Executing cfengine: $file"
echo "===== cfengine: $file =====" >> $LOGDIR/cfengine.log 2>&1
./$file -K -v -f $file -D${cfclasses} >> $LOGDIR/cfengine.log 2>&1
fc_check_status $file $? | tee -a $LOGDIR/cfengine.log
;;
*"perl script"*)
echo "Executing perl: $file"
echo "===== perl: $file =====" >> $LOGDIR/perl.log 2>&1
./$file >> $LOGDIR/perl.log 2>&1
fc_check_status $file $? | tee -a $LOGDIR/perl.log
;;
*"expect script"*)
echo "Executing expect: $file"
echo "===== expect: $file =====" >> $LOGDIR/expect.log 2>&1
./$file >> $LOGDIR/expect.log 2>&1
fc_check_status $file $? | tee -a $LOGDIR/expect.log
;;
*) echo "File $file has unsupported type $filetype." ;;
esac
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {
local ex
ex=$1
cat <<-EOF
fai-do-scripts $version. Copyright (C) 2003-2005 Thomas Lange
Read the manual page fai-do-scripts(1) for more information.
Usage: fai-do-scripts [OPTION] DIRECTORY
EOF
exit $ex
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main program
while getopts "nhL:" opt ; do
case "$opt" in
h) usage 0 ;;
L) LOGDIR=$OPTARG; export LOGDIR ;;
n) fake=1 ;;
esac
done
shift $(($OPTIND - 1))
[ -z "$1" ] || [ -n "$2" ] && usage 1
if [ "x$classes" = "x" ]; then
echo "No classes are defined."
exit 9
fi
call_conf $1
|