2015-09-13 15:29:22 +02:00
#!/bin/sh
# EvoMaintenance script
2016-12-08 16:51:55 +01:00
# Dependencies (all OS): git postgresql-client
# Dependencies (Debian): sudo
2015-09-13 15:29:22 +02:00
2018-09-25 11:55:41 +02:00
# version 0.4.1
2018-09-05 00:04:22 +02:00
# Copyright 2007-2018 Gregory Colpart <reg@evolix.fr>, Jérémy Lecour <jlecour@evolix.fr>, Evolix <info@evolix.fr>
2016-12-08 16:32:35 +01:00
2018-09-25 09:48:29 +02:00
get_system( ) {
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
}
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
}
get_who( ) {
who = $( LC_ALL = C who -m)
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) "
}
get_ip( ) {
ip = $( echo $( get_who) | cut -d" " -f6 | sed -e " s/^(// ; s/) $// " )
[ -z " ${ ip } " ] && ip = "unknown (no tty)"
[ " ${ ip } " = ":0" ] && ip = "localhost"
echo " ${ ip } "
}
get_end_date( ) {
date +"%Y %b %d %H:%M"
}
get_now( ) {
date +"%Y-%m-%dT%H:%M:%S%z"
}
2019-02-15 15:34:19 +01:00
# timeout on read(), uses TMOUT env as timer
timedout_read( ) {
if [ -z ${ TMOUT +x } ] || [ " $TMOUT " = 0 ] ; then
export TMOUT = 36000
fi
user_input = $1
old_tty_settings = $( stty -g)
stty -icanon min 0 time ${ TMOUT } 0
read $user_input
stty " $old_tty_settings "
unset TMOUT
}
2015-09-13 15:29:22 +02:00
test -f /etc/evomaintenance.cf && . /etc/evomaintenance.cf
2018-09-25 09:48:29 +02:00
[ -n " ${ HOSTNAME } " ] || HOSTNAME = $( get_fqdn)
2018-09-20 16:10:03 +02:00
[ -n " ${ EVOMAINTMAIL } " ] || EVOMAINTMAIL = evomaintenance-$( echo " ${ HOSTNAME } " | cut -d- -f1) @${ REALM }
[ -n " ${ LOGFILE } " ] || LOGFILE = /var/log/evomaintenance.log
2018-09-20 14:25:16 +02:00
2018-09-20 15:56:13 +02:00
# Treat unset variables as an error when substituting.
# Only after this line, because some config variables might be missing.
set -u
2018-09-25 09:48:29 +02:00
REAL_HOSTNAME = $( get_fqdn)
2018-09-20 16:10:03 +02:00
if [ " ${ HOSTNAME } " = " ${ REAL_HOSTNAME } " ] ; then
2018-09-20 14:25:16 +02:00
HOSTNAME_TEXT = " ${ HOSTNAME } "
else
2018-09-20 16:10:03 +02:00
HOSTNAME_TEXT = " ${ HOSTNAME } ( ${ REAL_HOSTNAME } ) "
2018-09-20 14:25:16 +02:00
fi
2015-09-13 15:29:22 +02:00
2018-09-25 09:48:29 +02:00
# TTY=$(get_tty)
# WHO=$(get_who)
IP = $( get_ip)
BEGIN_DATE = $( get_begin_date)
END_DATE = $( get_end_date)
USER = $( logname)
2018-09-05 18:44:53 +02:00
PATH = ${ PATH } :/usr/sbin
2015-09-13 15:29:22 +02:00
2018-09-20 15:23:52 +02:00
SENDMAIL_BIN = $( command -v sendmail)
2018-09-20 14:23:54 +02:00
GIT_BIN = $( command -v git)
2018-09-20 15:24:46 +02:00
GIT_REPOSITORIES = "/etc /etc/bind"
2018-09-20 15:56:25 +02:00
# git statuses
GIT_STATUSES = ""
2018-09-20 15:24:46 +02:00
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
2018-09-25 09:48:47 +02:00
CHANGED_LINES = $( ${ GIT_BIN } status --porcelain | wc -l | tr -d ' ' )
2018-09-20 15:24:46 +02:00
if [ " ${ CHANGED_LINES } " != "0" ] ; then
2018-09-20 15:56:25 +02:00
STATUS = $( ${ GIT_BIN } status --short | tail -n 10)
2018-09-20 15:24:46 +02:00
# append diff data, without empty lines
2018-09-25 11:55:41 +02:00
GIT_STATUSES = $( printf "%s\n%s\n%s\n" " ${ GIT_STATUSES } " " ${ GIT_DIR } (last 10 lines) " " ${ STATUS } " | sed -e '/^$/d' )
2018-09-20 15:24:46 +02:00
fi
fi
# unset environment variables to prevent accidental influence on other git commands
unset GIT_DIR GIT_WORK_TREE
done
2018-09-20 15:56:25 +02:00
if [ -n " ${ GIT_STATUSES } " ] ; then
2018-09-20 15:24:46 +02:00
echo "/!\ There are some uncommited changes. If you proceed, everything will be commited."
2018-09-20 15:56:25 +02:00
echo " ${ GIT_STATUSES } "
2018-09-20 15:24:46 +02:00
echo ""
fi
fi
2018-09-06 10:50:07 +02:00
# get input from stdin
2018-09-20 15:26:51 +02:00
echo "> Please, enter details about your maintenance"
2019-02-15 15:34:19 +01:00
timedout_read TEXTE
2015-09-13 15:29:22 +02:00
2018-09-04 23:08:42 +02:00
if [ " ${ TEXTE } " = "" ] ; then
2015-09-13 15:29:22 +02:00
echo "no value..."
exit 1
fi
# recapitulatif
2018-09-05 18:45:46 +02:00
BLOB = $( cat <<END
2018-09-20 14:25:16 +02:00
Host : $HOSTNAME_TEXT
2018-09-06 10:44:13 +02:00
User : $USER
IP : $IP
Begin : $BEGIN_DATE
End : $END_DATE
Message : $TEXTE
2018-09-05 18:45:46 +02:00
END
)
2018-09-20 15:26:51 +02:00
echo ""
2018-09-05 18:45:46 +02:00
echo " ${ BLOB } "
2018-09-20 15:26:51 +02:00
echo ""
echo "> Press <Enter> to submit, or <Ctrl+c> to cancel."
2019-02-15 15:34:19 +01:00
timedout_read enter
2015-09-13 15:29:22 +02:00
2018-09-04 22:50:25 +02:00
# write log
2018-09-25 09:48:29 +02:00
echo " ----------- $( get_now) --------------- " >> " ${ LOGFILE } "
2018-09-06 10:12:06 +02:00
echo " ${ BLOB } " >> " ${ LOGFILE } "
2018-09-04 22:50:25 +02:00
# git commit
2018-09-07 15:03:25 +02:00
GIT_COMMITS = ""
2018-09-04 23:10:27 +02:00
if test -x " ${ GIT_BIN } " ; then
2018-09-06 10:46:50 +02:00
# loop on possible directories managed by GIT
2018-09-20 15:24:46 +02:00
for dir in ${ GIT_REPOSITORIES } ; do
2018-09-06 10:46:50 +02:00
# 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
2018-09-25 09:48:47 +02:00
CHANGED_LINES = $( ${ GIT_BIN } status --porcelain | wc -l | tr -d ' ' )
2018-09-06 12:22:35 +02:00
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
2018-09-07 15:03:25 +02:00
SHA = $( ${ GIT_BIN } rev-parse --short HEAD)
STATS = $( ${ GIT_BIN } show --stat | tail -1)
# append commit data, without empty lines
2018-09-25 11:55:41 +02:00
GIT_COMMITS = $( printf "%s\n%s : %s – %s" " ${ GIT_COMMITS } " " ${ GIT_DIR } " " ${ SHA } " " ${ STATS } " | sed -e '/^$/d' )
2018-09-06 10:12:26 +02:00
fi
fi
2018-09-06 10:46:50 +02:00
# unset environment variables to prevent accidental influence on other git commands
unset GIT_DIR GIT_WORK_TREE
2018-09-06 10:12:26 +02:00
done
2018-09-07 15:03:25 +02:00
if [ -n " ${ GIT_COMMITS } " ] ; then
echo " ${ GIT_COMMITS } " >> " ${ LOGFILE } "
fi
2015-12-02 16:20:42 +01:00
fi
2018-09-04 22:50:25 +02:00
# insert into PG
2018-09-20 14:25:37 +02:00
# SQL_TEXTE=`echo "${TEXTE}" | sed "s/'/\\\\\\'/g ; s@/@\\\\\/@g ; s@\\&@et@g"`
SQL_TEXTE = ` echo " ${ TEXTE } " | sed "s/'/''/g" `
2018-09-04 22:50:25 +02:00
2018-09-20 16:10:03 +02:00
PG_QUERY = " INSERT INTO evomaint(hostname,userid,ipaddress,begin_date,end_date,details) VALUES (' ${ HOSTNAME } ',' ${ USER } ',' ${ IP } ',' ${ BEGIN_DATE } ',now(),' ${ SQL_TEXTE } ') "
2018-11-02 10:06:06 +01:00
echo " ${ PG_QUERY } " | psql ${ PGDB } ${ PGTABLE } -h ${ PGHOST }
2015-09-13 15:29:22 +02:00
2018-09-04 22:50:25 +02:00
# send mail
2018-09-20 15:55:59 +02:00
MAIL_TEXTE = $( echo " ${ TEXTE } " | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@" )
MAIL_GIT_COMMITS = $( echo " ${ GIT_COMMITS } " | sed -e "s@/@\\\\\/@g ; s@&@\\\\&@" )
2018-09-07 15:03:25 +02:00
2015-09-13 15:29:22 +02:00
cat /usr/share/scripts/evomaintenance.tpl | \
2018-09-20 14:25:16 +02:00
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 } / " | \
2018-09-20 15:23:52 +02:00
${ SENDMAIL_BIN } -oi -t -f ${ FROM }
2018-09-20 16:20:34 +02:00
exit 0