| 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-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 | # variables needed: $classes, $cfclasses, $LOGDIR, $FAI_USE_OLD_SCRIPT_LOGS |
| 30 | # |
| 31 | # And many other variables like: |
| 32 | |
| 33 | # execute all scripts that match the name of a class. |
| 34 | # If class is a directory, execute all $class/[0-9][0-9]* scripts in |
| 35 | # it, but do not execute files ending in ~ |
| 36 | |
| 37 | maxstatus=0 |
| 38 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 39 | savemaxstatus() { |
| 40 | |
| 41 | exitcode=$1 # set global variable |
| 42 | # save the highest exit status |
| 43 | [ $1 -gt $maxstatus ] && maxstatus=$1 |
| 44 | } |
| 45 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 46 | fc_check_status() { |
| 47 | |
| 48 | local cmd st res |
| 49 | cmd=$1 |
| 50 | st=$2 |
| 51 | |
| 52 | if [ $st -eq 0 ]; then |
| 53 | res="OK." |
| 54 | else |
| 55 | res="FAILED with exit code $st." |
| 56 | fi |
| 57 | # put result in the log file and write to stdout |
| 58 | printf "%-20s $res\n" $cmd | tee -a $LOGDIR/status.log |
| 59 | } |
| 60 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 61 | call_conf() { |
| 62 | |
| 63 | local class f |
| 64 | cd $1 |
| 65 | for class in $classes ; do |
| 66 | [ -x $class -a -f $class ] && do_script $class |
| 67 | if [ -d $class ]; then |
| 68 | for f in $(echo $class/[0-9][0-9]* ) ; do |
| 69 | [ -x $f -a -f $f ] && do_script $f |
| 70 | done |
| 71 | fi |
| 72 | done |
| 73 | } |
| 74 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 75 | vecho() { |
| 76 | |
| 77 | if [ X$verbose = X1 ]; then |
| 78 | echo $* |
| 79 | fi |
| 80 | } |
| 81 | |
| 82 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 83 | do_script() { |
| 84 | |
| 85 | # execute scripts and save their output in log files |
| 86 | # cfengine, shell, perl and expect scripts are known types |
| 87 | local shelldebug file filetype name |
| 88 | local -r slog=$LOGDIR/scripts.log |
| 89 | |
| 90 | file=$1 |
| 91 | |
| 92 | # may be remove some day |
| 93 | case $file in |
| 94 | *~) [ X$debug = X1 ] && echo "Skipping backup file $file"; return ;; |
| 95 | *.dpkg-old) [ X$debug = X1 ] && echo "Skipping file $file"; return ;; |
| 96 | *.dpkg-new) [ X$debug = X1 ] && echo "Skipping file $file"; return ;; |
| 97 | *.dpkg-inst) [ X$debug = X1 ] && echo "Skipping file $file"; return ;; |
| 98 | *.dpkg-tmp) [ X$debug = X1 ] && echo "Skipping file $file"; return ;; |
| 99 | *.dpkg-dist) [ X$debug = X1 ] && echo "Skipping file $file"; return ;; |
| 100 | esac |
| 101 | |
| 102 | name=${file##*/} # basename function |
| 103 | filetype=$(file -bL $file) |
| 104 | |
| 105 | if [ "$fake" ]; then |
| 106 | echo "Executing $filetype" |
| 107 | return |
| 108 | fi |
| 109 | |
| 110 | shelldebug= |
| 111 | case $filetype in |
| 112 | *"POSIX shell script"*|*"Bourne shell script"*) |
| 113 | [ X$debug = X1 ] && shelldebug="sh -x" ;; |
| 114 | *"Bourne-Again shell script"*) |
| 115 | [ X$debug = X1 ] && shelldebug="bash -x" ;; |
| 116 | *"zsh script"*) |
| 117 | [ X$debug = X1 ] && shelldebug="zsh -x" ;; |
| 118 | esac |
| 119 | |
| 120 | |
| 121 | if [ X$FAI_USE_OLD_SCRIPT_LOGS = X1 ]; then |
| 122 | echo "WARNING. Old style log file names for custom scripts may be removed in the future." |
| 123 | echo "unset FAI_USE_OLD_SCRIPT_LOGS for writing output to scripts.log." |
| 124 | # use old code below |
| 125 | else |
| 126 | |
| 127 | # always write to scripts.log |
| 128 | case $filetype in |
| 129 | *"POSIX shell script"*|*"executable shell script"*|*"/bash script"*|*"Bourne shell script"*|*"Bourne-Again shell script"*|*"zsh script"*) |
| 130 | vecho "Executing $shelldebug shell: $file" |
| 131 | echo "===== shell: $file =====" >> $slog 2>&1 |
| 132 | $shelldebug ./$file >> $slog 2>&1 |
| 133 | savemaxstatus $? |
| 134 | fc_check_status $file $exitcode | tee -a $slog |
| 135 | ;; |
| 136 | *) |
| 137 | vecho "Executing: $file" |
| 138 | echo "===== $filetype $file =====" >> $slog 2>&1 |
| 139 | ./$file >> $slog 2>&1 |
| 140 | savemaxstatus $? |
| 141 | fc_check_status $file $exitcode | tee -a $slog |
| 142 | ;; |
| 143 | esac |
| 144 | return |
| 145 | fi |
| 146 | |
| 147 | case $filetype in |
| 148 | |
| 149 | *"POSIX shell script"*|*"executable shell script"*|*"/bash script"*|*"Bourne shell script"*|*"Bourne-Again shell script"*|*"zsh script"*) |
| 150 | vecho "Executing $shelldebug shell: $file" |
| 151 | echo "===== shell: $file =====" >> $LOGDIR/shell.log 2>&1 |
| 152 | $shelldebug ./$file >> $LOGDIR/shell.log 2>&1 |
| 153 | savemaxstatus $? |
| 154 | fc_check_status $file $exitcode | tee -a $LOGDIR/shell.log |
| 155 | ;; |
| 156 | |
| 157 | *"cf-agent"*) |
| 158 | vecho "Executing cf-agent: $file" |
| 159 | echo "===== cf-agent: $file =====" >> $LOGDIR/cfagent.log 2>&1 |
| 160 | ./$file $cfagentdebug -KI -D${cfclasses} >> $LOGDIR/cfagent.log 2>&1 |
| 161 | savemaxstatus $? |
| 162 | fc_check_status $file $exitcode | tee -a $LOGDIR/cfagent.log |
| 163 | ;; |
| 164 | |
| 165 | *[Pp]erl*script*) |
| 166 | vecho "Executing perl: $file" |
| 167 | echo "===== perl: $file =====" >> $LOGDIR/perl.log 2>&1 |
| 168 | ./$file >> $LOGDIR/perl.log 2>&1 |
| 169 | savemaxstatus $? |
| 170 | fc_check_status $file $exitcode | tee -a $LOGDIR/perl.log |
| 171 | ;; |
| 172 | |
| 173 | *"expect script"*) |
| 174 | vecho "Executing expect: $file" |
| 175 | echo "===== expect: $file =====" >> $LOGDIR/expect.log 2>&1 |
| 176 | ./$file >> $LOGDIR/expect.log 2>&1 |
| 177 | savemaxstatus $? |
| 178 | fc_check_status $file $exitcode | tee -a $LOGDIR/expect.log |
| 179 | ;; |
| 180 | |
| 181 | *[Pp]ython*script*) |
| 182 | vecho "Executing python: $file" |
| 183 | echo "===== python: $file =====" >> $LOGDIR/python.log 2>&1 |
| 184 | ./$file >> $LOGDIR/python.log 2>&1 |
| 185 | savemaxstatus $? |
| 186 | fc_check_status $file $exitcode | tee -a $LOGDIR/python.log |
| 187 | ;; |
| 188 | |
| 189 | *[Rr]uby*script*) |
| 190 | vecho "Executing ruby: $file" |
| 191 | echo "===== ruby: $file =====" >> $LOGDIR/ruby.log 2>&1 |
| 192 | ./$file >> $LOGDIR/ruby.log 2>&1 |
| 193 | savemaxstatus $? |
| 194 | fc_check_status $file $exitcode | tee -a $LOGDIR/ruby.log |
| 195 | ;; |
| 196 | |
| 197 | *) |
| 198 | vecho "Executing: $file" |
| 199 | echo "===== $filetype $file =====" >> $LOGDIR/scripts.log 2>&1 |
| 200 | ./$file >> $LOGDIR/scripts.log 2>&1 |
| 201 | savemaxstatus $? |
| 202 | fc_check_status $file $exitcode | tee -a $LOGDIR/scripts.log |
| 203 | ;; |
| 204 | esac |
| 205 | } |
| 206 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 207 | usage() { |
| 208 | |
| 209 | local ex |
| 210 | ex=$1 |
| 211 | cat <<-EOF |
| 212 | fai-do-scripts Copyright (C) 2003-2010 Thomas Lange |
| 213 | Read the manual page fai-do-scripts(1) for more information. |
| 214 | |
| 215 | Usage: fai-do-scripts [OPTION] DIRECTORY |
| 216 | |
| 217 | EOF |
| 218 | exit $ex |
| 219 | } |
| 220 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 221 | # main program |
| 222 | |
| 223 | while getopts "nhL:" opt ; do |
| 224 | case "$opt" in |
| 225 | h) usage 0 ;; |
| 226 | L) LOGDIR=$OPTARG; export LOGDIR ;; |
| 227 | n) fake=1 ;; |
| 228 | esac |
| 229 | done |
| 230 | shift $((OPTIND - 1)) |
| 231 | [ -z "$1" ] || [ -n "$2" ] && usage 1 |
| 232 | |
| 233 | if [ "x$classes" = "x" ]; then |
| 234 | echo "No classes are defined." |
| 235 | exit 9 |
| 236 | fi |
| 237 | |
| 238 | [ X$debug = X1 ] && cfagentdebug=-v |
| 239 | |
| 240 | call_conf $1 |
| 241 | # move error number from child scripts to 100+x if any error happened |
| 242 | [ $maxstatus -gt 0 ] && maxstatus=$((100+maxstatus)) |
| 243 | exit $maxstatus |