interactive hooks + add verbose mode + shellcheck fixes
This commit is contained in:
parent
b9da112b6d
commit
997eff6ca7
|
@ -62,6 +62,7 @@ get_end_date() {
|
||||||
get_now() {
|
get_now() {
|
||||||
date +"%Y-%m-%dT%H:%M:%S%z"
|
date +"%Y-%m-%dT%H:%M:%S%z"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_complete_hostname() {
|
get_complete_hostname() {
|
||||||
REAL_HOSTNAME=$(get_fqdn)
|
REAL_HOSTNAME=$(get_fqdn)
|
||||||
if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then
|
if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then
|
||||||
|
@ -70,6 +71,7 @@ get_complete_hostname() {
|
||||||
echo "${HOSTNAME} (${REAL_HOSTNAME})"
|
echo "${HOSTNAME} (${REAL_HOSTNAME})"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
get_repository_status() {
|
get_repository_status() {
|
||||||
dir=$1
|
dir=$1
|
||||||
# tell Git where to find the repository and the work tree (no need to `cd …` there)
|
# tell Git where to find the repository and the work tree (no need to `cd …` there)
|
||||||
|
@ -116,9 +118,10 @@ hook_commit() {
|
||||||
unset GIT_DIR GIT_WORK_TREE
|
unset GIT_DIR GIT_WORK_TREE
|
||||||
done
|
done
|
||||||
if [ -n "${GIT_COMMITS}" ]; then
|
if [ -n "${GIT_COMMITS}" ]; then
|
||||||
if [ "${DRY_RUN}" = "1" ]; then
|
if [ "${VERBOSE}" = "1" ]; then
|
||||||
echo "\n\n********** Commits ****************\n${GIT_COMMITS}\n***********************************"
|
echo "\n\n********** Commits ****************\n${GIT_COMMITS}\n***********************************"
|
||||||
else
|
fi
|
||||||
|
if [ "${DRY_RUN}" != "1" ]; then
|
||||||
echo "${GIT_COMMITS}" >> "${LOGFILE}"
|
echo "${GIT_COMMITS}" >> "${LOGFILE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
@ -129,9 +132,10 @@ hook_db() {
|
||||||
SQL_DETAILS=$(echo "${MESSAGE}" | sed "s/'/''/g")
|
SQL_DETAILS=$(echo "${MESSAGE}" | sed "s/'/''/g")
|
||||||
PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_DETAILS}')"
|
PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_DETAILS}')"
|
||||||
|
|
||||||
if [ "${DRY_RUN}" = "1" ]; then
|
if [ "${VERBOSE}" = "1" ]; then
|
||||||
echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************"
|
echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************"
|
||||||
else
|
fi
|
||||||
|
if [ "${DRY_RUN}" != "1" ]; then
|
||||||
echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST}
|
echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
@ -139,16 +143,22 @@ hook_db() {
|
||||||
hook_mail() {
|
hook_mail() {
|
||||||
MAIL_TEXTE=$(echo "${MESSAGE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
|
MAIL_TEXTE=$(echo "${MESSAGE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
|
||||||
MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
|
MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@")
|
||||||
MAIL_CONTENT=$(cat /usr/share/scripts/evomaintenance.tpl | \
|
MAIL_CONTENT=$(sed -e "s/__TO__/${EVOMAINTMAIL}/ ; s/__HOSTNAME__/${HOSTNAME_TEXT}/ ; s/__USER__/${USER}/ ; s/__BEGIN_DATE__/${BEGIN_DATE}/ ; s/__END_DATE__/${END_DATE}/ ; s/__GIT_COMMITS__/${MAIL_GIT_COMMITS}/ ; s/__TEXTE__/${MAIL_TEXTE}/ ; s/__IP__/${IP}/ ; s/__FULLFROM__/${FULLFROM}/ ; s/__FROM__/${FROM}/ ; s/__URGENCYFROM__/${URGENCYFROM}/ ; s/__URGENCYTEL__/${URGENCYTEL}/" /usr/share/scripts/evomaintenance.tpl)
|
||||||
sed -e "s/__TO__/${EVOMAINTMAIL}/ ; s/__HOSTNAME__/${HOSTNAME_TEXT}/ ; s/__USER__/${USER}/ ; s/__BEGIN_DATE__/${BEGIN_DATE}/ ; s/__END_DATE__/${END_DATE}/ ; s/__GIT_COMMITS__/${MAIL_GIT_COMMITS}/ ; s/__TEXTE__/${MAIL_TEXTE}/ ; s/__IP__/${IP}/ ; s/__FULLFROM__/${FULLFROM}/ ; s/__FROM__/${FROM}/ ; s/__URGENCYFROM__/${URGENCYFROM}/ ; s/__URGENCYTEL__/${URGENCYTEL}/")
|
|
||||||
|
|
||||||
if [ "${DRY_RUN}" = "1" ]; then
|
if [ "${VERBOSE}" = "1" ]; then
|
||||||
echo "\n\n********** Mail *******************\n${MAIL_CONTENT}\n***********************************"
|
echo "\n\n********** Mail *******************\n${MAIL_CONTENT}\n***********************************"
|
||||||
else
|
fi
|
||||||
|
if [ "${DRY_RUN}" != "1" ]; then
|
||||||
echo "${MAIL_CONTENT}" | ${SENDMAIL_BIN} -oi -t -f ${FROM}
|
echo "${MAIL_CONTENT}" | ${SENDMAIL_BIN} -oi -t -f ${FROM}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hook_log() {
|
||||||
|
echo "----------- $(get_now) ---------------" >> "${LOGFILE}"
|
||||||
|
echo "${BLOB}" >> "${LOGFILE}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# load configuration if present.
|
||||||
test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
|
test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
|
||||||
|
|
||||||
[ -n "${HOSTNAME}" ] || HOSTNAME=$(get_fqdn)
|
[ -n "${HOSTNAME}" ] || HOSTNAME=$(get_fqdn)
|
||||||
|
@ -159,6 +169,10 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
|
||||||
[ -n "${OPT_DB}" ] || OPT_DB=1
|
[ -n "${OPT_DB}" ] || OPT_DB=1
|
||||||
[ -n "${OPT_MAIL}" ] || OPT_MAIL=1
|
[ -n "${OPT_MAIL}" ] || OPT_MAIL=1
|
||||||
[ -n "${DRY_RUN}" ] || DRY_RUN=0
|
[ -n "${DRY_RUN}" ] || DRY_RUN=0
|
||||||
|
[ -n "${VERBOSE}" ] || VERBOSE=0
|
||||||
|
|
||||||
|
# initialize variable
|
||||||
|
MESSAGE=""
|
||||||
|
|
||||||
# Parse options
|
# Parse options
|
||||||
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
|
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
|
||||||
|
@ -168,7 +182,8 @@ while :; do
|
||||||
# show_help
|
# show_help
|
||||||
# exit
|
# exit
|
||||||
# ;;
|
# ;;
|
||||||
-m|--message) # Takes an option argument, ensuring it has been specified.
|
-m|--message)
|
||||||
|
# message options, with value speparated by space
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
MESSAGE=$2
|
MESSAGE=$2
|
||||||
shift
|
shift
|
||||||
|
@ -178,54 +193,79 @@ while :; do
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
--message=?*)
|
--message=?*)
|
||||||
MESSAGE=${1#*=} # Delete everything up to "=" and assign the remainder.
|
# message options, with value speparated by =
|
||||||
|
MESSAGE=${1#*=}
|
||||||
;;
|
;;
|
||||||
--message=) # Handle the case of an empty --file=
|
--message=)
|
||||||
|
# message options, without value
|
||||||
printf 'ERROR: "--message" requires a non-empty option argument.\n' >&2
|
printf 'ERROR: "--message" requires a non-empty option argument.\n' >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
--no-commit) # Takes an option argument
|
--no-commit)
|
||||||
|
# disable commit hook
|
||||||
OPT_COMMIT=0
|
OPT_COMMIT=0
|
||||||
;;
|
;;
|
||||||
--commit) # Takes an option argument
|
--commit)
|
||||||
|
# enable commit hook
|
||||||
OPT_COMMIT=1
|
OPT_COMMIT=1
|
||||||
;;
|
;;
|
||||||
--no-db) # Takes an option argument
|
--no-db)
|
||||||
|
# disable DB hook
|
||||||
OPT_DB=0
|
OPT_DB=0
|
||||||
;;
|
;;
|
||||||
--db) # Takes an option argument
|
--db)
|
||||||
|
# enable DB hook
|
||||||
OPT_DB=1
|
OPT_DB=1
|
||||||
;;
|
;;
|
||||||
--no-mail) # Takes an option argument
|
--no-mail)
|
||||||
|
# disable mail hook
|
||||||
OPT_MAIL=0
|
OPT_MAIL=0
|
||||||
;;
|
;;
|
||||||
--mail) # Takes an option argument
|
--mail)
|
||||||
|
# enable mail hook
|
||||||
OPT_MAIL=1
|
OPT_MAIL=1
|
||||||
;;
|
;;
|
||||||
-n|--dry-run) # Takes an option argument
|
-n|--dry-run)
|
||||||
|
# disable actual commands
|
||||||
DRY_RUN=1
|
DRY_RUN=1
|
||||||
;;
|
;;
|
||||||
--) # End of all options.
|
-v|--verbose)
|
||||||
|
# print verbose information
|
||||||
|
VERBOSE=1
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
# End of all options.
|
||||||
shift
|
shift
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
-?*)
|
-?*)
|
||||||
|
# ignore unknown options
|
||||||
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
|
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
|
||||||
;;
|
;;
|
||||||
*) # Default case: If no more options then break out of the loop.
|
*)
|
||||||
|
# Default case: If no more options then break out of the loop.
|
||||||
break
|
break
|
||||||
esac
|
esac
|
||||||
|
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Print options
|
||||||
|
if [ "${VERBOSE}" = "1" ]; then
|
||||||
|
echo "********** Options ****************"
|
||||||
|
echo "MESSAGE: ${MESSAGE}"
|
||||||
|
echo "OPT_COMMIT: ${OPT_COMMIT}"
|
||||||
|
echo "OPT_DB: ${OPT_DB}"
|
||||||
|
echo "OPT_MAIL: ${OPT_MAIL}"
|
||||||
|
echo "DRY_RUN: ${DRY_RUN}"
|
||||||
|
echo "***********************************\n"
|
||||||
|
fi
|
||||||
|
|
||||||
# Treat unset variables as an error when substituting.
|
# Treat unset variables as an error when substituting.
|
||||||
# Only after this line, because some config variables might be missing.
|
# Only after this line, because some config variables might be missing.
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
# Gather information
|
# Gather information
|
||||||
MESSAGE=""
|
|
||||||
|
|
||||||
HOSTNAME_TEXT=$(get_complete_hostname)
|
HOSTNAME_TEXT=$(get_complete_hostname)
|
||||||
# TTY=$(get_tty)
|
# TTY=$(get_tty)
|
||||||
# WHO=$(get_who)
|
# WHO=$(get_who)
|
||||||
|
@ -241,9 +281,9 @@ GIT_BIN=$(command -v git)
|
||||||
|
|
||||||
GIT_REPOSITORIES="/etc /etc/bind"
|
GIT_REPOSITORIES="/etc /etc/bind"
|
||||||
|
|
||||||
# git statuses
|
# initialize variable
|
||||||
GIT_STATUSES=""
|
GIT_STATUSES=""
|
||||||
|
# git statuses
|
||||||
if test -x "${GIT_BIN}"; then
|
if test -x "${GIT_BIN}"; then
|
||||||
# loop on possible directories managed by GIT
|
# loop on possible directories managed by GIT
|
||||||
for dir in ${GIT_REPOSITORIES}; do
|
for dir in ${GIT_REPOSITORIES}; do
|
||||||
|
@ -254,24 +294,19 @@ if test -x "${GIT_BIN}"; then
|
||||||
fi
|
fi
|
||||||
unset RESULT
|
unset RESULT
|
||||||
done
|
done
|
||||||
|
|
||||||
if test -n "${GIT_STATUSES}"; then
|
|
||||||
echo "/!\ There are some uncommited changes. If you proceed, everything will be commited."
|
|
||||||
echo "${GIT_STATUSES}"
|
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# find out if running in interactive mode, or not
|
||||||
if [ -t 0 ]; then
|
if [ -t 0 ]; then
|
||||||
INTERACTIVE=1
|
INTERACTIVE=1
|
||||||
else
|
else
|
||||||
INTERACTIVE=0
|
INTERACTIVE=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "${MESSAGE}" -a "${INTERACTIVE}" = "1" ]; then
|
if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then
|
||||||
# get input from stdin
|
# get input from stdin
|
||||||
echo "> Please, enter details about your maintenance"
|
echo "> Please, enter details about your maintenance"
|
||||||
read MESSAGE
|
read -r MESSAGE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if test -z "${MESSAGE}"; then
|
if test -z "${MESSAGE}"; then
|
||||||
|
@ -279,7 +314,7 @@ if test -z "${MESSAGE}"; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# recapitulatif
|
# Display information
|
||||||
BLOB=$(cat <<END
|
BLOB=$(cat <<END
|
||||||
Host : $HOSTNAME_TEXT
|
Host : $HOSTNAME_TEXT
|
||||||
User : $USER
|
User : $USER
|
||||||
|
@ -289,26 +324,125 @@ End : $END_DATE
|
||||||
Message : $MESSAGE
|
Message : $MESSAGE
|
||||||
END
|
END
|
||||||
)
|
)
|
||||||
|
|
||||||
echo ""
|
|
||||||
echo "${BLOB}"
|
echo "${BLOB}"
|
||||||
echo ""
|
echo ""
|
||||||
echo "> Press <Enter> to submit, or <Ctrl+c> to cancel."
|
|
||||||
read enter
|
|
||||||
|
|
||||||
# write log
|
# Log hook
|
||||||
if [ "${DRY_RUN}" = "1" ]; then
|
if [ "${DRY_RUN}" != "1" ]; then
|
||||||
echo "\n\n********** Log ********************\n${BLOB}\n***********************************"
|
hook_log
|
||||||
else
|
fi
|
||||||
echo "----------- $(get_now) ---------------" >> "${LOGFILE}"
|
|
||||||
echo "${BLOB}" >> "${LOGFILE}"
|
# Commit hook
|
||||||
|
if [ "${INTERACTIVE}" = "1" ]; then
|
||||||
|
if test -n "${GIT_STATUSES}" && [ "${OPT_COMMIT}" = "1" ]; then
|
||||||
|
echo "/!\ There are some uncommited changes."
|
||||||
|
echo "${GIT_STATUSES}"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
y="Y"; n="n"
|
||||||
|
question="Do you want to commit the changes? [${y}${n}] "
|
||||||
|
answer=""
|
||||||
|
while true; do
|
||||||
|
echo "${question}"
|
||||||
|
read -r answer
|
||||||
|
case $answer in
|
||||||
|
[Yy] )
|
||||||
|
hook_commit;
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
[Nn] )
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
"" )
|
||||||
|
if [ "${OPT_COMMIT}" = "1" ]; then
|
||||||
|
hook_commit
|
||||||
|
fi
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
* )
|
||||||
|
echo "answer with a valid choice"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ "${OPT_COMMIT}" = "1" ]; then
|
||||||
|
hook_commit
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Database hook
|
||||||
|
if [ "${INTERACTIVE}" = "1" ]; then
|
||||||
|
if [ "${OPT_DB}" = "1" ]; then
|
||||||
|
y="Y"; n="n"
|
||||||
|
else
|
||||||
|
y="y"; n="N"
|
||||||
|
fi
|
||||||
|
question="\nDo you want to insert your message into the database? [${y}${n}] "
|
||||||
|
answer=""
|
||||||
|
while true; do
|
||||||
|
echo "${question}"
|
||||||
|
read -r answer
|
||||||
|
case $answer in
|
||||||
|
[Yy] )
|
||||||
|
hook_db;
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
[Nn] )
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
"" )
|
||||||
|
if [ "${OPT_DB}" = "1" ]; then
|
||||||
|
hook_db
|
||||||
|
fi
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
* )
|
||||||
|
echo "answer with a valid choice"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
if [ "${OPT_DB}" = "1" ]; then
|
||||||
|
hook_db
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Mail hook
|
||||||
|
if [ "${INTERACTIVE}" = "1" ]; then
|
||||||
|
if [ "${OPT_MAIL}" = "1" ]; then
|
||||||
|
y="Y"; n="n"
|
||||||
|
else
|
||||||
|
y="y"; n="N"
|
||||||
|
fi
|
||||||
|
question="Do you want to send an email? [${y}${n}] "
|
||||||
|
answer=""
|
||||||
|
while true; do
|
||||||
|
echo "${question}"
|
||||||
|
read -r answer
|
||||||
|
case $answer in
|
||||||
|
[Yy] )
|
||||||
|
hook_db;
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
[Nn] )
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
"" )
|
||||||
|
if [ "${OPT_MAIL}" = "1" ]; then
|
||||||
|
hook_mail
|
||||||
|
fi
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
* )
|
||||||
|
echo "answer with a valid choice"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
if [ "${OPT_MAIL}" = "1" ]; then
|
||||||
|
hook_mail
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Hooks
|
|
||||||
# git commit
|
|
||||||
[ "${OPT_COMMIT}" = "1" ] && hook_commit
|
|
||||||
# insert into PG
|
|
||||||
[ "${OPT_DB}" = "1" ] && hook_db
|
|
||||||
# send mail
|
|
||||||
[ "${OPT_MAIL}" = "1" ] && hook_mail
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue