home *** CD-ROM | disk | FTP | other *** search
/ ftp.ac-grenoble.fr / 2015.02.ftp.ac-grenoble.fr.tar / ftp.ac-grenoble.fr / pub / slis / updates_4.0_rsync / slis_update-4.0 < prev    next >
Text File  |  2007-08-19  |  11KB  |  328 lines

  1. #!/bin/bash
  2. # This script is part of the SLIS Project initiated by the CARMI-Internet
  3. # (AcadΘmie de Grenoble - France 38).
  4. # Ce script fait partie du projet SLIS dΘmarrΘ par le CARMI-Internet
  5. # (AcadΘmie de Grenoble - France 38).
  6. #
  7. # SLIS : Serveur de communications Linux pour l'Internet Scolaire.
  8. # Copyright (C) 1998-2003 Bruno Bzeznik
  9. #
  10. #    This program is free software; you can redistribute it and/or modify
  11. #    it under the terms of the GNU General Public License as published by
  12. #    the Free Software Foundation; either version 2 of the License, or
  13. #    (at your option) any later version.
  14. #
  15. #    This program is distributed in the hope that it will be useful,
  16. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #    GNU General Public License for more details.
  19. #    You should have received a copy of the GNU General Public License
  20. #    along with this program (For example ./COPYING);
  21. #    if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  22. #    Cambridge, MA 02139, USA.
  23. #
  24. # Please send all comments and bug reports by electronic mail to:
  25. #   Bruno Bzeznik <Bruno@ac-grenoble.fr>
  26. # or to <slis@ac-grenoble.fr>
  27. #
  28. # Envoyez vos suggestions et reports de bugs par e-mail α
  29. #   Bruno Bzeznik <Bruno@ac-grenoble.fr>
  30. # ou α <slis@ac-grenoble.fr>
  31. #
  32. # Contact: Olivier.LeCam@crdp.ac-versailles.fr
  33. #
  34. # Comments: 
  35. # This script is run by the crontab once a night for upgrading the SLIS
  36. # The SLIS admin can also run it at any moment it through the interface
  37. # NB: The kernel upgrade can be done overnight only (bettween 0-6)
  38. # Updates:
  39. # olecam: Dec 13, 2005: script adapted to SLIS v4
  40. # olecam: Jan 5, 2006: messages resulting of a failed apt-get are now logged to help debugging
  41. # olecam: Jan 6, 2006: check against freezed updates (lock)
  42. # olecam: Jan 6, 2006: get debconf less curious
  43. # olecam: Feb 7, 2007: improved exception processing in blacklist updates
  44.  
  45. #
  46. # Various variables
  47. #
  48. LOCKFILE="/var/lock/slis/slis_update-4.0"
  49. LOGTAG="slis_update-4.0[$$]"
  50. SLIS_LIB="/usr/sbin/slis-sys.inc.bash"
  51. SCRIPT_STATUS_MSG="Script 'slis_update' version 4.0"
  52. SQUIDGUARD_DB_PATH="/var/lib/squidguard/db"
  53. SLIS_UPDATE_SQUIDGUARD="slis_update_squidguard"
  54. BLACKLISTS="academie adult agressif audio-video dangerous_material drogue forums \
  55.     gambling hacking mobile-phone phishing publicite radio redirector \
  56.     strict_redirector strong_redirector tricheur warez webmail"
  57. REBOOT_MANDOTORY=0
  58. ERRCODE=0
  59.  
  60. #
  61. # Load the SLIS library
  62. #
  63. if [ -f $SLIS_LIB ]
  64. then
  65.       . $SLIS_LIB
  66. else
  67.     MSG="Error: $SLIS_LIB not found."
  68.       /usr/bin/logger -t "$LOGTAG" "$MSG"
  69.       /bin/echo "$MSG"
  70.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: failed."
  71.       exit 1
  72. fi
  73.  
  74. #
  75. # Load the variables
  76. #
  77. load_config
  78.  
  79. if [ "$SLIM_MAX_AGE" = "" ]
  80. then
  81.         # This is the maximum number of seconds you think
  82.         # a monitor must leave before suspecting it to be
  83.         # freezed.
  84.         SLIM_MAX_AGE=36000
  85. fi
  86.  
  87. if [ "$SLIM_RETRY_TIME" = "" ]
  88. then
  89.         # This is the max number of time the script will try 
  90.         # to relock when already locked 
  91.         SLIM_RETRY_TIME=360
  92. fi
  93.  
  94. #/bin/echo "[$PATH]" |/usr/bin/logger -t "$LOGTAG"
  95.  
  96. #
  97. # Locking
  98. #
  99.  
  100. LOCK_OUTPUT=`/usr/bin/lockfile -1 -r 3 -l $SLIM_MAX_AGE -s 0 $LOCKFILE 2>&1`; LOCK_RC=$?
  101.  
  102. #
  103. # If unable to lock (recent lock file found)
  104. #
  105. if [ "$LOCK_RC" != "0" ]
  106. then
  107.         MSG="Error: Lock found: update already running!"
  108.         /usr/bin/logger -t "$LOGTAG" "$MSG"
  109.     /bin/echo "$MSG"
  110.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: failed."
  111.         exit 1
  112. fi
  113.  
  114. #
  115. # If a very old lock file is found
  116. #
  117. if [ "`/bin/echo $LOCK_OUTPUT |/bin/grep -i "forcing lock"`" != "" ]
  118. then
  119.         /usr/bin/logger -t "$LOGTAG" "Warning: forcing lock and killing possibly sleeping processes."
  120.  
  121.         # Perhaps some processes are freezed?
  122.         /usr/bin/killall apt-get 2>/dev/null
  123.     /bin/sleep 1
  124.     /usr/bin/killall dpkg-preconfigure 2>/dev/null
  125.     /bin/sleep 1
  126.  
  127.         # Try to lock another time while waiting for the previous one to finish
  128.         /usr/bin/lockfile -5 -r $SLIM_RETRY_TIME $LOCKFILE ; LOCK_RC=$?
  129.         if [ "$LOCK_RC" != "0" ]
  130.         then
  131.                 MSG="Error: second try for locking failed"
  132.                 /usr/bin/logger -t "$LOGTAG" "$MSG!"
  133.                 /bin/echo "$LOGTAG" |/usr/bin/mail "$SLISMASTER" -s "Update: $MSG: $HOSTNAME"
  134.         /bin/echo "$MSG"
  135.         /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: failed."
  136.                 exit 1
  137.         fi
  138. fi
  139.  
  140. /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: starting..."
  141. /usr/bin/logger -t "$LOGTAG" "=== PLEASE WAIT. DO NOT USE SLIS WHILE UPDATING ==="
  142.  
  143. #
  144. # Make updates
  145. #
  146. /usr/bin/logger -t "$LOGTAG" "APT: Updating package index files from the dpkg sources..."
  147. APT_RESULT=`/usr/bin/apt-get -qq update 2>&1`
  148. if [ $? != 0 ]
  149. then
  150.       /usr/bin/logger -t "$LOGTAG" "ERROR: apt-get update failed. See report below:"
  151.     /bin/echo -n "$APT_RESULT" |/usr/bin/logger -t "$LOGTAG"
  152.     ERRCODE=1
  153. else
  154.     #
  155.     # APT upgrade
  156.     #
  157.  
  158.     # Ensure that we only are asked "critical" questions
  159.     export DEBIAN_PRIORITY=critical
  160. #    export DEBIAN_FRONTEND=text
  161.     export DEBIAN_FRONTEND=teletype
  162. #    export DEBCONF_DEBUG=developer
  163.  
  164.     # To be even more nasty, use: 
  165.     #   DEBIAN_FRONTEND=noninteractive
  166.     # which should ask *no* question (at least not through debconf)
  167.  
  168. #    # Get the list of upgrade candidates
  169. #    APT_RESULT=`apt-get -dsy upgrade 2>&1`
  170. #    CANDIDATES=`echo -n "$APT_RESULT" |grep Inst |awk '{print $2}' |tr '\n' ' '`
  171. #    
  172. #    if [ "$CANDIDATES" == "" ] ; then
  173. #        logger -t "$LOGTAG" "APT: Upgrading packages: none (system is up-to-date)"
  174. #    else
  175. #        logger -t "$LOGTAG" "APT: Upgrading packages: $CANDIDATES"
  176. #        logger -t "$LOGTAG" "APT: Be patient, this can take time!..."
  177. #        apt-get --force-yes -y -qq upgrade 2>&1 |logger -t "$LOGTAG"
  178. ##        APT_RESULT=`apt-get -y -qq upgrade 2>&1` 
  179. ##        if [ $? != 0 ]
  180. ##         then
  181. ##              logger -t "$LOGTAG" "ERROR: apt-get upgrade failed. See report below:"
  182. ##            echo -n "$APT_RESULT" |logger -t "$LOGTAG"
  183. ##         else
  184. ##             logger -t "$LOGTAG" "APT: Packages upgrading terminated."
  185. ##        fi
  186. #      fi
  187. #
  188. #    #
  189. #    # APT dist-upgrade (executed only when it's nighttime)
  190. #    #
  191. #    declare -i HOUR
  192. #    HOUR=`date +%k`
  193. #      if [ $HOUR -lt 6 ]
  194. #      then
  195. #            logger -t "$LOGTAG" "Well, it's nighttime, let's check for a kernel update..."
  196. #
  197. #        # Get the list of dist-upgrade candidates
  198. #            APT_RESULT=`apt-get -dsy dist-upgrade 2>&1`
  199. #            CANDIDATES=`echo -n "$APT_RESULT" |grep Inst |awk '{print $2}' |tr '\n' ' '`
  200. #
  201. #        if [ "$CANDIDATE" == "" ] ; then
  202. #            logger -t "$LOGTAG" "APT: Upgrading kernel: none (kernel is up-to-date)"
  203. #        else
  204. #            logger -t "$LOGTAG" "APT: Upgrading kernel: $CANDIDATE"
  205. #            APT_RESULT=`apt-get -y -qq dist-upgrade 2>&1`
  206. #                if [ $? != 0 ]
  207. #                then
  208. #                        logger -t "$LOGTAG" "ERROR: apt-get dist-upgrade failed. See report below:"
  209. #                echo -n "$APT_RESULT" |logger -t "$LOGTAG"
  210. #                else
  211. #                logger -t "$LOGTAG" "APT: Kernel upgrading terminated."
  212. #                    logger -t "$LOGTAG" "KERNEL UPDATED. REBOOT PLANNED IN ONE MINUTE..."
  213. #                /sbin/shutdown -r +1
  214. #            fi
  215. #            fi
  216. #    fi
  217.  
  218.         #
  219.         # APT dist-upgrade. Install also kernel-image updates, but if any wait nighttime to reboot.
  220.         #
  221.     
  222.     # Get the list of dist-upgrade candidates
  223.     APT_RESULT=`/usr/bin/apt-get -dsy dist-upgrade 2>&1`
  224.     CANDIDATES=`/bin/echo -n "$APT_RESULT" |/bin/grep Inst |/usr/bin/awk '{print $2}' |/usr/bin/tr '\n' ' '`
  225.  
  226.     if [ "$CANDIDATES" == "" ] ; then
  227.         /usr/bin/logger -t "$LOGTAG" "APT: Upgrading packages: none (system is up-to-date)."
  228.     else
  229.         /usr/bin/logger -t "$LOGTAG" "APT: Upgrading packages: $CANDIDATES"
  230.                 /usr/bin/logger -t "$LOGTAG" "APT: Be patient, this may take a long time!..."
  231.         /usr/bin/apt-get --force-yes -y -qq dist-upgrade 2>&1 |/usr/bin/logger -t "$LOGTAG"
  232.         [ $? != 0 ] && ERRCODE=1
  233.         /usr/bin/logger -t "$LOGTAG" "APT: Packages upgrading terminated."
  234.         
  235.         if [ "$(/bin/echo "$CANDIDATES" |/bin/grep kernel-image)" != "" ]
  236.         then
  237.             # A kernel update has been installed - Reboot now if it's nighttime or
  238.             # do it later.
  239.             HOUR=`/bin/date +%k`
  240.                 if [ $HOUR -lt 6 ]
  241.                 then
  242.                 /usr/bin/logger -t "$LOGTAG" "Kernel updated: a reboot is mandotory after the SLIS update..."
  243.                 REBOOT_MANDOTORY=1
  244.             else
  245.                 /usr/bin/logger -t "$LOGTAG" "Kernel updated: the SLIS will reboot tonight..."
  246.                 /sbin/shutdown -r now |at 0100
  247.             fi
  248.         fi
  249.  
  250.                 if [ "$(/bin/echo "$CANDIDATES" |/bin/grep slis-update)" != "" ]
  251.                 then
  252.                         # slis-update has been upgraded - Reboot now if it's nighttime or
  253.                         # do it later.
  254.                         HOUR=`/bin/date +%k`
  255.                         if [ $HOUR -lt 6 ]
  256.                         then
  257.                                 /usr/bin/logger -t "$LOGTAG" "slis-update upgraded: a reboot is mandotory after the SLIS update..."
  258.                                 REBOOT_MANDOTORY=1
  259.                         else
  260.                                 /usr/bin/logger -t "$LOGTAG" "slis-update upgraded: the SLIS will reboot tonight..."
  261.                                 /sbin/shutdown -r 01:00 & 
  262.                         fi
  263.                 fi
  264.     fi
  265. fi
  266.  
  267. #
  268. # Update the squidGuard databases
  269. #
  270. HOUR=`/bin/date +%k`
  271. if [ -e $SQUIDGUARD_DB_PATH ] && [ $HOUR -lt 8 ]
  272. then
  273.     /usr/bin/logger -t "$LOGTAG" "Downloading the URL blacklist databases update..."
  274.     cd $SQUIDGUARD_DB_PATH
  275.     for blacklist in $BLACKLISTS
  276.     do
  277.         USE_OLD=0
  278.         /bin/mv -f $blacklist.tar.gz $blacklist.tar.gz.backup
  279.         /usr/bin/rsync --timeout=30 rsync://$RSYNC_HOST/$RSYNC_MODULE/$blacklist.tar.gz . >/dev/null 2>&1 
  280.         if [ "$?" = "0" ] && [ -f $blacklist.tar.gz ]
  281.         then
  282.             /bin/tar xfz $blacklist.tar.gz
  283.             RC=$?
  284.             if [ "$RC" != "0" ]
  285.             then
  286.                 /usr/bin/logger -t "$LOGTAG" "Error: '$blacklist' dabatase corrupted: keeping the old version."
  287.                 USE_OLD=1
  288.             fi
  289.         else
  290.             /usr/bin/logger -t "$LOGTAG" "Warning: '$blacklist' database update could not be transfered from the central update server: keeping the old version"
  291.             USE_OLD=1
  292.         fi
  293.         if [ "$USE_OLD" = "1" ]
  294.         then
  295.             rm -f $blacklist.tar.gz
  296.             /bin/mv -f $blacklist.tar.gz.backup $blacklist.tar.gz
  297.             if [ -f $blacklist.tar.gz ]
  298.             then
  299.                 /bin/tar xfz $blacklist.tar.gz
  300.                 [ "$?" != "0" ] && echo "FATAL: the backuped '$blacklist' database is also corrupted. Skipping..."
  301.             else
  302.                 echo "FATAL: '$blacklist' has no backup. Skipping..."
  303.             fi
  304.         else
  305.             /bin/rm -f $blacklist.tar.gz.backup
  306.         fi
  307.     done
  308.     /bin/rm -f *.backup
  309.  
  310.     /usr/bin/logger -t "$LOGTAG" "Compiling the URL blacklist databases (be patient!)..."
  311.     $SLIS_BINDIR/$SLIS_UPDATE_SQUIDGUARD >/dev/null 2>&1
  312. fi
  313.  
  314. #
  315. # Ending script
  316. #
  317. if [ "$REBOOT_MANDATORY" = "1" ]
  318. then
  319.     /sbin/shutdown -r +1
  320.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: successed. Reboot planned in one minute."
  321. else
  322.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: successed"
  323. fi
  324. /bin/rm -f $LOCKFILE
  325. exit $ERRCODE
  326.  
  327.