check_rc_upgrade/check_rc_upgrade.sh

368 lines
8.9 KiB
Bash
Raw Permalink Normal View History

2010-05-11 09:48:45 +02:00
#!/bin/bash
### CONFIGURATION ###
#NAME="My Roundcube"
RC_HOME=/var/www/webmail/public_html
2010-05-11 09:48:45 +02:00
MAIL=root
SEND_EVERY_NB_DAY=7
CACHE=$RC_HOME/../.cache_check_upgrade
CACHE_FAILED=$CACHE.failed
MAX_CHECK=10
MAX_FAILED=3
SLEEP_ON_FAILED=10
2017-04-24 14:51:03 +02:00
CHECK_URL=https://roundcube.net/download/
2011-02-10 12:09:08 +01:00
DOWNLOAD=0
DOWNLOAD_DIR=$RC_HOME/../upstream
DONT_CHECK_SSL_CERT=0
WGET_OPTS=""
CURL_OPTS=""
### DO NOT MODIFY AFTER THIS LINE ###
2010-05-11 09:48:45 +02:00
DEBUG=0
2014-12-31 10:08:39 +01:00
NAGIOS=0
2011-02-10 13:16:06 +01:00
UPGRADE=0
CURL=0
2011-02-10 13:16:06 +01:00
EXTRACT=0
2013-07-03 09:38:32 +02:00
JUST_DISP=0
JUST_AFTER_UPGRADE=0
function usage() {
echo "Usage : $0 [-v] [-n] [-d] [-u] [-c]"
echo " -v Verbose mode"
2014-12-31 10:08:39 +01:00
echo " -n Nagios mode"
echo " -S Don't check SSL certificate (Wget only)"
echo " -d Download new release"
2011-02-10 13:16:06 +01:00
echo " -u Upgrade installation"
echo " -c Use curl instead of wget"
echo " -a Just run after_upgrade function"
2013-07-03 09:38:32 +02:00
echo " -j Just display current stable version"
echo " -h Display this help"
}
while getopts ":dvnShucaj" opt; do
case $opt in
v)
DEBUG=1
;;
2014-12-31 10:08:39 +01:00
n)
NAGIOS=1
;;
S)
DONT_CHECK_SSL_CERT=1
;;
d)
DOWNLOAD=1
;;
2011-02-10 13:16:06 +01:00
u)
DOWNLOAD=1
UPGRADE=1
DEBUG=1
EXTRACT=1
;;
c)
CURL=1
;;
a)
JUST_AFTER_UPGRADE=1
;;
2013-07-03 09:38:32 +02:00
j)
JUST_DISP=1
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo
usage
exit 1
;;
esac
done
2010-05-11 09:48:45 +02:00
[ -f "$0.local" ] && source "$0.local" && [ $DEBUG -eq 1 ] && echo "Import local config file : $0.local"
function run_after_upgrade() {
RC_NEW_HOME="$1"
type after_upgrade > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Execution de la fonction custom after_upgrade() :"
after_upgrade "$RC_NEW_HOME"
res=$?
echo "Fin de la fonction after_upgrade() (Retour : $res)"
return $res
else
echo "La fonction 'after_upgrade' n'est pas définie. On passe."
return 0
fi
}
if [ $JUST_AFTER_UPGRADE -eq 1 ]
then
run_after_upgrade "$RC_HOME"
exit $?
fi
WGET_SSL_OPT=""
if [ "$DONT_CHECK_SSL_CERT" == "1" ]
then
[ $DEBUG -eq 1 ] && echo "Disable SSL certificate check"
WGET_SSL_OPT="--no-check-certificate"
fi
for i in `seq 1 $MAX_CHECK`
do
tmpfile=`mktemp`
if [ $CURL -eq 1 ]
then
curl $CURL_OPTS --location --silent --output $tmpfile $CHECK_URL
else
wget $WGET_OPTS $WGET_SSL_OPT -q $CHECK_URL -O $tmpfile
fi
newest=`cat $tmpfile|grep 'github.com'|grep 'complete.tar.gz'|head -n 1|sed 's|^.*github.com/roundcube/roundcubemail/releases/download/\([^/]*\)/roundcubemail-.*$|\1|'`
if [ -n "$newest" ]
then
[ $DEBUG -eq 1 ] && echo "Newest : $newest"
[ $NAGIOS -ne 1 ] && rm -f $CACHE_FAILED > /dev/null 2>&1
DOWNLOAD_URL=`cat $tmpfile|grep "$newest"|grep "\-complete.tar.gz"|head -1|sed 's/.* href="[ \t]*\([^"]*\)[ \t]*" .*$/\1/'`
[ $DEBUG -eq 1 ] && echo "Download URL : $DOWNLOAD_URL"
[ ! -n "$DOWNLOAD_URL" ] && DOWNLOAD_URL="$CHECK_URL" && [ $DEBUG -eq 1 ] && echo "Use check URL as download URL : $DOWNLOAD_URL"
2010-05-11 09:48:45 +02:00
rm -f $tmpfile > /dev/null 2>&1
break;
elif [ $i -eq $MAX_CHECK ]
then
2015-01-26 10:12:28 +01:00
[ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current stable version" && exit 1
error="Site du projet injoignable (ou structure du site modifie !!) => Impossible de recuperer le numero de la version stable actuel."
[ $DEBUG -eq 1 ] && echo $error
if [ -f $CACHE_FAILED ]
then
NB=`cat $CACHE_FAILED`
[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur existe : $NB echec. Max : $MAX_FAILED"
if [ $NB -lt $MAX_FAILED ]
then
let NNB=NB+1
[ $DEBUG -eq 1 ] && echo "Augmentation du nb d'erreur dans le fichier de cache d'erreur : $NB -> $NNB"
echo -n $NNB > $CACHE_FAILED
else
[ $DEBUG -eq 1 ] && echo "MAX_FAILED atteint : on envoi un mail"
echo $error|mail -s "New RoundCude release check : FAILED" $MAIL
fi
else
[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur n'existe pas : on l'initialise à 1."
echo -n 1 > $CACHE_FAILED
fi
[ $DEBUG -eq 1 ] && echo "exit 1"
exit 1
fi
[ $DEBUG -eq 1 ] && echo "Check failed ($i/$MAX_CHECK) : Sleep $SLEEP_ON_FAILED second before try again ..."
sleep $SLEEP_ON_FAILED
done
2013-07-03 09:38:32 +02:00
[ $JUST_DISP -eq 1 ] && echo -e "Current stable version : $newest\nDownload URL : $DOWNLOAD_URL" && exit 0
2010-05-11 09:48:45 +02:00
2013-07-03 09:38:32 +02:00
current=`egrep "define.*RCMAIL_VERSION" $RC_HOME/program/include/iniset.php|sed "s/define('.*', '\([^']*\)'.*$/\1/"`
current="`echo $current|sed 's/-rc$//'`"
current="`echo $current|sed 's/-beta$//'`"
[ $DEBUG -eq 1 ] && echo "Current : $current"
2014-12-31 10:08:39 +01:00
if [ -z "$current" ]
then
[ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current installed version" && exit 3
echo "ERROR : Fail to retrieve current version !"
exit 3
fi
2010-05-11 09:48:45 +02:00
if [ "$newest" != "$current" ]
then
2014-12-31 10:08:39 +01:00
if [ $NAGIOS -eq 1 ]
then
2015-01-26 10:12:28 +01:00
echo "WARNING : A newest version of Roundcube Webmail is available (newest : $newest / current : $current)"
2014-12-31 10:08:39 +01:00
exit 2
fi
if [ $DOWNLOAD -eq 1 ]
then
if [ -d "$DOWNLOAD_DIR" ]
then
cd "$DOWNLOAD_DIR"
2011-02-10 12:09:08 +01:00
DOWNLOAD_FILE="$( pwd )/roundcubemail-$newest.tar.gz"
[ $DEBUG -eq 1 ] && echo "Download new release in $DOWNLOAD_FILE"
if [ $CURL -eq 1 ]
then
curl $CURL_OPTS --location --silent --output $DOWNLOAD_FILE $DOWNLOAD_URL
else
wget $WGET_OPTS $WGET_SSL_OPT -q -O "$DOWNLOAD_FILE" "$DOWNLOAD_URL"
fi
2011-02-10 13:16:06 +01:00
if [ $EXTRACT -eq 1 ]
then
[ $DEBUG -eq 1 ] && echo "Extract new release"
tar xzf $DOWNLOAD_FILE
DOWNLOAD_FILE_DIR=$( tar tzf $DOWNLOAD_FILE 2> /dev/null|head -n1|cut -d'/' -f1 )
2011-02-10 13:16:06 +01:00
fi
else
echo "[WARNING] Le dossier de telechargement n'existe pas ($DOWNLOAD_DIR)."
2011-02-10 13:16:06 +01:00
[ $UPGRADE -eq 1 ] && exit 1
fi
fi
2011-02-10 12:09:08 +01:00
2011-02-10 13:16:06 +01:00
if [ $UPGRADE -eq 1 ]
then
cd "$DOWNLOAD_DIR"
default_src="$( pwd )/$DOWNLOAD_FILE_DIR"
2011-02-10 13:16:06 +01:00
src=""
while [ ! -n "$src" ]
do
echo -n "Source de la nouvelle version [$default_src] : "
read a
if [ -n "$a" ]
then
src="$a"
else
src="$default_src"
fi
if [ ! -d "$src" ]
then
echo -n "[WARNING] Le dossier $src n'existe pas."
src=""
2011-02-10 13:16:06 +01:00
[ ! -n "$a" ] && default_src=""
fi
if [ ! -x "$src/bin/installto.sh" ]
then
echo -n "[WARNING] Le script bin/installto.sh n'est pas présent dans ce répertoire source."
src=""
[ ! -n "$a" ] && default_src=""
fi
2011-02-10 13:16:06 +01:00
done
echo "Source : $src"
cd "$RC_HOME/../"
from=""
while [ ! -n "$from" ]
do
echo -n "Installation source [$RC_HOME] : "
read a
if [ -n "$a" ]
then
if [ -d "$a" ]
then
from="$a"
else
from=""
2011-02-10 13:16:06 +01:00
echo "[ERROR] $a n'est pas un dossier valide"
fi
else
from="$RC_HOME"
fi
done
echo "From : $from"
default_to="$( pwd )/roundcubemail-$newest"
to=""
while [ ! -n "$to" ]
do
echo -n "Destination de la nouvelle installation [$default_to] : "
read a
if [ -n "$a" ]
then
to="$a"
else
to="$default_to"
fi
if [ -d "$to" ]
then
echo -n "[WARNING] Le dossier $to existe deja. Celui-ci va etre deplace dans $to.old. Confirmez-vous votre choix [Y/n] ? "
read b
if [ "$b" == "n" -o "$b" == "N" ]
then
to=""
[ ! -n "$a" ] && default_to=""
else
echo "Deplace $to dans $to.old"
mv "$to" "$to.old"
fi
fi
done
echo "To : $to"
echo "Copie de $from dans $to"
rsync -a --copy-dirlinks "$from/" "$to/"
echo "Mise a jour de l'installation $to a partir de $src : "
cd "$src"
./bin/installto.sh "$to"
2011-02-10 12:09:08 +01:00
run_after_upgrade "$to"
2011-02-10 13:16:06 +01:00
if [ -L "$RC_HOME" ]
then
echo -n "Voulez-vous faire pointer le lien symbolique $RC_HOME vers cette nouvelle installation [Y/n] ? "
read a
if [ "$a" != "n" -a "$a" != "N" ]
then
echo " - Suppression de l'actuel lien $RC_HOME"
rm -f $RC_HOME
echo " - Creation du nouveau lien $RC_HOME vers $to"
ln -s "$to" "$RC_HOME"
fi
fi
else
2010-05-11 09:48:45 +02:00
2011-02-10 13:16:06 +01:00
tmp=`mktemp`
echo "New RoundCude release" > $tmp
echo "=====================" >> $tmp
echo "Current : $current" >> $tmp
echo "Newest : $newest" >> $tmp
echo >> $tmp
echo "Download URL : $DOWNLOAD_URL" >> $tmp
[ -n "$DOWNLOAD_FILE" -a -f "$DOWNLOAD_FILE" ] && echo "Download file : $( pwd )/roundcubemail-$newest.tar.gz" >> $tmp
M=0
if [ -f $CACHE ]
2010-05-11 09:48:45 +02:00
then
2011-02-10 13:16:06 +01:00
if [ "`diff $tmp $CACHE`" != "" -o `find $CACHE -mtime +$SEND_EVERY_NB_DAY | wc -l` -eq 1 ]
then
[ $DEBUG -eq 1 ] && echo "Cache trop vieux ou modif => On envoi"
M=1
else
[ $DEBUG -eq 1 ] && echo "Pas de modif et cache trop recent : pas d'envoi"
fi
2010-05-11 09:48:45 +02:00
else
2011-02-10 13:16:06 +01:00
[ $DEBUG -eq 1 ] && echo "Pas encore de cache => On envoi"
M=1
2010-05-11 09:48:45 +02:00
fi
2011-02-10 13:16:06 +01:00
if [ $DEBUG -eq 1 -a $M -eq 1 ]
then
2011-02-10 13:16:06 +01:00
echo "Mode debug : Pas d'envoi de mail"
fi
2011-02-10 13:16:06 +01:00
if [ $M -eq 1 -a $DEBUG -ne 1 ]
then
cat $tmp > $CACHE
if [ -n "$NAME" ]
then
S="[$NAME] New RoundCude release"
else
S="New RoundCude release"
fi
cat $tmp | mail -s "$S : $newest" $MAIL
[ $DEBUG -eq 1 ] && echo "Mail envoyé"
fi
rm -f $tmp
2010-05-11 09:48:45 +02:00
fi
else
[ $DEBUG -eq 1 ] && echo "Pas de changement de version"
[ $NAGIOS -eq 1 ] && echo "OK : Lastest version installed ($current)" && exit 0
2010-05-11 09:48:45 +02:00
fi