+

Last commit for check_rc_upgrade.sh: 6d9a8089e73ba301e522c16c212f5757d118a27d

Add --location parameter in Curl mode to handle redirect

Benjamin Renard [2017-04-28 15:11:48]
Add --location parameter in Curl mode to handle redirect
  1. #!/bin/bash
  2.  
  3. ### CONFIGURATION ###
  4.  
  5. #NAME="My Roundcube"
  6. RC_HOME=/var/www/webmail/public_html
  7. MAIL=root
  8. SEND_EVERY_NB_DAY=7
  9. CACHE=$RC_HOME/../.cache_check_upgrade
  10. CACHE_FAILED=$CACHE.failed
  11. MAX_CHECK=10
  12. MAX_FAILED=3
  13. SLEEP_ON_FAILED=10
  14. CHECK_URL=https://roundcube.net/download/
  15. DOWNLOAD=0
  16. DOWNLOAD_DIR=$RC_HOME/../upstream
  17. DONT_CHECK_SSL_CERT=0
  18. WGET_OPTS=""
  19. CURL_OPTS=""
  20.  
  21. ### DO NOT MODIFY AFTER THIS LINE ###
  22.  
  23. DEBUG=0
  24. NAGIOS=0
  25. UPGRADE=0
  26. CURL=0
  27. EXTRACT=0
  28. JUST_DISP=0
  29. JUST_AFTER_UPGRADE=0
  30.  
  31. function usage() {
  32. echo "Usage : $0 [-v] [-n] [-d] [-u] [-c]"
  33. echo " -v Verbose mode"
  34. echo " -n Nagios mode"
  35. echo " -S Don't check SSL certificate (Wget only)"
  36. echo " -d Download new release"
  37. echo " -u Upgrade installation"
  38. echo " -c Use curl instead of wget"
  39. echo " -a Just run after_upgrade function"
  40. echo " -j Just display current stable version"
  41. echo " -h Display this help"
  42. }
  43.  
  44. while getopts ":dvnShucaj" opt; do
  45. case $opt in
  46. v)
  47. DEBUG=1
  48. ;;
  49. n)
  50. NAGIOS=1
  51. ;;
  52. S)
  53. DONT_CHECK_SSL_CERT=1
  54. ;;
  55. d)
  56. DOWNLOAD=1
  57. ;;
  58. u)
  59. DOWNLOAD=1
  60. UPGRADE=1
  61. DEBUG=1
  62. EXTRACT=1
  63. ;;
  64. c)
  65. CURL=1
  66. ;;
  67. a)
  68. JUST_AFTER_UPGRADE=1
  69. ;;
  70. j)
  71. JUST_DISP=1
  72. ;;
  73. h)
  74. usage
  75. exit 0
  76. ;;
  77. \?)
  78. echo "Invalid option: -$OPTARG" >&2
  79. echo
  80. usage
  81. exit 1
  82. ;;
  83. :)
  84. echo "Option -$OPTARG requires an argument." >&2
  85. echo
  86. usage
  87. exit 1
  88. ;;
  89.  
  90. esac
  91. done
  92.  
  93. [ -f "$0.local" ] && source "$0.local" && [ $DEBUG -eq 1 ] && echo "Import local config file : $0.local"
  94.  
  95. function run_after_upgrade() {
  96. RC_NEW_HOME="$1"
  97. type after_upgrade > /dev/null 2>&1
  98. if [ $? -eq 0 ]
  99. then
  100. echo "Execution de la fonction custom after_upgrade() :"
  101. after_upgrade "$RC_NEW_HOME"
  102. res=$?
  103. echo "Fin de la fonction after_upgrade() (Retour : $res)"
  104. return $res
  105. else
  106. echo "La fonction 'after_upgrade' n'est pas définie. On passe."
  107. return 0
  108. fi
  109. }
  110.  
  111. if [ $JUST_AFTER_UPGRADE -eq 1 ]
  112. then
  113. run_after_upgrade "$RC_HOME"
  114. exit $?
  115. fi
  116.  
  117. WGET_SSL_OPT=""
  118. if [ "$DONT_CHECK_SSL_CERT" == "1" ]
  119. then
  120. [ $DEBUG -eq 1 ] && echo "Disable SSL certificate check"
  121. WGET_SSL_OPT="--no-check-certificate"
  122. fi
  123.  
  124. for i in `seq 1 $MAX_CHECK`
  125. do
  126. tmpfile=`mktemp`
  127. if [ $CURL -eq 1 ]
  128. then
  129. curl $CURL_OPTS --location --silent --output $tmpfile $CHECK_URL
  130. else
  131. wget $WGET_OPTS $WGET_SSL_OPT -q $CHECK_URL -O $tmpfile
  132. fi
  133. newest=`cat $tmpfile|grep 'github.com'|grep 'complete.tar.gz'|head -n 1|sed 's|^.*github.com/roundcube/roundcubemail/releases/download/\([^/]*\)/roundcubemail-.*$|\1|'`
  134. if [ -n "$newest" ]
  135. then
  136. [ $DEBUG -eq 1 ] && echo "Newest : $newest"
  137. [ $NAGIOS -ne 1 ] && rm -f $CACHE_FAILED > /dev/null 2>&1
  138.  
  139. DOWNLOAD_URL=`cat $tmpfile|grep "$newest"|grep "\-complete.tar.gz"|head -1|sed 's/.* href="[ \t]*\([^"]*\)[ \t]*" .*$/\1/'`
  140. [ $DEBUG -eq 1 ] && echo "Download URL : $DOWNLOAD_URL"
  141. [ ! -n "$DOWNLOAD_URL" ] && DOWNLOAD_URL="$CHECK_URL" && [ $DEBUG -eq 1 ] && echo "Use check URL as download URL : $DOWNLOAD_URL"
  142.  
  143. rm -f $tmpfile > /dev/null 2>&1
  144. break;
  145. elif [ $i -eq $MAX_CHECK ]
  146. then
  147. [ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current stable version" && exit 1
  148. error="Site du projet injoignable (ou structure du site modifie !!) => Impossible de recuperer le numero de la version stable actuel."
  149. [ $DEBUG -eq 1 ] && echo $error
  150. if [ -f $CACHE_FAILED ]
  151. then
  152. NB=`cat $CACHE_FAILED`
  153. [ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur existe : $NB echec. Max : $MAX_FAILED"
  154. if [ $NB -lt $MAX_FAILED ]
  155. then
  156. let NNB=NB+1
  157. [ $DEBUG -eq 1 ] && echo "Augmentation du nb d'erreur dans le fichier de cache d'erreur : $NB -> $NNB"
  158. echo -n $NNB > $CACHE_FAILED
  159. else
  160. [ $DEBUG -eq 1 ] && echo "MAX_FAILED atteint : on envoi un mail"
  161. echo $error|mail -s "New RoundCude release check : FAILED" $MAIL
  162. fi
  163. else
  164. [ $DEBUG -eq 1 ] && echo "Fichier de cache d'erreur n'existe pas : on l'initialise à 1."
  165. echo -n 1 > $CACHE_FAILED
  166. fi
  167. [ $DEBUG -eq 1 ] && echo "exit 1"
  168. exit 1
  169. fi
  170. [ $DEBUG -eq 1 ] && echo "Check failed ($i/$MAX_CHECK) : Sleep $SLEEP_ON_FAILED second before try again ..."
  171. sleep $SLEEP_ON_FAILED
  172. done
  173. [ $JUST_DISP -eq 1 ] && echo -e "Current stable version : $newest\nDownload URL : $DOWNLOAD_URL" && exit 0
  174.  
  175. current=`egrep "define.*RCMAIL_VERSION" $RC_HOME/program/include/iniset.php|sed "s/define('.*', '\([^']*\)'.*$/\1/"`
  176. current="`echo $current|sed 's/-rc$//'`"
  177. current="`echo $current|sed 's/-beta$//'`"
  178.  
  179. [ $DEBUG -eq 1 ] && echo "Current : $current"
  180.  
  181. if [ -z "$current" ]
  182. then
  183. [ $NAGIOS -eq 1 ] && echo "UNKNOWN : Fail to retrieve current installed version" && exit 3
  184. echo "ERROR : Fail to retrieve current version !"
  185. exit 3
  186. fi
  187.  
  188. if [ "$newest" != "$current" ]
  189. then
  190. if [ $NAGIOS -eq 1 ]
  191. then
  192. echo "WARNING : A newest version of Roundcube Webmail is available (newest : $newest / current : $current)"
  193. exit 2
  194. fi
  195.  
  196. if [ $DOWNLOAD -eq 1 ]
  197. then
  198. if [ -d "$DOWNLOAD_DIR" ]
  199. then
  200. cd "$DOWNLOAD_DIR"
  201. DOWNLOAD_FILE="$( pwd )/roundcubemail-$newest.tar.gz"
  202. [ $DEBUG -eq 1 ] && echo "Download new release in $DOWNLOAD_FILE"
  203. if [ $CURL -eq 1 ]
  204. then
  205. curl $CURL_OPTS --location --silent --output $DOWNLOAD_FILE $DOWNLOAD_URL
  206. else
  207. wget $WGET_OPTS $WGET_SSL_OPT -q -O "$DOWNLOAD_FILE" "$DOWNLOAD_URL"
  208. fi
  209. if [ $EXTRACT -eq 1 ]
  210. then
  211. [ $DEBUG -eq 1 ] && echo "Extract new release"
  212. tar xzf $DOWNLOAD_FILE
  213. DOWNLOAD_FILE_DIR=$( tar tzf $DOWNLOAD_FILE 2> /dev/null|head -n1|cut -d'/' -f1 )
  214. fi
  215. else
  216. echo "[WARNING] Le dossier de telechargement n'existe pas ($DOWNLOAD_DIR)."
  217. [ $UPGRADE -eq 1 ] && exit 1
  218. fi
  219. fi
  220.  
  221. if [ $UPGRADE -eq 1 ]
  222. then
  223. cd "$DOWNLOAD_DIR"
  224. default_src="$( pwd )/$DOWNLOAD_FILE_DIR"
  225. src=""
  226. while [ ! -n "$src" ]
  227. do
  228. echo -n "Source de la nouvelle version [$default_src] : "
  229. read a
  230. if [ -n "$a" ]
  231. then
  232. src="$a"
  233. else
  234. src="$default_src"
  235. fi
  236. if [ ! -d "$src" ]
  237. then
  238. echo -n "[WARNING] Le dossier $src n'existe pas."
  239. src=""
  240. [ ! -n "$a" ] && default_src=""
  241. fi
  242. if [ ! -x "$src/bin/installto.sh" ]
  243. then
  244. echo -n "[WARNING] Le script bin/installto.sh n'est pas présent dans ce répertoire source."
  245. src=""
  246. [ ! -n "$a" ] && default_src=""
  247. fi
  248. done
  249. echo "Source : $src"
  250.  
  251. cd "$RC_HOME/../"
  252. from=""
  253. while [ ! -n "$from" ]
  254. do
  255. echo -n "Installation source [$RC_HOME] : "
  256. read a
  257. if [ -n "$a" ]
  258. then
  259. if [ -d "$a" ]
  260. then
  261. from="$a"
  262. else
  263. from=""
  264. echo "[ERROR] $a n'est pas un dossier valide"
  265. fi
  266. else
  267. from="$RC_HOME"
  268. fi
  269. done
  270. echo "From : $from"
  271. default_to="$( pwd )/roundcubemail-$newest"
  272. to=""
  273. while [ ! -n "$to" ]
  274. do
  275. echo -n "Destination de la nouvelle installation [$default_to] : "
  276. read a
  277. if [ -n "$a" ]
  278. then
  279. to="$a"
  280. else
  281. to="$default_to"
  282. fi
  283. if [ -d "$to" ]
  284. then
  285. echo -n "[WARNING] Le dossier $to existe deja. Celui-ci va etre deplace dans $to.old. Confirmez-vous votre choix [Y/n] ? "
  286. read b
  287. if [ "$b" == "n" -o "$b" == "N" ]
  288. then
  289. to=""
  290. [ ! -n "$a" ] && default_to=""
  291. else
  292. echo "Deplace $to dans $to.old"
  293. mv "$to" "$to.old"
  294. fi
  295. fi
  296. done
  297. echo "To : $to"
  298. echo "Copie de $from dans $to"
  299. rsync -a --copy-dirlinks "$from/" "$to/"
  300.  
  301. echo "Mise a jour de l'installation $to a partir de $src : "
  302. cd "$src"
  303. ./bin/installto.sh "$to"
  304.  
  305. run_after_upgrade "$to"
  306.  
  307. if [ -L "$RC_HOME" ]
  308. then
  309. echo -n "Voulez-vous faire pointer le lien symbolique $RC_HOME vers cette nouvelle installation [Y/n] ? "
  310. read a
  311. if [ "$a" != "n" -a "$a" != "N" ]
  312. then
  313. echo " - Suppression de l'actuel lien $RC_HOME"
  314. rm -f $RC_HOME
  315. echo " - Creation du nouveau lien $RC_HOME vers $to"
  316. ln -s "$to" "$RC_HOME"
  317. fi
  318. fi
  319. else
  320.  
  321. tmp=`mktemp`
  322. echo "New RoundCude release" > $tmp
  323. echo "=====================" >> $tmp
  324. echo "Current : $current" >> $tmp
  325. echo "Newest : $newest" >> $tmp
  326. echo >> $tmp
  327. echo "Download URL : $DOWNLOAD_URL" >> $tmp
  328. [ -n "$DOWNLOAD_FILE" -a -f "$DOWNLOAD_FILE" ] && echo "Download file : $( pwd )/roundcubemail-$newest.tar.gz" >> $tmp
  329.  
  330.  
  331. M=0
  332. if [ -f $CACHE ]
  333. then
  334. if [ "`diff $tmp $CACHE`" != "" -o `find $CACHE -mtime +$SEND_EVERY_NB_DAY | wc -l` -eq 1 ]
  335. then
  336. [ $DEBUG -eq 1 ] && echo "Cache trop vieux ou modif => On envoi"
  337. M=1
  338. else
  339. [ $DEBUG -eq 1 ] && echo "Pas de modif et cache trop recent : pas d'envoi"
  340. fi
  341. else
  342. [ $DEBUG -eq 1 ] && echo "Pas encore de cache => On envoi"
  343. M=1
  344. fi
  345. if [ $DEBUG -eq 1 -a $M -eq 1 ]
  346. then
  347. echo "Mode debug : Pas d'envoi de mail"
  348. fi
  349. if [ $M -eq 1 -a $DEBUG -ne 1 ]
  350. then
  351. cat $tmp > $CACHE
  352. if [ -n "$NAME" ]
  353. then
  354. S="[$NAME] New RoundCude release"
  355. else
  356. S="New RoundCude release"
  357. fi
  358. cat $tmp | mail -s "$S : $newest" $MAIL
  359. [ $DEBUG -eq 1 ] && echo "Mail envoyé"
  360. fi
  361. rm -f $tmp
  362.  
  363. fi
  364. else
  365. [ $DEBUG -eq 1 ] && echo "Pas de changement de version"
  366. [ $NAGIOS -eq 1 ] && echo "OK : Lastest version installed" && exit 0
  367. fi