029aec7b9e45d29119af1b8e5bff2824ca57c4ac
bn8 Import initial

bn8 authored 14 years ago

1) #!/bin/bash
2) 
Benjamin Renard Remove OWNER and GROUP depr...

Benjamin Renard authored 9 years ago

3) ### CONFIGURATION ###
4) 
Benjamin Renard Ajout d'un paramètre NAME p...

Benjamin Renard authored 13 years ago

5) #NAME="My Roundcube"
root Multi check en cas d'erreur...

root authored 14 years ago

6) RC_HOME=/var/www/webmail/public_html
bn8 Import initial

bn8 authored 14 years ago

7) MAIL=root
root Multi check en cas d'erreur...

root authored 14 years ago

8) SEND_EVERY_NB_DAY=7
root Ajout de l'inclusion d'un f...

root authored 14 years ago

9) CACHE=$RC_HOME/../.cache_check_upgrade
root Multi check en cas d'erreur...

root authored 14 years ago

10) CACHE_FAILED=$CACHE.failed
11) MAX_CHECK=10
12) MAX_FAILED=3
13) SLEEP_ON_FAILED=10
14) CHECK_URL=http://www.roundcube.net/download
root Change order for download f...

root authored 13 years ago

15) DOWNLOAD=0
root Used getopts and added down...

root authored 13 years ago

16) DOWNLOAD_DIR=$RC_HOME/../upstream
Benjamin Renard Remove OWNER and GROUP depr...

Benjamin Renard authored 9 years ago

17) 
18) ### DO NOT MODIFY AFTER THIS LINE ###
bn8 Import initial

bn8 authored 14 years ago

19) 
20) DEBUG=0
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

21) NAGIOS=0
root Added upgrade feature

root authored 13 years ago

22) UPGRADE=0
23) EXTRACT=0
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

24) JUST_DISP=0
root Used getopts and added down...

root authored 13 years ago

25) 
26) function usage() {
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

27) 	echo "Usage : $0 [-v] [-n] [-d] [-u]"
root Used getopts and added down...

root authored 13 years ago

28) 	echo "  -v   Verbose mode"
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

29) 	echo "  -n   Nagios mode"
root Used getopts and added down...

root authored 13 years ago

30) 	echo "  -d   Download new release"
root Added upgrade feature

root authored 13 years ago

31) 	echo "  -u   Upgrade installation"
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

32) 	echo "  -j   Just display current stable version"
root Used getopts and added down...

root authored 13 years ago

33) 	echo "  -h   Display this help"
34) }
35) 
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

36) while getopts ":dvnhuj" opt; do
root Used getopts and added down...

root authored 13 years ago

37) 	case $opt in
38) 		v)
39) 			DEBUG=1
40) 		;;
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

41) 		n)
42) 			NAGIOS=1
43) 		;;
root Used getopts and added down...

root authored 13 years ago

44) 		d)
45) 			DOWNLOAD=1
46) 		;;
root Added upgrade feature

root authored 13 years ago

47) 		u)
48) 			DOWNLOAD=1
49) 			UPGRADE=1
50) 			DEBUG=1
51) 			EXTRACT=1
52) 		;;
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

53) 		j)
54) 			JUST_DISP=1
55) 		;;
root Used getopts and added down...

root authored 13 years ago

56) 		h)
57) 			usage
58) 			exit 0
59) 		;;
60) 		\?)
61)                         echo "Invalid option: -$OPTARG" >&2
62) 			echo
63) 			usage
64) 			exit 1
65) 		;;
66) 		:)
67)                         echo "Option -$OPTARG requires an argument." >&2
68) 			echo
69) 			usage
70)                         exit 1
71)                 ;;
72) 
73) 	esac
74) done
bn8 Import initial

bn8 authored 14 years ago

75) 
root Ajout de l'inclusion d'un f...

root authored 14 years ago

76) [ -f "$0.local" ] && source "$0.local" && [ $DEBUG -eq 1 ] && echo "Import local config file : $0.local"
77) 
root Multi check en cas d'erreur...

root authored 14 years ago

78) for i in `seq 1 $MAX_CHECK`
79) do
80) 	tmpfile=`mktemp`
81) 	wget -q $CHECK_URL -O $tmpfile
Benjamin Renard Fixed after website structu...

Benjamin Renard authored 11 years ago

82) 	newest=`cat $tmpfile|grep dlversion|grep Complete|head -n 1|sed 's/^.*: \([0-9\.]*\).*$/\1/'`
root Multi check en cas d'erreur...

root authored 14 years ago

83) 	if [ -n "$newest" ]
84) 	then
85) 		[ $DEBUG -eq 1 ] && echo "Newest : $newest"
Benjamin Renard Do not remove failed cache...

