mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-pkg
synced 2026-01-15 03:53:32 +01:00
355 lines
5.9 KiB
Bash
355 lines
5.9 KiB
Bash
export PATH=$PATH:/opt/ytools/bin
|
|
|
|
# -- private stuff not intended for use outside of this script
|
|
_ytools_check_config_present()
|
|
{
|
|
if [ -z "$ytools_config_files" ]; then
|
|
if [ "$1" = true ]; then
|
|
ytools_log "warning: $0 tries to access configuration witout having a config file"
|
|
fi
|
|
return 1;
|
|
fi
|
|
return 0;
|
|
}
|
|
|
|
_ytools_probe_default_config_paths()
|
|
{
|
|
[ "$ytools_config_files" ] && return 0
|
|
|
|
local f
|
|
for f in \
|
|
/etc/opt/$ytools_project/$ytools_basename.conf \
|
|
$HOME/.$ytools_project/$ytools_basename.conf \
|
|
$HOME/.$ytools_project/$rc \
|
|
$HOME/.$ytools_basename/$ytools_basename.conf \
|
|
$HOME/.$ytools_basename/$rc \
|
|
$HOME/.$rc \
|
|
; do
|
|
[ -r "$f" ] && ytools_config_file="$ytools_config_files $f"
|
|
done
|
|
}
|
|
|
|
_ytools_cat_section()
|
|
{
|
|
if [ "$2" ]; then
|
|
sed -n "/\[$2\]/,/[^ ]*\[/ p" $1 | sed '/[^ ]*\[/ d'
|
|
return
|
|
fi
|
|
|
|
sed -n '0,/[^ ]*\[/ p' | sed '/[^ ]*\[/ d; s/=.*/=/' $1
|
|
}
|
|
|
|
_ytools_format_config()
|
|
{
|
|
sed '/^$/d; s/$/;/'
|
|
}
|
|
|
|
_ytools_format_section()
|
|
{
|
|
_ytools_cat_section $* | _ytools_format_config
|
|
}
|
|
|
|
_ytools_source_section()
|
|
{
|
|
eval `_ytools_cat_section $* | _ytools_format_config`
|
|
}
|
|
|
|
# -- exported variables
|
|
[ -z "$ytools_basename" ] && ytools_basename=`basename $0 | sed 's/\.sh$//'`
|
|
[ -z "$ytools_project" ] && ytools_project="ytools"
|
|
[ -z "$ytools_rc" ] && ytools_rc="$ytools_basename"rc
|
|
[ -z "$ytools_config_files" ] && _ytools_probe_default_config_paths
|
|
[ -z "$ytools_loggers" ] && ytools_loggers="stdout"
|
|
|
|
# -- exported utilities
|
|
ytools_log_err()
|
|
{
|
|
echo "# $@" >&2
|
|
}
|
|
|
|
ytools_log_stdout()
|
|
{
|
|
echo "# $@"
|
|
}
|
|
|
|
ytools_log_stderr()
|
|
{
|
|
echo "# $@" >&2
|
|
}
|
|
|
|
ytools_log_syslog()
|
|
{
|
|
logger -t $ytools_basename "$*"
|
|
}
|
|
|
|
ytools_log()
|
|
{
|
|
local logger
|
|
for logger in $ytools_loggers; do
|
|
eval ytools_log_$logger "$@"
|
|
done
|
|
}
|
|
|
|
ytools_empty_config()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true;;
|
|
-s)
|
|
o_section=$2; shift;;
|
|
*)
|
|
ytools_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_ytools_check_config_present $o_verbose || return 1
|
|
|
|
[ ! -r "$1" ] && return
|
|
[ "$o_verbose" ] && ytools_log o resetting config "\"$1\""
|
|
if [ "$o_section" ]; then
|
|
[ "$o_verbose" ] && ytools_log o resetting section "[$o_section]" of config "\"$1\""
|
|
fi
|
|
|
|
eval `_ytools_format_section $1 $o_section | sed 's/=[^;]*;/=;/g'`
|
|
}
|
|
|
|
ytools_empty_configs()
|
|
{
|
|
local section
|
|
local c
|
|
local o_verbose=""
|
|
local o_section
|
|
local opts=""
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
opts="$opts -v"
|
|
;;
|
|
-s)
|
|
o_section="-s $2"; shift;;
|
|
*)
|
|
ytools_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_ytools_check_config_present $o_verbose || return 1
|
|
|
|
for section in "" `ytools_config_sections`; do
|
|
|
|
for c in $ytools_config_files; do
|
|
|
|
ytools_empty_config -s "$section" section $opts $c
|
|
|
|
if [ -d $c.d ]; then
|
|
|
|
local dirconfs=`find $c.d -maxdepth 1 -type f`
|
|
local f
|
|
for f in $dirconfs; do
|
|
ytools_empty_config -s "$section" $opts $f
|
|
done
|
|
fi
|
|
done
|
|
done
|
|
|
|
return 0
|
|
}
|
|
|
|
ytools_source_config()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
;;
|
|
-s)
|
|
o_section=$2; shift;;
|
|
*)
|
|
ytools_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_ytools_check_config_present $o_verbose || return 1
|
|
|
|
if [ "$o_verbose" = true ]; then
|
|
if [ "$o_section" ]; then
|
|
ytools_log o sourcing section "[$o_section]" from config file "\"$1\""
|
|
else
|
|
ytools_log o sourcing config "\"$1\""
|
|
fi
|
|
|
|
_ytools_cat_section $1 $o_section
|
|
fi
|
|
|
|
_ytools_source_section $1 $o_section
|
|
}
|
|
|
|
ytools_source_configs()
|
|
{
|
|
local c
|
|
local source_opts=""
|
|
local o_verbose=""
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
source_opts="$source_opts -v"
|
|
;;
|
|
-s)
|
|
source_opts="$source_opts -s $2"
|
|
shift;;
|
|
*)
|
|
ytools_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_ytools_check_config_present $o_verbose || return 1
|
|
|
|
for c in $ytools_config_files; do
|
|
ytools_source_config $source_opts $c
|
|
if [ -d $c.d ]; then
|
|
local dirconfs=`find $c.d -maxdepth 1 -type f`
|
|
local f
|
|
for f in $dirconfs; do
|
|
ytools_source_config $source_opts $f
|
|
done
|
|
fi
|
|
done
|
|
}
|
|
|
|
ytools_config_sections()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
local o_file
|
|
local o_exact=false
|
|
|
|
OPTIND=1
|
|
while getopts ves:f: flag; do
|
|
|
|
case $flag in
|
|
v)
|
|
o_verbose=true;;
|
|
s)
|
|
o_section="$OPTARG";;
|
|
f)
|
|
o_file="$OPTARG";;
|
|
e)
|
|
o_exact=true;;
|
|
*)
|
|
ytools_log_err "unknown option -$flag"
|
|
exit 1;;
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
_ytools_check_config_present $o_verbose || return 1
|
|
|
|
[ "$o_file" ] || o_file="$ytools_config_files"
|
|
|
|
if [ "$o_exact" = false ]; then
|
|
|
|
if [ "$o_section" ]; then
|
|
sed "
|
|
/^[ ]*\[$o_section\.\(.*\)\]/ !d
|
|
s/^[ ]*\[$o_section\.\(.*\)\].*/\1/
|
|
" $o_file 2>/dev/null | sort -u
|
|
else
|
|
sed "
|
|
/^[ ]*\[\(.*\)\]/ !d
|
|
s/^[ ]*\[\(.*\)\].*/\1/
|
|
" $o_file 2>/dev/null | sort -u
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$o_section" ]; then
|
|
sed "
|
|
/^[ ]*\[$o_section\.\([^.]*\)\]/ !d
|
|
s/^[ ]*\[$o_section\.\([^.]*\)\].*/\1/
|
|
" $o_file 2>/dev/null | sort -u
|
|
else
|
|
sed "
|
|
/^[ ]*\[\([^.]*\)\]/ !d
|
|
s/^[ ]*\[\([^.]*\)\].*/\1/
|
|
" $o_file 2>/dev/null | sort -u
|
|
fi
|
|
fi
|
|
|
|
return
|
|
}
|
|
|
|
ytools_cat()
|
|
{
|
|
cat | sed 's/^[ ]*|//'
|
|
}
|
|
|
|
ytools_waitpid()
|
|
{
|
|
local pid=$1
|
|
local t=5
|
|
[ "$2" ] && t=$2
|
|
|
|
while ((t > 0)); do
|
|
[ -f /proc/$pid/status ] || {
|
|
wait $pid
|
|
return 0
|
|
}
|
|
sleep 1
|
|
((t -= 1))
|
|
done
|
|
return 1
|
|
}
|
|
|
|
ytools_terminate()
|
|
{
|
|
local pid
|
|
for pid in $@; do
|
|
[ -f /proc/$pid/status ] && {
|
|
echo terminating process $pid
|
|
kill $pid
|
|
ytools_waitpid $pid || {
|
|
|
|
echo "failed to normally terminate process $pid, killing it" >&2
|
|
kill -9 $pid
|
|
|
|
ytools_waitpid $pid || {
|
|
echo "failed to kill process $pid" >&2
|
|
return 1
|
|
}
|
|
}
|
|
}
|
|
done
|
|
return 0
|
|
}
|
|
|