2013-02-14 00:41:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Inspired by Debian and RedHat run-parts but portable and specific to di-b.
|
|
|
|
#
|
|
|
|
# Copyright 2012 Hewlett-Packard Development Company, L.P.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2013-02-14 18:20:30 +00:00
|
|
|
allowed_regex=${RUN_PARTS_REGEX:-"^[0-9A-Za-z_-]+$"}
|
2013-09-04 08:43:11 +00:00
|
|
|
show_list=
|
2013-02-14 18:20:30 +00:00
|
|
|
|
2013-02-14 00:41:09 +00:00
|
|
|
set -ue
|
2014-04-03 02:24:15 +00:00
|
|
|
set -o pipefail
|
2013-02-14 00:41:09 +00:00
|
|
|
|
|
|
|
name=$(basename $0)
|
|
|
|
|
|
|
|
usage() {
|
2013-11-27 09:38:19 +00:00
|
|
|
echo "Usage: $name [OPTION] scripts_directory"
|
|
|
|
echo "Option:"
|
|
|
|
echo " --list print names of all valid files"
|
|
|
|
echo
|
|
|
|
echo "Examples:"
|
|
|
|
echo " dib-run-parts --list /opt/stack/os-config-refresh/configure.d/"
|
|
|
|
echo " dib-run-parts /opt/stack/os-config-refresh/configure.d/"
|
2013-02-14 00:41:09 +00:00
|
|
|
exit 1
|
2013-11-27 09:38:19 +00:00
|
|
|
} >&2
|
2013-02-14 00:41:09 +00:00
|
|
|
|
|
|
|
output () {
|
|
|
|
echo $name $(date) $* >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ $# -lt 1 ] ; then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2013-09-04 08:43:11 +00:00
|
|
|
if [ "$1" == "--list" ] ; then
|
|
|
|
show_list="1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2013-11-27 09:38:19 +00:00
|
|
|
target_dir="${1:-}"
|
2013-02-14 00:41:09 +00:00
|
|
|
|
2013-11-27 09:38:19 +00:00
|
|
|
if ! [ -d "$target_dir" ] ; then
|
|
|
|
output "Scripts directory [$target_dir] must exist and be a directory"
|
2013-02-14 00:41:09 +00:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We specifically only want to sort *by the numbers*.
|
2013-02-14 18:20:30 +00:00
|
|
|
# Lexical sorting is not guaranteed, and identical numbers may be
|
|
|
|
# parallelized later
|
2013-07-22 10:06:54 +00:00
|
|
|
# Note: -maxdepth 1 ensures only files in the target directory (but not
|
|
|
|
# subdirectories) are run, which is the way run-parts behaves.
|
2014-04-03 22:49:18 +00:00
|
|
|
targets=$(find $target_dir -maxdepth 1 -xtype f -executable -printf '%f\n' | grep -E "$allowed_regex" | LANG=C sort -n || echo "")
|
2013-02-14 00:41:09 +00:00
|
|
|
|
2013-09-04 08:43:11 +00:00
|
|
|
if [ "$show_list" == "1" ] ; then
|
2014-05-02 17:02:38 +00:00
|
|
|
for target in $targets ; do
|
|
|
|
echo "${target_dir}/${target}"
|
|
|
|
done
|
|
|
|
exit 0
|
2013-09-04 08:43:11 +00:00
|
|
|
fi
|
|
|
|
|
2013-03-22 16:54:21 +00:00
|
|
|
PROFILE_DIR=$(mktemp -d /tmp/profiledir.XXXXXX)
|
|
|
|
|
2014-02-11 13:33:45 +00:00
|
|
|
ENVIRONMENT_D_DIR=$target_dir/../environment.d
|
|
|
|
|
|
|
|
if [ -d $ENVIRONMENT_D_DIR ] ; then
|
2014-07-11 15:50:15 +00:00
|
|
|
env_files=$(find $ENVIRONMENT_D_DIR -maxdepth 1 -xtype f | \
|
|
|
|
grep -E "/[0-9A-Za-z_\.-]+$" | \
|
|
|
|
LANG=C sort -n)
|
|
|
|
for env_file in $env_files ; do
|
2014-07-08 15:12:04 +00:00
|
|
|
source $env_file
|
2013-07-09 22:01:33 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2013-02-14 00:41:09 +00:00
|
|
|
for target in $targets ; do
|
|
|
|
output "Running $target_dir/$target"
|
2013-03-22 16:54:21 +00:00
|
|
|
target_tag=${target//\//_}
|
|
|
|
date +%s.%N > $PROFILE_DIR/start_$target_tag
|
2013-02-14 00:41:09 +00:00
|
|
|
$target_dir/$target
|
2013-03-22 16:54:21 +00:00
|
|
|
target_tag=${target//\//_}
|
|
|
|
date +%s.%N > $PROFILE_DIR/stop_$target_tag
|
2013-02-14 00:41:09 +00:00
|
|
|
output "$target completed"
|
|
|
|
done
|
2013-03-22 16:54:21 +00:00
|
|
|
|
|
|
|
echo "----------------------- PROFILING -----------------------"
|
|
|
|
echo ""
|
|
|
|
echo "Target: $(basename $target_dir)"
|
|
|
|
echo ""
|
|
|
|
printf "%-40s %9s\n" Script Seconds
|
|
|
|
printf "%-40s %9s\n" --------------------------------------- ----------
|
|
|
|
echo ""
|
|
|
|
pushd $PROFILE_DIR > /dev/null
|
2013-06-12 14:06:37 +00:00
|
|
|
for target in $(find . -name 'start_*' -printf '%f\n') ; do
|
2013-03-22 16:54:21 +00:00
|
|
|
stop_file=stop_${target##start_}
|
|
|
|
start_seconds=$(cat $target)
|
|
|
|
stop_seconds=$(cat $stop_file)
|
2014-05-21 12:28:15 +00:00
|
|
|
duration=$(python -c "print($stop_seconds - $start_seconds)")
|
2014-07-01 08:47:00 +00:00
|
|
|
LC_NUMERIC=C LC_ALL=C printf "%-40s %10.3f\n" ${target##start_} $duration
|
2013-03-22 16:54:21 +00:00
|
|
|
done
|
|
|
|
popd > /dev/null
|
|
|
|
rm -rf $PROFILE_DIR
|
|
|
|
echo ""
|
|
|
|
echo "--------------------- END PROFILING ---------------------"
|