Benjamin Renard authored 9 years ago

86) 		[ $NAGIOS -ne 1 ] && rm -f $CACHE_FAILED > /dev/null 2>&1
root Multi check en cas d'erreur...

root authored 14 years ago

87) 
Benjamin Renard change regex to match downl...

Benjamin Renard authored 12 years ago

88) 		DOWNLOAD_URL=`cat $tmpfile|grep "$newest"|grep "tar.gz"|head -1|sed 's/.* href="[ \t]*\([^"]*\)[ \t]*" .*$/\1/'`
root Multi check en cas d'erreur...

root authored 14 years ago

89) 		[ $DEBUG -eq 1 ] && echo "Download URL : $DOWNLOAD_URL"
90) 		[ ! -n "$DOWNLOAD_URL" ] && DOWNLOAD_URL="$CHECK_URL" && [ $DEBUG -eq 1 ] && echo "Use check URL as download URL : $DOWNLOAD_URL"
bn8 Import initial

bn8 authored 14 years ago

91) 
root Multi check en cas d'erreur...

root authored 14 years ago

92) 		rm -f $tmpfile > /dev/null 2>&1
93) 		break;
94) 	elif [ $i -eq $MAX_CHECK ]
95) 	then
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

96) 		[ $NAGIOS -eq 1 ] && echo "WARNING : Fail to retrieve current stable version" && exit 1
root Multi check en cas d'erreur...

root authored 14 years ago

97) 		error="Site du projet injoignable (ou structure du site modifie !!) => Impossible de recuperer le numero de la version stable actuel."
98) 		[ $DEBUG -eq 1 ] && echo $error
99) 		if [ -f $CACHE_FAILED ]
100) 		then
101) 			NB=`cat $CACHE_FAILED`
102) 			[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur existe : $NB echec. Max : $MAX_FAILED"
103) 			if [ $NB -lt $MAX_FAILED ]
104) 			then
105) 				let NNB=NB+1
106) 				[ $DEBUG -eq 1 ] && echo "Augmentation du nb d'erreur dans le fichier de cache d'erreur : $NB -> $NNB"
107) 				echo -n $NNB > $CACHE_FAILED
108) 			else
109) 				[ $DEBUG -eq 1 ] && echo "MAX_FAILED atteint : on envoi un mail"
110) 				echo $error|mail -s "New RoundCude release check : FAILED" $MAIL
111) 			fi
112) 		else
113) 			[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur n'existe pas : on l'initialise à 1."
114) 			echo -n 1 > $CACHE_FAILED
115) 		fi
116) 		[ $DEBUG -eq 1 ] && echo "exit 1"
117) 		exit 1
118) 	fi
119) 	[ $DEBUG -eq 1 ] && echo "Check failed ($i/$MAX_CHECK) : Sleep $SLEEP_ON_FAILED second before try again ..."
120) 	sleep $SLEEP_ON_FAILED
121) done
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

122) [ $JUST_DISP -eq 1 ] && echo -e "Current stable version : $newest\nDownload URL : $DOWNLOAD_URL" && exit 0
bn8 Import initial

bn8 authored 14 years ago

123) 
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

124) current=`egrep "define.*RCMAIL_VERSION" $RC_HOME/program/include/iniset.php|sed "s/define('.*', '\([^']*\)'.*$/\1/"`
125) current="`echo $current|sed 's/-rc$//'`"
126) current="`echo $current|sed 's/-beta$//'`"
127) 
128) [ $DEBUG -eq 1 ] && echo "Current : $current"
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

129) 
130) if [ -z "$current" ]
131) then
132) 	[ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current installed version" && exit 3
133) 	echo "ERROR : Fail to retrieve current version !"
134) 	exit 3
135) fi
136) 
bn8 Import initial

bn8 authored 14 years ago

137) if [ "$newest" != "$current" ]
138) then
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

139) 	if [ $NAGIOS -eq 1 ]
140) 	then
141) 		echo "CRITICAL : A newest version of Roundcube Webmail is available (newest : $newest / current : $current)"
142) 		exit 2
143) 	fi
root Used getopts and added down...

root authored 13 years ago

144) 
145) 	if [ $DOWNLOAD -eq 1 ]
146) 	then
147) 		if [ -d "$DOWNLOAD_DIR" ]
148) 		then
149) 			cd "$DOWNLOAD_DIR"
root Change order for download f...

root authored 13 years ago

150) 			DOWNLOAD_FILE="$( pwd )/roundcubemail-$newest.tar.gz"
151) 			[ $DEBUG -eq 1 ] && echo "Download new release in $DOWNLOAD_FILE"
root Fixed bug in last comit

