From 3c377b5b241526a32e8ed9c90165023f614767b9 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Thu, 24 Apr 2014 22:01:36 +0000 Subject: [PATCH] Move dib-run-parts into bin/ --- bin/dib-run-parts | 110 ++++++++++++++++++++++- elements/dib-run-parts/bin/dib-run-parts | 109 ---------------------- 2 files changed, 109 insertions(+), 110 deletions(-) mode change 120000 => 100755 bin/dib-run-parts delete mode 100755 elements/dib-run-parts/bin/dib-run-parts diff --git a/bin/dib-run-parts b/bin/dib-run-parts deleted file mode 120000 index 055c8b3..0000000 --- a/bin/dib-run-parts +++ /dev/null @@ -1 +0,0 @@ -../elements/dib-run-parts/bin/dib-run-parts \ No newline at end of file diff --git a/bin/dib-run-parts b/bin/dib-run-parts new file mode 100755 index 0000000..af5fde9 --- /dev/null +++ b/bin/dib-run-parts @@ -0,0 +1,109 @@ +#!/bin/bash +# Inspired by Debian and RedHat run-parts but portable and specific to di-b. +# +# Copyright 2012 Hewlett-Packard Development Company, L.P. +# All Rights Reserved. +# +# 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. + +allowed_regex=${RUN_PARTS_REGEX:-"^[0-9A-Za-z_-]+$"} +show_list= + +set -ue + +name=$(basename $0) + +usage() { + 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/" + exit 1 +} >&2 + +output () { + echo $name $(date) $* >&2 +} + +if [ $# -lt 1 ] ; then + usage +fi + +if [ "$1" == "--list" ] ; then + show_list="1" + shift +fi + +target_dir="${1:-}" + +if ! [ -d "$target_dir" ] ; then + output "Scripts directory [$target_dir] must exist and be a directory" + usage +fi + +# We specifically only want to sort *by the numbers*. +# Lexical sorting is not guaranteed, and identical numbers may be +# parallelized later +# Note: -maxdepth 1 ensures only files in the target directory (but not +# subdirectories) are run, which is the way run-parts behaves. +targets=$(find $target_dir -maxdepth 1 -xtype f -executable -printf '%f\n' | grep -E "$allowed_regex" | LANG=C sort -n || echo "") + +if [ "$show_list" == "1" ] ; then + for target in $targets ; do + echo "${target_dir}/${target}" + done + exit 0 +fi + +PROFILE_DIR=$(mktemp -d /tmp/profiledir.XXXXXX) + +ENVIRONMENT_D_DIR=$target_dir/../environment.d + +if [ -d $ENVIRONMENT_D_DIR ] ; then + for env_file in $ENVIRONMENT_D_DIR/* ; do + source $env_file + done +fi + +for target in $targets ; do + output "Running $target_dir/$target" + target_tag=${target//\//_} + date +%s.%N > $PROFILE_DIR/start_$target_tag + $target_dir/$target + target_tag=${target//\//_} + date +%s.%N > $PROFILE_DIR/stop_$target_tag + output "$target completed" +done + +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 +for target in $(find . -name 'start_*' -printf '%f\n') ; do + stop_file=stop_${target##start_} + start_seconds=$(cat $target) + stop_seconds=$(cat $stop_file) + duration=$(python -c "print $stop_seconds - $start_seconds") + LC_NUMERIC=C printf "%-40s %10.3f\n" ${target##start_} $duration +done +popd > /dev/null +rm -rf $PROFILE_DIR +echo "" +echo "--------------------- END PROFILING ---------------------" diff --git a/elements/dib-run-parts/bin/dib-run-parts b/elements/dib-run-parts/bin/dib-run-parts deleted file mode 100755 index af5fde9..0000000 --- a/elements/dib-run-parts/bin/dib-run-parts +++ /dev/null @@ -1,109 +0,0 @@ -#!/bin/bash -# Inspired by Debian and RedHat run-parts but portable and specific to di-b. -# -# Copyright 2012 Hewlett-Packard Development Company, L.P. -# All Rights Reserved. -# -# 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. - -allowed_regex=${RUN_PARTS_REGEX:-"^[0-9A-Za-z_-]+$"} -show_list= - -set -ue - -name=$(basename $0) - -usage() { - 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/" - exit 1 -} >&2 - -output () { - echo $name $(date) $* >&2 -} - -if [ $# -lt 1 ] ; then - usage -fi - -if [ "$1" == "--list" ] ; then - show_list="1" - shift -fi - -target_dir="${1:-}" - -if ! [ -d "$target_dir" ] ; then - output "Scripts directory [$target_dir] must exist and be a directory" - usage -fi - -# We specifically only want to sort *by the numbers*. -# Lexical sorting is not guaranteed, and identical numbers may be -# parallelized later -# Note: -maxdepth 1 ensures only files in the target directory (but not -# subdirectories) are run, which is the way run-parts behaves. -targets=$(find $target_dir -maxdepth 1 -xtype f -executable -printf '%f\n' | grep -E "$allowed_regex" | LANG=C sort -n || echo "") - -if [ "$show_list" == "1" ] ; then - for target in $targets ; do - echo "${target_dir}/${target}" - done - exit 0 -fi - -PROFILE_DIR=$(mktemp -d /tmp/profiledir.XXXXXX) - -ENVIRONMENT_D_DIR=$target_dir/../environment.d - -if [ -d $ENVIRONMENT_D_DIR ] ; then - for env_file in $ENVIRONMENT_D_DIR/* ; do - source $env_file - done -fi - -for target in $targets ; do - output "Running $target_dir/$target" - target_tag=${target//\//_} - date +%s.%N > $PROFILE_DIR/start_$target_tag - $target_dir/$target - target_tag=${target//\//_} - date +%s.%N > $PROFILE_DIR/stop_$target_tag - output "$target completed" -done - -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 -for target in $(find . -name 'start_*' -printf '%f\n') ; do - stop_file=stop_${target##start_} - start_seconds=$(cat $target) - stop_seconds=$(cat $stop_file) - duration=$(python -c "print $stop_seconds - $start_seconds") - LC_NUMERIC=C printf "%-40s %10.3f\n" ${target##start_} $duration -done -popd > /dev/null -rm -rf $PROFILE_DIR -echo "" -echo "--------------------- END PROFILING ---------------------"