Tamarin/lib/util.sh

92 lines
1.6 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2015-08-05 15:49:04 +02:00
HOOKS_DIR="${BASE_DIR}/hooks"
OPT_FILE="${BASE_DIR}/tmp/.tamarin_opts"
2015-09-23 21:21:30 +02:00
OPT_PREFIX="tamarin_opt_"
2015-08-04 23:58:43 +02:00
function info {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log INFO "${str}"
done
else
log INFO "$@"
fi
}
function debug {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log DEBUG "${str}"
done
else
log DEBUG "$@"
fi
}
function error {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log ERROR "${str}" >&2
done
else
log ERROR "$@" >&2
fi
}
2015-07-04 18:45:36 +02:00
function fatal {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log FATAL "${str}" >&2
done
else
log FATAL "$@" >&2
fi
exit 1
}
2015-08-04 23:58:43 +02:00
2015-08-05 15:49:04 +02:00
function log {
local args=( $@ )
echo "[${HOSTNAME}] [${args[0]}] ${args[@]:1}"
}
2015-08-04 23:58:43 +02:00
function get_opt {
2015-09-23 21:21:30 +02:00
local opt_name=${OPT_PREFIX}${1}
2015-08-05 15:49:04 +02:00
local default_value=${2}
touch "${OPT_FILE}"
source "${OPT_FILE}"
echo ${!opt_name:-${default_value}}
2015-08-04 23:58:43 +02:00
}
function set_opt {
2015-08-05 15:49:04 +02:00
local opt_name=${1}
local opt_value=${2}
touch "${OPT_FILE}"
2015-09-23 21:21:30 +02:00
sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}"
echo "${OPT_PREFIX}${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}"
2015-08-05 15:49:04 +02:00
}
function exec_hooks {
local hook=${1}
local workspace=${2}
local hook_scripts=$( find "${HOOKS_DIR}" -type f -name "*${hook}" -executable | sort )
2015-08-05 15:49:04 +02:00
for hook_script in ${hook_scripts}; do
info "[${hook}] Executing ${hook_script}"
( cd "${workspace}" && "${hook_script}" ) 2> >(error) 1> >(info)
# If the script did not execute properly, we stop here
if [ $? != 0 ]; then
fatal "The '${hook_script}' hook script did not finished properly !"
fi
info "[${hook}] ${hook_script} Done."
done
2015-08-04 23:58:43 +02:00
}