2014-01-08 11:31:49 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Copyright 2010-2014 Frank Liepold / 1&1 Internet AG
|
2013-07-04 08:05:39 +00:00
|
|
|
#
|
2014-01-08 11:31:49 +00:00
|
|
|
# Email: frank.liepold@1und1.de
|
2013-07-04 08:05:39 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
2014-01-29 08:20:27 +00:00
|
|
|
## For a general documentation please refer to README, section "Running a test"
|
2013-07-04 08:05:39 +00:00
|
|
|
|
2013-11-07 09:33:43 +00:00
|
|
|
function set_host_locks
|
|
|
|
{
|
|
|
|
local host
|
2014-02-04 09:42:11 +00:00
|
|
|
echo "================= Creating lock files =========================================="
|
|
|
|
if [ ${#global_host_list[*]} -eq 0 ]; then
|
|
|
|
lib_vmsg " global_host_list empty, no lock files necessary"
|
2013-11-07 09:33:43 +00:00
|
|
|
return
|
|
|
|
fi
|
2014-02-04 09:42:11 +00:00
|
|
|
for host in "${global_host_list[@]}"; do
|
2013-11-07 09:33:43 +00:00
|
|
|
local lock_file=${main_lock_file_list[$host]}
|
|
|
|
if [ -z "$lock_file" ]; then
|
|
|
|
lib_exit 1 "no entry in main_lock_file_list for host $host"
|
|
|
|
fi
|
|
|
|
if [ -f $lock_file ]; then
|
|
|
|
echo "Failure lockfile $lock_file for host $host exists!" >&2
|
2014-02-03 07:06:13 +00:00
|
|
|
lib_exit $main_prevent_remove_lock_files_exit_code
|
2013-11-07 09:33:43 +00:00
|
|
|
fi
|
|
|
|
date > $lock_file || lib_exit 1
|
|
|
|
lib_vmsg " created lockfile $lock_file on $host"
|
|
|
|
done
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= End creating lock files ======================================"
|
2013-11-07 09:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function release_host_locks
|
|
|
|
{
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= Deleting lock files =========================================="
|
2014-02-04 09:42:11 +00:00
|
|
|
for host in "${global_host_list[@]}"; do
|
2013-11-07 09:33:43 +00:00
|
|
|
local lock_file=${main_lock_file_list[$host]}
|
|
|
|
rm -f $lock_file || lib_exit 1
|
|
|
|
lib_vmsg " deleted lockfile $lock_file on $host"
|
|
|
|
done
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= End deleting lock files ======================================"
|
2013-11-07 09:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:05:39 +00:00
|
|
|
function source_module
|
|
|
|
{
|
2014-02-04 09:42:11 +00:00
|
|
|
local module="$1"
|
|
|
|
local modname="$(basename $module .sh)"
|
2013-07-04 08:05:39 +00:00
|
|
|
if source_config default-$modname; then
|
2013-11-07 09:33:43 +00:00
|
|
|
echo "Sourcing module $modname"
|
2014-01-08 11:31:49 +00:00
|
|
|
source $module || start_test_exit $?
|
2014-02-04 09:42:11 +00:00
|
|
|
else
|
|
|
|
start_test_exit 1
|
2013-07-04 08:05:39 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-12-30 09:28:30 +00:00
|
|
|
function save_environment
|
|
|
|
{
|
|
|
|
# we cannot use lib_exit because the libs are not yet sourced in
|
|
|
|
environ_save=/tmp/environ.sav.$$
|
|
|
|
rm -f $environ_save || lib_exit 1 "cannot remove $environ_save"
|
|
|
|
set >$environ_save || lib_exit 1 "cannot create $environ_save"
|
|
|
|
}
|
|
|
|
|
|
|
|
# prints all shell variables which are set via sourcing the *.conf files
|
|
|
|
function print_config_environment
|
|
|
|
{
|
|
|
|
local f
|
|
|
|
local environ_actual=/tmp/environ.act.$$
|
|
|
|
[ -n "$environ_save" ] || lib_exit 1 "variable environ_save not set"
|
|
|
|
[ -r "$environ_save" ] || lib_exit 1 "file $environ_save not readable"
|
|
|
|
rm -f $environ_actual || lib_exit 1 "cannot remove $environ_actual"
|
|
|
|
set >$environ_actual || lib_exit 1 "cannot create $environ_actual"
|
|
|
|
# delete function definitions and sort
|
|
|
|
for f in $environ_save $environ_actual; do
|
|
|
|
sed -i -e '/^.* () *$/d;/^{ *$/,/^} *$/d' $f || lib_exit 1
|
|
|
|
sort -o $f $f || lib_exit 1
|
|
|
|
done
|
|
|
|
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= Configuration variables ======================================"
|
2013-12-30 09:28:30 +00:00
|
|
|
# print lines uniq to $environ_actual and remove some not interesting
|
|
|
|
# variables
|
|
|
|
comm -2 -3 $environ_actual $environ_save | \
|
|
|
|
egrep -v '^(BASH_LINENO|FUNCNAME|OLDPWD|_)='
|
2014-01-08 11:31:49 +00:00
|
|
|
rm -f $environ_actual
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= End configuration variables =================================="
|
2013-12-30 09:28:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-04 09:42:11 +00:00
|
|
|
# wrapper for the exit builtin to be able to remove temporary files
|
|
|
|
function start_test_exit
|
|
|
|
{
|
|
|
|
rm -f $environ_save
|
|
|
|
exit $1
|
|
|
|
}
|
|
|
|
|
2014-01-29 08:20:27 +00:00
|
|
|
function usage
|
|
|
|
{
|
|
|
|
echo "usage: $(basename $0) [--dry-run] [--config_root_dir=<my_dir>]" >&2
|
|
|
|
echo " Option --dry-run:" >&2
|
|
|
|
echo " Print all configuration variables and the name " >&2
|
|
|
|
echo " of the test. Don't execute it." >&2
|
|
|
|
echo " Option --config_root_dir:" >&2
|
|
|
|
echo " Include all *.conf files belonging to subdirectories " >&2
|
|
|
|
echo " between my_dir and test directory." >&2
|
|
|
|
echo " Default: my_dir = working directory" >&2
|
2014-04-01 04:54:34 +00:00
|
|
|
echo " Option --doc:" >&2
|
|
|
|
echo " Show all config variables for the actual test directory.">&2
|
|
|
|
echo " The variable run_list contains the functions called in this test." >&2
|
2014-01-29 08:20:27 +00:00
|
|
|
exit 1
|
|
|
|
}
|
2013-12-30 09:28:30 +00:00
|
|
|
|
2014-01-29 08:20:27 +00:00
|
|
|
dry_run_script=0
|
|
|
|
start_dir=$(pwd)
|
|
|
|
# all directories between config_root_dir and the actual test directory will be
|
|
|
|
# considered as "configuration options". That means that a <dirname>.conf file
|
|
|
|
# must be provided for all these directories
|
|
|
|
config_root_dir=$start_dir
|
2013-07-04 08:05:39 +00:00
|
|
|
|
|
|
|
# parse options.
|
|
|
|
while [ $# -ge 1 ]; do
|
|
|
|
key="$(echo "$1" | cut -d= -f1)"
|
|
|
|
val="$(echo "$1" | cut -d= -f2-)"
|
|
|
|
case "$key" in
|
2014-01-29 08:20:27 +00:00
|
|
|
--dry-run)
|
|
|
|
dry_run_script="$val"
|
|
|
|
shift
|
|
|
|
;;
|
2013-12-20 16:01:37 +00:00
|
|
|
--config_root_dir)
|
2014-01-29 08:20:27 +00:00
|
|
|
config_root_dir="$val"
|
|
|
|
shift
|
|
|
|
;;
|
2014-04-01 04:54:34 +00:00
|
|
|
--doc)
|
|
|
|
dry_run_script=1
|
|
|
|
shift
|
|
|
|
;;
|
2013-11-07 09:33:43 +00:00
|
|
|
*)
|
2014-01-29 08:20:27 +00:00
|
|
|
usage
|
2013-07-04 08:05:39 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2013-12-20 16:01:37 +00:00
|
|
|
if ! cd $config_root_dir ; then
|
|
|
|
echo "cannot cd $config_root_dir" >&2
|
2014-01-08 11:31:49 +00:00
|
|
|
start_test_exit 1
|
2013-12-20 16:01:37 +00:00
|
|
|
else
|
|
|
|
config_root_dir=$(pwd) # we need the absolute path
|
|
|
|
cd $start_dir
|
|
|
|
fi
|
2014-01-29 08:20:27 +00:00
|
|
|
|
|
|
|
shopt -s extdebug
|
|
|
|
|
|
|
|
# Use directory names as basis for configuration variants
|
|
|
|
|
|
|
|
script_dir="$(cd "$(dirname "$(which "$0")")"; pwd)"
|
2014-02-04 09:42:11 +00:00
|
|
|
lib_dir=$script_dir/lib
|
2014-01-29 08:20:27 +00:00
|
|
|
echo "================= Sourcing libraries in $lib_dir ==============================="
|
|
|
|
for lib in $lib_dir/lib*.sh; do
|
|
|
|
echo "Sourcing $lib"
|
|
|
|
source "$lib" || start_test_exit $?
|
|
|
|
done
|
|
|
|
echo "================= End sourcing libraries ======================================="
|
|
|
|
|
|
|
|
to_start="${to_start:-main}"
|
|
|
|
marker_file="i_am_a_testdirectory"
|
|
|
|
|
|
|
|
# check some preconditions
|
|
|
|
|
2014-04-25 05:46:23 +00:00
|
|
|
check_list="truncate grep sed gawk head tail cut nice date gzip gunzip zcat buffer pstree"
|
2014-01-29 08:20:27 +00:00
|
|
|
check_installed "$check_list"
|
|
|
|
|
|
|
|
# include modules
|
|
|
|
prepare_list=""
|
|
|
|
setup_list=""
|
|
|
|
run_list=""
|
|
|
|
cleanup_list=""
|
|
|
|
finish_list=""
|
|
|
|
|
|
|
|
save_environment # for later use in print_config_environment
|
|
|
|
|
|
|
|
shopt -s nullglob
|
|
|
|
echo "================= Sourcing modules and default configuration ==================="
|
2014-02-04 09:42:11 +00:00
|
|
|
module_dir=$script_dir/modules
|
|
|
|
[ -d "$module_dir" ] || \
|
|
|
|
lib_exit 1 "directory module_dir=$module_dir not found"
|
|
|
|
for module in $script_dir/modules/*.sh; do
|
2014-01-29 08:20:27 +00:00
|
|
|
source_module "$module"
|
|
|
|
done
|
2014-02-04 09:42:11 +00:00
|
|
|
source_config global || lib_exit 1
|
|
|
|
[ -n "$global_user_module_dir" ] || lib_exit 1 "global_user_module_dir not set"
|
|
|
|
[ -d "$global_user_module_dir" ] || \
|
|
|
|
lib_exit 1 "directory global_user_module_dir=$global_user_module_dir not found"
|
|
|
|
for module in $global_user_module_dir/*.sh; do
|
|
|
|
source_module "$module"
|
|
|
|
done
|
|
|
|
|
2014-01-29 08:20:27 +00:00
|
|
|
echo "================= End sourcing modules and default configuration ==============="
|
2013-07-04 08:05:39 +00:00
|
|
|
|
|
|
|
# find directories
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= Scanning subdirectories of $start_dir ========================"
|
2014-02-04 09:42:11 +00:00
|
|
|
for marker in $(find . -type f -name "$marker_file" | sort); do
|
2014-01-29 08:20:27 +00:00
|
|
|
test_dir=$(dirname $marker)
|
2013-11-07 09:33:43 +00:00
|
|
|
shopt -u nullglob
|
|
|
|
(
|
|
|
|
cd $test_dir
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= Test directory $(pwd) $date =================================="
|
2014-01-08 09:18:31 +00:00
|
|
|
echo "================= Sourcing config files between $config_root_dir and $(pwd) ===="
|
2013-11-07 09:33:43 +00:00
|
|
|
|
|
|
|
# to be able to call error recovery functions in case of signals
|
|
|
|
trap 'lib_exit 1 "caught signal"' SIGHUP SIGINT
|
|
|
|
|
|
|
|
# source all individual config files (for overrides)
|
2013-12-20 16:01:37 +00:00
|
|
|
# between $config_root_dir (exclusive) and $(pwd) (inclusive)
|
2013-11-07 09:33:43 +00:00
|
|
|
shopt -s nullglob
|
2013-12-23 06:13:11 +00:00
|
|
|
t=$(pwd) # absolute path
|
|
|
|
if [ "$t" = "$config_root_dir" ]; then
|
2013-12-20 16:01:37 +00:00
|
|
|
components=$(basename $(pwd))
|
|
|
|
else
|
|
|
|
components=$(echo ${t#$config_root_dir/} | sed 's/\// /g')
|
|
|
|
fi
|
2013-11-07 09:33:43 +00:00
|
|
|
for i in $components; do
|
|
|
|
[ "$i" = "." ] && continue
|
|
|
|
if ! source_config "$i"; then
|
|
|
|
echo "Cannot source config file '$i.conf' -- please provide one."
|
2014-01-08 11:31:49 +00:00
|
|
|
start_test_exit -1
|
2013-11-07 09:33:43 +00:00
|
|
|
fi
|
|
|
|
done
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= End sourcing config files between $config_root_dir and $(pwd) "
|
2013-12-30 09:28:30 +00:00
|
|
|
print_config_environment
|
2013-07-04 08:05:39 +00:00
|
|
|
shopt -u nullglob
|
2013-11-07 09:33:43 +00:00
|
|
|
|
|
|
|
if (( dry_run_script )); then
|
2014-01-29 08:20:27 +00:00
|
|
|
echo "would start $(pwd)"
|
|
|
|
exit 0
|
2013-11-07 09:33:43 +00:00
|
|
|
else
|
|
|
|
set_host_locks
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "================= Starting $(pwd) $(date) ======================================"
|
2014-01-20 10:56:32 +00:00
|
|
|
eval "$to_start"
|
|
|
|
test_rc=$?
|
2013-11-07 09:33:43 +00:00
|
|
|
release_host_locks
|
2014-01-20 10:56:32 +00:00
|
|
|
exit $test_rc
|
2013-07-04 08:05:39 +00:00
|
|
|
fi
|
2013-11-07 09:33:43 +00:00
|
|
|
)
|
|
|
|
rc=$?
|
|
|
|
if [ $rc -ne 0 ]; then
|
2014-01-09 11:04:38 +00:00
|
|
|
echo "========================== Failure $rc $(cd $test_dir; pwd) $date ==============" >&2
|
2013-11-07 09:33:43 +00:00
|
|
|
else
|
2014-01-09 11:04:38 +00:00
|
|
|
echo "========================== Finished $(cd $test_dir; pwd) $(date) ==============="
|
2013-11-07 09:33:43 +00:00
|
|
|
fi
|
2014-01-08 11:31:49 +00:00
|
|
|
[ $rc -ne 0 ] && start_test_exit $rc
|
2013-07-04 08:05:39 +00:00
|
|
|
done
|
|
|
|
|
2014-01-09 06:31:18 +00:00
|
|
|
echo "========================== Finished start directory $start_dir ================="
|
2014-01-08 11:31:49 +00:00
|
|
|
start_test_exit 0
|