95e035deb1fdee8e0d8dc0c5bc24b5a5ced463db
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 13 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 13 years ago

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

root authored 13 years ago

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

root authored 13 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
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

25) JUST_AFTER_UPGRADE=0
root Used getopts and added down...

root authored 13 years ago

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

Benjamin Renard authored 9 years ago

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

root authored 13 years ago

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

Benjamin Renard authored 9 years ago

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

root authored 13 years ago

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

root authored 13 years ago

32) 	echo "  -u   Upgrade installation"
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

33) 	echo "  -a   Just run after_upgrade function"
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

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

root authored 13 years ago

35) 	echo "  -h   Display this help"
36) }
37) 
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

38) while getopts ":dvnhuaj" opt; do
root Used getopts and added down...

root authored 13 years ago

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

Benjamin Renard authored 9 years ago

43) 		n)
44) 			NAGIOS=1
45) 		;;
root Used getopts and added down...

root authored 13 years ago

46) 		d)
47) 			DOWNLOAD=1
48) 		;;
root Added upgrade feature

root authored 13 years ago

49) 		u)
50) 			DOWNLOAD=1
51) 			UPGRADE=1
52) 			DEBUG=1
53) 			EXTRACT=1
54) 		;;
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

55) 		a)
56) 			JUST_AFTER_UPGRADE=1
57) 		;;
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

58) 		j)
59) 			JUST_DISP=1
60) 		;;
root Used getopts and added down...

root authored 13 years ago

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

bn8 authored 14 years ago

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

root authored 13 years ago

81) [ -f "$0.local" ] && source "$0.local" && [ $DEBUG -eq 1 ] && echo "Import local config file : $0.local"
82) 
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

83) function run_after_upgrade() {
84) 	RC_NEW_HOME="$1"
85) 	type after_upgrade > /dev/null 2>&1
86) 	if [ $? -eq 0 ]
87) 	then
88) 		echo "Execution de la fonction custom after_upgrade() :"
89) 		after_upgrade "$RC_NEW_HOME"
90) 		res=$?
91) 		echo "Fin de la fonction after_upgrade() (Retour : $res)"
92) 		return $res
93) 	else
94) 		echo "La fonction 'after_upgrade' n'est pas définie. On passe."
95) 		return 0
96) 	fi
97) }
98) 
99) if [ $JUST_AFTER_UPGRADE -eq 1 ]
100) then
101) 	run_after_upgrade "$RC_HOME"
102) 	exit $?
103) fi
104) 
root Multi check en cas d'erreur...

root authored 13 years ago

105) for i in `seq 1 $MAX_CHECK`
106) do
107) 	tmpfile=`mktemp`
108) 	wget -q $CHECK_URL -O $tmpfile
Benjamin Renard Fixed after website structu...

Benjamin Renard authored 11 years ago

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

root authored 13 years ago

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

Benjamin Renard authored 9 years ago

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

root authored 13 years ago

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

Benjamin Renard authored 12 years ago

115) 		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 13 years ago

116) 		[ $DEBUG -eq 1 ] && echo "Download URL : $DOWNLOAD_URL"
117) 		[ ! -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

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

root authored 13 years ago

119) 		rm -f $tmpfile > /dev/null 2>&1
120) 		break;
121) 	elif [ $i -eq $MAX_CHECK ]
122) 	then
Benjamin Renard Fix Nagios mode return levels

Benjamin Renard authored 9 years ago

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

root authored 13 years ago

124) 		error="Site du projet injoignable (ou structure du site modifie !!) => Impossible de recuperer le numero de la version stable actuel."
125) 		[ $DEBUG -eq 1 ] && echo $error
126) 		if [ -f $CACHE_FAILED ]
127) 		then
128) 			NB=`cat $CACHE_FAILED`
129) 			[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur existe : $NB echec. Max : $MAX_FAILED"
130) 			if [ $NB -lt $MAX_FAILED ]
131) 			then
132) 				let NNB=NB+1
133) 				[ $DEBUG -eq 1 ] && echo "Augmentation du nb d'erreur dans le fichier de cache d'erreur : $NB -> $NNB"
134) 				echo -n $NNB > $CACHE_FAILED
135) 			else
136) 				[ $DEBUG -eq 1 ] && echo "MAX_FAILED atteint : on envoi un mail"
137) 				echo $error|mail -s "New RoundCude release check : FAILED" $MAIL
138) 			fi
139) 		else
140) 			[ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur n'existe pas : on l'initialise à 1."
141) 			echo -n 1 > $CACHE_FAILED
142) 		fi
143) 		[ $DEBUG -eq 1 ] && echo "exit 1"
144) 		exit 1
145) 	fi
146) 	[ $DEBUG -eq 1 ] && echo "Check failed ($i/$MAX_CHECK) : Sleep $SLEEP_ON_FAILED second before try again ..."
147) 	sleep $SLEEP_ON_FAILED
148) done
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

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

