From 5e0705391e8a37538d9da7bb2e181bd5c7fdffab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 22:21:44 +0100 Subject: [PATCH 01/61] whitespaces --- evomaintenance.sh | 88 +++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 1d22d71..4077acf 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -8,59 +8,59 @@ # Copyright 2007-2018 Gregory Colpart , Jérémy Lecour , Evolix get_system() { - uname -s + uname -s } get_fqdn() { - if [ "$(get_system)" = "Linux" ]; then - hostname --fqdn - elif [ "$(get_system)" = "OpenBSD" ]; then - hostname - else - echo "OS not detected!" - exit 1 - fi + if [ "$(get_system)" = "Linux" ]; then + hostname --fqdn + elif [ "$(get_system)" = "OpenBSD" ]; then + hostname + else + echo "OS not detected!" + exit 1 + fi } get_tty() { - if [ "$(get_system)" = "Linux" ]; then - ps -o tty= | tail -1 - elif [ "$(get_system)" = "OpenBSD" ]; then - env | grep SSH_TTY | cut -d"/" -f3 - else - echo "OS not detected!" - exit 1 - fi + if [ "$(get_system)" = "Linux" ]; then + ps -o tty= | tail -1 + elif [ "$(get_system)" = "OpenBSD" ]; then + env | grep SSH_TTY | cut -d"/" -f3 + else + echo "OS not detected!" + exit 1 + fi } get_who() { - who=$(LC_ALL=C who -m) + who=$(LC_ALL=C who -m) - if [ -n "${who}" ]; then - echo "${who}" - else - LC_ALL=C who | grep $(get_tty) | tr -s ' ' - fi + if [ -n "${who}" ]; then + echo "${who}" + else + LC_ALL=C who | grep $(get_tty) | tr -s ' ' + fi } get_begin_date() { - echo "$(date "+%Y") $(echo $(get_who) | cut -d" " -f3,4,5)" + echo "$(date "+%Y") $(echo $(get_who) | cut -d" " -f3,4,5)" } get_ip() { - ip=$(echo $(get_who) | cut -d" " -f6 | sed -e "s/^(// ; s/)$//") - [ -z "${ip}" ] && ip="unknown (no tty)" - [ "${ip}" = ":0" ] && ip="localhost" + ip=$(echo $(get_who) | cut -d" " -f6 | sed -e "s/^(// ; s/)$//") + [ -z "${ip}" ] && ip="unknown (no tty)" + [ "${ip}" = ":0" ] && ip="localhost" - echo "${ip}" + echo "${ip}" } get_end_date() { - date +"%Y %b %d %H:%M" + date +"%Y %b %d %H:%M" } get_now() { - date +"%Y-%m-%dT%H:%M:%S%z" + date +"%Y-%m-%dT%H:%M:%S%z" } test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf @@ -106,18 +106,18 @@ if test -x "${GIT_BIN}"; then if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then - STATUS=$(${GIT_BIN} status --short | tail -n 10) - # append diff data, without empty lines - GIT_STATUSES=$(printf "%s\n%s\n%s\n" "${GIT_STATUSES}" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d') + STATUS=$(${GIT_BIN} status --short | tail -n 10) + # append diff data, without empty lines + GIT_STATUSES=$(printf "%s\n%s\n%s\n" "${GIT_STATUSES}" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d') fi fi # unset environment variables to prevent accidental influence on other git commands unset GIT_DIR GIT_WORK_TREE done if [ -n "${GIT_STATUSES}" ]; then - echo "/!\ There are some uncommited changes. If you proceed, everything will be commited." - echo "${GIT_STATUSES}" - echo "" + echo "/!\ There are some uncommited changes. If you proceed, everything will be commited." + echo "${GIT_STATUSES}" + echo "" fi fi @@ -163,20 +163,20 @@ if test -x "${GIT_BIN}"; then if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then - ${GIT_BIN} add --all - ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet - # Add the SHA to the log file if something has been committed - SHA=$(${GIT_BIN} rev-parse --short HEAD) - STATS=$(${GIT_BIN} show --stat | tail -1) - # append commit data, without empty lines - GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') + ${GIT_BIN} add --all + ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet + # Add the SHA to the log file if something has been committed + SHA=$(${GIT_BIN} rev-parse --short HEAD) + STATS=$(${GIT_BIN} show --stat | tail -1) + # append commit data, without empty lines + GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') fi fi # unset environment variables to prevent accidental influence on other git commands unset GIT_DIR GIT_WORK_TREE done if [ -n "${GIT_COMMITS}" ]; then - echo "${GIT_COMMITS}" >> "${LOGFILE}" + echo "${GIT_COMMITS}" >> "${LOGFILE}" fi fi From 3dba4f7fc11e1387f4e9f8c71acf96c8a62f4b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 22:32:58 +0100 Subject: [PATCH 02/61] extract functions --- evomaintenance.sh | 92 +++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 4077acf..c4e0efc 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -63,12 +63,63 @@ get_now() { date +"%Y-%m-%dT%H:%M:%S%z" } +commit_repositories() { + GIT_COMMITS="" + + if test -x "${GIT_BIN}"; then + # loop on possible directories managed by GIT + for dir in ${GIT_REPOSITORIES}; do + # tell Git where to find the repository and the work tree (no need to `cd …` there) + export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" + # If the repository and the work tree exist, try to commit changes + if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then + CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') + if [ "${CHANGED_LINES}" != "0" ]; then + ${GIT_BIN} add --all + ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet + # Add the SHA to the log file if something has been committed + SHA=$(${GIT_BIN} rev-parse --short HEAD) + STATS=$(${GIT_BIN} show --stat | tail -1) + # append commit data, without empty lines + GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') + fi + fi + # unset environment variables to prevent accidental influence on other git commands + unset GIT_DIR GIT_WORK_TREE + done + if [ -n "${GIT_COMMITS}" ]; then + echo "${GIT_COMMITS}" >> "${LOGFILE}" + fi + fi +} + +save_db() { + # SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` + SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/''/g"` + + PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}')" + echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} +} + +send_mail() { + MAIL_TEXTE=$(echo "${TEXTE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") + MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") + + cat /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}/" | \ + ${SENDMAIL_BIN} -oi -t -f ${FROM} +} + test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -n "${HOSTNAME}" ] || HOSTNAME=$(get_fqdn) [ -n "${EVOMAINTMAIL}" ] || EVOMAINTMAIL=evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM} [ -n "${LOGFILE}" ] || LOGFILE=/var/log/evomaintenance.log +[ -n "${OPT_COMMIT}" ] || OPT_COMMIT=1 +[ -n "${OPT_DB}" ] || OPT_DB=1 +[ -n "${OPT_MAIL}" ] || OPT_MAIL=1 + # Treat unset variables as an error when substituting. # Only after this line, because some config variables might be missing. set -u @@ -152,47 +203,12 @@ echo "----------- $(get_now) ---------------" >> "${LOGFILE}" echo "${BLOB}" >> "${LOGFILE}" # git commit -GIT_COMMITS="" - -if test -x "${GIT_BIN}"; then - # loop on possible directories managed by GIT - for dir in ${GIT_REPOSITORIES}; do - # tell Git where to find the repository and the work tree (no need to `cd …` there) - export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" - # If the repository and the work tree exist, try to commit changes - if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then - CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') - if [ "${CHANGED_LINES}" != "0" ]; then - ${GIT_BIN} add --all - ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet - # Add the SHA to the log file if something has been committed - SHA=$(${GIT_BIN} rev-parse --short HEAD) - STATS=$(${GIT_BIN} show --stat | tail -1) - # append commit data, without empty lines - GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') - fi - fi - # unset environment variables to prevent accidental influence on other git commands - unset GIT_DIR GIT_WORK_TREE - done - if [ -n "${GIT_COMMITS}" ]; then - echo "${GIT_COMMITS}" >> "${LOGFILE}" - fi -fi +[ "${OPT_COMMIT}" = "1" ] && commit_repositories # insert into PG -# SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` -SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/''/g"` - -PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}')" -echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} +[ "${OPT_DB}" = "1" ] && save_db # send mail -MAIL_TEXTE=$(echo "${TEXTE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") -MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") - -cat /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}/" | \ - ${SENDMAIL_BIN} -oi -t -f ${FROM} +[ "${OPT_MAIL}" = "1" ] && send_mail exit 0 From d26aea7ab29af74df36e1cf5f2ee699c34fa6dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 22:50:18 +0100 Subject: [PATCH 03/61] Parse options --- evomaintenance.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/evomaintenance.sh b/evomaintenance.sh index c4e0efc..1507567 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -120,10 +120,50 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -n "${OPT_DB}" ] || OPT_DB=1 [ -n "${OPT_MAIL}" ] || OPT_MAIL=1 +# Parse options +while :; do + case $1 in + # -h|-\?|--help) # Call a "show_help" function to display a synopsis, then exit. + # show_help + # exit + # ;; + --no-commit) # Takes an option argument + OPT_COMMIT=0 + ;; + --commit) # Takes an option argument + OPT_COMMIT=1 + ;; + --no-db) # Takes an option argument + OPT_DB=0 + ;; + --db) # Takes an option argument + OPT_DB=1 + ;; + --no-mail) # Takes an option argument + OPT_MAIL=0 + ;; + --mail) # Takes an option argument + OPT_MAIL=1 + ;; + --) # End of all options. + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) # Default case: If no more options then break out of the loop. + break + esac + + shift +done + # Treat unset variables as an error when substituting. # Only after this line, because some config variables might be missing. set -u +# Gather information REAL_HOSTNAME=$(get_fqdn) if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then HOSTNAME_TEXT="${HOSTNAME}" From e528a27048ae0bc6aab3ec32bf766fc8e7422875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:04:06 +0100 Subject: [PATCH 04/61] extract more functions --- evomaintenance.sh | 50 ++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 1507567..4825163 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -62,6 +62,31 @@ get_end_date() { get_now() { date +"%Y-%m-%dT%H:%M:%S%z" } +get_complete_hostname() { + REAL_HOSTNAME=$(get_fqdn) + if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then + echo "${HOSTNAME}" + else + echo "${HOSTNAME} (${REAL_HOSTNAME})" + fi +} +get_repository_status() { + dir=$1 + # tell Git where to find the repository and the work tree (no need to `cd …` there) + export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" + # If the repository and the work tree exist, try to commit changes + if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then + CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') + if [ "${CHANGED_LINES}" != "0" ]; then + STATUS=$(${GIT_BIN} status --short | tail -n 10) + # append diff data, without empty lines + RESULT=$(printf "%s\n%s\n" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d') + fi + fi + # unset environment variables to prevent accidental influence on other git commands + unset GIT_DIR GIT_WORK_TREE + echo "${RESULT}" +} commit_repositories() { GIT_COMMITS="" @@ -164,13 +189,7 @@ done set -u # Gather information -REAL_HOSTNAME=$(get_fqdn) -if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then - HOSTNAME_TEXT="${HOSTNAME}" -else - HOSTNAME_TEXT="${HOSTNAME} (${REAL_HOSTNAME})" -fi - +HOSTNAME_TEXT=$(get_complete_hostname) # TTY=$(get_tty) # WHO=$(get_who) IP=$(get_ip) @@ -191,21 +210,12 @@ GIT_STATUSES="" if test -x "${GIT_BIN}"; then # loop on possible directories managed by GIT for dir in ${GIT_REPOSITORIES}; do - # tell Git where to find the repository and the work tree (no need to `cd …` there) - export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" - # If the repository and the work tree exist, try to commit changes - if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then - CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') - if [ "${CHANGED_LINES}" != "0" ]; then - STATUS=$(${GIT_BIN} status --short | tail -n 10) - # append diff data, without empty lines - GIT_STATUSES=$(printf "%s\n%s\n%s\n" "${GIT_STATUSES}" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d') - fi + RESULT=$(get_repository_status "${dir}") + if test -n "${RESULT}"; then + GIT_STATUSES=$(printf "%s\n%s\n" "${GIT_STATUSES}" "${RESULT}" | sed -e '/^$/d') fi - # unset environment variables to prevent accidental influence on other git commands - unset GIT_DIR GIT_WORK_TREE done - if [ -n "${GIT_STATUSES}" ]; then + if test -n "${GIT_STATUSES}"; then echo "/!\ There are some uncommited changes. If you proceed, everything will be commited." echo "${GIT_STATUSES}" echo "" From 6b70d2416cb37a3dd468d1b777d8ec1cc0269e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:04:19 +0100 Subject: [PATCH 05/61] rename hook functions --- evomaintenance.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 4825163..179cbad 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -88,7 +88,7 @@ get_repository_status() { echo "${RESULT}" } -commit_repositories() { +hook_commit() { GIT_COMMITS="" if test -x "${GIT_BIN}"; then @@ -118,7 +118,7 @@ commit_repositories() { fi } -save_db() { +hook_db() { # SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/''/g"` @@ -126,7 +126,7 @@ save_db() { echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} } -send_mail() { +hook_mail() { MAIL_TEXTE=$(echo "${TEXTE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") @@ -253,12 +253,12 @@ echo "----------- $(get_now) ---------------" >> "${LOGFILE}" echo "${BLOB}" >> "${LOGFILE}" # git commit -[ "${OPT_COMMIT}" = "1" ] && commit_repositories +[ "${OPT_COMMIT}" = "1" ] && hook_commit # insert into PG -[ "${OPT_DB}" = "1" ] && save_db +[ "${OPT_DB}" = "1" ] && hook_db # send mail -[ "${OPT_MAIL}" = "1" ] && send_mail +[ "${OPT_MAIL}" = "1" ] && hook_mail exit 0 From b73bd72eacb86982093ea9286e22f3baae90d5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:07:25 +0100 Subject: [PATCH 06/61] Use $(...) notation instead of legacy backticked `...` --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 179cbad..96c8cf5 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -120,7 +120,7 @@ hook_commit() { hook_db() { # SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` - SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/''/g"` + SQL_TEXTE=$(echo "${TEXTE}" | sed "s/'/''/g") PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}')" echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} From 4e2d55ea5ea1dd107be53bfa5fbcc5723083b3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:20:31 +0100 Subject: [PATCH 07/61] fix git diff append --- evomaintenance.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 96c8cf5..ce05a78 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -79,13 +79,11 @@ get_repository_status() { CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then STATUS=$(${GIT_BIN} status --short | tail -n 10) - # append diff data, without empty lines - RESULT=$(printf "%s\n%s\n" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d') + printf "%s\n%s\n" "${GIT_DIR} (last 10 lines)" "${STATUS}" | sed -e '/^$/d' fi fi # unset environment variables to prevent accidental influence on other git commands unset GIT_DIR GIT_WORK_TREE - echo "${RESULT}" } hook_commit() { @@ -212,9 +210,12 @@ if test -x "${GIT_BIN}"; then for dir in ${GIT_REPOSITORIES}; do RESULT=$(get_repository_status "${dir}") if test -n "${RESULT}"; then + # append diff data, without empty lines GIT_STATUSES=$(printf "%s\n%s\n" "${GIT_STATUSES}" "${RESULT}" | sed -e '/^$/d') fi + unset RESULT done + if test -n "${GIT_STATUSES}"; then echo "/!\ There are some uncommited changes. If you proceed, everything will be commited." echo "${GIT_STATUSES}" From d9fedcdbcfa09d72282b95e22fc2f6bc741028b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:20:53 +0100 Subject: [PATCH 08/61] use test instead of [] --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index ce05a78..65eb50b 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -227,7 +227,7 @@ fi echo "> Please, enter details about your maintenance" read TEXTE -if [ "${TEXTE}" = "" ]; then +if test -z "${TEXTE}"; then echo "no value..." exit 1 fi From 463555475b762ce3701cceef9ccdf559495851ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Thu, 7 Mar 2019 23:26:24 +0100 Subject: [PATCH 09/61] Add dry-run option --- evomaintenance.sh | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 65eb50b..a1075d8 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -142,6 +142,7 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -n "${OPT_COMMIT}" ] || OPT_COMMIT=1 [ -n "${OPT_DB}" ] || OPT_DB=1 [ -n "${OPT_MAIL}" ] || OPT_MAIL=1 +[ -n "${DRY_RUN}" ] || DRY_RUN=0 # Parse options while :; do @@ -168,6 +169,9 @@ while :; do --mail) # Takes an option argument OPT_MAIL=1 ;; + -n|--dry-run) # Takes an option argument + DRY_RUN=1 + ;; --) # End of all options. shift break @@ -253,13 +257,21 @@ read enter echo "----------- $(get_now) ---------------" >> "${LOGFILE}" echo "${BLOB}" >> "${LOGFILE}" -# git commit -[ "${OPT_COMMIT}" = "1" ] && hook_commit +if [ "${DRY_RUN}" = "1" ]; then + echo + echo "[DRY-RUN] Options:" + echo "OPT_COMMIT: ${OPT_COMMIT}" + echo "OPT_DB: ${OPT_DB}" + echo "OPT_MAIL: ${OPT_MAIL}" -# insert into PG -[ "${OPT_DB}" = "1" ] && hook_db + exit 2 +else + # git commit + [ "${OPT_COMMIT}" = "1" ] && hook_commit + # insert into PG + [ "${OPT_DB}" = "1" ] && hook_db + # send mail + [ "${OPT_MAIL}" = "1" ] && hook_mail -# send mail -[ "${OPT_MAIL}" = "1" ] && hook_mail - -exit 0 + exit 0 +fi From 293c38fb83da0219c1b6359f412bcce0fd3b385d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 13:48:38 +0100 Subject: [PATCH 10/61] Improve "dry run" mode Hooks are executed, but they show the actions instead of doing them. --- evomaintenance.sh | 73 +++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index a1075d8..e37f366 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -98,20 +98,29 @@ hook_commit() { if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then - ${GIT_BIN} add --all - ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet - # Add the SHA to the log file if something has been committed - SHA=$(${GIT_BIN} rev-parse --short HEAD) - STATS=$(${GIT_BIN} show --stat | tail -1) - # append commit data, without empty lines - GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') + if [ "${DRY_RUN}" = "1" ]; then + STATS=$(${GIT_BIN} diff --stat | tail -1) + GIT_COMMITS=$(printf "%s\n%s : %s" "${GIT_COMMITS}" "${GIT_DIR}" "${STATS}" | sed -e '/^$/d') + else + ${GIT_BIN} add --all + ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet + # Add the SHA to the log file if something has been committed + SHA=$(${GIT_BIN} rev-parse --short HEAD) + STATS=$(${GIT_BIN} show --stat | tail -1) + # append commit data, without empty lines + GIT_COMMITS=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') + fi fi fi # unset environment variables to prevent accidental influence on other git commands unset GIT_DIR GIT_WORK_TREE done if [ -n "${GIT_COMMITS}" ]; then - echo "${GIT_COMMITS}" >> "${LOGFILE}" + if [ "${DRY_RUN}" = "1" ]; then + echo "\n\n********** Commits ****************\n${GIT_COMMITS}\n***********************************" + else + echo "${GIT_COMMITS}" >> "${LOGFILE}" + fi fi fi } @@ -119,18 +128,26 @@ hook_commit() { hook_db() { # SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` SQL_TEXTE=$(echo "${TEXTE}" | sed "s/'/''/g") - PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}')" - echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} + + if [ "${DRY_RUN}" = "1" ]; then + echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************" + else + echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} + fi } hook_mail() { MAIL_TEXTE=$(echo "${TEXTE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") + MAIL_CONTENT=$(cat /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}/") - cat /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}/" | \ - ${SENDMAIL_BIN} -oi -t -f ${FROM} + if [ "${DRY_RUN}" = "1" ]; then + echo "\n\n********** Mail *******************\n${MAIL_CONTENT}\n***********************************" + else + echo "${MAIL_CONTENT}" | ${SENDMAIL_BIN} -oi -t -f ${FROM} + fi } test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf @@ -254,24 +271,18 @@ echo "> Press to submit, or to cancel." read enter # write log -echo "----------- $(get_now) ---------------" >> "${LOGFILE}" -echo "${BLOB}" >> "${LOGFILE}" - if [ "${DRY_RUN}" = "1" ]; then - echo - echo "[DRY-RUN] Options:" - echo "OPT_COMMIT: ${OPT_COMMIT}" - echo "OPT_DB: ${OPT_DB}" - echo "OPT_MAIL: ${OPT_MAIL}" - - exit 2 + echo "\n\n********** Log ********************\n${BLOB}\n***********************************" else - # git commit - [ "${OPT_COMMIT}" = "1" ] && hook_commit - # insert into PG - [ "${OPT_DB}" = "1" ] && hook_db - # send mail - [ "${OPT_MAIL}" = "1" ] && hook_mail - - exit 0 + echo "----------- $(get_now) ---------------" >> "${LOGFILE}" + echo "${BLOB}" >> "${LOGFILE}" 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 From b9da112b6d71c07f96833d4487968866b8fd1dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 14:01:38 +0100 Subject: [PATCH 11/61] Add real interactive mode The message can be given as an option, as stdin or interactively. --- evomaintenance.sh | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index e37f366..8c6faf8 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -103,7 +103,7 @@ hook_commit() { GIT_COMMITS=$(printf "%s\n%s : %s" "${GIT_COMMITS}" "${GIT_DIR}" "${STATS}" | sed -e '/^$/d') else ${GIT_BIN} add --all - ${GIT_BIN} commit --message "${TEXTE}" --author="${USER} <${USER}@evolix.net>" --quiet + ${GIT_BIN} commit --message "${MESSAGE}" --author="${USER} <${USER}@evolix.net>" --quiet # Add the SHA to the log file if something has been committed SHA=$(${GIT_BIN} rev-parse --short HEAD) STATS=$(${GIT_BIN} show --stat | tail -1) @@ -126,9 +126,8 @@ hook_commit() { } hook_db() { - # SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"` - SQL_TEXTE=$(echo "${TEXTE}" | sed "s/'/''/g") - PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_TEXTE}')" + 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}')" if [ "${DRY_RUN}" = "1" ]; then echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************" @@ -138,7 +137,7 @@ hook_db() { } hook_mail() { - MAIL_TEXTE=$(echo "${TEXTE}" | 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_CONTENT=$(cat /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}/") @@ -162,12 +161,29 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -n "${DRY_RUN}" ] || DRY_RUN=0 # Parse options +# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a while :; do case $1 in # -h|-\?|--help) # Call a "show_help" function to display a synopsis, then exit. # show_help # exit # ;; + -m|--message) # Takes an option argument, ensuring it has been specified. + if [ -n "$2" ]; then + MESSAGE=$2 + shift + else + printf 'ERROR: "--message" requires a non-empty option argument.\n' >&2 + exit 1 + fi + ;; + --message=?*) + MESSAGE=${1#*=} # Delete everything up to "=" and assign the remainder. + ;; + --message=) # Handle the case of an empty --file= + printf 'ERROR: "--message" requires a non-empty option argument.\n' >&2 + exit 1 + ;; --no-commit) # Takes an option argument OPT_COMMIT=0 ;; @@ -208,6 +224,8 @@ done set -u # Gather information +MESSAGE="" + HOSTNAME_TEXT=$(get_complete_hostname) # TTY=$(get_tty) # WHO=$(get_who) @@ -244,11 +262,19 @@ if test -x "${GIT_BIN}"; then fi fi -# get input from stdin -echo "> Please, enter details about your maintenance" -read TEXTE +if [ -t 0 ]; then + INTERACTIVE=1 +else + INTERACTIVE=0 +fi -if test -z "${TEXTE}"; then +if [ -z "${MESSAGE}" -a "${INTERACTIVE}" = "1" ]; then + # get input from stdin + echo "> Please, enter details about your maintenance" + read MESSAGE +fi + +if test -z "${MESSAGE}"; then echo "no value..." exit 1 fi @@ -260,7 +286,7 @@ User : $USER IP : $IP Begin : $BEGIN_DATE End : $END_DATE -Message : $TEXTE +Message : $MESSAGE END ) From 997eff6ca7209c35b745e07cfee07d69c99cf7a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 20:56:53 +0100 Subject: [PATCH 12/61] interactive hooks + add verbose mode + shellcheck fixes --- evomaintenance.sh | 234 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 184 insertions(+), 50 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 8c6faf8..f62d033 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -62,6 +62,7 @@ get_end_date() { get_now() { date +"%Y-%m-%dT%H:%M:%S%z" } + get_complete_hostname() { REAL_HOSTNAME=$(get_fqdn) if [ "${HOSTNAME}" = "${REAL_HOSTNAME}" ]; then @@ -70,6 +71,7 @@ get_complete_hostname() { echo "${HOSTNAME} (${REAL_HOSTNAME})" fi } + get_repository_status() { dir=$1 # 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 done if [ -n "${GIT_COMMITS}" ]; then - if [ "${DRY_RUN}" = "1" ]; then + if [ "${VERBOSE}" = "1" ]; then echo "\n\n********** Commits ****************\n${GIT_COMMITS}\n***********************************" - else + fi + if [ "${DRY_RUN}" != "1" ]; then echo "${GIT_COMMITS}" >> "${LOGFILE}" fi fi @@ -129,9 +132,10 @@ hook_db() { 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}')" - if [ "${DRY_RUN}" = "1" ]; then + if [ "${VERBOSE}" = "1" ]; then echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************" - else + fi + if [ "${DRY_RUN}" != "1" ]; then echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} fi } @@ -139,16 +143,22 @@ hook_db() { hook_mail() { MAIL_TEXTE=$(echo "${MESSAGE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") - MAIL_CONTENT=$(cat /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}/") + 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) - if [ "${DRY_RUN}" = "1" ]; then + if [ "${VERBOSE}" = "1" ]; then echo "\n\n********** Mail *******************\n${MAIL_CONTENT}\n***********************************" - else + fi + if [ "${DRY_RUN}" != "1" ]; then echo "${MAIL_CONTENT}" | ${SENDMAIL_BIN} -oi -t -f ${FROM} fi } +hook_log() { + echo "----------- $(get_now) ---------------" >> "${LOGFILE}" + echo "${BLOB}" >> "${LOGFILE}" +} + +# load configuration if present. test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -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_MAIL}" ] || OPT_MAIL=1 [ -n "${DRY_RUN}" ] || DRY_RUN=0 +[ -n "${VERBOSE}" ] || VERBOSE=0 + +# initialize variable +MESSAGE="" # Parse options # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a @@ -168,7 +182,8 @@ while :; do # show_help # 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 MESSAGE=$2 shift @@ -178,54 +193,79 @@ while :; do fi ;; --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 exit 1 ;; - --no-commit) # Takes an option argument + --no-commit) + # disable commit hook OPT_COMMIT=0 ;; - --commit) # Takes an option argument + --commit) + # enable commit hook OPT_COMMIT=1 ;; - --no-db) # Takes an option argument + --no-db) + # disable DB hook OPT_DB=0 ;; - --db) # Takes an option argument + --db) + # enable DB hook OPT_DB=1 ;; - --no-mail) # Takes an option argument + --no-mail) + # disable mail hook OPT_MAIL=0 ;; - --mail) # Takes an option argument + --mail) + # enable mail hook OPT_MAIL=1 ;; - -n|--dry-run) # Takes an option argument + -n|--dry-run) + # disable actual commands DRY_RUN=1 ;; - --) # End of all options. + -v|--verbose) + # print verbose information + VERBOSE=1 + ;; + --) + # End of all options. shift break ;; -?*) + # ignore unknown options 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 esac shift 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. # Only after this line, because some config variables might be missing. set -u # Gather information -MESSAGE="" - HOSTNAME_TEXT=$(get_complete_hostname) # TTY=$(get_tty) # WHO=$(get_who) @@ -241,9 +281,9 @@ GIT_BIN=$(command -v git) GIT_REPOSITORIES="/etc /etc/bind" -# git statuses +# initialize variable GIT_STATUSES="" - +# git statuses if test -x "${GIT_BIN}"; then # loop on possible directories managed by GIT for dir in ${GIT_REPOSITORIES}; do @@ -254,24 +294,19 @@ if test -x "${GIT_BIN}"; then fi unset RESULT 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 +# find out if running in interactive mode, or not if [ -t 0 ]; then INTERACTIVE=1 else INTERACTIVE=0 fi -if [ -z "${MESSAGE}" -a "${INTERACTIVE}" = "1" ]; then +if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then # get input from stdin echo "> Please, enter details about your maintenance" - read MESSAGE + read -r MESSAGE fi if test -z "${MESSAGE}"; then @@ -279,7 +314,7 @@ if test -z "${MESSAGE}"; then exit 1 fi -# recapitulatif +# Display information BLOB=$(cat < Press to submit, or to cancel." -read enter -# write log -if [ "${DRY_RUN}" = "1" ]; then - echo "\n\n********** Log ********************\n${BLOB}\n***********************************" -else - echo "----------- $(get_now) ---------------" >> "${LOGFILE}" - echo "${BLOB}" >> "${LOGFILE}" +# Log hook +if [ "${DRY_RUN}" != "1" ]; then + hook_log +fi + +# 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 -# 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 From c2b44f0e4886caa015fb7d2003e94d41d7b53ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 21:42:05 +0100 Subject: [PATCH 13/61] extract functions --- evomaintenance.sh | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index f62d033..0f07442 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -88,6 +88,30 @@ get_repository_status() { unset GIT_DIR GIT_WORK_TREE } +print_summary() { + BLOB=$(cat < Date: Sat, 9 Mar 2019 21:59:17 +0100 Subject: [PATCH 14/61] The mail recipient can be modified on the fly --- evomaintenance.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 0f07442..d3b6237 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -421,10 +421,9 @@ if [ "${INTERACTIVE}" = "1" ]; then else y="y"; n="N" fi - question="Do you want to send an email? [${y}${n}] " answer="" while true; do - echo "${question}" + echo "Do you want to send an email to <${EVOMAINTMAIL}>? [${y}${n}e] " read -r answer case $answer in [Yy] ) @@ -434,6 +433,14 @@ if [ "${INTERACTIVE}" = "1" ]; then [Nn] ) break ;; + [Ee] ) + echo "To: [${EVOMAINTMAIL}] " + read -r mail_recipient + if [ -n "${mail_recipient}" ]; then + EVOMAINTMAIL="${mail_recipient}" + echo "changed to ${EVOMAINTMAIL}" + fi + ;; "" ) if [ "${OPT_MAIL}" = "1" ]; then hook_mail From cf05263adb4a4fa67835614a6c3fad596aa9fea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 21:59:37 +0100 Subject: [PATCH 15/61] move questions in the while loop --- evomaintenance.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index d3b6237..ce31619 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -346,10 +346,9 @@ if [ "${INTERACTIVE}" = "1" ]; then echo "" y="Y"; n="n" - question="Do you want to commit the changes? [${y}${n}] " answer="" while true; do - echo "${question}" + echo "Do you want to commit the changes? [${y}${n}] " read -r answer case $answer in [Yy] ) @@ -384,10 +383,9 @@ if [ "${INTERACTIVE}" = "1" ]; then 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}" + echo "Do you want to insert your message into the database? [${y}${n}] " read -r answer case $answer in [Yy] ) From 44d83dd4742b7b67e14659f169e462a14d04896a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 22:03:09 +0100 Subject: [PATCH 16/61] fix hook_mail with for yes answer --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index ce31619..b7ac91f 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -425,7 +425,7 @@ if [ "${INTERACTIVE}" = "1" ]; then read -r answer case $answer in [Yy] ) - hook_db; + hook_mail; break ;; [Nn] ) From ca62314864ae37001cee7ab3d9a2ebace6cb5dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 22:03:25 +0100 Subject: [PATCH 17/61] initialize GIT_COMMITS variable --- evomaintenance.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index b7ac91f..1d86743 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -113,7 +113,6 @@ print_options() { } hook_commit() { - GIT_COMMITS="" if test -x "${GIT_BIN}"; then # loop on possible directories managed by GIT @@ -195,8 +194,9 @@ test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf [ -n "${DRY_RUN}" ] || DRY_RUN=0 [ -n "${VERBOSE}" ] || VERBOSE=0 -# initialize variable +# initialize variables MESSAGE="" +GIT_COMMITS="" # Parse options # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a From d01e79e832697ce9c4ee2f690184707f68cdcdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 9 Mar 2019 22:09:26 +0100 Subject: [PATCH 18/61] add a prompt indicator --- evomaintenance.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 1d86743..296d59c 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -348,7 +348,7 @@ if [ "${INTERACTIVE}" = "1" ]; then y="Y"; n="n" answer="" while true; do - echo "Do you want to commit the changes? [${y}${n}] " + echo "> Do you want to commit the changes? [${y}${n}] " read -r answer case $answer in [Yy] ) @@ -385,7 +385,7 @@ if [ "${INTERACTIVE}" = "1" ]; then fi answer="" while true; do - echo "Do you want to insert your message into the database? [${y}${n}] " + echo "> Do you want to insert your message into the database? [${y}${n}] " read -r answer case $answer in [Yy] ) @@ -421,7 +421,7 @@ if [ "${INTERACTIVE}" = "1" ]; then fi answer="" while true; do - echo "Do you want to send an email to <${EVOMAINTMAIL}>? [${y}${n}e] " + echo "> Do you want to send an email to <${EVOMAINTMAIL}>? [${y}${n}e] " read -r answer case $answer in [Yy] ) @@ -432,7 +432,7 @@ if [ "${INTERACTIVE}" = "1" ]; then break ;; [Ee] ) - echo "To: [${EVOMAINTMAIL}] " + echo "> To: [${EVOMAINTMAIL}] " read -r mail_recipient if [ -n "${mail_recipient}" ]; then EVOMAINTMAIL="${mail_recipient}" From 0c6123bb54eae1b67126b7d48d5ffe05d393004e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 00:31:28 +0100 Subject: [PATCH 19/61] Add "version" and "help" options --- evomaintenance.sh | 55 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 296d59c..f87878b 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -4,8 +4,47 @@ # Dependencies (all OS): git postgresql-client # Dependencies (Debian): sudo -# version 0.4.1 -# Copyright 2007-2018 Gregory Colpart , Jérémy Lecour , Evolix +# version 0.5.0.beta1 +# Copyright 2007-2019 Evolix , Gregory Colpart , +# Jérémy Lecour and others. + +VERSION="0.5.0.beta1" + +show_version() { + printf "%s\n" "evomaintenance version ${VERSION}" +} + +show_help() { + cat <, Gregory Colpart , + Jérémy Lecour and others. + +evomaintenance comes with ABSOLUTELY NO WARRANTY. This is free software, +and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. + +evomaintenance is a program that helps reporting what you've done on a server + +Usage: evomaintenance + or evomaintenance --message="add new host" + or evomaintenance --no-db --no-mail --no-commit + or echo "add new vhost | evomaintenance + +Options + -m, --message=MESSAGE set the message from the command line + --mail enable the mail hook + --no-mail disable the mail hook + --db enable the database hook + --no-db disable the database hook + --commit enable the commit hook + --no-commit disable the commit hook + -v, --verbose increase verbosity + -n, --dry-run actions are not executed + --help print this message and exit + --version print version and exit +END +} get_system() { uname -s @@ -202,10 +241,14 @@ GIT_COMMITS="" # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a while :; do case $1 in - # -h|-\?|--help) # Call a "show_help" function to display a synopsis, then exit. - # show_help - # exit - # ;; + -h|-\?|--help) + show_help + exit 0 + ;; + --version) + show_version + exit 0 + ;; -m|--message) # message options, with value speparated by space if [ -n "$2" ]; then From 6568e66f734bbdd2a70ee6830b057502b398b845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 00:34:33 +0100 Subject: [PATCH 20/61] Use printf instead of echo The behaviour of "echo" is unspecified when the string contains backslashes. --- evomaintenance.sh | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index f87878b..682a3f1 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -137,18 +137,20 @@ End : $END_DATE Message : $MESSAGE END ) - echo "${BLOB}" - echo "" + printf "%s\n\n" "${BLOB}" } print_options() { - 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" + cat <> "${LOGFILE}" @@ -195,7 +197,7 @@ hook_db() { PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_DETAILS}')" if [ "${VERBOSE}" = "1" ]; then - echo "\n\n********** DB query **************\n${PG_QUERY}\n***********************************" + printf "\n\n********** DB query **************\n%s\n***********************************\n" "${PG_QUERY}" fi if [ "${DRY_RUN}" != "1" ]; then echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} @@ -208,10 +210,10 @@ hook_mail() { 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) if [ "${VERBOSE}" = "1" ]; then - echo "\n\n********** Mail *******************\n${MAIL_CONTENT}\n***********************************" + printf "\n\n********** Mail *******************\n%s\n***********************************\n" "${MAIL_CONTENT}" 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 } @@ -365,7 +367,7 @@ fi if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then # get input from stdin - echo "> Please, enter details about your maintenance" + printf "> Please, enter details about your maintenance:\n" read -r MESSAGE fi @@ -391,7 +393,7 @@ if [ "${INTERACTIVE}" = "1" ]; then y="Y"; n="n" answer="" while true; do - echo "> Do you want to commit the changes? [${y}${n}] " + printf "> Do you want to commit the changes? [%s] " "${y}${n}" read -r answer case $answer in [Yy] ) @@ -428,7 +430,7 @@ if [ "${INTERACTIVE}" = "1" ]; then fi answer="" while true; do - echo "> Do you want to insert your message into the database? [${y}${n}] " + printf "> Do you want to insert your message into the database? [%s] " "${y}${n}" read -r answer case $answer in [Yy] ) @@ -464,7 +466,7 @@ if [ "${INTERACTIVE}" = "1" ]; then fi answer="" while true; do - echo "> Do you want to send an email to <${EVOMAINTMAIL}>? [${y}${n}e] " + printf "> Do you want to send an email to <%s>? [%s] " "${EVOMAINTMAIL}" "${y}${n}e" read -r answer case $answer in [Yy] ) @@ -475,11 +477,10 @@ if [ "${INTERACTIVE}" = "1" ]; then break ;; [Ee] ) - echo "> To: [${EVOMAINTMAIL}] " + printf "> To: [%s] " "${EVOMAINTMAIL}" read -r mail_recipient if [ -n "${mail_recipient}" ]; then EVOMAINTMAIL="${mail_recipient}" - echo "changed to ${EVOMAINTMAIL}" fi ;; "" ) From e31e5c73df3e37b40b836f638c886d628a1d9b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 00:40:46 +0100 Subject: [PATCH 21/61] quote PG variables --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 682a3f1..e768094 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -200,7 +200,7 @@ hook_db() { printf "\n\n********** DB query **************\n%s\n***********************************\n" "${PG_QUERY}" fi if [ "${DRY_RUN}" != "1" ]; then - echo "${PG_QUERY}" | psql ${PGDB} ${PGTABLE} -h ${PGHOST} + echo "${PG_QUERY}" | psql "${PGDB}" "${PGTABLE}" -h "${PGHOST}" fi } From 434536b1e9ceb45a05c645f3534f939c05dce3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 00:42:25 +0100 Subject: [PATCH 22/61] Use multiple sed (internal) commands Instead of a single command with ultiple substitutions, we use multiple (internal) commands, for better error handling and line-splitting. --- evomaintenance.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index e768094..863e87e 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -207,7 +207,19 @@ hook_db() { hook_mail() { MAIL_TEXTE=$(echo "${MESSAGE}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") MAIL_GIT_COMMITS=$(echo "${GIT_COMMITS}" | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@") - 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) + MAIL_CONTENT=$(sed -e "s/__TO__/${EVOMAINTMAIL}/" \ + -e "s/__HOSTNAME__/${HOSTNAME_TEXT}/" \ + -e "s/__USER__/${USER}/" \ + -e "s/__BEGIN_DATE__/${BEGIN_DATE}/" \ + -e "s/__END_DATE__/${END_DATE}/" \ + -e "s/__GIT_COMMITS__/${MAIL_GIT_COMMITS}/" \ + -e "s/__TEXTE__/${MAIL_TEXTE}/" \ + -e "s/__IP__/${IP}/" \ + -e "s/__FULLFROM__/${FULLFROM}/" \ + -e "s/__FROM__/${FROM}/" \ + -e "s/__URGENCYFROM__/${URGENCYFROM}/" \ + -e "s/__URGENCYTEL__/${URGENCYTEL}/" \ + /usr/share/scripts/evomaintenance.tpl) if [ "${VERBOSE}" = "1" ]; then printf "\n\n********** Mail *******************\n%s\n***********************************\n" "${MAIL_CONTENT}" From 3ded64677fff0b9ebbd1e60d25a473c77b0c563f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 00:47:58 +0100 Subject: [PATCH 23/61] whitespace --- evomaintenance.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 863e87e..7837687 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -154,7 +154,6 @@ END } hook_commit() { - if test -x "${GIT_BIN}"; then # loop on possible directories managed by GIT for dir in ${GIT_REPOSITORIES}; do From 9f99b12cd4e57c87aa90ea6bd52f809ada3c702a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 11:16:57 +0100 Subject: [PATCH 24/61] update changelogs --- changelog | 1 + debian/changelog | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/changelog b/changelog index 163831c..042de3c 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,4 @@ +0.5.0 : options et mode interactif pour l'exécution des actions, meilleure compatibilité POSIX 0.4.1 : Utilisation de "printf" à la place de "echo" pour mieux gérer les sauts de ligne 0.4.0 : Amélioration de la récupération d'information (plus de cas gérés). Infos Git avant la saisie. 0.3.0 : Écriture dans un fichier de log, amélioration de la récupération d'informations, amélioration de la syntaxe shell diff --git a/debian/changelog b/debian/changelog index 020560c..c1b92c3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,14 @@ -evomaintenance (0.4.1-1) UNRELEASED; urgency=low +evomaintenance (0.5.0-1) UNRELEASED; urgency=low - * Use "printf" instead of "echo" for better line breaks support + * options and interactive mode for selective hooks execution, and better POSIX compliance - -- Jérémy Lecour Tue, 25 Sep 2018 11:54:12 +0200 + -- Jérémy Lecour Sun, 10 Mar 2019 11:13:12 +0100 + + evomaintenance (0.4.1-1) UNRELEASED; urgency=low + + * Use "printf" instead of "echo" for better line breaks support + + -- Jérémy Lecour Tue, 25 Sep 2018 11:54:12 +0200 evomaintenance (0.4.0-1) UNRELEASED; urgency=low @@ -33,7 +39,7 @@ evomaintenance (0.2.8-1) UNRELEASED; urgency=medium evomaintenance (0.2.7-1) UNRELEASED; urgency=low * New upstream release. - * This version fixes bug when text contains a character '&'. + * This version fixes bug when text contains a character '&'. -- Gregory Colpart Thu, 11 Mar 2010 16:14:19 +0100 @@ -41,7 +47,7 @@ evomaintenance (0.2.6-1) UNRELEASED; urgency=low * New upstream release. * This version fixes the bug for charset in mail. - * Add Build-Depends on debhelper. + * Add Build-Depends on debhelper. -- Gregory Colpart Mon, 01 Jun 2009 15:57:32 +0200 @@ -62,13 +68,13 @@ evomaintenance (0.2.3-1) UNRELEASED; urgency=low evomaintenance (0.2.2-1) UNRELEASED; urgency=low * New upstream release. - * Fix debian/rules to have correct permissions. + * Fix debian/rules to have correct permissions. -- Gregory Colpart Mon, 10 Nov 2008 00:09:39 +0100 evomaintenance (0.2.1-1) UNRELEASED; urgency=low - * New upstream release. + * New upstream release. -- Gregory Colpart Sun, 9 Nov 2008 22:37:20 +0100 From b1e2a6ac0fa69ac1ca58b2e955cc743a97fbe309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 19:05:30 +0100 Subject: [PATCH 25/61] Rename variables for hooks --- evomaintenance.sh | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 7837687..1c69240 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -144,9 +144,9 @@ print_options() { cat < Date: Sun, 10 Mar 2019 19:06:39 +0100 Subject: [PATCH 26/61] simplify default values declaration --- evomaintenance.sh | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 1c69240..ec1cd2f 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -236,15 +236,14 @@ hook_log() { # load configuration if present. test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf -[ -n "${HOSTNAME}" ] || HOSTNAME=$(get_fqdn) -[ -n "${EVOMAINTMAIL}" ] || EVOMAINTMAIL=evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM} -[ -n "${LOGFILE}" ] || LOGFILE=/var/log/evomaintenance.log - -[ -n "${HOOK_COMMIT}" ] || HOOK_COMMIT=1 -[ -n "${HOOK_DB}" ] || HOOK_DB=1 -[ -n "${HOOK_MAIL}" ] || HOOK_MAIL=1 -[ -n "${DRY_RUN}" ] || DRY_RUN=0 -[ -n "${VERBOSE}" ] || VERBOSE=0 +HOSTNAME=${HOSTNAME:-$(get_fqdn)} +EVOMAINTMAIL=${EVOMAINTMAIL:-"evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM}"} +LOGFILE=${LOGFILE:-"/var/log/evomaintenance.log"} +HOOK_COMMIT=${HOOK_COMMIT:-"1"} +HOOK_DB=${HOOK_DB:-"1"} +HOOK_MAIL=${HOOK_MAIL:-"1"} +DRY_RUN=${DRY_RUN:-"0"} +VERBOSE=${VERBOSE:-"0"} # initialize variables MESSAGE="" From 90e3bc188f5f03e97936a420844b976a05150dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 19:06:59 +0100 Subject: [PATCH 27/61] binaries and interactive variables are readonly --- evomaintenance.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/evomaintenance.sh b/evomaintenance.sh index ec1cd2f..51b7d55 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -349,7 +349,10 @@ USER=$(logname) PATH=${PATH}:/usr/sbin SENDMAIL_BIN=$(command -v sendmail) +readonly SENDMAIL_BIN + GIT_BIN=$(command -v git) +readonly GIT_BIN GIT_REPOSITORIES="/etc /etc/bind" @@ -374,6 +377,7 @@ if [ -t 0 ]; then else INTERACTIVE=0 fi +readonly INTERACTIVE if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then # get input from stdin From 42bb96d005fa552238b4ef8b57cc0911e710e7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 22:31:34 +0100 Subject: [PATCH 28/61] Let's consider that [] is the default test idiom when combined with if --- evomaintenance.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 51b7d55..1481416 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -116,7 +116,7 @@ get_repository_status() { # tell Git where to find the repository and the work tree (no need to `cd …` there) export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" # If the repository and the work tree exist, try to commit changes - if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then + if [ -d "${GIT_DIR}" ] && [ -d "${GIT_WORK_TREE}" ]; then CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then STATUS=$(${GIT_BIN} status --short | tail -n 10) @@ -154,13 +154,13 @@ END } hook_commit() { - if test -x "${GIT_BIN}"; then + if [ -x "${GIT_BIN}" ]; then # loop on possible directories managed by GIT for dir in ${GIT_REPOSITORIES}; do # tell Git where to find the repository and the work tree (no need to `cd …` there) export GIT_DIR="${dir}/.git" GIT_WORK_TREE="${dir}" # If the repository and the work tree exist, try to commit changes - if test -d "${GIT_DIR}" && test -d "${GIT_WORK_TREE}"; then + if [ -d "${GIT_DIR}" ] && [ -d "${GIT_WORK_TREE}" ]; then CHANGED_LINES=$(${GIT_BIN} status --porcelain | wc -l | tr -d ' ') if [ "${CHANGED_LINES}" != "0" ]; then if [ "${DRY_RUN}" = "1" ]; then @@ -180,6 +180,7 @@ hook_commit() { # unset environment variables to prevent accidental influence on other git commands unset GIT_DIR GIT_WORK_TREE done + if [ -n "${GIT_COMMITS}" ]; then if [ "${VERBOSE}" = "1" ]; then printf "\n\n********** Commits ****************\n%s\n***********************************\n" "${GIT_COMMITS}" @@ -359,11 +360,11 @@ GIT_REPOSITORIES="/etc /etc/bind" # initialize variable GIT_STATUSES="" # git statuses -if test -x "${GIT_BIN}"; then +if [ -x "${GIT_BIN}" ]; then # loop on possible directories managed by GIT for dir in ${GIT_REPOSITORIES}; do RESULT=$(get_repository_status "${dir}") - if test -n "${RESULT}"; then + if [ -n "${RESULT}" ]; then # append diff data, without empty lines GIT_STATUSES=$(printf "%s\n%s\n" "${GIT_STATUSES}" "${RESULT}" | sed -e '/^$/d') fi @@ -385,7 +386,7 @@ if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then read -r MESSAGE fi -if test -z "${MESSAGE}"; then +if [ -z "${MESSAGE}" ]; then echo "no value..." exit 1 fi From 7833c9c3ee2bcbed8b410343e0929ff89cba1259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 22:32:03 +0100 Subject: [PATCH 29/61] replace a couple of echo with printf --- evomaintenance.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 1481416..a45d270 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -230,8 +230,7 @@ hook_mail() { } hook_log() { - echo "----------- $(get_now) ---------------" >> "${LOGFILE}" - echo "${BLOB}" >> "${LOGFILE}" + printf "----------- %s ---------------\n%s\n" "$(get_now)" "${BLOB}" >> "${LOGFILE}" } # load configuration if present. @@ -400,10 +399,8 @@ fi # Commit hook if [ "${INTERACTIVE}" = "1" ]; then - if test -n "${GIT_STATUSES}" && [ "${HOOK_COMMIT}" = "1" ]; then - echo "/!\ There are some uncommited changes." - echo "${GIT_STATUSES}" - echo "" + if [ -n "${GIT_STATUSES}" ] && [ "${HOOK_COMMIT}" = "1" ]; then + printf "/!\ There are some uncommited changes.\n%s\n\n" "${GIT_STATUSES}" y="Y"; n="n" answer="" From 5f06aec8037d61eb11d4a9b94b99be503647f25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 22:32:49 +0100 Subject: [PATCH 30/61] replace nested if/else/if with if/elif --- evomaintenance.sh | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index a45d270..d82cc61 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -427,10 +427,8 @@ if [ "${INTERACTIVE}" = "1" ]; then esac done fi -else - if [ "${HOOK_COMMIT}" = "1" ]; then - hook_commit - fi +elif [ "${HOOK_COMMIT}" = "1" ]; then + hook_commit fi # Database hook @@ -463,10 +461,8 @@ if [ "${INTERACTIVE}" = "1" ]; then ;; esac done -else - if [ "${HOOK_DB}" = "1" ]; then - hook_db - fi +elif [ "${HOOK_DB}" = "1" ]; then + hook_db fi # Mail hook @@ -506,10 +502,8 @@ if [ "${INTERACTIVE}" = "1" ]; then ;; esac done -else - if [ "${HOOK_MAIL}" = "1" ]; then - hook_mail - fi +elif [ "${HOOK_MAIL}" = "1" ]; then + hook_mail fi exit 0 From cc71b797c9e509c4e0c92306efed46263648d53c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 22:54:56 +0100 Subject: [PATCH 31/61] bump version to 5.0.0.beta2 --- evomaintenance.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index d82cc61..0afb0ac 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -4,11 +4,11 @@ # Dependencies (all OS): git postgresql-client # Dependencies (Debian): sudo -# version 0.5.0.beta1 +# version 0.5.0.beta2 # Copyright 2007-2019 Evolix , Gregory Colpart , # Jérémy Lecour and others. -VERSION="0.5.0.beta1" +VERSION="0.5.0.beta2" show_version() { printf "%s\n" "evomaintenance version ${VERSION}" From d61907225457a46b077713e1e1dc95cf31d1cc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 23:01:59 +0100 Subject: [PATCH 32/61] replace - by * so printf doesn't think they are options --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 0afb0ac..34a8d44 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -230,7 +230,7 @@ hook_mail() { } hook_log() { - printf "----------- %s ---------------\n%s\n" "$(get_now)" "${BLOB}" >> "${LOGFILE}" + printf "*********** %s ***************\n%s\n" "$(get_now)" "${BLOB}" >> "${LOGFILE}" } # load configuration if present. From 7b2a3fbcd1da3d97a64cea397791628b7daf0fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sun, 10 Mar 2019 23:04:22 +0100 Subject: [PATCH 33/61] bump version to 0.5.0.beta3 --- evomaintenance.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 34a8d44..bcba507 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -4,11 +4,11 @@ # Dependencies (all OS): git postgresql-client # Dependencies (Debian): sudo -# version 0.5.0.beta2 +# version 0.5.0.beta3 # Copyright 2007-2019 Evolix , Gregory Colpart , # Jérémy Lecour and others. -VERSION="0.5.0.beta2" +VERSION="0.5.0.beta3" show_version() { printf "%s\n" "evomaintenance version ${VERSION}" From e0a75db0153b9cf4dca6d466adfe0a34c95933bd Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 12 Mar 2019 12:12:02 +0100 Subject: [PATCH 34/61] typo --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index bcba507..5d283e3 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -29,7 +29,7 @@ evomaintenance is a program that helps reporting what you've done on a server Usage: evomaintenance or evomaintenance --message="add new host" or evomaintenance --no-db --no-mail --no-commit - or echo "add new vhost | evomaintenance + or echo "add new vhost" | evomaintenance Options -m, --message=MESSAGE set the message from the command line From 0a6a45066d1bd1454952fa9384ee45ca0f073f2a Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 12 Mar 2019 22:49:27 +0100 Subject: [PATCH 35/61] Split interactive and non interactive parts of hooks execution --- evomaintenance.sh | 88 +++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 5d283e3..31b1e42 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -397,15 +397,15 @@ if [ "${DRY_RUN}" != "1" ]; then hook_log fi -# Commit hook if [ "${INTERACTIVE}" = "1" ]; then + # Commit hook if [ -n "${GIT_STATUSES}" ] && [ "${HOOK_COMMIT}" = "1" ]; then printf "/!\ There are some uncommited changes.\n%s\n\n" "${GIT_STATUSES}" y="Y"; n="n" answer="" while true; do - printf "> Do you want to commit the changes? [%s] " "${y}${n}" + printf "> Do you want to commit the changes? [%s] " "${y},${n}" read -r answer case $answer in [Yy] ) @@ -427,46 +427,8 @@ if [ "${INTERACTIVE}" = "1" ]; then esac done fi -elif [ "${HOOK_COMMIT}" = "1" ]; then - hook_commit -fi -# Database hook -if [ "${INTERACTIVE}" = "1" ]; then - if [ "${HOOK_DB}" = "1" ]; then - y="Y"; n="n" - else - y="y"; n="N" - fi - answer="" - while true; do - printf "> Do you want to insert your message into the database? [%s] " "${y}${n}" - read -r answer - case $answer in - [Yy] ) - hook_db; - break - ;; - [Nn] ) - break - ;; - "" ) - if [ "${HOOK_DB}" = "1" ]; then - hook_db - fi - break - ;; - * ) - echo "answer with a valid choice" - ;; - esac - done -elif [ "${HOOK_DB}" = "1" ]; then - hook_db -fi - -# Mail hook -if [ "${INTERACTIVE}" = "1" ]; then + # Mail hook if [ "${HOOK_MAIL}" = "1" ]; then y="Y"; n="n" else @@ -474,7 +436,7 @@ if [ "${INTERACTIVE}" = "1" ]; then fi answer="" while true; do - printf "> Do you want to send an email to <%s>? [%s] " "${EVOMAINTMAIL}" "${y}${n}e" + printf "> Do you want to send an email to <%s>? [%s] " "${EVOMAINTMAIL}" "${y},${n},e" read -r answer case $answer in [Yy] ) @@ -502,8 +464,46 @@ if [ "${INTERACTIVE}" = "1" ]; then ;; esac done -elif [ "${HOOK_MAIL}" = "1" ]; then - hook_mail + + # Database hook + if [ "${HOOK_DB}" = "1" ]; then + y="Y"; n="n" + else + y="y"; n="N" + fi + answer="" + while true; do + printf "> Do you want to insert your message into the database? [%s] " "${y},${n}" + read -r answer + case $answer in + [Yy] ) + hook_db; + break + ;; + [Nn] ) + break + ;; + "" ) + if [ "${HOOK_DB}" = "1" ]; then + hook_db + fi + break + ;; + * ) + echo "answer with a valid choice" + ;; + esac + done +else + if [ "${HOOK_COMMIT}" = "1" ]; then + hook_commit + fi + if [ "${HOOK_MAIL}" = "1" ]; then + hook_mail + fi + if [ "${HOOK_DB}" = "1" ]; then + hook_db + fi fi exit 0 From ca9c7ec654b9ec8a6ac4981df14f0a519c5bdfa7 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 12 Mar 2019 23:29:25 +0100 Subject: [PATCH 36/61] Add "auto" mode --- evomaintenance.sh | 78 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 31b1e42..76fdf26 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -33,12 +33,14 @@ Usage: evomaintenance Options -m, --message=MESSAGE set the message from the command line - --mail enable the mail hook + --mail enable the mail hook (default) --no-mail disable the mail hook - --db enable the database hook + --db enable the database hook (default) --no-db disable the database hook - --commit enable the commit hook + --commit enable the commit hook (default) --no-commit disable the commit hook + --auto use "auto" mode + --no-auto use "manual" mode (default) -v, --verbose increase verbosity -n, --dry-run actions are not executed --help print this message and exit @@ -148,6 +150,7 @@ HOOK_COMMIT: ${HOOK_COMMIT} HOOK_DB: ${HOOK_DB} HOOK_MAIL: ${HOOK_MAIL} DRY_RUN: ${DRY_RUN} +AUTO: ${AUTO} *********************************** END @@ -244,6 +247,7 @@ HOOK_DB=${HOOK_DB:-"1"} HOOK_MAIL=${HOOK_MAIL:-"1"} DRY_RUN=${DRY_RUN:-"0"} VERBOSE=${VERBOSE:-"0"} +AUTO=${AUTO:-"0"} # initialize variables MESSAGE="" @@ -304,6 +308,14 @@ while :; do # enable mail hook HOOK_MAIL=1 ;; + --no-auto) + # use "manual" mode + AUTO=0 + ;; + --auto) + # use "auto" mode + AUTO=1 + ;; -n|--dry-run) # disable actual commands DRY_RUN=1 @@ -397,14 +409,62 @@ if [ "${DRY_RUN}" != "1" ]; then hook_log fi -if [ "${INTERACTIVE}" = "1" ]; then +if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then + if [ "${HOOK_COMMIT}" = "1" ] || [ "${HOOK_MAIL}" = "1" ] || [ "${HOOK_DB}" = "1" ]; then + printf "Actions to execute:\n" + if [ "${HOOK_COMMIT}" = "1" ]; then + printf "* commit changes in repositories\n" + fi + if [ "${HOOK_MAIL}" = "1" ]; then + printf "* send mail to %s\n" "${EVOMAINTMAIL}" + fi + if [ "${HOOK_DB}" = "1" ]; then + printf "* save metadata to the database\n" + fi + echo "" + + answer="" + while :; do + printf "> Let's continue? [Y,n,i,?] " + read -r answer + case $answer in + [Yy]|"" ) + # force "auto" mode, but keep hooks settings + AUTO=1 + break + ;; + [Nn] ) + # force "auto" mode, and disable all hooks + HOOK_COMMIT=0 + HOOK_MAIL=0 + HOOK_DB=0 + AUTO=1 + break + ;; + [Ii] ) + # force "manual" mode + AUTO=0 + break + ;; + * ) + printf "y - yes, execute actions and exit\n" + printf "n - no, don't execute actions and exit\n" + printf "i - switch to interactive mode\n" + printf "? - print this help\n" + ;; + esac + done + fi +fi + +if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then # Commit hook if [ -n "${GIT_STATUSES}" ] && [ "${HOOK_COMMIT}" = "1" ]; then printf "/!\ There are some uncommited changes.\n%s\n\n" "${GIT_STATUSES}" y="Y"; n="n" answer="" - while true; do + while :; do printf "> Do you want to commit the changes? [%s] " "${y},${n}" read -r answer case $answer in @@ -435,7 +495,7 @@ if [ "${INTERACTIVE}" = "1" ]; then y="y"; n="N" fi answer="" - while true; do + while :; do printf "> Do you want to send an email to <%s>? [%s] " "${EVOMAINTMAIL}" "${y},${n},e" read -r answer case $answer in @@ -472,7 +532,7 @@ if [ "${INTERACTIVE}" = "1" ]; then y="y"; n="N" fi answer="" - while true; do + while :; do printf "> Do you want to insert your message into the database? [%s] " "${y},${n}" read -r answer case $answer in @@ -494,7 +554,9 @@ if [ "${INTERACTIVE}" = "1" ]; then ;; esac done -else +fi + +if [ "${INTERACTIVE}" = "0" ] || [ "${AUTO}" = "1" ]; then if [ "${HOOK_COMMIT}" = "1" ]; then hook_commit fi From 42b781db9652b6dd1f5360dc1e3764b13ec7e48d Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 12 Mar 2019 23:31:58 +0100 Subject: [PATCH 37/61] Display git statuses before typing the message --- evomaintenance.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/evomaintenance.sh b/evomaintenance.sh index 76fdf26..fdf6ea1 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -391,6 +391,10 @@ else fi readonly INTERACTIVE +if [ -n "${GIT_STATUSES}" ] && [ "${INTERACTIVE}" = "1" ]; then + printf "/!\ There are some uncommited changes.\n%s\n\n" "${GIT_STATUSES}" +fi + if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then # get input from stdin printf "> Please, enter details about your maintenance:\n" From 6d134e2d890f108172a5bb4caec9de9969330b70 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Tue, 12 Mar 2019 23:57:24 +0100 Subject: [PATCH 38/61] move copyrigh and license into "show_version()" --- evomaintenance.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index fdf6ea1..8beeebe 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -11,19 +11,20 @@ VERSION="0.5.0.beta3" show_version() { - printf "%s\n" "evomaintenance version ${VERSION}" -} - -show_help() { cat <, Gregory Colpart , Jérémy Lecour and others. evomaintenance comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. +END +} +show_help() { + cat < Date: Wed, 13 Mar 2019 15:35:44 +0100 Subject: [PATCH 39/61] bump beta version --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 8beeebe..346a82b 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -8,7 +8,7 @@ # Copyright 2007-2019 Evolix , Gregory Colpart , # Jérémy Lecour and others. -VERSION="0.5.0.beta3" +VERSION="0.5.0.beta4" show_version() { cat < Date: Wed, 13 Mar 2019 15:40:48 +0100 Subject: [PATCH 40/61] reading from stdin is back! --- evomaintenance.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 346a82b..6b1fdc7 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -396,9 +396,10 @@ if [ -n "${GIT_STATUSES}" ] && [ "${INTERACTIVE}" = "1" ]; then printf "/!\ There are some uncommited changes.\n%s\n\n" "${GIT_STATUSES}" fi -if [ -z "${MESSAGE}" ] && [ "${INTERACTIVE}" = "1" ]; then - # get input from stdin - printf "> Please, enter details about your maintenance:\n" +if [ -z "${MESSAGE}" ]; then + if [ "${INTERACTIVE}" = "1" ]; then + printf "> Please, enter details about your maintenance:\n" + fi read -r MESSAGE fi From c47ebee7c0b564852f15e98ef0c993bfe8734f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Fri, 15 Mar 2019 22:53:08 +0100 Subject: [PATCH 41/61] Improve logging logging is treated as other hooks regarding verbose and dry-run modes --- evomaintenance.sh | 60 ++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 6b1fdc7..95845b5 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -130,31 +130,23 @@ get_repository_status() { unset GIT_DIR GIT_WORK_TREE } -print_summary() { - BLOB=$(cat <> "${LOGFILE}" + if [ "${VERBOSE}" = "1" ]; then + print_log + fi + if [ "${DRY_RUN}" != "1" ]; then + print_log >> "${LOGFILE}" + fi } # load configuration if present. @@ -342,9 +339,6 @@ while :; do shift done -if [ "${VERBOSE}" = "1" ]; then - print_options -fi # Treat unset variables as an error when substituting. # Only after this line, because some config variables might be missing. @@ -408,16 +402,11 @@ if [ -z "${MESSAGE}" ]; then exit 1 fi -print_summary - -# Log hook -if [ "${DRY_RUN}" != "1" ]; then - hook_log -fi +print_session_data if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then if [ "${HOOK_COMMIT}" = "1" ] || [ "${HOOK_MAIL}" = "1" ] || [ "${HOOK_DB}" = "1" ]; then - printf "Actions to execute:\n" + printf "\nActions to execute:\n" if [ "${HOOK_COMMIT}" = "1" ]; then printf "* commit changes in repositories\n" fi @@ -562,6 +551,9 @@ if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then done fi +# Log hook +hook_log + if [ "${INTERACTIVE}" = "0" ] || [ "${AUTO}" = "1" ]; then if [ "${HOOK_COMMIT}" = "1" ]; then hook_commit From ac111c2742dcc0f700a7a9968b74eb5f580de4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 12:12:48 +0100 Subject: [PATCH 42/61] update README with usage output --- README | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/README b/README index d61af76..8db8f3e 100644 --- a/README +++ b/README @@ -1,7 +1,26 @@ -script for Evolix maintenance -============================= +# Evomaintenance -script evomaintenance.sh which sends a mail -for each intervention on a Pack Evolix server. +``` +# evomaintenance --help +evomaintenance is a program that helps reporting what you've done on a server -* Pb quand '&' dans le msg d'evomaintenance... +Usage: evomaintenance + or evomaintenance --message="add new host" + or evomaintenance --no-db --no-mail --no-commit + or echo "add new vhost" | evomaintenance + +Options + -m, --message=MESSAGE set the message from the command line + --mail enable the mail hook (default) + --no-mail disable the mail hook + --db enable the database hook (default) + --no-db disable the database hook + --commit enable the commit hook (default) + --no-commit disable the commit hook + --auto use "auto" mode + --no-auto use "manual" mode (default) + -v, --verbose increase verbosity + -n, --dry-run actions are not executed + --help print this message and exit + --version print version and exit +``` From 8561d64c0eea808beaf0de04a53fe4c8ed8904fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 12:13:23 +0100 Subject: [PATCH 43/61] README in Markdown --- README => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.md (100%) diff --git a/README b/README.md similarity index 100% rename from README rename to README.md From ac86a97531e45ebc5a182bce7e7f64e061d7aecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 12:14:44 +0100 Subject: [PATCH 44/61] update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8db8f3e..4fe0e74 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Evomaintenance -``` -# evomaintenance --help +```.plain +$ evomaintenance --help evomaintenance is a program that helps reporting what you've done on a server Usage: evomaintenance From 71d1fc417c4adc5b0329c2c527da1a5694f1b8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 19:40:20 +0100 Subject: [PATCH 45/61] "who -m" output must be normalized --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 95845b5..d997ab7 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -76,7 +76,7 @@ get_tty() { } get_who() { - who=$(LC_ALL=C who -m) + who=$(LC_ALL=C who -m | tr -s ' ') if [ -n "${who}" ]; then echo "${who}" From d05de0f9251e9e5be08d0447c60a1350701cf420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 19:40:53 +0100 Subject: [PATCH 46/61] replace $(echo $(cmd)) by $(cmd) --- evomaintenance.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index d997ab7..0eb33b0 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -86,11 +86,11 @@ get_who() { } get_begin_date() { - echo "$(date "+%Y") $(echo $(get_who) | cut -d" " -f3,4,5)" + printf "%s %s" "$(date "+%Y")" "$(get_who | cut -d" " -f3,4,5)" } get_ip() { - ip=$(echo $(get_who) | cut -d" " -f6 | sed -e "s/^(// ; s/)$//") + ip=$(get_who | cut -d" " -f6 | sed -e "s/^(// ; s/)$//") [ -z "${ip}" ] && ip="unknown (no tty)" [ "${ip}" = ":0" ] && ip="localhost" From 80087f3c38145894e0f16cc1df84a8f4fb05d6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Sat, 16 Mar 2019 20:09:33 +0100 Subject: [PATCH 47/61] small display fix --- evomaintenance.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 0eb33b0..c1487a3 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -179,7 +179,7 @@ hook_commit() { if [ -n "${GIT_COMMITS}" ]; then if [ "${VERBOSE}" = "1" ]; then - printf "\n\n********** Commits ****************\n%s\n***********************************\n" "${GIT_COMMITS}" + printf "\n********** Commits ****************\n%s\n***********************************\n" "${GIT_COMMITS}" fi if [ "${DRY_RUN}" != "1" ]; then echo "${GIT_COMMITS}" >> "${LOGFILE}" @@ -193,7 +193,7 @@ hook_db() { PG_QUERY="INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES ('${HOSTNAME}','${USER}','${IP}','${BEGIN_DATE}',now(),'${SQL_DETAILS}')" if [ "${VERBOSE}" = "1" ]; then - printf "\n\n********** DB query **************\n%s\n***********************************\n" "${PG_QUERY}" + printf "\n********** DB query **************\n%s\n***********************************\n" "${PG_QUERY}" fi if [ "${DRY_RUN}" != "1" ]; then echo "${PG_QUERY}" | psql "${PGDB}" "${PGTABLE}" -h "${PGHOST}" @@ -218,7 +218,7 @@ hook_mail() { /usr/share/scripts/evomaintenance.tpl) if [ "${VERBOSE}" = "1" ]; then - printf "\n\n********** Mail *******************\n%s\n***********************************\n" "${MAIL_CONTENT}" + printf "\n********** Mail *******************\n%s\n***********************************\n" "${MAIL_CONTENT}" fi if [ "${DRY_RUN}" != "1" ]; then echo "${MAIL_CONTENT}" | ${SENDMAIL_BIN} -oi -t -f "${FROM}" From c9c061f81142c46fc06801a5010f67746c9fdec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Mon, 18 Mar 2019 07:44:28 +0100 Subject: [PATCH 48/61] long and short commit information --- evomaintenance.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index c1487a3..721e03d 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -8,7 +8,7 @@ # Copyright 2007-2019 Evolix , Gregory Colpart , # Jérémy Lecour and others. -VERSION="0.5.0.beta4" +VERSION="0.5.0.beta5" show_version() { cat < Date: Wed, 13 Mar 2019 16:50:26 +0100 Subject: [PATCH 49/61] ignore unknown arguments that don't start with a "-" --- evomaintenance.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 721e03d..71da8a3 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -333,13 +333,14 @@ while :; do shift break ;; - -?*) + -?*|[[:alnum:]]*) # ignore unknown options printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 ;; *) # Default case: If no more options then break out of the loop. break + ;; esac shift From bea327a2547a0675bfbd51f6dd3c65ac55b4e388 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Mon, 18 Mar 2019 14:15:09 +0100 Subject: [PATCH 50/61] embed the template in a function sed is not very good with multi-line search/replace, so we've decided to use a shell function to format the mail. --- evomaintenance.sh | 62 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 71da8a3..808a510 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -204,22 +204,54 @@ hook_db() { fi } +format_mail() { + cat < Date: Mon, 18 Mar 2019 15:11:09 +0100 Subject: [PATCH 51/61] Show more git stats --- evomaintenance.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 808a510..af764cb 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -170,7 +170,7 @@ hook_commit() { # Add the SHA to the log file if something has been committed SHA=$(${GIT_BIN} rev-parse --short HEAD) STATS_SHORT=$(${GIT_BIN} show --stat | tail -1) - STATS=$(${GIT_BIN} show --stat | tail -n ${GIT_STATUS_MAX_LINES}) + STATS=$(${GIT_BIN} show --stat --pretty=format:"" | tail -n ${GIT_STATUS_MAX_LINES}) # append commit data, without empty lines GIT_COMMITS_SHORT=$(printf "%s\n%s : %s –%s" "${GIT_COMMITS_SHORT}" "${GIT_DIR}" "${SHA}" "${STATS_SHORT}" | sed -e '/^$/d') GIT_COMMITS=$(printf "%s\n%s : %s\n%s" "${GIT_COMMITS}" "${GIT_DIR}" "${SHA}" "${STATS}" | sed -e '/^$/d') @@ -182,9 +182,9 @@ hook_commit() { done if [ -n "${GIT_COMMITS}" ]; then - if [ "${VERBOSE}" = "1" ]; then + # if [ "${VERBOSE}" = "1" ]; then printf "\n********** Commits ****************\n%s\n***********************************\n" "${GIT_COMMITS}" - fi + # fi if [ "${DRY_RUN}" != "1" ]; then echo "${GIT_COMMITS}" >> "${LOGFILE}" fi From 8d0eddaf6f337e264417f389fa5c6a33892fab55 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Mon, 18 Mar 2019 15:11:16 +0100 Subject: [PATCH 52/61] bump version --- evomaintenance.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index af764cb..3c52aa3 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -4,11 +4,11 @@ # Dependencies (all OS): git postgresql-client # Dependencies (Debian): sudo -# version 0.5.0.beta3 +# version 0.5.0.beta6 # Copyright 2007-2019 Evolix , Gregory Colpart , # Jérémy Lecour and others. -VERSION="0.5.0.beta5" +VERSION="0.5.0.beta6" show_version() { cat < Date: Mon, 18 Mar 2019 22:59:37 +0100 Subject: [PATCH 53/61] quote grep parameter --- evomaintenance.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 3c52aa3..cee64b3 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -81,7 +81,7 @@ get_who() { if [ -n "${who}" ]; then echo "${who}" else - LC_ALL=C who | grep $(get_tty) | tr -s ' ' + LC_ALL=C who | grep "$(get_tty)" | tr -s ' ' fi } From b13c62723017f217a6e9fefc518b30f45ffa3917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Mon, 18 Mar 2019 23:24:09 +0100 Subject: [PATCH 54/61] We don't need the template file anymore --- Vagrantfile | 1 - debian/rules | 1 - evomaintenance.tpl | 33 --------------------------------- 3 files changed, 35 deletions(-) delete mode 100644 evomaintenance.tpl diff --git a/Vagrantfile b/Vagrantfile index 26788ec..7649cc1 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -22,7 +22,6 @@ if [ ! -d /usr/share/scripts ]; then chown root:$1 /usr/share/scripts fi ln -fs /vagrant/evomaintenance.sh /usr/share/scripts -ln -fs /vagrant/evomaintenance.tpl /usr/share/scripts ln -fs /vagrant/evomaintenance.cf /etc SCRIPT diff --git a/debian/rules b/debian/rules index 8cf209e..5adc822 100755 --- a/debian/rules +++ b/debian/rules @@ -24,7 +24,6 @@ install: build dh_installdirs install -m 700 -d $(CURDIR)/debian/evomaintenance/usr/share/scripts install -m 700 evomaintenance.sh $(CURDIR)/debian/evomaintenance/usr/share/scripts/ - install -m 600 evomaintenance.tpl $(CURDIR)/debian/evomaintenance/usr/share/scripts/ install -m 755 -d $(CURDIR)/debian/evomaintenance/etc install -m 600 evomaintenance.cf $(CURDIR)/debian/evomaintenance/etc/ # Build architecture-independent files here. diff --git a/evomaintenance.tpl b/evomaintenance.tpl deleted file mode 100644 index ddddd7b..0000000 --- a/evomaintenance.tpl +++ /dev/null @@ -1,33 +0,0 @@ -From: __FULLFROM__ -Content-Type: text/plain; charset=UTF-8 -MIME-Version: 1.0 -Content-Transfer-Encoding: 8bit -To: __TO__ -Subject: [evomaintenance] Intervention sur __HOSTNAME__ (__USER__) - -Bonjour, - -Une intervention vient de se terminer sur votre serveur. -Voici les renseignements sur l'intervention : - -Nom du serveur : __HOSTNAME__ -Personne ayant réalisée l'intervention : __USER__ -Intervention réalisée depuis : __IP__ -Début de l'intervention : __BEGIN_DATE__ -Fin de l'intervention : __END_DATE__ - -### -Renseignements sur l'intervention : -__TEXTE__ -### - -__GIT_COMMITS__ - -Pour réagir à cette intervention, vous pouvez répondre à ce message -(sur l'adresse mail __FROM__). En cas d'urgence, utilisez -l'adresse __URGENCYFROM__ ou notre téléphone portable d'astreinte -(__URGENCYTEL__) - -Cordialement, --- -__FULLFROM__ From 13885a4743a6eb7a834c2426649cf2700e566cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Mon, 18 Mar 2019 23:24:31 +0100 Subject: [PATCH 55/61] whitespaces --- Vagrantfile | 4 ++-- debian/rules | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 7649cc1..74a4a71 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -10,7 +10,7 @@ load File.expand_path(vagrantfile) if File.exists?(vagrantfile) Vagrant.configure('2') do |config| config.vm.synced_folder "./", "/vagrant", type: "rsync", rsync__exclude: [ '.vagrant', '.git' ] config.ssh.shell = "/bin/sh" - + $deps = <