root authored 13 years ago

152) 			wget -q -O "$DOWNLOAD_FILE" "$DOWNLOAD_URL"
root Added upgrade feature

root authored 13 years ago

153) 			if [ $EXTRACT -eq 1 ]
154) 			then
155) 				[ $DEBUG -eq 1 ] && echo "Extract new release"
156) 				tar xzf $DOWNLOAD_FILE
Benjamin Renard Added auto-detection of dir...

Benjamin Renard authored 13 years ago

157) 				DOWNLOAD_FILE_DIR=$( tar tzf $DOWNLOAD_FILE 2> /dev/null|head -n1|cut -d'/' -f1 )
root Added upgrade feature

root authored 13 years ago

158) 			fi
root Used getopts and added down...

root authored 13 years ago

159) 		else
160) 			echo "[WARNING] Le dossier de telechargement n'existe pas ($DOWNLOAD_DIR)."
root Added upgrade feature

root authored 13 years ago

161) 			[ $UPGRADE -eq 1 ] && exit 1
root Used getopts and added down...

root authored 13 years ago

162) 		fi
163) 	fi
root Change order for download f...

root authored 13 years ago

164) 
root Added upgrade feature

root authored 13 years ago

165) 	if [ $UPGRADE -eq 1 ]
166) 	then
167) 		cd "$DOWNLOAD_DIR"
Benjamin Renard Added auto-detection of dir...

Benjamin Renard authored 13 years ago

168) 		default_src="$( pwd )/$DOWNLOAD_FILE_DIR"
root Added upgrade feature

root authored 13 years ago

169) 		src=""
170) 		while [ ! -n "$src" ]
171) 		do 
172) 			echo -n "Source de la nouvelle version [$default_src] : "
173) 			read a
174) 			if [ -n "$a" ]
175) 			then
176) 				src="$a"
177) 			else
178) 				src="$default_src"
179) 			fi
180) 			if [ ! -d "$src" ]
181) 			then
182) 				echo -n "[WARNING] Le dossier $src n'existe pas."
Benjamin Renard Fixed bug when asking for s...

Benjamin Renard authored 13 years ago

183) 				src=""
root Added upgrade feature

root authored 13 years ago

184) 				[ ! -n "$a" ] && default_src=""
185) 			fi
Benjamin Renard Upgrade script to use bin/i...

Benjamin Renard authored 10 years ago

186) 			if [ ! -x "$src/bin/installto.sh" ]
187) 			then
188) 				echo -n "[WARNING] Le script bin/installto.sh n'est pas présent dans ce répertoire source."
189) 				src=""
190) 				[ ! -n "$a" ] && default_src=""
191) 			fi
root Added upgrade feature

root authored 13 years ago

192) 		done
193) 		echo "Source : $src"
194) 
195) 		cd "$RC_HOME/../"
196) 		from=""
197) 		while [ ! -n "$from" ]
198) 		do 
199) 			echo -n "Installation source [$RC_HOME] : "
200) 			read a
201) 			if [ -n "$a" ]
202) 			then
203) 				if [ -d "$a" ]
204) 				then
205) 					from="$a"
206) 				else
Benjamin Renard Fixed bug when asking for s...

Benjamin Renard authored 13 years ago

207) 					from=""
root Added upgrade feature

root authored 13 years ago

208) 					echo "[ERROR] $a n'est pas un dossier valide"
209) 				fi
210) 			else
211) 				from="$RC_HOME"
212) 			fi	
213) 		done
214) 		echo "From : $from"
215) 		default_to="$( pwd )/roundcubemail-$newest"
216) 		to=""
217) 		while [ ! -n "$to" ]
218) 		do
219) 			echo -n "Destination de la nouvelle installation [$default_to] : "
220) 			read a
221) 			if [ -n "$a" ]
222) 			then
223) 				to="$a"
224) 			else
225) 				to="$default_to"
226) 			fi
227) 			if [ -d "$to" ]
228) 			then
229) 				echo -n "[WARNING] Le dossier $to existe deja. Celui-ci va etre deplace dans $to.old. Confirmez-vous votre choix [Y/n] ? "
230) 				read b
231) 				if [ "$b" == "n" -o "$b" == "N" ]
232) 				then
233) 					to=""
234) 					[ ! -n "$a" ] && default_to=""
235) 				else
236) 					echo "Deplace $to dans $to.old"
237) 					mv "$to" "$to.old"
238) 				fi
239) 			fi
240) 		done
241) 		echo "To : $to"
242) 		echo "Copie de $from dans $to"
243) 		rsync -a --copy-dirlinks "$from/" "$to/"
244) 
245) 		echo "Mise a jour de l'installation $to a partir de $src : "
Benjamin Renard Upgrade script to use bin/i...