bn8 authored 14 years ago

150) 
Benjamin Renard Added -j parameter

Benjamin Renard authored 11 years ago

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

Benjamin Renard authored 9 years ago

156) 
157) if [ -z "$current" ]
158) then
159) 	[ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current installed version" && exit 3
160) 	echo "ERROR : Fail to retrieve current version !"
161) 	exit 3
162) fi
163) 
bn8 Import initial

bn8 authored 14 years ago

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

Benjamin Renard authored 9 years ago

166) 	if [ $NAGIOS -eq 1 ]
167) 	then
Benjamin Renard Fix Nagios mode return levels

Benjamin Renard authored 9 years ago

168) 		echo "WARNING : A newest version of Roundcube Webmail is available (newest : $newest / current : $current)"
Benjamin Renard Add Nagios mode

Benjamin Renard authored 9 years ago

169) 		exit 2
170) 	fi
root Used getopts and added down...

root authored 13 years ago

171) 
172) 	if [ $DOWNLOAD -eq 1 ]
173) 	then
174) 		if [ -d "$DOWNLOAD_DIR" ]
175) 		then
176) 			cd "$DOWNLOAD_DIR"
root Change order for download f...

root authored 13 years ago

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

root authored 13 years ago

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

root authored 13 years ago

180) 			if [ $EXTRACT -eq 1 ]
181) 			then
182) 				[ $DEBUG -eq 1 ] && echo "Extract new release"
183) 				tar xzf $DOWNLOAD_FILE
Benjamin Renard Added auto-detection of dir...

Benjamin Renard authored 12 years ago

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

root authored 13 years ago

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

root authored 13 years ago

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

root authored 13 years ago

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

root authored 13 years ago

189) 		fi
190) 	fi
root Change order for download f...

root authored 13 years ago

191) 
root Added upgrade feature

root authored 13 years ago

192) 	if [ $UPGRADE -eq 1 ]
193) 	then
194) 		cd "$DOWNLOAD_DIR"
Benjamin Renard Added auto-detection of dir...

Benjamin Renard authored 12 years ago

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

root authored 13 years ago

196) 		src=""
197) 		while [ ! -n "$src" ]
198) 		do 
199) 			echo -n "Source de la nouvelle version [$default_src] : "
200) 			read a
201) 			if [ -n "$a" ]
202) 			then
203) 				src="$a"
204) 			else
205) 				src="$default_src"
206) 			fi
207) 			if [ ! -d "$src" ]
208) 			then
209) 				echo -n "[WARNING] Le dossier $src n'existe pas."
Benjamin Renard Fixed bug when asking for s...

Benjamin Renard authored 12 years ago

210) 				src=""
root Added upgrade feature

root authored 13 years ago

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

Benjamin Renard authored 10 years ago

213) 			if [ ! -x "$src/bin/installto.sh" ]
214) 			then
215) 				echo -n "[WARNING] Le script bin/installto.sh n'est pas présent dans ce répertoire source."
216) 				src=""
217) 				[ ! -n "$a" ] && default_src=""
218) 			fi
root Added upgrade feature

root authored 13 years ago

219) 		done
220) 		echo "Source : $src"
221) 
222) 		cd "$RC_HOME/../"
223) 		from=""
224) 		while [ ! -n "$from" ]
225) 		do 
226) 			echo -n "Installation source [$RC_HOME] : "
227) 			read a
228) 			if [ -n "$a" ]
229) 			then
230) 				if [ -d "$a" ]
231) 				then
232) 					from="$a"
233) 				else
Benjamin Renard Fixed bug when asking for s...

Benjamin Renard authored 12 years ago

234) 					from=""
root Added upgrade feature

root authored 13 years ago

