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 / lcslis_dom0_update-4.0 < prev    next >
Text File  |  2007-06-20  |  6KB  |  182 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, 2005: get debconf less curious
  43.  
  44. #
  45. # Various variables
  46. #
  47. LOCKFILE="/var/lock/lcslis_dom0_update-4.0"
  48. LOGTAG="lcslis_dom0_update-4.0[$$]"
  49. SCRIPT_STATUS_MSG="Script 'lcslis_dom0_update' version 4.0"
  50. REBOOT_MANDOTORY=0
  51.  
  52. if [ "$SLIM_MAX_AGE" = "" ]
  53. then
  54.         # This is the maximum number of seconds you think
  55.         # a monitor must leave before suspecting it to be
  56.         # freezed.
  57.         SLIM_MAX_AGE=36000
  58. fi
  59.  
  60. if [ "$SLIM_RETRY_TIME" = "" ]
  61. then
  62.         # This is the max number of time the script will try 
  63.         # to relock when already locked 
  64.         SLIM_RETRY_TIME=360
  65. fi
  66.  
  67. /bin/echo "[$PATH]" |/usr/bin/logger -t "$LOGTAG"
  68.  
  69. #
  70. # Locking
  71. #
  72. LOCK_OUTPUT=`/usr/bin/lockfile -1 -r 3 -l $SLIM_MAX_AGE -s 0 $LOCKFILE 2>&1`; LOCK_RC=$?
  73.  
  74. #
  75. # If unable to lock (recent lock file found)
  76. #
  77. if [ "$LOCK_RC" != "0" ]
  78. then
  79.         MSG="Error: Lock found: update already running!"
  80.         /usr/bin/logger -t "$LOGTAG" "$MSG"
  81.     /bin/echo "$MSG"
  82.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: failed."
  83.         exit 1
  84. fi
  85.  
  86. #
  87. # If a very old lock file is found
  88. #
  89. if [ "`/bin/echo $LOCK_OUTPUT |/bin/grep -i "forcing lock"`" != "" ]
  90. then
  91.         /usr/bin/logger -t "$LOGTAG" "Warning: forcing lock and killing possibly sleeping processes."
  92.  
  93.         # Perhaps some processes are freezed?
  94.         /usr/bin/killall apt-get 2>/dev/null
  95.     /bin/sleep 1
  96.     /usr/bin/killall dpkg-preconfigure 2>/dev/null
  97.     /bin/sleep 1
  98.  
  99.         # Try to lock another time while waiting for the previous one to finish
  100.         /usr/bin/lockfile -5 -r $SLIM_RETRY_TIME $LOCKFILE ; LOCK_RC=$?
  101.         if [ "$LOCK_RC" != "0" ]
  102.         then
  103.                 MSG="Error: second try for locking failed"
  104.                 /usr/bin/logger -t "$LOGTAG" "$MSG!"
  105.                 /bin/echo "$LOGTAG" |/usr/bin/mail "$SLISMASTER" -s "Update: $MSG: $HOSTNAME"
  106.         /bin/echo "$MSG"
  107.         /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: failed."
  108.                 exit 1
  109.         fi
  110. fi
  111.  
  112. /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: starting..."
  113. /usr/bin/logger -t "$LOGTAG" "=== PLEASE WAIT. DO NOT USE SLIS WHILE UPDATING ==="
  114.  
  115. #
  116. # Make updates
  117. #
  118. /usr/bin/logger -t "$LOGTAG" "APT: Updating package index files from the dpkg sources..."
  119. APT_RESULT=`/usr/bin/apt-get -qq update 2>&1`
  120. if [ $? != 0 ]
  121. then
  122.       /usr/bin/logger -t "$LOGTAG" "ERROR: apt-get update failed. See report below:"
  123.     /bin/echo -n "$APT_RESULT" |/usr/bin/logger -t "$LOGTAG"
  124. else
  125.     #
  126.     # APT upgrade
  127.     #
  128.  
  129.     # Ensure that we only are asked "critical" questions
  130.     export DEBIAN_PRIORITY=critical
  131.     export DEBIAN_FRONTEND=teletype
  132.  
  133.     # To be even more nasty, use: 
  134.     #   DEBIAN_FRONTEND=noninteractive
  135.     # which should ask *no* question (at least not through debconf)
  136.  
  137.         #
  138.         # APT dist-upgrade. Install also kernel-image updates, but if any wait nighttime to reboot.
  139.         #
  140.     
  141.     # Get the list of dist-upgrade candidates
  142.     APT_RESULT=`/usr/bin/apt-get -dsy dist-upgrade 2>&1`
  143.     CANDIDATES=`/bin/echo -n "$APT_RESULT" |/bin/grep Inst |/usr/bin/awk '{print $2}' |/usr/bin/tr '\n' ' '`
  144.  
  145.     if [ "$CANDIDATES" == "" ] ; then
  146.         /usr/bin/logger -t "$LOGTAG" "APT: Upgrading packages: none (system is up-to-date)."
  147.     else
  148.         /usr/bin/logger -t "$LOGTAG" "APT: Upgrading packages: $CANDIDATES"
  149.                 /usr/bin/logger -t "$LOGTAG" "APT: Be patient, this may take a long time!..."
  150.         /usr/bin/apt-get --force-yes -y -qq dist-upgrade 2>&1 |/usr/bin/logger -t "$LOGTAG"
  151.         /usr/bin/logger -t "$LOGTAG" "APT: Packages upgrading terminated."
  152.  
  153.         if [ "$(/bin/echo \"$CANDIDATES\" |/bin/grep \"kernel-image\")" != "" ]
  154.         then
  155.             # A kernel update has been installed - Reboot now if it's nighttime or
  156.             # do it later.
  157.             HOUR=`/bin/date +%k`
  158.                 if [ $HOUR -lt 6 ]
  159.                 then
  160.                 /usr/bin/logger -t "$LOGTAG" "Kernel updated: a reboot is mandotory after the SLIS update..."
  161.                 REBOOT_MANDOTORY=1
  162.             else
  163.                 /usr/bin/logger -t "$LOGTAG" "Kernel updated: the SLIS will reboot tonight..."
  164.                 /sbin/shutdown -r now |at 0100
  165.             fi
  166.         fi
  167.     fi
  168. fi
  169.  
  170. #
  171. # Ending script
  172. #
  173. if [ "$REBOOT_MANDATORY" = "1" ]
  174. then
  175.     /sbin/shutdown -r +1
  176.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: successed. Reboot planned in one minute."
  177. else
  178.     /usr/bin/logger -t "$LOGTAG" "$SCRIPT_STATUS_MSG: successed"
  179. fi
  180. /bin/rm -f $LOCKFILE
  181.