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.1_rsync / slis_update-4.1 < prev    next >
Text File  |  2009-09-13  |  7KB  |  230 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. # Contact: Philippe Le Brouster <plb@ac-grenoble.fr>
  8. #
  9. # Comments: 
  10. # This script is run by the crontab once a night for upgrading the SLIS
  11. # The SLIS admin can also run it at any moment it through the interface
  12. # NB: The kernel upgrade can be done overnight only (bettween 0-6)
  13. # Updates:
  14. # olecam: Dec 13, 2005: script adapted to SLIS v4
  15. # olecam: Jan  5, 2006: messages resulting of a failed apt-get are now logged to help debugging
  16. # olecam: Jan  6, 2006: check against freezed updates (lock)
  17. # olecam: Jan  6, 2006: get debconf less curious
  18. # olecam: Feb  7, 2007: improved exception processing in blacklist updates
  19. # plb:    Jul 30, 2008: adaptation to SLIS 4.1
  20. # plb:    sep  4, 2008: some fixes according to buxy advice.
  21.  
  22. # TODO : Add 'apt-get autoremove' for lenny.
  23. # TODO : Add 'apt-get clean' to not keep useless debian package in the dpkg cache.
  24.  
  25. #
  26. # Various variables
  27. #
  28. LOCKFILE="/var/lock/slis/slis_update-4.1"
  29. LOGTAG="slis_update-4.1[$$]"
  30. SLIS_LIB="/usr/share/slis/slis-common.sh"
  31. SCRIPT_STATUS_MSG="Script 'slis_update' version 4.1"
  32. SQUIDGUARD_DB_PATH="/var/lib/squidguard/db"
  33. SLIS_UPDATE_SQUIDGUARD="slis_update_squidguard"
  34. BLACKLISTS="academie adult agressif audio-video dangerous_material drogue forums \
  35.     gambling hacking mobile-phone phishing publicite radio redirector \
  36.     strict_redirector strong_redirector tricheur warez webmail"
  37. REBOOT_MANDATORY=0
  38. ERRCODE=0
  39.  
  40. write_to_log()
  41. {
  42.     logger -t "$LOGTAG" $*
  43. }
  44.  
  45. send_to_slismaster()
  46. {
  47.     echo "$*" | mail "$SLISMASTER" -s "`hostname` : Update: $LOGTAG"
  48. }
  49.  
  50. exit_this_update()
  51. {
  52.     write_to_log $*
  53.     send_to_slismaster $*
  54.     write_to_log "$SCRIPT_STATUS_MSG: failed."
  55.     exit 1
  56. }
  57.  
  58. #
  59. # Load the SLIS library
  60. #
  61. if [ -f $SLIS_LIB ]; then
  62.     . $SLIS_LIB
  63. else
  64.     exit_this_update "Error: $SLIS_LIB not found."
  65. fi
  66.  
  67. #
  68. # Load the variables
  69. #
  70. load_slis_config
  71.  
  72. # This is the maximum number of seconds you think
  73. # a monitor must leave before suspecting it to be
  74. # freezed.
  75. [ "$SLIM_MAX_AGE" = "" ] &&  SLIM_MAX_AGE=36000
  76.  
  77. # This is the max number of time the script will try 
  78. # to relock when already locked 
  79. [ "$SLIM_RETRY_TIME" = "" ] &&  SLIM_RETRY_TIME=360
  80.  
  81. #
  82. # Locking
  83. #
  84. LOCK_OUTPUT=`lockfile -1 -r 3 -l $SLIM_MAX_AGE -s 0 $LOCKFILE 2>&1`; 
  85. [ "$?" != "0" ] && exit_this_update "Error: Lock found: update already running!"
  86.  
  87. #
  88. # If a very old lock file is found
  89. #
  90. if [ "`echo $LOCK_OUTPUT | grep -i "forcing lock"`" != "" ]; then
  91.     write_to_log "Warning: forcing lock and killing possibly sleeping processes."
  92.  
  93.     # Perhaps some processes are freezed?
  94.     killall apt-get 2>/dev/null
  95.     sleep 1
  96.     killall dpkg-preconfigure 2>/dev/null
  97.     sleep 1
  98.     
  99.     [ "`dpkg -C`" != "" ] && exit_this_update "Warning: Some packages not installed correctly";
  100.  
  101.     # Then if the lock is older than 10 days, remove the lockfile and lock again.
  102.     LOCKFILE_OLD=`find $LOCKFILE -nowarn -ctime +10 -print`
  103.     if [ "$LOCKFILE_OLD" != "" ]; then
  104.         write_to_log "Info: Very old lock found (more than 10 days), removing it."
  105.         rm -f $LOCKFILE
  106.     fi
  107.  
  108.     # Try to lock another time while waiting for the previous one to finish
  109.     lockfile -5 -r $SLIM_RETRY_TIME $LOCKFILE
  110.     [ "$?" != "0" ] && exit_this_update "Error: second try for locking failed"
  111. fi
  112.  
  113. write_to_log "$SCRIPT_STATUS_MSG: starting..."
  114. write_to_log "=== PLEASE WAIT. DO NOT USE SLIS WHILE UPDATING ==="
  115.  
  116. #
  117. # Make updates
  118. #
  119. write_to_log "APT: Updating package index files from the dpkg sources..."
  120. APT_RESULT=`apt-get -qq update 2>&1`
  121. if [ $? != 0 ]; then
  122.       write_to_log "ERROR: apt-get update failed. See report below:"
  123.     echo -n "$APT_RESULT" | logger -t "$LOGTAG"
  124.     ERRCODE=1
  125. else
  126.     #
  127.     # APT upgrade
  128.     #
  129.  
  130.     # Ensure that no question will be asked for
  131.     export DEBIAN_FRONTEND=noninteractive
  132.  
  133.         DPKG_OPTS='-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold'
  134.  
  135.         # Get the list of dist-upgrade candidates
  136.     APT_RESULT=`apt-get -dsy dist-upgrade 2>&1`
  137.     CANDIDATES=`echo -n "$APT_RESULT" | grep Inst | awk '{print $2}' | tr '\n' ' '`
  138.  
  139.     if [ "$CANDIDATES" = "" ]; then
  140.         write_to_log "APT: Upgrading packages: none (system is up-to-date)."
  141.     else
  142.         write_to_log "APT: Upgrading packages: $CANDIDATES"
  143.         write_to_log "APT: Be patient, this may take a long time!..."
  144.         RES=$(tempfile)
  145.         apt-get $DPKG_OPTS --force-yes -y -qq dist-upgrade 2>&1 > $RES
  146.         ERRCODE=$?
  147.         cat $RES |/usr/bin/logger -t "$LOGTAG"
  148.         rm $RES
  149.         
  150.         if [ $ERRCODE != 0 ]; then
  151.             exit_this_update "Error: apt-get failed when dist-upgrading. See /var/log/syslog"
  152.         fi
  153.         
  154.         write_to_log "APT: Packages upgrading terminated."
  155.         
  156.         if [ "$(echo "$CANDIDATES" | grep linux-image)" != "" ]; then
  157.             # Reboot the machine if needed
  158.             if [ "$SLIS_VIRTUALIZATION" = "none" ]; then
  159.                 write_to_log "slis-update upgraded: a reboot is MANDATORY after the SLIS update..."
  160.                     REBOOT_MANDATORY=1
  161.                 fi
  162.         fi
  163.  
  164.                NEW_KERNEL=`echo \"$CANDIDATES\" | grep linux-image `
  165.            if [ "$NEW_KERNEL" != "" ]; then
  166.                 # Reboot the machine if needed
  167.                 if [ "$SLIS_VIRTUALIZATION" = "none" ]; then
  168.                     write_to_log "slis-update upgraded: a reboot is MANDATORY after the SLIS update..."
  169.                     REBOOT_MANDATORY=1
  170.                 fi
  171.         fi
  172.     fi
  173. fi
  174.  
  175. #
  176. # Update the squidGuard databases
  177. #
  178. HOUR=`date +%k`
  179. if [ -e $SQUIDGUARD_DB_PATH ] && [ $HOUR -lt 8 ]; then
  180.     write_to_log  "Downloading the URL blacklist databases update..."
  181.     cd $SQUIDGUARD_DB_PATH
  182.     for blacklist in $BLACKLISTS; do
  183.         USE_OLD=0
  184.         cp -a $blacklist.tar.gz $blacklist.tar.gz.backup
  185.         rsync --timeout=30 rsync://$RSYNC_HOST/$RSYNC_MODULE/$blacklist.tar.gz . >/dev/null 2>&1 
  186.         if [ "$?" = "0" ] && [ $blacklist.tar.gz -nt $blacklist.tar.gz.backup.gz ]; then
  187.             tar xfz $blacklist.tar.gz
  188.             RC=$?
  189.             if [ "$RC" != "0" ]; then
  190.                 write_to_log "Error: '$blacklist' dabatase corrupted: keeping the old version."
  191.                 USE_OLD=1
  192.             fi
  193.         else
  194.             write_to_log "Warning: '$blacklist' database update could not be transfered from the central update server: keeping the old version"
  195.             USE_OLD=1
  196.         fi
  197.         if [ "$USE_OLD" = "1" ]; then
  198.             rm -f $blacklist.tar.gz
  199.             mv -f $blacklist.tar.gz.backup $blacklist.tar.gz
  200.             if [ -f $blacklist.tar.gz ]; then
  201.                 tar xfz $blacklist.tar.gz
  202.                 [ "$?" != "0" ] && echo "FATAL: the backuped '$blacklist' database is also corrupted. Skipping..."
  203.             else
  204.                 echo "FATAL: '$blacklist' has no backup. Skipping..."
  205.             fi
  206.         else
  207.             rm -f $blacklist.tar.gz.backup
  208.         fi
  209.     done
  210.     rm -f *.backup
  211.  
  212.     write_to_log "Compiling the URL blacklist databases (be patient!)..."
  213.     $SLIS_UPDATE_SQUIDGUARD >/dev/null 2>&1
  214. fi
  215.  
  216. #
  217. # Ending script
  218. #
  219. if [ "$REBOOT_MANDATORY" = "1" ]; then
  220.     echo 'shutdown -r +1' | at 4am tomorrow
  221.     write_to_log "$SCRIPT_STATUS_MSG: successed. Reboot planned tomorrow at 4 a.m"
  222. else
  223.     write_to_log "$SCRIPT_STATUS_MSG: successed"
  224. fi
  225.  
  226. rm -f $LOCKFILE
  227. exit $ERRCODE
  228.  
  229.