235) 					echo "[ERROR] $a n'est pas un dossier valide"
236) 				fi
237) 			else
238) 				from="$RC_HOME"
239) 			fi	
240) 		done
241) 		echo "From : $from"
242) 		default_to="$( pwd )/roundcubemail-$newest"
243) 		to=""
244) 		while [ ! -n "$to" ]
245) 		do
246) 			echo -n "Destination de la nouvelle installation [$default_to] : "
247) 			read a
248) 			if [ -n "$a" ]
249) 			then
250) 				to="$a"
251) 			else
252) 				to="$default_to"
253) 			fi
254) 			if [ -d "$to" ]
255) 			then
256) 				echo -n "[WARNING] Le dossier $to existe deja. Celui-ci va etre deplace dans $to.old. Confirmez-vous votre choix [Y/n] ? "
257) 				read b
258) 				if [ "$b" == "n" -o "$b" == "N" ]
259) 				then
260) 					to=""
261) 					[ ! -n "$a" ] && default_to=""
262) 				else
263) 					echo "Deplace $to dans $to.old"
264) 					mv "$to" "$to.old"
265) 				fi
266) 			fi
267) 		done
268) 		echo "To : $to"
269) 		echo "Copie de $from dans $to"
270) 		rsync -a --copy-dirlinks "$from/" "$to/"
271) 
272) 		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

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

root authored 13 years ago

275) 
Benjamin Renard Add -a parameter to only ru...

Benjamin Renard authored 9 years ago

276) 		run_after_upgrade "$to"
Benjamin Renard Add after_upgrade() functio...

Benjamin Renard authored 10 years ago

277) 
root Added upgrade feature

root authored 13 years ago

278) 		if [ -L "$RC_HOME" ]
279) 		then
280) 			echo -n "Voulez-vous faire pointer le lien symbolique $RC_HOME vers cette nouvelle installation [Y/n] ? "
281) 			read a
282) 			if [ "$a" != "n" -a "$a" != "N" ]
283) 			then
284) 				echo "  - Suppression de l'actuel lien $RC_HOME"
285) 				rm -f $RC_HOME
286) 				echo "  - Creation du nouveau lien $RC_HOME vers $to"
287) 				ln -s "$to" "$RC_HOME"
288) 			fi
289) 		fi
290) 	else 
bn8 Import initial

bn8 authored 14 years ago

291) 	
root Added upgrade feature

root authored 13 years ago

292) 		tmp=`mktemp`
293) 		echo "New RoundCude release" > $tmp
294) 		echo "=====================" >> $tmp
295) 		echo "Current : $current" >> $tmp
296) 		echo "Newest : $newest" >> $tmp
297) 		echo >> $tmp
298) 		echo "Download URL : $DOWNLOAD_URL" >> $tmp
299) 		[ -n "$DOWNLOAD_FILE" -a -f "$DOWNLOAD_FILE" ] && echo "Download file : $( pwd )/roundcubemail-$newest.tar.gz" >> $tmp
300) 	
301) 		
302) 		M=0
303) 		if [ -f $CACHE ]
bn8 Import initial

bn8 authored 14 years ago

304) 		then
root Added upgrade feature

root authored 13 years ago

305) 			if [ "`diff $tmp $CACHE`" != "" -o `find $CACHE -mtime +$SEND_EVERY_NB_DAY | wc -l` -eq 1 ]
306) 			then
307) 				[ $DEBUG -eq 1 ] && echo "Cache trop vieux ou modif => On envoi"
308) 				M=1
309) 			else
310) 				[ $DEBUG -eq 1 ] && echo "Pas de modif et cache trop recent : pas d'envoi"
311) 			fi
bn8 Import initial

bn8 authored 14 years ago

312) 		else
root Added upgrade feature

root authored 13 years ago

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

bn8 authored 14 years ago

315) 		fi
root Added upgrade feature

root authored 13 years ago

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

Benjamin Renard authored 13 years ago

317) 		then
root Added upgrade feature

root authored 13 years ago

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

Benjamin Renard authored 13 years ago

319) 		fi
root Added upgrade feature

root authored 13 years ago

320) 		if [ $M -eq 1 -a $DEBUG -ne 1 ]
321) 		then
322) 			cat $tmp > $CACHE
323) 			if [ -n "$NAME" ]
324) 			then
325) 				S="[$NAME] New RoundCude release"
326) 			else
327) 				S="New RoundCude release"
328) 			fi
329) 			cat $tmp | mail -s "$S : $newest" $MAIL
330) 			[ $DEBUG -eq 1 ] && echo "Mail envoyé"
331) 		fi
332) 		rm -f $tmp
333) 
bn8 Import initial

bn8 authored 14 years ago

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

Benjamin Renard authored 9 years ago

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