Benjamin Renard authored 10 years ago

246) 		cd "$src"
247) 		./bin/installto.sh "$to"
root Change order for download f...

root authored 13 years ago

248) 
Benjamin Renard Add after_upgrade() functio...

Benjamin Renard authored 10 years ago

249) 		type after_upgrade > /dev/null 2>&1
250) 		if [ $? -eq 0 ]
251) 		then
252) 			echo "Execution de la fonction custom after_upgrade() :"
Benjamin Renard Pass installation path to a...

Benjamin Renard authored 9 years ago

253) 			after_upgrade "$to"
Benjamin Renard Add after_upgrade() functio...

Benjamin Renard authored 10 years ago

254) 			echo "Fin de la fonction after_upgrade()"
255) 		fi
256) 
root Added upgrade feature

root authored 13 years ago

257) 		if [ -L "$RC_HOME" ]
258) 		then
259) 			echo -n "Voulez-vous faire pointer le lien symbolique $RC_HOME vers cette nouvelle installation [Y/n] ? "
260) 			read a
261) 			if [ "$a" != "n" -a "$a" != "N" ]
262) 			then
263) 				echo "  - Suppression de l'actuel lien $RC_HOME"
264) 				rm -f $RC_HOME
265) 				echo "  - Creation du nouveau lien $RC_HOME vers $to"
266) 				ln -s "$to" "$RC_HOME"
267) 			fi
268) 		fi
269) 	else 
bn8 Import initial

bn8 authored 14 years ago

270) 	
root Added upgrade feature

root authored 13 years ago

271) 		tmp=`mktemp`
272) 		echo "New RoundCude release" > $tmp
273) 		echo "=====================" >> $tmp
274) 		echo "Current : $current" >> $tmp
275) 		echo "Newest : $newest" >> $tmp
276) 		echo >> $tmp
277) 		echo "Download URL : $DOWNLOAD_URL" >> $tmp
278) 		[ -n "$DOWNLOAD_FILE" -a -f "$DOWNLOAD_FILE" ] && echo "Download file : $( pwd )/roundcubemail-$newest.tar.gz" >> $tmp
279) 	
280) 		
281) 		M=0
282) 		if [ -f $CACHE ]
bn8 Import initial

bn8 authored 14 years ago

283) 		then
root Added upgrade feature

root authored 13 years ago

284) 			if [ "`diff $tmp $CACHE`" != "" -o `find $CACHE -mtime +$SEND_EVERY_NB_DAY | wc -l` -eq 1 ]
285) 			then
286) 				[ $DEBUG -eq 1 ] && echo "Cache trop vieux ou modif => On envoi"
287) 				M=1
288) 			else
289) 				[ $DEBUG -eq 1 ] && echo "Pas de modif et cache trop recent : pas d'envoi"
290) 			fi
bn8 Import initial

bn8 authored 14 years ago

291) 		else
root Added upgrade feature

root authored 13 years ago

292) 			[ $DEBUG -eq 1 ] && echo "Pas encore de cache => On envoi"
293) 			M=1
bn8 Import initial

bn8 authored 14 years ago

294) 		fi
root Added upgrade feature

root authored 13 years ago

295) 		if [ $DEBUG -eq 1 -a $M -eq 1 ]
Benjamin Renard Desactivation de l'envoi de...

Benjamin Renard authored 13 years ago

296) 		then
root Added upgrade feature

root authored 13 years ago

297) 			echo "Mode debug : Pas d'envoi de mail"
Benjamin Renard Desactivation de l'envoi de...

Benjamin Renard authored 13 years ago

298) 		fi
root Added upgrade feature

root authored 13 years ago

299) 		if [ $M -eq 1 -a $DEBUG -ne 1 ]
300) 		then
301) 			cat $tmp > $CACHE
302) 			if [ -n "$NAME" ]
303) 			then
304) 				S="[$NAME] New RoundCude release"
305) 			else
306) 				S="New RoundCude release"
307) 			fi
308) 			cat $tmp | mail -s "$S : $newest" $MAIL
309) 			[ $DEBUG -eq 1 ] && echo "Mail envoyé"
310) 		fi
311) 		rm -f $tmp
312) 
bn8 Import initial

bn8 authored 14 years ago

313) 	fi
314) else
315) 	[ $DEBUG -eq 1 ] && echo "Pas de changement de version"
Benjamin Renard Fix return code in OK case...

Benjamin Renard authored 9 years ago

316) 	[ $NAGIOS -eq 1 ] && echo "OK : Lastest version installed" && exit 0